2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
83 struct ctl_softc *control_softc = NULL;
86 * Size and alignment macros needed for Copan-specific HA hardware. These
87 * can go away when the HA code is re-written, and uses busdma for any
90 #define CTL_ALIGN_8B(target, source, type) \
91 if (((uint32_t)source & 0x7) != 0) \
92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
94 target = (type)source;
96 #define CTL_SIZE_8B(target, size) \
97 if ((size & 0x7) != 0) \
98 target = size + (0x8 - (size & 0x7)); \
102 #define CTL_ALIGN_8B_MARGIN 16
105 * Template mode pages.
109 * Note that these are default values only. The actual values will be
110 * filled in when the user does a mode sense.
112 static struct copan_power_subpage power_page_default = {
113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
114 /*subpage*/ PWR_SUBPAGE_CODE,
115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
117 /*page_version*/ PWR_VERSION,
119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 static struct copan_power_subpage power_page_changeable = {
126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
127 /*subpage*/ PWR_SUBPAGE_CODE,
128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
132 /* max_active_luns*/ 0,
133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 static struct copan_aps_subpage aps_page_default = {
139 APS_PAGE_CODE | SMPH_SPF, //page_code
140 APS_SUBPAGE_CODE, //subpage
141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
143 APS_VERSION, //page_version
145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0} //reserved
150 static struct copan_aps_subpage aps_page_changeable = {
151 APS_PAGE_CODE | SMPH_SPF, //page_code
152 APS_SUBPAGE_CODE, //subpage
153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0} //reserved
162 static struct copan_debugconf_subpage debugconf_page_default = {
163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
164 DBGCNF_SUBPAGE_CODE, /* subpage */
165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
167 DBGCNF_VERSION, /* page_version */
168 {CTL_TIME_IO_DEFAULT_SECS>>8,
169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
172 static struct copan_debugconf_subpage debugconf_page_changeable = {
173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
174 DBGCNF_SUBPAGE_CODE, /* subpage */
175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
177 0, /* page_version */
178 {0xff,0xff}, /* ctl_time_io_secs */
181 static struct scsi_format_page format_page_default = {
182 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
183 /*page_length*/sizeof(struct scsi_format_page) - 2,
184 /*tracks_per_zone*/ {0, 0},
185 /*alt_sectors_per_zone*/ {0, 0},
186 /*alt_tracks_per_zone*/ {0, 0},
187 /*alt_tracks_per_lun*/ {0, 0},
188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
190 /*bytes_per_sector*/ {0, 0},
191 /*interleave*/ {0, 0},
192 /*track_skew*/ {0, 0},
193 /*cylinder_skew*/ {0, 0},
195 /*reserved*/ {0, 0, 0}
198 static struct scsi_format_page format_page_changeable = {
199 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
200 /*page_length*/sizeof(struct scsi_format_page) - 2,
201 /*tracks_per_zone*/ {0, 0},
202 /*alt_sectors_per_zone*/ {0, 0},
203 /*alt_tracks_per_zone*/ {0, 0},
204 /*alt_tracks_per_lun*/ {0, 0},
205 /*sectors_per_track*/ {0, 0},
206 /*bytes_per_sector*/ {0, 0},
207 /*interleave*/ {0, 0},
208 /*track_skew*/ {0, 0},
209 /*cylinder_skew*/ {0, 0},
211 /*reserved*/ {0, 0, 0}
214 static struct scsi_rigid_disk_page rigid_disk_page_default = {
215 /*page_code*/SMS_RIGID_DISK_PAGE,
216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
217 /*cylinders*/ {0, 0, 0},
218 /*heads*/ CTL_DEFAULT_HEADS,
219 /*start_write_precomp*/ {0, 0, 0},
220 /*start_reduced_current*/ {0, 0, 0},
221 /*step_rate*/ {0, 0},
222 /*landing_zone_cylinder*/ {0, 0, 0},
223 /*rpl*/ SRDP_RPL_DISABLED,
224 /*rotational_offset*/ 0,
226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
227 CTL_DEFAULT_ROTATION_RATE & 0xff},
231 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
232 /*page_code*/SMS_RIGID_DISK_PAGE,
233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
234 /*cylinders*/ {0, 0, 0},
236 /*start_write_precomp*/ {0, 0, 0},
237 /*start_reduced_current*/ {0, 0, 0},
238 /*step_rate*/ {0, 0},
239 /*landing_zone_cylinder*/ {0, 0, 0},
241 /*rotational_offset*/ 0,
243 /*rotation_rate*/ {0, 0},
247 static struct scsi_caching_page caching_page_default = {
248 /*page_code*/SMS_CACHING_PAGE,
249 /*page_length*/sizeof(struct scsi_caching_page) - 2,
250 /*flags1*/ SCP_DISC | SCP_WCE,
252 /*disable_pf_transfer_len*/ {0xff, 0xff},
253 /*min_prefetch*/ {0, 0},
254 /*max_prefetch*/ {0xff, 0xff},
255 /*max_pf_ceiling*/ {0xff, 0xff},
257 /*cache_segments*/ 0,
258 /*cache_seg_size*/ {0, 0},
260 /*non_cache_seg_size*/ {0, 0, 0}
263 static struct scsi_caching_page caching_page_changeable = {
264 /*page_code*/SMS_CACHING_PAGE,
265 /*page_length*/sizeof(struct scsi_caching_page) - 2,
266 /*flags1*/ SCP_WCE | SCP_RCD,
268 /*disable_pf_transfer_len*/ {0, 0},
269 /*min_prefetch*/ {0, 0},
270 /*max_prefetch*/ {0, 0},
271 /*max_pf_ceiling*/ {0, 0},
273 /*cache_segments*/ 0,
274 /*cache_seg_size*/ {0, 0},
276 /*non_cache_seg_size*/ {0, 0, 0}
279 static struct scsi_control_page control_page_default = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE,
281 /*page_length*/sizeof(struct scsi_control_page) - 2,
283 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
286 /*aen_holdoff_period*/{0, 0},
287 /*busy_timeout_period*/{0, 0},
288 /*extended_selftest_completion_time*/{0, 0}
291 static struct scsi_control_page control_page_changeable = {
292 /*page_code*/SMS_CONTROL_MODE_PAGE,
293 /*page_length*/sizeof(struct scsi_control_page) - 2,
295 /*queue_flags*/SCP_QUEUE_ALG_MASK,
296 /*eca_and_aen*/SCP_SWP,
298 /*aen_holdoff_period*/{0, 0},
299 /*busy_timeout_period*/{0, 0},
300 /*extended_selftest_completion_time*/{0, 0}
305 * XXX KDM move these into the softc.
307 static int rcv_sync_msg;
308 static int persis_offset;
309 static uint8_t ctl_pause_rtr;
310 static int ctl_is_single = 1;
311 static int index_to_aps_page;
313 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
314 static int worker_threads = -1;
315 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
316 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
317 &worker_threads, 1, "Number of worker threads");
318 static int verbose = 0;
319 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
320 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
321 &verbose, 0, "Show SCSI errors returned to initiator");
324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
325 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
326 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
327 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
329 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
331 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
333 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
334 static int ctl_init(void);
335 void ctl_shutdown(void);
336 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
337 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
338 static void ctl_ioctl_online(void *arg);
339 static void ctl_ioctl_offline(void *arg);
340 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
341 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
342 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
343 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
344 static int ctl_ioctl_submit_wait(union ctl_io *io);
345 static void ctl_ioctl_datamove(union ctl_io *io);
346 static void ctl_ioctl_done(union ctl_io *io);
347 static void ctl_ioctl_hard_startstop_callback(void *arg,
348 struct cfi_metatask *metatask);
349 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
350 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
351 struct ctl_ooa *ooa_hdr,
352 struct ctl_ooa_entry *kern_entries);
353 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
355 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
356 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
358 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
359 uint32_t targ_target, uint32_t targ_lun,
361 static void ctl_kfree_io(union ctl_io *io);
363 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
364 struct ctl_be_lun *be_lun, struct ctl_id target_id);
365 static int ctl_free_lun(struct ctl_lun *lun);
366 static void ctl_create_lun(struct ctl_be_lun *be_lun);
368 static void ctl_failover_change_pages(struct ctl_softc *softc,
369 struct ctl_scsiio *ctsio, int master);
372 static int ctl_do_mode_select(union ctl_io *io);
373 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
374 uint64_t res_key, uint64_t sa_res_key,
375 uint8_t type, uint32_t residx,
376 struct ctl_scsiio *ctsio,
377 struct scsi_per_res_out *cdb,
378 struct scsi_per_res_out_parms* param);
379 static void ctl_pro_preempt_other(struct ctl_lun *lun,
380 union ctl_ha_msg *msg);
381 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
382 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
383 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
384 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
386 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
389 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
391 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
392 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
393 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
394 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
395 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
396 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
397 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
398 union ctl_io *pending_io, union ctl_io *ooa_io);
399 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
400 union ctl_io *starting_io);
401 static int ctl_check_blocked(struct ctl_lun *lun);
402 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
404 const struct ctl_cmd_entry *entry,
405 struct ctl_scsiio *ctsio);
406 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
407 static void ctl_failover(void);
408 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
409 struct ctl_scsiio *ctsio);
410 static int ctl_scsiio(struct ctl_scsiio *ctsio);
412 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
413 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
414 ctl_ua_type ua_type);
415 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
416 ctl_ua_type ua_type);
417 static int ctl_abort_task(union ctl_io *io);
418 static int ctl_abort_task_set(union ctl_io *io);
419 static int ctl_i_t_nexus_reset(union ctl_io *io);
420 static void ctl_run_task(union ctl_io *io);
422 static void ctl_datamove_timer_wakeup(void *arg);
423 static void ctl_done_timer_wakeup(void *arg);
424 #endif /* CTL_IO_DELAY */
426 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
427 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
428 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
429 static void ctl_datamove_remote_write(union ctl_io *io);
430 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
431 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
432 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
433 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
434 ctl_ha_dt_cb callback);
435 static void ctl_datamove_remote_read(union ctl_io *io);
436 static void ctl_datamove_remote(union ctl_io *io);
437 static int ctl_process_done(union ctl_io *io);
438 static void ctl_lun_thread(void *arg);
439 static void ctl_work_thread(void *arg);
440 static void ctl_enqueue_incoming(union ctl_io *io);
441 static void ctl_enqueue_rtr(union ctl_io *io);
442 static void ctl_enqueue_done(union ctl_io *io);
443 static void ctl_enqueue_isc(union ctl_io *io);
444 static const struct ctl_cmd_entry *
445 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
446 static const struct ctl_cmd_entry *
447 ctl_validate_command(struct ctl_scsiio *ctsio);
448 static int ctl_cmd_applicable(uint8_t lun_type,
449 const struct ctl_cmd_entry *entry);
452 * Load the serialization table. This isn't very pretty, but is probably
453 * the easiest way to do it.
455 #include "ctl_ser_table.c"
458 * We only need to define open, close and ioctl routines for this driver.
460 static struct cdevsw ctl_cdevsw = {
461 .d_version = D_VERSION,
464 .d_close = ctl_close,
465 .d_ioctl = ctl_ioctl,
470 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
471 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
473 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
475 static moduledata_t ctl_moduledata = {
477 ctl_module_event_handler,
481 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
482 MODULE_VERSION(ctl, 1);
484 static struct ctl_frontend ioctl_frontend =
490 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
491 union ctl_ha_msg *msg_info)
493 struct ctl_scsiio *ctsio;
495 if (msg_info->hdr.original_sc == NULL) {
496 printf("%s: original_sc == NULL!\n", __func__);
497 /* XXX KDM now what? */
501 ctsio = &msg_info->hdr.original_sc->scsiio;
502 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
503 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
504 ctsio->io_hdr.status = msg_info->hdr.status;
505 ctsio->scsi_status = msg_info->scsi.scsi_status;
506 ctsio->sense_len = msg_info->scsi.sense_len;
507 ctsio->sense_residual = msg_info->scsi.sense_residual;
508 ctsio->residual = msg_info->scsi.residual;
509 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
510 sizeof(ctsio->sense_data));
511 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
512 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
513 ctl_enqueue_isc((union ctl_io *)ctsio);
517 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
518 union ctl_ha_msg *msg_info)
520 struct ctl_scsiio *ctsio;
522 if (msg_info->hdr.serializing_sc == NULL) {
523 printf("%s: serializing_sc == NULL!\n", __func__);
524 /* XXX KDM now what? */
528 ctsio = &msg_info->hdr.serializing_sc->scsiio;
531 * Attempt to catch the situation where an I/O has
532 * been freed, and we're using it again.
534 if (ctsio->io_hdr.io_type == 0xff) {
535 union ctl_io *tmp_io;
536 tmp_io = (union ctl_io *)ctsio;
537 printf("%s: %p use after free!\n", __func__,
539 printf("%s: type %d msg %d cdb %x iptl: "
540 "%d:%d:%d:%d tag 0x%04x "
541 "flag %#x status %x\n",
543 tmp_io->io_hdr.io_type,
544 tmp_io->io_hdr.msg_type,
545 tmp_io->scsiio.cdb[0],
546 tmp_io->io_hdr.nexus.initid.id,
547 tmp_io->io_hdr.nexus.targ_port,
548 tmp_io->io_hdr.nexus.targ_target.id,
549 tmp_io->io_hdr.nexus.targ_lun,
550 (tmp_io->io_hdr.io_type ==
552 tmp_io->taskio.tag_num :
553 tmp_io->scsiio.tag_num,
554 tmp_io->io_hdr.flags,
555 tmp_io->io_hdr.status);
558 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
559 ctl_enqueue_isc((union ctl_io *)ctsio);
563 * ISC (Inter Shelf Communication) event handler. Events from the HA
564 * subsystem come in here.
567 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
569 struct ctl_softc *ctl_softc;
571 struct ctl_prio *presio;
572 ctl_ha_status isc_status;
574 ctl_softc = control_softc;
579 printf("CTL: Isc Msg event %d\n", event);
581 if (event == CTL_HA_EVT_MSG_RECV) {
582 union ctl_ha_msg msg_info;
584 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
585 sizeof(msg_info), /*wait*/ 0);
587 printf("CTL: msg_type %d\n", msg_info.msg_type);
589 if (isc_status != 0) {
590 printf("Error receiving message, status = %d\n",
595 switch (msg_info.hdr.msg_type) {
596 case CTL_MSG_SERIALIZE:
598 printf("Serialize\n");
600 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
602 printf("ctl_isc_event_handler: can't allocate "
605 /* Need to set busy and send msg back */
606 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
607 msg_info.hdr.status = CTL_SCSI_ERROR;
608 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
609 msg_info.scsi.sense_len = 0;
610 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
611 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
616 // populate ctsio from msg_info
617 io->io_hdr.io_type = CTL_IO_SCSI;
618 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
619 io->io_hdr.original_sc = msg_info.hdr.original_sc;
621 printf("pOrig %x\n", (int)msg_info.original_sc);
623 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
626 * If we're in serialization-only mode, we don't
627 * want to go through full done processing. Thus
630 * XXX KDM add another flag that is more specific.
632 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
633 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
634 io->io_hdr.nexus = msg_info.hdr.nexus;
636 printf("targ %d, port %d, iid %d, lun %d\n",
637 io->io_hdr.nexus.targ_target.id,
638 io->io_hdr.nexus.targ_port,
639 io->io_hdr.nexus.initid.id,
640 io->io_hdr.nexus.targ_lun);
642 io->scsiio.tag_num = msg_info.scsi.tag_num;
643 io->scsiio.tag_type = msg_info.scsi.tag_type;
644 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
646 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
647 const struct ctl_cmd_entry *entry;
649 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
650 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
652 entry->flags & CTL_FLAG_DATA_MASK;
657 /* Performed on the Originating SC, XFER mode only */
658 case CTL_MSG_DATAMOVE: {
659 struct ctl_sg_entry *sgl;
662 io = msg_info.hdr.original_sc;
664 printf("%s: original_sc == NULL!\n", __func__);
665 /* XXX KDM do something here */
668 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
669 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
671 * Keep track of this, we need to send it back over
672 * when the datamove is complete.
674 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
676 if (msg_info.dt.sg_sequence == 0) {
678 * XXX KDM we use the preallocated S/G list
679 * here, but we'll need to change this to
680 * dynamic allocation if we need larger S/G
683 if (msg_info.dt.kern_sg_entries >
684 sizeof(io->io_hdr.remote_sglist) /
685 sizeof(io->io_hdr.remote_sglist[0])) {
686 printf("%s: number of S/G entries "
687 "needed %u > allocated num %zd\n",
689 msg_info.dt.kern_sg_entries,
690 sizeof(io->io_hdr.remote_sglist)/
691 sizeof(io->io_hdr.remote_sglist[0]));
694 * XXX KDM send a message back to
695 * the other side to shut down the
696 * DMA. The error will come back
697 * through via the normal channel.
701 sgl = io->io_hdr.remote_sglist;
703 sizeof(io->io_hdr.remote_sglist));
705 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
707 io->scsiio.kern_sg_entries =
708 msg_info.dt.kern_sg_entries;
709 io->scsiio.rem_sg_entries =
710 msg_info.dt.kern_sg_entries;
711 io->scsiio.kern_data_len =
712 msg_info.dt.kern_data_len;
713 io->scsiio.kern_total_len =
714 msg_info.dt.kern_total_len;
715 io->scsiio.kern_data_resid =
716 msg_info.dt.kern_data_resid;
717 io->scsiio.kern_rel_offset =
718 msg_info.dt.kern_rel_offset;
720 * Clear out per-DMA flags.
722 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
724 * Add per-DMA flags that are set for this
725 * particular DMA request.
727 io->io_hdr.flags |= msg_info.dt.flags &
730 sgl = (struct ctl_sg_entry *)
731 io->scsiio.kern_data_ptr;
733 for (i = msg_info.dt.sent_sg_entries, j = 0;
734 i < (msg_info.dt.sent_sg_entries +
735 msg_info.dt.cur_sg_entries); i++, j++) {
736 sgl[i].addr = msg_info.dt.sg_list[j].addr;
737 sgl[i].len = msg_info.dt.sg_list[j].len;
740 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
742 msg_info.dt.sg_list[j].addr,
743 msg_info.dt.sg_list[j].len,
744 sgl[i].addr, sgl[i].len, j, i);
748 memcpy(&sgl[msg_info.dt.sent_sg_entries],
750 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
754 * If this is the last piece of the I/O, we've got
755 * the full S/G list. Queue processing in the thread.
756 * Otherwise wait for the next piece.
758 if (msg_info.dt.sg_last != 0)
762 /* Performed on the Serializing (primary) SC, XFER mode only */
763 case CTL_MSG_DATAMOVE_DONE: {
764 if (msg_info.hdr.serializing_sc == NULL) {
765 printf("%s: serializing_sc == NULL!\n",
767 /* XXX KDM now what? */
771 * We grab the sense information here in case
772 * there was a failure, so we can return status
773 * back to the initiator.
775 io = msg_info.hdr.serializing_sc;
776 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
777 io->io_hdr.status = msg_info.hdr.status;
778 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
779 io->scsiio.sense_len = msg_info.scsi.sense_len;
780 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
781 io->io_hdr.port_status = msg_info.scsi.fetd_status;
782 io->scsiio.residual = msg_info.scsi.residual;
783 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
784 sizeof(io->scsiio.sense_data));
789 /* Preformed on Originating SC, SER_ONLY mode */
791 io = msg_info.hdr.original_sc;
793 printf("%s: Major Bummer\n", __func__);
797 printf("pOrig %x\n",(int) ctsio);
800 io->io_hdr.msg_type = CTL_MSG_R2R;
801 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
806 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
808 * Performed on the Originating (i.e. secondary) SC in XFER
811 case CTL_MSG_FINISH_IO:
812 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
813 ctl_isc_handler_finish_xfer(ctl_softc,
816 ctl_isc_handler_finish_ser_only(ctl_softc,
820 /* Preformed on Originating SC */
821 case CTL_MSG_BAD_JUJU:
822 io = msg_info.hdr.original_sc;
824 printf("%s: Bad JUJU!, original_sc is NULL!\n",
828 ctl_copy_sense_data(&msg_info, io);
830 * IO should have already been cleaned up on other
831 * SC so clear this flag so we won't send a message
832 * back to finish the IO there.
834 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
835 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
837 /* io = msg_info.hdr.serializing_sc; */
838 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
842 /* Handle resets sent from the other side */
843 case CTL_MSG_MANAGE_TASKS: {
844 struct ctl_taskio *taskio;
845 taskio = (struct ctl_taskio *)ctl_alloc_io(
846 (void *)ctl_softc->othersc_pool);
847 if (taskio == NULL) {
848 printf("ctl_isc_event_handler: can't allocate "
851 /* should I just call the proper reset func
855 ctl_zero_io((union ctl_io *)taskio);
856 taskio->io_hdr.io_type = CTL_IO_TASK;
857 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
858 taskio->io_hdr.nexus = msg_info.hdr.nexus;
859 taskio->task_action = msg_info.task.task_action;
860 taskio->tag_num = msg_info.task.tag_num;
861 taskio->tag_type = msg_info.task.tag_type;
863 taskio->io_hdr.start_time = time_uptime;
864 getbintime(&taskio->io_hdr.start_bt);
866 cs_prof_gettime(&taskio->io_hdr.start_ticks);
868 #endif /* CTL_TIME_IO */
869 ctl_run_task((union ctl_io *)taskio);
872 /* Persistent Reserve action which needs attention */
873 case CTL_MSG_PERS_ACTION:
874 presio = (struct ctl_prio *)ctl_alloc_io(
875 (void *)ctl_softc->othersc_pool);
876 if (presio == NULL) {
877 printf("ctl_isc_event_handler: can't allocate "
880 /* Need to set busy and send msg back */
883 ctl_zero_io((union ctl_io *)presio);
884 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
885 presio->pr_msg = msg_info.pr;
886 ctl_enqueue_isc((union ctl_io *)presio);
888 case CTL_MSG_SYNC_FE:
891 case CTL_MSG_APS_LOCK: {
892 // It's quicker to execute this then to
895 struct ctl_page_index *page_index;
896 struct copan_aps_subpage *current_sp;
899 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
900 lun = ctl_softc->ctl_luns[targ_lun];
901 mtx_lock(&lun->lun_lock);
902 page_index = &lun->mode_pages.index[index_to_aps_page];
903 current_sp = (struct copan_aps_subpage *)
904 (page_index->page_data +
905 (page_index->page_len * CTL_PAGE_CURRENT));
907 current_sp->lock_active = msg_info.aps.lock_flag;
908 mtx_unlock(&lun->lun_lock);
912 printf("How did I get here?\n");
914 } else if (event == CTL_HA_EVT_MSG_SENT) {
915 if (param != CTL_HA_STATUS_SUCCESS) {
916 printf("Bad status from ctl_ha_msg_send status %d\n",
920 } else if (event == CTL_HA_EVT_DISCONNECT) {
921 printf("CTL: Got a disconnect from Isc\n");
924 printf("ctl_isc_event_handler: Unknown event %d\n", event);
933 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
935 struct scsi_sense_data *sense;
937 sense = &dest->scsiio.sense_data;
938 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
939 dest->scsiio.scsi_status = src->scsi.scsi_status;
940 dest->scsiio.sense_len = src->scsi.sense_len;
941 dest->io_hdr.status = src->hdr.status;
947 struct ctl_softc *softc;
948 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
949 struct ctl_port *port;
951 int i, error, retval;
958 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
960 softc = control_softc;
962 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
965 softc->dev->si_drv1 = softc;
968 * By default, return a "bad LUN" peripheral qualifier for unknown
969 * LUNs. The user can override this default using the tunable or
970 * sysctl. See the comment in ctl_inquiry_std() for more details.
972 softc->inquiry_pq_no_lun = 1;
973 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
974 &softc->inquiry_pq_no_lun);
975 sysctl_ctx_init(&softc->sysctl_ctx);
976 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
977 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
978 CTLFLAG_RD, 0, "CAM Target Layer");
980 if (softc->sysctl_tree == NULL) {
981 printf("%s: unable to allocate sysctl tree\n", __func__);
982 destroy_dev(softc->dev);
983 free(control_softc, M_DEVBUF);
984 control_softc = NULL;
988 SYSCTL_ADD_INT(&softc->sysctl_ctx,
989 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
990 "inquiry_pq_no_lun", CTLFLAG_RW,
991 &softc->inquiry_pq_no_lun, 0,
992 "Report no lun possible for invalid LUNs");
994 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
995 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
996 softc->open_count = 0;
999 * Default to actually sending a SYNCHRONIZE CACHE command down to
1002 softc->flags = CTL_FLAG_REAL_SYNC;
1005 * In Copan's HA scheme, the "master" and "slave" roles are
1006 * figured out through the slot the controller is in. Although it
1007 * is an active/active system, someone has to be in charge.
1010 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1014 softc->flags |= CTL_FLAG_MASTER_SHELF;
1017 persis_offset = CTL_MAX_INITIATORS;
1020 * XXX KDM need to figure out where we want to get our target ID
1021 * and WWID. Is it different on each port?
1023 softc->target.id = 0;
1024 softc->target.wwid[0] = 0x12345678;
1025 softc->target.wwid[1] = 0x87654321;
1026 STAILQ_INIT(&softc->lun_list);
1027 STAILQ_INIT(&softc->pending_lun_queue);
1028 STAILQ_INIT(&softc->fe_list);
1029 STAILQ_INIT(&softc->port_list);
1030 STAILQ_INIT(&softc->be_list);
1031 STAILQ_INIT(&softc->io_pools);
1032 ctl_tpc_init(softc);
1034 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1035 &internal_pool)!= 0){
1036 printf("ctl: can't allocate %d entry internal pool, "
1037 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1041 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1042 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1043 printf("ctl: can't allocate %d entry emergency pool, "
1044 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1045 ctl_pool_free(internal_pool);
1049 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1052 printf("ctl: can't allocate %d entry other SC pool, "
1053 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1054 ctl_pool_free(internal_pool);
1055 ctl_pool_free(emergency_pool);
1059 softc->internal_pool = internal_pool;
1060 softc->emergency_pool = emergency_pool;
1061 softc->othersc_pool = other_pool;
1063 if (worker_threads <= 0)
1064 worker_threads = max(1, mp_ncpus / 4);
1065 if (worker_threads > CTL_MAX_THREADS)
1066 worker_threads = CTL_MAX_THREADS;
1068 for (i = 0; i < worker_threads; i++) {
1069 struct ctl_thread *thr = &softc->threads[i];
1071 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1072 thr->ctl_softc = softc;
1073 STAILQ_INIT(&thr->incoming_queue);
1074 STAILQ_INIT(&thr->rtr_queue);
1075 STAILQ_INIT(&thr->done_queue);
1076 STAILQ_INIT(&thr->isc_queue);
1078 error = kproc_kthread_add(ctl_work_thread, thr,
1079 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1081 printf("error creating CTL work thread!\n");
1082 ctl_pool_free(internal_pool);
1083 ctl_pool_free(emergency_pool);
1084 ctl_pool_free(other_pool);
1088 error = kproc_kthread_add(ctl_lun_thread, softc,
1089 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1091 printf("error creating CTL lun thread!\n");
1092 ctl_pool_free(internal_pool);
1093 ctl_pool_free(emergency_pool);
1094 ctl_pool_free(other_pool);
1098 printf("ctl: CAM Target Layer loaded\n");
1101 * Initialize the ioctl front end.
1103 ctl_frontend_register(&ioctl_frontend);
1104 port = &softc->ioctl_info.port;
1105 port->frontend = &ioctl_frontend;
1106 sprintf(softc->ioctl_info.port_name, "ioctl");
1107 port->port_type = CTL_PORT_IOCTL;
1108 port->num_requested_ctl_io = 100;
1109 port->port_name = softc->ioctl_info.port_name;
1110 port->port_online = ctl_ioctl_online;
1111 port->port_offline = ctl_ioctl_offline;
1112 port->onoff_arg = &softc->ioctl_info;
1113 port->lun_enable = ctl_ioctl_lun_enable;
1114 port->lun_disable = ctl_ioctl_lun_disable;
1115 port->targ_lun_arg = &softc->ioctl_info;
1116 port->fe_datamove = ctl_ioctl_datamove;
1117 port->fe_done = ctl_ioctl_done;
1118 port->max_targets = 15;
1119 port->max_target_id = 15;
1121 if (ctl_port_register(&softc->ioctl_info.port,
1122 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1123 printf("ctl: ioctl front end registration failed, will "
1124 "continue anyway\n");
1128 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1129 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1130 sizeof(struct callout), CTL_TIMER_BYTES);
1133 #endif /* CTL_IO_DELAY */
1141 struct ctl_softc *softc;
1142 struct ctl_lun *lun, *next_lun;
1143 struct ctl_io_pool *pool;
1145 softc = (struct ctl_softc *)control_softc;
1147 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1148 printf("ctl: ioctl front end deregistration failed\n");
1150 mtx_lock(&softc->ctl_lock);
1155 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1156 next_lun = STAILQ_NEXT(lun, links);
1160 mtx_unlock(&softc->ctl_lock);
1162 ctl_frontend_deregister(&ioctl_frontend);
1165 * This will rip the rug out from under any FETDs or anyone else
1166 * that has a pool allocated. Since we increment our module
1167 * refcount any time someone outside the main CTL module allocates
1168 * a pool, we shouldn't have any problems here. The user won't be
1169 * able to unload the CTL module until client modules have
1170 * successfully unloaded.
1172 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1173 ctl_pool_free(pool);
1176 ctl_shutdown_thread(softc->work_thread);
1177 mtx_destroy(&softc->queue_lock);
1180 ctl_tpc_shutdown(softc);
1181 mtx_destroy(&softc->pool_lock);
1182 mtx_destroy(&softc->ctl_lock);
1184 destroy_dev(softc->dev);
1186 sysctl_ctx_free(&softc->sysctl_ctx);
1188 free(control_softc, M_DEVBUF);
1189 control_softc = NULL;
1192 printf("ctl: CAM Target Layer unloaded\n");
1196 ctl_module_event_handler(module_t mod, int what, void *arg)
1201 return (ctl_init());
1205 return (EOPNOTSUPP);
1210 * XXX KDM should we do some access checks here? Bump a reference count to
1211 * prevent a CTL module from being unloaded while someone has it open?
1214 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1220 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1226 ctl_port_enable(ctl_port_type port_type)
1228 struct ctl_softc *softc;
1229 struct ctl_port *port;
1231 if (ctl_is_single == 0) {
1232 union ctl_ha_msg msg_info;
1236 printf("%s: HA mode, synchronizing frontend enable\n",
1239 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1240 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1241 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1242 printf("Sync msg send error retval %d\n", isc_retval);
1244 if (!rcv_sync_msg) {
1245 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1246 sizeof(msg_info), 1);
1249 printf("CTL:Frontend Enable\n");
1251 printf("%s: single mode, skipping frontend synchronization\n",
1256 softc = control_softc;
1258 STAILQ_FOREACH(port, &softc->port_list, links) {
1259 if (port_type & port->port_type)
1262 printf("port %d\n", port->targ_port);
1264 ctl_port_online(port);
1272 ctl_port_disable(ctl_port_type port_type)
1274 struct ctl_softc *softc;
1275 struct ctl_port *port;
1277 softc = control_softc;
1279 STAILQ_FOREACH(port, &softc->port_list, links) {
1280 if (port_type & port->port_type)
1281 ctl_port_offline(port);
1288 * Returns 0 for success, 1 for failure.
1289 * Currently the only failure mode is if there aren't enough entries
1290 * allocated. So, in case of a failure, look at num_entries_dropped,
1291 * reallocate and try again.
1294 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1295 int *num_entries_filled, int *num_entries_dropped,
1296 ctl_port_type port_type, int no_virtual)
1298 struct ctl_softc *softc;
1299 struct ctl_port *port;
1300 int entries_dropped, entries_filled;
1304 softc = control_softc;
1308 entries_dropped = 0;
1311 mtx_lock(&softc->ctl_lock);
1312 STAILQ_FOREACH(port, &softc->port_list, links) {
1313 struct ctl_port_entry *entry;
1315 if ((port->port_type & port_type) == 0)
1318 if ((no_virtual != 0)
1319 && (port->virtual_port != 0))
1322 if (entries_filled >= num_entries_alloced) {
1326 entry = &entries[i];
1328 entry->port_type = port->port_type;
1329 strlcpy(entry->port_name, port->port_name,
1330 sizeof(entry->port_name));
1331 entry->physical_port = port->physical_port;
1332 entry->virtual_port = port->virtual_port;
1333 entry->wwnn = port->wwnn;
1334 entry->wwpn = port->wwpn;
1340 mtx_unlock(&softc->ctl_lock);
1342 if (entries_dropped > 0)
1345 *num_entries_dropped = entries_dropped;
1346 *num_entries_filled = entries_filled;
1352 ctl_ioctl_online(void *arg)
1354 struct ctl_ioctl_info *ioctl_info;
1356 ioctl_info = (struct ctl_ioctl_info *)arg;
1358 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1362 ctl_ioctl_offline(void *arg)
1364 struct ctl_ioctl_info *ioctl_info;
1366 ioctl_info = (struct ctl_ioctl_info *)arg;
1368 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1372 * Remove an initiator by port number and initiator ID.
1373 * Returns 0 for success, -1 for failure.
1376 ctl_remove_initiator(struct ctl_port *port, int iid)
1378 struct ctl_softc *softc = control_softc;
1380 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1382 if (iid > CTL_MAX_INIT_PER_PORT) {
1383 printf("%s: initiator ID %u > maximun %u!\n",
1384 __func__, iid, CTL_MAX_INIT_PER_PORT);
1388 mtx_lock(&softc->ctl_lock);
1389 port->wwpn_iid[iid].in_use--;
1390 port->wwpn_iid[iid].last_use = time_uptime;
1391 mtx_unlock(&softc->ctl_lock);
1397 * Add an initiator to the initiator map.
1398 * Returns iid for success, < 0 for failure.
1401 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1403 struct ctl_softc *softc = control_softc;
1407 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1409 if (iid >= CTL_MAX_INIT_PER_PORT) {
1410 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1411 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1416 mtx_lock(&softc->ctl_lock);
1418 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1419 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1420 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1424 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1425 strcmp(name, port->wwpn_iid[i].name) == 0) {
1433 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1434 if (port->wwpn_iid[i].in_use == 0 &&
1435 port->wwpn_iid[i].wwpn == 0 &&
1436 port->wwpn_iid[i].name == NULL) {
1445 best_time = INT32_MAX;
1446 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1447 if (port->wwpn_iid[i].in_use == 0) {
1448 if (port->wwpn_iid[i].last_use < best_time) {
1450 best_time = port->wwpn_iid[i].last_use;
1458 mtx_unlock(&softc->ctl_lock);
1463 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1465 * This is not an error yet.
1467 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1469 printf("%s: port %d iid %u WWPN %#jx arrived"
1470 " again\n", __func__, port->targ_port,
1471 iid, (uintmax_t)wwpn);
1475 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1476 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1478 printf("%s: port %d iid %u name '%s' arrived"
1479 " again\n", __func__, port->targ_port,
1486 * This is an error, but what do we do about it? The
1487 * driver is telling us we have a new WWPN for this
1488 * initiator ID, so we pretty much need to use it.
1490 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1491 " but WWPN %#jx '%s' is still at that address\n",
1492 __func__, port->targ_port, iid, wwpn, name,
1493 (uintmax_t)port->wwpn_iid[iid].wwpn,
1494 port->wwpn_iid[iid].name);
1497 * XXX KDM clear have_ca and ua_pending on each LUN for
1502 free(port->wwpn_iid[iid].name, M_CTL);
1503 port->wwpn_iid[iid].name = name;
1504 port->wwpn_iid[iid].wwpn = wwpn;
1505 port->wwpn_iid[iid].in_use++;
1506 mtx_unlock(&softc->ctl_lock);
1512 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1516 switch (port->port_type) {
1519 struct scsi_transportid_fcp *id =
1520 (struct scsi_transportid_fcp *)buf;
1521 if (port->wwpn_iid[iid].wwpn == 0)
1523 memset(id, 0, sizeof(*id));
1524 id->format_protocol = SCSI_PROTO_FC;
1525 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1526 return (sizeof(*id));
1528 case CTL_PORT_ISCSI:
1530 struct scsi_transportid_iscsi_port *id =
1531 (struct scsi_transportid_iscsi_port *)buf;
1532 if (port->wwpn_iid[iid].name == NULL)
1535 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1537 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1538 len = roundup2(min(len, 252), 4);
1539 scsi_ulto2b(len, id->additional_length);
1540 return (sizeof(*id) + len);
1544 struct scsi_transportid_sas *id =
1545 (struct scsi_transportid_sas *)buf;
1546 if (port->wwpn_iid[iid].wwpn == 0)
1548 memset(id, 0, sizeof(*id));
1549 id->format_protocol = SCSI_PROTO_SAS;
1550 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1551 return (sizeof(*id));
1555 struct scsi_transportid_spi *id =
1556 (struct scsi_transportid_spi *)buf;
1557 memset(id, 0, sizeof(*id));
1558 id->format_protocol = SCSI_PROTO_SPI;
1559 scsi_ulto2b(iid, id->scsi_addr);
1560 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1561 return (sizeof(*id));
1567 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1573 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1579 * Data movement routine for the CTL ioctl frontend port.
1582 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1584 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1585 struct ctl_sg_entry ext_entry, kern_entry;
1586 int ext_sglen, ext_sg_entries, kern_sg_entries;
1587 int ext_sg_start, ext_offset;
1588 int len_to_copy, len_copied;
1589 int kern_watermark, ext_watermark;
1590 int ext_sglist_malloced;
1593 ext_sglist_malloced = 0;
1597 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1600 * If this flag is set, fake the data transfer.
1602 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1603 ctsio->ext_data_filled = ctsio->ext_data_len;
1608 * To simplify things here, if we have a single buffer, stick it in
1609 * a S/G entry and just make it a single entry S/G list.
1611 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1614 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1616 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1618 ext_sglist_malloced = 1;
1619 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1621 ctl_set_internal_failure(ctsio,
1626 ext_sg_entries = ctsio->ext_sg_entries;
1628 for (i = 0; i < ext_sg_entries; i++) {
1629 if ((len_seen + ext_sglist[i].len) >=
1630 ctsio->ext_data_filled) {
1632 ext_offset = ctsio->ext_data_filled - len_seen;
1635 len_seen += ext_sglist[i].len;
1638 ext_sglist = &ext_entry;
1639 ext_sglist->addr = ctsio->ext_data_ptr;
1640 ext_sglist->len = ctsio->ext_data_len;
1643 ext_offset = ctsio->ext_data_filled;
1646 if (ctsio->kern_sg_entries > 0) {
1647 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1648 kern_sg_entries = ctsio->kern_sg_entries;
1650 kern_sglist = &kern_entry;
1651 kern_sglist->addr = ctsio->kern_data_ptr;
1652 kern_sglist->len = ctsio->kern_data_len;
1653 kern_sg_entries = 1;
1658 ext_watermark = ext_offset;
1660 for (i = ext_sg_start, j = 0;
1661 i < ext_sg_entries && j < kern_sg_entries;) {
1662 uint8_t *ext_ptr, *kern_ptr;
1664 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1665 kern_sglist[j].len - kern_watermark);
1667 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1668 ext_ptr = ext_ptr + ext_watermark;
1669 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1673 panic("need to implement bus address support");
1675 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1678 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1679 kern_ptr = kern_ptr + kern_watermark;
1681 kern_watermark += len_to_copy;
1682 ext_watermark += len_to_copy;
1684 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1686 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1687 "bytes to user\n", len_to_copy));
1688 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1689 "to %p\n", kern_ptr, ext_ptr));
1690 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1691 ctl_set_internal_failure(ctsio,
1697 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1698 "bytes from user\n", len_to_copy));
1699 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1700 "to %p\n", ext_ptr, kern_ptr));
1701 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1702 ctl_set_internal_failure(ctsio,
1709 len_copied += len_to_copy;
1711 if (ext_sglist[i].len == ext_watermark) {
1716 if (kern_sglist[j].len == kern_watermark) {
1722 ctsio->ext_data_filled += len_copied;
1724 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1725 "kern_sg_entries: %d\n", ext_sg_entries,
1727 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1728 "kern_data_len = %d\n", ctsio->ext_data_len,
1729 ctsio->kern_data_len));
1732 /* XXX KDM set residual?? */
1735 if (ext_sglist_malloced != 0)
1736 free(ext_sglist, M_CTL);
1738 return (CTL_RETVAL_COMPLETE);
1742 * Serialize a command that went down the "wrong" side, and so was sent to
1743 * this controller for execution. The logic is a little different than the
1744 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1745 * sent back to the other side, but in the success case, we execute the
1746 * command on this side (XFER mode) or tell the other side to execute it
1750 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1752 struct ctl_softc *ctl_softc;
1753 union ctl_ha_msg msg_info;
1754 struct ctl_lun *lun;
1758 ctl_softc = control_softc;
1760 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1761 lun = ctl_softc->ctl_luns[targ_lun];
1765 * Why isn't LUN defined? The other side wouldn't
1766 * send a cmd if the LUN is undefined.
1768 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1770 /* "Logical unit not supported" */
1771 ctl_set_sense_data(&msg_info.scsi.sense_data,
1773 /*sense_format*/SSD_TYPE_NONE,
1774 /*current_error*/ 1,
1775 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1780 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1781 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1782 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1783 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1784 msg_info.hdr.serializing_sc = NULL;
1785 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1786 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1787 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1793 mtx_lock(&lun->lun_lock);
1794 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1796 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1797 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1799 case CTL_ACTION_BLOCK:
1800 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1801 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1804 case CTL_ACTION_PASS:
1805 case CTL_ACTION_SKIP:
1806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1807 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1808 ctl_enqueue_rtr((union ctl_io *)ctsio);
1811 /* send msg back to other side */
1812 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1813 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1814 msg_info.hdr.msg_type = CTL_MSG_R2R;
1816 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1818 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1819 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1823 case CTL_ACTION_OVERLAP:
1824 /* OVERLAPPED COMMANDS ATTEMPTED */
1825 ctl_set_sense_data(&msg_info.scsi.sense_data,
1827 /*sense_format*/SSD_TYPE_NONE,
1828 /*current_error*/ 1,
1829 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1834 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1835 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1836 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1837 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1838 msg_info.hdr.serializing_sc = NULL;
1839 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1841 printf("BAD JUJU:Major Bummer Overlap\n");
1843 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1845 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1846 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1849 case CTL_ACTION_OVERLAP_TAG:
1850 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1851 ctl_set_sense_data(&msg_info.scsi.sense_data,
1853 /*sense_format*/SSD_TYPE_NONE,
1854 /*current_error*/ 1,
1855 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1857 /*ascq*/ ctsio->tag_num & 0xff,
1860 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1861 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1862 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1863 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1864 msg_info.hdr.serializing_sc = NULL;
1865 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1867 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1869 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1871 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1872 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1875 case CTL_ACTION_ERROR:
1877 /* "Internal target failure" */
1878 ctl_set_sense_data(&msg_info.scsi.sense_data,
1880 /*sense_format*/SSD_TYPE_NONE,
1881 /*current_error*/ 1,
1882 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1887 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1888 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1889 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1890 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1891 msg_info.hdr.serializing_sc = NULL;
1892 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1894 printf("BAD JUJU:Major Bummer HW Error\n");
1896 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1898 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1899 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1903 mtx_unlock(&lun->lun_lock);
1908 ctl_ioctl_submit_wait(union ctl_io *io)
1910 struct ctl_fe_ioctl_params params;
1911 ctl_fe_ioctl_state last_state;
1916 bzero(¶ms, sizeof(params));
1918 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1919 cv_init(¶ms.sem, "ctlioccv");
1920 params.state = CTL_IOCTL_INPROG;
1921 last_state = params.state;
1923 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1925 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1927 /* This shouldn't happen */
1928 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1934 mtx_lock(¶ms.ioctl_mtx);
1936 * Check the state here, and don't sleep if the state has
1937 * already changed (i.e. wakeup has already occured, but we
1938 * weren't waiting yet).
1940 if (params.state == last_state) {
1941 /* XXX KDM cv_wait_sig instead? */
1942 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1944 last_state = params.state;
1946 switch (params.state) {
1947 case CTL_IOCTL_INPROG:
1948 /* Why did we wake up? */
1949 /* XXX KDM error here? */
1950 mtx_unlock(¶ms.ioctl_mtx);
1952 case CTL_IOCTL_DATAMOVE:
1953 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1956 * change last_state back to INPROG to avoid
1957 * deadlock on subsequent data moves.
1959 params.state = last_state = CTL_IOCTL_INPROG;
1961 mtx_unlock(¶ms.ioctl_mtx);
1962 ctl_ioctl_do_datamove(&io->scsiio);
1964 * Note that in some cases, most notably writes,
1965 * this will queue the I/O and call us back later.
1966 * In other cases, generally reads, this routine
1967 * will immediately call back and wake us up,
1968 * probably using our own context.
1970 io->scsiio.be_move_done(io);
1972 case CTL_IOCTL_DONE:
1973 mtx_unlock(¶ms.ioctl_mtx);
1974 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1978 mtx_unlock(¶ms.ioctl_mtx);
1979 /* XXX KDM error here? */
1982 } while (done == 0);
1984 mtx_destroy(¶ms.ioctl_mtx);
1985 cv_destroy(¶ms.sem);
1987 return (CTL_RETVAL_COMPLETE);
1991 ctl_ioctl_datamove(union ctl_io *io)
1993 struct ctl_fe_ioctl_params *params;
1995 params = (struct ctl_fe_ioctl_params *)
1996 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1998 mtx_lock(¶ms->ioctl_mtx);
1999 params->state = CTL_IOCTL_DATAMOVE;
2000 cv_broadcast(¶ms->sem);
2001 mtx_unlock(¶ms->ioctl_mtx);
2005 ctl_ioctl_done(union ctl_io *io)
2007 struct ctl_fe_ioctl_params *params;
2009 params = (struct ctl_fe_ioctl_params *)
2010 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2012 mtx_lock(¶ms->ioctl_mtx);
2013 params->state = CTL_IOCTL_DONE;
2014 cv_broadcast(¶ms->sem);
2015 mtx_unlock(¶ms->ioctl_mtx);
2019 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2021 struct ctl_fe_ioctl_startstop_info *sd_info;
2023 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2025 sd_info->hs_info.status = metatask->status;
2026 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2027 sd_info->hs_info.luns_complete =
2028 metatask->taskinfo.startstop.luns_complete;
2029 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2031 cv_broadcast(&sd_info->sem);
2035 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2037 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2039 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2041 mtx_lock(fe_bbr_info->lock);
2042 fe_bbr_info->bbr_info->status = metatask->status;
2043 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2044 fe_bbr_info->wakeup_done = 1;
2045 mtx_unlock(fe_bbr_info->lock);
2047 cv_broadcast(&fe_bbr_info->sem);
2051 * Returns 0 for success, errno for failure.
2054 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2055 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2062 mtx_lock(&lun->lun_lock);
2063 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2064 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2066 struct ctl_ooa_entry *entry;
2069 * If we've got more than we can fit, just count the
2070 * remaining entries.
2072 if (*cur_fill_num >= ooa_hdr->alloc_num)
2075 entry = &kern_entries[*cur_fill_num];
2077 entry->tag_num = io->scsiio.tag_num;
2078 entry->lun_num = lun->lun;
2080 entry->start_bt = io->io_hdr.start_bt;
2082 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2083 entry->cdb_len = io->scsiio.cdb_len;
2084 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2085 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2087 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2088 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2090 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2091 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2093 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2094 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2096 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2097 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2099 mtx_unlock(&lun->lun_lock);
2105 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2106 size_t error_str_len)
2110 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2112 if (copyin(user_addr, kptr, len) != 0) {
2113 snprintf(error_str, error_str_len, "Error copying %d bytes "
2114 "from user address %p to kernel address %p", len,
2124 ctl_free_args(int num_args, struct ctl_be_arg *args)
2131 for (i = 0; i < num_args; i++) {
2132 free(args[i].kname, M_CTL);
2133 free(args[i].kvalue, M_CTL);
2139 static struct ctl_be_arg *
2140 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2141 char *error_str, size_t error_str_len)
2143 struct ctl_be_arg *args;
2146 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2147 error_str, error_str_len);
2152 for (i = 0; i < num_args; i++) {
2153 args[i].kname = NULL;
2154 args[i].kvalue = NULL;
2157 for (i = 0; i < num_args; i++) {
2160 args[i].kname = ctl_copyin_alloc(args[i].name,
2161 args[i].namelen, error_str, error_str_len);
2162 if (args[i].kname == NULL)
2165 if (args[i].kname[args[i].namelen - 1] != '\0') {
2166 snprintf(error_str, error_str_len, "Argument %d "
2167 "name is not NUL-terminated", i);
2171 if (args[i].flags & CTL_BEARG_RD) {
2172 tmpptr = ctl_copyin_alloc(args[i].value,
2173 args[i].vallen, error_str, error_str_len);
2176 if ((args[i].flags & CTL_BEARG_ASCII)
2177 && (tmpptr[args[i].vallen - 1] != '\0')) {
2178 snprintf(error_str, error_str_len, "Argument "
2179 "%d value is not NUL-terminated", i);
2182 args[i].kvalue = tmpptr;
2184 args[i].kvalue = malloc(args[i].vallen,
2185 M_CTL, M_WAITOK | M_ZERO);
2192 ctl_free_args(num_args, args);
2198 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2202 for (i = 0; i < num_args; i++) {
2203 if (args[i].flags & CTL_BEARG_WR)
2204 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2209 * Escape characters that are illegal or not recommended in XML.
2212 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2218 for (; *str; str++) {
2221 retval = sbuf_printf(sb, "&");
2224 retval = sbuf_printf(sb, ">");
2227 retval = sbuf_printf(sb, "<");
2230 retval = sbuf_putc(sb, *str);
2243 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2246 struct ctl_softc *softc;
2249 softc = control_softc;
2259 * If we haven't been "enabled", don't allow any SCSI I/O
2262 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2267 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2269 printf("ctl_ioctl: can't allocate ctl_io!\n");
2275 * Need to save the pool reference so it doesn't get
2276 * spammed by the user's ctl_io.
2278 pool_tmp = io->io_hdr.pool;
2280 memcpy(io, (void *)addr, sizeof(*io));
2282 io->io_hdr.pool = pool_tmp;
2284 * No status yet, so make sure the status is set properly.
2286 io->io_hdr.status = CTL_STATUS_NONE;
2289 * The user sets the initiator ID, target and LUN IDs.
2291 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2292 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2293 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2294 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2295 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2297 retval = ctl_ioctl_submit_wait(io);
2304 memcpy((void *)addr, io, sizeof(*io));
2306 /* return this to our pool */
2311 case CTL_ENABLE_PORT:
2312 case CTL_DISABLE_PORT:
2313 case CTL_SET_PORT_WWNS: {
2314 struct ctl_port *port;
2315 struct ctl_port_entry *entry;
2317 entry = (struct ctl_port_entry *)addr;
2319 mtx_lock(&softc->ctl_lock);
2320 STAILQ_FOREACH(port, &softc->port_list, links) {
2326 if ((entry->port_type == CTL_PORT_NONE)
2327 && (entry->targ_port == port->targ_port)) {
2329 * If the user only wants to enable or
2330 * disable or set WWNs on a specific port,
2331 * do the operation and we're done.
2335 } else if (entry->port_type & port->port_type) {
2337 * Compare the user's type mask with the
2338 * particular frontend type to see if we
2345 * Make sure the user isn't trying to set
2346 * WWNs on multiple ports at the same time.
2348 if (cmd == CTL_SET_PORT_WWNS) {
2349 printf("%s: Can't set WWNs on "
2350 "multiple ports\n", __func__);
2357 * XXX KDM we have to drop the lock here,
2358 * because the online/offline operations
2359 * can potentially block. We need to
2360 * reference count the frontends so they
2363 mtx_unlock(&softc->ctl_lock);
2365 if (cmd == CTL_ENABLE_PORT) {
2366 struct ctl_lun *lun;
2368 STAILQ_FOREACH(lun, &softc->lun_list,
2370 port->lun_enable(port->targ_lun_arg,
2375 ctl_port_online(port);
2376 } else if (cmd == CTL_DISABLE_PORT) {
2377 struct ctl_lun *lun;
2379 ctl_port_offline(port);
2381 STAILQ_FOREACH(lun, &softc->lun_list,
2390 mtx_lock(&softc->ctl_lock);
2392 if (cmd == CTL_SET_PORT_WWNS)
2393 ctl_port_set_wwns(port,
2394 (entry->flags & CTL_PORT_WWNN_VALID) ?
2396 (entry->flags & CTL_PORT_WWPN_VALID) ?
2397 1 : 0, entry->wwpn);
2402 mtx_unlock(&softc->ctl_lock);
2405 case CTL_GET_PORT_LIST: {
2406 struct ctl_port *port;
2407 struct ctl_port_list *list;
2410 list = (struct ctl_port_list *)addr;
2412 if (list->alloc_len != (list->alloc_num *
2413 sizeof(struct ctl_port_entry))) {
2414 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2415 "alloc_num %u * sizeof(struct ctl_port_entry) "
2416 "%zu\n", __func__, list->alloc_len,
2417 list->alloc_num, sizeof(struct ctl_port_entry));
2423 list->dropped_num = 0;
2425 mtx_lock(&softc->ctl_lock);
2426 STAILQ_FOREACH(port, &softc->port_list, links) {
2427 struct ctl_port_entry entry, *list_entry;
2429 if (list->fill_num >= list->alloc_num) {
2430 list->dropped_num++;
2434 entry.port_type = port->port_type;
2435 strlcpy(entry.port_name, port->port_name,
2436 sizeof(entry.port_name));
2437 entry.targ_port = port->targ_port;
2438 entry.physical_port = port->physical_port;
2439 entry.virtual_port = port->virtual_port;
2440 entry.wwnn = port->wwnn;
2441 entry.wwpn = port->wwpn;
2442 if (port->status & CTL_PORT_STATUS_ONLINE)
2447 list_entry = &list->entries[i];
2449 retval = copyout(&entry, list_entry, sizeof(entry));
2451 printf("%s: CTL_GET_PORT_LIST: copyout "
2452 "returned %d\n", __func__, retval);
2457 list->fill_len += sizeof(entry);
2459 mtx_unlock(&softc->ctl_lock);
2462 * If this is non-zero, we had a copyout fault, so there's
2463 * probably no point in attempting to set the status inside
2469 if (list->dropped_num > 0)
2470 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2472 list->status = CTL_PORT_LIST_OK;
2475 case CTL_DUMP_OOA: {
2476 struct ctl_lun *lun;
2481 mtx_lock(&softc->ctl_lock);
2482 printf("Dumping OOA queues:\n");
2483 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2484 mtx_lock(&lun->lun_lock);
2485 for (io = (union ctl_io *)TAILQ_FIRST(
2486 &lun->ooa_queue); io != NULL;
2487 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2489 sbuf_new(&sb, printbuf, sizeof(printbuf),
2491 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2495 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2497 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2499 CTL_FLAG_ABORT) ? " ABORT" : "",
2501 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2502 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2504 printf("%s\n", sbuf_data(&sb));
2506 mtx_unlock(&lun->lun_lock);
2508 printf("OOA queues dump done\n");
2509 mtx_unlock(&softc->ctl_lock);
2513 struct ctl_lun *lun;
2514 struct ctl_ooa *ooa_hdr;
2515 struct ctl_ooa_entry *entries;
2516 uint32_t cur_fill_num;
2518 ooa_hdr = (struct ctl_ooa *)addr;
2520 if ((ooa_hdr->alloc_len == 0)
2521 || (ooa_hdr->alloc_num == 0)) {
2522 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2523 "must be non-zero\n", __func__,
2524 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2529 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2530 sizeof(struct ctl_ooa_entry))) {
2531 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2532 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2533 __func__, ooa_hdr->alloc_len,
2534 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2539 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2540 if (entries == NULL) {
2541 printf("%s: could not allocate %d bytes for OOA "
2542 "dump\n", __func__, ooa_hdr->alloc_len);
2547 mtx_lock(&softc->ctl_lock);
2548 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2549 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2550 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2551 mtx_unlock(&softc->ctl_lock);
2552 free(entries, M_CTL);
2553 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2554 __func__, (uintmax_t)ooa_hdr->lun_num);
2561 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2562 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2563 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2569 mtx_unlock(&softc->ctl_lock);
2570 free(entries, M_CTL);
2574 lun = softc->ctl_luns[ooa_hdr->lun_num];
2576 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2579 mtx_unlock(&softc->ctl_lock);
2581 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2582 ooa_hdr->fill_len = ooa_hdr->fill_num *
2583 sizeof(struct ctl_ooa_entry);
2584 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2586 printf("%s: error copying out %d bytes for OOA dump\n",
2587 __func__, ooa_hdr->fill_len);
2590 getbintime(&ooa_hdr->cur_bt);
2592 if (cur_fill_num > ooa_hdr->alloc_num) {
2593 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2594 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2596 ooa_hdr->dropped_num = 0;
2597 ooa_hdr->status = CTL_OOA_OK;
2600 free(entries, M_CTL);
2603 case CTL_CHECK_OOA: {
2605 struct ctl_lun *lun;
2606 struct ctl_ooa_info *ooa_info;
2609 ooa_info = (struct ctl_ooa_info *)addr;
2611 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2612 ooa_info->status = CTL_OOA_INVALID_LUN;
2615 mtx_lock(&softc->ctl_lock);
2616 lun = softc->ctl_luns[ooa_info->lun_id];
2618 mtx_unlock(&softc->ctl_lock);
2619 ooa_info->status = CTL_OOA_INVALID_LUN;
2622 mtx_lock(&lun->lun_lock);
2623 mtx_unlock(&softc->ctl_lock);
2624 ooa_info->num_entries = 0;
2625 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2626 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2627 &io->io_hdr, ooa_links)) {
2628 ooa_info->num_entries++;
2630 mtx_unlock(&lun->lun_lock);
2632 ooa_info->status = CTL_OOA_SUCCESS;
2636 case CTL_HARD_START:
2637 case CTL_HARD_STOP: {
2638 struct ctl_fe_ioctl_startstop_info ss_info;
2639 struct cfi_metatask *metatask;
2642 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2644 cv_init(&ss_info.sem, "hard start/stop cv" );
2646 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2647 if (metatask == NULL) {
2649 mtx_destroy(&hs_mtx);
2653 if (cmd == CTL_HARD_START)
2654 metatask->tasktype = CFI_TASK_STARTUP;
2656 metatask->tasktype = CFI_TASK_SHUTDOWN;
2658 metatask->callback = ctl_ioctl_hard_startstop_callback;
2659 metatask->callback_arg = &ss_info;
2661 cfi_action(metatask);
2663 /* Wait for the callback */
2665 cv_wait_sig(&ss_info.sem, &hs_mtx);
2666 mtx_unlock(&hs_mtx);
2669 * All information has been copied from the metatask by the
2670 * time cv_broadcast() is called, so we free the metatask here.
2672 cfi_free_metatask(metatask);
2674 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2676 mtx_destroy(&hs_mtx);
2680 struct ctl_bbrread_info *bbr_info;
2681 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2683 struct cfi_metatask *metatask;
2685 bbr_info = (struct ctl_bbrread_info *)addr;
2687 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2689 bzero(&bbr_mtx, sizeof(bbr_mtx));
2690 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2692 fe_bbr_info.bbr_info = bbr_info;
2693 fe_bbr_info.lock = &bbr_mtx;
2695 cv_init(&fe_bbr_info.sem, "BBR read cv");
2696 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2698 if (metatask == NULL) {
2699 mtx_destroy(&bbr_mtx);
2700 cv_destroy(&fe_bbr_info.sem);
2704 metatask->tasktype = CFI_TASK_BBRREAD;
2705 metatask->callback = ctl_ioctl_bbrread_callback;
2706 metatask->callback_arg = &fe_bbr_info;
2707 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2708 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2709 metatask->taskinfo.bbrread.len = bbr_info->len;
2711 cfi_action(metatask);
2714 while (fe_bbr_info.wakeup_done == 0)
2715 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2716 mtx_unlock(&bbr_mtx);
2718 bbr_info->status = metatask->status;
2719 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2720 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2721 memcpy(&bbr_info->sense_data,
2722 &metatask->taskinfo.bbrread.sense_data,
2723 ctl_min(sizeof(bbr_info->sense_data),
2724 sizeof(metatask->taskinfo.bbrread.sense_data)));
2726 cfi_free_metatask(metatask);
2728 mtx_destroy(&bbr_mtx);
2729 cv_destroy(&fe_bbr_info.sem);
2733 case CTL_DELAY_IO: {
2734 struct ctl_io_delay_info *delay_info;
2736 struct ctl_lun *lun;
2737 #endif /* CTL_IO_DELAY */
2739 delay_info = (struct ctl_io_delay_info *)addr;
2742 mtx_lock(&softc->ctl_lock);
2744 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2745 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2746 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2748 lun = softc->ctl_luns[delay_info->lun_id];
2749 mtx_lock(&lun->lun_lock);
2751 delay_info->status = CTL_DELAY_STATUS_OK;
2753 switch (delay_info->delay_type) {
2754 case CTL_DELAY_TYPE_CONT:
2756 case CTL_DELAY_TYPE_ONESHOT:
2759 delay_info->status =
2760 CTL_DELAY_STATUS_INVALID_TYPE;
2764 switch (delay_info->delay_loc) {
2765 case CTL_DELAY_LOC_DATAMOVE:
2766 lun->delay_info.datamove_type =
2767 delay_info->delay_type;
2768 lun->delay_info.datamove_delay =
2769 delay_info->delay_secs;
2771 case CTL_DELAY_LOC_DONE:
2772 lun->delay_info.done_type =
2773 delay_info->delay_type;
2774 lun->delay_info.done_delay =
2775 delay_info->delay_secs;
2778 delay_info->status =
2779 CTL_DELAY_STATUS_INVALID_LOC;
2782 mtx_unlock(&lun->lun_lock);
2785 mtx_unlock(&softc->ctl_lock);
2787 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2788 #endif /* CTL_IO_DELAY */
2791 case CTL_REALSYNC_SET: {
2794 syncstate = (int *)addr;
2796 mtx_lock(&softc->ctl_lock);
2797 switch (*syncstate) {
2799 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2802 softc->flags |= CTL_FLAG_REAL_SYNC;
2808 mtx_unlock(&softc->ctl_lock);
2811 case CTL_REALSYNC_GET: {
2814 syncstate = (int*)addr;
2816 mtx_lock(&softc->ctl_lock);
2817 if (softc->flags & CTL_FLAG_REAL_SYNC)
2821 mtx_unlock(&softc->ctl_lock);
2827 struct ctl_sync_info *sync_info;
2828 struct ctl_lun *lun;
2830 sync_info = (struct ctl_sync_info *)addr;
2832 mtx_lock(&softc->ctl_lock);
2833 lun = softc->ctl_luns[sync_info->lun_id];
2835 mtx_unlock(&softc->ctl_lock);
2836 sync_info->status = CTL_GS_SYNC_NO_LUN;
2839 * Get or set the sync interval. We're not bounds checking
2840 * in the set case, hopefully the user won't do something
2843 mtx_lock(&lun->lun_lock);
2844 mtx_unlock(&softc->ctl_lock);
2845 if (cmd == CTL_GETSYNC)
2846 sync_info->sync_interval = lun->sync_interval;
2848 lun->sync_interval = sync_info->sync_interval;
2849 mtx_unlock(&lun->lun_lock);
2851 sync_info->status = CTL_GS_SYNC_OK;
2855 case CTL_GETSTATS: {
2856 struct ctl_stats *stats;
2857 struct ctl_lun *lun;
2860 stats = (struct ctl_stats *)addr;
2862 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2864 stats->status = CTL_SS_NEED_MORE_SPACE;
2865 stats->num_luns = softc->num_luns;
2869 * XXX KDM no locking here. If the LUN list changes,
2870 * things can blow up.
2872 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2873 i++, lun = STAILQ_NEXT(lun, links)) {
2874 retval = copyout(&lun->stats, &stats->lun_stats[i],
2875 sizeof(lun->stats));
2879 stats->num_luns = softc->num_luns;
2880 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2882 stats->status = CTL_SS_OK;
2884 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2886 stats->flags = CTL_STATS_FLAG_NONE;
2888 getnanouptime(&stats->timestamp);
2891 case CTL_ERROR_INJECT: {
2892 struct ctl_error_desc *err_desc, *new_err_desc;
2893 struct ctl_lun *lun;
2895 err_desc = (struct ctl_error_desc *)addr;
2897 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2899 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2901 mtx_lock(&softc->ctl_lock);
2902 lun = softc->ctl_luns[err_desc->lun_id];
2904 mtx_unlock(&softc->ctl_lock);
2905 free(new_err_desc, M_CTL);
2906 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2907 __func__, (uintmax_t)err_desc->lun_id);
2911 mtx_lock(&lun->lun_lock);
2912 mtx_unlock(&softc->ctl_lock);
2915 * We could do some checking here to verify the validity
2916 * of the request, but given the complexity of error
2917 * injection requests, the checking logic would be fairly
2920 * For now, if the request is invalid, it just won't get
2921 * executed and might get deleted.
2923 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2926 * XXX KDM check to make sure the serial number is unique,
2927 * in case we somehow manage to wrap. That shouldn't
2928 * happen for a very long time, but it's the right thing to
2931 new_err_desc->serial = lun->error_serial;
2932 err_desc->serial = lun->error_serial;
2933 lun->error_serial++;
2935 mtx_unlock(&lun->lun_lock);
2938 case CTL_ERROR_INJECT_DELETE: {
2939 struct ctl_error_desc *delete_desc, *desc, *desc2;
2940 struct ctl_lun *lun;
2943 delete_desc = (struct ctl_error_desc *)addr;
2946 mtx_lock(&softc->ctl_lock);
2947 lun = softc->ctl_luns[delete_desc->lun_id];
2949 mtx_unlock(&softc->ctl_lock);
2950 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2951 __func__, (uintmax_t)delete_desc->lun_id);
2955 mtx_lock(&lun->lun_lock);
2956 mtx_unlock(&softc->ctl_lock);
2957 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2958 if (desc->serial != delete_desc->serial)
2961 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2966 mtx_unlock(&lun->lun_lock);
2967 if (delete_done == 0) {
2968 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2969 "error serial %ju on LUN %u\n", __func__,
2970 delete_desc->serial, delete_desc->lun_id);
2976 case CTL_DUMP_STRUCTS: {
2978 struct ctl_port *port;
2979 struct ctl_frontend *fe;
2981 mtx_lock(&softc->ctl_lock);
2982 printf("CTL Persistent Reservation information start:\n");
2983 for (i = 0; i < CTL_MAX_LUNS; i++) {
2984 struct ctl_lun *lun;
2986 lun = softc->ctl_luns[i];
2989 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2992 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2993 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2994 idx = j * CTL_MAX_INIT_PER_PORT + k;
2995 if (lun->pr_keys[idx] == 0)
2997 printf(" LUN %d port %d iid %d key "
2999 (uintmax_t)lun->pr_keys[idx]);
3003 printf("CTL Persistent Reservation information end\n");
3004 printf("CTL Ports:\n");
3005 STAILQ_FOREACH(port, &softc->port_list, links) {
3006 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3007 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3008 port->frontend->name, port->port_type,
3009 port->physical_port, port->virtual_port,
3010 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3011 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3012 if (port->wwpn_iid[j].in_use == 0 &&
3013 port->wwpn_iid[j].wwpn == 0 &&
3014 port->wwpn_iid[j].name == NULL)
3017 printf(" iid %u use %d WWPN %#jx '%s'\n",
3018 j, port->wwpn_iid[j].in_use,
3019 (uintmax_t)port->wwpn_iid[j].wwpn,
3020 port->wwpn_iid[j].name);
3023 printf("CTL Port information end\n");
3024 mtx_unlock(&softc->ctl_lock);
3026 * XXX KDM calling this without a lock. We'd likely want
3027 * to drop the lock before calling the frontend's dump
3030 printf("CTL Frontends:\n");
3031 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3032 printf(" Frontend '%s'\n", fe->name);
3033 if (fe->fe_dump != NULL)
3036 printf("CTL Frontend information end\n");
3040 struct ctl_lun_req *lun_req;
3041 struct ctl_backend_driver *backend;
3043 lun_req = (struct ctl_lun_req *)addr;
3045 backend = ctl_backend_find(lun_req->backend);
3046 if (backend == NULL) {
3047 lun_req->status = CTL_LUN_ERROR;
3048 snprintf(lun_req->error_str,
3049 sizeof(lun_req->error_str),
3050 "Backend \"%s\" not found.",
3054 if (lun_req->num_be_args > 0) {
3055 lun_req->kern_be_args = ctl_copyin_args(
3056 lun_req->num_be_args,
3059 sizeof(lun_req->error_str));
3060 if (lun_req->kern_be_args == NULL) {
3061 lun_req->status = CTL_LUN_ERROR;
3066 retval = backend->ioctl(dev, cmd, addr, flag, td);
3068 if (lun_req->num_be_args > 0) {
3069 ctl_copyout_args(lun_req->num_be_args,
3070 lun_req->kern_be_args);
3071 ctl_free_args(lun_req->num_be_args,
3072 lun_req->kern_be_args);
3076 case CTL_LUN_LIST: {
3078 struct ctl_lun *lun;
3079 struct ctl_lun_list *list;
3080 struct ctl_option *opt;
3082 list = (struct ctl_lun_list *)addr;
3085 * Allocate a fixed length sbuf here, based on the length
3086 * of the user's buffer. We could allocate an auto-extending
3087 * buffer, and then tell the user how much larger our
3088 * amount of data is than his buffer, but that presents
3091 * 1. The sbuf(9) routines use a blocking malloc, and so
3092 * we can't hold a lock while calling them with an
3093 * auto-extending buffer.
3095 * 2. There is not currently a LUN reference counting
3096 * mechanism, outside of outstanding transactions on
3097 * the LUN's OOA queue. So a LUN could go away on us
3098 * while we're getting the LUN number, backend-specific
3099 * information, etc. Thus, given the way things
3100 * currently work, we need to hold the CTL lock while
3101 * grabbing LUN information.
3103 * So, from the user's standpoint, the best thing to do is
3104 * allocate what he thinks is a reasonable buffer length,
3105 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3106 * double the buffer length and try again. (And repeat
3107 * that until he succeeds.)
3109 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3111 list->status = CTL_LUN_LIST_ERROR;
3112 snprintf(list->error_str, sizeof(list->error_str),
3113 "Unable to allocate %d bytes for LUN list",
3118 sbuf_printf(sb, "<ctllunlist>\n");
3120 mtx_lock(&softc->ctl_lock);
3121 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3122 mtx_lock(&lun->lun_lock);
3123 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3124 (uintmax_t)lun->lun);
3127 * Bail out as soon as we see that we've overfilled
3133 retval = sbuf_printf(sb, "\t<backend_type>%s"
3134 "</backend_type>\n",
3135 (lun->backend == NULL) ? "none" :
3136 lun->backend->name);
3141 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3142 lun->be_lun->lun_type);
3147 if (lun->backend == NULL) {
3148 retval = sbuf_printf(sb, "</lun>\n");
3154 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3155 (lun->be_lun->maxlba > 0) ?
3156 lun->be_lun->maxlba + 1 : 0);
3161 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3162 lun->be_lun->blocksize);
3167 retval = sbuf_printf(sb, "\t<serial_number>");
3172 retval = ctl_sbuf_printf_esc(sb,
3173 lun->be_lun->serial_num);
3178 retval = sbuf_printf(sb, "</serial_number>\n");
3183 retval = sbuf_printf(sb, "\t<device_id>");
3188 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3193 retval = sbuf_printf(sb, "</device_id>\n");
3198 if (lun->backend->lun_info != NULL) {
3199 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3203 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3204 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3205 opt->name, opt->value, opt->name);
3210 retval = sbuf_printf(sb, "</lun>\n");
3214 mtx_unlock(&lun->lun_lock);
3217 mtx_unlock(&lun->lun_lock);
3218 mtx_unlock(&softc->ctl_lock);
3221 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3224 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3225 snprintf(list->error_str, sizeof(list->error_str),
3226 "Out of space, %d bytes is too small",
3233 retval = copyout(sbuf_data(sb), list->lun_xml,
3236 list->fill_len = sbuf_len(sb) + 1;
3237 list->status = CTL_LUN_LIST_OK;
3242 struct ctl_iscsi *ci;
3243 struct ctl_frontend *fe;
3245 ci = (struct ctl_iscsi *)addr;
3247 fe = ctl_frontend_find("iscsi");
3249 ci->status = CTL_ISCSI_ERROR;
3250 snprintf(ci->error_str, sizeof(ci->error_str),
3251 "Frontend \"iscsi\" not found.");
3255 retval = fe->ioctl(dev, cmd, addr, flag, td);
3258 case CTL_PORT_REQ: {
3259 struct ctl_req *req;
3260 struct ctl_frontend *fe;
3262 req = (struct ctl_req *)addr;
3264 fe = ctl_frontend_find(req->driver);
3266 req->status = CTL_LUN_ERROR;
3267 snprintf(req->error_str, sizeof(req->error_str),
3268 "Frontend \"%s\" not found.", req->driver);
3271 if (req->num_args > 0) {
3272 req->kern_args = ctl_copyin_args(req->num_args,
3273 req->args, req->error_str, sizeof(req->error_str));
3274 if (req->kern_args == NULL) {
3275 req->status = CTL_LUN_ERROR;
3280 retval = fe->ioctl(dev, cmd, addr, flag, td);
3282 if (req->num_args > 0) {
3283 ctl_copyout_args(req->num_args, req->kern_args);
3284 ctl_free_args(req->num_args, req->kern_args);
3288 case CTL_PORT_LIST: {
3290 struct ctl_port *port;
3291 struct ctl_lun_list *list;
3292 struct ctl_option *opt;
3294 list = (struct ctl_lun_list *)addr;
3296 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3298 list->status = CTL_LUN_LIST_ERROR;
3299 snprintf(list->error_str, sizeof(list->error_str),
3300 "Unable to allocate %d bytes for LUN list",
3305 sbuf_printf(sb, "<ctlportlist>\n");
3307 mtx_lock(&softc->ctl_lock);
3308 STAILQ_FOREACH(port, &softc->port_list, links) {
3309 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3310 (uintmax_t)port->targ_port);
3313 * Bail out as soon as we see that we've overfilled
3319 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3320 "</frontend_type>\n", port->frontend->name);
3324 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3329 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3330 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3334 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3339 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3340 port->physical_port);
3344 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3345 port->virtual_port);
3349 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3350 (uintmax_t)port->wwnn);
3354 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3355 (uintmax_t)port->wwpn);
3359 if (port->port_info != NULL) {
3360 retval = port->port_info(port->onoff_arg, sb);
3364 STAILQ_FOREACH(opt, &port->options, links) {
3365 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3366 opt->name, opt->value, opt->name);
3371 retval = sbuf_printf(sb, "</targ_port>\n");
3375 mtx_unlock(&softc->ctl_lock);
3378 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3381 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3382 snprintf(list->error_str, sizeof(list->error_str),
3383 "Out of space, %d bytes is too small",
3390 retval = copyout(sbuf_data(sb), list->lun_xml,
3393 list->fill_len = sbuf_len(sb) + 1;
3394 list->status = CTL_LUN_LIST_OK;
3399 /* XXX KDM should we fix this? */
3401 struct ctl_backend_driver *backend;
3408 * We encode the backend type as the ioctl type for backend
3409 * ioctls. So parse it out here, and then search for a
3410 * backend of this type.
3412 type = _IOC_TYPE(cmd);
3414 STAILQ_FOREACH(backend, &softc->be_list, links) {
3415 if (backend->type == type) {
3421 printf("ctl: unknown ioctl command %#lx or backend "
3426 retval = backend->ioctl(dev, cmd, addr, flag, td);
3436 ctl_get_initindex(struct ctl_nexus *nexus)
3438 if (nexus->targ_port < CTL_MAX_PORTS)
3439 return (nexus->initid.id +
3440 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3442 return (nexus->initid.id +
3443 ((nexus->targ_port - CTL_MAX_PORTS) *
3444 CTL_MAX_INIT_PER_PORT));
3448 ctl_get_resindex(struct ctl_nexus *nexus)
3450 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3454 ctl_port_idx(int port_num)
3456 if (port_num < CTL_MAX_PORTS)
3459 return(port_num - CTL_MAX_PORTS);
3463 ctl_map_lun(int port_num, uint32_t lun_id)
3465 struct ctl_port *port;
3467 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3469 return (UINT32_MAX);
3470 if (port->lun_map == NULL)
3472 return (port->lun_map(port->targ_lun_arg, lun_id));
3476 ctl_map_lun_back(int port_num, uint32_t lun_id)
3478 struct ctl_port *port;
3481 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3482 if (port->lun_map == NULL)
3484 for (i = 0; i < CTL_MAX_LUNS; i++) {
3485 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3488 return (UINT32_MAX);
3492 * Note: This only works for bitmask sizes that are at least 32 bits, and
3493 * that are a power of 2.
3496 ctl_ffz(uint32_t *mask, uint32_t size)
3498 uint32_t num_chunks, num_pieces;
3501 num_chunks = (size >> 5);
3502 if (num_chunks == 0)
3504 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3506 for (i = 0; i < num_chunks; i++) {
3507 for (j = 0; j < num_pieces; j++) {
3508 if ((mask[i] & (1 << j)) == 0)
3509 return ((i << 5) + j);
3517 ctl_set_mask(uint32_t *mask, uint32_t bit)
3519 uint32_t chunk, piece;
3522 piece = bit % (sizeof(uint32_t) * 8);
3524 if ((mask[chunk] & (1 << piece)) != 0)
3527 mask[chunk] |= (1 << piece);
3533 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3535 uint32_t chunk, piece;
3538 piece = bit % (sizeof(uint32_t) * 8);
3540 if ((mask[chunk] & (1 << piece)) == 0)
3543 mask[chunk] &= ~(1 << piece);
3549 ctl_is_set(uint32_t *mask, uint32_t bit)
3551 uint32_t chunk, piece;
3554 piece = bit % (sizeof(uint32_t) * 8);
3556 if ((mask[chunk] & (1 << piece)) == 0)
3564 * The bus, target and lun are optional, they can be filled in later.
3565 * can_wait is used to determine whether we can wait on the malloc or not.
3568 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3569 uint32_t targ_lun, int can_wait)
3574 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3576 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3579 io->io_hdr.io_type = io_type;
3580 io->io_hdr.targ_port = targ_port;
3582 * XXX KDM this needs to change/go away. We need to move
3583 * to a preallocated pool of ctl_scsiio structures.
3585 io->io_hdr.nexus.targ_target.id = targ_target;
3586 io->io_hdr.nexus.targ_lun = targ_lun;
3593 ctl_kfree_io(union ctl_io *io)
3600 * ctl_softc, pool_type, total_ctl_io are passed in.
3601 * npool is passed out.
3604 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3605 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3608 union ctl_io *cur_io, *next_io;
3609 struct ctl_io_pool *pool;
3614 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3621 pool->type = pool_type;
3622 pool->ctl_softc = ctl_softc;
3624 mtx_lock(&ctl_softc->pool_lock);
3625 pool->id = ctl_softc->cur_pool_id++;
3626 mtx_unlock(&ctl_softc->pool_lock);
3628 pool->flags = CTL_POOL_FLAG_NONE;
3629 pool->refcount = 1; /* Reference for validity. */
3630 STAILQ_INIT(&pool->free_queue);
3633 * XXX KDM other options here:
3634 * - allocate a page at a time
3635 * - allocate one big chunk of memory.
3636 * Page allocation might work well, but would take a little more
3639 for (i = 0; i < total_ctl_io; i++) {
3640 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3642 if (cur_io == NULL) {
3646 cur_io->io_hdr.pool = pool;
3647 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3648 pool->total_ctl_io++;
3649 pool->free_ctl_io++;
3653 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3654 cur_io != NULL; cur_io = next_io) {
3655 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3657 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3659 free(cur_io, M_CTLIO);
3665 mtx_lock(&ctl_softc->pool_lock);
3666 ctl_softc->num_pools++;
3667 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3669 * Increment our usage count if this is an external consumer, so we
3670 * can't get unloaded until the external consumer (most likely a
3671 * FETD) unloads and frees his pool.
3673 * XXX KDM will this increment the caller's module use count, or
3677 if ((pool_type != CTL_POOL_EMERGENCY)
3678 && (pool_type != CTL_POOL_INTERNAL)
3679 && (pool_type != CTL_POOL_4OTHERSC))
3683 mtx_unlock(&ctl_softc->pool_lock);
3693 ctl_pool_acquire(struct ctl_io_pool *pool)
3696 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3698 if (pool->flags & CTL_POOL_FLAG_INVALID)
3707 ctl_pool_release(struct ctl_io_pool *pool)
3709 struct ctl_softc *ctl_softc = pool->ctl_softc;
3712 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3714 if (--pool->refcount != 0)
3717 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3718 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3723 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3724 ctl_softc->num_pools--;
3727 * XXX KDM will this decrement the caller's usage count or mine?
3730 if ((pool->type != CTL_POOL_EMERGENCY)
3731 && (pool->type != CTL_POOL_INTERNAL)
3732 && (pool->type != CTL_POOL_4OTHERSC))
3740 ctl_pool_free(struct ctl_io_pool *pool)
3742 struct ctl_softc *ctl_softc;
3747 ctl_softc = pool->ctl_softc;
3748 mtx_lock(&ctl_softc->pool_lock);
3749 pool->flags |= CTL_POOL_FLAG_INVALID;
3750 ctl_pool_release(pool);
3751 mtx_unlock(&ctl_softc->pool_lock);
3755 * This routine does not block (except for spinlocks of course).
3756 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3760 ctl_alloc_io(void *pool_ref)
3763 struct ctl_softc *ctl_softc;
3764 struct ctl_io_pool *pool, *npool;
3765 struct ctl_io_pool *emergency_pool;
3767 pool = (struct ctl_io_pool *)pool_ref;
3770 printf("%s: pool is NULL\n", __func__);
3774 emergency_pool = NULL;
3776 ctl_softc = pool->ctl_softc;
3778 mtx_lock(&ctl_softc->pool_lock);
3780 * First, try to get the io structure from the user's pool.
3782 if (ctl_pool_acquire(pool) == 0) {
3783 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3785 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3786 pool->total_allocated++;
3787 pool->free_ctl_io--;
3788 mtx_unlock(&ctl_softc->pool_lock);
3791 ctl_pool_release(pool);
3794 * If he doesn't have any io structures left, search for an
3795 * emergency pool and grab one from there.
3797 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3798 if (npool->type != CTL_POOL_EMERGENCY)
3801 if (ctl_pool_acquire(npool) != 0)
3804 emergency_pool = npool;
3806 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3808 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3809 npool->total_allocated++;
3810 npool->free_ctl_io--;
3811 mtx_unlock(&ctl_softc->pool_lock);
3814 ctl_pool_release(npool);
3817 /* Drop the spinlock before we malloc */
3818 mtx_unlock(&ctl_softc->pool_lock);
3821 * The emergency pool (if it exists) didn't have one, so try an
3822 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3824 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3827 * If the emergency pool exists but is empty, add this
3828 * ctl_io to its list when it gets freed.
3830 if (emergency_pool != NULL) {
3831 mtx_lock(&ctl_softc->pool_lock);
3832 if (ctl_pool_acquire(emergency_pool) == 0) {
3833 io->io_hdr.pool = emergency_pool;
3834 emergency_pool->total_ctl_io++;
3836 * Need to bump this, otherwise
3837 * total_allocated and total_freed won't
3838 * match when we no longer have anything
3841 emergency_pool->total_allocated++;
3843 mtx_unlock(&ctl_softc->pool_lock);
3845 io->io_hdr.pool = NULL;
3852 ctl_free_io(union ctl_io *io)
3858 * If this ctl_io has a pool, return it to that pool.
3860 if (io->io_hdr.pool != NULL) {
3861 struct ctl_io_pool *pool;
3863 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3864 mtx_lock(&pool->ctl_softc->pool_lock);
3865 io->io_hdr.io_type = 0xff;
3866 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3867 pool->total_freed++;
3868 pool->free_ctl_io++;
3869 ctl_pool_release(pool);
3870 mtx_unlock(&pool->ctl_softc->pool_lock);
3873 * Otherwise, just free it. We probably malloced it and
3874 * the emergency pool wasn't available.
3882 ctl_zero_io(union ctl_io *io)
3890 * May need to preserve linked list pointers at some point too.
3892 pool_ref = io->io_hdr.pool;
3894 memset(io, 0, sizeof(*io));
3896 io->io_hdr.pool = pool_ref;
3900 * This routine is currently used for internal copies of ctl_ios that need
3901 * to persist for some reason after we've already returned status to the
3902 * FETD. (Thus the flag set.)
3905 * Note that this makes a blind copy of all fields in the ctl_io, except
3906 * for the pool reference. This includes any memory that has been
3907 * allocated! That memory will no longer be valid after done has been
3908 * called, so this would be VERY DANGEROUS for command that actually does
3909 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3910 * start and stop commands, which don't transfer any data, so this is not a
3911 * problem. If it is used for anything else, the caller would also need to
3912 * allocate data buffer space and this routine would need to be modified to
3913 * copy the data buffer(s) as well.
3916 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3925 * May need to preserve linked list pointers at some point too.
3927 pool_ref = dest->io_hdr.pool;
3929 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3931 dest->io_hdr.pool = pool_ref;
3933 * We need to know that this is an internal copy, and doesn't need
3934 * to get passed back to the FETD that allocated it.
3936 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3941 ctl_update_power_subpage(struct copan_power_subpage *page)
3943 int num_luns, num_partitions, config_type;
3944 struct ctl_softc *softc;
3945 cs_BOOL_t aor_present, shelf_50pct_power;
3946 cs_raidset_personality_t rs_type;
3947 int max_active_luns;
3949 softc = control_softc;
3951 /* subtract out the processor LUN */
3952 num_luns = softc->num_luns - 1;
3954 * Default to 7 LUNs active, which was the only number we allowed
3957 max_active_luns = 7;
3959 num_partitions = config_GetRsPartitionInfo();
3960 config_type = config_GetConfigType();
3961 shelf_50pct_power = config_GetShelfPowerMode();
3962 aor_present = config_IsAorRsPresent();
3964 rs_type = ddb_GetRsRaidType(1);
3965 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3966 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3967 EPRINT(0, "Unsupported RS type %d!", rs_type);
3971 page->total_luns = num_luns;
3973 switch (config_type) {
3976 * In a 40 drive configuration, it doesn't matter what DC
3977 * cards we have, whether we have AOR enabled or not,
3978 * partitioning or not, or what type of RAIDset we have.
3979 * In that scenario, we can power up every LUN we present
3982 max_active_luns = num_luns;
3986 if (shelf_50pct_power == CS_FALSE) {
3988 if (aor_present == CS_TRUE) {
3990 CS_RAIDSET_PERSONALITY_RAID5) {
3991 max_active_luns = 7;
3992 } else if (rs_type ==
3993 CS_RAIDSET_PERSONALITY_RAID1){
3994 max_active_luns = 14;
3996 /* XXX KDM now what?? */
4000 CS_RAIDSET_PERSONALITY_RAID5) {
4001 max_active_luns = 8;
4002 } else if (rs_type ==
4003 CS_RAIDSET_PERSONALITY_RAID1){
4004 max_active_luns = 16;
4006 /* XXX KDM now what?? */
4012 * With 50% power in a 64 drive configuration, we
4013 * can power all LUNs we present.
4015 max_active_luns = num_luns;
4019 if (shelf_50pct_power == CS_FALSE) {
4021 if (aor_present == CS_TRUE) {
4023 CS_RAIDSET_PERSONALITY_RAID5) {
4024 max_active_luns = 7;
4025 } else if (rs_type ==
4026 CS_RAIDSET_PERSONALITY_RAID1){
4027 max_active_luns = 14;
4029 /* XXX KDM now what?? */
4033 CS_RAIDSET_PERSONALITY_RAID5) {
4034 max_active_luns = 8;
4035 } else if (rs_type ==
4036 CS_RAIDSET_PERSONALITY_RAID1){
4037 max_active_luns = 16;
4039 /* XXX KDM now what?? */
4044 if (aor_present == CS_TRUE) {
4046 CS_RAIDSET_PERSONALITY_RAID5) {
4047 max_active_luns = 14;
4048 } else if (rs_type ==
4049 CS_RAIDSET_PERSONALITY_RAID1){
4051 * We're assuming here that disk
4052 * caching is enabled, and so we're
4053 * able to power up half of each
4054 * LUN, and cache all writes.
4056 max_active_luns = num_luns;
4058 /* XXX KDM now what?? */
4062 CS_RAIDSET_PERSONALITY_RAID5) {
4063 max_active_luns = 15;
4064 } else if (rs_type ==
4065 CS_RAIDSET_PERSONALITY_RAID1){
4066 max_active_luns = 30;
4068 /* XXX KDM now what?? */
4075 * In this case, we have an unknown configuration, so we
4076 * just use the default from above.
4081 page->max_active_luns = max_active_luns;
4083 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
4084 page->total_luns, page->max_active_luns);
4087 #endif /* NEEDTOPORT */
4090 * This routine could be used in the future to load default and/or saved
4091 * mode page parameters for a particuar lun.
4094 ctl_init_page_index(struct ctl_lun *lun)
4097 struct ctl_page_index *page_index;
4098 struct ctl_softc *softc;
4101 memcpy(&lun->mode_pages.index, page_index_template,
4102 sizeof(page_index_template));
4104 softc = lun->ctl_softc;
4106 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4108 page_index = &lun->mode_pages.index[i];
4110 * If this is a disk-only mode page, there's no point in
4111 * setting it up. For some pages, we have to have some
4112 * basic information about the disk in order to calculate the
4115 if ((lun->be_lun->lun_type != T_DIRECT)
4116 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4119 switch (page_index->page_code & SMPH_PC_MASK) {
4120 case SMS_FORMAT_DEVICE_PAGE: {
4121 struct scsi_format_page *format_page;
4123 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4124 panic("subpage is incorrect!");
4127 * Sectors per track are set above. Bytes per
4128 * sector need to be set here on a per-LUN basis.
4130 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4131 &format_page_default,
4132 sizeof(format_page_default));
4133 memcpy(&lun->mode_pages.format_page[
4134 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4135 sizeof(format_page_changeable));
4136 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4137 &format_page_default,
4138 sizeof(format_page_default));
4139 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4140 &format_page_default,
4141 sizeof(format_page_default));
4143 format_page = &lun->mode_pages.format_page[
4145 scsi_ulto2b(lun->be_lun->blocksize,
4146 format_page->bytes_per_sector);
4148 format_page = &lun->mode_pages.format_page[
4150 scsi_ulto2b(lun->be_lun->blocksize,
4151 format_page->bytes_per_sector);
4153 format_page = &lun->mode_pages.format_page[
4155 scsi_ulto2b(lun->be_lun->blocksize,
4156 format_page->bytes_per_sector);
4158 page_index->page_data =
4159 (uint8_t *)lun->mode_pages.format_page;
4162 case SMS_RIGID_DISK_PAGE: {
4163 struct scsi_rigid_disk_page *rigid_disk_page;
4164 uint32_t sectors_per_cylinder;
4168 #endif /* !__XSCALE__ */
4170 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4171 panic("invalid subpage value %d",
4172 page_index->subpage);
4175 * Rotation rate and sectors per track are set
4176 * above. We calculate the cylinders here based on
4177 * capacity. Due to the number of heads and
4178 * sectors per track we're using, smaller arrays
4179 * may turn out to have 0 cylinders. Linux and
4180 * FreeBSD don't pay attention to these mode pages
4181 * to figure out capacity, but Solaris does. It
4182 * seems to deal with 0 cylinders just fine, and
4183 * works out a fake geometry based on the capacity.
4185 memcpy(&lun->mode_pages.rigid_disk_page[
4186 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4187 sizeof(rigid_disk_page_default));
4188 memcpy(&lun->mode_pages.rigid_disk_page[
4189 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4190 sizeof(rigid_disk_page_changeable));
4191 memcpy(&lun->mode_pages.rigid_disk_page[
4192 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4193 sizeof(rigid_disk_page_default));
4194 memcpy(&lun->mode_pages.rigid_disk_page[
4195 CTL_PAGE_SAVED], &rigid_disk_page_default,
4196 sizeof(rigid_disk_page_default));
4198 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4202 * The divide method here will be more accurate,
4203 * probably, but results in floating point being
4204 * used in the kernel on i386 (__udivdi3()). On the
4205 * XScale, though, __udivdi3() is implemented in
4208 * The shift method for cylinder calculation is
4209 * accurate if sectors_per_cylinder is a power of
4210 * 2. Otherwise it might be slightly off -- you
4211 * might have a bit of a truncation problem.
4214 cylinders = (lun->be_lun->maxlba + 1) /
4215 sectors_per_cylinder;
4217 for (shift = 31; shift > 0; shift--) {
4218 if (sectors_per_cylinder & (1 << shift))
4221 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4225 * We've basically got 3 bytes, or 24 bits for the
4226 * cylinder size in the mode page. If we're over,
4227 * just round down to 2^24.
4229 if (cylinders > 0xffffff)
4230 cylinders = 0xffffff;
4232 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4234 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4236 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4238 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4240 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4242 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4244 page_index->page_data =
4245 (uint8_t *)lun->mode_pages.rigid_disk_page;
4248 case SMS_CACHING_PAGE: {
4249 struct scsi_caching_page *caching_page;
4251 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4252 panic("invalid subpage value %d",
4253 page_index->subpage);
4254 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4255 &caching_page_default,
4256 sizeof(caching_page_default));
4257 memcpy(&lun->mode_pages.caching_page[
4258 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4259 sizeof(caching_page_changeable));
4260 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4261 &caching_page_default,
4262 sizeof(caching_page_default));
4263 caching_page = &lun->mode_pages.caching_page[
4265 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4266 if (value != NULL && strcmp(value, "off") == 0)
4267 caching_page->flags1 &= ~SCP_WCE;
4268 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4269 if (value != NULL && strcmp(value, "off") == 0)
4270 caching_page->flags1 |= SCP_RCD;
4271 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4272 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4273 sizeof(caching_page_default));
4274 page_index->page_data =
4275 (uint8_t *)lun->mode_pages.caching_page;
4278 case SMS_CONTROL_MODE_PAGE: {
4279 struct scsi_control_page *control_page;
4281 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4282 panic("invalid subpage value %d",
4283 page_index->subpage);
4285 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4286 &control_page_default,
4287 sizeof(control_page_default));
4288 memcpy(&lun->mode_pages.control_page[
4289 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4290 sizeof(control_page_changeable));
4291 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4292 &control_page_default,
4293 sizeof(control_page_default));
4294 control_page = &lun->mode_pages.control_page[
4296 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4297 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4298 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4299 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4301 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4302 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4303 sizeof(control_page_default));
4304 page_index->page_data =
4305 (uint8_t *)lun->mode_pages.control_page;
4309 case SMS_VENDOR_SPECIFIC_PAGE:{
4310 switch (page_index->subpage) {
4311 case PWR_SUBPAGE_CODE: {
4312 struct copan_power_subpage *current_page,
4315 memcpy(&lun->mode_pages.power_subpage[
4317 &power_page_default,
4318 sizeof(power_page_default));
4319 memcpy(&lun->mode_pages.power_subpage[
4320 CTL_PAGE_CHANGEABLE],
4321 &power_page_changeable,
4322 sizeof(power_page_changeable));
4323 memcpy(&lun->mode_pages.power_subpage[
4325 &power_page_default,
4326 sizeof(power_page_default));
4327 memcpy(&lun->mode_pages.power_subpage[
4329 &power_page_default,
4330 sizeof(power_page_default));
4331 page_index->page_data =
4332 (uint8_t *)lun->mode_pages.power_subpage;
4334 current_page = (struct copan_power_subpage *)
4335 (page_index->page_data +
4336 (page_index->page_len *
4338 saved_page = (struct copan_power_subpage *)
4339 (page_index->page_data +
4340 (page_index->page_len *
4344 case APS_SUBPAGE_CODE: {
4345 struct copan_aps_subpage *current_page,
4348 // This gets set multiple times but
4349 // it should always be the same. It's
4350 // only done during init so who cares.
4351 index_to_aps_page = i;
4353 memcpy(&lun->mode_pages.aps_subpage[
4356 sizeof(aps_page_default));
4357 memcpy(&lun->mode_pages.aps_subpage[
4358 CTL_PAGE_CHANGEABLE],
4359 &aps_page_changeable,
4360 sizeof(aps_page_changeable));
4361 memcpy(&lun->mode_pages.aps_subpage[
4364 sizeof(aps_page_default));
4365 memcpy(&lun->mode_pages.aps_subpage[
4368 sizeof(aps_page_default));
4369 page_index->page_data =
4370 (uint8_t *)lun->mode_pages.aps_subpage;
4372 current_page = (struct copan_aps_subpage *)
4373 (page_index->page_data +
4374 (page_index->page_len *
4376 saved_page = (struct copan_aps_subpage *)
4377 (page_index->page_data +
4378 (page_index->page_len *
4382 case DBGCNF_SUBPAGE_CODE: {
4383 struct copan_debugconf_subpage *current_page,
4386 memcpy(&lun->mode_pages.debugconf_subpage[
4388 &debugconf_page_default,
4389 sizeof(debugconf_page_default));
4390 memcpy(&lun->mode_pages.debugconf_subpage[
4391 CTL_PAGE_CHANGEABLE],
4392 &debugconf_page_changeable,
4393 sizeof(debugconf_page_changeable));
4394 memcpy(&lun->mode_pages.debugconf_subpage[
4396 &debugconf_page_default,
4397 sizeof(debugconf_page_default));
4398 memcpy(&lun->mode_pages.debugconf_subpage[
4400 &debugconf_page_default,
4401 sizeof(debugconf_page_default));
4402 page_index->page_data =
4403 (uint8_t *)lun->mode_pages.debugconf_subpage;
4405 current_page = (struct copan_debugconf_subpage *)
4406 (page_index->page_data +
4407 (page_index->page_len *
4409 saved_page = (struct copan_debugconf_subpage *)
4410 (page_index->page_data +
4411 (page_index->page_len *
4416 panic("invalid subpage value %d",
4417 page_index->subpage);
4423 panic("invalid page value %d",
4424 page_index->page_code & SMPH_PC_MASK);
4429 return (CTL_RETVAL_COMPLETE);
4436 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4437 * wants us to allocate the LUN and he can block.
4438 * - ctl_softc is always set
4439 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4441 * Returns 0 for success, non-zero (errno) for failure.
4444 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4445 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4447 struct ctl_lun *nlun, *lun;
4448 struct ctl_port *port;
4449 struct scsi_vpd_id_descriptor *desc;
4450 struct scsi_vpd_id_t10 *t10id;
4451 const char *eui, *naa, *scsiname, *vendor, *value;
4452 int lun_number, i, lun_malloced;
4453 int devidlen, idlen1, idlen2 = 0, len;
4459 * We currently only support Direct Access or Processor LUN types.
4461 switch (be_lun->lun_type) {
4469 be_lun->lun_config_status(be_lun->be_lun,
4470 CTL_LUN_CONFIG_FAILURE);
4473 if (ctl_lun == NULL) {
4474 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4481 memset(lun, 0, sizeof(*lun));
4483 lun->flags = CTL_LUN_MALLOCED;
4485 /* Generate LUN ID. */
4486 devidlen = max(CTL_DEVID_MIN_LEN,
4487 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4488 idlen1 = sizeof(*t10id) + devidlen;
4489 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4490 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4491 if (scsiname != NULL) {
4492 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4493 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4495 eui = ctl_get_opt(&be_lun->options, "eui");
4497 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4499 naa = ctl_get_opt(&be_lun->options, "naa");
4501 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4503 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4504 M_CTL, M_WAITOK | M_ZERO);
4505 lun->lun_devid->len = len;
4506 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4507 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4508 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4509 desc->length = idlen1;
4510 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4511 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4512 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4513 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4515 strncpy(t10id->vendor, vendor,
4516 min(sizeof(t10id->vendor), strlen(vendor)));
4518 strncpy((char *)t10id->vendor_spec_id,
4519 (char *)be_lun->device_id, devidlen);
4520 if (scsiname != NULL) {
4521 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4523 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4524 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4525 SVPD_ID_TYPE_SCSI_NAME;
4526 desc->length = idlen2;
4527 strlcpy(desc->identifier, scsiname, idlen2);
4530 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4532 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4533 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4536 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4539 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4541 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4542 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4545 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4548 mtx_lock(&ctl_softc->ctl_lock);
4550 * See if the caller requested a particular LUN number. If so, see
4551 * if it is available. Otherwise, allocate the first available LUN.
4553 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4554 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4555 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4556 mtx_unlock(&ctl_softc->ctl_lock);
4557 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4558 printf("ctl: requested LUN ID %d is higher "
4559 "than CTL_MAX_LUNS - 1 (%d)\n",
4560 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4563 * XXX KDM return an error, or just assign
4564 * another LUN ID in this case??
4566 printf("ctl: requested LUN ID %d is already "
4567 "in use\n", be_lun->req_lun_id);
4569 if (lun->flags & CTL_LUN_MALLOCED)
4571 be_lun->lun_config_status(be_lun->be_lun,
4572 CTL_LUN_CONFIG_FAILURE);
4575 lun_number = be_lun->req_lun_id;
4577 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4578 if (lun_number == -1) {
4579 mtx_unlock(&ctl_softc->ctl_lock);
4580 printf("ctl: can't allocate LUN on target %ju, out of "
4581 "LUNs\n", (uintmax_t)target_id.id);
4582 if (lun->flags & CTL_LUN_MALLOCED)
4584 be_lun->lun_config_status(be_lun->be_lun,
4585 CTL_LUN_CONFIG_FAILURE);
4589 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4591 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4592 lun->target = target_id;
4593 lun->lun = lun_number;
4594 lun->be_lun = be_lun;
4596 * The processor LUN is always enabled. Disk LUNs come on line
4597 * disabled, and must be enabled by the backend.
4599 lun->flags |= CTL_LUN_DISABLED;
4600 lun->backend = be_lun->be;
4601 be_lun->ctl_lun = lun;
4602 be_lun->lun_id = lun_number;
4603 atomic_add_int(&be_lun->be->num_luns, 1);
4604 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4605 lun->flags |= CTL_LUN_STOPPED;
4607 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4608 lun->flags |= CTL_LUN_INOPERABLE;
4610 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4611 lun->flags |= CTL_LUN_PRIMARY_SC;
4613 value = ctl_get_opt(&be_lun->options, "readonly");
4614 if (value != NULL && strcmp(value, "on") == 0)
4615 lun->flags |= CTL_LUN_READONLY;
4617 lun->ctl_softc = ctl_softc;
4618 TAILQ_INIT(&lun->ooa_queue);
4619 TAILQ_INIT(&lun->blocked_queue);
4620 STAILQ_INIT(&lun->error_list);
4621 ctl_tpc_lun_init(lun);
4624 * Initialize the mode page index.
4626 ctl_init_page_index(lun);
4629 * Set the poweron UA for all initiators on this LUN only.
4631 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4632 lun->pending_ua[i] = CTL_UA_POWERON;
4635 * Now, before we insert this lun on the lun list, set the lun
4636 * inventory changed UA for all other luns.
4638 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4639 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4640 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4644 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4646 ctl_softc->ctl_luns[lun_number] = lun;
4648 ctl_softc->num_luns++;
4650 /* Setup statistics gathering */
4651 lun->stats.device_type = be_lun->lun_type;
4652 lun->stats.lun_number = lun_number;
4653 if (lun->stats.device_type == T_DIRECT)
4654 lun->stats.blocksize = be_lun->blocksize;
4656 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4657 for (i = 0;i < CTL_MAX_PORTS;i++)
4658 lun->stats.ports[i].targ_port = i;
4660 mtx_unlock(&ctl_softc->ctl_lock);
4662 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4665 * Run through each registered FETD and bring it online if it isn't
4666 * already. Enable the target ID if it hasn't been enabled, and
4667 * enable this particular LUN.
4669 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4672 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4674 printf("ctl_alloc_lun: FETD %s port %d returned error "
4675 "%d for lun_enable on target %ju lun %d\n",
4676 port->port_name, port->targ_port, retval,
4677 (uintmax_t)target_id.id, lun_number);
4679 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4687 * - LUN has already been marked invalid and any pending I/O has been taken
4691 ctl_free_lun(struct ctl_lun *lun)
4693 struct ctl_softc *softc;
4695 struct ctl_port *port;
4697 struct ctl_lun *nlun;
4700 softc = lun->ctl_softc;
4702 mtx_assert(&softc->ctl_lock, MA_OWNED);
4704 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4706 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4708 softc->ctl_luns[lun->lun] = NULL;
4710 if (!TAILQ_EMPTY(&lun->ooa_queue))
4711 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4716 * XXX KDM this scheme only works for a single target/multiple LUN
4717 * setup. It needs to be revamped for a multiple target scheme.
4719 * XXX KDM this results in port->lun_disable() getting called twice,
4720 * once when ctl_disable_lun() is called, and a second time here.
4721 * We really need to re-think the LUN disable semantics. There
4722 * should probably be several steps/levels to LUN removal:
4727 * Right now we only have a disable method when communicating to
4728 * the front end ports, at least for individual LUNs.
4731 STAILQ_FOREACH(port, &softc->port_list, links) {
4734 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4737 printf("ctl_free_lun: FETD %s port %d returned error "
4738 "%d for lun_disable on target %ju lun %jd\n",
4739 port->port_name, port->targ_port, retval,
4740 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4743 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4744 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4746 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4748 printf("ctl_free_lun: FETD %s port %d "
4749 "returned error %d for targ_disable on "
4750 "target %ju\n", port->port_name,
4751 port->targ_port, retval,
4752 (uintmax_t)lun->target.id);
4754 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4756 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4760 port->port_offline(port->onoff_arg);
4761 port->status &= ~CTL_PORT_STATUS_ONLINE;
4768 * Tell the backend to free resources, if this LUN has a backend.
4770 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4771 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4773 ctl_tpc_lun_shutdown(lun);
4774 mtx_destroy(&lun->lun_lock);
4775 free(lun->lun_devid, M_CTL);
4776 if (lun->flags & CTL_LUN_MALLOCED)
4779 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4780 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4781 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4789 ctl_create_lun(struct ctl_be_lun *be_lun)
4791 struct ctl_softc *ctl_softc;
4793 ctl_softc = control_softc;
4796 * ctl_alloc_lun() should handle all potential failure cases.
4798 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4802 ctl_add_lun(struct ctl_be_lun *be_lun)
4804 struct ctl_softc *ctl_softc = control_softc;
4806 mtx_lock(&ctl_softc->ctl_lock);
4807 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4808 mtx_unlock(&ctl_softc->ctl_lock);
4809 wakeup(&ctl_softc->pending_lun_queue);
4815 ctl_enable_lun(struct ctl_be_lun *be_lun)
4817 struct ctl_softc *ctl_softc;
4818 struct ctl_port *port, *nport;
4819 struct ctl_lun *lun;
4822 ctl_softc = control_softc;
4824 lun = (struct ctl_lun *)be_lun->ctl_lun;
4826 mtx_lock(&ctl_softc->ctl_lock);
4827 mtx_lock(&lun->lun_lock);
4828 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4830 * eh? Why did we get called if the LUN is already
4833 mtx_unlock(&lun->lun_lock);
4834 mtx_unlock(&ctl_softc->ctl_lock);
4837 lun->flags &= ~CTL_LUN_DISABLED;
4838 mtx_unlock(&lun->lun_lock);
4840 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4841 nport = STAILQ_NEXT(port, links);
4844 * Drop the lock while we call the FETD's enable routine.
4845 * This can lead to a callback into CTL (at least in the
4846 * case of the internal initiator frontend.
4848 mtx_unlock(&ctl_softc->ctl_lock);
4849 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4850 mtx_lock(&ctl_softc->ctl_lock);
4852 printf("%s: FETD %s port %d returned error "
4853 "%d for lun_enable on target %ju lun %jd\n",
4854 __func__, port->port_name, port->targ_port, retval,
4855 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4859 /* NOTE: TODO: why does lun enable affect port status? */
4860 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4865 mtx_unlock(&ctl_softc->ctl_lock);
4871 ctl_disable_lun(struct ctl_be_lun *be_lun)
4873 struct ctl_softc *ctl_softc;
4874 struct ctl_port *port;
4875 struct ctl_lun *lun;
4878 ctl_softc = control_softc;
4880 lun = (struct ctl_lun *)be_lun->ctl_lun;
4882 mtx_lock(&ctl_softc->ctl_lock);
4883 mtx_lock(&lun->lun_lock);
4884 if (lun->flags & CTL_LUN_DISABLED) {
4885 mtx_unlock(&lun->lun_lock);
4886 mtx_unlock(&ctl_softc->ctl_lock);
4889 lun->flags |= CTL_LUN_DISABLED;
4890 mtx_unlock(&lun->lun_lock);
4892 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4893 mtx_unlock(&ctl_softc->ctl_lock);
4895 * Drop the lock before we call the frontend's disable
4896 * routine, to avoid lock order reversals.
4898 * XXX KDM what happens if the frontend list changes while
4899 * we're traversing it? It's unlikely, but should be handled.
4901 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4903 mtx_lock(&ctl_softc->ctl_lock);
4905 printf("ctl_alloc_lun: FETD %s port %d returned error "
4906 "%d for lun_disable on target %ju lun %jd\n",
4907 port->port_name, port->targ_port, retval,
4908 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4912 mtx_unlock(&ctl_softc->ctl_lock);
4918 ctl_start_lun(struct ctl_be_lun *be_lun)
4920 struct ctl_softc *ctl_softc;
4921 struct ctl_lun *lun;
4923 ctl_softc = control_softc;
4925 lun = (struct ctl_lun *)be_lun->ctl_lun;
4927 mtx_lock(&lun->lun_lock);
4928 lun->flags &= ~CTL_LUN_STOPPED;
4929 mtx_unlock(&lun->lun_lock);
4935 ctl_stop_lun(struct ctl_be_lun *be_lun)
4937 struct ctl_softc *ctl_softc;
4938 struct ctl_lun *lun;
4940 ctl_softc = control_softc;
4942 lun = (struct ctl_lun *)be_lun->ctl_lun;
4944 mtx_lock(&lun->lun_lock);
4945 lun->flags |= CTL_LUN_STOPPED;
4946 mtx_unlock(&lun->lun_lock);
4952 ctl_lun_offline(struct ctl_be_lun *be_lun)
4954 struct ctl_softc *ctl_softc;
4955 struct ctl_lun *lun;
4957 ctl_softc = control_softc;
4959 lun = (struct ctl_lun *)be_lun->ctl_lun;
4961 mtx_lock(&lun->lun_lock);
4962 lun->flags |= CTL_LUN_OFFLINE;
4963 mtx_unlock(&lun->lun_lock);
4969 ctl_lun_online(struct ctl_be_lun *be_lun)
4971 struct ctl_softc *ctl_softc;
4972 struct ctl_lun *lun;
4974 ctl_softc = control_softc;
4976 lun = (struct ctl_lun *)be_lun->ctl_lun;
4978 mtx_lock(&lun->lun_lock);
4979 lun->flags &= ~CTL_LUN_OFFLINE;
4980 mtx_unlock(&lun->lun_lock);
4986 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4988 struct ctl_softc *ctl_softc;
4989 struct ctl_lun *lun;
4991 ctl_softc = control_softc;
4993 lun = (struct ctl_lun *)be_lun->ctl_lun;
4995 mtx_lock(&lun->lun_lock);
4998 * The LUN needs to be disabled before it can be marked invalid.
5000 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5001 mtx_unlock(&lun->lun_lock);
5005 * Mark the LUN invalid.
5007 lun->flags |= CTL_LUN_INVALID;
5010 * If there is nothing in the OOA queue, go ahead and free the LUN.
5011 * If we have something in the OOA queue, we'll free it when the
5012 * last I/O completes.
5014 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5015 mtx_unlock(&lun->lun_lock);
5016 mtx_lock(&ctl_softc->ctl_lock);
5018 mtx_unlock(&ctl_softc->ctl_lock);
5020 mtx_unlock(&lun->lun_lock);
5026 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5028 struct ctl_softc *ctl_softc;
5029 struct ctl_lun *lun;
5031 ctl_softc = control_softc;
5032 lun = (struct ctl_lun *)be_lun->ctl_lun;
5034 mtx_lock(&lun->lun_lock);
5035 lun->flags |= CTL_LUN_INOPERABLE;
5036 mtx_unlock(&lun->lun_lock);
5042 ctl_lun_operable(struct ctl_be_lun *be_lun)
5044 struct ctl_softc *ctl_softc;
5045 struct ctl_lun *lun;
5047 ctl_softc = control_softc;
5048 lun = (struct ctl_lun *)be_lun->ctl_lun;
5050 mtx_lock(&lun->lun_lock);
5051 lun->flags &= ~CTL_LUN_INOPERABLE;
5052 mtx_unlock(&lun->lun_lock);
5058 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5061 struct ctl_softc *softc;
5062 struct ctl_lun *lun;
5063 struct copan_aps_subpage *current_sp;
5064 struct ctl_page_index *page_index;
5067 softc = control_softc;
5069 mtx_lock(&softc->ctl_lock);
5071 lun = (struct ctl_lun *)be_lun->ctl_lun;
5072 mtx_lock(&lun->lun_lock);
5075 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5076 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5080 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5082 page_index = &lun->mode_pages.index[i];
5085 if (page_index == NULL) {
5086 mtx_unlock(&lun->lun_lock);
5087 mtx_unlock(&softc->ctl_lock);
5088 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5089 (uintmax_t)lun->lun);
5093 if ((softc->aps_locked_lun != 0)
5094 && (softc->aps_locked_lun != lun->lun)) {
5095 printf("%s: attempt to lock LUN %llu when %llu is already "
5097 mtx_unlock(&lun->lun_lock);
5098 mtx_unlock(&softc->ctl_lock);
5103 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5104 (page_index->page_len * CTL_PAGE_CURRENT));
5107 current_sp->lock_active = APS_LOCK_ACTIVE;
5108 softc->aps_locked_lun = lun->lun;
5110 current_sp->lock_active = 0;
5111 softc->aps_locked_lun = 0;
5116 * If we're in HA mode, try to send the lock message to the other
5119 if (ctl_is_single == 0) {
5121 union ctl_ha_msg lock_msg;
5123 lock_msg.hdr.nexus = *nexus;
5124 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5126 lock_msg.aps.lock_flag = 1;
5128 lock_msg.aps.lock_flag = 0;
5129 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5130 sizeof(lock_msg), 0);
5131 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5132 printf("%s: APS (lock=%d) error returned from "
5133 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5134 mtx_unlock(&lun->lun_lock);
5135 mtx_unlock(&softc->ctl_lock);
5140 mtx_unlock(&lun->lun_lock);
5141 mtx_unlock(&softc->ctl_lock);
5147 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5149 struct ctl_lun *lun;
5150 struct ctl_softc *softc;
5153 softc = control_softc;
5155 lun = (struct ctl_lun *)be_lun->ctl_lun;
5157 mtx_lock(&lun->lun_lock);
5159 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5160 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5162 mtx_unlock(&lun->lun_lock);
5166 * Backend "memory move is complete" callback for requests that never
5167 * make it down to say RAIDCore's configuration code.
5170 ctl_config_move_done(union ctl_io *io)
5174 retval = CTL_RETVAL_COMPLETE;
5177 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5179 * XXX KDM this shouldn't happen, but what if it does?
5181 if (io->io_hdr.io_type != CTL_IO_SCSI)
5182 panic("I/O type isn't CTL_IO_SCSI!");
5184 if ((io->io_hdr.port_status == 0)
5185 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5186 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5187 io->io_hdr.status = CTL_SUCCESS;
5188 else if ((io->io_hdr.port_status != 0)
5189 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5190 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5192 * For hardware error sense keys, the sense key
5193 * specific value is defined to be a retry count,
5194 * but we use it to pass back an internal FETD
5195 * error code. XXX KDM Hopefully the FETD is only
5196 * using 16 bits for an error code, since that's
5197 * all the space we have in the sks field.
5199 ctl_set_internal_failure(&io->scsiio,
5202 io->io_hdr.port_status);
5203 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5204 free(io->scsiio.kern_data_ptr, M_CTL);
5209 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5210 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5211 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5213 * XXX KDM just assuming a single pointer here, and not a
5214 * S/G list. If we start using S/G lists for config data,
5215 * we'll need to know how to clean them up here as well.
5217 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5218 free(io->scsiio.kern_data_ptr, M_CTL);
5219 /* Hopefully the user has already set the status... */
5223 * XXX KDM now we need to continue data movement. Some
5225 * - call ctl_scsiio() again? We don't do this for data
5226 * writes, because for those at least we know ahead of
5227 * time where the write will go and how long it is. For
5228 * config writes, though, that information is largely
5229 * contained within the write itself, thus we need to
5230 * parse out the data again.
5232 * - Call some other function once the data is in?
5236 * XXX KDM call ctl_scsiio() again for now, and check flag
5237 * bits to see whether we're allocated or not.
5239 retval = ctl_scsiio(&io->scsiio);
5246 * This gets called by a backend driver when it is done with a
5247 * data_submit method.
5250 ctl_data_submit_done(union ctl_io *io)
5253 * If the IO_CONT flag is set, we need to call the supplied
5254 * function to continue processing the I/O, instead of completing
5257 * If there is an error, though, we don't want to keep processing.
5258 * Instead, just send status back to the initiator.
5260 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5261 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5262 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5263 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5264 io->scsiio.io_cont(io);
5271 * This gets called by a backend driver when it is done with a
5272 * configuration write.
5275 ctl_config_write_done(union ctl_io *io)
5280 * If the IO_CONT flag is set, we need to call the supplied
5281 * function to continue processing the I/O, instead of completing
5284 * If there is an error, though, we don't want to keep processing.
5285 * Instead, just send status back to the initiator.
5287 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5288 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5289 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5290 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5291 io->scsiio.io_cont(io);
5295 * Since a configuration write can be done for commands that actually
5296 * have data allocated, like write buffer, and commands that have
5297 * no data, like start/stop unit, we need to check here.
5299 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5300 buf = io->scsiio.kern_data_ptr;
5309 * SCSI release command.
5312 ctl_scsi_release(struct ctl_scsiio *ctsio)
5314 int length, longid, thirdparty_id, resv_id;
5315 struct ctl_softc *ctl_softc;
5316 struct ctl_lun *lun;
5322 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5324 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5325 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5326 ctl_softc = control_softc;
5328 switch (ctsio->cdb[0]) {
5330 struct scsi_release_10 *cdb;
5332 cdb = (struct scsi_release_10 *)ctsio->cdb;
5334 if (cdb->byte2 & SR10_LONGID)
5337 thirdparty_id = cdb->thirdparty_id;
5339 resv_id = cdb->resv_id;
5340 length = scsi_2btoul(cdb->length);
5347 * XXX KDM right now, we only support LUN reservation. We don't
5348 * support 3rd party reservations, or extent reservations, which
5349 * might actually need the parameter list. If we've gotten this
5350 * far, we've got a LUN reservation. Anything else got kicked out
5351 * above. So, according to SPC, ignore the length.
5355 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5357 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5358 ctsio->kern_data_len = length;
5359 ctsio->kern_total_len = length;
5360 ctsio->kern_data_resid = 0;
5361 ctsio->kern_rel_offset = 0;
5362 ctsio->kern_sg_entries = 0;
5363 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5364 ctsio->be_move_done = ctl_config_move_done;
5365 ctl_datamove((union ctl_io *)ctsio);
5367 return (CTL_RETVAL_COMPLETE);
5371 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5373 mtx_lock(&lun->lun_lock);
5376 * According to SPC, it is not an error for an intiator to attempt
5377 * to release a reservation on a LUN that isn't reserved, or that
5378 * is reserved by another initiator. The reservation can only be
5379 * released, though, by the initiator who made it or by one of
5380 * several reset type events.
5382 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5383 lun->flags &= ~CTL_LUN_RESERVED;
5385 mtx_unlock(&lun->lun_lock);
5387 ctsio->scsi_status = SCSI_STATUS_OK;
5388 ctsio->io_hdr.status = CTL_SUCCESS;
5390 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5391 free(ctsio->kern_data_ptr, M_CTL);
5392 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5395 ctl_done((union ctl_io *)ctsio);
5396 return (CTL_RETVAL_COMPLETE);
5400 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5402 int extent, thirdparty, longid;
5403 int resv_id, length;
5404 uint64_t thirdparty_id;
5405 struct ctl_softc *ctl_softc;
5406 struct ctl_lun *lun;
5416 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5418 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5419 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5420 ctl_softc = control_softc;
5422 switch (ctsio->cdb[0]) {
5424 struct scsi_reserve_10 *cdb;
5426 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5428 if (cdb->byte2 & SR10_LONGID)
5431 thirdparty_id = cdb->thirdparty_id;
5433 resv_id = cdb->resv_id;
5434 length = scsi_2btoul(cdb->length);
5440 * XXX KDM right now, we only support LUN reservation. We don't
5441 * support 3rd party reservations, or extent reservations, which
5442 * might actually need the parameter list. If we've gotten this
5443 * far, we've got a LUN reservation. Anything else got kicked out
5444 * above. So, according to SPC, ignore the length.
5448 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5450 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5451 ctsio->kern_data_len = length;
5452 ctsio->kern_total_len = length;
5453 ctsio->kern_data_resid = 0;
5454 ctsio->kern_rel_offset = 0;
5455 ctsio->kern_sg_entries = 0;
5456 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5457 ctsio->be_move_done = ctl_config_move_done;
5458 ctl_datamove((union ctl_io *)ctsio);
5460 return (CTL_RETVAL_COMPLETE);
5464 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5466 mtx_lock(&lun->lun_lock);
5467 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5468 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5469 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5473 lun->flags |= CTL_LUN_RESERVED;
5474 lun->res_idx = residx;
5476 ctsio->scsi_status = SCSI_STATUS_OK;
5477 ctsio->io_hdr.status = CTL_SUCCESS;
5480 mtx_unlock(&lun->lun_lock);
5482 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5483 free(ctsio->kern_data_ptr, M_CTL);
5484 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5487 ctl_done((union ctl_io *)ctsio);
5488 return (CTL_RETVAL_COMPLETE);
5492 ctl_start_stop(struct ctl_scsiio *ctsio)
5494 struct scsi_start_stop_unit *cdb;
5495 struct ctl_lun *lun;
5496 struct ctl_softc *ctl_softc;
5499 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5501 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5502 ctl_softc = control_softc;
5505 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5509 * We don't support the immediate bit on a stop unit. In order to
5510 * do that, we would need to code up a way to know that a stop is
5511 * pending, and hold off any new commands until it completes, one
5512 * way or another. Then we could accept or reject those commands
5513 * depending on its status. We would almost need to do the reverse
5514 * of what we do below for an immediate start -- return the copy of
5515 * the ctl_io to the FETD with status to send to the host (and to
5516 * free the copy!) and then free the original I/O once the stop
5517 * actually completes. That way, the OOA queue mechanism can work
5518 * to block commands that shouldn't proceed. Another alternative
5519 * would be to put the copy in the queue in place of the original,
5520 * and return the original back to the caller. That could be
5523 if ((cdb->byte2 & SSS_IMMED)
5524 && ((cdb->how & SSS_START) == 0)) {
5525 ctl_set_invalid_field(ctsio,
5531 ctl_done((union ctl_io *)ctsio);
5532 return (CTL_RETVAL_COMPLETE);
5535 if ((lun->flags & CTL_LUN_PR_RESERVED)
5536 && ((cdb->how & SSS_START)==0)) {
5539 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5540 if (lun->pr_keys[residx] == 0
5541 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5543 ctl_set_reservation_conflict(ctsio);
5544 ctl_done((union ctl_io *)ctsio);
5545 return (CTL_RETVAL_COMPLETE);
5550 * If there is no backend on this device, we can't start or stop
5551 * it. In theory we shouldn't get any start/stop commands in the
5552 * first place at this level if the LUN doesn't have a backend.
5553 * That should get stopped by the command decode code.
5555 if (lun->backend == NULL) {
5556 ctl_set_invalid_opcode(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5562 * XXX KDM Copan-specific offline behavior.
5563 * Figure out a reasonable way to port this?
5566 mtx_lock(&lun->lun_lock);
5568 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5569 && (lun->flags & CTL_LUN_OFFLINE)) {
5571 * If the LUN is offline, and the on/offline bit isn't set,
5572 * reject the start or stop. Otherwise, let it through.
5574 mtx_unlock(&lun->lun_lock);
5575 ctl_set_lun_not_ready(ctsio);
5576 ctl_done((union ctl_io *)ctsio);
5578 mtx_unlock(&lun->lun_lock);
5579 #endif /* NEEDTOPORT */
5581 * This could be a start or a stop when we're online,
5582 * or a stop/offline or start/online. A start or stop when
5583 * we're offline is covered in the case above.
5586 * In the non-immediate case, we send the request to
5587 * the backend and return status to the user when
5590 * In the immediate case, we allocate a new ctl_io
5591 * to hold a copy of the request, and send that to
5592 * the backend. We then set good status on the
5593 * user's request and return it immediately.
5595 if (cdb->byte2 & SSS_IMMED) {
5596 union ctl_io *new_io;
5598 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5599 if (new_io == NULL) {
5600 ctl_set_busy(ctsio);
5601 ctl_done((union ctl_io *)ctsio);
5603 ctl_copy_io((union ctl_io *)ctsio,
5605 retval = lun->backend->config_write(new_io);
5606 ctl_set_success(ctsio);
5607 ctl_done((union ctl_io *)ctsio);
5610 retval = lun->backend->config_write(
5611 (union ctl_io *)ctsio);
5620 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5621 * we don't really do anything with the LBA and length fields if the user
5622 * passes them in. Instead we'll just flush out the cache for the entire
5626 ctl_sync_cache(struct ctl_scsiio *ctsio)
5628 struct ctl_lun *lun;
5629 struct ctl_softc *ctl_softc;
5630 uint64_t starting_lba;
5631 uint32_t block_count;
5634 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5636 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5637 ctl_softc = control_softc;
5640 switch (ctsio->cdb[0]) {
5641 case SYNCHRONIZE_CACHE: {
5642 struct scsi_sync_cache *cdb;
5643 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5645 starting_lba = scsi_4btoul(cdb->begin_lba);
5646 block_count = scsi_2btoul(cdb->lb_count);
5649 case SYNCHRONIZE_CACHE_16: {
5650 struct scsi_sync_cache_16 *cdb;
5651 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5653 starting_lba = scsi_8btou64(cdb->begin_lba);
5654 block_count = scsi_4btoul(cdb->lb_count);
5658 ctl_set_invalid_opcode(ctsio);
5659 ctl_done((union ctl_io *)ctsio);
5661 break; /* NOTREACHED */
5665 * We check the LBA and length, but don't do anything with them.
5666 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5667 * get flushed. This check will just help satisfy anyone who wants
5668 * to see an error for an out of range LBA.
5670 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5671 ctl_set_lba_out_of_range(ctsio);
5672 ctl_done((union ctl_io *)ctsio);
5677 * If this LUN has no backend, we can't flush the cache anyway.
5679 if (lun->backend == NULL) {
5680 ctl_set_invalid_opcode(ctsio);
5681 ctl_done((union ctl_io *)ctsio);
5686 * Check to see whether we're configured to send the SYNCHRONIZE
5687 * CACHE command directly to the back end.
5689 mtx_lock(&lun->lun_lock);
5690 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5691 && (++(lun->sync_count) >= lun->sync_interval)) {
5692 lun->sync_count = 0;
5693 mtx_unlock(&lun->lun_lock);
5694 retval = lun->backend->config_write((union ctl_io *)ctsio);
5696 mtx_unlock(&lun->lun_lock);
5697 ctl_set_success(ctsio);
5698 ctl_done((union ctl_io *)ctsio);
5707 ctl_format(struct ctl_scsiio *ctsio)
5709 struct scsi_format *cdb;
5710 struct ctl_lun *lun;
5711 struct ctl_softc *ctl_softc;
5712 int length, defect_list_len;
5714 CTL_DEBUG_PRINT(("ctl_format\n"));
5716 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5717 ctl_softc = control_softc;
5719 cdb = (struct scsi_format *)ctsio->cdb;
5722 if (cdb->byte2 & SF_FMTDATA) {
5723 if (cdb->byte2 & SF_LONGLIST)
5724 length = sizeof(struct scsi_format_header_long);
5726 length = sizeof(struct scsi_format_header_short);
5729 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5731 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5732 ctsio->kern_data_len = length;
5733 ctsio->kern_total_len = length;
5734 ctsio->kern_data_resid = 0;
5735 ctsio->kern_rel_offset = 0;
5736 ctsio->kern_sg_entries = 0;
5737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5738 ctsio->be_move_done = ctl_config_move_done;
5739 ctl_datamove((union ctl_io *)ctsio);
5741 return (CTL_RETVAL_COMPLETE);
5744 defect_list_len = 0;
5746 if (cdb->byte2 & SF_FMTDATA) {
5747 if (cdb->byte2 & SF_LONGLIST) {
5748 struct scsi_format_header_long *header;
5750 header = (struct scsi_format_header_long *)
5751 ctsio->kern_data_ptr;
5753 defect_list_len = scsi_4btoul(header->defect_list_len);
5754 if (defect_list_len != 0) {
5755 ctl_set_invalid_field(ctsio,
5764 struct scsi_format_header_short *header;
5766 header = (struct scsi_format_header_short *)
5767 ctsio->kern_data_ptr;
5769 defect_list_len = scsi_2btoul(header->defect_list_len);
5770 if (defect_list_len != 0) {
5771 ctl_set_invalid_field(ctsio,
5783 * The format command will clear out the "Medium format corrupted"
5784 * status if set by the configuration code. That status is really
5785 * just a way to notify the host that we have lost the media, and
5786 * get them to issue a command that will basically make them think
5787 * they're blowing away the media.
5789 mtx_lock(&lun->lun_lock);
5790 lun->flags &= ~CTL_LUN_INOPERABLE;
5791 mtx_unlock(&lun->lun_lock);
5793 ctsio->scsi_status = SCSI_STATUS_OK;
5794 ctsio->io_hdr.status = CTL_SUCCESS;
5797 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5798 free(ctsio->kern_data_ptr, M_CTL);
5799 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5802 ctl_done((union ctl_io *)ctsio);
5803 return (CTL_RETVAL_COMPLETE);
5807 ctl_read_buffer(struct ctl_scsiio *ctsio)
5809 struct scsi_read_buffer *cdb;
5810 struct ctl_lun *lun;
5811 int buffer_offset, len;
5812 static uint8_t descr[4];
5813 static uint8_t echo_descr[4] = { 0 };
5815 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5817 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5818 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5820 if (lun->flags & CTL_LUN_PR_RESERVED) {
5824 * XXX KDM need a lock here.
5826 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5827 if ((lun->res_type == SPR_TYPE_EX_AC
5828 && residx != lun->pr_res_idx)
5829 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5830 || lun->res_type == SPR_TYPE_EX_AC_AR)
5831 && lun->pr_keys[residx] == 0)) {
5832 ctl_set_reservation_conflict(ctsio);
5833 ctl_done((union ctl_io *)ctsio);
5834 return (CTL_RETVAL_COMPLETE);
5838 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5839 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5840 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5841 ctl_set_invalid_field(ctsio,
5847 ctl_done((union ctl_io *)ctsio);
5848 return (CTL_RETVAL_COMPLETE);
5851 len = scsi_3btoul(cdb->length);
5852 buffer_offset = scsi_3btoul(cdb->offset);
5854 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5855 ctl_set_invalid_field(ctsio,
5861 ctl_done((union ctl_io *)ctsio);
5862 return (CTL_RETVAL_COMPLETE);
5865 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5867 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5868 ctsio->kern_data_ptr = descr;
5869 len = min(len, sizeof(descr));
5870 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5871 ctsio->kern_data_ptr = echo_descr;
5872 len = min(len, sizeof(echo_descr));
5874 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5875 ctsio->kern_data_len = len;
5876 ctsio->kern_total_len = len;
5877 ctsio->kern_data_resid = 0;
5878 ctsio->kern_rel_offset = 0;
5879 ctsio->kern_sg_entries = 0;
5880 ctsio->be_move_done = ctl_config_move_done;
5881 ctl_datamove((union ctl_io *)ctsio);
5883 return (CTL_RETVAL_COMPLETE);
5887 ctl_write_buffer(struct ctl_scsiio *ctsio)
5889 struct scsi_write_buffer *cdb;
5890 struct ctl_lun *lun;
5891 int buffer_offset, len;
5893 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5895 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5896 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5898 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5899 ctl_set_invalid_field(ctsio,
5905 ctl_done((union ctl_io *)ctsio);
5906 return (CTL_RETVAL_COMPLETE);
5909 len = scsi_3btoul(cdb->length);
5910 buffer_offset = scsi_3btoul(cdb->offset);
5912 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5913 ctl_set_invalid_field(ctsio,
5919 ctl_done((union ctl_io *)ctsio);
5920 return (CTL_RETVAL_COMPLETE);
5924 * If we've got a kernel request that hasn't been malloced yet,
5925 * malloc it and tell the caller the data buffer is here.
5927 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5928 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5929 ctsio->kern_data_len = len;
5930 ctsio->kern_total_len = len;
5931 ctsio->kern_data_resid = 0;
5932 ctsio->kern_rel_offset = 0;
5933 ctsio->kern_sg_entries = 0;
5934 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5935 ctsio->be_move_done = ctl_config_move_done;
5936 ctl_datamove((union ctl_io *)ctsio);
5938 return (CTL_RETVAL_COMPLETE);
5941 ctl_done((union ctl_io *)ctsio);
5943 return (CTL_RETVAL_COMPLETE);
5947 ctl_write_same(struct ctl_scsiio *ctsio)
5949 struct ctl_lun *lun;
5950 struct ctl_lba_len_flags *lbalen;
5952 uint32_t num_blocks;
5956 retval = CTL_RETVAL_COMPLETE;
5958 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5960 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5962 switch (ctsio->cdb[0]) {
5963 case WRITE_SAME_10: {
5964 struct scsi_write_same_10 *cdb;
5966 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5968 lba = scsi_4btoul(cdb->addr);
5969 num_blocks = scsi_2btoul(cdb->length);
5973 case WRITE_SAME_16: {
5974 struct scsi_write_same_16 *cdb;
5976 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5978 lba = scsi_8btou64(cdb->addr);
5979 num_blocks = scsi_4btoul(cdb->length);
5985 * We got a command we don't support. This shouldn't
5986 * happen, commands should be filtered out above us.
5988 ctl_set_invalid_opcode(ctsio);
5989 ctl_done((union ctl_io *)ctsio);
5991 return (CTL_RETVAL_COMPLETE);
5992 break; /* NOTREACHED */
5995 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5996 if ((byte2 & SWS_UNMAP) == 0 &&
5997 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5998 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5999 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
6000 ctl_done((union ctl_io *)ctsio);
6001 return (CTL_RETVAL_COMPLETE);
6005 * The first check is to make sure we're in bounds, the second
6006 * check is to catch wrap-around problems. If the lba + num blocks
6007 * is less than the lba, then we've wrapped around and the block
6008 * range is invalid anyway.
6010 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6011 || ((lba + num_blocks) < lba)) {
6012 ctl_set_lba_out_of_range(ctsio);
6013 ctl_done((union ctl_io *)ctsio);
6014 return (CTL_RETVAL_COMPLETE);
6017 /* Zero number of blocks means "to the last logical block" */
6018 if (num_blocks == 0) {
6019 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
6020 ctl_set_invalid_field(ctsio,
6026 ctl_done((union ctl_io *)ctsio);
6027 return (CTL_RETVAL_COMPLETE);
6029 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6032 len = lun->be_lun->blocksize;
6035 * If we've got a kernel request that hasn't been malloced yet,
6036 * malloc it and tell the caller the data buffer is here.
6038 if ((byte2 & SWS_NDOB) == 0 &&
6039 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6040 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6041 ctsio->kern_data_len = len;
6042 ctsio->kern_total_len = len;
6043 ctsio->kern_data_resid = 0;
6044 ctsio->kern_rel_offset = 0;
6045 ctsio->kern_sg_entries = 0;
6046 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6047 ctsio->be_move_done = ctl_config_move_done;
6048 ctl_datamove((union ctl_io *)ctsio);
6050 return (CTL_RETVAL_COMPLETE);
6053 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6055 lbalen->len = num_blocks;
6056 lbalen->flags = byte2;
6057 retval = lun->backend->config_write((union ctl_io *)ctsio);
6063 ctl_unmap(struct ctl_scsiio *ctsio)
6065 struct ctl_lun *lun;
6066 struct scsi_unmap *cdb;
6067 struct ctl_ptr_len_flags *ptrlen;
6068 struct scsi_unmap_header *hdr;
6069 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6071 uint32_t num_blocks;
6075 retval = CTL_RETVAL_COMPLETE;
6077 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6080 cdb = (struct scsi_unmap *)ctsio->cdb;
6082 len = scsi_2btoul(cdb->length);
6086 * If we've got a kernel request that hasn't been malloced yet,
6087 * malloc it and tell the caller the data buffer is here.
6089 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6090 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6091 ctsio->kern_data_len = len;
6092 ctsio->kern_total_len = len;
6093 ctsio->kern_data_resid = 0;
6094 ctsio->kern_rel_offset = 0;
6095 ctsio->kern_sg_entries = 0;
6096 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6097 ctsio->be_move_done = ctl_config_move_done;
6098 ctl_datamove((union ctl_io *)ctsio);
6100 return (CTL_RETVAL_COMPLETE);
6103 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6104 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6105 if (len < sizeof (*hdr) ||
6106 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6107 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6108 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6109 ctl_set_invalid_field(ctsio,
6115 ctl_done((union ctl_io *)ctsio);
6116 return (CTL_RETVAL_COMPLETE);
6118 len = scsi_2btoul(hdr->desc_length);
6119 buf = (struct scsi_unmap_desc *)(hdr + 1);
6120 end = buf + len / sizeof(*buf);
6123 for (range = buf; range < end; range++) {
6124 lba = scsi_8btou64(range->lba);
6125 num_blocks = scsi_4btoul(range->length);
6126 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6127 || ((lba + num_blocks) < lba)) {
6128 ctl_set_lba_out_of_range(ctsio);
6129 ctl_done((union ctl_io *)ctsio);
6130 return (CTL_RETVAL_COMPLETE);
6132 if (num_blocks != 0)
6137 * Block backend can not handle zero last range.
6138 * Filter it out and return if there is nothing left.
6140 len = (uint8_t *)endnz - (uint8_t *)buf;
6142 ctl_set_success(ctsio);
6143 ctl_done((union ctl_io *)ctsio);
6144 return (CTL_RETVAL_COMPLETE);
6147 mtx_lock(&lun->lun_lock);
6148 ptrlen = (struct ctl_ptr_len_flags *)
6149 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6150 ptrlen->ptr = (void *)buf;
6152 ptrlen->flags = byte2;
6153 ctl_check_blocked(lun);
6154 mtx_unlock(&lun->lun_lock);
6156 retval = lun->backend->config_write((union ctl_io *)ctsio);
6161 * Note that this function currently doesn't actually do anything inside
6162 * CTL to enforce things if the DQue bit is turned on.
6164 * Also note that this function can't be used in the default case, because
6165 * the DQue bit isn't set in the changeable mask for the control mode page
6166 * anyway. This is just here as an example for how to implement a page
6167 * handler, and a placeholder in case we want to allow the user to turn
6168 * tagged queueing on and off.
6170 * The D_SENSE bit handling is functional, however, and will turn
6171 * descriptor sense on and off for a given LUN.
6174 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6175 struct ctl_page_index *page_index, uint8_t *page_ptr)
6177 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6178 struct ctl_lun *lun;
6179 struct ctl_softc *softc;
6183 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6184 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6187 user_cp = (struct scsi_control_page *)page_ptr;
6188 current_cp = (struct scsi_control_page *)
6189 (page_index->page_data + (page_index->page_len *
6191 saved_cp = (struct scsi_control_page *)
6192 (page_index->page_data + (page_index->page_len *
6195 softc = control_softc;
6197 mtx_lock(&lun->lun_lock);
6198 if (((current_cp->rlec & SCP_DSENSE) == 0)
6199 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6201 * Descriptor sense is currently turned off and the user
6202 * wants to turn it on.
6204 current_cp->rlec |= SCP_DSENSE;
6205 saved_cp->rlec |= SCP_DSENSE;
6206 lun->flags |= CTL_LUN_SENSE_DESC;
6208 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6209 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6211 * Descriptor sense is currently turned on, and the user
6212 * wants to turn it off.
6214 current_cp->rlec &= ~SCP_DSENSE;
6215 saved_cp->rlec &= ~SCP_DSENSE;
6216 lun->flags &= ~CTL_LUN_SENSE_DESC;
6219 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6220 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6221 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6222 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6223 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6224 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6227 if ((current_cp->eca_and_aen & SCP_SWP) !=
6228 (user_cp->eca_and_aen & SCP_SWP)) {
6229 current_cp->eca_and_aen &= ~SCP_SWP;
6230 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6231 saved_cp->eca_and_aen &= ~SCP_SWP;
6232 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6238 * Let other initiators know that the mode
6239 * parameters for this LUN have changed.
6241 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6245 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6248 mtx_unlock(&lun->lun_lock);
6254 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6255 struct ctl_page_index *page_index, uint8_t *page_ptr)
6257 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6258 struct ctl_lun *lun;
6262 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6263 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6266 user_cp = (struct scsi_caching_page *)page_ptr;
6267 current_cp = (struct scsi_caching_page *)
6268 (page_index->page_data + (page_index->page_len *
6270 saved_cp = (struct scsi_caching_page *)
6271 (page_index->page_data + (page_index->page_len *
6274 mtx_lock(&lun->lun_lock);
6275 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6276 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6277 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6278 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6279 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6280 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6286 * Let other initiators know that the mode
6287 * parameters for this LUN have changed.
6289 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6293 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6296 mtx_unlock(&lun->lun_lock);
6302 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6303 struct ctl_page_index *page_index, uint8_t *page_ptr)
6309 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6310 struct ctl_page_index *page_index, int pc)
6312 struct copan_power_subpage *page;
6314 page = (struct copan_power_subpage *)page_index->page_data +
6315 (page_index->page_len * pc);
6318 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6320 * We don't update the changable bits for this page.
6323 case SMS_PAGE_CTRL_CURRENT >> 6:
6324 case SMS_PAGE_CTRL_DEFAULT >> 6:
6325 case SMS_PAGE_CTRL_SAVED >> 6:
6327 ctl_update_power_subpage(page);
6332 EPRINT(0, "Invalid PC %d!!", pc);
6341 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6342 struct ctl_page_index *page_index, uint8_t *page_ptr)
6344 struct copan_aps_subpage *user_sp;
6345 struct copan_aps_subpage *current_sp;
6346 union ctl_modepage_info *modepage_info;
6347 struct ctl_softc *softc;
6348 struct ctl_lun *lun;
6351 retval = CTL_RETVAL_COMPLETE;
6352 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6353 (page_index->page_len * CTL_PAGE_CURRENT));
6354 softc = control_softc;
6355 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6357 user_sp = (struct copan_aps_subpage *)page_ptr;
6359 modepage_info = (union ctl_modepage_info *)
6360 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6362 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6363 modepage_info->header.subpage = page_index->subpage;
6364 modepage_info->aps.lock_active = user_sp->lock_active;
6366 mtx_lock(&softc->ctl_lock);
6369 * If there is a request to lock the LUN and another LUN is locked
6370 * this is an error. If the requested LUN is already locked ignore
6371 * the request. If no LUN is locked attempt to lock it.
6372 * if there is a request to unlock the LUN and the LUN is currently
6373 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6374 * if another LUN is locked or no LUN is locked.
6376 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6377 if (softc->aps_locked_lun == lun->lun) {
6379 * This LUN is already locked, so we're done.
6381 retval = CTL_RETVAL_COMPLETE;
6382 } else if (softc->aps_locked_lun == 0) {
6384 * No one has the lock, pass the request to the
6387 retval = lun->backend->config_write(
6388 (union ctl_io *)ctsio);
6391 * Someone else has the lock, throw out the request.
6393 ctl_set_already_locked(ctsio);
6394 free(ctsio->kern_data_ptr, M_CTL);
6395 ctl_done((union ctl_io *)ctsio);
6398 * Set the return value so that ctl_do_mode_select()
6399 * won't try to complete the command. We already
6400 * completed it here.
6402 retval = CTL_RETVAL_ERROR;
6404 } else if (softc->aps_locked_lun == lun->lun) {
6406 * This LUN is locked, so pass the unlock request to the
6409 retval = lun->backend->config_write((union ctl_io *)ctsio);
6411 mtx_unlock(&softc->ctl_lock);
6417 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6418 struct ctl_page_index *page_index,
6424 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6429 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6430 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6431 printf("page data:");
6433 printf(" %.2x",page_ptr[i]);
6439 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6440 struct ctl_page_index *page_index,
6443 struct copan_debugconf_subpage *page;
6445 page = (struct copan_debugconf_subpage *)page_index->page_data +
6446 (page_index->page_len * pc);
6449 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6450 case SMS_PAGE_CTRL_DEFAULT >> 6:
6451 case SMS_PAGE_CTRL_SAVED >> 6:
6453 * We don't update the changable or default bits for this page.
6456 case SMS_PAGE_CTRL_CURRENT >> 6:
6457 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6458 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6462 EPRINT(0, "Invalid PC %d!!", pc);
6463 #endif /* NEEDTOPORT */
6471 ctl_do_mode_select(union ctl_io *io)
6473 struct scsi_mode_page_header *page_header;
6474 struct ctl_page_index *page_index;
6475 struct ctl_scsiio *ctsio;
6476 int control_dev, page_len;
6477 int page_len_offset, page_len_size;
6478 union ctl_modepage_info *modepage_info;
6479 struct ctl_lun *lun;
6480 int *len_left, *len_used;
6483 ctsio = &io->scsiio;
6486 retval = CTL_RETVAL_COMPLETE;
6488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6490 if (lun->be_lun->lun_type != T_DIRECT)
6495 modepage_info = (union ctl_modepage_info *)
6496 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6497 len_left = &modepage_info->header.len_left;
6498 len_used = &modepage_info->header.len_used;
6502 page_header = (struct scsi_mode_page_header *)
6503 (ctsio->kern_data_ptr + *len_used);
6505 if (*len_left == 0) {
6506 free(ctsio->kern_data_ptr, M_CTL);
6507 ctl_set_success(ctsio);
6508 ctl_done((union ctl_io *)ctsio);
6509 return (CTL_RETVAL_COMPLETE);
6510 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6512 free(ctsio->kern_data_ptr, M_CTL);
6513 ctl_set_param_len_error(ctsio);
6514 ctl_done((union ctl_io *)ctsio);
6515 return (CTL_RETVAL_COMPLETE);
6517 } else if ((page_header->page_code & SMPH_SPF)
6518 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6520 free(ctsio->kern_data_ptr, M_CTL);
6521 ctl_set_param_len_error(ctsio);
6522 ctl_done((union ctl_io *)ctsio);
6523 return (CTL_RETVAL_COMPLETE);
6528 * XXX KDM should we do something with the block descriptor?
6530 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6532 if ((control_dev != 0)
6533 && (lun->mode_pages.index[i].page_flags &
6534 CTL_PAGE_FLAG_DISK_ONLY))
6537 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6538 (page_header->page_code & SMPH_PC_MASK))
6542 * If neither page has a subpage code, then we've got a
6545 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6546 && ((page_header->page_code & SMPH_SPF) == 0)) {
6547 page_index = &lun->mode_pages.index[i];
6548 page_len = page_header->page_length;
6553 * If both pages have subpages, then the subpage numbers
6556 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6557 && (page_header->page_code & SMPH_SPF)) {
6558 struct scsi_mode_page_header_sp *sph;
6560 sph = (struct scsi_mode_page_header_sp *)page_header;
6562 if (lun->mode_pages.index[i].subpage ==
6564 page_index = &lun->mode_pages.index[i];
6565 page_len = scsi_2btoul(sph->page_length);
6572 * If we couldn't find the page, or if we don't have a mode select
6573 * handler for it, send back an error to the user.
6575 if ((page_index == NULL)
6576 || (page_index->select_handler == NULL)) {
6577 ctl_set_invalid_field(ctsio,
6580 /*field*/ *len_used,
6583 free(ctsio->kern_data_ptr, M_CTL);
6584 ctl_done((union ctl_io *)ctsio);
6585 return (CTL_RETVAL_COMPLETE);
6588 if (page_index->page_code & SMPH_SPF) {
6589 page_len_offset = 2;
6593 page_len_offset = 1;
6597 * If the length the initiator gives us isn't the one we specify in
6598 * the mode page header, or if they didn't specify enough data in
6599 * the CDB to avoid truncating this page, kick out the request.
6601 if ((page_len != (page_index->page_len - page_len_offset -
6603 || (*len_left < page_index->page_len)) {
6606 ctl_set_invalid_field(ctsio,
6609 /*field*/ *len_used + page_len_offset,
6612 free(ctsio->kern_data_ptr, M_CTL);
6613 ctl_done((union ctl_io *)ctsio);
6614 return (CTL_RETVAL_COMPLETE);
6618 * Run through the mode page, checking to make sure that the bits
6619 * the user changed are actually legal for him to change.
6621 for (i = 0; i < page_index->page_len; i++) {
6622 uint8_t *user_byte, *change_mask, *current_byte;
6626 user_byte = (uint8_t *)page_header + i;
6627 change_mask = page_index->page_data +
6628 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6629 current_byte = page_index->page_data +
6630 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6633 * Check to see whether the user set any bits in this byte
6634 * that he is not allowed to set.
6636 if ((*user_byte & ~(*change_mask)) ==
6637 (*current_byte & ~(*change_mask)))
6641 * Go through bit by bit to determine which one is illegal.
6644 for (j = 7; j >= 0; j--) {
6645 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6646 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6651 ctl_set_invalid_field(ctsio,
6654 /*field*/ *len_used + i,
6657 free(ctsio->kern_data_ptr, M_CTL);
6658 ctl_done((union ctl_io *)ctsio);
6659 return (CTL_RETVAL_COMPLETE);
6663 * Decrement these before we call the page handler, since we may
6664 * end up getting called back one way or another before the handler
6665 * returns to this context.
6667 *len_left -= page_index->page_len;
6668 *len_used += page_index->page_len;
6670 retval = page_index->select_handler(ctsio, page_index,
6671 (uint8_t *)page_header);
6674 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6675 * wait until this queued command completes to finish processing
6676 * the mode page. If it returns anything other than
6677 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6678 * already set the sense information, freed the data pointer, and
6679 * completed the io for us.
6681 if (retval != CTL_RETVAL_COMPLETE)
6682 goto bailout_no_done;
6685 * If the initiator sent us more than one page, parse the next one.
6690 ctl_set_success(ctsio);
6691 free(ctsio->kern_data_ptr, M_CTL);
6692 ctl_done((union ctl_io *)ctsio);
6696 return (CTL_RETVAL_COMPLETE);
6701 ctl_mode_select(struct ctl_scsiio *ctsio)
6703 int param_len, pf, sp;
6704 int header_size, bd_len;
6705 int len_left, len_used;
6706 struct ctl_page_index *page_index;
6707 struct ctl_lun *lun;
6708 int control_dev, page_len;
6709 union ctl_modepage_info *modepage_info;
6721 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6723 if (lun->be_lun->lun_type != T_DIRECT)
6728 switch (ctsio->cdb[0]) {
6729 case MODE_SELECT_6: {
6730 struct scsi_mode_select_6 *cdb;
6732 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6734 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6735 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6737 param_len = cdb->length;
6738 header_size = sizeof(struct scsi_mode_header_6);
6741 case MODE_SELECT_10: {
6742 struct scsi_mode_select_10 *cdb;
6744 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6746 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6747 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6749 param_len = scsi_2btoul(cdb->length);
6750 header_size = sizeof(struct scsi_mode_header_10);
6754 ctl_set_invalid_opcode(ctsio);
6755 ctl_done((union ctl_io *)ctsio);
6756 return (CTL_RETVAL_COMPLETE);
6757 break; /* NOTREACHED */
6762 * "A parameter list length of zero indicates that the Data-Out Buffer
6763 * shall be empty. This condition shall not be considered as an error."
6765 if (param_len == 0) {
6766 ctl_set_success(ctsio);
6767 ctl_done((union ctl_io *)ctsio);
6768 return (CTL_RETVAL_COMPLETE);
6772 * Since we'll hit this the first time through, prior to
6773 * allocation, we don't need to free a data buffer here.
6775 if (param_len < header_size) {
6776 ctl_set_param_len_error(ctsio);
6777 ctl_done((union ctl_io *)ctsio);
6778 return (CTL_RETVAL_COMPLETE);
6782 * Allocate the data buffer and grab the user's data. In theory,
6783 * we shouldn't have to sanity check the parameter list length here
6784 * because the maximum size is 64K. We should be able to malloc
6785 * that much without too many problems.
6787 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6788 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6789 ctsio->kern_data_len = param_len;
6790 ctsio->kern_total_len = param_len;
6791 ctsio->kern_data_resid = 0;
6792 ctsio->kern_rel_offset = 0;
6793 ctsio->kern_sg_entries = 0;
6794 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6795 ctsio->be_move_done = ctl_config_move_done;
6796 ctl_datamove((union ctl_io *)ctsio);
6798 return (CTL_RETVAL_COMPLETE);
6801 switch (ctsio->cdb[0]) {
6802 case MODE_SELECT_6: {
6803 struct scsi_mode_header_6 *mh6;
6805 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6806 bd_len = mh6->blk_desc_len;
6809 case MODE_SELECT_10: {
6810 struct scsi_mode_header_10 *mh10;
6812 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6813 bd_len = scsi_2btoul(mh10->blk_desc_len);
6817 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6821 if (param_len < (header_size + bd_len)) {
6822 free(ctsio->kern_data_ptr, M_CTL);
6823 ctl_set_param_len_error(ctsio);
6824 ctl_done((union ctl_io *)ctsio);
6825 return (CTL_RETVAL_COMPLETE);
6829 * Set the IO_CONT flag, so that if this I/O gets passed to
6830 * ctl_config_write_done(), it'll get passed back to
6831 * ctl_do_mode_select() for further processing, or completion if
6834 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6835 ctsio->io_cont = ctl_do_mode_select;
6837 modepage_info = (union ctl_modepage_info *)
6838 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6840 memset(modepage_info, 0, sizeof(*modepage_info));
6842 len_left = param_len - header_size - bd_len;
6843 len_used = header_size + bd_len;
6845 modepage_info->header.len_left = len_left;
6846 modepage_info->header.len_used = len_used;
6848 return (ctl_do_mode_select((union ctl_io *)ctsio));
6852 ctl_mode_sense(struct ctl_scsiio *ctsio)
6854 struct ctl_lun *lun;
6855 int pc, page_code, dbd, llba, subpage;
6856 int alloc_len, page_len, header_len, total_len;
6857 struct scsi_mode_block_descr *block_desc;
6858 struct ctl_page_index *page_index;
6866 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6868 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6870 if (lun->be_lun->lun_type != T_DIRECT)
6875 if (lun->flags & CTL_LUN_PR_RESERVED) {
6879 * XXX KDM need a lock here.
6881 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6882 if ((lun->res_type == SPR_TYPE_EX_AC
6883 && residx != lun->pr_res_idx)
6884 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6885 || lun->res_type == SPR_TYPE_EX_AC_AR)
6886 && lun->pr_keys[residx] == 0)) {
6887 ctl_set_reservation_conflict(ctsio);
6888 ctl_done((union ctl_io *)ctsio);
6889 return (CTL_RETVAL_COMPLETE);
6893 switch (ctsio->cdb[0]) {
6894 case MODE_SENSE_6: {
6895 struct scsi_mode_sense_6 *cdb;
6897 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6899 header_len = sizeof(struct scsi_mode_hdr_6);
6900 if (cdb->byte2 & SMS_DBD)
6903 header_len += sizeof(struct scsi_mode_block_descr);
6905 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6906 page_code = cdb->page & SMS_PAGE_CODE;
6907 subpage = cdb->subpage;
6908 alloc_len = cdb->length;
6911 case MODE_SENSE_10: {
6912 struct scsi_mode_sense_10 *cdb;
6914 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6916 header_len = sizeof(struct scsi_mode_hdr_10);
6918 if (cdb->byte2 & SMS_DBD)
6921 header_len += sizeof(struct scsi_mode_block_descr);
6922 if (cdb->byte2 & SMS10_LLBAA)
6924 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6925 page_code = cdb->page & SMS_PAGE_CODE;
6926 subpage = cdb->subpage;
6927 alloc_len = scsi_2btoul(cdb->length);
6931 ctl_set_invalid_opcode(ctsio);
6932 ctl_done((union ctl_io *)ctsio);
6933 return (CTL_RETVAL_COMPLETE);
6934 break; /* NOTREACHED */
6938 * We have to make a first pass through to calculate the size of
6939 * the pages that match the user's query. Then we allocate enough
6940 * memory to hold it, and actually copy the data into the buffer.
6942 switch (page_code) {
6943 case SMS_ALL_PAGES_PAGE: {
6949 * At the moment, values other than 0 and 0xff here are
6950 * reserved according to SPC-3.
6952 if ((subpage != SMS_SUBPAGE_PAGE_0)
6953 && (subpage != SMS_SUBPAGE_ALL)) {
6954 ctl_set_invalid_field(ctsio,
6960 ctl_done((union ctl_io *)ctsio);
6961 return (CTL_RETVAL_COMPLETE);
6964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6965 if ((control_dev != 0)
6966 && (lun->mode_pages.index[i].page_flags &
6967 CTL_PAGE_FLAG_DISK_ONLY))
6971 * We don't use this subpage if the user didn't
6972 * request all subpages.
6974 if ((lun->mode_pages.index[i].subpage != 0)
6975 && (subpage == SMS_SUBPAGE_PAGE_0))
6979 printf("found page %#x len %d\n",
6980 lun->mode_pages.index[i].page_code &
6982 lun->mode_pages.index[i].page_len);
6984 page_len += lun->mode_pages.index[i].page_len;
6993 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6994 /* Look for the right page code */
6995 if ((lun->mode_pages.index[i].page_code &
6996 SMPH_PC_MASK) != page_code)
6999 /* Look for the right subpage or the subpage wildcard*/
7000 if ((lun->mode_pages.index[i].subpage != subpage)
7001 && (subpage != SMS_SUBPAGE_ALL))
7004 /* Make sure the page is supported for this dev type */
7005 if ((control_dev != 0)
7006 && (lun->mode_pages.index[i].page_flags &
7007 CTL_PAGE_FLAG_DISK_ONLY))
7011 printf("found page %#x len %d\n",
7012 lun->mode_pages.index[i].page_code &
7014 lun->mode_pages.index[i].page_len);
7017 page_len += lun->mode_pages.index[i].page_len;
7020 if (page_len == 0) {
7021 ctl_set_invalid_field(ctsio,
7027 ctl_done((union ctl_io *)ctsio);
7028 return (CTL_RETVAL_COMPLETE);
7034 total_len = header_len + page_len;
7036 printf("header_len = %d, page_len = %d, total_len = %d\n",
7037 header_len, page_len, total_len);
7040 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7041 ctsio->kern_sg_entries = 0;
7042 ctsio->kern_data_resid = 0;
7043 ctsio->kern_rel_offset = 0;
7044 if (total_len < alloc_len) {
7045 ctsio->residual = alloc_len - total_len;
7046 ctsio->kern_data_len = total_len;
7047 ctsio->kern_total_len = total_len;
7049 ctsio->residual = 0;
7050 ctsio->kern_data_len = alloc_len;
7051 ctsio->kern_total_len = alloc_len;
7054 switch (ctsio->cdb[0]) {
7055 case MODE_SENSE_6: {
7056 struct scsi_mode_hdr_6 *header;
7058 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
7060 header->datalen = ctl_min(total_len - 1, 254);
7061 if (control_dev == 0) {
7062 header->dev_specific = 0x10; /* DPOFUA */
7063 if ((lun->flags & CTL_LUN_READONLY) ||
7064 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
7065 .eca_and_aen & SCP_SWP) != 0)
7066 header->dev_specific |= 0x80; /* WP */
7069 header->block_descr_len = 0;
7071 header->block_descr_len =
7072 sizeof(struct scsi_mode_block_descr);
7073 block_desc = (struct scsi_mode_block_descr *)&header[1];
7076 case MODE_SENSE_10: {
7077 struct scsi_mode_hdr_10 *header;
7080 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7082 datalen = ctl_min(total_len - 2, 65533);
7083 scsi_ulto2b(datalen, header->datalen);
7084 if (control_dev == 0) {
7085 header->dev_specific = 0x10; /* DPOFUA */
7086 if ((lun->flags & CTL_LUN_READONLY) ||
7087 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
7088 .eca_and_aen & SCP_SWP) != 0)
7089 header->dev_specific |= 0x80; /* WP */
7092 scsi_ulto2b(0, header->block_descr_len);
7094 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7095 header->block_descr_len);
7096 block_desc = (struct scsi_mode_block_descr *)&header[1];
7100 panic("invalid CDB type %#x", ctsio->cdb[0]);
7101 break; /* NOTREACHED */
7105 * If we've got a disk, use its blocksize in the block
7106 * descriptor. Otherwise, just set it to 0.
7109 if (control_dev == 0)
7110 scsi_ulto3b(lun->be_lun->blocksize,
7111 block_desc->block_len);
7113 scsi_ulto3b(0, block_desc->block_len);
7116 switch (page_code) {
7117 case SMS_ALL_PAGES_PAGE: {
7120 data_used = header_len;
7121 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7122 struct ctl_page_index *page_index;
7124 page_index = &lun->mode_pages.index[i];
7126 if ((control_dev != 0)
7127 && (page_index->page_flags &
7128 CTL_PAGE_FLAG_DISK_ONLY))
7132 * We don't use this subpage if the user didn't
7133 * request all subpages. We already checked (above)
7134 * to make sure the user only specified a subpage
7135 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7137 if ((page_index->subpage != 0)
7138 && (subpage == SMS_SUBPAGE_PAGE_0))
7142 * Call the handler, if it exists, to update the
7143 * page to the latest values.
7145 if (page_index->sense_handler != NULL)
7146 page_index->sense_handler(ctsio, page_index,pc);
7148 memcpy(ctsio->kern_data_ptr + data_used,
7149 page_index->page_data +
7150 (page_index->page_len * pc),
7151 page_index->page_len);
7152 data_used += page_index->page_len;
7159 data_used = header_len;
7161 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7162 struct ctl_page_index *page_index;
7164 page_index = &lun->mode_pages.index[i];
7166 /* Look for the right page code */
7167 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7170 /* Look for the right subpage or the subpage wildcard*/
7171 if ((page_index->subpage != subpage)
7172 && (subpage != SMS_SUBPAGE_ALL))
7175 /* Make sure the page is supported for this dev type */
7176 if ((control_dev != 0)
7177 && (page_index->page_flags &
7178 CTL_PAGE_FLAG_DISK_ONLY))
7182 * Call the handler, if it exists, to update the
7183 * page to the latest values.
7185 if (page_index->sense_handler != NULL)
7186 page_index->sense_handler(ctsio, page_index,pc);
7188 memcpy(ctsio->kern_data_ptr + data_used,
7189 page_index->page_data +
7190 (page_index->page_len * pc),
7191 page_index->page_len);
7192 data_used += page_index->page_len;
7198 ctsio->scsi_status = SCSI_STATUS_OK;
7200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7201 ctsio->be_move_done = ctl_config_move_done;
7202 ctl_datamove((union ctl_io *)ctsio);
7204 return (CTL_RETVAL_COMPLETE);
7208 ctl_read_capacity(struct ctl_scsiio *ctsio)
7210 struct scsi_read_capacity *cdb;
7211 struct scsi_read_capacity_data *data;
7212 struct ctl_lun *lun;
7215 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7217 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7219 lba = scsi_4btoul(cdb->addr);
7220 if (((cdb->pmi & SRC_PMI) == 0)
7222 ctl_set_invalid_field(/*ctsio*/ ctsio,
7228 ctl_done((union ctl_io *)ctsio);
7229 return (CTL_RETVAL_COMPLETE);
7232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7234 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7235 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7236 ctsio->residual = 0;
7237 ctsio->kern_data_len = sizeof(*data);
7238 ctsio->kern_total_len = sizeof(*data);
7239 ctsio->kern_data_resid = 0;
7240 ctsio->kern_rel_offset = 0;
7241 ctsio->kern_sg_entries = 0;
7244 * If the maximum LBA is greater than 0xfffffffe, the user must
7245 * issue a SERVICE ACTION IN (16) command, with the read capacity
7246 * serivce action set.
7248 if (lun->be_lun->maxlba > 0xfffffffe)
7249 scsi_ulto4b(0xffffffff, data->addr);
7251 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7254 * XXX KDM this may not be 512 bytes...
7256 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7258 ctsio->scsi_status = SCSI_STATUS_OK;
7260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7261 ctsio->be_move_done = ctl_config_move_done;
7262 ctl_datamove((union ctl_io *)ctsio);
7264 return (CTL_RETVAL_COMPLETE);
7268 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7270 struct scsi_read_capacity_16 *cdb;
7271 struct scsi_read_capacity_data_long *data;
7272 struct ctl_lun *lun;
7276 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7278 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7280 alloc_len = scsi_4btoul(cdb->alloc_len);
7281 lba = scsi_8btou64(cdb->addr);
7283 if ((cdb->reladr & SRC16_PMI)
7285 ctl_set_invalid_field(/*ctsio*/ ctsio,
7291 ctl_done((union ctl_io *)ctsio);
7292 return (CTL_RETVAL_COMPLETE);
7295 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7297 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7298 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7300 if (sizeof(*data) < alloc_len) {
7301 ctsio->residual = alloc_len - sizeof(*data);
7302 ctsio->kern_data_len = sizeof(*data);
7303 ctsio->kern_total_len = sizeof(*data);
7305 ctsio->residual = 0;
7306 ctsio->kern_data_len = alloc_len;
7307 ctsio->kern_total_len = alloc_len;
7309 ctsio->kern_data_resid = 0;
7310 ctsio->kern_rel_offset = 0;
7311 ctsio->kern_sg_entries = 0;
7313 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7314 /* XXX KDM this may not be 512 bytes... */
7315 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7316 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7317 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7318 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7319 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7321 ctsio->scsi_status = SCSI_STATUS_OK;
7323 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7324 ctsio->be_move_done = ctl_config_move_done;
7325 ctl_datamove((union ctl_io *)ctsio);
7327 return (CTL_RETVAL_COMPLETE);
7331 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7333 struct scsi_maintenance_in *cdb;
7335 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7336 int num_target_port_groups, num_target_ports, single;
7337 struct ctl_lun *lun;
7338 struct ctl_softc *softc;
7339 struct ctl_port *port;
7340 struct scsi_target_group_data *rtg_ptr;
7341 struct scsi_target_group_data_extended *rtg_ext_ptr;
7342 struct scsi_target_port_group_descriptor *tpg_desc;
7344 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7346 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7347 softc = control_softc;
7348 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7350 retval = CTL_RETVAL_COMPLETE;
7352 switch (cdb->byte2 & STG_PDF_MASK) {
7353 case STG_PDF_LENGTH:
7356 case STG_PDF_EXTENDED:
7360 ctl_set_invalid_field(/*ctsio*/ ctsio,
7366 ctl_done((union ctl_io *)ctsio);
7370 single = ctl_is_single;
7372 num_target_port_groups = 1;
7374 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7375 num_target_ports = 0;
7376 mtx_lock(&softc->ctl_lock);
7377 STAILQ_FOREACH(port, &softc->port_list, links) {
7378 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7380 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7384 mtx_unlock(&softc->ctl_lock);
7387 total_len = sizeof(struct scsi_target_group_data_extended);
7389 total_len = sizeof(struct scsi_target_group_data);
7390 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7391 num_target_port_groups +
7392 sizeof(struct scsi_target_port_descriptor) *
7393 num_target_ports * num_target_port_groups;
7395 alloc_len = scsi_4btoul(cdb->length);
7397 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7399 ctsio->kern_sg_entries = 0;
7401 if (total_len < alloc_len) {
7402 ctsio->residual = alloc_len - total_len;
7403 ctsio->kern_data_len = total_len;
7404 ctsio->kern_total_len = total_len;
7406 ctsio->residual = 0;
7407 ctsio->kern_data_len = alloc_len;
7408 ctsio->kern_total_len = alloc_len;
7410 ctsio->kern_data_resid = 0;
7411 ctsio->kern_rel_offset = 0;
7414 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7415 ctsio->kern_data_ptr;
7416 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7417 rtg_ext_ptr->format_type = 0x10;
7418 rtg_ext_ptr->implicit_transition_time = 0;
7419 tpg_desc = &rtg_ext_ptr->groups[0];
7421 rtg_ptr = (struct scsi_target_group_data *)
7422 ctsio->kern_data_ptr;
7423 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7424 tpg_desc = &rtg_ptr->groups[0];
7427 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7428 mtx_lock(&softc->ctl_lock);
7429 for (g = 0; g < num_target_port_groups; g++) {
7431 tpg_desc->pref_state = TPG_PRIMARY |
7432 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7434 tpg_desc->pref_state =
7435 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7436 tpg_desc->support = TPG_AO_SUP;
7438 tpg_desc->support |= TPG_AN_SUP;
7439 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7440 tpg_desc->status = TPG_IMPLICIT;
7442 STAILQ_FOREACH(port, &softc->port_list, links) {
7443 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7445 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7448 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7449 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7450 relative_target_port_identifier);
7453 tpg_desc->target_port_count = pc;
7454 tpg_desc = (struct scsi_target_port_group_descriptor *)
7455 &tpg_desc->descriptors[pc];
7457 mtx_unlock(&softc->ctl_lock);
7459 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7460 ctsio->be_move_done = ctl_config_move_done;
7462 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7463 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7464 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7465 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7466 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7468 ctl_datamove((union ctl_io *)ctsio);
7473 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7475 struct ctl_lun *lun;
7476 struct scsi_report_supported_opcodes *cdb;
7477 const struct ctl_cmd_entry *entry, *sentry;
7478 struct scsi_report_supported_opcodes_all *all;
7479 struct scsi_report_supported_opcodes_descr *descr;
7480 struct scsi_report_supported_opcodes_one *one;
7482 int alloc_len, total_len;
7483 int opcode, service_action, i, j, num;
7485 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7487 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7490 retval = CTL_RETVAL_COMPLETE;
7492 opcode = cdb->requested_opcode;
7493 service_action = scsi_2btoul(cdb->requested_service_action);
7494 switch (cdb->options & RSO_OPTIONS_MASK) {
7495 case RSO_OPTIONS_ALL:
7497 for (i = 0; i < 256; i++) {
7498 entry = &ctl_cmd_table[i];
7499 if (entry->flags & CTL_CMD_FLAG_SA5) {
7500 for (j = 0; j < 32; j++) {
7501 sentry = &((const struct ctl_cmd_entry *)
7503 if (ctl_cmd_applicable(
7504 lun->be_lun->lun_type, sentry))
7508 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7513 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7514 num * sizeof(struct scsi_report_supported_opcodes_descr);
7516 case RSO_OPTIONS_OC:
7517 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7518 ctl_set_invalid_field(/*ctsio*/ ctsio,
7524 ctl_done((union ctl_io *)ctsio);
7525 return (CTL_RETVAL_COMPLETE);
7527 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7529 case RSO_OPTIONS_OC_SA:
7530 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7531 service_action >= 32) {
7532 ctl_set_invalid_field(/*ctsio*/ ctsio,
7538 ctl_done((union ctl_io *)ctsio);
7539 return (CTL_RETVAL_COMPLETE);
7541 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7544 ctl_set_invalid_field(/*ctsio*/ ctsio,
7550 ctl_done((union ctl_io *)ctsio);
7551 return (CTL_RETVAL_COMPLETE);
7554 alloc_len = scsi_4btoul(cdb->length);
7556 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7558 ctsio->kern_sg_entries = 0;
7560 if (total_len < alloc_len) {
7561 ctsio->residual = alloc_len - total_len;
7562 ctsio->kern_data_len = total_len;
7563 ctsio->kern_total_len = total_len;
7565 ctsio->residual = 0;
7566 ctsio->kern_data_len = alloc_len;
7567 ctsio->kern_total_len = alloc_len;
7569 ctsio->kern_data_resid = 0;
7570 ctsio->kern_rel_offset = 0;
7572 switch (cdb->options & RSO_OPTIONS_MASK) {
7573 case RSO_OPTIONS_ALL:
7574 all = (struct scsi_report_supported_opcodes_all *)
7575 ctsio->kern_data_ptr;
7577 for (i = 0; i < 256; i++) {
7578 entry = &ctl_cmd_table[i];
7579 if (entry->flags & CTL_CMD_FLAG_SA5) {
7580 for (j = 0; j < 32; j++) {
7581 sentry = &((const struct ctl_cmd_entry *)
7583 if (!ctl_cmd_applicable(
7584 lun->be_lun->lun_type, sentry))
7586 descr = &all->descr[num++];
7588 scsi_ulto2b(j, descr->service_action);
7589 descr->flags = RSO_SERVACTV;
7590 scsi_ulto2b(sentry->length,
7594 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7597 descr = &all->descr[num++];
7599 scsi_ulto2b(0, descr->service_action);
7601 scsi_ulto2b(entry->length, descr->cdb_length);
7605 num * sizeof(struct scsi_report_supported_opcodes_descr),
7608 case RSO_OPTIONS_OC:
7609 one = (struct scsi_report_supported_opcodes_one *)
7610 ctsio->kern_data_ptr;
7611 entry = &ctl_cmd_table[opcode];
7613 case RSO_OPTIONS_OC_SA:
7614 one = (struct scsi_report_supported_opcodes_one *)
7615 ctsio->kern_data_ptr;
7616 entry = &ctl_cmd_table[opcode];
7617 entry = &((const struct ctl_cmd_entry *)
7618 entry->execute)[service_action];
7620 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7622 scsi_ulto2b(entry->length, one->cdb_length);
7623 one->cdb_usage[0] = opcode;
7624 memcpy(&one->cdb_usage[1], entry->usage,
7631 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7632 ctsio->be_move_done = ctl_config_move_done;
7634 ctl_datamove((union ctl_io *)ctsio);
7639 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7641 struct ctl_lun *lun;
7642 struct scsi_report_supported_tmf *cdb;
7643 struct scsi_report_supported_tmf_data *data;
7645 int alloc_len, total_len;
7647 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7649 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7650 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7652 retval = CTL_RETVAL_COMPLETE;
7654 total_len = sizeof(struct scsi_report_supported_tmf_data);
7655 alloc_len = scsi_4btoul(cdb->length);
7657 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7659 ctsio->kern_sg_entries = 0;
7661 if (total_len < alloc_len) {
7662 ctsio->residual = alloc_len - total_len;
7663 ctsio->kern_data_len = total_len;
7664 ctsio->kern_total_len = total_len;
7666 ctsio->residual = 0;
7667 ctsio->kern_data_len = alloc_len;
7668 ctsio->kern_total_len = alloc_len;
7670 ctsio->kern_data_resid = 0;
7671 ctsio->kern_rel_offset = 0;
7673 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7674 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7675 data->byte2 |= RST_ITNRS;
7677 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7678 ctsio->be_move_done = ctl_config_move_done;
7680 ctl_datamove((union ctl_io *)ctsio);
7685 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7687 struct ctl_lun *lun;
7688 struct scsi_report_timestamp *cdb;
7689 struct scsi_report_timestamp_data *data;
7693 int alloc_len, total_len;
7695 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7697 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7700 retval = CTL_RETVAL_COMPLETE;
7702 total_len = sizeof(struct scsi_report_timestamp_data);
7703 alloc_len = scsi_4btoul(cdb->length);
7705 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7707 ctsio->kern_sg_entries = 0;
7709 if (total_len < alloc_len) {
7710 ctsio->residual = alloc_len - total_len;
7711 ctsio->kern_data_len = total_len;
7712 ctsio->kern_total_len = total_len;
7714 ctsio->residual = 0;
7715 ctsio->kern_data_len = alloc_len;
7716 ctsio->kern_total_len = alloc_len;
7718 ctsio->kern_data_resid = 0;
7719 ctsio->kern_rel_offset = 0;
7721 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7722 scsi_ulto2b(sizeof(*data) - 2, data->length);
7723 data->origin = RTS_ORIG_OUTSIDE;
7725 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7726 scsi_ulto4b(timestamp >> 16, data->timestamp);
7727 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7729 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7730 ctsio->be_move_done = ctl_config_move_done;
7732 ctl_datamove((union ctl_io *)ctsio);
7737 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7739 struct scsi_per_res_in *cdb;
7740 int alloc_len, total_len = 0;
7741 /* struct scsi_per_res_in_rsrv in_data; */
7742 struct ctl_lun *lun;
7743 struct ctl_softc *softc;
7745 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7747 softc = control_softc;
7749 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7751 alloc_len = scsi_2btoul(cdb->length);
7753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7756 mtx_lock(&lun->lun_lock);
7757 switch (cdb->action) {
7758 case SPRI_RK: /* read keys */
7759 total_len = sizeof(struct scsi_per_res_in_keys) +
7761 sizeof(struct scsi_per_res_key);
7763 case SPRI_RR: /* read reservation */
7764 if (lun->flags & CTL_LUN_PR_RESERVED)
7765 total_len = sizeof(struct scsi_per_res_in_rsrv);
7767 total_len = sizeof(struct scsi_per_res_in_header);
7769 case SPRI_RC: /* report capabilities */
7770 total_len = sizeof(struct scsi_per_res_cap);
7772 case SPRI_RS: /* read full status */
7773 total_len = sizeof(struct scsi_per_res_in_header) +
7774 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7778 panic("Invalid PR type %x", cdb->action);
7780 mtx_unlock(&lun->lun_lock);
7782 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7784 if (total_len < alloc_len) {
7785 ctsio->residual = alloc_len - total_len;
7786 ctsio->kern_data_len = total_len;
7787 ctsio->kern_total_len = total_len;
7789 ctsio->residual = 0;
7790 ctsio->kern_data_len = alloc_len;
7791 ctsio->kern_total_len = alloc_len;
7794 ctsio->kern_data_resid = 0;
7795 ctsio->kern_rel_offset = 0;
7796 ctsio->kern_sg_entries = 0;
7798 mtx_lock(&lun->lun_lock);
7799 switch (cdb->action) {
7800 case SPRI_RK: { // read keys
7801 struct scsi_per_res_in_keys *res_keys;
7804 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7807 * We had to drop the lock to allocate our buffer, which
7808 * leaves time for someone to come in with another
7809 * persistent reservation. (That is unlikely, though,
7810 * since this should be the only persistent reservation
7811 * command active right now.)
7813 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7814 (lun->pr_key_count *
7815 sizeof(struct scsi_per_res_key)))){
7816 mtx_unlock(&lun->lun_lock);
7817 free(ctsio->kern_data_ptr, M_CTL);
7818 printf("%s: reservation length changed, retrying\n",
7823 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7825 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7826 lun->pr_key_count, res_keys->header.length);
7828 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7829 if (lun->pr_keys[i] == 0)
7833 * We used lun->pr_key_count to calculate the
7834 * size to allocate. If it turns out the number of
7835 * initiators with the registered flag set is
7836 * larger than that (i.e. they haven't been kept in
7837 * sync), we've got a problem.
7839 if (key_count >= lun->pr_key_count) {
7841 csevent_log(CSC_CTL | CSC_SHELF_SW |
7843 csevent_LogType_Fault,
7844 csevent_AlertLevel_Yellow,
7845 csevent_FRU_ShelfController,
7846 csevent_FRU_Firmware,
7847 csevent_FRU_Unknown,
7848 "registered keys %d >= key "
7849 "count %d", key_count,
7855 scsi_u64to8b(lun->pr_keys[i],
7856 res_keys->keys[key_count].key);
7861 case SPRI_RR: { // read reservation
7862 struct scsi_per_res_in_rsrv *res;
7863 int tmp_len, header_only;
7865 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7867 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7869 if (lun->flags & CTL_LUN_PR_RESERVED)
7871 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7872 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7873 res->header.length);
7876 tmp_len = sizeof(struct scsi_per_res_in_header);
7877 scsi_ulto4b(0, res->header.length);
7882 * We had to drop the lock to allocate our buffer, which
7883 * leaves time for someone to come in with another
7884 * persistent reservation. (That is unlikely, though,
7885 * since this should be the only persistent reservation
7886 * command active right now.)
7888 if (tmp_len != total_len) {
7889 mtx_unlock(&lun->lun_lock);
7890 free(ctsio->kern_data_ptr, M_CTL);
7891 printf("%s: reservation status changed, retrying\n",
7897 * No reservation held, so we're done.
7899 if (header_only != 0)
7903 * If the registration is an All Registrants type, the key
7904 * is 0, since it doesn't really matter.
7906 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7907 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx],
7908 res->data.reservation);
7910 res->data.scopetype = lun->res_type;
7913 case SPRI_RC: //report capabilities
7915 struct scsi_per_res_cap *res_cap;
7918 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7919 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7920 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7921 type_mask = SPRI_TM_WR_EX_AR |
7927 scsi_ulto2b(type_mask, res_cap->type_mask);
7930 case SPRI_RS: { // read full status
7931 struct scsi_per_res_in_full *res_status;
7932 struct scsi_per_res_in_full_desc *res_desc;
7933 struct ctl_port *port;
7936 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7939 * We had to drop the lock to allocate our buffer, which
7940 * leaves time for someone to come in with another
7941 * persistent reservation. (That is unlikely, though,
7942 * since this should be the only persistent reservation
7943 * command active right now.)
7945 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7946 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7947 lun->pr_key_count)){
7948 mtx_unlock(&lun->lun_lock);
7949 free(ctsio->kern_data_ptr, M_CTL);
7950 printf("%s: reservation length changed, retrying\n",
7955 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7957 res_desc = &res_status->desc[0];
7958 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7959 if (lun->pr_keys[i] == 0)
7962 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key);
7963 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7964 (lun->pr_res_idx == i ||
7965 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7966 res_desc->flags = SPRI_FULL_R_HOLDER;
7967 res_desc->scopetype = lun->res_type;
7969 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7970 res_desc->rel_trgt_port_id);
7972 port = softc->ctl_ports[
7973 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7975 len = ctl_create_iid(port,
7976 i % CTL_MAX_INIT_PER_PORT,
7977 res_desc->transport_id);
7978 scsi_ulto4b(len, res_desc->additional_length);
7979 res_desc = (struct scsi_per_res_in_full_desc *)
7980 &res_desc->transport_id[len];
7982 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7983 res_status->header.length);
7988 * This is a bug, because we just checked for this above,
7989 * and should have returned an error.
7991 panic("Invalid PR type %x", cdb->action);
7992 break; /* NOTREACHED */
7994 mtx_unlock(&lun->lun_lock);
7996 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7997 ctsio->be_move_done = ctl_config_move_done;
7999 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
8000 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
8001 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
8002 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
8003 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
8005 ctl_datamove((union ctl_io *)ctsio);
8007 return (CTL_RETVAL_COMPLETE);
8011 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8015 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8016 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8017 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8018 struct scsi_per_res_out_parms* param)
8020 union ctl_ha_msg persis_io;
8026 mtx_lock(&lun->lun_lock);
8027 if (sa_res_key == 0) {
8028 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8029 /* validate scope and type */
8030 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8032 mtx_unlock(&lun->lun_lock);
8033 ctl_set_invalid_field(/*ctsio*/ ctsio,
8039 ctl_done((union ctl_io *)ctsio);
8043 if (type>8 || type==2 || type==4 || type==0) {
8044 mtx_unlock(&lun->lun_lock);
8045 ctl_set_invalid_field(/*ctsio*/ ctsio,
8051 ctl_done((union ctl_io *)ctsio);
8056 * Unregister everybody else and build UA for
8059 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8060 if (i == residx || lun->pr_keys[i] == 0)
8064 && i <CTL_MAX_INITIATORS)
8065 lun->pending_ua[i] |=
8067 else if (persis_offset
8068 && i >= persis_offset)
8069 lun->pending_ua[i-persis_offset] |=
8071 lun->pr_keys[i] = 0;
8073 lun->pr_key_count = 1;
8074 lun->res_type = type;
8075 if (lun->res_type != SPR_TYPE_WR_EX_AR
8076 && lun->res_type != SPR_TYPE_EX_AC_AR)
8077 lun->pr_res_idx = residx;
8079 /* send msg to other side */
8080 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8081 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8082 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8083 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8084 persis_io.pr.pr_info.res_type = type;
8085 memcpy(persis_io.pr.pr_info.sa_res_key,
8086 param->serv_act_res_key,
8087 sizeof(param->serv_act_res_key));
8088 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8089 &persis_io, sizeof(persis_io), 0)) >
8090 CTL_HA_STATUS_SUCCESS) {
8091 printf("CTL:Persis Out error returned "
8092 "from ctl_ha_msg_send %d\n",
8096 /* not all registrants */
8097 mtx_unlock(&lun->lun_lock);
8098 free(ctsio->kern_data_ptr, M_CTL);
8099 ctl_set_invalid_field(ctsio,
8105 ctl_done((union ctl_io *)ctsio);
8108 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8109 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8112 if (res_key == sa_res_key) {
8115 * The spec implies this is not good but doesn't
8116 * say what to do. There are two choices either
8117 * generate a res conflict or check condition
8118 * with illegal field in parameter data. Since
8119 * that is what is done when the sa_res_key is
8120 * zero I'll take that approach since this has
8121 * to do with the sa_res_key.
8123 mtx_unlock(&lun->lun_lock);
8124 free(ctsio->kern_data_ptr, M_CTL);
8125 ctl_set_invalid_field(ctsio,
8131 ctl_done((union ctl_io *)ctsio);
8135 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8136 if (lun->pr_keys[i] != sa_res_key)
8140 lun->pr_keys[i] = 0;
8141 lun->pr_key_count--;
8143 if (!persis_offset && i < CTL_MAX_INITIATORS)
8144 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8145 else if (persis_offset && i >= persis_offset)
8146 lun->pending_ua[i-persis_offset] |=
8150 mtx_unlock(&lun->lun_lock);
8151 free(ctsio->kern_data_ptr, M_CTL);
8152 ctl_set_reservation_conflict(ctsio);
8153 ctl_done((union ctl_io *)ctsio);
8154 return (CTL_RETVAL_COMPLETE);
8156 /* send msg to other side */
8157 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8158 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8159 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8160 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8161 persis_io.pr.pr_info.res_type = type;
8162 memcpy(persis_io.pr.pr_info.sa_res_key,
8163 param->serv_act_res_key,
8164 sizeof(param->serv_act_res_key));
8165 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8166 &persis_io, sizeof(persis_io), 0)) >
8167 CTL_HA_STATUS_SUCCESS) {
8168 printf("CTL:Persis Out error returned from "
8169 "ctl_ha_msg_send %d\n", isc_retval);
8172 /* Reserved but not all registrants */
8173 /* sa_res_key is res holder */
8174 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) {
8175 /* validate scope and type */
8176 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8178 mtx_unlock(&lun->lun_lock);
8179 ctl_set_invalid_field(/*ctsio*/ ctsio,
8185 ctl_done((union ctl_io *)ctsio);
8189 if (type>8 || type==2 || type==4 || type==0) {
8190 mtx_unlock(&lun->lun_lock);
8191 ctl_set_invalid_field(/*ctsio*/ ctsio,
8197 ctl_done((union ctl_io *)ctsio);
8203 * if sa_res_key != res_key remove all
8204 * registrants w/sa_res_key and generate UA
8205 * for these registrants(Registrations
8206 * Preempted) if it wasn't an exclusive
8207 * reservation generate UA(Reservations
8208 * Preempted) for all other registered nexuses
8209 * if the type has changed. Establish the new
8210 * reservation and holder. If res_key and
8211 * sa_res_key are the same do the above
8212 * except don't unregister the res holder.
8215 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8216 if (i == residx || lun->pr_keys[i] == 0)
8219 if (sa_res_key == lun->pr_keys[i]) {
8220 lun->pr_keys[i] = 0;
8221 lun->pr_key_count--;
8224 && i < CTL_MAX_INITIATORS)
8225 lun->pending_ua[i] |=
8227 else if (persis_offset
8228 && i >= persis_offset)
8229 lun->pending_ua[i-persis_offset] |=
8231 } else if (type != lun->res_type
8232 && (lun->res_type == SPR_TYPE_WR_EX_RO
8233 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8235 && i < CTL_MAX_INITIATORS)
8236 lun->pending_ua[i] |=
8238 else if (persis_offset
8239 && i >= persis_offset)
8245 lun->res_type = type;
8246 if (lun->res_type != SPR_TYPE_WR_EX_AR
8247 && lun->res_type != SPR_TYPE_EX_AC_AR)
8248 lun->pr_res_idx = residx;
8250 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8252 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8253 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8254 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8255 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8256 persis_io.pr.pr_info.res_type = type;
8257 memcpy(persis_io.pr.pr_info.sa_res_key,
8258 param->serv_act_res_key,
8259 sizeof(param->serv_act_res_key));
8260 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8261 &persis_io, sizeof(persis_io), 0)) >
8262 CTL_HA_STATUS_SUCCESS) {
8263 printf("CTL:Persis Out error returned "
8264 "from ctl_ha_msg_send %d\n",
8269 * sa_res_key is not the res holder just
8270 * remove registrants
8274 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8275 if (sa_res_key != lun->pr_keys[i])
8279 lun->pr_keys[i] = 0;
8280 lun->pr_key_count--;
8283 && i < CTL_MAX_INITIATORS)
8284 lun->pending_ua[i] |=
8286 else if (persis_offset
8287 && i >= persis_offset)
8288 lun->pending_ua[i-persis_offset] |=
8293 mtx_unlock(&lun->lun_lock);
8294 free(ctsio->kern_data_ptr, M_CTL);
8295 ctl_set_reservation_conflict(ctsio);
8296 ctl_done((union ctl_io *)ctsio);
8299 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8300 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8301 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8302 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8303 persis_io.pr.pr_info.res_type = type;
8304 memcpy(persis_io.pr.pr_info.sa_res_key,
8305 param->serv_act_res_key,
8306 sizeof(param->serv_act_res_key));
8307 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8308 &persis_io, sizeof(persis_io), 0)) >
8309 CTL_HA_STATUS_SUCCESS) {
8310 printf("CTL:Persis Out error returned "
8311 "from ctl_ha_msg_send %d\n",
8317 lun->PRGeneration++;
8318 mtx_unlock(&lun->lun_lock);
8324 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8326 uint64_t sa_res_key;
8329 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8331 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8332 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8333 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) {
8334 if (sa_res_key == 0) {
8336 * Unregister everybody else and build UA for
8339 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8340 if (i == msg->pr.pr_info.residx ||
8341 lun->pr_keys[i] == 0)
8345 && i < CTL_MAX_INITIATORS)
8346 lun->pending_ua[i] |=
8348 else if (persis_offset && i >= persis_offset)
8349 lun->pending_ua[i - persis_offset] |=
8351 lun->pr_keys[i] = 0;
8354 lun->pr_key_count = 1;
8355 lun->res_type = msg->pr.pr_info.res_type;
8356 if (lun->res_type != SPR_TYPE_WR_EX_AR
8357 && lun->res_type != SPR_TYPE_EX_AC_AR)
8358 lun->pr_res_idx = msg->pr.pr_info.residx;
8360 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8361 if (sa_res_key == lun->pr_keys[i])
8364 lun->pr_keys[i] = 0;
8365 lun->pr_key_count--;
8368 && i < persis_offset)
8369 lun->pending_ua[i] |=
8371 else if (persis_offset
8372 && i >= persis_offset)
8373 lun->pending_ua[i - persis_offset] |=
8378 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8379 if (i == msg->pr.pr_info.residx ||
8380 lun->pr_keys[i] == 0)
8383 if (sa_res_key == lun->pr_keys[i]) {
8384 lun->pr_keys[i] = 0;
8385 lun->pr_key_count--;
8387 && i < CTL_MAX_INITIATORS)
8388 lun->pending_ua[i] |=
8390 else if (persis_offset
8391 && i >= persis_offset)
8392 lun->pending_ua[i - persis_offset] |=
8394 } else if (msg->pr.pr_info.res_type != lun->res_type
8395 && (lun->res_type == SPR_TYPE_WR_EX_RO
8396 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8398 && i < persis_offset)
8399 lun->pending_ua[i] |=
8401 else if (persis_offset
8402 && i >= persis_offset)
8403 lun->pending_ua[i - persis_offset] |=
8407 lun->res_type = msg->pr.pr_info.res_type;
8408 if (lun->res_type != SPR_TYPE_WR_EX_AR
8409 && lun->res_type != SPR_TYPE_EX_AC_AR)
8410 lun->pr_res_idx = msg->pr.pr_info.residx;
8412 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8414 lun->PRGeneration++;
8420 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8424 u_int32_t param_len;
8425 struct scsi_per_res_out *cdb;
8426 struct ctl_lun *lun;
8427 struct scsi_per_res_out_parms* param;
8428 struct ctl_softc *softc;
8430 uint64_t res_key, sa_res_key;
8432 union ctl_ha_msg persis_io;
8435 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8437 retval = CTL_RETVAL_COMPLETE;
8439 softc = control_softc;
8441 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8442 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8445 * We only support whole-LUN scope. The scope & type are ignored for
8446 * register, register and ignore existing key and clear.
8447 * We sometimes ignore scope and type on preempts too!!
8448 * Verify reservation type here as well.
8450 type = cdb->scope_type & SPR_TYPE_MASK;
8451 if ((cdb->action == SPRO_RESERVE)
8452 || (cdb->action == SPRO_RELEASE)) {
8453 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8454 ctl_set_invalid_field(/*ctsio*/ ctsio,
8460 ctl_done((union ctl_io *)ctsio);
8461 return (CTL_RETVAL_COMPLETE);
8464 if (type>8 || type==2 || type==4 || type==0) {
8465 ctl_set_invalid_field(/*ctsio*/ ctsio,
8471 ctl_done((union ctl_io *)ctsio);
8472 return (CTL_RETVAL_COMPLETE);
8476 param_len = scsi_4btoul(cdb->length);
8478 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8479 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8480 ctsio->kern_data_len = param_len;
8481 ctsio->kern_total_len = param_len;
8482 ctsio->kern_data_resid = 0;
8483 ctsio->kern_rel_offset = 0;
8484 ctsio->kern_sg_entries = 0;
8485 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8486 ctsio->be_move_done = ctl_config_move_done;
8487 ctl_datamove((union ctl_io *)ctsio);
8489 return (CTL_RETVAL_COMPLETE);
8492 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8494 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8495 res_key = scsi_8btou64(param->res_key.key);
8496 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8499 * Validate the reservation key here except for SPRO_REG_IGNO
8500 * This must be done for all other service actions
8502 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8503 mtx_lock(&lun->lun_lock);
8504 if (lun->pr_keys[residx] != 0) {
8505 if (res_key != lun->pr_keys[residx]) {
8507 * The current key passed in doesn't match
8508 * the one the initiator previously
8511 mtx_unlock(&lun->lun_lock);
8512 free(ctsio->kern_data_ptr, M_CTL);
8513 ctl_set_reservation_conflict(ctsio);
8514 ctl_done((union ctl_io *)ctsio);
8515 return (CTL_RETVAL_COMPLETE);
8517 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8519 * We are not registered
8521 mtx_unlock(&lun->lun_lock);
8522 free(ctsio->kern_data_ptr, M_CTL);
8523 ctl_set_reservation_conflict(ctsio);
8524 ctl_done((union ctl_io *)ctsio);
8525 return (CTL_RETVAL_COMPLETE);
8526 } else if (res_key != 0) {
8528 * We are not registered and trying to register but
8529 * the register key isn't zero.
8531 mtx_unlock(&lun->lun_lock);
8532 free(ctsio->kern_data_ptr, M_CTL);
8533 ctl_set_reservation_conflict(ctsio);
8534 ctl_done((union ctl_io *)ctsio);
8535 return (CTL_RETVAL_COMPLETE);
8537 mtx_unlock(&lun->lun_lock);
8540 switch (cdb->action & SPRO_ACTION_MASK) {
8542 case SPRO_REG_IGNO: {
8545 printf("Registration received\n");
8549 * We don't support any of these options, as we report in
8550 * the read capabilities request (see
8551 * ctl_persistent_reserve_in(), above).
8553 if ((param->flags & SPR_SPEC_I_PT)
8554 || (param->flags & SPR_ALL_TG_PT)
8555 || (param->flags & SPR_APTPL)) {
8558 if (param->flags & SPR_APTPL)
8560 else if (param->flags & SPR_ALL_TG_PT)
8562 else /* SPR_SPEC_I_PT */
8565 free(ctsio->kern_data_ptr, M_CTL);
8566 ctl_set_invalid_field(ctsio,
8572 ctl_done((union ctl_io *)ctsio);
8573 return (CTL_RETVAL_COMPLETE);
8576 mtx_lock(&lun->lun_lock);
8579 * The initiator wants to clear the
8582 if (sa_res_key == 0) {
8584 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8585 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8586 && lun->pr_keys[residx] == 0)) {
8587 mtx_unlock(&lun->lun_lock);
8591 lun->pr_keys[residx] = 0;
8592 lun->pr_key_count--;
8594 if (residx == lun->pr_res_idx) {
8595 lun->flags &= ~CTL_LUN_PR_RESERVED;
8596 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8598 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8599 || lun->res_type == SPR_TYPE_EX_AC_RO)
8600 && lun->pr_key_count) {
8602 * If the reservation is a registrants
8603 * only type we need to generate a UA
8604 * for other registered inits. The
8605 * sense code should be RESERVATIONS
8609 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8611 i + persis_offset] == 0)
8613 lun->pending_ua[i] |=
8618 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8619 if (lun->pr_key_count==0) {
8620 lun->flags &= ~CTL_LUN_PR_RESERVED;
8622 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8625 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8626 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8627 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8628 persis_io.pr.pr_info.residx = residx;
8629 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8630 &persis_io, sizeof(persis_io), 0 )) >
8631 CTL_HA_STATUS_SUCCESS) {
8632 printf("CTL:Persis Out error returned from "
8633 "ctl_ha_msg_send %d\n", isc_retval);
8635 } else /* sa_res_key != 0 */ {
8638 * If we aren't registered currently then increment
8639 * the key count and set the registered flag.
8641 if (lun->pr_keys[residx] == 0)
8642 lun->pr_key_count++;
8643 lun->pr_keys[residx] = sa_res_key;
8645 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8646 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8647 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8648 persis_io.pr.pr_info.residx = residx;
8649 memcpy(persis_io.pr.pr_info.sa_res_key,
8650 param->serv_act_res_key,
8651 sizeof(param->serv_act_res_key));
8652 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8653 &persis_io, sizeof(persis_io), 0)) >
8654 CTL_HA_STATUS_SUCCESS) {
8655 printf("CTL:Persis Out error returned from "
8656 "ctl_ha_msg_send %d\n", isc_retval);
8659 lun->PRGeneration++;
8660 mtx_unlock(&lun->lun_lock);
8666 printf("Reserve executed type %d\n", type);
8668 mtx_lock(&lun->lun_lock);
8669 if (lun->flags & CTL_LUN_PR_RESERVED) {
8671 * if this isn't the reservation holder and it's
8672 * not a "all registrants" type or if the type is
8673 * different then we have a conflict
8675 if ((lun->pr_res_idx != residx
8676 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8677 || lun->res_type != type) {
8678 mtx_unlock(&lun->lun_lock);
8679 free(ctsio->kern_data_ptr, M_CTL);
8680 ctl_set_reservation_conflict(ctsio);
8681 ctl_done((union ctl_io *)ctsio);
8682 return (CTL_RETVAL_COMPLETE);
8684 mtx_unlock(&lun->lun_lock);
8685 } else /* create a reservation */ {
8687 * If it's not an "all registrants" type record
8688 * reservation holder
8690 if (type != SPR_TYPE_WR_EX_AR
8691 && type != SPR_TYPE_EX_AC_AR)
8692 lun->pr_res_idx = residx; /* Res holder */
8694 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8696 lun->flags |= CTL_LUN_PR_RESERVED;
8697 lun->res_type = type;
8699 mtx_unlock(&lun->lun_lock);
8701 /* send msg to other side */
8702 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8703 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8704 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8705 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8706 persis_io.pr.pr_info.res_type = type;
8707 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8708 &persis_io, sizeof(persis_io), 0)) >
8709 CTL_HA_STATUS_SUCCESS) {
8710 printf("CTL:Persis Out error returned from "
8711 "ctl_ha_msg_send %d\n", isc_retval);
8717 mtx_lock(&lun->lun_lock);
8718 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8719 /* No reservation exists return good status */
8720 mtx_unlock(&lun->lun_lock);
8724 * Is this nexus a reservation holder?
8726 if (lun->pr_res_idx != residx
8727 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8729 * not a res holder return good status but
8732 mtx_unlock(&lun->lun_lock);
8736 if (lun->res_type != type) {
8737 mtx_unlock(&lun->lun_lock);
8738 free(ctsio->kern_data_ptr, M_CTL);
8739 ctl_set_illegal_pr_release(ctsio);
8740 ctl_done((union ctl_io *)ctsio);
8741 return (CTL_RETVAL_COMPLETE);
8744 /* okay to release */
8745 lun->flags &= ~CTL_LUN_PR_RESERVED;
8746 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8750 * if this isn't an exclusive access
8751 * res generate UA for all other
8754 if (type != SPR_TYPE_EX_AC
8755 && type != SPR_TYPE_WR_EX) {
8756 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8758 lun->pr_keys[i + persis_offset] == 0)
8760 lun->pending_ua[i] |= CTL_UA_RES_RELEASE;
8763 mtx_unlock(&lun->lun_lock);
8764 /* Send msg to other side */
8765 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8766 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8767 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8768 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8769 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8770 printf("CTL:Persis Out error returned from "
8771 "ctl_ha_msg_send %d\n", isc_retval);
8776 /* send msg to other side */
8778 mtx_lock(&lun->lun_lock);
8779 lun->flags &= ~CTL_LUN_PR_RESERVED;
8781 lun->pr_key_count = 0;
8782 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8784 lun->pr_keys[residx] = 0;
8786 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8787 if (lun->pr_keys[i] != 0) {
8788 if (!persis_offset && i < CTL_MAX_INITIATORS)
8789 lun->pending_ua[i] |=
8791 else if (persis_offset && i >= persis_offset)
8792 lun->pending_ua[i-persis_offset] |=
8795 lun->pr_keys[i] = 0;
8797 lun->PRGeneration++;
8798 mtx_unlock(&lun->lun_lock);
8799 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8800 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8801 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8802 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8803 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8804 printf("CTL:Persis Out error returned from "
8805 "ctl_ha_msg_send %d\n", isc_retval);
8809 case SPRO_PREEMPT: {
8812 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8813 residx, ctsio, cdb, param);
8815 return (CTL_RETVAL_COMPLETE);
8819 panic("Invalid PR type %x", cdb->action);
8823 free(ctsio->kern_data_ptr, M_CTL);
8824 ctl_set_success(ctsio);
8825 ctl_done((union ctl_io *)ctsio);
8831 * This routine is for handling a message from the other SC pertaining to
8832 * persistent reserve out. All the error checking will have been done
8833 * so only perorming the action need be done here to keep the two
8837 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8839 struct ctl_lun *lun;
8840 struct ctl_softc *softc;
8844 softc = control_softc;
8846 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8847 lun = softc->ctl_luns[targ_lun];
8848 mtx_lock(&lun->lun_lock);
8849 switch(msg->pr.pr_info.action) {
8850 case CTL_PR_REG_KEY:
8851 if (lun->pr_keys[msg->pr.pr_info.residx] == 0)
8852 lun->pr_key_count++;
8853 lun->pr_keys[msg->pr.pr_info.residx] =
8854 scsi_8btou64(msg->pr.pr_info.sa_res_key);
8855 lun->PRGeneration++;
8858 case CTL_PR_UNREG_KEY:
8859 lun->pr_keys[msg->pr.pr_info.residx] = 0;
8860 lun->pr_key_count--;
8862 /* XXX Need to see if the reservation has been released */
8863 /* if so do we need to generate UA? */
8864 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8865 lun->flags &= ~CTL_LUN_PR_RESERVED;
8866 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8868 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8869 || lun->res_type == SPR_TYPE_EX_AC_RO)
8870 && lun->pr_key_count) {
8872 * If the reservation is a registrants
8873 * only type we need to generate a UA
8874 * for other registered inits. The
8875 * sense code should be RESERVATIONS
8879 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8881 persis_offset] == 0)
8884 lun->pending_ua[i] |=
8889 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8890 if (lun->pr_key_count==0) {
8891 lun->flags &= ~CTL_LUN_PR_RESERVED;
8893 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8896 lun->PRGeneration++;
8899 case CTL_PR_RESERVE:
8900 lun->flags |= CTL_LUN_PR_RESERVED;
8901 lun->res_type = msg->pr.pr_info.res_type;
8902 lun->pr_res_idx = msg->pr.pr_info.residx;
8906 case CTL_PR_RELEASE:
8908 * if this isn't an exclusive access res generate UA for all
8909 * other registrants.
8911 if (lun->res_type != SPR_TYPE_EX_AC
8912 && lun->res_type != SPR_TYPE_WR_EX) {
8913 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8914 if (lun->pr_keys[i+persis_offset] != 0)
8915 lun->pending_ua[i] |=
8919 lun->flags &= ~CTL_LUN_PR_RESERVED;
8920 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8924 case CTL_PR_PREEMPT:
8925 ctl_pro_preempt_other(lun, msg);
8928 lun->flags &= ~CTL_LUN_PR_RESERVED;
8930 lun->pr_key_count = 0;
8931 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8933 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8934 if (lun->pr_keys[i] == 0)
8937 && i < CTL_MAX_INITIATORS)
8938 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8939 else if (persis_offset
8940 && i >= persis_offset)
8941 lun->pending_ua[i-persis_offset] |=
8943 lun->pr_keys[i] = 0;
8945 lun->PRGeneration++;
8949 mtx_unlock(&lun->lun_lock);
8953 ctl_read_write(struct ctl_scsiio *ctsio)
8955 struct ctl_lun *lun;
8956 struct ctl_lba_len_flags *lbalen;
8958 uint32_t num_blocks;
8962 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8964 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8967 retval = CTL_RETVAL_COMPLETE;
8969 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8970 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8971 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8975 * XXX KDM need a lock here.
8977 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8978 if ((lun->res_type == SPR_TYPE_EX_AC
8979 && residx != lun->pr_res_idx)
8980 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8981 || lun->res_type == SPR_TYPE_EX_AC_AR)
8982 && lun->pr_keys[residx] == 0)) {
8983 ctl_set_reservation_conflict(ctsio);
8984 ctl_done((union ctl_io *)ctsio);
8985 return (CTL_RETVAL_COMPLETE);
8989 switch (ctsio->cdb[0]) {
8992 struct scsi_rw_6 *cdb;
8994 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8996 lba = scsi_3btoul(cdb->addr);
8997 /* only 5 bits are valid in the most significant address byte */
8999 num_blocks = cdb->length;
9001 * This is correct according to SBC-2.
9003 if (num_blocks == 0)
9009 struct scsi_rw_10 *cdb;
9011 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9012 if (cdb->byte2 & SRW10_FUA)
9013 flags |= CTL_LLF_FUA;
9014 if (cdb->byte2 & SRW10_DPO)
9015 flags |= CTL_LLF_DPO;
9016 lba = scsi_4btoul(cdb->addr);
9017 num_blocks = scsi_2btoul(cdb->length);
9020 case WRITE_VERIFY_10: {
9021 struct scsi_write_verify_10 *cdb;
9023 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9024 flags |= CTL_LLF_FUA;
9025 if (cdb->byte2 & SWV_DPO)
9026 flags |= CTL_LLF_DPO;
9027 lba = scsi_4btoul(cdb->addr);
9028 num_blocks = scsi_2btoul(cdb->length);
9033 struct scsi_rw_12 *cdb;
9035 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9036 if (cdb->byte2 & SRW12_FUA)
9037 flags |= CTL_LLF_FUA;
9038 if (cdb->byte2 & SRW12_DPO)
9039 flags |= CTL_LLF_DPO;
9040 lba = scsi_4btoul(cdb->addr);
9041 num_blocks = scsi_4btoul(cdb->length);
9044 case WRITE_VERIFY_12: {
9045 struct scsi_write_verify_12 *cdb;
9047 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9048 flags |= CTL_LLF_FUA;
9049 if (cdb->byte2 & SWV_DPO)
9050 flags |= CTL_LLF_DPO;
9051 lba = scsi_4btoul(cdb->addr);
9052 num_blocks = scsi_4btoul(cdb->length);
9057 struct scsi_rw_16 *cdb;
9059 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9060 if (cdb->byte2 & SRW12_FUA)
9061 flags |= CTL_LLF_FUA;
9062 if (cdb->byte2 & SRW12_DPO)
9063 flags |= CTL_LLF_DPO;
9064 lba = scsi_8btou64(cdb->addr);
9065 num_blocks = scsi_4btoul(cdb->length);
9068 case WRITE_ATOMIC_16: {
9069 struct scsi_rw_16 *cdb;
9071 if (lun->be_lun->atomicblock == 0) {
9072 ctl_set_invalid_opcode(ctsio);
9073 ctl_done((union ctl_io *)ctsio);
9074 return (CTL_RETVAL_COMPLETE);
9077 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9078 if (cdb->byte2 & SRW12_FUA)
9079 flags |= CTL_LLF_FUA;
9080 if (cdb->byte2 & SRW12_DPO)
9081 flags |= CTL_LLF_DPO;
9082 lba = scsi_8btou64(cdb->addr);
9083 num_blocks = scsi_4btoul(cdb->length);
9084 if (num_blocks > lun->be_lun->atomicblock) {
9085 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
9086 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
9088 ctl_done((union ctl_io *)ctsio);
9089 return (CTL_RETVAL_COMPLETE);
9093 case WRITE_VERIFY_16: {
9094 struct scsi_write_verify_16 *cdb;
9096 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9097 flags |= CTL_LLF_FUA;
9098 if (cdb->byte2 & SWV_DPO)
9099 flags |= CTL_LLF_DPO;
9100 lba = scsi_8btou64(cdb->addr);
9101 num_blocks = scsi_4btoul(cdb->length);
9106 * We got a command we don't support. This shouldn't
9107 * happen, commands should be filtered out above us.
9109 ctl_set_invalid_opcode(ctsio);
9110 ctl_done((union ctl_io *)ctsio);
9112 return (CTL_RETVAL_COMPLETE);
9113 break; /* NOTREACHED */
9117 * The first check is to make sure we're in bounds, the second
9118 * check is to catch wrap-around problems. If the lba + num blocks
9119 * is less than the lba, then we've wrapped around and the block
9120 * range is invalid anyway.
9122 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9123 || ((lba + num_blocks) < lba)) {
9124 ctl_set_lba_out_of_range(ctsio);
9125 ctl_done((union ctl_io *)ctsio);
9126 return (CTL_RETVAL_COMPLETE);
9130 * According to SBC-3, a transfer length of 0 is not an error.
9131 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9132 * translates to 256 blocks for those commands.
9134 if (num_blocks == 0) {
9135 ctl_set_success(ctsio);
9136 ctl_done((union ctl_io *)ctsio);
9137 return (CTL_RETVAL_COMPLETE);
9140 /* Set FUA and/or DPO if caches are disabled. */
9142 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9144 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9146 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9148 flags |= CTL_LLF_FUA;
9151 lbalen = (struct ctl_lba_len_flags *)
9152 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9154 lbalen->len = num_blocks;
9155 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9157 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9158 ctsio->kern_rel_offset = 0;
9160 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9162 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9168 ctl_cnw_cont(union ctl_io *io)
9170 struct ctl_scsiio *ctsio;
9171 struct ctl_lun *lun;
9172 struct ctl_lba_len_flags *lbalen;
9175 ctsio = &io->scsiio;
9176 ctsio->io_hdr.status = CTL_STATUS_NONE;
9177 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9178 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9179 lbalen = (struct ctl_lba_len_flags *)
9180 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9181 lbalen->flags &= ~CTL_LLF_COMPARE;
9182 lbalen->flags |= CTL_LLF_WRITE;
9184 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9185 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9190 ctl_cnw(struct ctl_scsiio *ctsio)
9192 struct ctl_lun *lun;
9193 struct ctl_lba_len_flags *lbalen;
9195 uint32_t num_blocks;
9198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9200 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9203 retval = CTL_RETVAL_COMPLETE;
9205 switch (ctsio->cdb[0]) {
9206 case COMPARE_AND_WRITE: {
9207 struct scsi_compare_and_write *cdb;
9209 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9210 if (cdb->byte2 & SRW10_FUA)
9211 flags |= CTL_LLF_FUA;
9212 if (cdb->byte2 & SRW10_DPO)
9213 flags |= CTL_LLF_DPO;
9214 lba = scsi_8btou64(cdb->addr);
9215 num_blocks = cdb->length;
9220 * We got a command we don't support. This shouldn't
9221 * happen, commands should be filtered out above us.
9223 ctl_set_invalid_opcode(ctsio);
9224 ctl_done((union ctl_io *)ctsio);
9226 return (CTL_RETVAL_COMPLETE);
9227 break; /* NOTREACHED */
9231 * The first check is to make sure we're in bounds, the second
9232 * check is to catch wrap-around problems. If the lba + num blocks
9233 * is less than the lba, then we've wrapped around and the block
9234 * range is invalid anyway.
9236 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9237 || ((lba + num_blocks) < lba)) {
9238 ctl_set_lba_out_of_range(ctsio);
9239 ctl_done((union ctl_io *)ctsio);
9240 return (CTL_RETVAL_COMPLETE);
9244 * According to SBC-3, a transfer length of 0 is not an error.
9246 if (num_blocks == 0) {
9247 ctl_set_success(ctsio);
9248 ctl_done((union ctl_io *)ctsio);
9249 return (CTL_RETVAL_COMPLETE);
9252 /* Set FUA if write cache is disabled. */
9253 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9255 flags |= CTL_LLF_FUA;
9257 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9258 ctsio->kern_rel_offset = 0;
9261 * Set the IO_CONT flag, so that if this I/O gets passed to
9262 * ctl_data_submit_done(), it'll get passed back to
9263 * ctl_ctl_cnw_cont() for further processing.
9265 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9266 ctsio->io_cont = ctl_cnw_cont;
9268 lbalen = (struct ctl_lba_len_flags *)
9269 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9271 lbalen->len = num_blocks;
9272 lbalen->flags = CTL_LLF_COMPARE | flags;
9274 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9275 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9280 ctl_verify(struct ctl_scsiio *ctsio)
9282 struct ctl_lun *lun;
9283 struct ctl_lba_len_flags *lbalen;
9285 uint32_t num_blocks;
9289 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9291 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9294 flags = CTL_LLF_FUA;
9295 retval = CTL_RETVAL_COMPLETE;
9297 switch (ctsio->cdb[0]) {
9299 struct scsi_verify_10 *cdb;
9301 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9302 if (cdb->byte2 & SVFY_BYTCHK)
9304 if (cdb->byte2 & SVFY_DPO)
9305 flags |= CTL_LLF_DPO;
9306 lba = scsi_4btoul(cdb->addr);
9307 num_blocks = scsi_2btoul(cdb->length);
9311 struct scsi_verify_12 *cdb;
9313 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9314 if (cdb->byte2 & SVFY_BYTCHK)
9316 if (cdb->byte2 & SVFY_DPO)
9317 flags |= CTL_LLF_DPO;
9318 lba = scsi_4btoul(cdb->addr);
9319 num_blocks = scsi_4btoul(cdb->length);
9323 struct scsi_rw_16 *cdb;
9325 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9326 if (cdb->byte2 & SVFY_BYTCHK)
9328 if (cdb->byte2 & SVFY_DPO)
9329 flags |= CTL_LLF_DPO;
9330 lba = scsi_8btou64(cdb->addr);
9331 num_blocks = scsi_4btoul(cdb->length);
9336 * We got a command we don't support. This shouldn't
9337 * happen, commands should be filtered out above us.
9339 ctl_set_invalid_opcode(ctsio);
9340 ctl_done((union ctl_io *)ctsio);
9341 return (CTL_RETVAL_COMPLETE);
9345 * The first check is to make sure we're in bounds, the second
9346 * check is to catch wrap-around problems. If the lba + num blocks
9347 * is less than the lba, then we've wrapped around and the block
9348 * range is invalid anyway.
9350 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9351 || ((lba + num_blocks) < lba)) {
9352 ctl_set_lba_out_of_range(ctsio);
9353 ctl_done((union ctl_io *)ctsio);
9354 return (CTL_RETVAL_COMPLETE);
9358 * According to SBC-3, a transfer length of 0 is not an error.
9360 if (num_blocks == 0) {
9361 ctl_set_success(ctsio);
9362 ctl_done((union ctl_io *)ctsio);
9363 return (CTL_RETVAL_COMPLETE);
9366 lbalen = (struct ctl_lba_len_flags *)
9367 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9369 lbalen->len = num_blocks;
9371 lbalen->flags = CTL_LLF_COMPARE | flags;
9372 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9374 lbalen->flags = CTL_LLF_VERIFY | flags;
9375 ctsio->kern_total_len = 0;
9377 ctsio->kern_rel_offset = 0;
9379 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9380 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9385 ctl_report_luns(struct ctl_scsiio *ctsio)
9387 struct scsi_report_luns *cdb;
9388 struct scsi_report_luns_data *lun_data;
9389 struct ctl_lun *lun, *request_lun;
9390 int num_luns, retval;
9391 uint32_t alloc_len, lun_datalen;
9392 int num_filled, well_known;
9393 uint32_t initidx, targ_lun_id, lun_id;
9395 retval = CTL_RETVAL_COMPLETE;
9398 cdb = (struct scsi_report_luns *)ctsio->cdb;
9400 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9402 mtx_lock(&control_softc->ctl_lock);
9403 num_luns = control_softc->num_luns;
9404 mtx_unlock(&control_softc->ctl_lock);
9406 switch (cdb->select_report) {
9407 case RPL_REPORT_DEFAULT:
9408 case RPL_REPORT_ALL:
9410 case RPL_REPORT_WELLKNOWN:
9415 ctl_set_invalid_field(ctsio,
9421 ctl_done((union ctl_io *)ctsio);
9423 break; /* NOTREACHED */
9426 alloc_len = scsi_4btoul(cdb->length);
9428 * The initiator has to allocate at least 16 bytes for this request,
9429 * so he can at least get the header and the first LUN. Otherwise
9430 * we reject the request (per SPC-3 rev 14, section 6.21).
9432 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9433 sizeof(struct scsi_report_luns_lundata))) {
9434 ctl_set_invalid_field(ctsio,
9440 ctl_done((union ctl_io *)ctsio);
9444 request_lun = (struct ctl_lun *)
9445 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9447 lun_datalen = sizeof(*lun_data) +
9448 (num_luns * sizeof(struct scsi_report_luns_lundata));
9450 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9451 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9452 ctsio->kern_sg_entries = 0;
9454 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9456 mtx_lock(&control_softc->ctl_lock);
9457 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9458 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9459 if (lun_id >= CTL_MAX_LUNS)
9461 lun = control_softc->ctl_luns[lun_id];
9465 if (targ_lun_id <= 0xff) {
9467 * Peripheral addressing method, bus number 0.
9469 lun_data->luns[num_filled].lundata[0] =
9470 RPL_LUNDATA_ATYP_PERIPH;
9471 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9473 } else if (targ_lun_id <= 0x3fff) {
9475 * Flat addressing method.
9477 lun_data->luns[num_filled].lundata[0] =
9478 RPL_LUNDATA_ATYP_FLAT |
9479 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9480 #ifdef OLDCTLHEADERS
9481 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9482 (targ_lun_id & SRLD_BUS_LUN_MASK);
9484 lun_data->luns[num_filled].lundata[1] =
9485 #ifdef OLDCTLHEADERS
9486 targ_lun_id >> SRLD_BUS_LUN_BITS;
9488 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9491 printf("ctl_report_luns: bogus LUN number %jd, "
9492 "skipping\n", (intmax_t)targ_lun_id);
9495 * According to SPC-3, rev 14 section 6.21:
9497 * "The execution of a REPORT LUNS command to any valid and
9498 * installed logical unit shall clear the REPORTED LUNS DATA
9499 * HAS CHANGED unit attention condition for all logical
9500 * units of that target with respect to the requesting
9501 * initiator. A valid and installed logical unit is one
9502 * having a PERIPHERAL QUALIFIER of 000b in the standard
9503 * INQUIRY data (see 6.4.2)."
9505 * If request_lun is NULL, the LUN this report luns command
9506 * was issued to is either disabled or doesn't exist. In that
9507 * case, we shouldn't clear any pending lun change unit
9510 if (request_lun != NULL) {
9511 mtx_lock(&lun->lun_lock);
9512 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9513 mtx_unlock(&lun->lun_lock);
9516 mtx_unlock(&control_softc->ctl_lock);
9519 * It's quite possible that we've returned fewer LUNs than we allocated
9520 * space for. Trim it.
9522 lun_datalen = sizeof(*lun_data) +
9523 (num_filled * sizeof(struct scsi_report_luns_lundata));
9525 if (lun_datalen < alloc_len) {
9526 ctsio->residual = alloc_len - lun_datalen;
9527 ctsio->kern_data_len = lun_datalen;
9528 ctsio->kern_total_len = lun_datalen;
9530 ctsio->residual = 0;
9531 ctsio->kern_data_len = alloc_len;
9532 ctsio->kern_total_len = alloc_len;
9534 ctsio->kern_data_resid = 0;
9535 ctsio->kern_rel_offset = 0;
9536 ctsio->kern_sg_entries = 0;
9539 * We set this to the actual data length, regardless of how much
9540 * space we actually have to return results. If the user looks at
9541 * this value, he'll know whether or not he allocated enough space
9542 * and reissue the command if necessary. We don't support well
9543 * known logical units, so if the user asks for that, return none.
9545 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9548 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9551 ctsio->scsi_status = SCSI_STATUS_OK;
9553 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9554 ctsio->be_move_done = ctl_config_move_done;
9555 ctl_datamove((union ctl_io *)ctsio);
9561 ctl_request_sense(struct ctl_scsiio *ctsio)
9563 struct scsi_request_sense *cdb;
9564 struct scsi_sense_data *sense_ptr;
9565 struct ctl_lun *lun;
9568 scsi_sense_data_type sense_format;
9570 cdb = (struct scsi_request_sense *)ctsio->cdb;
9572 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9574 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9577 * Determine which sense format the user wants.
9579 if (cdb->byte2 & SRS_DESC)
9580 sense_format = SSD_TYPE_DESC;
9582 sense_format = SSD_TYPE_FIXED;
9584 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9585 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9586 ctsio->kern_sg_entries = 0;
9589 * struct scsi_sense_data, which is currently set to 256 bytes, is
9590 * larger than the largest allowed value for the length field in the
9591 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9593 ctsio->residual = 0;
9594 ctsio->kern_data_len = cdb->length;
9595 ctsio->kern_total_len = cdb->length;
9597 ctsio->kern_data_resid = 0;
9598 ctsio->kern_rel_offset = 0;
9599 ctsio->kern_sg_entries = 0;
9602 * If we don't have a LUN, we don't have any pending sense.
9608 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9610 * Check for pending sense, and then for pending unit attentions.
9611 * Pending sense gets returned first, then pending unit attentions.
9613 mtx_lock(&lun->lun_lock);
9615 if (ctl_is_set(lun->have_ca, initidx)) {
9616 scsi_sense_data_type stored_format;
9619 * Check to see which sense format was used for the stored
9622 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9625 * If the user requested a different sense format than the
9626 * one we stored, then we need to convert it to the other
9627 * format. If we're going from descriptor to fixed format
9628 * sense data, we may lose things in translation, depending
9629 * on what options were used.
9631 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9632 * for some reason we'll just copy it out as-is.
9634 if ((stored_format == SSD_TYPE_FIXED)
9635 && (sense_format == SSD_TYPE_DESC))
9636 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9637 &lun->pending_sense[initidx],
9638 (struct scsi_sense_data_desc *)sense_ptr);
9639 else if ((stored_format == SSD_TYPE_DESC)
9640 && (sense_format == SSD_TYPE_FIXED))
9641 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9642 &lun->pending_sense[initidx],
9643 (struct scsi_sense_data_fixed *)sense_ptr);
9645 memcpy(sense_ptr, &lun->pending_sense[initidx],
9646 ctl_min(sizeof(*sense_ptr),
9647 sizeof(lun->pending_sense[initidx])));
9649 ctl_clear_mask(lun->have_ca, initidx);
9653 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9654 ctl_ua_type ua_type;
9656 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9657 sense_ptr, sense_format);
9658 if (ua_type != CTL_UA_NONE)
9661 mtx_unlock(&lun->lun_lock);
9664 * We already have a pending error, return it.
9666 if (have_error != 0) {
9668 * We report the SCSI status as OK, since the status of the
9669 * request sense command itself is OK.
9671 ctsio->scsi_status = SCSI_STATUS_OK;
9674 * We report 0 for the sense length, because we aren't doing
9675 * autosense in this case. We're reporting sense as
9678 ctsio->sense_len = 0;
9679 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9680 ctsio->be_move_done = ctl_config_move_done;
9681 ctl_datamove((union ctl_io *)ctsio);
9683 return (CTL_RETVAL_COMPLETE);
9689 * No sense information to report, so we report that everything is
9692 ctl_set_sense_data(sense_ptr,
9695 /*current_error*/ 1,
9696 /*sense_key*/ SSD_KEY_NO_SENSE,
9701 ctsio->scsi_status = SCSI_STATUS_OK;
9704 * We report 0 for the sense length, because we aren't doing
9705 * autosense in this case. We're reporting sense as parameter data.
9707 ctsio->sense_len = 0;
9708 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9709 ctsio->be_move_done = ctl_config_move_done;
9710 ctl_datamove((union ctl_io *)ctsio);
9712 return (CTL_RETVAL_COMPLETE);
9716 ctl_tur(struct ctl_scsiio *ctsio)
9718 struct ctl_lun *lun;
9720 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9722 CTL_DEBUG_PRINT(("ctl_tur\n"));
9727 ctsio->scsi_status = SCSI_STATUS_OK;
9728 ctsio->io_hdr.status = CTL_SUCCESS;
9730 ctl_done((union ctl_io *)ctsio);
9732 return (CTL_RETVAL_COMPLETE);
9737 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9744 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9746 struct scsi_vpd_supported_pages *pages;
9748 struct ctl_lun *lun;
9750 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9752 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9753 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9754 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9755 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9756 ctsio->kern_sg_entries = 0;
9758 if (sup_page_size < alloc_len) {
9759 ctsio->residual = alloc_len - sup_page_size;
9760 ctsio->kern_data_len = sup_page_size;
9761 ctsio->kern_total_len = sup_page_size;
9763 ctsio->residual = 0;
9764 ctsio->kern_data_len = alloc_len;
9765 ctsio->kern_total_len = alloc_len;
9767 ctsio->kern_data_resid = 0;
9768 ctsio->kern_rel_offset = 0;
9769 ctsio->kern_sg_entries = 0;
9772 * The control device is always connected. The disk device, on the
9773 * other hand, may not be online all the time. Need to change this
9774 * to figure out whether the disk device is actually online or not.
9777 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9778 lun->be_lun->lun_type;
9780 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9782 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9783 /* Supported VPD pages */
9784 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9786 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9787 /* Device Identification */
9788 pages->page_list[2] = SVPD_DEVICE_ID;
9789 /* Extended INQUIRY Data */
9790 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9791 /* Mode Page Policy */
9792 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9794 pages->page_list[5] = SVPD_SCSI_PORTS;
9795 /* Third-party Copy */
9796 pages->page_list[6] = SVPD_SCSI_TPC;
9798 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9799 /* Block Device Characteristics */
9800 pages->page_list[8] = SVPD_BDC;
9801 /* Logical Block Provisioning */
9802 pages->page_list[9] = SVPD_LBP;
9804 ctsio->scsi_status = SCSI_STATUS_OK;
9806 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9807 ctsio->be_move_done = ctl_config_move_done;
9808 ctl_datamove((union ctl_io *)ctsio);
9810 return (CTL_RETVAL_COMPLETE);
9814 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9816 struct scsi_vpd_unit_serial_number *sn_ptr;
9817 struct ctl_lun *lun;
9819 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9821 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9822 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9823 ctsio->kern_sg_entries = 0;
9825 if (sizeof(*sn_ptr) < alloc_len) {
9826 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9827 ctsio->kern_data_len = sizeof(*sn_ptr);
9828 ctsio->kern_total_len = sizeof(*sn_ptr);
9830 ctsio->residual = 0;
9831 ctsio->kern_data_len = alloc_len;
9832 ctsio->kern_total_len = alloc_len;
9834 ctsio->kern_data_resid = 0;
9835 ctsio->kern_rel_offset = 0;
9836 ctsio->kern_sg_entries = 0;
9839 * The control device is always connected. The disk device, on the
9840 * other hand, may not be online all the time. Need to change this
9841 * to figure out whether the disk device is actually online or not.
9844 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9845 lun->be_lun->lun_type;
9847 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9849 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9850 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9852 * If we don't have a LUN, we just leave the serial number as
9855 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9857 strncpy((char *)sn_ptr->serial_num,
9858 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9860 ctsio->scsi_status = SCSI_STATUS_OK;
9862 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9863 ctsio->be_move_done = ctl_config_move_done;
9864 ctl_datamove((union ctl_io *)ctsio);
9866 return (CTL_RETVAL_COMPLETE);
9871 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9873 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9874 struct ctl_lun *lun;
9877 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9879 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9880 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9881 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9882 ctsio->kern_sg_entries = 0;
9884 if (data_len < alloc_len) {
9885 ctsio->residual = alloc_len - data_len;
9886 ctsio->kern_data_len = data_len;
9887 ctsio->kern_total_len = data_len;
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.
9902 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9903 lun->be_lun->lun_type;
9905 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9906 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9907 eid_ptr->page_length = data_len - 4;
9908 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9909 eid_ptr->flags3 = SVPD_EID_V_SUP;
9911 ctsio->scsi_status = SCSI_STATUS_OK;
9912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9913 ctsio->be_move_done = ctl_config_move_done;
9914 ctl_datamove((union ctl_io *)ctsio);
9916 return (CTL_RETVAL_COMPLETE);
9920 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9922 struct scsi_vpd_mode_page_policy *mpp_ptr;
9923 struct ctl_lun *lun;
9926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9928 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9929 sizeof(struct scsi_vpd_mode_page_policy_descr);
9931 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9932 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9933 ctsio->kern_sg_entries = 0;
9935 if (data_len < alloc_len) {
9936 ctsio->residual = alloc_len - data_len;
9937 ctsio->kern_data_len = data_len;
9938 ctsio->kern_total_len = data_len;
9940 ctsio->residual = 0;
9941 ctsio->kern_data_len = alloc_len;
9942 ctsio->kern_total_len = alloc_len;
9944 ctsio->kern_data_resid = 0;
9945 ctsio->kern_rel_offset = 0;
9946 ctsio->kern_sg_entries = 0;
9949 * The control device is always connected. The disk device, on the
9950 * other hand, may not be online all the time.
9953 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9954 lun->be_lun->lun_type;
9956 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9957 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9958 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9959 mpp_ptr->descr[0].page_code = 0x3f;
9960 mpp_ptr->descr[0].subpage_code = 0xff;
9961 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9963 ctsio->scsi_status = SCSI_STATUS_OK;
9964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9965 ctsio->be_move_done = ctl_config_move_done;
9966 ctl_datamove((union ctl_io *)ctsio);
9968 return (CTL_RETVAL_COMPLETE);
9972 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9974 struct scsi_vpd_device_id *devid_ptr;
9975 struct scsi_vpd_id_descriptor *desc;
9976 struct ctl_softc *ctl_softc;
9977 struct ctl_lun *lun;
9978 struct ctl_port *port;
9982 ctl_softc = control_softc;
9984 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9985 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9987 data_len = sizeof(struct scsi_vpd_device_id) +
9988 sizeof(struct scsi_vpd_id_descriptor) +
9989 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9990 sizeof(struct scsi_vpd_id_descriptor) +
9991 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9992 if (lun && lun->lun_devid)
9993 data_len += lun->lun_devid->len;
9994 if (port->port_devid)
9995 data_len += port->port_devid->len;
9996 if (port->target_devid)
9997 data_len += port->target_devid->len;
9999 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10000 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10001 ctsio->kern_sg_entries = 0;
10003 if (data_len < alloc_len) {
10004 ctsio->residual = alloc_len - data_len;
10005 ctsio->kern_data_len = data_len;
10006 ctsio->kern_total_len = data_len;
10008 ctsio->residual = 0;
10009 ctsio->kern_data_len = alloc_len;
10010 ctsio->kern_total_len = alloc_len;
10012 ctsio->kern_data_resid = 0;
10013 ctsio->kern_rel_offset = 0;
10014 ctsio->kern_sg_entries = 0;
10017 * The control device is always connected. The disk device, on the
10018 * other hand, may not be online all the time.
10021 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10022 lun->be_lun->lun_type;
10024 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10025 devid_ptr->page_code = SVPD_DEVICE_ID;
10026 scsi_ulto2b(data_len - 4, devid_ptr->length);
10028 if (port->port_type == CTL_PORT_FC)
10029 proto = SCSI_PROTO_FC << 4;
10030 else if (port->port_type == CTL_PORT_ISCSI)
10031 proto = SCSI_PROTO_ISCSI << 4;
10033 proto = SCSI_PROTO_SPI << 4;
10034 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10037 * We're using a LUN association here. i.e., this device ID is a
10038 * per-LUN identifier.
10040 if (lun && lun->lun_devid) {
10041 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10042 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10043 lun->lun_devid->len);
10047 * This is for the WWPN which is a port association.
10049 if (port->port_devid) {
10050 memcpy(desc, port->port_devid->data, port->port_devid->len);
10051 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10052 port->port_devid->len);
10056 * This is for the Relative Target Port(type 4h) identifier
10058 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10059 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10060 SVPD_ID_TYPE_RELTARG;
10062 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10063 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10064 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10067 * This is for the Target Port Group(type 5h) identifier
10069 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10070 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10071 SVPD_ID_TYPE_TPORTGRP;
10073 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10074 &desc->identifier[2]);
10075 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10076 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10079 * This is for the Target identifier
10081 if (port->target_devid) {
10082 memcpy(desc, port->target_devid->data, port->target_devid->len);
10085 ctsio->scsi_status = SCSI_STATUS_OK;
10086 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10087 ctsio->be_move_done = ctl_config_move_done;
10088 ctl_datamove((union ctl_io *)ctsio);
10090 return (CTL_RETVAL_COMPLETE);
10094 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10096 struct ctl_softc *softc = control_softc;
10097 struct scsi_vpd_scsi_ports *sp;
10098 struct scsi_vpd_port_designation *pd;
10099 struct scsi_vpd_port_designation_cont *pdc;
10100 struct ctl_lun *lun;
10101 struct ctl_port *port;
10102 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10103 int num_target_port_groups, single;
10105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10107 single = ctl_is_single;
10109 num_target_port_groups = 1;
10111 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10112 num_target_ports = 0;
10115 mtx_lock(&softc->ctl_lock);
10116 STAILQ_FOREACH(port, &softc->port_list, links) {
10117 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10120 ctl_map_lun_back(port->targ_port, lun->lun) >=
10123 num_target_ports++;
10124 if (port->init_devid)
10125 iid_len += port->init_devid->len;
10126 if (port->port_devid)
10127 id_len += port->port_devid->len;
10129 mtx_unlock(&softc->ctl_lock);
10131 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10132 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10133 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10134 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10135 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10136 ctsio->kern_sg_entries = 0;
10138 if (data_len < alloc_len) {
10139 ctsio->residual = alloc_len - data_len;
10140 ctsio->kern_data_len = data_len;
10141 ctsio->kern_total_len = data_len;
10143 ctsio->residual = 0;
10144 ctsio->kern_data_len = alloc_len;
10145 ctsio->kern_total_len = alloc_len;
10147 ctsio->kern_data_resid = 0;
10148 ctsio->kern_rel_offset = 0;
10149 ctsio->kern_sg_entries = 0;
10152 * The control device is always connected. The disk device, on the
10153 * other hand, may not be online all the time. Need to change this
10154 * to figure out whether the disk device is actually online or not.
10157 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10158 lun->be_lun->lun_type;
10160 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10162 sp->page_code = SVPD_SCSI_PORTS;
10163 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10165 pd = &sp->design[0];
10167 mtx_lock(&softc->ctl_lock);
10168 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10172 for (g = 0; g < num_target_port_groups; g++) {
10173 STAILQ_FOREACH(port, &softc->port_list, links) {
10174 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10177 ctl_map_lun_back(port->targ_port, lun->lun) >=
10180 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10181 scsi_ulto2b(p, pd->relative_port_id);
10182 if (port->init_devid && g == pg) {
10183 iid_len = port->init_devid->len;
10184 memcpy(pd->initiator_transportid,
10185 port->init_devid->data, port->init_devid->len);
10188 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10189 pdc = (struct scsi_vpd_port_designation_cont *)
10190 (&pd->initiator_transportid[iid_len]);
10191 if (port->port_devid && g == pg) {
10192 id_len = port->port_devid->len;
10193 memcpy(pdc->target_port_descriptors,
10194 port->port_devid->data, port->port_devid->len);
10197 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10198 pd = (struct scsi_vpd_port_designation *)
10199 ((uint8_t *)pdc->target_port_descriptors + id_len);
10202 mtx_unlock(&softc->ctl_lock);
10204 ctsio->scsi_status = SCSI_STATUS_OK;
10205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10206 ctsio->be_move_done = ctl_config_move_done;
10207 ctl_datamove((union ctl_io *)ctsio);
10209 return (CTL_RETVAL_COMPLETE);
10213 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10215 struct scsi_vpd_block_limits *bl_ptr;
10216 struct ctl_lun *lun;
10219 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10221 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10222 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10223 ctsio->kern_sg_entries = 0;
10225 if (sizeof(*bl_ptr) < alloc_len) {
10226 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10227 ctsio->kern_data_len = sizeof(*bl_ptr);
10228 ctsio->kern_total_len = sizeof(*bl_ptr);
10230 ctsio->residual = 0;
10231 ctsio->kern_data_len = alloc_len;
10232 ctsio->kern_total_len = alloc_len;
10234 ctsio->kern_data_resid = 0;
10235 ctsio->kern_rel_offset = 0;
10236 ctsio->kern_sg_entries = 0;
10239 * The control device is always connected. The disk device, on the
10240 * other hand, may not be online all the time. Need to change this
10241 * to figure out whether the disk device is actually online or not.
10244 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10245 lun->be_lun->lun_type;
10247 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10249 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10250 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10251 bl_ptr->max_cmp_write_len = 0xff;
10252 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10254 bs = lun->be_lun->blocksize;
10255 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10256 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10257 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10258 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10259 if (lun->be_lun->pblockexp != 0) {
10260 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10261 bl_ptr->opt_unmap_grain);
10262 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10263 bl_ptr->unmap_grain_align);
10266 scsi_ulto4b(lun->be_lun->atomicblock,
10267 bl_ptr->max_atomic_transfer_length);
10268 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10269 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10271 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10273 ctsio->scsi_status = SCSI_STATUS_OK;
10274 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10275 ctsio->be_move_done = ctl_config_move_done;
10276 ctl_datamove((union ctl_io *)ctsio);
10278 return (CTL_RETVAL_COMPLETE);
10282 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10284 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10285 struct ctl_lun *lun;
10287 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10289 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10290 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10291 ctsio->kern_sg_entries = 0;
10293 if (sizeof(*bdc_ptr) < alloc_len) {
10294 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10295 ctsio->kern_data_len = sizeof(*bdc_ptr);
10296 ctsio->kern_total_len = sizeof(*bdc_ptr);
10298 ctsio->residual = 0;
10299 ctsio->kern_data_len = alloc_len;
10300 ctsio->kern_total_len = alloc_len;
10302 ctsio->kern_data_resid = 0;
10303 ctsio->kern_rel_offset = 0;
10304 ctsio->kern_sg_entries = 0;
10307 * The control device is always connected. The disk device, on the
10308 * other hand, may not be online all the time. Need to change this
10309 * to figure out whether the disk device is actually online or not.
10312 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10313 lun->be_lun->lun_type;
10315 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10316 bdc_ptr->page_code = SVPD_BDC;
10317 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10318 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10319 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10321 ctsio->scsi_status = SCSI_STATUS_OK;
10322 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10323 ctsio->be_move_done = ctl_config_move_done;
10324 ctl_datamove((union ctl_io *)ctsio);
10326 return (CTL_RETVAL_COMPLETE);
10330 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10332 struct scsi_vpd_logical_block_prov *lbp_ptr;
10333 struct ctl_lun *lun;
10335 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10337 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10338 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10339 ctsio->kern_sg_entries = 0;
10341 if (sizeof(*lbp_ptr) < alloc_len) {
10342 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10343 ctsio->kern_data_len = sizeof(*lbp_ptr);
10344 ctsio->kern_total_len = sizeof(*lbp_ptr);
10346 ctsio->residual = 0;
10347 ctsio->kern_data_len = alloc_len;
10348 ctsio->kern_total_len = alloc_len;
10350 ctsio->kern_data_resid = 0;
10351 ctsio->kern_rel_offset = 0;
10352 ctsio->kern_sg_entries = 0;
10355 * The control device is always connected. The disk device, on the
10356 * other hand, may not be online all the time. Need to change this
10357 * to figure out whether the disk device is actually online or not.
10360 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10361 lun->be_lun->lun_type;
10363 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10365 lbp_ptr->page_code = SVPD_LBP;
10366 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10367 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10368 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10369 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10370 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10373 ctsio->scsi_status = SCSI_STATUS_OK;
10374 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10375 ctsio->be_move_done = ctl_config_move_done;
10376 ctl_datamove((union ctl_io *)ctsio);
10378 return (CTL_RETVAL_COMPLETE);
10382 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10384 struct scsi_inquiry *cdb;
10385 struct ctl_lun *lun;
10386 int alloc_len, retval;
10388 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10389 cdb = (struct scsi_inquiry *)ctsio->cdb;
10391 retval = CTL_RETVAL_COMPLETE;
10393 alloc_len = scsi_2btoul(cdb->length);
10395 switch (cdb->page_code) {
10396 case SVPD_SUPPORTED_PAGES:
10397 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10399 case SVPD_UNIT_SERIAL_NUMBER:
10400 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10402 case SVPD_DEVICE_ID:
10403 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10405 case SVPD_EXTENDED_INQUIRY_DATA:
10406 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10408 case SVPD_MODE_PAGE_POLICY:
10409 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10411 case SVPD_SCSI_PORTS:
10412 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10414 case SVPD_SCSI_TPC:
10415 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10417 case SVPD_BLOCK_LIMITS:
10418 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10421 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10424 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10427 ctl_set_invalid_field(ctsio,
10433 ctl_done((union ctl_io *)ctsio);
10434 retval = CTL_RETVAL_COMPLETE;
10442 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10444 struct scsi_inquiry_data *inq_ptr;
10445 struct scsi_inquiry *cdb;
10446 struct ctl_softc *ctl_softc;
10447 struct ctl_lun *lun;
10449 uint32_t alloc_len, data_len;
10450 ctl_port_type port_type;
10452 ctl_softc = control_softc;
10455 * Figure out whether we're talking to a Fibre Channel port or not.
10456 * We treat the ioctl front end, and any SCSI adapters, as packetized
10459 port_type = ctl_softc->ctl_ports[
10460 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10461 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10462 port_type = CTL_PORT_SCSI;
10464 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10465 cdb = (struct scsi_inquiry *)ctsio->cdb;
10466 alloc_len = scsi_2btoul(cdb->length);
10469 * We malloc the full inquiry data size here and fill it
10470 * in. If the user only asks for less, we'll give him
10473 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10474 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10475 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10476 ctsio->kern_sg_entries = 0;
10477 ctsio->kern_data_resid = 0;
10478 ctsio->kern_rel_offset = 0;
10480 if (data_len < alloc_len) {
10481 ctsio->residual = alloc_len - data_len;
10482 ctsio->kern_data_len = data_len;
10483 ctsio->kern_total_len = data_len;
10485 ctsio->residual = 0;
10486 ctsio->kern_data_len = alloc_len;
10487 ctsio->kern_total_len = alloc_len;
10491 * If we have a LUN configured, report it as connected. Otherwise,
10492 * report that it is offline or no device is supported, depending
10493 * on the value of inquiry_pq_no_lun.
10495 * According to the spec (SPC-4 r34), the peripheral qualifier
10496 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10498 * "A peripheral device having the specified peripheral device type
10499 * is not connected to this logical unit. However, the device
10500 * server is capable of supporting the specified peripheral device
10501 * type on this logical unit."
10503 * According to the same spec, the peripheral qualifier
10504 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10506 * "The device server is not capable of supporting a peripheral
10507 * device on this logical unit. For this peripheral qualifier the
10508 * peripheral device type shall be set to 1Fh. All other peripheral
10509 * device type values are reserved for this peripheral qualifier."
10511 * Given the text, it would seem that we probably want to report that
10512 * the LUN is offline here. There is no LUN connected, but we can
10513 * support a LUN at the given LUN number.
10515 * In the real world, though, it sounds like things are a little
10518 * - Linux, when presented with a LUN with the offline peripheral
10519 * qualifier, will create an sg driver instance for it. So when
10520 * you attach it to CTL, you wind up with a ton of sg driver
10521 * instances. (One for every LUN that Linux bothered to probe.)
10522 * Linux does this despite the fact that it issues a REPORT LUNs
10523 * to LUN 0 to get the inventory of supported LUNs.
10525 * - There is other anecdotal evidence (from Emulex folks) about
10526 * arrays that use the offline peripheral qualifier for LUNs that
10527 * are on the "passive" path in an active/passive array.
10529 * So the solution is provide a hopefully reasonable default
10530 * (return bad/no LUN) and allow the user to change the behavior
10531 * with a tunable/sysctl variable.
10534 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10535 lun->be_lun->lun_type;
10536 else if (ctl_softc->inquiry_pq_no_lun == 0)
10537 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10539 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10541 /* RMB in byte 2 is 0 */
10542 inq_ptr->version = SCSI_REV_SPC4;
10545 * According to SAM-3, even if a device only supports a single
10546 * level of LUN addressing, it should still set the HISUP bit:
10548 * 4.9.1 Logical unit numbers overview
10550 * All logical unit number formats described in this standard are
10551 * hierarchical in structure even when only a single level in that
10552 * hierarchy is used. The HISUP bit shall be set to one in the
10553 * standard INQUIRY data (see SPC-2) when any logical unit number
10554 * format described in this standard is used. Non-hierarchical
10555 * formats are outside the scope of this standard.
10557 * Therefore we set the HiSup bit here.
10559 * The reponse format is 2, per SPC-3.
10561 inq_ptr->response_format = SID_HiSup | 2;
10563 inq_ptr->additional_length = data_len -
10564 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10565 CTL_DEBUG_PRINT(("additional_length = %d\n",
10566 inq_ptr->additional_length));
10568 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10569 /* 16 bit addressing */
10570 if (port_type == CTL_PORT_SCSI)
10571 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10572 /* XXX set the SID_MultiP bit here if we're actually going to
10573 respond on multiple ports */
10574 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10576 /* 16 bit data bus, synchronous transfers */
10577 if (port_type == CTL_PORT_SCSI)
10578 inq_ptr->flags = SID_WBus16 | SID_Sync;
10580 * XXX KDM do we want to support tagged queueing on the control
10584 || (lun->be_lun->lun_type != T_PROCESSOR))
10585 inq_ptr->flags |= SID_CmdQue;
10587 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10588 * We have 8 bytes for the vendor name, and 16 bytes for the device
10589 * name and 4 bytes for the revision.
10591 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10592 "vendor")) == NULL) {
10593 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10595 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10596 strncpy(inq_ptr->vendor, val,
10597 min(sizeof(inq_ptr->vendor), strlen(val)));
10600 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10601 sizeof(inq_ptr->product));
10602 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10603 switch (lun->be_lun->lun_type) {
10605 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10606 sizeof(inq_ptr->product));
10609 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10610 sizeof(inq_ptr->product));
10613 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10614 sizeof(inq_ptr->product));
10618 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10619 strncpy(inq_ptr->product, val,
10620 min(sizeof(inq_ptr->product), strlen(val)));
10624 * XXX make this a macro somewhere so it automatically gets
10625 * incremented when we make changes.
10627 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10628 "revision")) == NULL) {
10629 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10631 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10632 strncpy(inq_ptr->revision, val,
10633 min(sizeof(inq_ptr->revision), strlen(val)));
10637 * For parallel SCSI, we support double transition and single
10638 * transition clocking. We also support QAS (Quick Arbitration
10639 * and Selection) and Information Unit transfers on both the
10640 * control and array devices.
10642 if (port_type == CTL_PORT_SCSI)
10643 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10646 /* SAM-5 (no version claimed) */
10647 scsi_ulto2b(0x00A0, inq_ptr->version1);
10648 /* SPC-4 (no version claimed) */
10649 scsi_ulto2b(0x0460, inq_ptr->version2);
10650 if (port_type == CTL_PORT_FC) {
10651 /* FCP-2 ANSI INCITS.350:2003 */
10652 scsi_ulto2b(0x0917, inq_ptr->version3);
10653 } else if (port_type == CTL_PORT_SCSI) {
10654 /* SPI-4 ANSI INCITS.362:200x */
10655 scsi_ulto2b(0x0B56, inq_ptr->version3);
10656 } else if (port_type == CTL_PORT_ISCSI) {
10657 /* iSCSI (no version claimed) */
10658 scsi_ulto2b(0x0960, inq_ptr->version3);
10659 } else if (port_type == CTL_PORT_SAS) {
10660 /* SAS (no version claimed) */
10661 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10665 /* SBC-4 (no version claimed) */
10666 scsi_ulto2b(0x0600, inq_ptr->version4);
10668 switch (lun->be_lun->lun_type) {
10670 /* SBC-4 (no version claimed) */
10671 scsi_ulto2b(0x0600, inq_ptr->version4);
10679 ctsio->scsi_status = SCSI_STATUS_OK;
10680 if (ctsio->kern_data_len > 0) {
10681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10682 ctsio->be_move_done = ctl_config_move_done;
10683 ctl_datamove((union ctl_io *)ctsio);
10685 ctsio->io_hdr.status = CTL_SUCCESS;
10686 ctl_done((union ctl_io *)ctsio);
10689 return (CTL_RETVAL_COMPLETE);
10693 ctl_inquiry(struct ctl_scsiio *ctsio)
10695 struct scsi_inquiry *cdb;
10698 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10700 cdb = (struct scsi_inquiry *)ctsio->cdb;
10701 if (cdb->byte2 & SI_EVPD)
10702 retval = ctl_inquiry_evpd(ctsio);
10703 else if (cdb->page_code == 0)
10704 retval = ctl_inquiry_std(ctsio);
10706 ctl_set_invalid_field(ctsio,
10712 ctl_done((union ctl_io *)ctsio);
10713 return (CTL_RETVAL_COMPLETE);
10720 * For known CDB types, parse the LBA and length.
10723 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10725 if (io->io_hdr.io_type != CTL_IO_SCSI)
10728 switch (io->scsiio.cdb[0]) {
10729 case COMPARE_AND_WRITE: {
10730 struct scsi_compare_and_write *cdb;
10732 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10734 *lba = scsi_8btou64(cdb->addr);
10735 *len = cdb->length;
10740 struct scsi_rw_6 *cdb;
10742 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10744 *lba = scsi_3btoul(cdb->addr);
10745 /* only 5 bits are valid in the most significant address byte */
10747 *len = cdb->length;
10752 struct scsi_rw_10 *cdb;
10754 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10756 *lba = scsi_4btoul(cdb->addr);
10757 *len = scsi_2btoul(cdb->length);
10760 case WRITE_VERIFY_10: {
10761 struct scsi_write_verify_10 *cdb;
10763 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10765 *lba = scsi_4btoul(cdb->addr);
10766 *len = scsi_2btoul(cdb->length);
10771 struct scsi_rw_12 *cdb;
10773 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10775 *lba = scsi_4btoul(cdb->addr);
10776 *len = scsi_4btoul(cdb->length);
10779 case WRITE_VERIFY_12: {
10780 struct scsi_write_verify_12 *cdb;
10782 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10784 *lba = scsi_4btoul(cdb->addr);
10785 *len = scsi_4btoul(cdb->length);
10790 case WRITE_ATOMIC_16: {
10791 struct scsi_rw_16 *cdb;
10793 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10795 *lba = scsi_8btou64(cdb->addr);
10796 *len = scsi_4btoul(cdb->length);
10799 case WRITE_VERIFY_16: {
10800 struct scsi_write_verify_16 *cdb;
10802 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10804 *lba = scsi_8btou64(cdb->addr);
10805 *len = scsi_4btoul(cdb->length);
10808 case WRITE_SAME_10: {
10809 struct scsi_write_same_10 *cdb;
10811 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10813 *lba = scsi_4btoul(cdb->addr);
10814 *len = scsi_2btoul(cdb->length);
10817 case WRITE_SAME_16: {
10818 struct scsi_write_same_16 *cdb;
10820 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10822 *lba = scsi_8btou64(cdb->addr);
10823 *len = scsi_4btoul(cdb->length);
10827 struct scsi_verify_10 *cdb;
10829 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10831 *lba = scsi_4btoul(cdb->addr);
10832 *len = scsi_2btoul(cdb->length);
10836 struct scsi_verify_12 *cdb;
10838 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10840 *lba = scsi_4btoul(cdb->addr);
10841 *len = scsi_4btoul(cdb->length);
10845 struct scsi_verify_16 *cdb;
10847 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10849 *lba = scsi_8btou64(cdb->addr);
10850 *len = scsi_4btoul(cdb->length);
10860 break; /* NOTREACHED */
10867 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10869 uint64_t endlba1, endlba2;
10871 endlba1 = lba1 + len1 - 1;
10872 endlba2 = lba2 + len2 - 1;
10874 if ((endlba1 < lba2)
10875 || (endlba2 < lba1))
10876 return (CTL_ACTION_PASS);
10878 return (CTL_ACTION_BLOCK);
10882 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10884 struct ctl_ptr_len_flags *ptrlen;
10885 struct scsi_unmap_desc *buf, *end, *range;
10889 /* If not UNMAP -- go other way. */
10890 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10891 io->scsiio.cdb[0] != UNMAP)
10892 return (CTL_ACTION_ERROR);
10894 /* If UNMAP without data -- block and wait for data. */
10895 ptrlen = (struct ctl_ptr_len_flags *)
10896 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10897 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10898 ptrlen->ptr == NULL)
10899 return (CTL_ACTION_BLOCK);
10901 /* UNMAP with data -- check for collision. */
10902 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10903 end = buf + ptrlen->len / sizeof(*buf);
10904 for (range = buf; range < end; range++) {
10905 lba = scsi_8btou64(range->lba);
10906 len = scsi_4btoul(range->length);
10907 if ((lba < lba2 + len2) && (lba + len > lba2))
10908 return (CTL_ACTION_BLOCK);
10910 return (CTL_ACTION_PASS);
10914 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10916 uint64_t lba1, lba2;
10917 uint64_t len1, len2;
10920 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10921 return (CTL_ACTION_ERROR);
10923 retval = ctl_extent_check_unmap(io2, lba1, len1);
10924 if (retval != CTL_ACTION_ERROR)
10927 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10928 return (CTL_ACTION_ERROR);
10930 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10934 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10935 union ctl_io *ooa_io)
10937 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10938 ctl_serialize_action *serialize_row;
10941 * The initiator attempted multiple untagged commands at the same
10942 * time. Can't do that.
10944 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10945 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10946 && ((pending_io->io_hdr.nexus.targ_port ==
10947 ooa_io->io_hdr.nexus.targ_port)
10948 && (pending_io->io_hdr.nexus.initid.id ==
10949 ooa_io->io_hdr.nexus.initid.id))
10950 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10951 return (CTL_ACTION_OVERLAP);
10954 * The initiator attempted to send multiple tagged commands with
10955 * the same ID. (It's fine if different initiators have the same
10958 * Even if all of those conditions are true, we don't kill the I/O
10959 * if the command ahead of us has been aborted. We won't end up
10960 * sending it to the FETD, and it's perfectly legal to resend a
10961 * command with the same tag number as long as the previous
10962 * instance of this tag number has been aborted somehow.
10964 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10965 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10966 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10967 && ((pending_io->io_hdr.nexus.targ_port ==
10968 ooa_io->io_hdr.nexus.targ_port)
10969 && (pending_io->io_hdr.nexus.initid.id ==
10970 ooa_io->io_hdr.nexus.initid.id))
10971 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10972 return (CTL_ACTION_OVERLAP_TAG);
10975 * If we get a head of queue tag, SAM-3 says that we should
10976 * immediately execute it.
10978 * What happens if this command would normally block for some other
10979 * reason? e.g. a request sense with a head of queue tag
10980 * immediately after a write. Normally that would block, but this
10981 * will result in its getting executed immediately...
10983 * We currently return "pass" instead of "skip", so we'll end up
10984 * going through the rest of the queue to check for overlapped tags.
10986 * XXX KDM check for other types of blockage first??
10988 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10989 return (CTL_ACTION_PASS);
10992 * Ordered tags have to block until all items ahead of them
10993 * have completed. If we get called with an ordered tag, we always
10994 * block, if something else is ahead of us in the queue.
10996 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10997 return (CTL_ACTION_BLOCK);
11000 * Simple tags get blocked until all head of queue and ordered tags
11001 * ahead of them have completed. I'm lumping untagged commands in
11002 * with simple tags here. XXX KDM is that the right thing to do?
11004 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11005 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11006 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11007 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11008 return (CTL_ACTION_BLOCK);
11010 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11011 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11013 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11015 switch (serialize_row[pending_entry->seridx]) {
11016 case CTL_SER_BLOCK:
11017 return (CTL_ACTION_BLOCK);
11018 case CTL_SER_EXTENT:
11019 return (ctl_extent_check(pending_io, ooa_io));
11020 case CTL_SER_EXTENTOPT:
11021 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11022 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11023 return (ctl_extent_check(pending_io, ooa_io));
11026 return (CTL_ACTION_PASS);
11027 case CTL_SER_BLOCKOPT:
11028 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11029 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11030 return (CTL_ACTION_BLOCK);
11031 return (CTL_ACTION_PASS);
11033 return (CTL_ACTION_SKIP);
11035 panic("invalid serialization value %d",
11036 serialize_row[pending_entry->seridx]);
11039 return (CTL_ACTION_ERROR);
11043 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11045 * - pending_io is generally either incoming, or on the blocked queue
11046 * - starting I/O is the I/O we want to start the check with.
11049 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11050 union ctl_io *starting_io)
11052 union ctl_io *ooa_io;
11055 mtx_assert(&lun->lun_lock, MA_OWNED);
11058 * Run back along the OOA queue, starting with the current
11059 * blocked I/O and going through every I/O before it on the
11060 * queue. If starting_io is NULL, we'll just end up returning
11063 for (ooa_io = starting_io; ooa_io != NULL;
11064 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11068 * This routine just checks to see whether
11069 * cur_blocked is blocked by ooa_io, which is ahead
11070 * of it in the queue. It doesn't queue/dequeue
11073 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11075 case CTL_ACTION_BLOCK:
11076 case CTL_ACTION_OVERLAP:
11077 case CTL_ACTION_OVERLAP_TAG:
11078 case CTL_ACTION_SKIP:
11079 case CTL_ACTION_ERROR:
11081 break; /* NOTREACHED */
11082 case CTL_ACTION_PASS:
11085 panic("invalid action %d", action);
11086 break; /* NOTREACHED */
11090 return (CTL_ACTION_PASS);
11095 * - An I/O has just completed, and has been removed from the per-LUN OOA
11096 * queue, so some items on the blocked queue may now be unblocked.
11099 ctl_check_blocked(struct ctl_lun *lun)
11101 union ctl_io *cur_blocked, *next_blocked;
11103 mtx_assert(&lun->lun_lock, MA_OWNED);
11106 * Run forward from the head of the blocked queue, checking each
11107 * entry against the I/Os prior to it on the OOA queue to see if
11108 * there is still any blockage.
11110 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11111 * with our removing a variable on it while it is traversing the
11114 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11115 cur_blocked != NULL; cur_blocked = next_blocked) {
11116 union ctl_io *prev_ooa;
11119 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11122 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11123 ctl_ooaq, ooa_links);
11126 * If cur_blocked happens to be the first item in the OOA
11127 * queue now, prev_ooa will be NULL, and the action
11128 * returned will just be CTL_ACTION_PASS.
11130 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11133 case CTL_ACTION_BLOCK:
11134 /* Nothing to do here, still blocked */
11136 case CTL_ACTION_OVERLAP:
11137 case CTL_ACTION_OVERLAP_TAG:
11139 * This shouldn't happen! In theory we've already
11140 * checked this command for overlap...
11143 case CTL_ACTION_PASS:
11144 case CTL_ACTION_SKIP: {
11145 struct ctl_softc *softc;
11146 const struct ctl_cmd_entry *entry;
11151 * The skip case shouldn't happen, this transaction
11152 * should have never made it onto the blocked queue.
11155 * This I/O is no longer blocked, we can remove it
11156 * from the blocked queue. Since this is a TAILQ
11157 * (doubly linked list), we can do O(1) removals
11158 * from any place on the list.
11160 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11162 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11164 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11166 * Need to send IO back to original side to
11169 union ctl_ha_msg msg_info;
11171 msg_info.hdr.original_sc =
11172 cur_blocked->io_hdr.original_sc;
11173 msg_info.hdr.serializing_sc = cur_blocked;
11174 msg_info.hdr.msg_type = CTL_MSG_R2R;
11175 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11176 &msg_info, sizeof(msg_info), 0)) >
11177 CTL_HA_STATUS_SUCCESS) {
11178 printf("CTL:Check Blocked error from "
11179 "ctl_ha_msg_send %d\n",
11184 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11185 softc = control_softc;
11187 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11190 * Check this I/O for LUN state changes that may
11191 * have happened while this command was blocked.
11192 * The LUN state may have been changed by a command
11193 * ahead of us in the queue, so we need to re-check
11194 * for any states that can be caused by SCSI
11197 if (ctl_scsiio_lun_check(softc, lun, entry,
11198 &cur_blocked->scsiio) == 0) {
11199 cur_blocked->io_hdr.flags |=
11200 CTL_FLAG_IS_WAS_ON_RTR;
11201 ctl_enqueue_rtr(cur_blocked);
11203 ctl_done(cur_blocked);
11208 * This probably shouldn't happen -- we shouldn't
11209 * get CTL_ACTION_ERROR, or anything else.
11215 return (CTL_RETVAL_COMPLETE);
11219 * This routine (with one exception) checks LUN flags that can be set by
11220 * commands ahead of us in the OOA queue. These flags have to be checked
11221 * when a command initially comes in, and when we pull a command off the
11222 * blocked queue and are preparing to execute it. The reason we have to
11223 * check these flags for commands on the blocked queue is that the LUN
11224 * state may have been changed by a command ahead of us while we're on the
11227 * Ordering is somewhat important with these checks, so please pay
11228 * careful attention to the placement of any new checks.
11231 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11232 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11239 mtx_assert(&lun->lun_lock, MA_OWNED);
11242 * If this shelf is a secondary shelf controller, we have to reject
11243 * any media access commands.
11246 /* No longer needed for HA */
11247 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11248 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11249 ctl_set_lun_standby(ctsio);
11255 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11256 if (lun->flags & CTL_LUN_READONLY) {
11257 ctl_set_sense(ctsio, /*current_error*/ 1,
11258 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11259 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11263 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11264 .eca_and_aen & SCP_SWP) != 0) {
11265 ctl_set_sense(ctsio, /*current_error*/ 1,
11266 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11267 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11274 * Check for a reservation conflict. If this command isn't allowed
11275 * even on reserved LUNs, and if this initiator isn't the one who
11276 * reserved us, reject the command with a reservation conflict.
11278 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11279 if ((lun->flags & CTL_LUN_RESERVED)
11280 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11281 if (lun->res_idx != residx) {
11282 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11283 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11289 if ((lun->flags & CTL_LUN_PR_RESERVED)
11290 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11292 * if we aren't registered or it's a res holder type
11293 * reservation and this isn't the res holder then set a
11295 * NOTE: Commands which might be allowed on write exclusive
11296 * type reservations are checked in the particular command
11297 * for a conflict. Read and SSU are the only ones.
11299 if (lun->pr_keys[residx] == 0
11300 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11301 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11302 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11309 if ((lun->flags & CTL_LUN_OFFLINE)
11310 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11311 ctl_set_lun_not_ready(ctsio);
11317 * If the LUN is stopped, see if this particular command is allowed
11318 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11320 if ((lun->flags & CTL_LUN_STOPPED)
11321 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11322 /* "Logical unit not ready, initializing cmd. required" */
11323 ctl_set_lun_stopped(ctsio);
11328 if ((lun->flags & CTL_LUN_INOPERABLE)
11329 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11330 /* "Medium format corrupted" */
11331 ctl_set_medium_format_corrupted(ctsio);
11342 ctl_failover_io(union ctl_io *io, int have_lock)
11344 ctl_set_busy(&io->scsiio);
11351 struct ctl_lun *lun;
11352 struct ctl_softc *ctl_softc;
11353 union ctl_io *next_io, *pending_io;
11358 ctl_softc = control_softc;
11360 mtx_lock(&ctl_softc->ctl_lock);
11362 * Remove any cmds from the other SC from the rtr queue. These
11363 * will obviously only be for LUNs for which we're the primary.
11364 * We can't send status or get/send data for these commands.
11365 * Since they haven't been executed yet, we can just remove them.
11366 * We'll either abort them or delete them below, depending on
11367 * which HA mode we're in.
11370 mtx_lock(&ctl_softc->queue_lock);
11371 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11372 io != NULL; io = next_io) {
11373 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11374 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11375 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11376 ctl_io_hdr, links);
11378 mtx_unlock(&ctl_softc->queue_lock);
11381 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11382 lun = ctl_softc->ctl_luns[lun_idx];
11387 * Processor LUNs are primary on both sides.
11388 * XXX will this always be true?
11390 if (lun->be_lun->lun_type == T_PROCESSOR)
11393 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11394 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11395 printf("FAILOVER: primary lun %d\n", lun_idx);
11397 * Remove all commands from the other SC. First from the
11398 * blocked queue then from the ooa queue. Once we have
11399 * removed them. Call ctl_check_blocked to see if there
11400 * is anything that can run.
11402 for (io = (union ctl_io *)TAILQ_FIRST(
11403 &lun->blocked_queue); io != NULL; io = next_io) {
11405 next_io = (union ctl_io *)TAILQ_NEXT(
11406 &io->io_hdr, blocked_links);
11408 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11409 TAILQ_REMOVE(&lun->blocked_queue,
11410 &io->io_hdr,blocked_links);
11411 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11412 TAILQ_REMOVE(&lun->ooa_queue,
11413 &io->io_hdr, ooa_links);
11419 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11420 io != NULL; io = next_io) {
11422 next_io = (union ctl_io *)TAILQ_NEXT(
11423 &io->io_hdr, ooa_links);
11425 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11427 TAILQ_REMOVE(&lun->ooa_queue,
11434 ctl_check_blocked(lun);
11435 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11436 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11438 printf("FAILOVER: primary lun %d\n", lun_idx);
11440 * Abort all commands from the other SC. We can't
11441 * send status back for them now. These should get
11442 * cleaned up when they are completed or come out
11443 * for a datamove operation.
11445 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11446 io != NULL; io = next_io) {
11447 next_io = (union ctl_io *)TAILQ_NEXT(
11448 &io->io_hdr, ooa_links);
11450 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11451 io->io_hdr.flags |= CTL_FLAG_ABORT;
11453 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11454 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11456 printf("FAILOVER: secondary lun %d\n", lun_idx);
11458 lun->flags |= CTL_LUN_PRIMARY_SC;
11461 * We send all I/O that was sent to this controller
11462 * and redirected to the other side back with
11463 * busy status, and have the initiator retry it.
11464 * Figuring out how much data has been transferred,
11465 * etc. and picking up where we left off would be
11468 * XXX KDM need to remove I/O from the blocked
11471 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11472 &lun->ooa_queue); pending_io != NULL;
11473 pending_io = next_io) {
11475 next_io = (union ctl_io *)TAILQ_NEXT(
11476 &pending_io->io_hdr, ooa_links);
11478 pending_io->io_hdr.flags &=
11479 ~CTL_FLAG_SENT_2OTHER_SC;
11481 if (pending_io->io_hdr.flags &
11482 CTL_FLAG_IO_ACTIVE) {
11483 pending_io->io_hdr.flags |=
11486 ctl_set_busy(&pending_io->scsiio);
11487 ctl_done(pending_io);
11492 * Build Unit Attention
11494 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11495 lun->pending_ua[i] |=
11496 CTL_UA_ASYM_ACC_CHANGE;
11498 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11499 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11500 printf("FAILOVER: secondary lun %d\n", lun_idx);
11502 * if the first io on the OOA is not on the RtR queue
11505 lun->flags |= CTL_LUN_PRIMARY_SC;
11507 pending_io = (union ctl_io *)TAILQ_FIRST(
11509 if (pending_io==NULL) {
11510 printf("Nothing on OOA queue\n");
11514 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11515 if ((pending_io->io_hdr.flags &
11516 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11517 pending_io->io_hdr.flags |=
11518 CTL_FLAG_IS_WAS_ON_RTR;
11519 ctl_enqueue_rtr(pending_io);
11524 printf("Tag 0x%04x is running\n",
11525 pending_io->scsiio.tag_num);
11529 next_io = (union ctl_io *)TAILQ_NEXT(
11530 &pending_io->io_hdr, ooa_links);
11531 for (pending_io=next_io; pending_io != NULL;
11532 pending_io = next_io) {
11533 pending_io->io_hdr.flags &=
11534 ~CTL_FLAG_SENT_2OTHER_SC;
11535 next_io = (union ctl_io *)TAILQ_NEXT(
11536 &pending_io->io_hdr, ooa_links);
11537 if (pending_io->io_hdr.flags &
11538 CTL_FLAG_IS_WAS_ON_RTR) {
11540 printf("Tag 0x%04x is running\n",
11541 pending_io->scsiio.tag_num);
11546 switch (ctl_check_ooa(lun, pending_io,
11547 (union ctl_io *)TAILQ_PREV(
11548 &pending_io->io_hdr, ctl_ooaq,
11551 case CTL_ACTION_BLOCK:
11552 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11553 &pending_io->io_hdr,
11555 pending_io->io_hdr.flags |=
11558 case CTL_ACTION_PASS:
11559 case CTL_ACTION_SKIP:
11560 pending_io->io_hdr.flags |=
11561 CTL_FLAG_IS_WAS_ON_RTR;
11562 ctl_enqueue_rtr(pending_io);
11564 case CTL_ACTION_OVERLAP:
11565 ctl_set_overlapped_cmd(
11566 (struct ctl_scsiio *)pending_io);
11567 ctl_done(pending_io);
11569 case CTL_ACTION_OVERLAP_TAG:
11570 ctl_set_overlapped_tag(
11571 (struct ctl_scsiio *)pending_io,
11572 pending_io->scsiio.tag_num & 0xff);
11573 ctl_done(pending_io);
11575 case CTL_ACTION_ERROR:
11577 ctl_set_internal_failure(
11578 (struct ctl_scsiio *)pending_io,
11581 ctl_done(pending_io);
11587 * Build Unit Attention
11589 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11590 lun->pending_ua[i] |=
11591 CTL_UA_ASYM_ACC_CHANGE;
11594 panic("Unhandled HA mode failover, LUN flags = %#x, "
11595 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11599 mtx_unlock(&ctl_softc->ctl_lock);
11603 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11605 struct ctl_lun *lun;
11606 const struct ctl_cmd_entry *entry;
11607 uint32_t initidx, targ_lun;
11614 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11615 if ((targ_lun < CTL_MAX_LUNS)
11616 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11617 lun = ctl_softc->ctl_luns[targ_lun];
11619 * If the LUN is invalid, pretend that it doesn't exist.
11620 * It will go away as soon as all pending I/O has been
11623 if (lun->flags & CTL_LUN_DISABLED) {
11626 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11627 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11629 if (lun->be_lun->lun_type == T_PROCESSOR) {
11630 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11634 * Every I/O goes into the OOA queue for a
11635 * particular LUN, and stays there until completion.
11637 mtx_lock(&lun->lun_lock);
11638 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11642 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11643 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11646 /* Get command entry and return error if it is unsuppotyed. */
11647 entry = ctl_validate_command(ctsio);
11648 if (entry == NULL) {
11650 mtx_unlock(&lun->lun_lock);
11654 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11655 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11658 * Check to see whether we can send this command to LUNs that don't
11659 * exist. This should pretty much only be the case for inquiry
11660 * and request sense. Further checks, below, really require having
11661 * a LUN, so we can't really check the command anymore. Just put
11662 * it on the rtr queue.
11665 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11666 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11667 ctl_enqueue_rtr((union ctl_io *)ctsio);
11671 ctl_set_unsupported_lun(ctsio);
11672 ctl_done((union ctl_io *)ctsio);
11673 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11677 * Make sure we support this particular command on this LUN.
11678 * e.g., we don't support writes to the control LUN.
11680 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11681 mtx_unlock(&lun->lun_lock);
11682 ctl_set_invalid_opcode(ctsio);
11683 ctl_done((union ctl_io *)ctsio);
11688 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11692 * If we've got a request sense, it'll clear the contingent
11693 * allegiance condition. Otherwise, if we have a CA condition for
11694 * this initiator, clear it, because it sent down a command other
11695 * than request sense.
11697 if ((ctsio->cdb[0] != REQUEST_SENSE)
11698 && (ctl_is_set(lun->have_ca, initidx)))
11699 ctl_clear_mask(lun->have_ca, initidx);
11703 * If the command has this flag set, it handles its own unit
11704 * attention reporting, we shouldn't do anything. Otherwise we
11705 * check for any pending unit attentions, and send them back to the
11706 * initiator. We only do this when a command initially comes in,
11707 * not when we pull it off the blocked queue.
11709 * According to SAM-3, section 5.3.2, the order that things get
11710 * presented back to the host is basically unit attentions caused
11711 * by some sort of reset event, busy status, reservation conflicts
11712 * or task set full, and finally any other status.
11714 * One issue here is that some of the unit attentions we report
11715 * don't fall into the "reset" category (e.g. "reported luns data
11716 * has changed"). So reporting it here, before the reservation
11717 * check, may be technically wrong. I guess the only thing to do
11718 * would be to check for and report the reset events here, and then
11719 * check for the other unit attention types after we check for a
11720 * reservation conflict.
11722 * XXX KDM need to fix this
11724 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11725 ctl_ua_type ua_type;
11727 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11728 scsi_sense_data_type sense_format;
11731 sense_format = (lun->flags &
11732 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11735 sense_format = SSD_TYPE_FIXED;
11737 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11738 &ctsio->sense_data, sense_format);
11739 if (ua_type != CTL_UA_NONE) {
11740 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11741 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11743 ctsio->sense_len = SSD_FULL_SIZE;
11744 mtx_unlock(&lun->lun_lock);
11745 ctl_done((union ctl_io *)ctsio);
11752 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11753 mtx_unlock(&lun->lun_lock);
11754 ctl_done((union ctl_io *)ctsio);
11759 * XXX CHD this is where we want to send IO to other side if
11760 * this LUN is secondary on this SC. We will need to make a copy
11761 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11762 * the copy we send as FROM_OTHER.
11763 * We also need to stuff the address of the original IO so we can
11764 * find it easily. Something similar will need be done on the other
11765 * side so when we are done we can find the copy.
11767 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11768 union ctl_ha_msg msg_info;
11771 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11773 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11774 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11776 printf("1. ctsio %p\n", ctsio);
11778 msg_info.hdr.serializing_sc = NULL;
11779 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11780 msg_info.scsi.tag_num = ctsio->tag_num;
11781 msg_info.scsi.tag_type = ctsio->tag_type;
11782 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11784 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11786 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11787 (void *)&msg_info, sizeof(msg_info), 0)) >
11788 CTL_HA_STATUS_SUCCESS) {
11789 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11791 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11794 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11799 * XXX KDM this I/O is off the incoming queue, but hasn't
11800 * been inserted on any other queue. We may need to come
11801 * up with a holding queue while we wait for serialization
11802 * so that we have an idea of what we're waiting for from
11805 mtx_unlock(&lun->lun_lock);
11809 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11810 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11811 ctl_ooaq, ooa_links))) {
11812 case CTL_ACTION_BLOCK:
11813 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11814 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11816 mtx_unlock(&lun->lun_lock);
11818 case CTL_ACTION_PASS:
11819 case CTL_ACTION_SKIP:
11820 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11821 mtx_unlock(&lun->lun_lock);
11822 ctl_enqueue_rtr((union ctl_io *)ctsio);
11824 case CTL_ACTION_OVERLAP:
11825 mtx_unlock(&lun->lun_lock);
11826 ctl_set_overlapped_cmd(ctsio);
11827 ctl_done((union ctl_io *)ctsio);
11829 case CTL_ACTION_OVERLAP_TAG:
11830 mtx_unlock(&lun->lun_lock);
11831 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11832 ctl_done((union ctl_io *)ctsio);
11834 case CTL_ACTION_ERROR:
11836 mtx_unlock(&lun->lun_lock);
11837 ctl_set_internal_failure(ctsio,
11839 /*retry_count*/ 0);
11840 ctl_done((union ctl_io *)ctsio);
11846 const struct ctl_cmd_entry *
11847 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11849 const struct ctl_cmd_entry *entry;
11850 int service_action;
11852 entry = &ctl_cmd_table[ctsio->cdb[0]];
11854 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11855 if (entry->flags & CTL_CMD_FLAG_SA5) {
11856 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11857 entry = &((const struct ctl_cmd_entry *)
11858 entry->execute)[service_action];
11863 const struct ctl_cmd_entry *
11864 ctl_validate_command(struct ctl_scsiio *ctsio)
11866 const struct ctl_cmd_entry *entry;
11870 entry = ctl_get_cmd_entry(ctsio, &sa);
11871 if (entry->execute == NULL) {
11873 ctl_set_invalid_field(ctsio,
11880 ctl_set_invalid_opcode(ctsio);
11881 ctl_done((union ctl_io *)ctsio);
11884 KASSERT(entry->length > 0,
11885 ("Not defined length for command 0x%02x/0x%02x",
11886 ctsio->cdb[0], ctsio->cdb[1]));
11887 for (i = 1; i < entry->length; i++) {
11888 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11891 ctl_set_invalid_field(ctsio,
11896 /*bit*/ fls(diff) - 1);
11897 ctl_done((union ctl_io *)ctsio);
11904 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11907 switch (lun_type) {
11909 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11910 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11914 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11915 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11925 ctl_scsiio(struct ctl_scsiio *ctsio)
11928 const struct ctl_cmd_entry *entry;
11930 retval = CTL_RETVAL_COMPLETE;
11932 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11934 entry = ctl_get_cmd_entry(ctsio, NULL);
11937 * If this I/O has been aborted, just send it straight to
11938 * ctl_done() without executing it.
11940 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11941 ctl_done((union ctl_io *)ctsio);
11946 * All the checks should have been handled by ctl_scsiio_precheck().
11947 * We should be clear now to just execute the I/O.
11949 retval = entry->execute(ctsio);
11956 * Since we only implement one target right now, a bus reset simply resets
11957 * our single target.
11960 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11962 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11966 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11967 ctl_ua_type ua_type)
11969 struct ctl_lun *lun;
11972 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11973 union ctl_ha_msg msg_info;
11975 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11976 msg_info.hdr.nexus = io->io_hdr.nexus;
11977 if (ua_type==CTL_UA_TARG_RESET)
11978 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11980 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11981 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11982 msg_info.hdr.original_sc = NULL;
11983 msg_info.hdr.serializing_sc = NULL;
11984 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11985 (void *)&msg_info, sizeof(msg_info), 0)) {
11990 mtx_lock(&ctl_softc->ctl_lock);
11991 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11992 retval += ctl_lun_reset(lun, io, ua_type);
11993 mtx_unlock(&ctl_softc->ctl_lock);
11999 * The LUN should always be set. The I/O is optional, and is used to
12000 * distinguish between I/Os sent by this initiator, and by other
12001 * initiators. We set unit attention for initiators other than this one.
12002 * SAM-3 is vague on this point. It does say that a unit attention should
12003 * be established for other initiators when a LUN is reset (see section
12004 * 5.7.3), but it doesn't specifically say that the unit attention should
12005 * be established for this particular initiator when a LUN is reset. Here
12006 * is the relevant text, from SAM-3 rev 8:
12008 * 5.7.2 When a SCSI initiator port aborts its own tasks
12010 * When a SCSI initiator port causes its own task(s) to be aborted, no
12011 * notification that the task(s) have been aborted shall be returned to
12012 * the SCSI initiator port other than the completion response for the
12013 * command or task management function action that caused the task(s) to
12014 * be aborted and notification(s) associated with related effects of the
12015 * action (e.g., a reset unit attention condition).
12017 * XXX KDM for now, we're setting unit attention for all initiators.
12020 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
12024 uint32_t initindex;
12028 mtx_lock(&lun->lun_lock);
12030 * Run through the OOA queue and abort each I/O.
12033 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12035 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12036 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12037 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12041 * This version sets unit attention for every
12044 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12045 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12046 if (initindex == i)
12048 lun->pending_ua[i] |= ua_type;
12053 * A reset (any kind, really) clears reservations established with
12054 * RESERVE/RELEASE. It does not clear reservations established
12055 * with PERSISTENT RESERVE OUT, but we don't support that at the
12056 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12057 * reservations made with the RESERVE/RELEASE commands, because
12058 * those commands are obsolete in SPC-3.
12060 lun->flags &= ~CTL_LUN_RESERVED;
12062 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12064 ctl_clear_mask(lun->have_ca, i);
12066 lun->pending_ua[i] |= ua_type;
12068 mtx_unlock(&lun->lun_lock);
12074 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12079 mtx_assert(&lun->lun_lock, MA_OWNED);
12082 * Run through the OOA queue and attempt to find the given I/O.
12083 * The target port, initiator ID, tag type and tag number have to
12084 * match the values that we got from the initiator. If we have an
12085 * untagged command to abort, simply abort the first untagged command
12086 * we come to. We only allow one untagged command at a time of course.
12088 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12089 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12091 if ((targ_port == UINT32_MAX ||
12092 targ_port == xio->io_hdr.nexus.targ_port) &&
12093 (init_id == UINT32_MAX ||
12094 init_id == xio->io_hdr.nexus.initid.id)) {
12095 if (targ_port != xio->io_hdr.nexus.targ_port ||
12096 init_id != xio->io_hdr.nexus.initid.id)
12097 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12098 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12099 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12100 union ctl_ha_msg msg_info;
12102 msg_info.hdr.nexus = xio->io_hdr.nexus;
12103 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12104 msg_info.task.tag_num = xio->scsiio.tag_num;
12105 msg_info.task.tag_type = xio->scsiio.tag_type;
12106 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12107 msg_info.hdr.original_sc = NULL;
12108 msg_info.hdr.serializing_sc = NULL;
12109 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12110 (void *)&msg_info, sizeof(msg_info), 0);
12117 ctl_abort_task_set(union ctl_io *io)
12119 struct ctl_softc *softc = control_softc;
12120 struct ctl_lun *lun;
12126 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12127 mtx_lock(&softc->ctl_lock);
12128 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12129 lun = softc->ctl_luns[targ_lun];
12131 mtx_unlock(&softc->ctl_lock);
12135 mtx_lock(&lun->lun_lock);
12136 mtx_unlock(&softc->ctl_lock);
12137 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12138 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12139 io->io_hdr.nexus.initid.id,
12140 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12141 } else { /* CTL_TASK_CLEAR_TASK_SET */
12142 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12143 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12145 mtx_unlock(&lun->lun_lock);
12150 ctl_i_t_nexus_reset(union ctl_io *io)
12152 struct ctl_softc *softc = control_softc;
12153 struct ctl_lun *lun;
12154 uint32_t initindex, residx;
12156 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12157 residx = ctl_get_resindex(&io->io_hdr.nexus);
12158 mtx_lock(&softc->ctl_lock);
12159 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12160 mtx_lock(&lun->lun_lock);
12161 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12162 io->io_hdr.nexus.initid.id,
12163 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12165 ctl_clear_mask(lun->have_ca, initindex);
12167 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12168 lun->flags &= ~CTL_LUN_RESERVED;
12169 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12170 mtx_unlock(&lun->lun_lock);
12172 mtx_unlock(&softc->ctl_lock);
12177 ctl_abort_task(union ctl_io *io)
12180 struct ctl_lun *lun;
12181 struct ctl_softc *ctl_softc;
12184 char printbuf[128];
12189 ctl_softc = control_softc;
12195 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12196 mtx_lock(&ctl_softc->ctl_lock);
12197 if ((targ_lun < CTL_MAX_LUNS)
12198 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12199 lun = ctl_softc->ctl_luns[targ_lun];
12201 mtx_unlock(&ctl_softc->ctl_lock);
12206 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12207 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12210 mtx_lock(&lun->lun_lock);
12211 mtx_unlock(&ctl_softc->ctl_lock);
12213 * Run through the OOA queue and attempt to find the given I/O.
12214 * The target port, initiator ID, tag type and tag number have to
12215 * match the values that we got from the initiator. If we have an
12216 * untagged command to abort, simply abort the first untagged command
12217 * we come to. We only allow one untagged command at a time of course.
12220 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12222 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12223 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12225 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12227 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12228 lun->lun, xio->scsiio.tag_num,
12229 xio->scsiio.tag_type,
12230 (xio->io_hdr.blocked_links.tqe_prev
12231 == NULL) ? "" : " BLOCKED",
12232 (xio->io_hdr.flags &
12233 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12234 (xio->io_hdr.flags &
12235 CTL_FLAG_ABORT) ? " ABORT" : "",
12236 (xio->io_hdr.flags &
12237 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12238 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12240 printf("%s\n", sbuf_data(&sb));
12243 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12244 && (xio->io_hdr.nexus.initid.id ==
12245 io->io_hdr.nexus.initid.id)) {
12247 * If the abort says that the task is untagged, the
12248 * task in the queue must be untagged. Otherwise,
12249 * we just check to see whether the tag numbers
12250 * match. This is because the QLogic firmware
12251 * doesn't pass back the tag type in an abort
12255 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12256 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12257 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12260 * XXX KDM we've got problems with FC, because it
12261 * doesn't send down a tag type with aborts. So we
12262 * can only really go by the tag number...
12263 * This may cause problems with parallel SCSI.
12264 * Need to figure that out!!
12266 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12267 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12269 if ((io->io_hdr.flags &
12270 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12271 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12272 union ctl_ha_msg msg_info;
12274 io->io_hdr.flags |=
12275 CTL_FLAG_SENT_2OTHER_SC;
12276 msg_info.hdr.nexus = io->io_hdr.nexus;
12277 msg_info.task.task_action =
12278 CTL_TASK_ABORT_TASK;
12279 msg_info.task.tag_num =
12280 io->taskio.tag_num;
12281 msg_info.task.tag_type =
12282 io->taskio.tag_type;
12283 msg_info.hdr.msg_type =
12284 CTL_MSG_MANAGE_TASKS;
12285 msg_info.hdr.original_sc = NULL;
12286 msg_info.hdr.serializing_sc = NULL;
12288 printf("Sent Abort to other side\n");
12290 if (CTL_HA_STATUS_SUCCESS !=
12291 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12293 sizeof(msg_info), 0)) {
12297 printf("ctl_abort_task: found I/O to abort\n");
12303 mtx_unlock(&lun->lun_lock);
12307 * This isn't really an error. It's entirely possible for
12308 * the abort and command completion to cross on the wire.
12309 * This is more of an informative/diagnostic error.
12312 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12313 "%d:%d:%d:%d tag %d type %d\n",
12314 io->io_hdr.nexus.initid.id,
12315 io->io_hdr.nexus.targ_port,
12316 io->io_hdr.nexus.targ_target.id,
12317 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12318 io->taskio.tag_type);
12325 ctl_run_task(union ctl_io *io)
12327 struct ctl_softc *ctl_softc = control_softc;
12329 const char *task_desc;
12331 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12333 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12334 ("ctl_run_task: Unextected io_type %d\n",
12335 io->io_hdr.io_type));
12337 task_desc = ctl_scsi_task_string(&io->taskio);
12338 if (task_desc != NULL) {
12340 csevent_log(CSC_CTL | CSC_SHELF_SW |
12342 csevent_LogType_Trace,
12343 csevent_Severity_Information,
12344 csevent_AlertLevel_Green,
12345 csevent_FRU_Firmware,
12346 csevent_FRU_Unknown,
12347 "CTL: received task: %s",task_desc);
12351 csevent_log(CSC_CTL | CSC_SHELF_SW |
12353 csevent_LogType_Trace,
12354 csevent_Severity_Information,
12355 csevent_AlertLevel_Green,
12356 csevent_FRU_Firmware,
12357 csevent_FRU_Unknown,
12358 "CTL: received unknown task "
12360 io->taskio.task_action,
12361 io->taskio.task_action);
12364 switch (io->taskio.task_action) {
12365 case CTL_TASK_ABORT_TASK:
12366 retval = ctl_abort_task(io);
12368 case CTL_TASK_ABORT_TASK_SET:
12369 case CTL_TASK_CLEAR_TASK_SET:
12370 retval = ctl_abort_task_set(io);
12372 case CTL_TASK_CLEAR_ACA:
12374 case CTL_TASK_I_T_NEXUS_RESET:
12375 retval = ctl_i_t_nexus_reset(io);
12377 case CTL_TASK_LUN_RESET: {
12378 struct ctl_lun *lun;
12381 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12382 mtx_lock(&ctl_softc->ctl_lock);
12383 if ((targ_lun < CTL_MAX_LUNS)
12384 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12385 lun = ctl_softc->ctl_luns[targ_lun];
12387 mtx_unlock(&ctl_softc->ctl_lock);
12392 if (!(io->io_hdr.flags &
12393 CTL_FLAG_FROM_OTHER_SC)) {
12394 union ctl_ha_msg msg_info;
12396 io->io_hdr.flags |=
12397 CTL_FLAG_SENT_2OTHER_SC;
12398 msg_info.hdr.msg_type =
12399 CTL_MSG_MANAGE_TASKS;
12400 msg_info.hdr.nexus = io->io_hdr.nexus;
12401 msg_info.task.task_action =
12402 CTL_TASK_LUN_RESET;
12403 msg_info.hdr.original_sc = NULL;
12404 msg_info.hdr.serializing_sc = NULL;
12405 if (CTL_HA_STATUS_SUCCESS !=
12406 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12408 sizeof(msg_info), 0)) {
12412 retval = ctl_lun_reset(lun, io,
12414 mtx_unlock(&ctl_softc->ctl_lock);
12417 case CTL_TASK_TARGET_RESET:
12418 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12420 case CTL_TASK_BUS_RESET:
12421 retval = ctl_bus_reset(ctl_softc, io);
12423 case CTL_TASK_PORT_LOGIN:
12425 case CTL_TASK_PORT_LOGOUT:
12428 printf("ctl_run_task: got unknown task management event %d\n",
12429 io->taskio.task_action);
12433 io->io_hdr.status = CTL_SUCCESS;
12435 io->io_hdr.status = CTL_ERROR;
12440 * For HA operation. Handle commands that come in from the other
12444 ctl_handle_isc(union ctl_io *io)
12447 struct ctl_lun *lun;
12448 struct ctl_softc *ctl_softc;
12451 ctl_softc = control_softc;
12453 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12454 lun = ctl_softc->ctl_luns[targ_lun];
12456 switch (io->io_hdr.msg_type) {
12457 case CTL_MSG_SERIALIZE:
12458 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12460 case CTL_MSG_R2R: {
12461 const struct ctl_cmd_entry *entry;
12464 * This is only used in SER_ONLY mode.
12467 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12468 mtx_lock(&lun->lun_lock);
12469 if (ctl_scsiio_lun_check(ctl_softc, lun,
12470 entry, (struct ctl_scsiio *)io) != 0) {
12471 mtx_unlock(&lun->lun_lock);
12475 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12476 mtx_unlock(&lun->lun_lock);
12477 ctl_enqueue_rtr(io);
12480 case CTL_MSG_FINISH_IO:
12481 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12486 mtx_lock(&lun->lun_lock);
12487 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12489 ctl_check_blocked(lun);
12490 mtx_unlock(&lun->lun_lock);
12493 case CTL_MSG_PERS_ACTION:
12494 ctl_hndl_per_res_out_on_other_sc(
12495 (union ctl_ha_msg *)&io->presio.pr_msg);
12498 case CTL_MSG_BAD_JUJU:
12502 case CTL_MSG_DATAMOVE:
12503 /* Only used in XFER mode */
12505 ctl_datamove_remote(io);
12507 case CTL_MSG_DATAMOVE_DONE:
12508 /* Only used in XFER mode */
12510 io->scsiio.be_move_done(io);
12514 printf("%s: Invalid message type %d\n",
12515 __func__, io->io_hdr.msg_type);
12525 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12526 * there is no match.
12528 static ctl_lun_error_pattern
12529 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12531 const struct ctl_cmd_entry *entry;
12532 ctl_lun_error_pattern filtered_pattern, pattern;
12534 pattern = desc->error_pattern;
12537 * XXX KDM we need more data passed into this function to match a
12538 * custom pattern, and we actually need to implement custom pattern
12541 if (pattern & CTL_LUN_PAT_CMD)
12542 return (CTL_LUN_PAT_CMD);
12544 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12545 return (CTL_LUN_PAT_ANY);
12547 entry = ctl_get_cmd_entry(ctsio, NULL);
12549 filtered_pattern = entry->pattern & pattern;
12552 * If the user requested specific flags in the pattern (e.g.
12553 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12556 * If the user did not specify any flags, it doesn't matter whether
12557 * or not the command supports the flags.
12559 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12560 (pattern & ~CTL_LUN_PAT_MASK))
12561 return (CTL_LUN_PAT_NONE);
12564 * If the user asked for a range check, see if the requested LBA
12565 * range overlaps with this command's LBA range.
12567 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12573 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12575 return (CTL_LUN_PAT_NONE);
12577 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12578 desc->lba_range.len);
12580 * A "pass" means that the LBA ranges don't overlap, so
12581 * this doesn't match the user's range criteria.
12583 if (action == CTL_ACTION_PASS)
12584 return (CTL_LUN_PAT_NONE);
12587 return (filtered_pattern);
12591 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12593 struct ctl_error_desc *desc, *desc2;
12595 mtx_assert(&lun->lun_lock, MA_OWNED);
12597 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12598 ctl_lun_error_pattern pattern;
12600 * Check to see whether this particular command matches
12601 * the pattern in the descriptor.
12603 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12604 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12607 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12608 case CTL_LUN_INJ_ABORTED:
12609 ctl_set_aborted(&io->scsiio);
12611 case CTL_LUN_INJ_MEDIUM_ERR:
12612 ctl_set_medium_error(&io->scsiio);
12614 case CTL_LUN_INJ_UA:
12615 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12617 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12619 case CTL_LUN_INJ_CUSTOM:
12621 * We're assuming the user knows what he is doing.
12622 * Just copy the sense information without doing
12625 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12626 ctl_min(sizeof(desc->custom_sense),
12627 sizeof(io->scsiio.sense_data)));
12628 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12629 io->scsiio.sense_len = SSD_FULL_SIZE;
12630 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12632 case CTL_LUN_INJ_NONE:
12635 * If this is an error injection type we don't know
12636 * about, clear the continuous flag (if it is set)
12637 * so it will get deleted below.
12639 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12643 * By default, each error injection action is a one-shot
12645 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12648 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12654 #ifdef CTL_IO_DELAY
12656 ctl_datamove_timer_wakeup(void *arg)
12660 io = (union ctl_io *)arg;
12664 #endif /* CTL_IO_DELAY */
12667 ctl_datamove(union ctl_io *io)
12669 void (*fe_datamove)(union ctl_io *io);
12671 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12673 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12676 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12681 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12682 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12684 sbuf_cat(&sb, path_str);
12685 switch (io->io_hdr.io_type) {
12687 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12688 sbuf_printf(&sb, "\n");
12689 sbuf_cat(&sb, path_str);
12690 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12691 io->scsiio.tag_num, io->scsiio.tag_type);
12694 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12695 "Tag Type: %d\n", io->taskio.task_action,
12696 io->taskio.tag_num, io->taskio.tag_type);
12699 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12700 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12703 sbuf_cat(&sb, path_str);
12704 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12705 (intmax_t)time_uptime - io->io_hdr.start_time);
12707 printf("%s", sbuf_data(&sb));
12709 #endif /* CTL_TIME_IO */
12711 #ifdef CTL_IO_DELAY
12712 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12713 struct ctl_lun *lun;
12715 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12717 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12719 struct ctl_lun *lun;
12721 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12723 && (lun->delay_info.datamove_delay > 0)) {
12724 struct callout *callout;
12726 callout = (struct callout *)&io->io_hdr.timer_bytes;
12727 callout_init(callout, /*mpsafe*/ 1);
12728 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12729 callout_reset(callout,
12730 lun->delay_info.datamove_delay * hz,
12731 ctl_datamove_timer_wakeup, io);
12732 if (lun->delay_info.datamove_type ==
12733 CTL_DELAY_TYPE_ONESHOT)
12734 lun->delay_info.datamove_delay = 0;
12741 * This command has been aborted. Set the port status, so we fail
12744 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12745 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12746 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12747 io->io_hdr.nexus.targ_port,
12748 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12749 io->io_hdr.nexus.targ_lun);
12750 io->io_hdr.port_status = 31337;
12752 * Note that the backend, in this case, will get the
12753 * callback in its context. In other cases it may get
12754 * called in the frontend's interrupt thread context.
12756 io->scsiio.be_move_done(io);
12761 * If we're in XFER mode and this I/O is from the other shelf
12762 * controller, we need to send the DMA to the other side to
12763 * actually transfer the data to/from the host. In serialize only
12764 * mode the transfer happens below CTL and ctl_datamove() is only
12765 * called on the machine that originally received the I/O.
12767 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12768 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12769 union ctl_ha_msg msg;
12770 uint32_t sg_entries_sent;
12774 memset(&msg, 0, sizeof(msg));
12775 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12776 msg.hdr.original_sc = io->io_hdr.original_sc;
12777 msg.hdr.serializing_sc = io;
12778 msg.hdr.nexus = io->io_hdr.nexus;
12779 msg.dt.flags = io->io_hdr.flags;
12781 * We convert everything into a S/G list here. We can't
12782 * pass by reference, only by value between controllers.
12783 * So we can't pass a pointer to the S/G list, only as many
12784 * S/G entries as we can fit in here. If it's possible for
12785 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12786 * then we need to break this up into multiple transfers.
12788 if (io->scsiio.kern_sg_entries == 0) {
12789 msg.dt.kern_sg_entries = 1;
12791 * If this is in cached memory, flush the cache
12792 * before we send the DMA request to the other
12793 * controller. We want to do this in either the
12794 * read or the write case. The read case is
12795 * straightforward. In the write case, we want to
12796 * make sure nothing is in the local cache that
12797 * could overwrite the DMAed data.
12799 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12801 * XXX KDM use bus_dmamap_sync() here.
12806 * Convert to a physical address if this is a
12809 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12810 msg.dt.sg_list[0].addr =
12811 io->scsiio.kern_data_ptr;
12814 * XXX KDM use busdma here!
12817 msg.dt.sg_list[0].addr = (void *)
12818 vtophys(io->scsiio.kern_data_ptr);
12822 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12825 struct ctl_sg_entry *sgl;
12828 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12829 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12830 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12832 * XXX KDM use bus_dmamap_sync() here.
12837 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12838 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12839 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12840 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12841 msg.dt.sg_sequence = 0;
12844 * Loop until we've sent all of the S/G entries. On the
12845 * other end, we'll recompose these S/G entries into one
12846 * contiguous list before passing it to the
12848 for (sg_entries_sent = 0; sg_entries_sent <
12849 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12850 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12851 sizeof(msg.dt.sg_list[0])),
12852 msg.dt.kern_sg_entries - sg_entries_sent);
12854 if (do_sg_copy != 0) {
12855 struct ctl_sg_entry *sgl;
12858 sgl = (struct ctl_sg_entry *)
12859 io->scsiio.kern_data_ptr;
12861 * If this is in cached memory, flush the cache
12862 * before we send the DMA request to the other
12863 * controller. We want to do this in either
12864 * the * read or the write case. The read
12865 * case is straightforward. In the write
12866 * case, we want to make sure nothing is
12867 * in the local cache that could overwrite
12871 for (i = sg_entries_sent, j = 0;
12872 i < msg.dt.cur_sg_entries; i++, j++) {
12873 if ((io->io_hdr.flags &
12874 CTL_FLAG_NO_DATASYNC) == 0) {
12876 * XXX KDM use bus_dmamap_sync()
12879 if ((io->io_hdr.flags &
12880 CTL_FLAG_BUS_ADDR) == 0) {
12882 * XXX KDM use busdma.
12885 msg.dt.sg_list[j].addr =(void *)
12886 vtophys(sgl[i].addr);
12889 msg.dt.sg_list[j].addr =
12892 msg.dt.sg_list[j].len = sgl[i].len;
12896 sg_entries_sent += msg.dt.cur_sg_entries;
12897 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12898 msg.dt.sg_last = 1;
12900 msg.dt.sg_last = 0;
12903 * XXX KDM drop and reacquire the lock here?
12905 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12906 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12908 * XXX do something here.
12912 msg.dt.sent_sg_entries = sg_entries_sent;
12914 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12915 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12916 ctl_failover_io(io, /*have_lock*/ 0);
12921 * Lookup the fe_datamove() function for this particular
12925 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12932 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12934 union ctl_ha_msg msg;
12937 memset(&msg, 0, sizeof(msg));
12939 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12940 msg.hdr.original_sc = io;
12941 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12942 msg.hdr.nexus = io->io_hdr.nexus;
12943 msg.hdr.status = io->io_hdr.status;
12944 msg.scsi.tag_num = io->scsiio.tag_num;
12945 msg.scsi.tag_type = io->scsiio.tag_type;
12946 msg.scsi.scsi_status = io->scsiio.scsi_status;
12947 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12948 sizeof(io->scsiio.sense_data));
12949 msg.scsi.sense_len = io->scsiio.sense_len;
12950 msg.scsi.sense_residual = io->scsiio.sense_residual;
12951 msg.scsi.fetd_status = io->io_hdr.port_status;
12952 msg.scsi.residual = io->scsiio.residual;
12953 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12955 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12956 ctl_failover_io(io, /*have_lock*/ have_lock);
12960 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12961 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12962 /* XXX do something if this fails */
12968 * The DMA to the remote side is done, now we need to tell the other side
12969 * we're done so it can continue with its data movement.
12972 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12978 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12979 printf("%s: ISC DMA write failed with error %d", __func__,
12981 ctl_set_internal_failure(&io->scsiio,
12983 /*retry_count*/ rq->ret);
12986 ctl_dt_req_free(rq);
12989 * In this case, we had to malloc the memory locally. Free it.
12991 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12993 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12994 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12997 * The data is in local and remote memory, so now we need to send
12998 * status (good or back) back to the other side.
13000 ctl_send_datamove_done(io, /*have_lock*/ 0);
13004 * We've moved the data from the host/controller into local memory. Now we
13005 * need to push it over to the remote controller's memory.
13008 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
13014 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13015 ctl_datamove_remote_write_cb);
13021 ctl_datamove_remote_write(union ctl_io *io)
13024 void (*fe_datamove)(union ctl_io *io);
13027 * - Get the data from the host/HBA into local memory.
13028 * - DMA memory from the local controller to the remote controller.
13029 * - Send status back to the remote controller.
13032 retval = ctl_datamove_remote_sgl_setup(io);
13036 /* Switch the pointer over so the FETD knows what to do */
13037 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13040 * Use a custom move done callback, since we need to send completion
13041 * back to the other controller, not to the backend on this side.
13043 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13045 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13054 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13063 * In this case, we had to malloc the memory locally. Free it.
13065 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13067 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13068 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13072 scsi_path_string(io, path_str, sizeof(path_str));
13073 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13074 sbuf_cat(&sb, path_str);
13075 scsi_command_string(&io->scsiio, NULL, &sb);
13076 sbuf_printf(&sb, "\n");
13077 sbuf_cat(&sb, path_str);
13078 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13079 io->scsiio.tag_num, io->scsiio.tag_type);
13080 sbuf_cat(&sb, path_str);
13081 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13082 io->io_hdr.flags, io->io_hdr.status);
13084 printk("%s", sbuf_data(&sb));
13089 * The read is done, now we need to send status (good or bad) back
13090 * to the other side.
13092 ctl_send_datamove_done(io, /*have_lock*/ 0);
13098 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13101 void (*fe_datamove)(union ctl_io *io);
13105 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13106 printf("%s: ISC DMA read failed with error %d", __func__,
13108 ctl_set_internal_failure(&io->scsiio,
13110 /*retry_count*/ rq->ret);
13113 ctl_dt_req_free(rq);
13115 /* Switch the pointer over so the FETD knows what to do */
13116 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13119 * Use a custom move done callback, since we need to send completion
13120 * back to the other controller, not to the backend on this side.
13122 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13124 /* XXX KDM add checks like the ones in ctl_datamove? */
13126 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13132 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13134 struct ctl_sg_entry *local_sglist, *remote_sglist;
13135 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13136 struct ctl_softc *softc;
13141 softc = control_softc;
13143 local_sglist = io->io_hdr.local_sglist;
13144 local_dma_sglist = io->io_hdr.local_dma_sglist;
13145 remote_sglist = io->io_hdr.remote_sglist;
13146 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13148 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13149 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13150 local_sglist[i].len = remote_sglist[i].len;
13153 * XXX Detect the situation where the RS-level I/O
13154 * redirector on the other side has already read the
13155 * data off of the AOR RS on this side, and
13156 * transferred it to remote (mirror) memory on the
13157 * other side. Since we already have the data in
13158 * memory here, we just need to use it.
13160 * XXX KDM this can probably be removed once we
13161 * get the cache device code in and take the
13162 * current AOR implementation out.
13165 if ((remote_sglist[i].addr >=
13166 (void *)vtophys(softc->mirr->addr))
13167 && (remote_sglist[i].addr <
13168 ((void *)vtophys(softc->mirr->addr) +
13169 CacheMirrorOffset))) {
13170 local_sglist[i].addr = remote_sglist[i].addr -
13172 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13174 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13176 local_sglist[i].addr = remote_sglist[i].addr +
13181 printf("%s: local %p, remote %p, len %d\n",
13182 __func__, local_sglist[i].addr,
13183 remote_sglist[i].addr, local_sglist[i].len);
13187 uint32_t len_to_go;
13190 * In this case, we don't have automatically allocated
13191 * memory for this I/O on this controller. This typically
13192 * happens with internal CTL I/O -- e.g. inquiry, mode
13193 * sense, etc. Anything coming from RAIDCore will have
13194 * a mirror area available.
13196 len_to_go = io->scsiio.kern_data_len;
13199 * Clear the no datasync flag, we have to use malloced
13202 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13205 * The difficult thing here is that the size of the various
13206 * S/G segments may be different than the size from the
13207 * remote controller. That'll make it harder when DMAing
13208 * the data back to the other side.
13210 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13211 sizeof(io->io_hdr.remote_sglist[0])) &&
13212 (len_to_go > 0); i++) {
13213 local_sglist[i].len = ctl_min(len_to_go, 131072);
13214 CTL_SIZE_8B(local_dma_sglist[i].len,
13215 local_sglist[i].len);
13216 local_sglist[i].addr =
13217 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13219 local_dma_sglist[i].addr = local_sglist[i].addr;
13221 if (local_sglist[i].addr == NULL) {
13224 printf("malloc failed for %zd bytes!",
13225 local_dma_sglist[i].len);
13226 for (j = 0; j < i; j++) {
13227 free(local_sglist[j].addr, M_CTL);
13229 ctl_set_internal_failure(&io->scsiio,
13231 /*retry_count*/ 4857);
13233 goto bailout_error;
13236 /* XXX KDM do we need a sync here? */
13238 len_to_go -= local_sglist[i].len;
13241 * Reset the number of S/G entries accordingly. The
13242 * original number of S/G entries is available in
13245 io->scsiio.kern_sg_entries = i;
13248 printf("%s: kern_sg_entries = %d\n", __func__,
13249 io->scsiio.kern_sg_entries);
13250 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13251 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13252 local_sglist[i].addr, local_sglist[i].len,
13253 local_dma_sglist[i].len);
13262 ctl_send_datamove_done(io, /*have_lock*/ 0);
13268 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13269 ctl_ha_dt_cb callback)
13271 struct ctl_ha_dt_req *rq;
13272 struct ctl_sg_entry *remote_sglist, *local_sglist;
13273 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13274 uint32_t local_used, remote_used, total_used;
13280 rq = ctl_dt_req_alloc();
13283 * If we failed to allocate the request, and if the DMA didn't fail
13284 * anyway, set busy status. This is just a resource allocation
13288 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13289 ctl_set_busy(&io->scsiio);
13291 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13294 ctl_dt_req_free(rq);
13297 * The data move failed. We need to return status back
13298 * to the other controller. No point in trying to DMA
13299 * data to the remote controller.
13302 ctl_send_datamove_done(io, /*have_lock*/ 0);
13309 local_sglist = io->io_hdr.local_sglist;
13310 local_dma_sglist = io->io_hdr.local_dma_sglist;
13311 remote_sglist = io->io_hdr.remote_sglist;
13312 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13317 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13318 rq->ret = CTL_HA_STATUS_SUCCESS;
13325 * Pull/push the data over the wire from/to the other controller.
13326 * This takes into account the possibility that the local and
13327 * remote sglists may not be identical in terms of the size of
13328 * the elements and the number of elements.
13330 * One fundamental assumption here is that the length allocated for
13331 * both the local and remote sglists is identical. Otherwise, we've
13332 * essentially got a coding error of some sort.
13334 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13336 uint32_t cur_len, dma_length;
13339 rq->id = CTL_HA_DATA_CTL;
13340 rq->command = command;
13344 * Both pointers should be aligned. But it is possible
13345 * that the allocation length is not. They should both
13346 * also have enough slack left over at the end, though,
13347 * to round up to the next 8 byte boundary.
13349 cur_len = ctl_min(local_sglist[i].len - local_used,
13350 remote_sglist[j].len - remote_used);
13353 * In this case, we have a size issue and need to decrease
13354 * the size, except in the case where we actually have less
13355 * than 8 bytes left. In that case, we need to increase
13356 * the DMA length to get the last bit.
13358 if ((cur_len & 0x7) != 0) {
13359 if (cur_len > 0x7) {
13360 cur_len = cur_len - (cur_len & 0x7);
13361 dma_length = cur_len;
13363 CTL_SIZE_8B(dma_length, cur_len);
13367 dma_length = cur_len;
13370 * If we had to allocate memory for this I/O, instead of using
13371 * the non-cached mirror memory, we'll need to flush the cache
13372 * before trying to DMA to the other controller.
13374 * We could end up doing this multiple times for the same
13375 * segment if we have a larger local segment than remote
13376 * segment. That shouldn't be an issue.
13378 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13380 * XXX KDM use bus_dmamap_sync() here.
13384 rq->size = dma_length;
13386 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13387 tmp_ptr += local_used;
13389 /* Use physical addresses when talking to ISC hardware */
13390 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13391 /* XXX KDM use busdma */
13393 rq->local = vtophys(tmp_ptr);
13396 rq->local = tmp_ptr;
13398 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13399 tmp_ptr += remote_used;
13400 rq->remote = tmp_ptr;
13402 rq->callback = NULL;
13404 local_used += cur_len;
13405 if (local_used >= local_sglist[i].len) {
13410 remote_used += cur_len;
13411 if (remote_used >= remote_sglist[j].len) {
13415 total_used += cur_len;
13417 if (total_used >= io->scsiio.kern_data_len)
13418 rq->callback = callback;
13420 if ((rq->size & 0x7) != 0) {
13421 printf("%s: warning: size %d is not on 8b boundary\n",
13422 __func__, rq->size);
13424 if (((uintptr_t)rq->local & 0x7) != 0) {
13425 printf("%s: warning: local %p not on 8b boundary\n",
13426 __func__, rq->local);
13428 if (((uintptr_t)rq->remote & 0x7) != 0) {
13429 printf("%s: warning: remote %p not on 8b boundary\n",
13430 __func__, rq->local);
13433 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13434 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13435 rq->local, rq->remote, rq->size);
13438 isc_ret = ctl_dt_single(rq);
13439 if (isc_ret == CTL_HA_STATUS_WAIT)
13442 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13443 rq->ret = CTL_HA_STATUS_SUCCESS;
13457 ctl_datamove_remote_read(union ctl_io *io)
13463 * This will send an error to the other controller in the case of a
13466 retval = ctl_datamove_remote_sgl_setup(io);
13470 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13471 ctl_datamove_remote_read_cb);
13473 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13475 * Make sure we free memory if there was an error.. The
13476 * ctl_datamove_remote_xfer() function will send the
13477 * datamove done message, or call the callback with an
13478 * error if there is a problem.
13480 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13481 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13488 * Process a datamove request from the other controller. This is used for
13489 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13490 * first. Once that is complete, the data gets DMAed into the remote
13491 * controller's memory. For reads, we DMA from the remote controller's
13492 * memory into our memory first, and then move it out to the FETD.
13495 ctl_datamove_remote(union ctl_io *io)
13497 struct ctl_softc *softc;
13499 softc = control_softc;
13501 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13504 * Note that we look for an aborted I/O here, but don't do some of
13505 * the other checks that ctl_datamove() normally does.
13506 * We don't need to run the datamove delay code, since that should
13507 * have been done if need be on the other controller.
13509 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13510 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13511 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13512 io->io_hdr.nexus.targ_port,
13513 io->io_hdr.nexus.targ_target.id,
13514 io->io_hdr.nexus.targ_lun);
13515 io->io_hdr.port_status = 31338;
13516 ctl_send_datamove_done(io, /*have_lock*/ 0);
13520 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13521 ctl_datamove_remote_write(io);
13522 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13523 ctl_datamove_remote_read(io);
13525 union ctl_ha_msg msg;
13526 struct scsi_sense_data *sense;
13530 memset(&msg, 0, sizeof(msg));
13532 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13533 msg.hdr.status = CTL_SCSI_ERROR;
13534 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13536 retry_count = 4243;
13538 sense = &msg.scsi.sense_data;
13539 sks[0] = SSD_SCS_VALID;
13540 sks[1] = (retry_count >> 8) & 0xff;
13541 sks[2] = retry_count & 0xff;
13543 /* "Internal target failure" */
13544 scsi_set_sense_data(sense,
13545 /*sense_format*/ SSD_TYPE_NONE,
13546 /*current_error*/ 1,
13547 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13550 /*type*/ SSD_ELEM_SKS,
13551 /*size*/ sizeof(sks),
13555 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13556 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13557 ctl_failover_io(io, /*have_lock*/ 1);
13561 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13562 CTL_HA_STATUS_SUCCESS) {
13563 /* XXX KDM what to do if this fails? */
13571 ctl_process_done(union ctl_io *io)
13573 struct ctl_lun *lun;
13574 struct ctl_softc *ctl_softc;
13575 void (*fe_done)(union ctl_io *io);
13576 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13578 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13581 control_softc->ctl_ports[targ_port]->fe_done;
13584 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13589 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13590 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13592 sbuf_cat(&sb, path_str);
13593 switch (io->io_hdr.io_type) {
13595 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13596 sbuf_printf(&sb, "\n");
13597 sbuf_cat(&sb, path_str);
13598 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13599 io->scsiio.tag_num, io->scsiio.tag_type);
13602 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13603 "Tag Type: %d\n", io->taskio.task_action,
13604 io->taskio.tag_num, io->taskio.tag_type);
13607 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13608 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13611 sbuf_cat(&sb, path_str);
13612 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13613 (intmax_t)time_uptime - io->io_hdr.start_time);
13615 printf("%s", sbuf_data(&sb));
13617 #endif /* CTL_TIME_IO */
13619 switch (io->io_hdr.io_type) {
13623 if (bootverbose || verbose > 0)
13624 ctl_io_error_print(io, NULL);
13625 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13629 return (CTL_RETVAL_COMPLETE);
13632 printf("ctl_process_done: invalid io type %d\n",
13633 io->io_hdr.io_type);
13634 panic("ctl_process_done: invalid io type %d\n",
13635 io->io_hdr.io_type);
13636 break; /* NOTREACHED */
13639 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13641 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13642 io->io_hdr.nexus.targ_mapped_lun));
13646 ctl_softc = lun->ctl_softc;
13648 mtx_lock(&lun->lun_lock);
13651 * Check to see if we have any errors to inject here. We only
13652 * inject errors for commands that don't already have errors set.
13654 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13655 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13656 ctl_inject_error(lun, io);
13659 * XXX KDM how do we treat commands that aren't completed
13662 * XXX KDM should we also track I/O latency?
13664 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13665 io->io_hdr.io_type == CTL_IO_SCSI) {
13667 struct bintime cur_bt;
13671 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13673 type = CTL_STATS_READ;
13674 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13676 type = CTL_STATS_WRITE;
13678 type = CTL_STATS_NO_IO;
13680 lun->stats.ports[targ_port].bytes[type] +=
13681 io->scsiio.kern_total_len;
13682 lun->stats.ports[targ_port].operations[type]++;
13684 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13685 &io->io_hdr.dma_bt);
13686 lun->stats.ports[targ_port].num_dmas[type] +=
13687 io->io_hdr.num_dmas;
13688 getbintime(&cur_bt);
13689 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13690 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13695 * Remove this from the OOA queue.
13697 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13700 * Run through the blocked queue on this LUN and see if anything
13701 * has become unblocked, now that this transaction is done.
13703 ctl_check_blocked(lun);
13706 * If the LUN has been invalidated, free it if there is nothing
13707 * left on its OOA queue.
13709 if ((lun->flags & CTL_LUN_INVALID)
13710 && TAILQ_EMPTY(&lun->ooa_queue)) {
13711 mtx_unlock(&lun->lun_lock);
13712 mtx_lock(&ctl_softc->ctl_lock);
13714 mtx_unlock(&ctl_softc->ctl_lock);
13716 mtx_unlock(&lun->lun_lock);
13719 * If this command has been aborted, make sure we set the status
13720 * properly. The FETD is responsible for freeing the I/O and doing
13721 * whatever it needs to do to clean up its state.
13723 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13724 ctl_set_task_aborted(&io->scsiio);
13727 * We print out status for every task management command. For SCSI
13728 * commands, we filter out any unit attention errors; they happen
13729 * on every boot, and would clutter up the log. Note: task
13730 * management commands aren't printed here, they are printed above,
13731 * since they should never even make it down here.
13733 switch (io->io_hdr.io_type) {
13734 case CTL_IO_SCSI: {
13735 int error_code, sense_key, asc, ascq;
13739 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13740 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13742 * Since this is just for printing, no need to
13743 * show errors here.
13745 scsi_extract_sense_len(&io->scsiio.sense_data,
13746 io->scsiio.sense_len,
13751 /*show_errors*/ 0);
13754 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13755 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13756 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13757 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13759 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13760 ctl_softc->skipped_prints++;
13762 uint32_t skipped_prints;
13764 skipped_prints = ctl_softc->skipped_prints;
13766 ctl_softc->skipped_prints = 0;
13767 ctl_softc->last_print_jiffies = time_uptime;
13769 if (skipped_prints > 0) {
13771 csevent_log(CSC_CTL | CSC_SHELF_SW |
13773 csevent_LogType_Trace,
13774 csevent_Severity_Information,
13775 csevent_AlertLevel_Green,
13776 csevent_FRU_Firmware,
13777 csevent_FRU_Unknown,
13778 "High CTL error volume, %d prints "
13779 "skipped", skipped_prints);
13782 if (bootverbose || verbose > 0)
13783 ctl_io_error_print(io, NULL);
13789 if (bootverbose || verbose > 0)
13790 ctl_io_error_print(io, NULL);
13797 * Tell the FETD or the other shelf controller we're done with this
13798 * command. Note that only SCSI commands get to this point. Task
13799 * management commands are completed above.
13801 * We only send status to the other controller if we're in XFER
13802 * mode. In SER_ONLY mode, the I/O is done on the controller that
13803 * received the I/O (from CTL's perspective), and so the status is
13806 * XXX KDM if we hold the lock here, we could cause a deadlock
13807 * if the frontend comes back in in this context to queue
13810 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13811 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13812 union ctl_ha_msg msg;
13814 memset(&msg, 0, sizeof(msg));
13815 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13816 msg.hdr.original_sc = io->io_hdr.original_sc;
13817 msg.hdr.nexus = io->io_hdr.nexus;
13818 msg.hdr.status = io->io_hdr.status;
13819 msg.scsi.scsi_status = io->scsiio.scsi_status;
13820 msg.scsi.tag_num = io->scsiio.tag_num;
13821 msg.scsi.tag_type = io->scsiio.tag_type;
13822 msg.scsi.sense_len = io->scsiio.sense_len;
13823 msg.scsi.sense_residual = io->scsiio.sense_residual;
13824 msg.scsi.residual = io->scsiio.residual;
13825 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13826 sizeof(io->scsiio.sense_data));
13828 * We copy this whether or not this is an I/O-related
13829 * command. Otherwise, we'd have to go and check to see
13830 * whether it's a read/write command, and it really isn't
13833 memcpy(&msg.scsi.lbalen,
13834 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13835 sizeof(msg.scsi.lbalen));
13837 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13838 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13839 /* XXX do something here */
13848 return (CTL_RETVAL_COMPLETE);
13853 * Front end should call this if it doesn't do autosense. When the request
13854 * sense comes back in from the initiator, we'll dequeue this and send it.
13857 ctl_queue_sense(union ctl_io *io)
13859 struct ctl_lun *lun;
13860 struct ctl_softc *ctl_softc;
13861 uint32_t initidx, targ_lun;
13863 ctl_softc = control_softc;
13865 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13868 * LUN lookup will likely move to the ctl_work_thread() once we
13869 * have our new queueing infrastructure (that doesn't put things on
13870 * a per-LUN queue initially). That is so that we can handle
13871 * things like an INQUIRY to a LUN that we don't have enabled. We
13872 * can't deal with that right now.
13874 mtx_lock(&ctl_softc->ctl_lock);
13877 * If we don't have a LUN for this, just toss the sense
13880 targ_lun = io->io_hdr.nexus.targ_lun;
13881 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13882 if ((targ_lun < CTL_MAX_LUNS)
13883 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13884 lun = ctl_softc->ctl_luns[targ_lun];
13888 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13890 mtx_lock(&lun->lun_lock);
13892 * Already have CA set for this LUN...toss the sense information.
13894 if (ctl_is_set(lun->have_ca, initidx)) {
13895 mtx_unlock(&lun->lun_lock);
13899 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13900 ctl_min(sizeof(lun->pending_sense[initidx]),
13901 sizeof(io->scsiio.sense_data)));
13902 ctl_set_mask(lun->have_ca, initidx);
13903 mtx_unlock(&lun->lun_lock);
13906 mtx_unlock(&ctl_softc->ctl_lock);
13910 return (CTL_RETVAL_COMPLETE);
13915 * Primary command inlet from frontend ports. All SCSI and task I/O
13916 * requests must go through this function.
13919 ctl_queue(union ctl_io *io)
13921 struct ctl_softc *ctl_softc;
13923 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13925 ctl_softc = control_softc;
13928 io->io_hdr.start_time = time_uptime;
13929 getbintime(&io->io_hdr.start_bt);
13930 #endif /* CTL_TIME_IO */
13932 /* Map FE-specific LUN ID into global one. */
13933 io->io_hdr.nexus.targ_mapped_lun =
13934 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13936 switch (io->io_hdr.io_type) {
13939 ctl_enqueue_incoming(io);
13942 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13946 return (CTL_RETVAL_COMPLETE);
13949 #ifdef CTL_IO_DELAY
13951 ctl_done_timer_wakeup(void *arg)
13955 io = (union ctl_io *)arg;
13958 #endif /* CTL_IO_DELAY */
13961 ctl_done(union ctl_io *io)
13963 struct ctl_softc *ctl_softc;
13965 ctl_softc = control_softc;
13968 * Enable this to catch duplicate completion issues.
13971 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13972 printf("%s: type %d msg %d cdb %x iptl: "
13973 "%d:%d:%d:%d tag 0x%04x "
13974 "flag %#x status %x\n",
13976 io->io_hdr.io_type,
13977 io->io_hdr.msg_type,
13979 io->io_hdr.nexus.initid.id,
13980 io->io_hdr.nexus.targ_port,
13981 io->io_hdr.nexus.targ_target.id,
13982 io->io_hdr.nexus.targ_lun,
13983 (io->io_hdr.io_type ==
13985 io->taskio.tag_num :
13986 io->scsiio.tag_num,
13988 io->io_hdr.status);
13990 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13994 * This is an internal copy of an I/O, and should not go through
13995 * the normal done processing logic.
13997 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14001 * We need to send a msg to the serializing shelf to finish the IO
14002 * as well. We don't send a finish message to the other shelf if
14003 * this is a task management command. Task management commands
14004 * aren't serialized in the OOA queue, but rather just executed on
14005 * both shelf controllers for commands that originated on that
14008 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
14009 && (io->io_hdr.io_type != CTL_IO_TASK)) {
14010 union ctl_ha_msg msg_io;
14012 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
14013 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
14014 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
14015 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
14017 /* continue on to finish IO */
14019 #ifdef CTL_IO_DELAY
14020 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14021 struct ctl_lun *lun;
14023 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14025 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14027 struct ctl_lun *lun;
14029 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14032 && (lun->delay_info.done_delay > 0)) {
14033 struct callout *callout;
14035 callout = (struct callout *)&io->io_hdr.timer_bytes;
14036 callout_init(callout, /*mpsafe*/ 1);
14037 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14038 callout_reset(callout,
14039 lun->delay_info.done_delay * hz,
14040 ctl_done_timer_wakeup, io);
14041 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14042 lun->delay_info.done_delay = 0;
14046 #endif /* CTL_IO_DELAY */
14048 ctl_enqueue_done(io);
14052 ctl_isc(struct ctl_scsiio *ctsio)
14054 struct ctl_lun *lun;
14057 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14059 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14061 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14063 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14070 ctl_work_thread(void *arg)
14072 struct ctl_thread *thr = (struct ctl_thread *)arg;
14073 struct ctl_softc *softc = thr->ctl_softc;
14077 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14083 * We handle the queues in this order:
14085 * - done queue (to free up resources, unblock other commands)
14089 * If those queues are empty, we break out of the loop and
14092 mtx_lock(&thr->queue_lock);
14093 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14095 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14096 mtx_unlock(&thr->queue_lock);
14097 ctl_handle_isc(io);
14100 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14102 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14103 /* clear any blocked commands, call fe_done */
14104 mtx_unlock(&thr->queue_lock);
14105 retval = ctl_process_done(io);
14108 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14110 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14111 mtx_unlock(&thr->queue_lock);
14112 if (io->io_hdr.io_type == CTL_IO_TASK)
14115 ctl_scsiio_precheck(softc, &io->scsiio);
14118 if (!ctl_pause_rtr) {
14119 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14121 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14122 mtx_unlock(&thr->queue_lock);
14123 retval = ctl_scsiio(&io->scsiio);
14124 if (retval != CTL_RETVAL_COMPLETE)
14125 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14130 /* Sleep until we have something to do. */
14131 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14136 ctl_lun_thread(void *arg)
14138 struct ctl_softc *softc = (struct ctl_softc *)arg;
14139 struct ctl_be_lun *be_lun;
14142 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14146 mtx_lock(&softc->ctl_lock);
14147 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14148 if (be_lun != NULL) {
14149 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14150 mtx_unlock(&softc->ctl_lock);
14151 ctl_create_lun(be_lun);
14155 /* Sleep until we have something to do. */
14156 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14157 PDROP | PRIBIO, "-", 0);
14162 ctl_enqueue_incoming(union ctl_io *io)
14164 struct ctl_softc *softc = control_softc;
14165 struct ctl_thread *thr;
14168 idx = (io->io_hdr.nexus.targ_port * 127 +
14169 io->io_hdr.nexus.initid.id) % worker_threads;
14170 thr = &softc->threads[idx];
14171 mtx_lock(&thr->queue_lock);
14172 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14173 mtx_unlock(&thr->queue_lock);
14178 ctl_enqueue_rtr(union ctl_io *io)
14180 struct ctl_softc *softc = control_softc;
14181 struct ctl_thread *thr;
14183 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14184 mtx_lock(&thr->queue_lock);
14185 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14186 mtx_unlock(&thr->queue_lock);
14191 ctl_enqueue_done(union ctl_io *io)
14193 struct ctl_softc *softc = control_softc;
14194 struct ctl_thread *thr;
14196 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14197 mtx_lock(&thr->queue_lock);
14198 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14199 mtx_unlock(&thr->queue_lock);
14204 ctl_enqueue_isc(union ctl_io *io)
14206 struct ctl_softc *softc = control_softc;
14207 struct ctl_thread *thr;
14209 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14210 mtx_lock(&thr->queue_lock);
14211 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14212 mtx_unlock(&thr->queue_lock);
14216 /* Initialization and failover */
14219 ctl_init_isc_msg(void)
14221 printf("CTL: Still calling this thing\n");
14226 * Initializes component into configuration defined by bootMode
14228 * returns hasc_Status:
14230 * ERROR - fatal error
14232 static ctl_ha_comp_status
14233 ctl_isc_init(struct ctl_ha_component *c)
14235 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14242 * Starts component in state requested. If component starts successfully,
14243 * it must set its own state to the requestrd state
14244 * When requested state is HASC_STATE_HA, the component may refine it
14245 * by adding _SLAVE or _MASTER flags.
14246 * Currently allowed state transitions are:
14247 * UNKNOWN->HA - initial startup
14248 * UNKNOWN->SINGLE - initial startup when no parter detected
14249 * HA->SINGLE - failover
14250 * returns ctl_ha_comp_status:
14251 * OK - component successfully started in requested state
14252 * FAILED - could not start the requested state, failover may
14254 * ERROR - fatal error detected, no future startup possible
14256 static ctl_ha_comp_status
14257 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14259 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14261 printf("%s: go\n", __func__);
14263 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14264 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14266 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14267 != CTL_HA_STATUS_SUCCESS) {
14268 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14269 ret = CTL_HA_COMP_STATUS_ERROR;
14271 } else if (CTL_HA_STATE_IS_HA(c->state)
14272 && CTL_HA_STATE_IS_SINGLE(state)){
14273 // HA->SINGLE transition
14277 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14279 ret = CTL_HA_COMP_STATUS_ERROR;
14281 if (CTL_HA_STATE_IS_SINGLE(state))
14290 * Quiesce component
14291 * The component must clear any error conditions (set status to OK) and
14292 * prepare itself to another Start call
14293 * returns ctl_ha_comp_status:
14297 static ctl_ha_comp_status
14298 ctl_isc_quiesce(struct ctl_ha_component *c)
14300 int ret = CTL_HA_COMP_STATUS_OK;
14307 struct ctl_ha_component ctl_ha_component_ctlisc =
14310 .state = CTL_HA_STATE_UNKNOWN,
14311 .init = ctl_isc_init,
14312 .start = ctl_isc_start,
14313 .quiesce = ctl_isc_quiesce