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,
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,
286 /*aen_holdoff_period*/{0, 0}
289 static struct scsi_control_page control_page_changeable = {
290 /*page_code*/SMS_CONTROL_MODE_PAGE,
291 /*page_length*/sizeof(struct scsi_control_page) - 2,
296 /*aen_holdoff_period*/{0, 0}
301 * XXX KDM move these into the softc.
303 static int rcv_sync_msg;
304 static int persis_offset;
305 static uint8_t ctl_pause_rtr;
306 static int ctl_is_single = 1;
307 static int index_to_aps_page;
309 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
310 static int worker_threads = -1;
311 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
312 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
313 &worker_threads, 1, "Number of worker threads");
314 static int verbose = 0;
315 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
316 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
317 &verbose, 0, "Show SCSI errors returned to initiator");
320 * Serial number (0x80), device id (0x83), supported pages (0x00),
321 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
323 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
325 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
327 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
328 static int ctl_init(void);
329 void ctl_shutdown(void);
330 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
331 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
332 static void ctl_ioctl_online(void *arg);
333 static void ctl_ioctl_offline(void *arg);
334 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
335 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
336 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
337 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
338 static int ctl_ioctl_submit_wait(union ctl_io *io);
339 static void ctl_ioctl_datamove(union ctl_io *io);
340 static void ctl_ioctl_done(union ctl_io *io);
341 static void ctl_ioctl_hard_startstop_callback(void *arg,
342 struct cfi_metatask *metatask);
343 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
344 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
345 struct ctl_ooa *ooa_hdr,
346 struct ctl_ooa_entry *kern_entries);
347 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
349 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
350 uint32_t ctl_port_idx(int port_num);
352 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
353 uint32_t targ_target, uint32_t targ_lun,
355 static void ctl_kfree_io(union ctl_io *io);
357 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
358 struct ctl_be_lun *be_lun, struct ctl_id target_id);
359 static int ctl_free_lun(struct ctl_lun *lun);
360 static void ctl_create_lun(struct ctl_be_lun *be_lun);
362 static void ctl_failover_change_pages(struct ctl_softc *softc,
363 struct ctl_scsiio *ctsio, int master);
366 static int ctl_do_mode_select(union ctl_io *io);
367 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
368 uint64_t res_key, uint64_t sa_res_key,
369 uint8_t type, uint32_t residx,
370 struct ctl_scsiio *ctsio,
371 struct scsi_per_res_out *cdb,
372 struct scsi_per_res_out_parms* param);
373 static void ctl_pro_preempt_other(struct ctl_lun *lun,
374 union ctl_ha_msg *msg);
375 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
376 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
377 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
378 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
379 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
381 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
383 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
384 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
385 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
386 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
387 union ctl_io *ooa_io);
388 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
389 union ctl_io *starting_io);
390 static int ctl_check_blocked(struct ctl_lun *lun);
391 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
393 const struct ctl_cmd_entry *entry,
394 struct ctl_scsiio *ctsio);
395 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
396 static void ctl_failover(void);
397 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
398 struct ctl_scsiio *ctsio);
399 static int ctl_scsiio(struct ctl_scsiio *ctsio);
401 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
402 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
403 ctl_ua_type ua_type);
404 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
405 ctl_ua_type ua_type);
406 static int ctl_abort_task(union ctl_io *io);
407 static void ctl_run_task(union ctl_io *io);
409 static void ctl_datamove_timer_wakeup(void *arg);
410 static void ctl_done_timer_wakeup(void *arg);
411 #endif /* CTL_IO_DELAY */
413 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
414 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
415 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
416 static void ctl_datamove_remote_write(union ctl_io *io);
417 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
418 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
419 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
420 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
421 ctl_ha_dt_cb callback);
422 static void ctl_datamove_remote_read(union ctl_io *io);
423 static void ctl_datamove_remote(union ctl_io *io);
424 static int ctl_process_done(union ctl_io *io);
425 static void ctl_lun_thread(void *arg);
426 static void ctl_work_thread(void *arg);
427 static void ctl_enqueue_incoming(union ctl_io *io);
428 static void ctl_enqueue_rtr(union ctl_io *io);
429 static void ctl_enqueue_done(union ctl_io *io);
430 static void ctl_enqueue_isc(union ctl_io *io);
431 static const struct ctl_cmd_entry *
432 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
433 static const struct ctl_cmd_entry *
434 ctl_validate_command(struct ctl_scsiio *ctsio);
435 static int ctl_cmd_applicable(uint8_t lun_type,
436 const struct ctl_cmd_entry *entry);
439 * Load the serialization table. This isn't very pretty, but is probably
440 * the easiest way to do it.
442 #include "ctl_ser_table.c"
445 * We only need to define open, close and ioctl routines for this driver.
447 static struct cdevsw ctl_cdevsw = {
448 .d_version = D_VERSION,
451 .d_close = ctl_close,
452 .d_ioctl = ctl_ioctl,
457 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
458 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
460 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
462 static moduledata_t ctl_moduledata = {
464 ctl_module_event_handler,
468 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
469 MODULE_VERSION(ctl, 1);
471 static struct ctl_frontend ioctl_frontend =
477 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
478 union ctl_ha_msg *msg_info)
480 struct ctl_scsiio *ctsio;
482 if (msg_info->hdr.original_sc == NULL) {
483 printf("%s: original_sc == NULL!\n", __func__);
484 /* XXX KDM now what? */
488 ctsio = &msg_info->hdr.original_sc->scsiio;
489 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
490 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
491 ctsio->io_hdr.status = msg_info->hdr.status;
492 ctsio->scsi_status = msg_info->scsi.scsi_status;
493 ctsio->sense_len = msg_info->scsi.sense_len;
494 ctsio->sense_residual = msg_info->scsi.sense_residual;
495 ctsio->residual = msg_info->scsi.residual;
496 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
497 sizeof(ctsio->sense_data));
498 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
499 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
500 ctl_enqueue_isc((union ctl_io *)ctsio);
504 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
505 union ctl_ha_msg *msg_info)
507 struct ctl_scsiio *ctsio;
509 if (msg_info->hdr.serializing_sc == NULL) {
510 printf("%s: serializing_sc == NULL!\n", __func__);
511 /* XXX KDM now what? */
515 ctsio = &msg_info->hdr.serializing_sc->scsiio;
518 * Attempt to catch the situation where an I/O has
519 * been freed, and we're using it again.
521 if (ctsio->io_hdr.io_type == 0xff) {
522 union ctl_io *tmp_io;
523 tmp_io = (union ctl_io *)ctsio;
524 printf("%s: %p use after free!\n", __func__,
526 printf("%s: type %d msg %d cdb %x iptl: "
527 "%d:%d:%d:%d tag 0x%04x "
528 "flag %#x status %x\n",
530 tmp_io->io_hdr.io_type,
531 tmp_io->io_hdr.msg_type,
532 tmp_io->scsiio.cdb[0],
533 tmp_io->io_hdr.nexus.initid.id,
534 tmp_io->io_hdr.nexus.targ_port,
535 tmp_io->io_hdr.nexus.targ_target.id,
536 tmp_io->io_hdr.nexus.targ_lun,
537 (tmp_io->io_hdr.io_type ==
539 tmp_io->taskio.tag_num :
540 tmp_io->scsiio.tag_num,
541 tmp_io->io_hdr.flags,
542 tmp_io->io_hdr.status);
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 ctl_enqueue_isc((union ctl_io *)ctsio);
550 * ISC (Inter Shelf Communication) event handler. Events from the HA
551 * subsystem come in here.
554 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
556 struct ctl_softc *ctl_softc;
558 struct ctl_prio *presio;
559 ctl_ha_status isc_status;
561 ctl_softc = control_softc;
566 printf("CTL: Isc Msg event %d\n", event);
568 if (event == CTL_HA_EVT_MSG_RECV) {
569 union ctl_ha_msg msg_info;
571 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
572 sizeof(msg_info), /*wait*/ 0);
574 printf("CTL: msg_type %d\n", msg_info.msg_type);
576 if (isc_status != 0) {
577 printf("Error receiving message, status = %d\n",
582 switch (msg_info.hdr.msg_type) {
583 case CTL_MSG_SERIALIZE:
585 printf("Serialize\n");
587 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
589 printf("ctl_isc_event_handler: can't allocate "
592 /* Need to set busy and send msg back */
593 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
594 msg_info.hdr.status = CTL_SCSI_ERROR;
595 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
596 msg_info.scsi.sense_len = 0;
597 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
598 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
603 // populate ctsio from msg_info
604 io->io_hdr.io_type = CTL_IO_SCSI;
605 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
606 io->io_hdr.original_sc = msg_info.hdr.original_sc;
608 printf("pOrig %x\n", (int)msg_info.original_sc);
610 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
613 * If we're in serialization-only mode, we don't
614 * want to go through full done processing. Thus
617 * XXX KDM add another flag that is more specific.
619 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
620 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
621 io->io_hdr.nexus = msg_info.hdr.nexus;
623 printf("targ %d, port %d, iid %d, lun %d\n",
624 io->io_hdr.nexus.targ_target.id,
625 io->io_hdr.nexus.targ_port,
626 io->io_hdr.nexus.initid.id,
627 io->io_hdr.nexus.targ_lun);
629 io->scsiio.tag_num = msg_info.scsi.tag_num;
630 io->scsiio.tag_type = msg_info.scsi.tag_type;
631 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
633 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
634 const struct ctl_cmd_entry *entry;
636 entry = ctl_get_cmd_entry(&io->scsiio);
637 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
639 entry->flags & CTL_FLAG_DATA_MASK;
644 /* Performed on the Originating SC, XFER mode only */
645 case CTL_MSG_DATAMOVE: {
646 struct ctl_sg_entry *sgl;
649 io = msg_info.hdr.original_sc;
651 printf("%s: original_sc == NULL!\n", __func__);
652 /* XXX KDM do something here */
655 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
656 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
658 * Keep track of this, we need to send it back over
659 * when the datamove is complete.
661 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
663 if (msg_info.dt.sg_sequence == 0) {
665 * XXX KDM we use the preallocated S/G list
666 * here, but we'll need to change this to
667 * dynamic allocation if we need larger S/G
670 if (msg_info.dt.kern_sg_entries >
671 sizeof(io->io_hdr.remote_sglist) /
672 sizeof(io->io_hdr.remote_sglist[0])) {
673 printf("%s: number of S/G entries "
674 "needed %u > allocated num %zd\n",
676 msg_info.dt.kern_sg_entries,
677 sizeof(io->io_hdr.remote_sglist)/
678 sizeof(io->io_hdr.remote_sglist[0]));
681 * XXX KDM send a message back to
682 * the other side to shut down the
683 * DMA. The error will come back
684 * through via the normal channel.
688 sgl = io->io_hdr.remote_sglist;
690 sizeof(io->io_hdr.remote_sglist));
692 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
694 io->scsiio.kern_sg_entries =
695 msg_info.dt.kern_sg_entries;
696 io->scsiio.rem_sg_entries =
697 msg_info.dt.kern_sg_entries;
698 io->scsiio.kern_data_len =
699 msg_info.dt.kern_data_len;
700 io->scsiio.kern_total_len =
701 msg_info.dt.kern_total_len;
702 io->scsiio.kern_data_resid =
703 msg_info.dt.kern_data_resid;
704 io->scsiio.kern_rel_offset =
705 msg_info.dt.kern_rel_offset;
707 * Clear out per-DMA flags.
709 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
711 * Add per-DMA flags that are set for this
712 * particular DMA request.
714 io->io_hdr.flags |= msg_info.dt.flags &
717 sgl = (struct ctl_sg_entry *)
718 io->scsiio.kern_data_ptr;
720 for (i = msg_info.dt.sent_sg_entries, j = 0;
721 i < (msg_info.dt.sent_sg_entries +
722 msg_info.dt.cur_sg_entries); i++, j++) {
723 sgl[i].addr = msg_info.dt.sg_list[j].addr;
724 sgl[i].len = msg_info.dt.sg_list[j].len;
727 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
729 msg_info.dt.sg_list[j].addr,
730 msg_info.dt.sg_list[j].len,
731 sgl[i].addr, sgl[i].len, j, i);
735 memcpy(&sgl[msg_info.dt.sent_sg_entries],
737 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
741 * If this is the last piece of the I/O, we've got
742 * the full S/G list. Queue processing in the thread.
743 * Otherwise wait for the next piece.
745 if (msg_info.dt.sg_last != 0)
749 /* Performed on the Serializing (primary) SC, XFER mode only */
750 case CTL_MSG_DATAMOVE_DONE: {
751 if (msg_info.hdr.serializing_sc == NULL) {
752 printf("%s: serializing_sc == NULL!\n",
754 /* XXX KDM now what? */
758 * We grab the sense information here in case
759 * there was a failure, so we can return status
760 * back to the initiator.
762 io = msg_info.hdr.serializing_sc;
763 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
764 io->io_hdr.status = msg_info.hdr.status;
765 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
766 io->scsiio.sense_len = msg_info.scsi.sense_len;
767 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
768 io->io_hdr.port_status = msg_info.scsi.fetd_status;
769 io->scsiio.residual = msg_info.scsi.residual;
770 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
771 sizeof(io->scsiio.sense_data));
776 /* Preformed on Originating SC, SER_ONLY mode */
778 io = msg_info.hdr.original_sc;
780 printf("%s: Major Bummer\n", __func__);
784 printf("pOrig %x\n",(int) ctsio);
787 io->io_hdr.msg_type = CTL_MSG_R2R;
788 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
793 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
795 * Performed on the Originating (i.e. secondary) SC in XFER
798 case CTL_MSG_FINISH_IO:
799 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
800 ctl_isc_handler_finish_xfer(ctl_softc,
803 ctl_isc_handler_finish_ser_only(ctl_softc,
807 /* Preformed on Originating SC */
808 case CTL_MSG_BAD_JUJU:
809 io = msg_info.hdr.original_sc;
811 printf("%s: Bad JUJU!, original_sc is NULL!\n",
815 ctl_copy_sense_data(&msg_info, io);
817 * IO should have already been cleaned up on other
818 * SC so clear this flag so we won't send a message
819 * back to finish the IO there.
821 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
822 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
824 /* io = msg_info.hdr.serializing_sc; */
825 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
829 /* Handle resets sent from the other side */
830 case CTL_MSG_MANAGE_TASKS: {
831 struct ctl_taskio *taskio;
832 taskio = (struct ctl_taskio *)ctl_alloc_io(
833 (void *)ctl_softc->othersc_pool);
834 if (taskio == NULL) {
835 printf("ctl_isc_event_handler: can't allocate "
838 /* should I just call the proper reset func
842 ctl_zero_io((union ctl_io *)taskio);
843 taskio->io_hdr.io_type = CTL_IO_TASK;
844 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
845 taskio->io_hdr.nexus = msg_info.hdr.nexus;
846 taskio->task_action = msg_info.task.task_action;
847 taskio->tag_num = msg_info.task.tag_num;
848 taskio->tag_type = msg_info.task.tag_type;
850 taskio->io_hdr.start_time = time_uptime;
851 getbintime(&taskio->io_hdr.start_bt);
853 cs_prof_gettime(&taskio->io_hdr.start_ticks);
855 #endif /* CTL_TIME_IO */
856 ctl_run_task((union ctl_io *)taskio);
859 /* Persistent Reserve action which needs attention */
860 case CTL_MSG_PERS_ACTION:
861 presio = (struct ctl_prio *)ctl_alloc_io(
862 (void *)ctl_softc->othersc_pool);
863 if (presio == NULL) {
864 printf("ctl_isc_event_handler: can't allocate "
867 /* Need to set busy and send msg back */
870 ctl_zero_io((union ctl_io *)presio);
871 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
872 presio->pr_msg = msg_info.pr;
873 ctl_enqueue_isc((union ctl_io *)presio);
875 case CTL_MSG_SYNC_FE:
878 case CTL_MSG_APS_LOCK: {
879 // It's quicker to execute this then to
882 struct ctl_page_index *page_index;
883 struct copan_aps_subpage *current_sp;
886 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
887 lun = ctl_softc->ctl_luns[targ_lun];
888 mtx_lock(&lun->lun_lock);
889 page_index = &lun->mode_pages.index[index_to_aps_page];
890 current_sp = (struct copan_aps_subpage *)
891 (page_index->page_data +
892 (page_index->page_len * CTL_PAGE_CURRENT));
894 current_sp->lock_active = msg_info.aps.lock_flag;
895 mtx_unlock(&lun->lun_lock);
899 printf("How did I get here?\n");
901 } else if (event == CTL_HA_EVT_MSG_SENT) {
902 if (param != CTL_HA_STATUS_SUCCESS) {
903 printf("Bad status from ctl_ha_msg_send status %d\n",
907 } else if (event == CTL_HA_EVT_DISCONNECT) {
908 printf("CTL: Got a disconnect from Isc\n");
911 printf("ctl_isc_event_handler: Unknown event %d\n", event);
920 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
922 struct scsi_sense_data *sense;
924 sense = &dest->scsiio.sense_data;
925 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
926 dest->scsiio.scsi_status = src->scsi.scsi_status;
927 dest->scsiio.sense_len = src->scsi.sense_len;
928 dest->io_hdr.status = src->hdr.status;
934 struct ctl_softc *softc;
935 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
936 struct ctl_port *port;
938 int i, error, retval;
945 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
947 softc = control_softc;
949 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
952 softc->dev->si_drv1 = softc;
955 * By default, return a "bad LUN" peripheral qualifier for unknown
956 * LUNs. The user can override this default using the tunable or
957 * sysctl. See the comment in ctl_inquiry_std() for more details.
959 softc->inquiry_pq_no_lun = 1;
960 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
961 &softc->inquiry_pq_no_lun);
962 sysctl_ctx_init(&softc->sysctl_ctx);
963 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
964 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
965 CTLFLAG_RD, 0, "CAM Target Layer");
967 if (softc->sysctl_tree == NULL) {
968 printf("%s: unable to allocate sysctl tree\n", __func__);
969 destroy_dev(softc->dev);
970 free(control_softc, M_DEVBUF);
971 control_softc = NULL;
975 SYSCTL_ADD_INT(&softc->sysctl_ctx,
976 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
977 "inquiry_pq_no_lun", CTLFLAG_RW,
978 &softc->inquiry_pq_no_lun, 0,
979 "Report no lun possible for invalid LUNs");
981 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
982 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
983 softc->open_count = 0;
986 * Default to actually sending a SYNCHRONIZE CACHE command down to
989 softc->flags = CTL_FLAG_REAL_SYNC;
992 * In Copan's HA scheme, the "master" and "slave" roles are
993 * figured out through the slot the controller is in. Although it
994 * is an active/active system, someone has to be in charge.
997 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1001 softc->flags |= CTL_FLAG_MASTER_SHELF;
1004 persis_offset = CTL_MAX_INITIATORS;
1007 * XXX KDM need to figure out where we want to get our target ID
1008 * and WWID. Is it different on each port?
1010 softc->target.id = 0;
1011 softc->target.wwid[0] = 0x12345678;
1012 softc->target.wwid[1] = 0x87654321;
1013 STAILQ_INIT(&softc->lun_list);
1014 STAILQ_INIT(&softc->pending_lun_queue);
1015 STAILQ_INIT(&softc->fe_list);
1016 STAILQ_INIT(&softc->port_list);
1017 STAILQ_INIT(&softc->be_list);
1018 STAILQ_INIT(&softc->io_pools);
1020 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1021 &internal_pool)!= 0){
1022 printf("ctl: can't allocate %d entry internal pool, "
1023 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1027 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1028 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1029 printf("ctl: can't allocate %d entry emergency pool, "
1030 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1031 ctl_pool_free(internal_pool);
1035 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1038 printf("ctl: can't allocate %d entry other SC pool, "
1039 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1040 ctl_pool_free(internal_pool);
1041 ctl_pool_free(emergency_pool);
1045 softc->internal_pool = internal_pool;
1046 softc->emergency_pool = emergency_pool;
1047 softc->othersc_pool = other_pool;
1049 if (worker_threads <= 0)
1050 worker_threads = max(1, mp_ncpus / 4);
1051 if (worker_threads > CTL_MAX_THREADS)
1052 worker_threads = CTL_MAX_THREADS;
1054 for (i = 0; i < worker_threads; i++) {
1055 struct ctl_thread *thr = &softc->threads[i];
1057 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1058 thr->ctl_softc = softc;
1059 STAILQ_INIT(&thr->incoming_queue);
1060 STAILQ_INIT(&thr->rtr_queue);
1061 STAILQ_INIT(&thr->done_queue);
1062 STAILQ_INIT(&thr->isc_queue);
1064 error = kproc_kthread_add(ctl_work_thread, thr,
1065 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1067 printf("error creating CTL work thread!\n");
1068 ctl_pool_free(internal_pool);
1069 ctl_pool_free(emergency_pool);
1070 ctl_pool_free(other_pool);
1074 error = kproc_kthread_add(ctl_lun_thread, softc,
1075 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1077 printf("error creating CTL lun thread!\n");
1078 ctl_pool_free(internal_pool);
1079 ctl_pool_free(emergency_pool);
1080 ctl_pool_free(other_pool);
1084 printf("ctl: CAM Target Layer loaded\n");
1087 * Initialize the initiator and portname mappings
1089 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1092 * Initialize the ioctl front end.
1094 ctl_frontend_register(&ioctl_frontend);
1095 port = &softc->ioctl_info.port;
1096 port->frontend = &ioctl_frontend;
1097 sprintf(softc->ioctl_info.port_name, "ioctl");
1098 port->port_type = CTL_PORT_IOCTL;
1099 port->num_requested_ctl_io = 100;
1100 port->port_name = softc->ioctl_info.port_name;
1101 port->port_online = ctl_ioctl_online;
1102 port->port_offline = ctl_ioctl_offline;
1103 port->onoff_arg = &softc->ioctl_info;
1104 port->lun_enable = ctl_ioctl_lun_enable;
1105 port->lun_disable = ctl_ioctl_lun_disable;
1106 port->targ_lun_arg = &softc->ioctl_info;
1107 port->fe_datamove = ctl_ioctl_datamove;
1108 port->fe_done = ctl_ioctl_done;
1109 port->max_targets = 15;
1110 port->max_target_id = 15;
1112 if (ctl_port_register(&softc->ioctl_info.port,
1113 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1114 printf("ctl: ioctl front end registration failed, will "
1115 "continue anyway\n");
1119 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1120 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1121 sizeof(struct callout), CTL_TIMER_BYTES);
1124 #endif /* CTL_IO_DELAY */
1132 struct ctl_softc *softc;
1133 struct ctl_lun *lun, *next_lun;
1134 struct ctl_io_pool *pool;
1136 softc = (struct ctl_softc *)control_softc;
1138 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1139 printf("ctl: ioctl front end deregistration failed\n");
1141 mtx_lock(&softc->ctl_lock);
1146 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1147 next_lun = STAILQ_NEXT(lun, links);
1151 mtx_unlock(&softc->ctl_lock);
1153 ctl_frontend_deregister(&ioctl_frontend);
1156 * This will rip the rug out from under any FETDs or anyone else
1157 * that has a pool allocated. Since we increment our module
1158 * refcount any time someone outside the main CTL module allocates
1159 * a pool, we shouldn't have any problems here. The user won't be
1160 * able to unload the CTL module until client modules have
1161 * successfully unloaded.
1163 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1164 ctl_pool_free(pool);
1167 ctl_shutdown_thread(softc->work_thread);
1168 mtx_destroy(&softc->queue_lock);
1171 mtx_destroy(&softc->pool_lock);
1172 mtx_destroy(&softc->ctl_lock);
1174 destroy_dev(softc->dev);
1176 sysctl_ctx_free(&softc->sysctl_ctx);
1178 free(control_softc, M_DEVBUF);
1179 control_softc = NULL;
1182 printf("ctl: CAM Target Layer unloaded\n");
1186 ctl_module_event_handler(module_t mod, int what, void *arg)
1191 return (ctl_init());
1195 return (EOPNOTSUPP);
1200 * XXX KDM should we do some access checks here? Bump a reference count to
1201 * prevent a CTL module from being unloaded while someone has it open?
1204 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1210 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1216 ctl_port_enable(ctl_port_type port_type)
1218 struct ctl_softc *softc;
1219 struct ctl_port *port;
1221 if (ctl_is_single == 0) {
1222 union ctl_ha_msg msg_info;
1226 printf("%s: HA mode, synchronizing frontend enable\n",
1229 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1230 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1231 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1232 printf("Sync msg send error retval %d\n", isc_retval);
1234 if (!rcv_sync_msg) {
1235 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1236 sizeof(msg_info), 1);
1239 printf("CTL:Frontend Enable\n");
1241 printf("%s: single mode, skipping frontend synchronization\n",
1246 softc = control_softc;
1248 STAILQ_FOREACH(port, &softc->port_list, links) {
1249 if (port_type & port->port_type)
1252 printf("port %d\n", port->targ_port);
1254 ctl_port_online(port);
1262 ctl_port_disable(ctl_port_type port_type)
1264 struct ctl_softc *softc;
1265 struct ctl_port *port;
1267 softc = control_softc;
1269 STAILQ_FOREACH(port, &softc->port_list, links) {
1270 if (port_type & port->port_type)
1271 ctl_port_offline(port);
1278 * Returns 0 for success, 1 for failure.
1279 * Currently the only failure mode is if there aren't enough entries
1280 * allocated. So, in case of a failure, look at num_entries_dropped,
1281 * reallocate and try again.
1284 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1285 int *num_entries_filled, int *num_entries_dropped,
1286 ctl_port_type port_type, int no_virtual)
1288 struct ctl_softc *softc;
1289 struct ctl_port *port;
1290 int entries_dropped, entries_filled;
1294 softc = control_softc;
1298 entries_dropped = 0;
1301 mtx_lock(&softc->ctl_lock);
1302 STAILQ_FOREACH(port, &softc->port_list, links) {
1303 struct ctl_port_entry *entry;
1305 if ((port->port_type & port_type) == 0)
1308 if ((no_virtual != 0)
1309 && (port->virtual_port != 0))
1312 if (entries_filled >= num_entries_alloced) {
1316 entry = &entries[i];
1318 entry->port_type = port->port_type;
1319 strlcpy(entry->port_name, port->port_name,
1320 sizeof(entry->port_name));
1321 entry->physical_port = port->physical_port;
1322 entry->virtual_port = port->virtual_port;
1323 entry->wwnn = port->wwnn;
1324 entry->wwpn = port->wwpn;
1330 mtx_unlock(&softc->ctl_lock);
1332 if (entries_dropped > 0)
1335 *num_entries_dropped = entries_dropped;
1336 *num_entries_filled = entries_filled;
1342 ctl_ioctl_online(void *arg)
1344 struct ctl_ioctl_info *ioctl_info;
1346 ioctl_info = (struct ctl_ioctl_info *)arg;
1348 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1352 ctl_ioctl_offline(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 * Remove an initiator by port number and initiator ID.
1363 * Returns 0 for success, 1 for failure.
1366 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1368 struct ctl_softc *softc;
1370 softc = control_softc;
1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1375 || (targ_port > CTL_MAX_PORTS)) {
1376 printf("%s: invalid port number %d\n", __func__, targ_port);
1379 if (iid > CTL_MAX_INIT_PER_PORT) {
1380 printf("%s: initiator ID %u > maximun %u!\n",
1381 __func__, iid, CTL_MAX_INIT_PER_PORT);
1385 mtx_lock(&softc->ctl_lock);
1387 softc->wwpn_iid[targ_port][iid].in_use = 0;
1389 mtx_unlock(&softc->ctl_lock);
1395 * Add an initiator to the initiator map.
1396 * Returns 0 for success, 1 for failure.
1399 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1401 struct ctl_softc *softc;
1404 softc = control_softc;
1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1411 || (targ_port > CTL_MAX_PORTS)) {
1412 printf("%s: invalid port number %d\n", __func__, targ_port);
1415 if (iid > CTL_MAX_INIT_PER_PORT) {
1416 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1417 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1421 mtx_lock(&softc->ctl_lock);
1423 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1425 * We don't treat this as an error.
1427 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1428 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1429 __func__, targ_port, iid, (uintmax_t)wwpn);
1434 * This is an error, but what do we do about it? The
1435 * driver is telling us we have a new WWPN for this
1436 * initiator ID, so we pretty much need to use it.
1438 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1439 "still at that address\n", __func__, targ_port, iid,
1441 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1444 * XXX KDM clear have_ca and ua_pending on each LUN for
1448 softc->wwpn_iid[targ_port][iid].in_use = 1;
1449 softc->wwpn_iid[targ_port][iid].iid = iid;
1450 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1451 softc->wwpn_iid[targ_port][iid].port = targ_port;
1455 mtx_unlock(&softc->ctl_lock);
1461 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1467 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1473 * Data movement routine for the CTL ioctl frontend port.
1476 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1478 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1479 struct ctl_sg_entry ext_entry, kern_entry;
1480 int ext_sglen, ext_sg_entries, kern_sg_entries;
1481 int ext_sg_start, ext_offset;
1482 int len_to_copy, len_copied;
1483 int kern_watermark, ext_watermark;
1484 int ext_sglist_malloced;
1487 ext_sglist_malloced = 0;
1491 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1494 * If this flag is set, fake the data transfer.
1496 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1497 ctsio->ext_data_filled = ctsio->ext_data_len;
1502 * To simplify things here, if we have a single buffer, stick it in
1503 * a S/G entry and just make it a single entry S/G list.
1505 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1508 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1510 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1512 ext_sglist_malloced = 1;
1513 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1515 ctl_set_internal_failure(ctsio,
1520 ext_sg_entries = ctsio->ext_sg_entries;
1522 for (i = 0; i < ext_sg_entries; i++) {
1523 if ((len_seen + ext_sglist[i].len) >=
1524 ctsio->ext_data_filled) {
1526 ext_offset = ctsio->ext_data_filled - len_seen;
1529 len_seen += ext_sglist[i].len;
1532 ext_sglist = &ext_entry;
1533 ext_sglist->addr = ctsio->ext_data_ptr;
1534 ext_sglist->len = ctsio->ext_data_len;
1537 ext_offset = ctsio->ext_data_filled;
1540 if (ctsio->kern_sg_entries > 0) {
1541 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1542 kern_sg_entries = ctsio->kern_sg_entries;
1544 kern_sglist = &kern_entry;
1545 kern_sglist->addr = ctsio->kern_data_ptr;
1546 kern_sglist->len = ctsio->kern_data_len;
1547 kern_sg_entries = 1;
1552 ext_watermark = ext_offset;
1554 for (i = ext_sg_start, j = 0;
1555 i < ext_sg_entries && j < kern_sg_entries;) {
1556 uint8_t *ext_ptr, *kern_ptr;
1558 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1559 kern_sglist[j].len - kern_watermark);
1561 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1562 ext_ptr = ext_ptr + ext_watermark;
1563 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1567 panic("need to implement bus address support");
1569 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1572 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1573 kern_ptr = kern_ptr + kern_watermark;
1575 kern_watermark += len_to_copy;
1576 ext_watermark += len_to_copy;
1578 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1580 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1581 "bytes to user\n", len_to_copy));
1582 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1583 "to %p\n", kern_ptr, ext_ptr));
1584 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1585 ctl_set_internal_failure(ctsio,
1591 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1592 "bytes from user\n", len_to_copy));
1593 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1594 "to %p\n", ext_ptr, kern_ptr));
1595 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1596 ctl_set_internal_failure(ctsio,
1603 len_copied += len_to_copy;
1605 if (ext_sglist[i].len == ext_watermark) {
1610 if (kern_sglist[j].len == kern_watermark) {
1616 ctsio->ext_data_filled += len_copied;
1618 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1619 "kern_sg_entries: %d\n", ext_sg_entries,
1621 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1622 "kern_data_len = %d\n", ctsio->ext_data_len,
1623 ctsio->kern_data_len));
1626 /* XXX KDM set residual?? */
1629 if (ext_sglist_malloced != 0)
1630 free(ext_sglist, M_CTL);
1632 return (CTL_RETVAL_COMPLETE);
1636 * Serialize a command that went down the "wrong" side, and so was sent to
1637 * this controller for execution. The logic is a little different than the
1638 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1639 * sent back to the other side, but in the success case, we execute the
1640 * command on this side (XFER mode) or tell the other side to execute it
1644 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1646 struct ctl_softc *ctl_softc;
1647 union ctl_ha_msg msg_info;
1648 struct ctl_lun *lun;
1652 ctl_softc = control_softc;
1654 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1655 lun = ctl_softc->ctl_luns[targ_lun];
1659 * Why isn't LUN defined? The other side wouldn't
1660 * send a cmd if the LUN is undefined.
1662 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1664 /* "Logical unit not supported" */
1665 ctl_set_sense_data(&msg_info.scsi.sense_data,
1667 /*sense_format*/SSD_TYPE_NONE,
1668 /*current_error*/ 1,
1669 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1674 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1675 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1676 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1677 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1678 msg_info.hdr.serializing_sc = NULL;
1679 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1680 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1681 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1687 mtx_lock(&lun->lun_lock);
1688 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1690 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1691 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1693 case CTL_ACTION_BLOCK:
1694 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1695 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1698 case CTL_ACTION_PASS:
1699 case CTL_ACTION_SKIP:
1700 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1701 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1702 ctl_enqueue_rtr((union ctl_io *)ctsio);
1705 /* send msg back to other side */
1706 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1707 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1708 msg_info.hdr.msg_type = CTL_MSG_R2R;
1710 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1712 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1713 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1717 case CTL_ACTION_OVERLAP:
1718 /* OVERLAPPED COMMANDS ATTEMPTED */
1719 ctl_set_sense_data(&msg_info.scsi.sense_data,
1721 /*sense_format*/SSD_TYPE_NONE,
1722 /*current_error*/ 1,
1723 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1728 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1729 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1730 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1731 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1732 msg_info.hdr.serializing_sc = NULL;
1733 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1735 printf("BAD JUJU:Major Bummer Overlap\n");
1737 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1739 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1740 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1743 case CTL_ACTION_OVERLAP_TAG:
1744 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1745 ctl_set_sense_data(&msg_info.scsi.sense_data,
1747 /*sense_format*/SSD_TYPE_NONE,
1748 /*current_error*/ 1,
1749 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1751 /*ascq*/ ctsio->tag_num & 0xff,
1754 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1755 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1756 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1757 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1758 msg_info.hdr.serializing_sc = NULL;
1759 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1761 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1763 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1765 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1766 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1769 case CTL_ACTION_ERROR:
1771 /* "Internal target failure" */
1772 ctl_set_sense_data(&msg_info.scsi.sense_data,
1774 /*sense_format*/SSD_TYPE_NONE,
1775 /*current_error*/ 1,
1776 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1781 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1782 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1783 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1784 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1785 msg_info.hdr.serializing_sc = NULL;
1786 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1788 printf("BAD JUJU:Major Bummer HW Error\n");
1790 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1792 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1793 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1797 mtx_unlock(&lun->lun_lock);
1802 ctl_ioctl_submit_wait(union ctl_io *io)
1804 struct ctl_fe_ioctl_params params;
1805 ctl_fe_ioctl_state last_state;
1810 bzero(¶ms, sizeof(params));
1812 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1813 cv_init(¶ms.sem, "ctlioccv");
1814 params.state = CTL_IOCTL_INPROG;
1815 last_state = params.state;
1817 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1819 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1821 /* This shouldn't happen */
1822 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1828 mtx_lock(¶ms.ioctl_mtx);
1830 * Check the state here, and don't sleep if the state has
1831 * already changed (i.e. wakeup has already occured, but we
1832 * weren't waiting yet).
1834 if (params.state == last_state) {
1835 /* XXX KDM cv_wait_sig instead? */
1836 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1838 last_state = params.state;
1840 switch (params.state) {
1841 case CTL_IOCTL_INPROG:
1842 /* Why did we wake up? */
1843 /* XXX KDM error here? */
1844 mtx_unlock(¶ms.ioctl_mtx);
1846 case CTL_IOCTL_DATAMOVE:
1847 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1850 * change last_state back to INPROG to avoid
1851 * deadlock on subsequent data moves.
1853 params.state = last_state = CTL_IOCTL_INPROG;
1855 mtx_unlock(¶ms.ioctl_mtx);
1856 ctl_ioctl_do_datamove(&io->scsiio);
1858 * Note that in some cases, most notably writes,
1859 * this will queue the I/O and call us back later.
1860 * In other cases, generally reads, this routine
1861 * will immediately call back and wake us up,
1862 * probably using our own context.
1864 io->scsiio.be_move_done(io);
1866 case CTL_IOCTL_DONE:
1867 mtx_unlock(¶ms.ioctl_mtx);
1868 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1872 mtx_unlock(¶ms.ioctl_mtx);
1873 /* XXX KDM error here? */
1876 } while (done == 0);
1878 mtx_destroy(¶ms.ioctl_mtx);
1879 cv_destroy(¶ms.sem);
1881 return (CTL_RETVAL_COMPLETE);
1885 ctl_ioctl_datamove(union ctl_io *io)
1887 struct ctl_fe_ioctl_params *params;
1889 params = (struct ctl_fe_ioctl_params *)
1890 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1892 mtx_lock(¶ms->ioctl_mtx);
1893 params->state = CTL_IOCTL_DATAMOVE;
1894 cv_broadcast(¶ms->sem);
1895 mtx_unlock(¶ms->ioctl_mtx);
1899 ctl_ioctl_done(union ctl_io *io)
1901 struct ctl_fe_ioctl_params *params;
1903 params = (struct ctl_fe_ioctl_params *)
1904 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1906 mtx_lock(¶ms->ioctl_mtx);
1907 params->state = CTL_IOCTL_DONE;
1908 cv_broadcast(¶ms->sem);
1909 mtx_unlock(¶ms->ioctl_mtx);
1913 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1915 struct ctl_fe_ioctl_startstop_info *sd_info;
1917 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1919 sd_info->hs_info.status = metatask->status;
1920 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1921 sd_info->hs_info.luns_complete =
1922 metatask->taskinfo.startstop.luns_complete;
1923 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1925 cv_broadcast(&sd_info->sem);
1929 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1931 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1933 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1935 mtx_lock(fe_bbr_info->lock);
1936 fe_bbr_info->bbr_info->status = metatask->status;
1937 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1938 fe_bbr_info->wakeup_done = 1;
1939 mtx_unlock(fe_bbr_info->lock);
1941 cv_broadcast(&fe_bbr_info->sem);
1945 * Returns 0 for success, errno for failure.
1948 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1949 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1956 mtx_lock(&lun->lun_lock);
1957 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1958 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1960 struct ctl_ooa_entry *entry;
1963 * If we've got more than we can fit, just count the
1964 * remaining entries.
1966 if (*cur_fill_num >= ooa_hdr->alloc_num)
1969 entry = &kern_entries[*cur_fill_num];
1971 entry->tag_num = io->scsiio.tag_num;
1972 entry->lun_num = lun->lun;
1974 entry->start_bt = io->io_hdr.start_bt;
1976 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1977 entry->cdb_len = io->scsiio.cdb_len;
1978 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1979 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1981 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1982 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1984 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1985 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1987 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1988 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1990 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1991 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1993 mtx_unlock(&lun->lun_lock);
1999 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2000 size_t error_str_len)
2004 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2006 if (copyin(user_addr, kptr, len) != 0) {
2007 snprintf(error_str, error_str_len, "Error copying %d bytes "
2008 "from user address %p to kernel address %p", len,
2018 ctl_free_args(int num_args, struct ctl_be_arg *args)
2025 for (i = 0; i < num_args; i++) {
2026 free(args[i].kname, M_CTL);
2027 free(args[i].kvalue, M_CTL);
2033 static struct ctl_be_arg *
2034 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2035 char *error_str, size_t error_str_len)
2037 struct ctl_be_arg *args;
2040 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2041 error_str, error_str_len);
2046 for (i = 0; i < num_args; i++) {
2047 args[i].kname = NULL;
2048 args[i].kvalue = NULL;
2051 for (i = 0; i < num_args; i++) {
2054 args[i].kname = ctl_copyin_alloc(args[i].name,
2055 args[i].namelen, error_str, error_str_len);
2056 if (args[i].kname == NULL)
2059 if (args[i].kname[args[i].namelen - 1] != '\0') {
2060 snprintf(error_str, error_str_len, "Argument %d "
2061 "name is not NUL-terminated", i);
2065 if (args[i].flags & CTL_BEARG_RD) {
2066 tmpptr = ctl_copyin_alloc(args[i].value,
2067 args[i].vallen, error_str, error_str_len);
2070 if ((args[i].flags & CTL_BEARG_ASCII)
2071 && (tmpptr[args[i].vallen - 1] != '\0')) {
2072 snprintf(error_str, error_str_len, "Argument "
2073 "%d value is not NUL-terminated", i);
2076 args[i].kvalue = tmpptr;
2078 args[i].kvalue = malloc(args[i].vallen,
2079 M_CTL, M_WAITOK | M_ZERO);
2086 ctl_free_args(num_args, args);
2092 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2096 for (i = 0; i < num_args; i++) {
2097 if (args[i].flags & CTL_BEARG_WR)
2098 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2103 * Escape characters that are illegal or not recommended in XML.
2106 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2112 for (; *str; str++) {
2115 retval = sbuf_printf(sb, "&");
2118 retval = sbuf_printf(sb, ">");
2121 retval = sbuf_printf(sb, "<");
2124 retval = sbuf_putc(sb, *str);
2137 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2140 struct ctl_softc *softc;
2143 softc = control_softc;
2153 * If we haven't been "enabled", don't allow any SCSI I/O
2156 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2161 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2163 printf("ctl_ioctl: can't allocate ctl_io!\n");
2169 * Need to save the pool reference so it doesn't get
2170 * spammed by the user's ctl_io.
2172 pool_tmp = io->io_hdr.pool;
2174 memcpy(io, (void *)addr, sizeof(*io));
2176 io->io_hdr.pool = pool_tmp;
2178 * No status yet, so make sure the status is set properly.
2180 io->io_hdr.status = CTL_STATUS_NONE;
2183 * The user sets the initiator ID, target and LUN IDs.
2185 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2186 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2187 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2188 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2189 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2191 retval = ctl_ioctl_submit_wait(io);
2198 memcpy((void *)addr, io, sizeof(*io));
2200 /* return this to our pool */
2205 case CTL_ENABLE_PORT:
2206 case CTL_DISABLE_PORT:
2207 case CTL_SET_PORT_WWNS: {
2208 struct ctl_port *port;
2209 struct ctl_port_entry *entry;
2211 entry = (struct ctl_port_entry *)addr;
2213 mtx_lock(&softc->ctl_lock);
2214 STAILQ_FOREACH(port, &softc->port_list, links) {
2220 if ((entry->port_type == CTL_PORT_NONE)
2221 && (entry->targ_port == port->targ_port)) {
2223 * If the user only wants to enable or
2224 * disable or set WWNs on a specific port,
2225 * do the operation and we're done.
2229 } else if (entry->port_type & port->port_type) {
2231 * Compare the user's type mask with the
2232 * particular frontend type to see if we
2239 * Make sure the user isn't trying to set
2240 * WWNs on multiple ports at the same time.
2242 if (cmd == CTL_SET_PORT_WWNS) {
2243 printf("%s: Can't set WWNs on "
2244 "multiple ports\n", __func__);
2251 * XXX KDM we have to drop the lock here,
2252 * because the online/offline operations
2253 * can potentially block. We need to
2254 * reference count the frontends so they
2257 mtx_unlock(&softc->ctl_lock);
2259 if (cmd == CTL_ENABLE_PORT) {
2260 struct ctl_lun *lun;
2262 STAILQ_FOREACH(lun, &softc->lun_list,
2264 port->lun_enable(port->targ_lun_arg,
2269 ctl_port_online(port);
2270 } else if (cmd == CTL_DISABLE_PORT) {
2271 struct ctl_lun *lun;
2273 ctl_port_offline(port);
2275 STAILQ_FOREACH(lun, &softc->lun_list,
2284 mtx_lock(&softc->ctl_lock);
2286 if (cmd == CTL_SET_PORT_WWNS)
2287 ctl_port_set_wwns(port,
2288 (entry->flags & CTL_PORT_WWNN_VALID) ?
2290 (entry->flags & CTL_PORT_WWPN_VALID) ?
2291 1 : 0, entry->wwpn);
2296 mtx_unlock(&softc->ctl_lock);
2299 case CTL_GET_PORT_LIST: {
2300 struct ctl_port *port;
2301 struct ctl_port_list *list;
2304 list = (struct ctl_port_list *)addr;
2306 if (list->alloc_len != (list->alloc_num *
2307 sizeof(struct ctl_port_entry))) {
2308 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2309 "alloc_num %u * sizeof(struct ctl_port_entry) "
2310 "%zu\n", __func__, list->alloc_len,
2311 list->alloc_num, sizeof(struct ctl_port_entry));
2317 list->dropped_num = 0;
2319 mtx_lock(&softc->ctl_lock);
2320 STAILQ_FOREACH(port, &softc->port_list, links) {
2321 struct ctl_port_entry entry, *list_entry;
2323 if (list->fill_num >= list->alloc_num) {
2324 list->dropped_num++;
2328 entry.port_type = port->port_type;
2329 strlcpy(entry.port_name, port->port_name,
2330 sizeof(entry.port_name));
2331 entry.targ_port = port->targ_port;
2332 entry.physical_port = port->physical_port;
2333 entry.virtual_port = port->virtual_port;
2334 entry.wwnn = port->wwnn;
2335 entry.wwpn = port->wwpn;
2336 if (port->status & CTL_PORT_STATUS_ONLINE)
2341 list_entry = &list->entries[i];
2343 retval = copyout(&entry, list_entry, sizeof(entry));
2345 printf("%s: CTL_GET_PORT_LIST: copyout "
2346 "returned %d\n", __func__, retval);
2351 list->fill_len += sizeof(entry);
2353 mtx_unlock(&softc->ctl_lock);
2356 * If this is non-zero, we had a copyout fault, so there's
2357 * probably no point in attempting to set the status inside
2363 if (list->dropped_num > 0)
2364 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2366 list->status = CTL_PORT_LIST_OK;
2369 case CTL_DUMP_OOA: {
2370 struct ctl_lun *lun;
2375 mtx_lock(&softc->ctl_lock);
2376 printf("Dumping OOA queues:\n");
2377 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2378 mtx_lock(&lun->lun_lock);
2379 for (io = (union ctl_io *)TAILQ_FIRST(
2380 &lun->ooa_queue); io != NULL;
2381 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2383 sbuf_new(&sb, printbuf, sizeof(printbuf),
2385 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2389 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2391 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2393 CTL_FLAG_ABORT) ? " ABORT" : "",
2395 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2396 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2398 printf("%s\n", sbuf_data(&sb));
2400 mtx_unlock(&lun->lun_lock);
2402 printf("OOA queues dump done\n");
2403 mtx_unlock(&softc->ctl_lock);
2407 struct ctl_lun *lun;
2408 struct ctl_ooa *ooa_hdr;
2409 struct ctl_ooa_entry *entries;
2410 uint32_t cur_fill_num;
2412 ooa_hdr = (struct ctl_ooa *)addr;
2414 if ((ooa_hdr->alloc_len == 0)
2415 || (ooa_hdr->alloc_num == 0)) {
2416 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2417 "must be non-zero\n", __func__,
2418 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2423 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2424 sizeof(struct ctl_ooa_entry))) {
2425 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2426 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2427 __func__, ooa_hdr->alloc_len,
2428 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2433 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2434 if (entries == NULL) {
2435 printf("%s: could not allocate %d bytes for OOA "
2436 "dump\n", __func__, ooa_hdr->alloc_len);
2441 mtx_lock(&softc->ctl_lock);
2442 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2443 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2444 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2445 mtx_unlock(&softc->ctl_lock);
2446 free(entries, M_CTL);
2447 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2448 __func__, (uintmax_t)ooa_hdr->lun_num);
2455 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2456 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2457 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2463 mtx_unlock(&softc->ctl_lock);
2464 free(entries, M_CTL);
2468 lun = softc->ctl_luns[ooa_hdr->lun_num];
2470 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2473 mtx_unlock(&softc->ctl_lock);
2475 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2476 ooa_hdr->fill_len = ooa_hdr->fill_num *
2477 sizeof(struct ctl_ooa_entry);
2478 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2480 printf("%s: error copying out %d bytes for OOA dump\n",
2481 __func__, ooa_hdr->fill_len);
2484 getbintime(&ooa_hdr->cur_bt);
2486 if (cur_fill_num > ooa_hdr->alloc_num) {
2487 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2488 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2490 ooa_hdr->dropped_num = 0;
2491 ooa_hdr->status = CTL_OOA_OK;
2494 free(entries, M_CTL);
2497 case CTL_CHECK_OOA: {
2499 struct ctl_lun *lun;
2500 struct ctl_ooa_info *ooa_info;
2503 ooa_info = (struct ctl_ooa_info *)addr;
2505 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2506 ooa_info->status = CTL_OOA_INVALID_LUN;
2509 mtx_lock(&softc->ctl_lock);
2510 lun = softc->ctl_luns[ooa_info->lun_id];
2512 mtx_unlock(&softc->ctl_lock);
2513 ooa_info->status = CTL_OOA_INVALID_LUN;
2516 mtx_lock(&lun->lun_lock);
2517 mtx_unlock(&softc->ctl_lock);
2518 ooa_info->num_entries = 0;
2519 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2520 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2521 &io->io_hdr, ooa_links)) {
2522 ooa_info->num_entries++;
2524 mtx_unlock(&lun->lun_lock);
2526 ooa_info->status = CTL_OOA_SUCCESS;
2530 case CTL_HARD_START:
2531 case CTL_HARD_STOP: {
2532 struct ctl_fe_ioctl_startstop_info ss_info;
2533 struct cfi_metatask *metatask;
2536 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2538 cv_init(&ss_info.sem, "hard start/stop cv" );
2540 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2541 if (metatask == NULL) {
2543 mtx_destroy(&hs_mtx);
2547 if (cmd == CTL_HARD_START)
2548 metatask->tasktype = CFI_TASK_STARTUP;
2550 metatask->tasktype = CFI_TASK_SHUTDOWN;
2552 metatask->callback = ctl_ioctl_hard_startstop_callback;
2553 metatask->callback_arg = &ss_info;
2555 cfi_action(metatask);
2557 /* Wait for the callback */
2559 cv_wait_sig(&ss_info.sem, &hs_mtx);
2560 mtx_unlock(&hs_mtx);
2563 * All information has been copied from the metatask by the
2564 * time cv_broadcast() is called, so we free the metatask here.
2566 cfi_free_metatask(metatask);
2568 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2570 mtx_destroy(&hs_mtx);
2574 struct ctl_bbrread_info *bbr_info;
2575 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2577 struct cfi_metatask *metatask;
2579 bbr_info = (struct ctl_bbrread_info *)addr;
2581 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2583 bzero(&bbr_mtx, sizeof(bbr_mtx));
2584 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2586 fe_bbr_info.bbr_info = bbr_info;
2587 fe_bbr_info.lock = &bbr_mtx;
2589 cv_init(&fe_bbr_info.sem, "BBR read cv");
2590 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2592 if (metatask == NULL) {
2593 mtx_destroy(&bbr_mtx);
2594 cv_destroy(&fe_bbr_info.sem);
2598 metatask->tasktype = CFI_TASK_BBRREAD;
2599 metatask->callback = ctl_ioctl_bbrread_callback;
2600 metatask->callback_arg = &fe_bbr_info;
2601 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2602 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2603 metatask->taskinfo.bbrread.len = bbr_info->len;
2605 cfi_action(metatask);
2608 while (fe_bbr_info.wakeup_done == 0)
2609 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2610 mtx_unlock(&bbr_mtx);
2612 bbr_info->status = metatask->status;
2613 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2614 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2615 memcpy(&bbr_info->sense_data,
2616 &metatask->taskinfo.bbrread.sense_data,
2617 ctl_min(sizeof(bbr_info->sense_data),
2618 sizeof(metatask->taskinfo.bbrread.sense_data)));
2620 cfi_free_metatask(metatask);
2622 mtx_destroy(&bbr_mtx);
2623 cv_destroy(&fe_bbr_info.sem);
2627 case CTL_DELAY_IO: {
2628 struct ctl_io_delay_info *delay_info;
2630 struct ctl_lun *lun;
2631 #endif /* CTL_IO_DELAY */
2633 delay_info = (struct ctl_io_delay_info *)addr;
2636 mtx_lock(&softc->ctl_lock);
2638 if ((delay_info->lun_id > CTL_MAX_LUNS)
2639 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2640 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2642 lun = softc->ctl_luns[delay_info->lun_id];
2643 mtx_lock(&lun->lun_lock);
2645 delay_info->status = CTL_DELAY_STATUS_OK;
2647 switch (delay_info->delay_type) {
2648 case CTL_DELAY_TYPE_CONT:
2650 case CTL_DELAY_TYPE_ONESHOT:
2653 delay_info->status =
2654 CTL_DELAY_STATUS_INVALID_TYPE;
2658 switch (delay_info->delay_loc) {
2659 case CTL_DELAY_LOC_DATAMOVE:
2660 lun->delay_info.datamove_type =
2661 delay_info->delay_type;
2662 lun->delay_info.datamove_delay =
2663 delay_info->delay_secs;
2665 case CTL_DELAY_LOC_DONE:
2666 lun->delay_info.done_type =
2667 delay_info->delay_type;
2668 lun->delay_info.done_delay =
2669 delay_info->delay_secs;
2672 delay_info->status =
2673 CTL_DELAY_STATUS_INVALID_LOC;
2676 mtx_unlock(&lun->lun_lock);
2679 mtx_unlock(&softc->ctl_lock);
2681 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2682 #endif /* CTL_IO_DELAY */
2685 case CTL_REALSYNC_SET: {
2688 syncstate = (int *)addr;
2690 mtx_lock(&softc->ctl_lock);
2691 switch (*syncstate) {
2693 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2696 softc->flags |= CTL_FLAG_REAL_SYNC;
2702 mtx_unlock(&softc->ctl_lock);
2705 case CTL_REALSYNC_GET: {
2708 syncstate = (int*)addr;
2710 mtx_lock(&softc->ctl_lock);
2711 if (softc->flags & CTL_FLAG_REAL_SYNC)
2715 mtx_unlock(&softc->ctl_lock);
2721 struct ctl_sync_info *sync_info;
2722 struct ctl_lun *lun;
2724 sync_info = (struct ctl_sync_info *)addr;
2726 mtx_lock(&softc->ctl_lock);
2727 lun = softc->ctl_luns[sync_info->lun_id];
2729 mtx_unlock(&softc->ctl_lock);
2730 sync_info->status = CTL_GS_SYNC_NO_LUN;
2733 * Get or set the sync interval. We're not bounds checking
2734 * in the set case, hopefully the user won't do something
2737 mtx_lock(&lun->lun_lock);
2738 mtx_unlock(&softc->ctl_lock);
2739 if (cmd == CTL_GETSYNC)
2740 sync_info->sync_interval = lun->sync_interval;
2742 lun->sync_interval = sync_info->sync_interval;
2743 mtx_unlock(&lun->lun_lock);
2745 sync_info->status = CTL_GS_SYNC_OK;
2749 case CTL_GETSTATS: {
2750 struct ctl_stats *stats;
2751 struct ctl_lun *lun;
2754 stats = (struct ctl_stats *)addr;
2756 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2758 stats->status = CTL_SS_NEED_MORE_SPACE;
2759 stats->num_luns = softc->num_luns;
2763 * XXX KDM no locking here. If the LUN list changes,
2764 * things can blow up.
2766 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2767 i++, lun = STAILQ_NEXT(lun, links)) {
2768 retval = copyout(&lun->stats, &stats->lun_stats[i],
2769 sizeof(lun->stats));
2773 stats->num_luns = softc->num_luns;
2774 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2776 stats->status = CTL_SS_OK;
2778 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2780 stats->flags = CTL_STATS_FLAG_NONE;
2782 getnanouptime(&stats->timestamp);
2785 case CTL_ERROR_INJECT: {
2786 struct ctl_error_desc *err_desc, *new_err_desc;
2787 struct ctl_lun *lun;
2789 err_desc = (struct ctl_error_desc *)addr;
2791 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2793 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2795 mtx_lock(&softc->ctl_lock);
2796 lun = softc->ctl_luns[err_desc->lun_id];
2798 mtx_unlock(&softc->ctl_lock);
2799 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2800 __func__, (uintmax_t)err_desc->lun_id);
2804 mtx_lock(&lun->lun_lock);
2805 mtx_unlock(&softc->ctl_lock);
2808 * We could do some checking here to verify the validity
2809 * of the request, but given the complexity of error
2810 * injection requests, the checking logic would be fairly
2813 * For now, if the request is invalid, it just won't get
2814 * executed and might get deleted.
2816 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2819 * XXX KDM check to make sure the serial number is unique,
2820 * in case we somehow manage to wrap. That shouldn't
2821 * happen for a very long time, but it's the right thing to
2824 new_err_desc->serial = lun->error_serial;
2825 err_desc->serial = lun->error_serial;
2826 lun->error_serial++;
2828 mtx_unlock(&lun->lun_lock);
2831 case CTL_ERROR_INJECT_DELETE: {
2832 struct ctl_error_desc *delete_desc, *desc, *desc2;
2833 struct ctl_lun *lun;
2836 delete_desc = (struct ctl_error_desc *)addr;
2839 mtx_lock(&softc->ctl_lock);
2840 lun = softc->ctl_luns[delete_desc->lun_id];
2842 mtx_unlock(&softc->ctl_lock);
2843 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2844 __func__, (uintmax_t)delete_desc->lun_id);
2848 mtx_lock(&lun->lun_lock);
2849 mtx_unlock(&softc->ctl_lock);
2850 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2851 if (desc->serial != delete_desc->serial)
2854 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2859 mtx_unlock(&lun->lun_lock);
2860 if (delete_done == 0) {
2861 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2862 "error serial %ju on LUN %u\n", __func__,
2863 delete_desc->serial, delete_desc->lun_id);
2869 case CTL_DUMP_STRUCTS: {
2871 struct ctl_port *port;
2872 struct ctl_frontend *fe;
2874 printf("CTL IID to WWPN map start:\n");
2875 for (i = 0; i < CTL_MAX_PORTS; i++) {
2876 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2877 if (softc->wwpn_iid[i][j].in_use == 0)
2880 printf("port %d iid %u WWPN %#jx\n",
2881 softc->wwpn_iid[i][j].port,
2882 softc->wwpn_iid[i][j].iid,
2883 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2886 printf("CTL IID to WWPN map end\n");
2887 printf("CTL Persistent Reservation information start:\n");
2888 for (i = 0; i < CTL_MAX_LUNS; i++) {
2889 struct ctl_lun *lun;
2891 lun = softc->ctl_luns[i];
2894 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2897 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2898 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2899 if (lun->per_res[j+k].registered == 0)
2901 printf("LUN %d port %d iid %d key "
2903 (uintmax_t)scsi_8btou64(
2904 lun->per_res[j+k].res_key.key));
2908 printf("CTL Persistent Reservation information end\n");
2909 printf("CTL Ports:\n");
2911 * XXX KDM calling this without a lock. We'd likely want
2912 * to drop the lock before calling the frontend's dump
2915 STAILQ_FOREACH(port, &softc->port_list, links) {
2916 printf("Port %s Frontend %s Type %u pport %d vport %d WWNN "
2917 "%#jx WWPN %#jx\n", port->port_name,
2918 port->frontend->name, port->port_type,
2919 port->physical_port, port->virtual_port,
2920 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2922 printf("CTL Port information end\n");
2923 printf("CTL Frontends:\n");
2924 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2925 printf("Frontend %s\n", fe->name);
2926 if (fe->fe_dump != NULL)
2929 printf("CTL Frontend information end\n");
2933 struct ctl_lun_req *lun_req;
2934 struct ctl_backend_driver *backend;
2936 lun_req = (struct ctl_lun_req *)addr;
2938 backend = ctl_backend_find(lun_req->backend);
2939 if (backend == NULL) {
2940 lun_req->status = CTL_LUN_ERROR;
2941 snprintf(lun_req->error_str,
2942 sizeof(lun_req->error_str),
2943 "Backend \"%s\" not found.",
2947 if (lun_req->num_be_args > 0) {
2948 lun_req->kern_be_args = ctl_copyin_args(
2949 lun_req->num_be_args,
2952 sizeof(lun_req->error_str));
2953 if (lun_req->kern_be_args == NULL) {
2954 lun_req->status = CTL_LUN_ERROR;
2959 retval = backend->ioctl(dev, cmd, addr, flag, td);
2961 if (lun_req->num_be_args > 0) {
2962 ctl_copyout_args(lun_req->num_be_args,
2963 lun_req->kern_be_args);
2964 ctl_free_args(lun_req->num_be_args,
2965 lun_req->kern_be_args);
2969 case CTL_LUN_LIST: {
2971 struct ctl_lun *lun;
2972 struct ctl_lun_list *list;
2973 struct ctl_option *opt;
2975 list = (struct ctl_lun_list *)addr;
2978 * Allocate a fixed length sbuf here, based on the length
2979 * of the user's buffer. We could allocate an auto-extending
2980 * buffer, and then tell the user how much larger our
2981 * amount of data is than his buffer, but that presents
2984 * 1. The sbuf(9) routines use a blocking malloc, and so
2985 * we can't hold a lock while calling them with an
2986 * auto-extending buffer.
2988 * 2. There is not currently a LUN reference counting
2989 * mechanism, outside of outstanding transactions on
2990 * the LUN's OOA queue. So a LUN could go away on us
2991 * while we're getting the LUN number, backend-specific
2992 * information, etc. Thus, given the way things
2993 * currently work, we need to hold the CTL lock while
2994 * grabbing LUN information.
2996 * So, from the user's standpoint, the best thing to do is
2997 * allocate what he thinks is a reasonable buffer length,
2998 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2999 * double the buffer length and try again. (And repeat
3000 * that until he succeeds.)
3002 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3004 list->status = CTL_LUN_LIST_ERROR;
3005 snprintf(list->error_str, sizeof(list->error_str),
3006 "Unable to allocate %d bytes for LUN list",
3011 sbuf_printf(sb, "<ctllunlist>\n");
3013 mtx_lock(&softc->ctl_lock);
3014 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3015 mtx_lock(&lun->lun_lock);
3016 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3017 (uintmax_t)lun->lun);
3020 * Bail out as soon as we see that we've overfilled
3026 retval = sbuf_printf(sb, "\t<backend_type>%s"
3027 "</backend_type>\n",
3028 (lun->backend == NULL) ? "none" :
3029 lun->backend->name);
3034 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3035 lun->be_lun->lun_type);
3040 if (lun->backend == NULL) {
3041 retval = sbuf_printf(sb, "</lun>\n");
3047 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3048 (lun->be_lun->maxlba > 0) ?
3049 lun->be_lun->maxlba + 1 : 0);
3054 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3055 lun->be_lun->blocksize);
3060 retval = sbuf_printf(sb, "\t<serial_number>");
3065 retval = ctl_sbuf_printf_esc(sb,
3066 lun->be_lun->serial_num);
3071 retval = sbuf_printf(sb, "</serial_number>\n");
3076 retval = sbuf_printf(sb, "\t<device_id>");
3081 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3086 retval = sbuf_printf(sb, "</device_id>\n");
3091 if (lun->backend->lun_info != NULL) {
3092 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3096 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3097 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3098 opt->name, opt->value, opt->name);
3103 retval = sbuf_printf(sb, "</lun>\n");
3107 mtx_unlock(&lun->lun_lock);
3110 mtx_unlock(&lun->lun_lock);
3111 mtx_unlock(&softc->ctl_lock);
3114 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3117 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3118 snprintf(list->error_str, sizeof(list->error_str),
3119 "Out of space, %d bytes is too small",
3126 retval = copyout(sbuf_data(sb), list->lun_xml,
3129 list->fill_len = sbuf_len(sb) + 1;
3130 list->status = CTL_LUN_LIST_OK;
3135 struct ctl_iscsi *ci;
3136 struct ctl_frontend *fe;
3138 ci = (struct ctl_iscsi *)addr;
3140 fe = ctl_frontend_find("iscsi");
3142 ci->status = CTL_ISCSI_ERROR;
3143 snprintf(ci->error_str, sizeof(ci->error_str),
3144 "Frontend \"iscsi\" not found.");
3148 retval = fe->ioctl(dev, cmd, addr, flag, td);
3151 case CTL_PORT_REQ: {
3152 struct ctl_req *req;
3153 struct ctl_frontend *fe;
3155 req = (struct ctl_req *)addr;
3157 fe = ctl_frontend_find(req->driver);
3159 req->status = CTL_LUN_ERROR;
3160 snprintf(req->error_str, sizeof(req->error_str),
3161 "Frontend \"%s\" not found.", req->driver);
3164 if (req->num_args > 0) {
3165 req->kern_args = ctl_copyin_args(req->num_args,
3166 req->args, req->error_str, sizeof(req->error_str));
3167 if (req->kern_args == NULL) {
3168 req->status = CTL_LUN_ERROR;
3173 retval = fe->ioctl(dev, cmd, addr, flag, td);
3175 if (req->num_args > 0) {
3176 ctl_copyout_args(req->num_args, req->kern_args);
3177 ctl_free_args(req->num_args, req->kern_args);
3181 case CTL_PORT_LIST: {
3183 struct ctl_port *port;
3184 struct ctl_lun_list *list;
3185 struct ctl_option *opt;
3187 list = (struct ctl_lun_list *)addr;
3189 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3191 list->status = CTL_LUN_LIST_ERROR;
3192 snprintf(list->error_str, sizeof(list->error_str),
3193 "Unable to allocate %d bytes for LUN list",
3198 sbuf_printf(sb, "<ctlportlist>\n");
3200 mtx_lock(&softc->ctl_lock);
3201 STAILQ_FOREACH(port, &softc->port_list, links) {
3202 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3203 (uintmax_t)port->targ_port);
3206 * Bail out as soon as we see that we've overfilled
3212 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3213 "</frontend_type>\n", port->frontend->name);
3217 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3222 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3223 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3227 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3232 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3233 port->physical_port);
3237 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3238 port->virtual_port);
3242 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3243 (uintmax_t)port->wwnn);
3247 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3248 (uintmax_t)port->wwpn);
3252 STAILQ_FOREACH(opt, &port->options, links) {
3253 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3254 opt->name, opt->value, opt->name);
3259 retval = sbuf_printf(sb, "</targ_port>\n");
3263 mtx_unlock(&softc->ctl_lock);
3266 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3269 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3270 snprintf(list->error_str, sizeof(list->error_str),
3271 "Out of space, %d bytes is too small",
3278 retval = copyout(sbuf_data(sb), list->lun_xml,
3281 list->fill_len = sbuf_len(sb) + 1;
3282 list->status = CTL_LUN_LIST_OK;
3287 /* XXX KDM should we fix this? */
3289 struct ctl_backend_driver *backend;
3296 * We encode the backend type as the ioctl type for backend
3297 * ioctls. So parse it out here, and then search for a
3298 * backend of this type.
3300 type = _IOC_TYPE(cmd);
3302 STAILQ_FOREACH(backend, &softc->be_list, links) {
3303 if (backend->type == type) {
3309 printf("ctl: unknown ioctl command %#lx or backend "
3314 retval = backend->ioctl(dev, cmd, addr, flag, td);
3324 ctl_get_initindex(struct ctl_nexus *nexus)
3326 if (nexus->targ_port < CTL_MAX_PORTS)
3327 return (nexus->initid.id +
3328 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3330 return (nexus->initid.id +
3331 ((nexus->targ_port - CTL_MAX_PORTS) *
3332 CTL_MAX_INIT_PER_PORT));
3336 ctl_get_resindex(struct ctl_nexus *nexus)
3338 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3342 ctl_port_idx(int port_num)
3344 if (port_num < CTL_MAX_PORTS)
3347 return(port_num - CTL_MAX_PORTS);
3351 * Note: This only works for bitmask sizes that are at least 32 bits, and
3352 * that are a power of 2.
3355 ctl_ffz(uint32_t *mask, uint32_t size)
3357 uint32_t num_chunks, num_pieces;
3360 num_chunks = (size >> 5);
3361 if (num_chunks == 0)
3363 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3365 for (i = 0; i < num_chunks; i++) {
3366 for (j = 0; j < num_pieces; j++) {
3367 if ((mask[i] & (1 << j)) == 0)
3368 return ((i << 5) + j);
3376 ctl_set_mask(uint32_t *mask, uint32_t bit)
3378 uint32_t chunk, piece;
3381 piece = bit % (sizeof(uint32_t) * 8);
3383 if ((mask[chunk] & (1 << piece)) != 0)
3386 mask[chunk] |= (1 << piece);
3392 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3394 uint32_t chunk, piece;
3397 piece = bit % (sizeof(uint32_t) * 8);
3399 if ((mask[chunk] & (1 << piece)) == 0)
3402 mask[chunk] &= ~(1 << piece);
3408 ctl_is_set(uint32_t *mask, uint32_t bit)
3410 uint32_t chunk, piece;
3413 piece = bit % (sizeof(uint32_t) * 8);
3415 if ((mask[chunk] & (1 << piece)) == 0)
3423 * The bus, target and lun are optional, they can be filled in later.
3424 * can_wait is used to determine whether we can wait on the malloc or not.
3427 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3428 uint32_t targ_lun, int can_wait)
3433 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3435 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3438 io->io_hdr.io_type = io_type;
3439 io->io_hdr.targ_port = targ_port;
3441 * XXX KDM this needs to change/go away. We need to move
3442 * to a preallocated pool of ctl_scsiio structures.
3444 io->io_hdr.nexus.targ_target.id = targ_target;
3445 io->io_hdr.nexus.targ_lun = targ_lun;
3452 ctl_kfree_io(union ctl_io *io)
3459 * ctl_softc, pool_type, total_ctl_io are passed in.
3460 * npool is passed out.
3463 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3464 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3467 union ctl_io *cur_io, *next_io;
3468 struct ctl_io_pool *pool;
3473 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3480 pool->type = pool_type;
3481 pool->ctl_softc = ctl_softc;
3483 mtx_lock(&ctl_softc->pool_lock);
3484 pool->id = ctl_softc->cur_pool_id++;
3485 mtx_unlock(&ctl_softc->pool_lock);
3487 pool->flags = CTL_POOL_FLAG_NONE;
3488 pool->refcount = 1; /* Reference for validity. */
3489 STAILQ_INIT(&pool->free_queue);
3492 * XXX KDM other options here:
3493 * - allocate a page at a time
3494 * - allocate one big chunk of memory.
3495 * Page allocation might work well, but would take a little more
3498 for (i = 0; i < total_ctl_io; i++) {
3499 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3501 if (cur_io == NULL) {
3505 cur_io->io_hdr.pool = pool;
3506 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3507 pool->total_ctl_io++;
3508 pool->free_ctl_io++;
3512 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3513 cur_io != NULL; cur_io = next_io) {
3514 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3516 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3518 free(cur_io, M_CTLIO);
3524 mtx_lock(&ctl_softc->pool_lock);
3525 ctl_softc->num_pools++;
3526 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3528 * Increment our usage count if this is an external consumer, so we
3529 * can't get unloaded until the external consumer (most likely a
3530 * FETD) unloads and frees his pool.
3532 * XXX KDM will this increment the caller's module use count, or
3536 if ((pool_type != CTL_POOL_EMERGENCY)
3537 && (pool_type != CTL_POOL_INTERNAL)
3538 && (pool_type != CTL_POOL_4OTHERSC))
3542 mtx_unlock(&ctl_softc->pool_lock);
3552 ctl_pool_acquire(struct ctl_io_pool *pool)
3555 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3557 if (pool->flags & CTL_POOL_FLAG_INVALID)
3566 ctl_pool_release(struct ctl_io_pool *pool)
3568 struct ctl_softc *ctl_softc = pool->ctl_softc;
3571 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3573 if (--pool->refcount != 0)
3576 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3577 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3582 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3583 ctl_softc->num_pools--;
3586 * XXX KDM will this decrement the caller's usage count or mine?
3589 if ((pool->type != CTL_POOL_EMERGENCY)
3590 && (pool->type != CTL_POOL_INTERNAL)
3591 && (pool->type != CTL_POOL_4OTHERSC))
3599 ctl_pool_free(struct ctl_io_pool *pool)
3601 struct ctl_softc *ctl_softc;
3606 ctl_softc = pool->ctl_softc;
3607 mtx_lock(&ctl_softc->pool_lock);
3608 pool->flags |= CTL_POOL_FLAG_INVALID;
3609 ctl_pool_release(pool);
3610 mtx_unlock(&ctl_softc->pool_lock);
3614 * This routine does not block (except for spinlocks of course).
3615 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3619 ctl_alloc_io(void *pool_ref)
3622 struct ctl_softc *ctl_softc;
3623 struct ctl_io_pool *pool, *npool;
3624 struct ctl_io_pool *emergency_pool;
3626 pool = (struct ctl_io_pool *)pool_ref;
3629 printf("%s: pool is NULL\n", __func__);
3633 emergency_pool = NULL;
3635 ctl_softc = pool->ctl_softc;
3637 mtx_lock(&ctl_softc->pool_lock);
3639 * First, try to get the io structure from the user's pool.
3641 if (ctl_pool_acquire(pool) == 0) {
3642 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3644 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3645 pool->total_allocated++;
3646 pool->free_ctl_io--;
3647 mtx_unlock(&ctl_softc->pool_lock);
3650 ctl_pool_release(pool);
3653 * If he doesn't have any io structures left, search for an
3654 * emergency pool and grab one from there.
3656 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3657 if (npool->type != CTL_POOL_EMERGENCY)
3660 if (ctl_pool_acquire(npool) != 0)
3663 emergency_pool = npool;
3665 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3667 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3668 npool->total_allocated++;
3669 npool->free_ctl_io--;
3670 mtx_unlock(&ctl_softc->pool_lock);
3673 ctl_pool_release(npool);
3676 /* Drop the spinlock before we malloc */
3677 mtx_unlock(&ctl_softc->pool_lock);
3680 * The emergency pool (if it exists) didn't have one, so try an
3681 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3683 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3686 * If the emergency pool exists but is empty, add this
3687 * ctl_io to its list when it gets freed.
3689 if (emergency_pool != NULL) {
3690 mtx_lock(&ctl_softc->pool_lock);
3691 if (ctl_pool_acquire(emergency_pool) == 0) {
3692 io->io_hdr.pool = emergency_pool;
3693 emergency_pool->total_ctl_io++;
3695 * Need to bump this, otherwise
3696 * total_allocated and total_freed won't
3697 * match when we no longer have anything
3700 emergency_pool->total_allocated++;
3702 mtx_unlock(&ctl_softc->pool_lock);
3704 io->io_hdr.pool = NULL;
3711 ctl_free_io(union ctl_io *io)
3717 * If this ctl_io has a pool, return it to that pool.
3719 if (io->io_hdr.pool != NULL) {
3720 struct ctl_io_pool *pool;
3722 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3723 mtx_lock(&pool->ctl_softc->pool_lock);
3724 io->io_hdr.io_type = 0xff;
3725 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3726 pool->total_freed++;
3727 pool->free_ctl_io++;
3728 ctl_pool_release(pool);
3729 mtx_unlock(&pool->ctl_softc->pool_lock);
3732 * Otherwise, just free it. We probably malloced it and
3733 * the emergency pool wasn't available.
3741 ctl_zero_io(union ctl_io *io)
3749 * May need to preserve linked list pointers at some point too.
3751 pool_ref = io->io_hdr.pool;
3753 memset(io, 0, sizeof(*io));
3755 io->io_hdr.pool = pool_ref;
3759 * This routine is currently used for internal copies of ctl_ios that need
3760 * to persist for some reason after we've already returned status to the
3761 * FETD. (Thus the flag set.)
3764 * Note that this makes a blind copy of all fields in the ctl_io, except
3765 * for the pool reference. This includes any memory that has been
3766 * allocated! That memory will no longer be valid after done has been
3767 * called, so this would be VERY DANGEROUS for command that actually does
3768 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3769 * start and stop commands, which don't transfer any data, so this is not a
3770 * problem. If it is used for anything else, the caller would also need to
3771 * allocate data buffer space and this routine would need to be modified to
3772 * copy the data buffer(s) as well.
3775 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3784 * May need to preserve linked list pointers at some point too.
3786 pool_ref = dest->io_hdr.pool;
3788 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3790 dest->io_hdr.pool = pool_ref;
3792 * We need to know that this is an internal copy, and doesn't need
3793 * to get passed back to the FETD that allocated it.
3795 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3800 ctl_update_power_subpage(struct copan_power_subpage *page)
3802 int num_luns, num_partitions, config_type;
3803 struct ctl_softc *softc;
3804 cs_BOOL_t aor_present, shelf_50pct_power;
3805 cs_raidset_personality_t rs_type;
3806 int max_active_luns;
3808 softc = control_softc;
3810 /* subtract out the processor LUN */
3811 num_luns = softc->num_luns - 1;
3813 * Default to 7 LUNs active, which was the only number we allowed
3816 max_active_luns = 7;
3818 num_partitions = config_GetRsPartitionInfo();
3819 config_type = config_GetConfigType();
3820 shelf_50pct_power = config_GetShelfPowerMode();
3821 aor_present = config_IsAorRsPresent();
3823 rs_type = ddb_GetRsRaidType(1);
3824 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3825 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3826 EPRINT(0, "Unsupported RS type %d!", rs_type);
3830 page->total_luns = num_luns;
3832 switch (config_type) {
3835 * In a 40 drive configuration, it doesn't matter what DC
3836 * cards we have, whether we have AOR enabled or not,
3837 * partitioning or not, or what type of RAIDset we have.
3838 * In that scenario, we can power up every LUN we present
3841 max_active_luns = num_luns;
3845 if (shelf_50pct_power == CS_FALSE) {
3847 if (aor_present == CS_TRUE) {
3849 CS_RAIDSET_PERSONALITY_RAID5) {
3850 max_active_luns = 7;
3851 } else if (rs_type ==
3852 CS_RAIDSET_PERSONALITY_RAID1){
3853 max_active_luns = 14;
3855 /* XXX KDM now what?? */
3859 CS_RAIDSET_PERSONALITY_RAID5) {
3860 max_active_luns = 8;
3861 } else if (rs_type ==
3862 CS_RAIDSET_PERSONALITY_RAID1){
3863 max_active_luns = 16;
3865 /* XXX KDM now what?? */
3871 * With 50% power in a 64 drive configuration, we
3872 * can power all LUNs we present.
3874 max_active_luns = num_luns;
3878 if (shelf_50pct_power == CS_FALSE) {
3880 if (aor_present == CS_TRUE) {
3882 CS_RAIDSET_PERSONALITY_RAID5) {
3883 max_active_luns = 7;
3884 } else if (rs_type ==
3885 CS_RAIDSET_PERSONALITY_RAID1){
3886 max_active_luns = 14;
3888 /* XXX KDM now what?? */
3892 CS_RAIDSET_PERSONALITY_RAID5) {
3893 max_active_luns = 8;
3894 } else if (rs_type ==
3895 CS_RAIDSET_PERSONALITY_RAID1){
3896 max_active_luns = 16;
3898 /* XXX KDM now what?? */
3903 if (aor_present == CS_TRUE) {
3905 CS_RAIDSET_PERSONALITY_RAID5) {
3906 max_active_luns = 14;
3907 } else if (rs_type ==
3908 CS_RAIDSET_PERSONALITY_RAID1){
3910 * We're assuming here that disk
3911 * caching is enabled, and so we're
3912 * able to power up half of each
3913 * LUN, and cache all writes.
3915 max_active_luns = num_luns;
3917 /* XXX KDM now what?? */
3921 CS_RAIDSET_PERSONALITY_RAID5) {
3922 max_active_luns = 15;
3923 } else if (rs_type ==
3924 CS_RAIDSET_PERSONALITY_RAID1){
3925 max_active_luns = 30;
3927 /* XXX KDM now what?? */
3934 * In this case, we have an unknown configuration, so we
3935 * just use the default from above.
3940 page->max_active_luns = max_active_luns;
3942 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3943 page->total_luns, page->max_active_luns);
3946 #endif /* NEEDTOPORT */
3949 * This routine could be used in the future to load default and/or saved
3950 * mode page parameters for a particuar lun.
3953 ctl_init_page_index(struct ctl_lun *lun)
3956 struct ctl_page_index *page_index;
3957 struct ctl_softc *softc;
3959 memcpy(&lun->mode_pages.index, page_index_template,
3960 sizeof(page_index_template));
3962 softc = lun->ctl_softc;
3964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3966 page_index = &lun->mode_pages.index[i];
3968 * If this is a disk-only mode page, there's no point in
3969 * setting it up. For some pages, we have to have some
3970 * basic information about the disk in order to calculate the
3973 if ((lun->be_lun->lun_type != T_DIRECT)
3974 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3977 switch (page_index->page_code & SMPH_PC_MASK) {
3978 case SMS_FORMAT_DEVICE_PAGE: {
3979 struct scsi_format_page *format_page;
3981 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3982 panic("subpage is incorrect!");
3985 * Sectors per track are set above. Bytes per
3986 * sector need to be set here on a per-LUN basis.
3988 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3989 &format_page_default,
3990 sizeof(format_page_default));
3991 memcpy(&lun->mode_pages.format_page[
3992 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3993 sizeof(format_page_changeable));
3994 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3995 &format_page_default,
3996 sizeof(format_page_default));
3997 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3998 &format_page_default,
3999 sizeof(format_page_default));
4001 format_page = &lun->mode_pages.format_page[
4003 scsi_ulto2b(lun->be_lun->blocksize,
4004 format_page->bytes_per_sector);
4006 format_page = &lun->mode_pages.format_page[
4008 scsi_ulto2b(lun->be_lun->blocksize,
4009 format_page->bytes_per_sector);
4011 format_page = &lun->mode_pages.format_page[
4013 scsi_ulto2b(lun->be_lun->blocksize,
4014 format_page->bytes_per_sector);
4016 page_index->page_data =
4017 (uint8_t *)lun->mode_pages.format_page;
4020 case SMS_RIGID_DISK_PAGE: {
4021 struct scsi_rigid_disk_page *rigid_disk_page;
4022 uint32_t sectors_per_cylinder;
4026 #endif /* !__XSCALE__ */
4028 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4029 panic("invalid subpage value %d",
4030 page_index->subpage);
4033 * Rotation rate and sectors per track are set
4034 * above. We calculate the cylinders here based on
4035 * capacity. Due to the number of heads and
4036 * sectors per track we're using, smaller arrays
4037 * may turn out to have 0 cylinders. Linux and
4038 * FreeBSD don't pay attention to these mode pages
4039 * to figure out capacity, but Solaris does. It
4040 * seems to deal with 0 cylinders just fine, and
4041 * works out a fake geometry based on the capacity.
4043 memcpy(&lun->mode_pages.rigid_disk_page[
4044 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4045 sizeof(rigid_disk_page_default));
4046 memcpy(&lun->mode_pages.rigid_disk_page[
4047 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4048 sizeof(rigid_disk_page_changeable));
4049 memcpy(&lun->mode_pages.rigid_disk_page[
4050 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4051 sizeof(rigid_disk_page_default));
4052 memcpy(&lun->mode_pages.rigid_disk_page[
4053 CTL_PAGE_SAVED], &rigid_disk_page_default,
4054 sizeof(rigid_disk_page_default));
4056 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4060 * The divide method here will be more accurate,
4061 * probably, but results in floating point being
4062 * used in the kernel on i386 (__udivdi3()). On the
4063 * XScale, though, __udivdi3() is implemented in
4066 * The shift method for cylinder calculation is
4067 * accurate if sectors_per_cylinder is a power of
4068 * 2. Otherwise it might be slightly off -- you
4069 * might have a bit of a truncation problem.
4072 cylinders = (lun->be_lun->maxlba + 1) /
4073 sectors_per_cylinder;
4075 for (shift = 31; shift > 0; shift--) {
4076 if (sectors_per_cylinder & (1 << shift))
4079 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4083 * We've basically got 3 bytes, or 24 bits for the
4084 * cylinder size in the mode page. If we're over,
4085 * just round down to 2^24.
4087 if (cylinders > 0xffffff)
4088 cylinders = 0xffffff;
4090 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4092 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4094 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4096 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4098 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4100 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4102 page_index->page_data =
4103 (uint8_t *)lun->mode_pages.rigid_disk_page;
4106 case SMS_CACHING_PAGE: {
4108 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4109 panic("invalid subpage value %d",
4110 page_index->subpage);
4112 * Defaults should be okay here, no calculations
4115 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4116 &caching_page_default,
4117 sizeof(caching_page_default));
4118 memcpy(&lun->mode_pages.caching_page[
4119 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4120 sizeof(caching_page_changeable));
4121 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4122 &caching_page_default,
4123 sizeof(caching_page_default));
4124 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4125 &caching_page_default,
4126 sizeof(caching_page_default));
4127 page_index->page_data =
4128 (uint8_t *)lun->mode_pages.caching_page;
4131 case SMS_CONTROL_MODE_PAGE: {
4133 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4134 panic("invalid subpage value %d",
4135 page_index->subpage);
4138 * Defaults should be okay here, no calculations
4141 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4142 &control_page_default,
4143 sizeof(control_page_default));
4144 memcpy(&lun->mode_pages.control_page[
4145 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4146 sizeof(control_page_changeable));
4147 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4148 &control_page_default,
4149 sizeof(control_page_default));
4150 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4151 &control_page_default,
4152 sizeof(control_page_default));
4153 page_index->page_data =
4154 (uint8_t *)lun->mode_pages.control_page;
4158 case SMS_VENDOR_SPECIFIC_PAGE:{
4159 switch (page_index->subpage) {
4160 case PWR_SUBPAGE_CODE: {
4161 struct copan_power_subpage *current_page,
4164 memcpy(&lun->mode_pages.power_subpage[
4166 &power_page_default,
4167 sizeof(power_page_default));
4168 memcpy(&lun->mode_pages.power_subpage[
4169 CTL_PAGE_CHANGEABLE],
4170 &power_page_changeable,
4171 sizeof(power_page_changeable));
4172 memcpy(&lun->mode_pages.power_subpage[
4174 &power_page_default,
4175 sizeof(power_page_default));
4176 memcpy(&lun->mode_pages.power_subpage[
4178 &power_page_default,
4179 sizeof(power_page_default));
4180 page_index->page_data =
4181 (uint8_t *)lun->mode_pages.power_subpage;
4183 current_page = (struct copan_power_subpage *)
4184 (page_index->page_data +
4185 (page_index->page_len *
4187 saved_page = (struct copan_power_subpage *)
4188 (page_index->page_data +
4189 (page_index->page_len *
4193 case APS_SUBPAGE_CODE: {
4194 struct copan_aps_subpage *current_page,
4197 // This gets set multiple times but
4198 // it should always be the same. It's
4199 // only done during init so who cares.
4200 index_to_aps_page = i;
4202 memcpy(&lun->mode_pages.aps_subpage[
4205 sizeof(aps_page_default));
4206 memcpy(&lun->mode_pages.aps_subpage[
4207 CTL_PAGE_CHANGEABLE],
4208 &aps_page_changeable,
4209 sizeof(aps_page_changeable));
4210 memcpy(&lun->mode_pages.aps_subpage[
4213 sizeof(aps_page_default));
4214 memcpy(&lun->mode_pages.aps_subpage[
4217 sizeof(aps_page_default));
4218 page_index->page_data =
4219 (uint8_t *)lun->mode_pages.aps_subpage;
4221 current_page = (struct copan_aps_subpage *)
4222 (page_index->page_data +
4223 (page_index->page_len *
4225 saved_page = (struct copan_aps_subpage *)
4226 (page_index->page_data +
4227 (page_index->page_len *
4231 case DBGCNF_SUBPAGE_CODE: {
4232 struct copan_debugconf_subpage *current_page,
4235 memcpy(&lun->mode_pages.debugconf_subpage[
4237 &debugconf_page_default,
4238 sizeof(debugconf_page_default));
4239 memcpy(&lun->mode_pages.debugconf_subpage[
4240 CTL_PAGE_CHANGEABLE],
4241 &debugconf_page_changeable,
4242 sizeof(debugconf_page_changeable));
4243 memcpy(&lun->mode_pages.debugconf_subpage[
4245 &debugconf_page_default,
4246 sizeof(debugconf_page_default));
4247 memcpy(&lun->mode_pages.debugconf_subpage[
4249 &debugconf_page_default,
4250 sizeof(debugconf_page_default));
4251 page_index->page_data =
4252 (uint8_t *)lun->mode_pages.debugconf_subpage;
4254 current_page = (struct copan_debugconf_subpage *)
4255 (page_index->page_data +
4256 (page_index->page_len *
4258 saved_page = (struct copan_debugconf_subpage *)
4259 (page_index->page_data +
4260 (page_index->page_len *
4265 panic("invalid subpage value %d",
4266 page_index->subpage);
4272 panic("invalid page value %d",
4273 page_index->page_code & SMPH_PC_MASK);
4278 return (CTL_RETVAL_COMPLETE);
4285 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4286 * wants us to allocate the LUN and he can block.
4287 * - ctl_softc is always set
4288 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4290 * Returns 0 for success, non-zero (errno) for failure.
4293 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4294 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4296 struct ctl_lun *nlun, *lun;
4297 struct ctl_port *port;
4298 struct scsi_vpd_id_descriptor *desc;
4299 struct scsi_vpd_id_t10 *t10id;
4300 const char *scsiname, *vendor;
4301 int lun_number, i, lun_malloced;
4302 int devidlen, idlen1, idlen2, len;
4308 * We currently only support Direct Access or Processor LUN types.
4310 switch (be_lun->lun_type) {
4318 be_lun->lun_config_status(be_lun->be_lun,
4319 CTL_LUN_CONFIG_FAILURE);
4322 if (ctl_lun == NULL) {
4323 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4330 memset(lun, 0, sizeof(*lun));
4332 lun->flags = CTL_LUN_MALLOCED;
4334 /* Generate LUN ID. */
4335 devidlen = max(CTL_DEVID_MIN_LEN,
4336 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4337 idlen1 = sizeof(*t10id) + devidlen;
4338 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4339 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4340 if (scsiname != NULL) {
4341 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4342 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4344 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4345 M_CTL, M_WAITOK | M_ZERO);
4346 lun->lun_devid->len = len;
4347 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4348 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4349 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4350 desc->length = idlen1;
4351 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4352 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4353 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4354 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4356 strncpy(t10id->vendor, vendor,
4357 min(sizeof(t10id->vendor), strlen(vendor)));
4359 strncpy((char *)t10id->vendor_spec_id,
4360 (char *)be_lun->device_id, devidlen);
4361 if (scsiname != NULL) {
4362 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4364 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4365 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4366 SVPD_ID_TYPE_SCSI_NAME;
4367 desc->length = idlen2;
4368 strlcpy(desc->identifier, scsiname, idlen2);
4371 mtx_lock(&ctl_softc->ctl_lock);
4373 * See if the caller requested a particular LUN number. If so, see
4374 * if it is available. Otherwise, allocate the first available LUN.
4376 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4377 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4378 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4379 mtx_unlock(&ctl_softc->ctl_lock);
4380 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4381 printf("ctl: requested LUN ID %d is higher "
4382 "than CTL_MAX_LUNS - 1 (%d)\n",
4383 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4386 * XXX KDM return an error, or just assign
4387 * another LUN ID in this case??
4389 printf("ctl: requested LUN ID %d is already "
4390 "in use\n", be_lun->req_lun_id);
4392 if (lun->flags & CTL_LUN_MALLOCED)
4394 be_lun->lun_config_status(be_lun->be_lun,
4395 CTL_LUN_CONFIG_FAILURE);
4398 lun_number = be_lun->req_lun_id;
4400 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4401 if (lun_number == -1) {
4402 mtx_unlock(&ctl_softc->ctl_lock);
4403 printf("ctl: can't allocate LUN on target %ju, out of "
4404 "LUNs\n", (uintmax_t)target_id.id);
4405 if (lun->flags & CTL_LUN_MALLOCED)
4407 be_lun->lun_config_status(be_lun->be_lun,
4408 CTL_LUN_CONFIG_FAILURE);
4412 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4414 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4415 lun->target = target_id;
4416 lun->lun = lun_number;
4417 lun->be_lun = be_lun;
4419 * The processor LUN is always enabled. Disk LUNs come on line
4420 * disabled, and must be enabled by the backend.
4422 lun->flags |= CTL_LUN_DISABLED;
4423 lun->backend = be_lun->be;
4424 be_lun->ctl_lun = lun;
4425 be_lun->lun_id = lun_number;
4426 atomic_add_int(&be_lun->be->num_luns, 1);
4427 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4428 lun->flags |= CTL_LUN_STOPPED;
4430 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4431 lun->flags |= CTL_LUN_INOPERABLE;
4433 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4434 lun->flags |= CTL_LUN_PRIMARY_SC;
4436 lun->ctl_softc = ctl_softc;
4437 TAILQ_INIT(&lun->ooa_queue);
4438 TAILQ_INIT(&lun->blocked_queue);
4439 STAILQ_INIT(&lun->error_list);
4442 * Initialize the mode page index.
4444 ctl_init_page_index(lun);
4447 * Set the poweron UA for all initiators on this LUN only.
4449 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4450 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4453 * Now, before we insert this lun on the lun list, set the lun
4454 * inventory changed UA for all other luns.
4456 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4457 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4458 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4462 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4464 ctl_softc->ctl_luns[lun_number] = lun;
4466 ctl_softc->num_luns++;
4468 /* Setup statistics gathering */
4469 lun->stats.device_type = be_lun->lun_type;
4470 lun->stats.lun_number = lun_number;
4471 if (lun->stats.device_type == T_DIRECT)
4472 lun->stats.blocksize = be_lun->blocksize;
4474 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4475 for (i = 0;i < CTL_MAX_PORTS;i++)
4476 lun->stats.ports[i].targ_port = i;
4478 mtx_unlock(&ctl_softc->ctl_lock);
4480 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4483 * Run through each registered FETD and bring it online if it isn't
4484 * already. Enable the target ID if it hasn't been enabled, and
4485 * enable this particular LUN.
4487 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4490 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4492 printf("ctl_alloc_lun: FETD %s port %d returned error "
4493 "%d for lun_enable on target %ju lun %d\n",
4494 port->port_name, port->targ_port, retval,
4495 (uintmax_t)target_id.id, lun_number);
4497 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4505 * - LUN has already been marked invalid and any pending I/O has been taken
4509 ctl_free_lun(struct ctl_lun *lun)
4511 struct ctl_softc *softc;
4513 struct ctl_port *port;
4515 struct ctl_lun *nlun;
4518 softc = lun->ctl_softc;
4520 mtx_assert(&softc->ctl_lock, MA_OWNED);
4522 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4524 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4526 softc->ctl_luns[lun->lun] = NULL;
4528 if (!TAILQ_EMPTY(&lun->ooa_queue))
4529 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4534 * XXX KDM this scheme only works for a single target/multiple LUN
4535 * setup. It needs to be revamped for a multiple target scheme.
4537 * XXX KDM this results in port->lun_disable() getting called twice,
4538 * once when ctl_disable_lun() is called, and a second time here.
4539 * We really need to re-think the LUN disable semantics. There
4540 * should probably be several steps/levels to LUN removal:
4545 * Right now we only have a disable method when communicating to
4546 * the front end ports, at least for individual LUNs.
4549 STAILQ_FOREACH(port, &softc->port_list, links) {
4552 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4555 printf("ctl_free_lun: FETD %s port %d returned error "
4556 "%d for lun_disable on target %ju lun %jd\n",
4557 port->port_name, port->targ_port, retval,
4558 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4561 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4562 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4564 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4566 printf("ctl_free_lun: FETD %s port %d "
4567 "returned error %d for targ_disable on "
4568 "target %ju\n", port->port_name,
4569 port->targ_port, retval,
4570 (uintmax_t)lun->target.id);
4572 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4574 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4578 port->port_offline(port->onoff_arg);
4579 port->status &= ~CTL_PORT_STATUS_ONLINE;
4586 * Tell the backend to free resources, if this LUN has a backend.
4588 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4589 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4591 mtx_destroy(&lun->lun_lock);
4592 free(lun->lun_devid, M_CTL);
4593 if (lun->flags & CTL_LUN_MALLOCED)
4596 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4597 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4598 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4606 ctl_create_lun(struct ctl_be_lun *be_lun)
4608 struct ctl_softc *ctl_softc;
4610 ctl_softc = control_softc;
4613 * ctl_alloc_lun() should handle all potential failure cases.
4615 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4619 ctl_add_lun(struct ctl_be_lun *be_lun)
4621 struct ctl_softc *ctl_softc = control_softc;
4623 mtx_lock(&ctl_softc->ctl_lock);
4624 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4625 mtx_unlock(&ctl_softc->ctl_lock);
4626 wakeup(&ctl_softc->pending_lun_queue);
4632 ctl_enable_lun(struct ctl_be_lun *be_lun)
4634 struct ctl_softc *ctl_softc;
4635 struct ctl_port *port, *nport;
4636 struct ctl_lun *lun;
4639 ctl_softc = control_softc;
4641 lun = (struct ctl_lun *)be_lun->ctl_lun;
4643 mtx_lock(&ctl_softc->ctl_lock);
4644 mtx_lock(&lun->lun_lock);
4645 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4647 * eh? Why did we get called if the LUN is already
4650 mtx_unlock(&lun->lun_lock);
4651 mtx_unlock(&ctl_softc->ctl_lock);
4654 lun->flags &= ~CTL_LUN_DISABLED;
4655 mtx_unlock(&lun->lun_lock);
4657 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4658 nport = STAILQ_NEXT(port, links);
4661 * Drop the lock while we call the FETD's enable routine.
4662 * This can lead to a callback into CTL (at least in the
4663 * case of the internal initiator frontend.
4665 mtx_unlock(&ctl_softc->ctl_lock);
4666 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4667 mtx_lock(&ctl_softc->ctl_lock);
4669 printf("%s: FETD %s port %d returned error "
4670 "%d for lun_enable on target %ju lun %jd\n",
4671 __func__, port->port_name, port->targ_port, retval,
4672 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4676 /* NOTE: TODO: why does lun enable affect port status? */
4677 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4682 mtx_unlock(&ctl_softc->ctl_lock);
4688 ctl_disable_lun(struct ctl_be_lun *be_lun)
4690 struct ctl_softc *ctl_softc;
4691 struct ctl_port *port;
4692 struct ctl_lun *lun;
4695 ctl_softc = control_softc;
4697 lun = (struct ctl_lun *)be_lun->ctl_lun;
4699 mtx_lock(&ctl_softc->ctl_lock);
4700 mtx_lock(&lun->lun_lock);
4701 if (lun->flags & CTL_LUN_DISABLED) {
4702 mtx_unlock(&lun->lun_lock);
4703 mtx_unlock(&ctl_softc->ctl_lock);
4706 lun->flags |= CTL_LUN_DISABLED;
4707 mtx_unlock(&lun->lun_lock);
4709 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4710 mtx_unlock(&ctl_softc->ctl_lock);
4712 * Drop the lock before we call the frontend's disable
4713 * routine, to avoid lock order reversals.
4715 * XXX KDM what happens if the frontend list changes while
4716 * we're traversing it? It's unlikely, but should be handled.
4718 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4720 mtx_lock(&ctl_softc->ctl_lock);
4722 printf("ctl_alloc_lun: FETD %s port %d returned error "
4723 "%d for lun_disable on target %ju lun %jd\n",
4724 port->port_name, port->targ_port, retval,
4725 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4729 mtx_unlock(&ctl_softc->ctl_lock);
4735 ctl_start_lun(struct ctl_be_lun *be_lun)
4737 struct ctl_softc *ctl_softc;
4738 struct ctl_lun *lun;
4740 ctl_softc = control_softc;
4742 lun = (struct ctl_lun *)be_lun->ctl_lun;
4744 mtx_lock(&lun->lun_lock);
4745 lun->flags &= ~CTL_LUN_STOPPED;
4746 mtx_unlock(&lun->lun_lock);
4752 ctl_stop_lun(struct ctl_be_lun *be_lun)
4754 struct ctl_softc *ctl_softc;
4755 struct ctl_lun *lun;
4757 ctl_softc = control_softc;
4759 lun = (struct ctl_lun *)be_lun->ctl_lun;
4761 mtx_lock(&lun->lun_lock);
4762 lun->flags |= CTL_LUN_STOPPED;
4763 mtx_unlock(&lun->lun_lock);
4769 ctl_lun_offline(struct ctl_be_lun *be_lun)
4771 struct ctl_softc *ctl_softc;
4772 struct ctl_lun *lun;
4774 ctl_softc = control_softc;
4776 lun = (struct ctl_lun *)be_lun->ctl_lun;
4778 mtx_lock(&lun->lun_lock);
4779 lun->flags |= CTL_LUN_OFFLINE;
4780 mtx_unlock(&lun->lun_lock);
4786 ctl_lun_online(struct ctl_be_lun *be_lun)
4788 struct ctl_softc *ctl_softc;
4789 struct ctl_lun *lun;
4791 ctl_softc = control_softc;
4793 lun = (struct ctl_lun *)be_lun->ctl_lun;
4795 mtx_lock(&lun->lun_lock);
4796 lun->flags &= ~CTL_LUN_OFFLINE;
4797 mtx_unlock(&lun->lun_lock);
4803 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4805 struct ctl_softc *ctl_softc;
4806 struct ctl_lun *lun;
4808 ctl_softc = control_softc;
4810 lun = (struct ctl_lun *)be_lun->ctl_lun;
4812 mtx_lock(&lun->lun_lock);
4815 * The LUN needs to be disabled before it can be marked invalid.
4817 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4818 mtx_unlock(&lun->lun_lock);
4822 * Mark the LUN invalid.
4824 lun->flags |= CTL_LUN_INVALID;
4827 * If there is nothing in the OOA queue, go ahead and free the LUN.
4828 * If we have something in the OOA queue, we'll free it when the
4829 * last I/O completes.
4831 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4832 mtx_unlock(&lun->lun_lock);
4833 mtx_lock(&ctl_softc->ctl_lock);
4835 mtx_unlock(&ctl_softc->ctl_lock);
4837 mtx_unlock(&lun->lun_lock);
4843 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4845 struct ctl_softc *ctl_softc;
4846 struct ctl_lun *lun;
4848 ctl_softc = control_softc;
4849 lun = (struct ctl_lun *)be_lun->ctl_lun;
4851 mtx_lock(&lun->lun_lock);
4852 lun->flags |= CTL_LUN_INOPERABLE;
4853 mtx_unlock(&lun->lun_lock);
4859 ctl_lun_operable(struct ctl_be_lun *be_lun)
4861 struct ctl_softc *ctl_softc;
4862 struct ctl_lun *lun;
4864 ctl_softc = control_softc;
4865 lun = (struct ctl_lun *)be_lun->ctl_lun;
4867 mtx_lock(&lun->lun_lock);
4868 lun->flags &= ~CTL_LUN_INOPERABLE;
4869 mtx_unlock(&lun->lun_lock);
4875 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4878 struct ctl_softc *softc;
4879 struct ctl_lun *lun;
4880 struct copan_aps_subpage *current_sp;
4881 struct ctl_page_index *page_index;
4884 softc = control_softc;
4886 mtx_lock(&softc->ctl_lock);
4888 lun = (struct ctl_lun *)be_lun->ctl_lun;
4889 mtx_lock(&lun->lun_lock);
4892 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4893 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4897 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4899 page_index = &lun->mode_pages.index[i];
4902 if (page_index == NULL) {
4903 mtx_unlock(&lun->lun_lock);
4904 mtx_unlock(&softc->ctl_lock);
4905 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4906 (uintmax_t)lun->lun);
4910 if ((softc->aps_locked_lun != 0)
4911 && (softc->aps_locked_lun != lun->lun)) {
4912 printf("%s: attempt to lock LUN %llu when %llu is already "
4914 mtx_unlock(&lun->lun_lock);
4915 mtx_unlock(&softc->ctl_lock);
4920 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4921 (page_index->page_len * CTL_PAGE_CURRENT));
4924 current_sp->lock_active = APS_LOCK_ACTIVE;
4925 softc->aps_locked_lun = lun->lun;
4927 current_sp->lock_active = 0;
4928 softc->aps_locked_lun = 0;
4933 * If we're in HA mode, try to send the lock message to the other
4936 if (ctl_is_single == 0) {
4938 union ctl_ha_msg lock_msg;
4940 lock_msg.hdr.nexus = *nexus;
4941 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4943 lock_msg.aps.lock_flag = 1;
4945 lock_msg.aps.lock_flag = 0;
4946 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4947 sizeof(lock_msg), 0);
4948 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4949 printf("%s: APS (lock=%d) error returned from "
4950 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4951 mtx_unlock(&lun->lun_lock);
4952 mtx_unlock(&softc->ctl_lock);
4957 mtx_unlock(&lun->lun_lock);
4958 mtx_unlock(&softc->ctl_lock);
4964 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4966 struct ctl_lun *lun;
4967 struct ctl_softc *softc;
4970 softc = control_softc;
4972 lun = (struct ctl_lun *)be_lun->ctl_lun;
4974 mtx_lock(&lun->lun_lock);
4976 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4977 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4979 mtx_unlock(&lun->lun_lock);
4983 * Backend "memory move is complete" callback for requests that never
4984 * make it down to say RAIDCore's configuration code.
4987 ctl_config_move_done(union ctl_io *io)
4991 retval = CTL_RETVAL_COMPLETE;
4994 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4996 * XXX KDM this shouldn't happen, but what if it does?
4998 if (io->io_hdr.io_type != CTL_IO_SCSI)
4999 panic("I/O type isn't CTL_IO_SCSI!");
5001 if ((io->io_hdr.port_status == 0)
5002 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5003 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5004 io->io_hdr.status = CTL_SUCCESS;
5005 else if ((io->io_hdr.port_status != 0)
5006 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5007 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5009 * For hardware error sense keys, the sense key
5010 * specific value is defined to be a retry count,
5011 * but we use it to pass back an internal FETD
5012 * error code. XXX KDM Hopefully the FETD is only
5013 * using 16 bits for an error code, since that's
5014 * all the space we have in the sks field.
5016 ctl_set_internal_failure(&io->scsiio,
5019 io->io_hdr.port_status);
5020 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5021 free(io->scsiio.kern_data_ptr, M_CTL);
5026 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5027 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5028 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5030 * XXX KDM just assuming a single pointer here, and not a
5031 * S/G list. If we start using S/G lists for config data,
5032 * we'll need to know how to clean them up here as well.
5034 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5035 free(io->scsiio.kern_data_ptr, M_CTL);
5036 /* Hopefully the user has already set the status... */
5040 * XXX KDM now we need to continue data movement. Some
5042 * - call ctl_scsiio() again? We don't do this for data
5043 * writes, because for those at least we know ahead of
5044 * time where the write will go and how long it is. For
5045 * config writes, though, that information is largely
5046 * contained within the write itself, thus we need to
5047 * parse out the data again.
5049 * - Call some other function once the data is in?
5053 * XXX KDM call ctl_scsiio() again for now, and check flag
5054 * bits to see whether we're allocated or not.
5056 retval = ctl_scsiio(&io->scsiio);
5063 * This gets called by a backend driver when it is done with a
5064 * data_submit method.
5067 ctl_data_submit_done(union ctl_io *io)
5070 * If the IO_CONT flag is set, we need to call the supplied
5071 * function to continue processing the I/O, instead of completing
5074 * If there is an error, though, we don't want to keep processing.
5075 * Instead, just send status back to the initiator.
5077 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5078 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5079 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5080 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5081 io->scsiio.io_cont(io);
5088 * This gets called by a backend driver when it is done with a
5089 * configuration write.
5092 ctl_config_write_done(union ctl_io *io)
5095 * If the IO_CONT flag is set, we need to call the supplied
5096 * function to continue processing the I/O, instead of completing
5099 * If there is an error, though, we don't want to keep processing.
5100 * Instead, just send status back to the initiator.
5102 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5103 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5104 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5105 io->scsiio.io_cont(io);
5109 * Since a configuration write can be done for commands that actually
5110 * have data allocated, like write buffer, and commands that have
5111 * no data, like start/stop unit, we need to check here.
5113 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5114 free(io->scsiio.kern_data_ptr, M_CTL);
5119 * SCSI release command.
5122 ctl_scsi_release(struct ctl_scsiio *ctsio)
5124 int length, longid, thirdparty_id, resv_id;
5125 struct ctl_softc *ctl_softc;
5126 struct ctl_lun *lun;
5131 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5133 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5134 ctl_softc = control_softc;
5136 switch (ctsio->cdb[0]) {
5138 struct scsi_release_10 *cdb;
5140 cdb = (struct scsi_release_10 *)ctsio->cdb;
5142 if (cdb->byte2 & SR10_LONGID)
5145 thirdparty_id = cdb->thirdparty_id;
5147 resv_id = cdb->resv_id;
5148 length = scsi_2btoul(cdb->length);
5155 * XXX KDM right now, we only support LUN reservation. We don't
5156 * support 3rd party reservations, or extent reservations, which
5157 * might actually need the parameter list. If we've gotten this
5158 * far, we've got a LUN reservation. Anything else got kicked out
5159 * above. So, according to SPC, ignore the length.
5163 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5165 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5166 ctsio->kern_data_len = length;
5167 ctsio->kern_total_len = length;
5168 ctsio->kern_data_resid = 0;
5169 ctsio->kern_rel_offset = 0;
5170 ctsio->kern_sg_entries = 0;
5171 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5172 ctsio->be_move_done = ctl_config_move_done;
5173 ctl_datamove((union ctl_io *)ctsio);
5175 return (CTL_RETVAL_COMPLETE);
5179 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5181 mtx_lock(&lun->lun_lock);
5184 * According to SPC, it is not an error for an intiator to attempt
5185 * to release a reservation on a LUN that isn't reserved, or that
5186 * is reserved by another initiator. The reservation can only be
5187 * released, though, by the initiator who made it or by one of
5188 * several reset type events.
5190 if (lun->flags & CTL_LUN_RESERVED) {
5191 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5192 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5193 && (ctsio->io_hdr.nexus.targ_target.id ==
5194 lun->rsv_nexus.targ_target.id)) {
5195 lun->flags &= ~CTL_LUN_RESERVED;
5199 mtx_unlock(&lun->lun_lock);
5201 ctsio->scsi_status = SCSI_STATUS_OK;
5202 ctsio->io_hdr.status = CTL_SUCCESS;
5204 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5205 free(ctsio->kern_data_ptr, M_CTL);
5206 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5209 ctl_done((union ctl_io *)ctsio);
5210 return (CTL_RETVAL_COMPLETE);
5214 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5216 int extent, thirdparty, longid;
5217 int resv_id, length;
5218 uint64_t thirdparty_id;
5219 struct ctl_softc *ctl_softc;
5220 struct ctl_lun *lun;
5229 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5232 ctl_softc = control_softc;
5234 switch (ctsio->cdb[0]) {
5236 struct scsi_reserve_10 *cdb;
5238 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5240 if (cdb->byte2 & SR10_LONGID)
5243 thirdparty_id = cdb->thirdparty_id;
5245 resv_id = cdb->resv_id;
5246 length = scsi_2btoul(cdb->length);
5252 * XXX KDM right now, we only support LUN reservation. We don't
5253 * support 3rd party reservations, or extent reservations, which
5254 * might actually need the parameter list. If we've gotten this
5255 * far, we've got a LUN reservation. Anything else got kicked out
5256 * above. So, according to SPC, ignore the length.
5260 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5262 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5263 ctsio->kern_data_len = length;
5264 ctsio->kern_total_len = length;
5265 ctsio->kern_data_resid = 0;
5266 ctsio->kern_rel_offset = 0;
5267 ctsio->kern_sg_entries = 0;
5268 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5269 ctsio->be_move_done = ctl_config_move_done;
5270 ctl_datamove((union ctl_io *)ctsio);
5272 return (CTL_RETVAL_COMPLETE);
5276 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5278 mtx_lock(&lun->lun_lock);
5279 if (lun->flags & CTL_LUN_RESERVED) {
5280 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5281 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5282 || (ctsio->io_hdr.nexus.targ_target.id !=
5283 lun->rsv_nexus.targ_target.id)) {
5284 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5285 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5290 lun->flags |= CTL_LUN_RESERVED;
5291 lun->rsv_nexus = ctsio->io_hdr.nexus;
5293 ctsio->scsi_status = SCSI_STATUS_OK;
5294 ctsio->io_hdr.status = CTL_SUCCESS;
5297 mtx_unlock(&lun->lun_lock);
5299 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5300 free(ctsio->kern_data_ptr, M_CTL);
5301 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5304 ctl_done((union ctl_io *)ctsio);
5305 return (CTL_RETVAL_COMPLETE);
5309 ctl_start_stop(struct ctl_scsiio *ctsio)
5311 struct scsi_start_stop_unit *cdb;
5312 struct ctl_lun *lun;
5313 struct ctl_softc *ctl_softc;
5316 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5318 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5319 ctl_softc = control_softc;
5322 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5326 * We don't support the immediate bit on a stop unit. In order to
5327 * do that, we would need to code up a way to know that a stop is
5328 * pending, and hold off any new commands until it completes, one
5329 * way or another. Then we could accept or reject those commands
5330 * depending on its status. We would almost need to do the reverse
5331 * of what we do below for an immediate start -- return the copy of
5332 * the ctl_io to the FETD with status to send to the host (and to
5333 * free the copy!) and then free the original I/O once the stop
5334 * actually completes. That way, the OOA queue mechanism can work
5335 * to block commands that shouldn't proceed. Another alternative
5336 * would be to put the copy in the queue in place of the original,
5337 * and return the original back to the caller. That could be
5340 if ((cdb->byte2 & SSS_IMMED)
5341 && ((cdb->how & SSS_START) == 0)) {
5342 ctl_set_invalid_field(ctsio,
5348 ctl_done((union ctl_io *)ctsio);
5349 return (CTL_RETVAL_COMPLETE);
5352 if ((lun->flags & CTL_LUN_PR_RESERVED)
5353 && ((cdb->how & SSS_START)==0)) {
5356 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5357 if (!lun->per_res[residx].registered
5358 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5360 ctl_set_reservation_conflict(ctsio);
5361 ctl_done((union ctl_io *)ctsio);
5362 return (CTL_RETVAL_COMPLETE);
5367 * If there is no backend on this device, we can't start or stop
5368 * it. In theory we shouldn't get any start/stop commands in the
5369 * first place at this level if the LUN doesn't have a backend.
5370 * That should get stopped by the command decode code.
5372 if (lun->backend == NULL) {
5373 ctl_set_invalid_opcode(ctsio);
5374 ctl_done((union ctl_io *)ctsio);
5375 return (CTL_RETVAL_COMPLETE);
5379 * XXX KDM Copan-specific offline behavior.
5380 * Figure out a reasonable way to port this?
5383 mtx_lock(&lun->lun_lock);
5385 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5386 && (lun->flags & CTL_LUN_OFFLINE)) {
5388 * If the LUN is offline, and the on/offline bit isn't set,
5389 * reject the start or stop. Otherwise, let it through.
5391 mtx_unlock(&lun->lun_lock);
5392 ctl_set_lun_not_ready(ctsio);
5393 ctl_done((union ctl_io *)ctsio);
5395 mtx_unlock(&lun->lun_lock);
5396 #endif /* NEEDTOPORT */
5398 * This could be a start or a stop when we're online,
5399 * or a stop/offline or start/online. A start or stop when
5400 * we're offline is covered in the case above.
5403 * In the non-immediate case, we send the request to
5404 * the backend and return status to the user when
5407 * In the immediate case, we allocate a new ctl_io
5408 * to hold a copy of the request, and send that to
5409 * the backend. We then set good status on the
5410 * user's request and return it immediately.
5412 if (cdb->byte2 & SSS_IMMED) {
5413 union ctl_io *new_io;
5415 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5416 if (new_io == NULL) {
5417 ctl_set_busy(ctsio);
5418 ctl_done((union ctl_io *)ctsio);
5420 ctl_copy_io((union ctl_io *)ctsio,
5422 retval = lun->backend->config_write(new_io);
5423 ctl_set_success(ctsio);
5424 ctl_done((union ctl_io *)ctsio);
5427 retval = lun->backend->config_write(
5428 (union ctl_io *)ctsio);
5437 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5438 * we don't really do anything with the LBA and length fields if the user
5439 * passes them in. Instead we'll just flush out the cache for the entire
5443 ctl_sync_cache(struct ctl_scsiio *ctsio)
5445 struct ctl_lun *lun;
5446 struct ctl_softc *ctl_softc;
5447 uint64_t starting_lba;
5448 uint32_t block_count;
5451 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5453 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5454 ctl_softc = control_softc;
5457 switch (ctsio->cdb[0]) {
5458 case SYNCHRONIZE_CACHE: {
5459 struct scsi_sync_cache *cdb;
5460 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5462 starting_lba = scsi_4btoul(cdb->begin_lba);
5463 block_count = scsi_2btoul(cdb->lb_count);
5466 case SYNCHRONIZE_CACHE_16: {
5467 struct scsi_sync_cache_16 *cdb;
5468 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5470 starting_lba = scsi_8btou64(cdb->begin_lba);
5471 block_count = scsi_4btoul(cdb->lb_count);
5475 ctl_set_invalid_opcode(ctsio);
5476 ctl_done((union ctl_io *)ctsio);
5478 break; /* NOTREACHED */
5482 * We check the LBA and length, but don't do anything with them.
5483 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5484 * get flushed. This check will just help satisfy anyone who wants
5485 * to see an error for an out of range LBA.
5487 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5488 ctl_set_lba_out_of_range(ctsio);
5489 ctl_done((union ctl_io *)ctsio);
5494 * If this LUN has no backend, we can't flush the cache anyway.
5496 if (lun->backend == NULL) {
5497 ctl_set_invalid_opcode(ctsio);
5498 ctl_done((union ctl_io *)ctsio);
5503 * Check to see whether we're configured to send the SYNCHRONIZE
5504 * CACHE command directly to the back end.
5506 mtx_lock(&lun->lun_lock);
5507 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5508 && (++(lun->sync_count) >= lun->sync_interval)) {
5509 lun->sync_count = 0;
5510 mtx_unlock(&lun->lun_lock);
5511 retval = lun->backend->config_write((union ctl_io *)ctsio);
5513 mtx_unlock(&lun->lun_lock);
5514 ctl_set_success(ctsio);
5515 ctl_done((union ctl_io *)ctsio);
5524 ctl_format(struct ctl_scsiio *ctsio)
5526 struct scsi_format *cdb;
5527 struct ctl_lun *lun;
5528 struct ctl_softc *ctl_softc;
5529 int length, defect_list_len;
5531 CTL_DEBUG_PRINT(("ctl_format\n"));
5533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5534 ctl_softc = control_softc;
5536 cdb = (struct scsi_format *)ctsio->cdb;
5539 if (cdb->byte2 & SF_FMTDATA) {
5540 if (cdb->byte2 & SF_LONGLIST)
5541 length = sizeof(struct scsi_format_header_long);
5543 length = sizeof(struct scsi_format_header_short);
5546 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5548 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5549 ctsio->kern_data_len = length;
5550 ctsio->kern_total_len = length;
5551 ctsio->kern_data_resid = 0;
5552 ctsio->kern_rel_offset = 0;
5553 ctsio->kern_sg_entries = 0;
5554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5555 ctsio->be_move_done = ctl_config_move_done;
5556 ctl_datamove((union ctl_io *)ctsio);
5558 return (CTL_RETVAL_COMPLETE);
5561 defect_list_len = 0;
5563 if (cdb->byte2 & SF_FMTDATA) {
5564 if (cdb->byte2 & SF_LONGLIST) {
5565 struct scsi_format_header_long *header;
5567 header = (struct scsi_format_header_long *)
5568 ctsio->kern_data_ptr;
5570 defect_list_len = scsi_4btoul(header->defect_list_len);
5571 if (defect_list_len != 0) {
5572 ctl_set_invalid_field(ctsio,
5581 struct scsi_format_header_short *header;
5583 header = (struct scsi_format_header_short *)
5584 ctsio->kern_data_ptr;
5586 defect_list_len = scsi_2btoul(header->defect_list_len);
5587 if (defect_list_len != 0) {
5588 ctl_set_invalid_field(ctsio,
5600 * The format command will clear out the "Medium format corrupted"
5601 * status if set by the configuration code. That status is really
5602 * just a way to notify the host that we have lost the media, and
5603 * get them to issue a command that will basically make them think
5604 * they're blowing away the media.
5606 mtx_lock(&lun->lun_lock);
5607 lun->flags &= ~CTL_LUN_INOPERABLE;
5608 mtx_unlock(&lun->lun_lock);
5610 ctsio->scsi_status = SCSI_STATUS_OK;
5611 ctsio->io_hdr.status = CTL_SUCCESS;
5614 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5615 free(ctsio->kern_data_ptr, M_CTL);
5616 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5619 ctl_done((union ctl_io *)ctsio);
5620 return (CTL_RETVAL_COMPLETE);
5624 ctl_read_buffer(struct ctl_scsiio *ctsio)
5626 struct scsi_read_buffer *cdb;
5627 struct ctl_lun *lun;
5628 int buffer_offset, len;
5629 static uint8_t descr[4];
5630 static uint8_t echo_descr[4] = { 0 };
5632 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5634 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5635 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5637 if (lun->flags & CTL_LUN_PR_RESERVED) {
5641 * XXX KDM need a lock here.
5643 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5644 if ((lun->res_type == SPR_TYPE_EX_AC
5645 && residx != lun->pr_res_idx)
5646 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5647 || lun->res_type == SPR_TYPE_EX_AC_AR)
5648 && !lun->per_res[residx].registered)) {
5649 ctl_set_reservation_conflict(ctsio);
5650 ctl_done((union ctl_io *)ctsio);
5651 return (CTL_RETVAL_COMPLETE);
5655 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5656 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5657 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5658 ctl_set_invalid_field(ctsio,
5664 ctl_done((union ctl_io *)ctsio);
5665 return (CTL_RETVAL_COMPLETE);
5668 len = scsi_3btoul(cdb->length);
5669 buffer_offset = scsi_3btoul(cdb->offset);
5671 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5672 ctl_set_invalid_field(ctsio,
5678 ctl_done((union ctl_io *)ctsio);
5679 return (CTL_RETVAL_COMPLETE);
5682 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5684 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5685 ctsio->kern_data_ptr = descr;
5686 len = min(len, sizeof(descr));
5687 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5688 ctsio->kern_data_ptr = echo_descr;
5689 len = min(len, sizeof(echo_descr));
5691 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5692 ctsio->kern_data_len = len;
5693 ctsio->kern_total_len = len;
5694 ctsio->kern_data_resid = 0;
5695 ctsio->kern_rel_offset = 0;
5696 ctsio->kern_sg_entries = 0;
5697 ctsio->be_move_done = ctl_config_move_done;
5698 ctl_datamove((union ctl_io *)ctsio);
5700 return (CTL_RETVAL_COMPLETE);
5704 ctl_write_buffer(struct ctl_scsiio *ctsio)
5706 struct scsi_write_buffer *cdb;
5707 struct ctl_lun *lun;
5708 int buffer_offset, len;
5710 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5712 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5713 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5715 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5716 ctl_set_invalid_field(ctsio,
5722 ctl_done((union ctl_io *)ctsio);
5723 return (CTL_RETVAL_COMPLETE);
5726 len = scsi_3btoul(cdb->length);
5727 buffer_offset = scsi_3btoul(cdb->offset);
5729 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5730 ctl_set_invalid_field(ctsio,
5736 ctl_done((union ctl_io *)ctsio);
5737 return (CTL_RETVAL_COMPLETE);
5741 * If we've got a kernel request that hasn't been malloced yet,
5742 * malloc it and tell the caller the data buffer is here.
5744 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5745 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5746 ctsio->kern_data_len = len;
5747 ctsio->kern_total_len = len;
5748 ctsio->kern_data_resid = 0;
5749 ctsio->kern_rel_offset = 0;
5750 ctsio->kern_sg_entries = 0;
5751 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5752 ctsio->be_move_done = ctl_config_move_done;
5753 ctl_datamove((union ctl_io *)ctsio);
5755 return (CTL_RETVAL_COMPLETE);
5758 ctl_done((union ctl_io *)ctsio);
5760 return (CTL_RETVAL_COMPLETE);
5764 ctl_write_same(struct ctl_scsiio *ctsio)
5766 struct ctl_lun *lun;
5767 struct ctl_lba_len_flags *lbalen;
5769 uint32_t num_blocks;
5773 retval = CTL_RETVAL_COMPLETE;
5775 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5779 switch (ctsio->cdb[0]) {
5780 case WRITE_SAME_10: {
5781 struct scsi_write_same_10 *cdb;
5783 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5785 lba = scsi_4btoul(cdb->addr);
5786 num_blocks = scsi_2btoul(cdb->length);
5790 case WRITE_SAME_16: {
5791 struct scsi_write_same_16 *cdb;
5793 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5795 lba = scsi_8btou64(cdb->addr);
5796 num_blocks = scsi_4btoul(cdb->length);
5802 * We got a command we don't support. This shouldn't
5803 * happen, commands should be filtered out above us.
5805 ctl_set_invalid_opcode(ctsio);
5806 ctl_done((union ctl_io *)ctsio);
5808 return (CTL_RETVAL_COMPLETE);
5809 break; /* NOTREACHED */
5813 * The first check is to make sure we're in bounds, the second
5814 * check is to catch wrap-around problems. If the lba + num blocks
5815 * is less than the lba, then we've wrapped around and the block
5816 * range is invalid anyway.
5818 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5819 || ((lba + num_blocks) < lba)) {
5820 ctl_set_lba_out_of_range(ctsio);
5821 ctl_done((union ctl_io *)ctsio);
5822 return (CTL_RETVAL_COMPLETE);
5825 /* Zero number of blocks means "to the last logical block" */
5826 if (num_blocks == 0) {
5827 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5828 ctl_set_invalid_field(ctsio,
5834 ctl_done((union ctl_io *)ctsio);
5835 return (CTL_RETVAL_COMPLETE);
5837 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5840 len = lun->be_lun->blocksize;
5843 * If we've got a kernel request that hasn't been malloced yet,
5844 * malloc it and tell the caller the data buffer is here.
5846 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5847 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5848 ctsio->kern_data_len = len;
5849 ctsio->kern_total_len = len;
5850 ctsio->kern_data_resid = 0;
5851 ctsio->kern_rel_offset = 0;
5852 ctsio->kern_sg_entries = 0;
5853 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5854 ctsio->be_move_done = ctl_config_move_done;
5855 ctl_datamove((union ctl_io *)ctsio);
5857 return (CTL_RETVAL_COMPLETE);
5860 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5862 lbalen->len = num_blocks;
5863 lbalen->flags = byte2;
5864 retval = lun->backend->config_write((union ctl_io *)ctsio);
5870 ctl_unmap(struct ctl_scsiio *ctsio)
5872 struct ctl_lun *lun;
5873 struct scsi_unmap *cdb;
5874 struct ctl_ptr_len_flags *ptrlen;
5875 struct scsi_unmap_header *hdr;
5876 struct scsi_unmap_desc *buf, *end;
5878 uint32_t num_blocks;
5882 retval = CTL_RETVAL_COMPLETE;
5884 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5886 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5887 cdb = (struct scsi_unmap *)ctsio->cdb;
5889 len = scsi_2btoul(cdb->length);
5893 * If we've got a kernel request that hasn't been malloced yet,
5894 * malloc it and tell the caller the data buffer is here.
5896 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5897 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5898 ctsio->kern_data_len = len;
5899 ctsio->kern_total_len = len;
5900 ctsio->kern_data_resid = 0;
5901 ctsio->kern_rel_offset = 0;
5902 ctsio->kern_sg_entries = 0;
5903 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5904 ctsio->be_move_done = ctl_config_move_done;
5905 ctl_datamove((union ctl_io *)ctsio);
5907 return (CTL_RETVAL_COMPLETE);
5910 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5911 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5912 if (len < sizeof (*hdr) ||
5913 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5914 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5915 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5916 ctl_set_invalid_field(ctsio,
5922 ctl_done((union ctl_io *)ctsio);
5923 return (CTL_RETVAL_COMPLETE);
5925 len = scsi_2btoul(hdr->desc_length);
5926 buf = (struct scsi_unmap_desc *)(hdr + 1);
5927 end = buf + len / sizeof(*buf);
5929 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5930 ptrlen->ptr = (void *)buf;
5932 ptrlen->flags = byte2;
5934 for (; buf < end; buf++) {
5935 lba = scsi_8btou64(buf->lba);
5936 num_blocks = scsi_4btoul(buf->length);
5937 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5938 || ((lba + num_blocks) < lba)) {
5939 ctl_set_lba_out_of_range(ctsio);
5940 ctl_done((union ctl_io *)ctsio);
5941 return (CTL_RETVAL_COMPLETE);
5945 retval = lun->backend->config_write((union ctl_io *)ctsio);
5951 * Note that this function currently doesn't actually do anything inside
5952 * CTL to enforce things if the DQue bit is turned on.
5954 * Also note that this function can't be used in the default case, because
5955 * the DQue bit isn't set in the changeable mask for the control mode page
5956 * anyway. This is just here as an example for how to implement a page
5957 * handler, and a placeholder in case we want to allow the user to turn
5958 * tagged queueing on and off.
5960 * The D_SENSE bit handling is functional, however, and will turn
5961 * descriptor sense on and off for a given LUN.
5964 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5965 struct ctl_page_index *page_index, uint8_t *page_ptr)
5967 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5968 struct ctl_lun *lun;
5969 struct ctl_softc *softc;
5973 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5974 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5977 user_cp = (struct scsi_control_page *)page_ptr;
5978 current_cp = (struct scsi_control_page *)
5979 (page_index->page_data + (page_index->page_len *
5981 saved_cp = (struct scsi_control_page *)
5982 (page_index->page_data + (page_index->page_len *
5985 softc = control_softc;
5987 mtx_lock(&lun->lun_lock);
5988 if (((current_cp->rlec & SCP_DSENSE) == 0)
5989 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5991 * Descriptor sense is currently turned off and the user
5992 * wants to turn it on.
5994 current_cp->rlec |= SCP_DSENSE;
5995 saved_cp->rlec |= SCP_DSENSE;
5996 lun->flags |= CTL_LUN_SENSE_DESC;
5998 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5999 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6001 * Descriptor sense is currently turned on, and the user
6002 * wants to turn it off.
6004 current_cp->rlec &= ~SCP_DSENSE;
6005 saved_cp->rlec &= ~SCP_DSENSE;
6006 lun->flags &= ~CTL_LUN_SENSE_DESC;
6009 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6010 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6012 csevent_log(CSC_CTL | CSC_SHELF_SW |
6014 csevent_LogType_Trace,
6015 csevent_Severity_Information,
6016 csevent_AlertLevel_Green,
6017 csevent_FRU_Firmware,
6018 csevent_FRU_Unknown,
6019 "Received untagged to untagged transition");
6020 #endif /* NEEDTOPORT */
6023 csevent_log(CSC_CTL | CSC_SHELF_SW |
6025 csevent_LogType_ConfigChange,
6026 csevent_Severity_Information,
6027 csevent_AlertLevel_Green,
6028 csevent_FRU_Firmware,
6029 csevent_FRU_Unknown,
6030 "Received untagged to tagged "
6031 "queueing transition");
6032 #endif /* NEEDTOPORT */
6034 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6035 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6039 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6041 csevent_log(CSC_CTL | CSC_SHELF_SW |
6043 csevent_LogType_ConfigChange,
6044 csevent_Severity_Warning,
6045 csevent_AlertLevel_Yellow,
6046 csevent_FRU_Firmware,
6047 csevent_FRU_Unknown,
6048 "Received tagged queueing to untagged "
6050 #endif /* NEEDTOPORT */
6052 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6053 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6057 csevent_log(CSC_CTL | CSC_SHELF_SW |
6059 csevent_LogType_Trace,
6060 csevent_Severity_Information,
6061 csevent_AlertLevel_Green,
6062 csevent_FRU_Firmware,
6063 csevent_FRU_Unknown,
6064 "Received tagged queueing to tagged "
6065 "queueing transition");
6066 #endif /* NEEDTOPORT */
6072 * Let other initiators know that the mode
6073 * parameters for this LUN have changed.
6075 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6079 lun->pending_sense[i].ua_pending |=
6083 mtx_unlock(&lun->lun_lock);
6089 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6090 struct ctl_page_index *page_index, uint8_t *page_ptr)
6096 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6097 struct ctl_page_index *page_index, int pc)
6099 struct copan_power_subpage *page;
6101 page = (struct copan_power_subpage *)page_index->page_data +
6102 (page_index->page_len * pc);
6105 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6107 * We don't update the changable bits for this page.
6110 case SMS_PAGE_CTRL_CURRENT >> 6:
6111 case SMS_PAGE_CTRL_DEFAULT >> 6:
6112 case SMS_PAGE_CTRL_SAVED >> 6:
6114 ctl_update_power_subpage(page);
6119 EPRINT(0, "Invalid PC %d!!", pc);
6128 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6129 struct ctl_page_index *page_index, uint8_t *page_ptr)
6131 struct copan_aps_subpage *user_sp;
6132 struct copan_aps_subpage *current_sp;
6133 union ctl_modepage_info *modepage_info;
6134 struct ctl_softc *softc;
6135 struct ctl_lun *lun;
6138 retval = CTL_RETVAL_COMPLETE;
6139 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6140 (page_index->page_len * CTL_PAGE_CURRENT));
6141 softc = control_softc;
6142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6144 user_sp = (struct copan_aps_subpage *)page_ptr;
6146 modepage_info = (union ctl_modepage_info *)
6147 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6149 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6150 modepage_info->header.subpage = page_index->subpage;
6151 modepage_info->aps.lock_active = user_sp->lock_active;
6153 mtx_lock(&softc->ctl_lock);
6156 * If there is a request to lock the LUN and another LUN is locked
6157 * this is an error. If the requested LUN is already locked ignore
6158 * the request. If no LUN is locked attempt to lock it.
6159 * if there is a request to unlock the LUN and the LUN is currently
6160 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6161 * if another LUN is locked or no LUN is locked.
6163 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6164 if (softc->aps_locked_lun == lun->lun) {
6166 * This LUN is already locked, so we're done.
6168 retval = CTL_RETVAL_COMPLETE;
6169 } else if (softc->aps_locked_lun == 0) {
6171 * No one has the lock, pass the request to the
6174 retval = lun->backend->config_write(
6175 (union ctl_io *)ctsio);
6178 * Someone else has the lock, throw out the request.
6180 ctl_set_already_locked(ctsio);
6181 free(ctsio->kern_data_ptr, M_CTL);
6182 ctl_done((union ctl_io *)ctsio);
6185 * Set the return value so that ctl_do_mode_select()
6186 * won't try to complete the command. We already
6187 * completed it here.
6189 retval = CTL_RETVAL_ERROR;
6191 } else if (softc->aps_locked_lun == lun->lun) {
6193 * This LUN is locked, so pass the unlock request to the
6196 retval = lun->backend->config_write((union ctl_io *)ctsio);
6198 mtx_unlock(&softc->ctl_lock);
6204 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6205 struct ctl_page_index *page_index,
6211 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6216 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6217 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6218 printf("page data:");
6220 printf(" %.2x",page_ptr[i]);
6226 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6227 struct ctl_page_index *page_index,
6230 struct copan_debugconf_subpage *page;
6232 page = (struct copan_debugconf_subpage *)page_index->page_data +
6233 (page_index->page_len * pc);
6236 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6237 case SMS_PAGE_CTRL_DEFAULT >> 6:
6238 case SMS_PAGE_CTRL_SAVED >> 6:
6240 * We don't update the changable or default bits for this page.
6243 case SMS_PAGE_CTRL_CURRENT >> 6:
6244 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6245 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6249 EPRINT(0, "Invalid PC %d!!", pc);
6250 #endif /* NEEDTOPORT */
6258 ctl_do_mode_select(union ctl_io *io)
6260 struct scsi_mode_page_header *page_header;
6261 struct ctl_page_index *page_index;
6262 struct ctl_scsiio *ctsio;
6263 int control_dev, page_len;
6264 int page_len_offset, page_len_size;
6265 union ctl_modepage_info *modepage_info;
6266 struct ctl_lun *lun;
6267 int *len_left, *len_used;
6270 ctsio = &io->scsiio;
6273 retval = CTL_RETVAL_COMPLETE;
6275 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6277 if (lun->be_lun->lun_type != T_DIRECT)
6282 modepage_info = (union ctl_modepage_info *)
6283 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6284 len_left = &modepage_info->header.len_left;
6285 len_used = &modepage_info->header.len_used;
6289 page_header = (struct scsi_mode_page_header *)
6290 (ctsio->kern_data_ptr + *len_used);
6292 if (*len_left == 0) {
6293 free(ctsio->kern_data_ptr, M_CTL);
6294 ctl_set_success(ctsio);
6295 ctl_done((union ctl_io *)ctsio);
6296 return (CTL_RETVAL_COMPLETE);
6297 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6299 free(ctsio->kern_data_ptr, M_CTL);
6300 ctl_set_param_len_error(ctsio);
6301 ctl_done((union ctl_io *)ctsio);
6302 return (CTL_RETVAL_COMPLETE);
6304 } else if ((page_header->page_code & SMPH_SPF)
6305 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6307 free(ctsio->kern_data_ptr, M_CTL);
6308 ctl_set_param_len_error(ctsio);
6309 ctl_done((union ctl_io *)ctsio);
6310 return (CTL_RETVAL_COMPLETE);
6315 * XXX KDM should we do something with the block descriptor?
6317 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6319 if ((control_dev != 0)
6320 && (lun->mode_pages.index[i].page_flags &
6321 CTL_PAGE_FLAG_DISK_ONLY))
6324 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6325 (page_header->page_code & SMPH_PC_MASK))
6329 * If neither page has a subpage code, then we've got a
6332 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6333 && ((page_header->page_code & SMPH_SPF) == 0)) {
6334 page_index = &lun->mode_pages.index[i];
6335 page_len = page_header->page_length;
6340 * If both pages have subpages, then the subpage numbers
6343 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6344 && (page_header->page_code & SMPH_SPF)) {
6345 struct scsi_mode_page_header_sp *sph;
6347 sph = (struct scsi_mode_page_header_sp *)page_header;
6349 if (lun->mode_pages.index[i].subpage ==
6351 page_index = &lun->mode_pages.index[i];
6352 page_len = scsi_2btoul(sph->page_length);
6359 * If we couldn't find the page, or if we don't have a mode select
6360 * handler for it, send back an error to the user.
6362 if ((page_index == NULL)
6363 || (page_index->select_handler == NULL)) {
6364 ctl_set_invalid_field(ctsio,
6367 /*field*/ *len_used,
6370 free(ctsio->kern_data_ptr, M_CTL);
6371 ctl_done((union ctl_io *)ctsio);
6372 return (CTL_RETVAL_COMPLETE);
6375 if (page_index->page_code & SMPH_SPF) {
6376 page_len_offset = 2;
6380 page_len_offset = 1;
6384 * If the length the initiator gives us isn't the one we specify in
6385 * the mode page header, or if they didn't specify enough data in
6386 * the CDB to avoid truncating this page, kick out the request.
6388 if ((page_len != (page_index->page_len - page_len_offset -
6390 || (*len_left < page_index->page_len)) {
6393 ctl_set_invalid_field(ctsio,
6396 /*field*/ *len_used + page_len_offset,
6399 free(ctsio->kern_data_ptr, M_CTL);
6400 ctl_done((union ctl_io *)ctsio);
6401 return (CTL_RETVAL_COMPLETE);
6405 * Run through the mode page, checking to make sure that the bits
6406 * the user changed are actually legal for him to change.
6408 for (i = 0; i < page_index->page_len; i++) {
6409 uint8_t *user_byte, *change_mask, *current_byte;
6413 user_byte = (uint8_t *)page_header + i;
6414 change_mask = page_index->page_data +
6415 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6416 current_byte = page_index->page_data +
6417 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6420 * Check to see whether the user set any bits in this byte
6421 * that he is not allowed to set.
6423 if ((*user_byte & ~(*change_mask)) ==
6424 (*current_byte & ~(*change_mask)))
6428 * Go through bit by bit to determine which one is illegal.
6431 for (j = 7; j >= 0; j--) {
6432 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6433 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6438 ctl_set_invalid_field(ctsio,
6441 /*field*/ *len_used + i,
6444 free(ctsio->kern_data_ptr, M_CTL);
6445 ctl_done((union ctl_io *)ctsio);
6446 return (CTL_RETVAL_COMPLETE);
6450 * Decrement these before we call the page handler, since we may
6451 * end up getting called back one way or another before the handler
6452 * returns to this context.
6454 *len_left -= page_index->page_len;
6455 *len_used += page_index->page_len;
6457 retval = page_index->select_handler(ctsio, page_index,
6458 (uint8_t *)page_header);
6461 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6462 * wait until this queued command completes to finish processing
6463 * the mode page. If it returns anything other than
6464 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6465 * already set the sense information, freed the data pointer, and
6466 * completed the io for us.
6468 if (retval != CTL_RETVAL_COMPLETE)
6469 goto bailout_no_done;
6472 * If the initiator sent us more than one page, parse the next one.
6477 ctl_set_success(ctsio);
6478 free(ctsio->kern_data_ptr, M_CTL);
6479 ctl_done((union ctl_io *)ctsio);
6483 return (CTL_RETVAL_COMPLETE);
6488 ctl_mode_select(struct ctl_scsiio *ctsio)
6490 int param_len, pf, sp;
6491 int header_size, bd_len;
6492 int len_left, len_used;
6493 struct ctl_page_index *page_index;
6494 struct ctl_lun *lun;
6495 int control_dev, page_len;
6496 union ctl_modepage_info *modepage_info;
6508 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6510 if (lun->be_lun->lun_type != T_DIRECT)
6515 switch (ctsio->cdb[0]) {
6516 case MODE_SELECT_6: {
6517 struct scsi_mode_select_6 *cdb;
6519 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6521 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6522 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6524 param_len = cdb->length;
6525 header_size = sizeof(struct scsi_mode_header_6);
6528 case MODE_SELECT_10: {
6529 struct scsi_mode_select_10 *cdb;
6531 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6533 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6534 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6536 param_len = scsi_2btoul(cdb->length);
6537 header_size = sizeof(struct scsi_mode_header_10);
6541 ctl_set_invalid_opcode(ctsio);
6542 ctl_done((union ctl_io *)ctsio);
6543 return (CTL_RETVAL_COMPLETE);
6544 break; /* NOTREACHED */
6549 * "A parameter list length of zero indicates that the Data-Out Buffer
6550 * shall be empty. This condition shall not be considered as an error."
6552 if (param_len == 0) {
6553 ctl_set_success(ctsio);
6554 ctl_done((union ctl_io *)ctsio);
6555 return (CTL_RETVAL_COMPLETE);
6559 * Since we'll hit this the first time through, prior to
6560 * allocation, we don't need to free a data buffer here.
6562 if (param_len < header_size) {
6563 ctl_set_param_len_error(ctsio);
6564 ctl_done((union ctl_io *)ctsio);
6565 return (CTL_RETVAL_COMPLETE);
6569 * Allocate the data buffer and grab the user's data. In theory,
6570 * we shouldn't have to sanity check the parameter list length here
6571 * because the maximum size is 64K. We should be able to malloc
6572 * that much without too many problems.
6574 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6575 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6576 ctsio->kern_data_len = param_len;
6577 ctsio->kern_total_len = param_len;
6578 ctsio->kern_data_resid = 0;
6579 ctsio->kern_rel_offset = 0;
6580 ctsio->kern_sg_entries = 0;
6581 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6582 ctsio->be_move_done = ctl_config_move_done;
6583 ctl_datamove((union ctl_io *)ctsio);
6585 return (CTL_RETVAL_COMPLETE);
6588 switch (ctsio->cdb[0]) {
6589 case MODE_SELECT_6: {
6590 struct scsi_mode_header_6 *mh6;
6592 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6593 bd_len = mh6->blk_desc_len;
6596 case MODE_SELECT_10: {
6597 struct scsi_mode_header_10 *mh10;
6599 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6600 bd_len = scsi_2btoul(mh10->blk_desc_len);
6604 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6608 if (param_len < (header_size + bd_len)) {
6609 free(ctsio->kern_data_ptr, M_CTL);
6610 ctl_set_param_len_error(ctsio);
6611 ctl_done((union ctl_io *)ctsio);
6612 return (CTL_RETVAL_COMPLETE);
6616 * Set the IO_CONT flag, so that if this I/O gets passed to
6617 * ctl_config_write_done(), it'll get passed back to
6618 * ctl_do_mode_select() for further processing, or completion if
6621 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6622 ctsio->io_cont = ctl_do_mode_select;
6624 modepage_info = (union ctl_modepage_info *)
6625 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6627 memset(modepage_info, 0, sizeof(*modepage_info));
6629 len_left = param_len - header_size - bd_len;
6630 len_used = header_size + bd_len;
6632 modepage_info->header.len_left = len_left;
6633 modepage_info->header.len_used = len_used;
6635 return (ctl_do_mode_select((union ctl_io *)ctsio));
6639 ctl_mode_sense(struct ctl_scsiio *ctsio)
6641 struct ctl_lun *lun;
6642 int pc, page_code, dbd, llba, subpage;
6643 int alloc_len, page_len, header_len, total_len;
6644 struct scsi_mode_block_descr *block_desc;
6645 struct ctl_page_index *page_index;
6653 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6657 if (lun->be_lun->lun_type != T_DIRECT)
6662 if (lun->flags & CTL_LUN_PR_RESERVED) {
6666 * XXX KDM need a lock here.
6668 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6669 if ((lun->res_type == SPR_TYPE_EX_AC
6670 && residx != lun->pr_res_idx)
6671 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6672 || lun->res_type == SPR_TYPE_EX_AC_AR)
6673 && !lun->per_res[residx].registered)) {
6674 ctl_set_reservation_conflict(ctsio);
6675 ctl_done((union ctl_io *)ctsio);
6676 return (CTL_RETVAL_COMPLETE);
6680 switch (ctsio->cdb[0]) {
6681 case MODE_SENSE_6: {
6682 struct scsi_mode_sense_6 *cdb;
6684 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6686 header_len = sizeof(struct scsi_mode_hdr_6);
6687 if (cdb->byte2 & SMS_DBD)
6690 header_len += sizeof(struct scsi_mode_block_descr);
6692 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6693 page_code = cdb->page & SMS_PAGE_CODE;
6694 subpage = cdb->subpage;
6695 alloc_len = cdb->length;
6698 case MODE_SENSE_10: {
6699 struct scsi_mode_sense_10 *cdb;
6701 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6703 header_len = sizeof(struct scsi_mode_hdr_10);
6705 if (cdb->byte2 & SMS_DBD)
6708 header_len += sizeof(struct scsi_mode_block_descr);
6709 if (cdb->byte2 & SMS10_LLBAA)
6711 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6712 page_code = cdb->page & SMS_PAGE_CODE;
6713 subpage = cdb->subpage;
6714 alloc_len = scsi_2btoul(cdb->length);
6718 ctl_set_invalid_opcode(ctsio);
6719 ctl_done((union ctl_io *)ctsio);
6720 return (CTL_RETVAL_COMPLETE);
6721 break; /* NOTREACHED */
6725 * We have to make a first pass through to calculate the size of
6726 * the pages that match the user's query. Then we allocate enough
6727 * memory to hold it, and actually copy the data into the buffer.
6729 switch (page_code) {
6730 case SMS_ALL_PAGES_PAGE: {
6736 * At the moment, values other than 0 and 0xff here are
6737 * reserved according to SPC-3.
6739 if ((subpage != SMS_SUBPAGE_PAGE_0)
6740 && (subpage != SMS_SUBPAGE_ALL)) {
6741 ctl_set_invalid_field(ctsio,
6747 ctl_done((union ctl_io *)ctsio);
6748 return (CTL_RETVAL_COMPLETE);
6751 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6752 if ((control_dev != 0)
6753 && (lun->mode_pages.index[i].page_flags &
6754 CTL_PAGE_FLAG_DISK_ONLY))
6758 * We don't use this subpage if the user didn't
6759 * request all subpages.
6761 if ((lun->mode_pages.index[i].subpage != 0)
6762 && (subpage == SMS_SUBPAGE_PAGE_0))
6766 printf("found page %#x len %d\n",
6767 lun->mode_pages.index[i].page_code &
6769 lun->mode_pages.index[i].page_len);
6771 page_len += lun->mode_pages.index[i].page_len;
6780 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6781 /* Look for the right page code */
6782 if ((lun->mode_pages.index[i].page_code &
6783 SMPH_PC_MASK) != page_code)
6786 /* Look for the right subpage or the subpage wildcard*/
6787 if ((lun->mode_pages.index[i].subpage != subpage)
6788 && (subpage != SMS_SUBPAGE_ALL))
6791 /* Make sure the page is supported for this dev type */
6792 if ((control_dev != 0)
6793 && (lun->mode_pages.index[i].page_flags &
6794 CTL_PAGE_FLAG_DISK_ONLY))
6798 printf("found page %#x len %d\n",
6799 lun->mode_pages.index[i].page_code &
6801 lun->mode_pages.index[i].page_len);
6804 page_len += lun->mode_pages.index[i].page_len;
6807 if (page_len == 0) {
6808 ctl_set_invalid_field(ctsio,
6814 ctl_done((union ctl_io *)ctsio);
6815 return (CTL_RETVAL_COMPLETE);
6821 total_len = header_len + page_len;
6823 printf("header_len = %d, page_len = %d, total_len = %d\n",
6824 header_len, page_len, total_len);
6827 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6828 ctsio->kern_sg_entries = 0;
6829 ctsio->kern_data_resid = 0;
6830 ctsio->kern_rel_offset = 0;
6831 if (total_len < alloc_len) {
6832 ctsio->residual = alloc_len - total_len;
6833 ctsio->kern_data_len = total_len;
6834 ctsio->kern_total_len = total_len;
6836 ctsio->residual = 0;
6837 ctsio->kern_data_len = alloc_len;
6838 ctsio->kern_total_len = alloc_len;
6841 switch (ctsio->cdb[0]) {
6842 case MODE_SENSE_6: {
6843 struct scsi_mode_hdr_6 *header;
6845 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6847 header->datalen = ctl_min(total_len - 1, 254);
6850 header->block_descr_len = 0;
6852 header->block_descr_len =
6853 sizeof(struct scsi_mode_block_descr);
6854 block_desc = (struct scsi_mode_block_descr *)&header[1];
6857 case MODE_SENSE_10: {
6858 struct scsi_mode_hdr_10 *header;
6861 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6863 datalen = ctl_min(total_len - 2, 65533);
6864 scsi_ulto2b(datalen, header->datalen);
6866 scsi_ulto2b(0, header->block_descr_len);
6868 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6869 header->block_descr_len);
6870 block_desc = (struct scsi_mode_block_descr *)&header[1];
6874 panic("invalid CDB type %#x", ctsio->cdb[0]);
6875 break; /* NOTREACHED */
6879 * If we've got a disk, use its blocksize in the block
6880 * descriptor. Otherwise, just set it to 0.
6883 if (control_dev != 0)
6884 scsi_ulto3b(lun->be_lun->blocksize,
6885 block_desc->block_len);
6887 scsi_ulto3b(0, block_desc->block_len);
6890 switch (page_code) {
6891 case SMS_ALL_PAGES_PAGE: {
6894 data_used = header_len;
6895 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6896 struct ctl_page_index *page_index;
6898 page_index = &lun->mode_pages.index[i];
6900 if ((control_dev != 0)
6901 && (page_index->page_flags &
6902 CTL_PAGE_FLAG_DISK_ONLY))
6906 * We don't use this subpage if the user didn't
6907 * request all subpages. We already checked (above)
6908 * to make sure the user only specified a subpage
6909 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6911 if ((page_index->subpage != 0)
6912 && (subpage == SMS_SUBPAGE_PAGE_0))
6916 * Call the handler, if it exists, to update the
6917 * page to the latest values.
6919 if (page_index->sense_handler != NULL)
6920 page_index->sense_handler(ctsio, page_index,pc);
6922 memcpy(ctsio->kern_data_ptr + data_used,
6923 page_index->page_data +
6924 (page_index->page_len * pc),
6925 page_index->page_len);
6926 data_used += page_index->page_len;
6933 data_used = header_len;
6935 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6936 struct ctl_page_index *page_index;
6938 page_index = &lun->mode_pages.index[i];
6940 /* Look for the right page code */
6941 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6944 /* Look for the right subpage or the subpage wildcard*/
6945 if ((page_index->subpage != subpage)
6946 && (subpage != SMS_SUBPAGE_ALL))
6949 /* Make sure the page is supported for this dev type */
6950 if ((control_dev != 0)
6951 && (page_index->page_flags &
6952 CTL_PAGE_FLAG_DISK_ONLY))
6956 * Call the handler, if it exists, to update the
6957 * page to the latest values.
6959 if (page_index->sense_handler != NULL)
6960 page_index->sense_handler(ctsio, page_index,pc);
6962 memcpy(ctsio->kern_data_ptr + data_used,
6963 page_index->page_data +
6964 (page_index->page_len * pc),
6965 page_index->page_len);
6966 data_used += page_index->page_len;
6972 ctsio->scsi_status = SCSI_STATUS_OK;
6974 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6975 ctsio->be_move_done = ctl_config_move_done;
6976 ctl_datamove((union ctl_io *)ctsio);
6978 return (CTL_RETVAL_COMPLETE);
6982 ctl_read_capacity(struct ctl_scsiio *ctsio)
6984 struct scsi_read_capacity *cdb;
6985 struct scsi_read_capacity_data *data;
6986 struct ctl_lun *lun;
6989 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6991 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6993 lba = scsi_4btoul(cdb->addr);
6994 if (((cdb->pmi & SRC_PMI) == 0)
6996 ctl_set_invalid_field(/*ctsio*/ ctsio,
7002 ctl_done((union ctl_io *)ctsio);
7003 return (CTL_RETVAL_COMPLETE);
7006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7008 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7009 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7010 ctsio->residual = 0;
7011 ctsio->kern_data_len = sizeof(*data);
7012 ctsio->kern_total_len = sizeof(*data);
7013 ctsio->kern_data_resid = 0;
7014 ctsio->kern_rel_offset = 0;
7015 ctsio->kern_sg_entries = 0;
7018 * If the maximum LBA is greater than 0xfffffffe, the user must
7019 * issue a SERVICE ACTION IN (16) command, with the read capacity
7020 * serivce action set.
7022 if (lun->be_lun->maxlba > 0xfffffffe)
7023 scsi_ulto4b(0xffffffff, data->addr);
7025 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7028 * XXX KDM this may not be 512 bytes...
7030 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7032 ctsio->scsi_status = SCSI_STATUS_OK;
7034 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7035 ctsio->be_move_done = ctl_config_move_done;
7036 ctl_datamove((union ctl_io *)ctsio);
7038 return (CTL_RETVAL_COMPLETE);
7042 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7044 struct scsi_read_capacity_16 *cdb;
7045 struct scsi_read_capacity_data_long *data;
7046 struct ctl_lun *lun;
7050 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7052 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7054 alloc_len = scsi_4btoul(cdb->alloc_len);
7055 lba = scsi_8btou64(cdb->addr);
7057 if ((cdb->reladr & SRC16_PMI)
7059 ctl_set_invalid_field(/*ctsio*/ ctsio,
7065 ctl_done((union ctl_io *)ctsio);
7066 return (CTL_RETVAL_COMPLETE);
7069 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7071 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7072 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7074 if (sizeof(*data) < alloc_len) {
7075 ctsio->residual = alloc_len - sizeof(*data);
7076 ctsio->kern_data_len = sizeof(*data);
7077 ctsio->kern_total_len = sizeof(*data);
7079 ctsio->residual = 0;
7080 ctsio->kern_data_len = alloc_len;
7081 ctsio->kern_total_len = alloc_len;
7083 ctsio->kern_data_resid = 0;
7084 ctsio->kern_rel_offset = 0;
7085 ctsio->kern_sg_entries = 0;
7087 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7088 /* XXX KDM this may not be 512 bytes... */
7089 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7090 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7091 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7092 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7093 data->lalba_lbp[0] |= SRC16_LBPME;
7095 ctsio->scsi_status = SCSI_STATUS_OK;
7097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7098 ctsio->be_move_done = ctl_config_move_done;
7099 ctl_datamove((union ctl_io *)ctsio);
7101 return (CTL_RETVAL_COMPLETE);
7105 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7107 struct scsi_maintenance_in *cdb;
7109 int alloc_len, total_len = 0;
7110 int num_target_port_groups, single;
7111 struct ctl_lun *lun;
7112 struct ctl_softc *softc;
7113 struct scsi_target_group_data *rtg_ptr;
7114 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7115 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7116 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7118 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7120 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7121 softc = control_softc;
7122 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7124 retval = CTL_RETVAL_COMPLETE;
7126 single = ctl_is_single;
7128 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7130 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7132 total_len = sizeof(struct scsi_target_group_data) +
7133 sizeof(struct scsi_target_port_group_descriptor) *
7134 num_target_port_groups +
7135 sizeof(struct scsi_target_port_descriptor) *
7136 NUM_PORTS_PER_GRP * num_target_port_groups;
7138 alloc_len = scsi_4btoul(cdb->length);
7140 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7142 ctsio->kern_sg_entries = 0;
7144 if (total_len < alloc_len) {
7145 ctsio->residual = alloc_len - total_len;
7146 ctsio->kern_data_len = total_len;
7147 ctsio->kern_total_len = total_len;
7149 ctsio->residual = 0;
7150 ctsio->kern_data_len = alloc_len;
7151 ctsio->kern_total_len = alloc_len;
7153 ctsio->kern_data_resid = 0;
7154 ctsio->kern_rel_offset = 0;
7156 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7158 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7159 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7160 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7161 &tp_desc_ptr1_1->desc_list[0];
7164 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7165 &tp_desc_ptr1_2->desc_list[0];
7166 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7167 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7168 &tp_desc_ptr2_1->desc_list[0];
7170 tpg_desc_ptr2 = NULL;
7171 tp_desc_ptr2_1 = NULL;
7172 tp_desc_ptr2_2 = NULL;
7175 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7177 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7178 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7179 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7180 tpg_desc_ptr2->pref_state =
7181 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7183 tpg_desc_ptr1->pref_state =
7184 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7185 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7188 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7189 tpg_desc_ptr1->pref_state =
7190 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7191 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7193 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7194 tpg_desc_ptr2->pref_state =
7195 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7199 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7201 tpg_desc_ptr1->support = 0;
7202 tpg_desc_ptr1->target_port_group[1] = 1;
7203 tpg_desc_ptr1->status = TPG_IMPLICIT;
7204 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7207 tpg_desc_ptr2->support = 0;
7208 tpg_desc_ptr2->target_port_group[1] = 2;
7209 tpg_desc_ptr2->status = TPG_IMPLICIT;
7210 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7212 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7213 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7215 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7216 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7218 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7219 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7220 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7222 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7223 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7227 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7228 ctsio->be_move_done = ctl_config_move_done;
7230 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7231 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7232 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7233 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7234 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7236 ctl_datamove((union ctl_io *)ctsio);
7241 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7243 struct ctl_lun *lun;
7244 struct scsi_report_supported_opcodes *cdb;
7245 const struct ctl_cmd_entry *entry, *sentry;
7246 struct scsi_report_supported_opcodes_all *all;
7247 struct scsi_report_supported_opcodes_descr *descr;
7248 struct scsi_report_supported_opcodes_one *one;
7250 int alloc_len, total_len;
7251 int opcode, service_action, i, j, num;
7253 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7255 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7256 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7258 retval = CTL_RETVAL_COMPLETE;
7260 opcode = cdb->requested_opcode;
7261 service_action = scsi_2btoul(cdb->requested_service_action);
7262 switch (cdb->options & RSO_OPTIONS_MASK) {
7263 case RSO_OPTIONS_ALL:
7265 for (i = 0; i < 256; i++) {
7266 entry = &ctl_cmd_table[i];
7267 if (entry->flags & CTL_CMD_FLAG_SA5) {
7268 for (j = 0; j < 32; j++) {
7269 sentry = &((const struct ctl_cmd_entry *)
7271 if (ctl_cmd_applicable(
7272 lun->be_lun->lun_type, sentry))
7276 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7281 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7282 num * sizeof(struct scsi_report_supported_opcodes_descr);
7284 case RSO_OPTIONS_OC:
7285 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7286 ctl_set_invalid_field(/*ctsio*/ ctsio,
7292 ctl_done((union ctl_io *)ctsio);
7293 return (CTL_RETVAL_COMPLETE);
7295 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7297 case RSO_OPTIONS_OC_SA:
7298 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7299 service_action >= 32) {
7300 ctl_set_invalid_field(/*ctsio*/ ctsio,
7306 ctl_done((union ctl_io *)ctsio);
7307 return (CTL_RETVAL_COMPLETE);
7309 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7312 ctl_set_invalid_field(/*ctsio*/ ctsio,
7318 ctl_done((union ctl_io *)ctsio);
7319 return (CTL_RETVAL_COMPLETE);
7322 alloc_len = scsi_4btoul(cdb->length);
7324 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7326 ctsio->kern_sg_entries = 0;
7328 if (total_len < alloc_len) {
7329 ctsio->residual = alloc_len - total_len;
7330 ctsio->kern_data_len = total_len;
7331 ctsio->kern_total_len = total_len;
7333 ctsio->residual = 0;
7334 ctsio->kern_data_len = alloc_len;
7335 ctsio->kern_total_len = alloc_len;
7337 ctsio->kern_data_resid = 0;
7338 ctsio->kern_rel_offset = 0;
7340 switch (cdb->options & RSO_OPTIONS_MASK) {
7341 case RSO_OPTIONS_ALL:
7342 all = (struct scsi_report_supported_opcodes_all *)
7343 ctsio->kern_data_ptr;
7345 for (i = 0; i < 256; i++) {
7346 entry = &ctl_cmd_table[i];
7347 if (entry->flags & CTL_CMD_FLAG_SA5) {
7348 for (j = 0; j < 32; j++) {
7349 sentry = &((const struct ctl_cmd_entry *)
7351 if (!ctl_cmd_applicable(
7352 lun->be_lun->lun_type, sentry))
7354 descr = &all->descr[num++];
7356 scsi_ulto2b(j, descr->service_action);
7357 descr->flags = RSO_SERVACTV;
7358 scsi_ulto2b(sentry->length,
7362 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7365 descr = &all->descr[num++];
7367 scsi_ulto2b(0, descr->service_action);
7369 scsi_ulto2b(entry->length, descr->cdb_length);
7373 num * sizeof(struct scsi_report_supported_opcodes_descr),
7376 case RSO_OPTIONS_OC:
7377 one = (struct scsi_report_supported_opcodes_one *)
7378 ctsio->kern_data_ptr;
7379 entry = &ctl_cmd_table[opcode];
7381 case RSO_OPTIONS_OC_SA:
7382 one = (struct scsi_report_supported_opcodes_one *)
7383 ctsio->kern_data_ptr;
7384 entry = &ctl_cmd_table[opcode];
7385 entry = &((const struct ctl_cmd_entry *)
7386 entry->execute)[service_action];
7388 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7390 scsi_ulto2b(entry->length, one->cdb_length);
7391 one->cdb_usage[0] = opcode;
7392 memcpy(&one->cdb_usage[1], entry->usage,
7399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7400 ctsio->be_move_done = ctl_config_move_done;
7402 ctl_datamove((union ctl_io *)ctsio);
7407 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7409 struct ctl_lun *lun;
7410 struct scsi_report_supported_tmf *cdb;
7411 struct scsi_report_supported_tmf_data *data;
7413 int alloc_len, total_len;
7415 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7417 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7418 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7420 retval = CTL_RETVAL_COMPLETE;
7422 total_len = sizeof(struct scsi_report_supported_tmf_data);
7423 alloc_len = scsi_4btoul(cdb->length);
7425 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7427 ctsio->kern_sg_entries = 0;
7429 if (total_len < alloc_len) {
7430 ctsio->residual = alloc_len - total_len;
7431 ctsio->kern_data_len = total_len;
7432 ctsio->kern_total_len = total_len;
7434 ctsio->residual = 0;
7435 ctsio->kern_data_len = alloc_len;
7436 ctsio->kern_total_len = alloc_len;
7438 ctsio->kern_data_resid = 0;
7439 ctsio->kern_rel_offset = 0;
7441 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7442 data->byte1 |= RST_ATS | RST_LURS | RST_TRS;
7444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7445 ctsio->be_move_done = ctl_config_move_done;
7447 ctl_datamove((union ctl_io *)ctsio);
7452 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7454 struct ctl_lun *lun;
7455 struct scsi_report_timestamp *cdb;
7456 struct scsi_report_timestamp_data *data;
7460 int alloc_len, total_len;
7462 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7464 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7465 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7467 retval = CTL_RETVAL_COMPLETE;
7469 total_len = sizeof(struct scsi_report_timestamp_data);
7470 alloc_len = scsi_4btoul(cdb->length);
7472 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7474 ctsio->kern_sg_entries = 0;
7476 if (total_len < alloc_len) {
7477 ctsio->residual = alloc_len - total_len;
7478 ctsio->kern_data_len = total_len;
7479 ctsio->kern_total_len = total_len;
7481 ctsio->residual = 0;
7482 ctsio->kern_data_len = alloc_len;
7483 ctsio->kern_total_len = alloc_len;
7485 ctsio->kern_data_resid = 0;
7486 ctsio->kern_rel_offset = 0;
7488 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7489 scsi_ulto2b(sizeof(*data) - 2, data->length);
7490 data->origin = RTS_ORIG_OUTSIDE;
7492 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7493 scsi_ulto4b(timestamp >> 16, data->timestamp);
7494 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7496 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7497 ctsio->be_move_done = ctl_config_move_done;
7499 ctl_datamove((union ctl_io *)ctsio);
7504 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7506 struct scsi_per_res_in *cdb;
7507 int alloc_len, total_len = 0;
7508 /* struct scsi_per_res_in_rsrv in_data; */
7509 struct ctl_lun *lun;
7510 struct ctl_softc *softc;
7512 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7514 softc = control_softc;
7516 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7518 alloc_len = scsi_2btoul(cdb->length);
7520 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7523 mtx_lock(&lun->lun_lock);
7524 switch (cdb->action) {
7525 case SPRI_RK: /* read keys */
7526 total_len = sizeof(struct scsi_per_res_in_keys) +
7528 sizeof(struct scsi_per_res_key);
7530 case SPRI_RR: /* read reservation */
7531 if (lun->flags & CTL_LUN_PR_RESERVED)
7532 total_len = sizeof(struct scsi_per_res_in_rsrv);
7534 total_len = sizeof(struct scsi_per_res_in_header);
7536 case SPRI_RC: /* report capabilities */
7537 total_len = sizeof(struct scsi_per_res_cap);
7540 panic("Invalid PR type %x", cdb->action);
7542 mtx_unlock(&lun->lun_lock);
7544 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7546 if (total_len < alloc_len) {
7547 ctsio->residual = alloc_len - total_len;
7548 ctsio->kern_data_len = total_len;
7549 ctsio->kern_total_len = total_len;
7551 ctsio->residual = 0;
7552 ctsio->kern_data_len = alloc_len;
7553 ctsio->kern_total_len = alloc_len;
7556 ctsio->kern_data_resid = 0;
7557 ctsio->kern_rel_offset = 0;
7558 ctsio->kern_sg_entries = 0;
7560 mtx_lock(&lun->lun_lock);
7561 switch (cdb->action) {
7562 case SPRI_RK: { // read keys
7563 struct scsi_per_res_in_keys *res_keys;
7566 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7569 * We had to drop the lock to allocate our buffer, which
7570 * leaves time for someone to come in with another
7571 * persistent reservation. (That is unlikely, though,
7572 * since this should be the only persistent reservation
7573 * command active right now.)
7575 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7576 (lun->pr_key_count *
7577 sizeof(struct scsi_per_res_key)))){
7578 mtx_unlock(&lun->lun_lock);
7579 free(ctsio->kern_data_ptr, M_CTL);
7580 printf("%s: reservation length changed, retrying\n",
7585 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7587 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7588 lun->pr_key_count, res_keys->header.length);
7590 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7591 if (!lun->per_res[i].registered)
7595 * We used lun->pr_key_count to calculate the
7596 * size to allocate. If it turns out the number of
7597 * initiators with the registered flag set is
7598 * larger than that (i.e. they haven't been kept in
7599 * sync), we've got a problem.
7601 if (key_count >= lun->pr_key_count) {
7603 csevent_log(CSC_CTL | CSC_SHELF_SW |
7605 csevent_LogType_Fault,
7606 csevent_AlertLevel_Yellow,
7607 csevent_FRU_ShelfController,
7608 csevent_FRU_Firmware,
7609 csevent_FRU_Unknown,
7610 "registered keys %d >= key "
7611 "count %d", key_count,
7617 memcpy(res_keys->keys[key_count].key,
7618 lun->per_res[i].res_key.key,
7619 ctl_min(sizeof(res_keys->keys[key_count].key),
7620 sizeof(lun->per_res[i].res_key)));
7625 case SPRI_RR: { // read reservation
7626 struct scsi_per_res_in_rsrv *res;
7627 int tmp_len, header_only;
7629 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7631 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7633 if (lun->flags & CTL_LUN_PR_RESERVED)
7635 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7636 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7637 res->header.length);
7640 tmp_len = sizeof(struct scsi_per_res_in_header);
7641 scsi_ulto4b(0, res->header.length);
7646 * We had to drop the lock to allocate our buffer, which
7647 * leaves time for someone to come in with another
7648 * persistent reservation. (That is unlikely, though,
7649 * since this should be the only persistent reservation
7650 * command active right now.)
7652 if (tmp_len != total_len) {
7653 mtx_unlock(&lun->lun_lock);
7654 free(ctsio->kern_data_ptr, M_CTL);
7655 printf("%s: reservation status changed, retrying\n",
7661 * No reservation held, so we're done.
7663 if (header_only != 0)
7667 * If the registration is an All Registrants type, the key
7668 * is 0, since it doesn't really matter.
7670 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7671 memcpy(res->data.reservation,
7672 &lun->per_res[lun->pr_res_idx].res_key,
7673 sizeof(struct scsi_per_res_key));
7675 res->data.scopetype = lun->res_type;
7678 case SPRI_RC: //report capabilities
7680 struct scsi_per_res_cap *res_cap;
7683 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7684 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7685 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7686 type_mask = SPRI_TM_WR_EX_AR |
7692 scsi_ulto2b(type_mask, res_cap->type_mask);
7695 case SPRI_RS: //read full status
7698 * This is a bug, because we just checked for this above,
7699 * and should have returned an error.
7701 panic("Invalid PR type %x", cdb->action);
7702 break; /* NOTREACHED */
7704 mtx_unlock(&lun->lun_lock);
7706 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7707 ctsio->be_move_done = ctl_config_move_done;
7709 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7710 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7711 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7712 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7713 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7715 ctl_datamove((union ctl_io *)ctsio);
7717 return (CTL_RETVAL_COMPLETE);
7721 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7725 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7726 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7727 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7728 struct scsi_per_res_out_parms* param)
7730 union ctl_ha_msg persis_io;
7736 mtx_lock(&lun->lun_lock);
7737 if (sa_res_key == 0) {
7738 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7739 /* validate scope and type */
7740 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7742 mtx_unlock(&lun->lun_lock);
7743 ctl_set_invalid_field(/*ctsio*/ ctsio,
7749 ctl_done((union ctl_io *)ctsio);
7753 if (type>8 || type==2 || type==4 || type==0) {
7754 mtx_unlock(&lun->lun_lock);
7755 ctl_set_invalid_field(/*ctsio*/ ctsio,
7761 ctl_done((union ctl_io *)ctsio);
7765 /* temporarily unregister this nexus */
7766 lun->per_res[residx].registered = 0;
7769 * Unregister everybody else and build UA for
7772 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7773 if (lun->per_res[i].registered == 0)
7777 && i <CTL_MAX_INITIATORS)
7778 lun->pending_sense[i].ua_pending |=
7780 else if (persis_offset
7781 && i >= persis_offset)
7782 lun->pending_sense[i-persis_offset
7785 lun->per_res[i].registered = 0;
7786 memset(&lun->per_res[i].res_key, 0,
7787 sizeof(struct scsi_per_res_key));
7789 lun->per_res[residx].registered = 1;
7790 lun->pr_key_count = 1;
7791 lun->res_type = type;
7792 if (lun->res_type != SPR_TYPE_WR_EX_AR
7793 && lun->res_type != SPR_TYPE_EX_AC_AR)
7794 lun->pr_res_idx = residx;
7796 /* send msg to other side */
7797 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7798 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7799 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7800 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7801 persis_io.pr.pr_info.res_type = type;
7802 memcpy(persis_io.pr.pr_info.sa_res_key,
7803 param->serv_act_res_key,
7804 sizeof(param->serv_act_res_key));
7805 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7806 &persis_io, sizeof(persis_io), 0)) >
7807 CTL_HA_STATUS_SUCCESS) {
7808 printf("CTL:Persis Out error returned "
7809 "from ctl_ha_msg_send %d\n",
7813 /* not all registrants */
7814 mtx_unlock(&lun->lun_lock);
7815 free(ctsio->kern_data_ptr, M_CTL);
7816 ctl_set_invalid_field(ctsio,
7822 ctl_done((union ctl_io *)ctsio);
7825 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7826 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7829 if (res_key == sa_res_key) {
7832 * The spec implies this is not good but doesn't
7833 * say what to do. There are two choices either
7834 * generate a res conflict or check condition
7835 * with illegal field in parameter data. Since
7836 * that is what is done when the sa_res_key is
7837 * zero I'll take that approach since this has
7838 * to do with the sa_res_key.
7840 mtx_unlock(&lun->lun_lock);
7841 free(ctsio->kern_data_ptr, M_CTL);
7842 ctl_set_invalid_field(ctsio,
7848 ctl_done((union ctl_io *)ctsio);
7852 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7853 if (lun->per_res[i].registered
7854 && memcmp(param->serv_act_res_key,
7855 lun->per_res[i].res_key.key,
7856 sizeof(struct scsi_per_res_key)) != 0)
7860 lun->per_res[i].registered = 0;
7861 memset(&lun->per_res[i].res_key, 0,
7862 sizeof(struct scsi_per_res_key));
7863 lun->pr_key_count--;
7866 && i < CTL_MAX_INITIATORS)
7867 lun->pending_sense[i].ua_pending |=
7869 else if (persis_offset
7870 && i >= persis_offset)
7871 lun->pending_sense[i-persis_offset].ua_pending|=
7875 mtx_unlock(&lun->lun_lock);
7876 free(ctsio->kern_data_ptr, M_CTL);
7877 ctl_set_reservation_conflict(ctsio);
7878 ctl_done((union ctl_io *)ctsio);
7879 return (CTL_RETVAL_COMPLETE);
7881 /* send msg to other side */
7882 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7883 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7884 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7885 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7886 persis_io.pr.pr_info.res_type = type;
7887 memcpy(persis_io.pr.pr_info.sa_res_key,
7888 param->serv_act_res_key,
7889 sizeof(param->serv_act_res_key));
7890 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7891 &persis_io, sizeof(persis_io), 0)) >
7892 CTL_HA_STATUS_SUCCESS) {
7893 printf("CTL:Persis Out error returned from "
7894 "ctl_ha_msg_send %d\n", isc_retval);
7897 /* Reserved but not all registrants */
7898 /* sa_res_key is res holder */
7899 if (memcmp(param->serv_act_res_key,
7900 lun->per_res[lun->pr_res_idx].res_key.key,
7901 sizeof(struct scsi_per_res_key)) == 0) {
7902 /* validate scope and type */
7903 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7905 mtx_unlock(&lun->lun_lock);
7906 ctl_set_invalid_field(/*ctsio*/ ctsio,
7912 ctl_done((union ctl_io *)ctsio);
7916 if (type>8 || type==2 || type==4 || type==0) {
7917 mtx_unlock(&lun->lun_lock);
7918 ctl_set_invalid_field(/*ctsio*/ ctsio,
7924 ctl_done((union ctl_io *)ctsio);
7930 * if sa_res_key != res_key remove all
7931 * registrants w/sa_res_key and generate UA
7932 * for these registrants(Registrations
7933 * Preempted) if it wasn't an exclusive
7934 * reservation generate UA(Reservations
7935 * Preempted) for all other registered nexuses
7936 * if the type has changed. Establish the new
7937 * reservation and holder. If res_key and
7938 * sa_res_key are the same do the above
7939 * except don't unregister the res holder.
7943 * Temporarily unregister so it won't get
7944 * removed or UA generated
7946 lun->per_res[residx].registered = 0;
7947 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7948 if (lun->per_res[i].registered == 0)
7951 if (memcmp(param->serv_act_res_key,
7952 lun->per_res[i].res_key.key,
7953 sizeof(struct scsi_per_res_key)) == 0) {
7954 lun->per_res[i].registered = 0;
7955 memset(&lun->per_res[i].res_key,
7957 sizeof(struct scsi_per_res_key));
7958 lun->pr_key_count--;
7961 && i < CTL_MAX_INITIATORS)
7962 lun->pending_sense[i
7965 else if (persis_offset
7966 && i >= persis_offset)
7968 i-persis_offset].ua_pending |=
7970 } else if (type != lun->res_type
7971 && (lun->res_type == SPR_TYPE_WR_EX_RO
7972 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7974 && i < CTL_MAX_INITIATORS)
7975 lun->pending_sense[i
7978 else if (persis_offset
7979 && i >= persis_offset)
7986 lun->per_res[residx].registered = 1;
7987 lun->res_type = type;
7988 if (lun->res_type != SPR_TYPE_WR_EX_AR
7989 && lun->res_type != SPR_TYPE_EX_AC_AR)
7990 lun->pr_res_idx = residx;
7993 CTL_PR_ALL_REGISTRANTS;
7995 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7996 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7997 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7998 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7999 persis_io.pr.pr_info.res_type = type;
8000 memcpy(persis_io.pr.pr_info.sa_res_key,
8001 param->serv_act_res_key,
8002 sizeof(param->serv_act_res_key));
8003 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8004 &persis_io, sizeof(persis_io), 0)) >
8005 CTL_HA_STATUS_SUCCESS) {
8006 printf("CTL:Persis Out error returned "
8007 "from ctl_ha_msg_send %d\n",
8012 * sa_res_key is not the res holder just
8013 * remove registrants
8017 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8018 if (memcmp(param->serv_act_res_key,
8019 lun->per_res[i].res_key.key,
8020 sizeof(struct scsi_per_res_key)) != 0)
8024 lun->per_res[i].registered = 0;
8025 memset(&lun->per_res[i].res_key, 0,
8026 sizeof(struct scsi_per_res_key));
8027 lun->pr_key_count--;
8030 && i < CTL_MAX_INITIATORS)
8031 lun->pending_sense[i].ua_pending |=
8033 else if (persis_offset
8034 && i >= persis_offset)
8036 i-persis_offset].ua_pending |=
8041 mtx_unlock(&lun->lun_lock);
8042 free(ctsio->kern_data_ptr, M_CTL);
8043 ctl_set_reservation_conflict(ctsio);
8044 ctl_done((union ctl_io *)ctsio);
8047 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8048 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8049 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8050 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8051 persis_io.pr.pr_info.res_type = type;
8052 memcpy(persis_io.pr.pr_info.sa_res_key,
8053 param->serv_act_res_key,
8054 sizeof(param->serv_act_res_key));
8055 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8056 &persis_io, sizeof(persis_io), 0)) >
8057 CTL_HA_STATUS_SUCCESS) {
8058 printf("CTL:Persis Out error returned "
8059 "from ctl_ha_msg_send %d\n",
8065 lun->PRGeneration++;
8066 mtx_unlock(&lun->lun_lock);
8072 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8076 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8077 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8078 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8079 msg->pr.pr_info.sa_res_key,
8080 sizeof(struct scsi_per_res_key)) != 0) {
8081 uint64_t sa_res_key;
8082 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8084 if (sa_res_key == 0) {
8085 /* temporarily unregister this nexus */
8086 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8089 * Unregister everybody else and build UA for
8092 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8093 if (lun->per_res[i].registered == 0)
8097 && i < CTL_MAX_INITIATORS)
8098 lun->pending_sense[i].ua_pending |=
8100 else if (persis_offset && i >= persis_offset)
8101 lun->pending_sense[i -
8102 persis_offset].ua_pending |=
8104 lun->per_res[i].registered = 0;
8105 memset(&lun->per_res[i].res_key, 0,
8106 sizeof(struct scsi_per_res_key));
8109 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8110 lun->pr_key_count = 1;
8111 lun->res_type = msg->pr.pr_info.res_type;
8112 if (lun->res_type != SPR_TYPE_WR_EX_AR
8113 && lun->res_type != SPR_TYPE_EX_AC_AR)
8114 lun->pr_res_idx = msg->pr.pr_info.residx;
8116 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8117 if (memcmp(msg->pr.pr_info.sa_res_key,
8118 lun->per_res[i].res_key.key,
8119 sizeof(struct scsi_per_res_key)) != 0)
8122 lun->per_res[i].registered = 0;
8123 memset(&lun->per_res[i].res_key, 0,
8124 sizeof(struct scsi_per_res_key));
8125 lun->pr_key_count--;
8128 && i < persis_offset)
8129 lun->pending_sense[i].ua_pending |=
8131 else if (persis_offset
8132 && i >= persis_offset)
8133 lun->pending_sense[i -
8134 persis_offset].ua_pending |=
8140 * Temporarily unregister so it won't get removed
8143 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8144 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8145 if (lun->per_res[i].registered == 0)
8148 if (memcmp(msg->pr.pr_info.sa_res_key,
8149 lun->per_res[i].res_key.key,
8150 sizeof(struct scsi_per_res_key)) == 0) {
8151 lun->per_res[i].registered = 0;
8152 memset(&lun->per_res[i].res_key, 0,
8153 sizeof(struct scsi_per_res_key));
8154 lun->pr_key_count--;
8156 && i < CTL_MAX_INITIATORS)
8157 lun->pending_sense[i].ua_pending |=
8159 else if (persis_offset
8160 && i >= persis_offset)
8161 lun->pending_sense[i -
8162 persis_offset].ua_pending |=
8164 } else if (msg->pr.pr_info.res_type != lun->res_type
8165 && (lun->res_type == SPR_TYPE_WR_EX_RO
8166 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8168 && i < persis_offset)
8169 lun->pending_sense[i
8172 else if (persis_offset
8173 && i >= persis_offset)
8174 lun->pending_sense[i -
8175 persis_offset].ua_pending |=
8179 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8180 lun->res_type = msg->pr.pr_info.res_type;
8181 if (lun->res_type != SPR_TYPE_WR_EX_AR
8182 && lun->res_type != SPR_TYPE_EX_AC_AR)
8183 lun->pr_res_idx = msg->pr.pr_info.residx;
8185 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8187 lun->PRGeneration++;
8193 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8197 u_int32_t param_len;
8198 struct scsi_per_res_out *cdb;
8199 struct ctl_lun *lun;
8200 struct scsi_per_res_out_parms* param;
8201 struct ctl_softc *softc;
8203 uint64_t res_key, sa_res_key;
8205 union ctl_ha_msg persis_io;
8208 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8210 retval = CTL_RETVAL_COMPLETE;
8212 softc = control_softc;
8214 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8215 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8218 * We only support whole-LUN scope. The scope & type are ignored for
8219 * register, register and ignore existing key and clear.
8220 * We sometimes ignore scope and type on preempts too!!
8221 * Verify reservation type here as well.
8223 type = cdb->scope_type & SPR_TYPE_MASK;
8224 if ((cdb->action == SPRO_RESERVE)
8225 || (cdb->action == SPRO_RELEASE)) {
8226 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8227 ctl_set_invalid_field(/*ctsio*/ ctsio,
8233 ctl_done((union ctl_io *)ctsio);
8234 return (CTL_RETVAL_COMPLETE);
8237 if (type>8 || type==2 || type==4 || type==0) {
8238 ctl_set_invalid_field(/*ctsio*/ ctsio,
8244 ctl_done((union ctl_io *)ctsio);
8245 return (CTL_RETVAL_COMPLETE);
8249 param_len = scsi_4btoul(cdb->length);
8251 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8252 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8253 ctsio->kern_data_len = param_len;
8254 ctsio->kern_total_len = param_len;
8255 ctsio->kern_data_resid = 0;
8256 ctsio->kern_rel_offset = 0;
8257 ctsio->kern_sg_entries = 0;
8258 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8259 ctsio->be_move_done = ctl_config_move_done;
8260 ctl_datamove((union ctl_io *)ctsio);
8262 return (CTL_RETVAL_COMPLETE);
8265 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8267 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8268 res_key = scsi_8btou64(param->res_key.key);
8269 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8272 * Validate the reservation key here except for SPRO_REG_IGNO
8273 * This must be done for all other service actions
8275 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8276 mtx_lock(&lun->lun_lock);
8277 if (lun->per_res[residx].registered) {
8278 if (memcmp(param->res_key.key,
8279 lun->per_res[residx].res_key.key,
8280 ctl_min(sizeof(param->res_key),
8281 sizeof(lun->per_res[residx].res_key))) != 0) {
8283 * The current key passed in doesn't match
8284 * the one the initiator previously
8287 mtx_unlock(&lun->lun_lock);
8288 free(ctsio->kern_data_ptr, M_CTL);
8289 ctl_set_reservation_conflict(ctsio);
8290 ctl_done((union ctl_io *)ctsio);
8291 return (CTL_RETVAL_COMPLETE);
8293 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8295 * We are not registered
8297 mtx_unlock(&lun->lun_lock);
8298 free(ctsio->kern_data_ptr, M_CTL);
8299 ctl_set_reservation_conflict(ctsio);
8300 ctl_done((union ctl_io *)ctsio);
8301 return (CTL_RETVAL_COMPLETE);
8302 } else if (res_key != 0) {
8304 * We are not registered and trying to register but
8305 * the register key isn't zero.
8307 mtx_unlock(&lun->lun_lock);
8308 free(ctsio->kern_data_ptr, M_CTL);
8309 ctl_set_reservation_conflict(ctsio);
8310 ctl_done((union ctl_io *)ctsio);
8311 return (CTL_RETVAL_COMPLETE);
8313 mtx_unlock(&lun->lun_lock);
8316 switch (cdb->action & SPRO_ACTION_MASK) {
8318 case SPRO_REG_IGNO: {
8321 printf("Registration received\n");
8325 * We don't support any of these options, as we report in
8326 * the read capabilities request (see
8327 * ctl_persistent_reserve_in(), above).
8329 if ((param->flags & SPR_SPEC_I_PT)
8330 || (param->flags & SPR_ALL_TG_PT)
8331 || (param->flags & SPR_APTPL)) {
8334 if (param->flags & SPR_APTPL)
8336 else if (param->flags & SPR_ALL_TG_PT)
8338 else /* SPR_SPEC_I_PT */
8341 free(ctsio->kern_data_ptr, M_CTL);
8342 ctl_set_invalid_field(ctsio,
8348 ctl_done((union ctl_io *)ctsio);
8349 return (CTL_RETVAL_COMPLETE);
8352 mtx_lock(&lun->lun_lock);
8355 * The initiator wants to clear the
8358 if (sa_res_key == 0) {
8360 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8361 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8362 && !lun->per_res[residx].registered)) {
8363 mtx_unlock(&lun->lun_lock);
8367 lun->per_res[residx].registered = 0;
8368 memset(&lun->per_res[residx].res_key,
8369 0, sizeof(lun->per_res[residx].res_key));
8370 lun->pr_key_count--;
8372 if (residx == lun->pr_res_idx) {
8373 lun->flags &= ~CTL_LUN_PR_RESERVED;
8374 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8376 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8377 || lun->res_type == SPR_TYPE_EX_AC_RO)
8378 && lun->pr_key_count) {
8380 * If the reservation is a registrants
8381 * only type we need to generate a UA
8382 * for other registered inits. The
8383 * sense code should be RESERVATIONS
8387 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8389 i+persis_offset].registered
8392 lun->pending_sense[i
8398 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8399 if (lun->pr_key_count==0) {
8400 lun->flags &= ~CTL_LUN_PR_RESERVED;
8402 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8405 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8406 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8407 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8408 persis_io.pr.pr_info.residx = residx;
8409 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8410 &persis_io, sizeof(persis_io), 0 )) >
8411 CTL_HA_STATUS_SUCCESS) {
8412 printf("CTL:Persis Out error returned from "
8413 "ctl_ha_msg_send %d\n", isc_retval);
8415 } else /* sa_res_key != 0 */ {
8418 * If we aren't registered currently then increment
8419 * the key count and set the registered flag.
8421 if (!lun->per_res[residx].registered) {
8422 lun->pr_key_count++;
8423 lun->per_res[residx].registered = 1;
8426 memcpy(&lun->per_res[residx].res_key,
8427 param->serv_act_res_key,
8428 ctl_min(sizeof(param->serv_act_res_key),
8429 sizeof(lun->per_res[residx].res_key)));
8431 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8432 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8433 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8434 persis_io.pr.pr_info.residx = residx;
8435 memcpy(persis_io.pr.pr_info.sa_res_key,
8436 param->serv_act_res_key,
8437 sizeof(param->serv_act_res_key));
8438 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8439 &persis_io, sizeof(persis_io), 0)) >
8440 CTL_HA_STATUS_SUCCESS) {
8441 printf("CTL:Persis Out error returned from "
8442 "ctl_ha_msg_send %d\n", isc_retval);
8445 lun->PRGeneration++;
8446 mtx_unlock(&lun->lun_lock);
8452 printf("Reserve executed type %d\n", type);
8454 mtx_lock(&lun->lun_lock);
8455 if (lun->flags & CTL_LUN_PR_RESERVED) {
8457 * if this isn't the reservation holder and it's
8458 * not a "all registrants" type or if the type is
8459 * different then we have a conflict
8461 if ((lun->pr_res_idx != residx
8462 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8463 || lun->res_type != type) {
8464 mtx_unlock(&lun->lun_lock);
8465 free(ctsio->kern_data_ptr, M_CTL);
8466 ctl_set_reservation_conflict(ctsio);
8467 ctl_done((union ctl_io *)ctsio);
8468 return (CTL_RETVAL_COMPLETE);
8470 mtx_unlock(&lun->lun_lock);
8471 } else /* create a reservation */ {
8473 * If it's not an "all registrants" type record
8474 * reservation holder
8476 if (type != SPR_TYPE_WR_EX_AR
8477 && type != SPR_TYPE_EX_AC_AR)
8478 lun->pr_res_idx = residx; /* Res holder */
8480 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8482 lun->flags |= CTL_LUN_PR_RESERVED;
8483 lun->res_type = type;
8485 mtx_unlock(&lun->lun_lock);
8487 /* send msg to other side */
8488 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8489 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8490 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8491 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8492 persis_io.pr.pr_info.res_type = type;
8493 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8494 &persis_io, sizeof(persis_io), 0)) >
8495 CTL_HA_STATUS_SUCCESS) {
8496 printf("CTL:Persis Out error returned from "
8497 "ctl_ha_msg_send %d\n", isc_retval);
8503 mtx_lock(&lun->lun_lock);
8504 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8505 /* No reservation exists return good status */
8506 mtx_unlock(&lun->lun_lock);
8510 * Is this nexus a reservation holder?
8512 if (lun->pr_res_idx != residx
8513 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8515 * not a res holder return good status but
8518 mtx_unlock(&lun->lun_lock);
8522 if (lun->res_type != type) {
8523 mtx_unlock(&lun->lun_lock);
8524 free(ctsio->kern_data_ptr, M_CTL);
8525 ctl_set_illegal_pr_release(ctsio);
8526 ctl_done((union ctl_io *)ctsio);
8527 return (CTL_RETVAL_COMPLETE);
8530 /* okay to release */
8531 lun->flags &= ~CTL_LUN_PR_RESERVED;
8532 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8536 * if this isn't an exclusive access
8537 * res generate UA for all other
8540 if (type != SPR_TYPE_EX_AC
8541 && type != SPR_TYPE_WR_EX) {
8543 * temporarily unregister so we don't generate UA
8545 lun->per_res[residx].registered = 0;
8547 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8548 if (lun->per_res[i+persis_offset].registered
8551 lun->pending_sense[i].ua_pending |=
8555 lun->per_res[residx].registered = 1;
8557 mtx_unlock(&lun->lun_lock);
8558 /* Send msg to other side */
8559 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8560 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8561 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8562 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8563 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8564 printf("CTL:Persis Out error returned from "
8565 "ctl_ha_msg_send %d\n", isc_retval);
8570 /* send msg to other side */
8572 mtx_lock(&lun->lun_lock);
8573 lun->flags &= ~CTL_LUN_PR_RESERVED;
8575 lun->pr_key_count = 0;
8576 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8579 memset(&lun->per_res[residx].res_key,
8580 0, sizeof(lun->per_res[residx].res_key));
8581 lun->per_res[residx].registered = 0;
8583 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8584 if (lun->per_res[i].registered) {
8585 if (!persis_offset && i < CTL_MAX_INITIATORS)
8586 lun->pending_sense[i].ua_pending |=
8588 else if (persis_offset && i >= persis_offset)
8589 lun->pending_sense[i-persis_offset
8590 ].ua_pending |= CTL_UA_RES_PREEMPT;
8592 memset(&lun->per_res[i].res_key,
8593 0, sizeof(struct scsi_per_res_key));
8594 lun->per_res[i].registered = 0;
8596 lun->PRGeneration++;
8597 mtx_unlock(&lun->lun_lock);
8598 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8599 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8600 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8601 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8602 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8603 printf("CTL:Persis Out error returned from "
8604 "ctl_ha_msg_send %d\n", isc_retval);
8608 case SPRO_PREEMPT: {
8611 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8612 residx, ctsio, cdb, param);
8614 return (CTL_RETVAL_COMPLETE);
8618 panic("Invalid PR type %x", cdb->action);
8622 free(ctsio->kern_data_ptr, M_CTL);
8623 ctl_set_success(ctsio);
8624 ctl_done((union ctl_io *)ctsio);
8630 * This routine is for handling a message from the other SC pertaining to
8631 * persistent reserve out. All the error checking will have been done
8632 * so only perorming the action need be done here to keep the two
8636 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8638 struct ctl_lun *lun;
8639 struct ctl_softc *softc;
8643 softc = control_softc;
8645 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8646 lun = softc->ctl_luns[targ_lun];
8647 mtx_lock(&lun->lun_lock);
8648 switch(msg->pr.pr_info.action) {
8649 case CTL_PR_REG_KEY:
8650 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8651 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8652 lun->pr_key_count++;
8654 lun->PRGeneration++;
8655 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8656 msg->pr.pr_info.sa_res_key,
8657 sizeof(struct scsi_per_res_key));
8660 case CTL_PR_UNREG_KEY:
8661 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8662 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8663 0, sizeof(struct scsi_per_res_key));
8664 lun->pr_key_count--;
8666 /* XXX Need to see if the reservation has been released */
8667 /* if so do we need to generate UA? */
8668 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8669 lun->flags &= ~CTL_LUN_PR_RESERVED;
8670 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8672 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8673 || lun->res_type == SPR_TYPE_EX_AC_RO)
8674 && lun->pr_key_count) {
8676 * If the reservation is a registrants
8677 * only type we need to generate a UA
8678 * for other registered inits. The
8679 * sense code should be RESERVATIONS
8683 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8685 persis_offset].registered == 0)
8688 lun->pending_sense[i
8694 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8695 if (lun->pr_key_count==0) {
8696 lun->flags &= ~CTL_LUN_PR_RESERVED;
8698 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8701 lun->PRGeneration++;
8704 case CTL_PR_RESERVE:
8705 lun->flags |= CTL_LUN_PR_RESERVED;
8706 lun->res_type = msg->pr.pr_info.res_type;
8707 lun->pr_res_idx = msg->pr.pr_info.residx;
8711 case CTL_PR_RELEASE:
8713 * if this isn't an exclusive access res generate UA for all
8714 * other registrants.
8716 if (lun->res_type != SPR_TYPE_EX_AC
8717 && lun->res_type != SPR_TYPE_WR_EX) {
8718 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8719 if (lun->per_res[i+persis_offset].registered)
8720 lun->pending_sense[i].ua_pending |=
8724 lun->flags &= ~CTL_LUN_PR_RESERVED;
8725 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8729 case CTL_PR_PREEMPT:
8730 ctl_pro_preempt_other(lun, msg);
8733 lun->flags &= ~CTL_LUN_PR_RESERVED;
8735 lun->pr_key_count = 0;
8736 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8738 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8739 if (lun->per_res[i].registered == 0)
8742 && i < CTL_MAX_INITIATORS)
8743 lun->pending_sense[i].ua_pending |=
8745 else if (persis_offset
8746 && i >= persis_offset)
8747 lun->pending_sense[i-persis_offset].ua_pending|=
8749 memset(&lun->per_res[i].res_key, 0,
8750 sizeof(struct scsi_per_res_key));
8751 lun->per_res[i].registered = 0;
8753 lun->PRGeneration++;
8757 mtx_unlock(&lun->lun_lock);
8761 ctl_read_write(struct ctl_scsiio *ctsio)
8763 struct ctl_lun *lun;
8764 struct ctl_lba_len_flags *lbalen;
8766 uint32_t num_blocks;
8771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8773 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8778 retval = CTL_RETVAL_COMPLETE;
8780 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8781 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8782 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8786 * XXX KDM need a lock here.
8788 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8789 if ((lun->res_type == SPR_TYPE_EX_AC
8790 && residx != lun->pr_res_idx)
8791 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8792 || lun->res_type == SPR_TYPE_EX_AC_AR)
8793 && !lun->per_res[residx].registered)) {
8794 ctl_set_reservation_conflict(ctsio);
8795 ctl_done((union ctl_io *)ctsio);
8796 return (CTL_RETVAL_COMPLETE);
8800 switch (ctsio->cdb[0]) {
8803 struct scsi_rw_6 *cdb;
8805 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8807 lba = scsi_3btoul(cdb->addr);
8808 /* only 5 bits are valid in the most significant address byte */
8810 num_blocks = cdb->length;
8812 * This is correct according to SBC-2.
8814 if (num_blocks == 0)
8820 struct scsi_rw_10 *cdb;
8822 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8824 if (cdb->byte2 & SRW10_FUA)
8826 if (cdb->byte2 & SRW10_DPO)
8829 lba = scsi_4btoul(cdb->addr);
8830 num_blocks = scsi_2btoul(cdb->length);
8833 case WRITE_VERIFY_10: {
8834 struct scsi_write_verify_10 *cdb;
8836 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8839 * XXX KDM we should do actual write verify support at some
8840 * point. This is obviously fake, we're just translating
8841 * things to a write. So we don't even bother checking the
8842 * BYTCHK field, since we don't do any verification. If
8843 * the user asks for it, we'll just pretend we did it.
8845 if (cdb->byte2 & SWV_DPO)
8848 lba = scsi_4btoul(cdb->addr);
8849 num_blocks = scsi_2btoul(cdb->length);
8854 struct scsi_rw_12 *cdb;
8856 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8858 if (cdb->byte2 & SRW12_FUA)
8860 if (cdb->byte2 & SRW12_DPO)
8862 lba = scsi_4btoul(cdb->addr);
8863 num_blocks = scsi_4btoul(cdb->length);
8866 case WRITE_VERIFY_12: {
8867 struct scsi_write_verify_12 *cdb;
8869 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8871 if (cdb->byte2 & SWV_DPO)
8874 lba = scsi_4btoul(cdb->addr);
8875 num_blocks = scsi_4btoul(cdb->length);
8881 struct scsi_rw_16 *cdb;
8883 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8885 if (cdb->byte2 & SRW12_FUA)
8887 if (cdb->byte2 & SRW12_DPO)
8890 lba = scsi_8btou64(cdb->addr);
8891 num_blocks = scsi_4btoul(cdb->length);
8894 case WRITE_VERIFY_16: {
8895 struct scsi_write_verify_16 *cdb;
8897 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8899 if (cdb->byte2 & SWV_DPO)
8902 lba = scsi_8btou64(cdb->addr);
8903 num_blocks = scsi_4btoul(cdb->length);
8908 * We got a command we don't support. This shouldn't
8909 * happen, commands should be filtered out above us.
8911 ctl_set_invalid_opcode(ctsio);
8912 ctl_done((union ctl_io *)ctsio);
8914 return (CTL_RETVAL_COMPLETE);
8915 break; /* NOTREACHED */
8919 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8920 * interesting for us, but if RAIDCore is in write-back mode,
8921 * getting it to do write-through for a particular transaction may
8926 * The first check is to make sure we're in bounds, the second
8927 * check is to catch wrap-around problems. If the lba + num blocks
8928 * is less than the lba, then we've wrapped around and the block
8929 * range is invalid anyway.
8931 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8932 || ((lba + num_blocks) < lba)) {
8933 ctl_set_lba_out_of_range(ctsio);
8934 ctl_done((union ctl_io *)ctsio);
8935 return (CTL_RETVAL_COMPLETE);
8939 * According to SBC-3, a transfer length of 0 is not an error.
8940 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8941 * translates to 256 blocks for those commands.
8943 if (num_blocks == 0) {
8944 ctl_set_success(ctsio);
8945 ctl_done((union ctl_io *)ctsio);
8946 return (CTL_RETVAL_COMPLETE);
8949 lbalen = (struct ctl_lba_len_flags *)
8950 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8952 lbalen->len = num_blocks;
8953 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8955 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8956 ctsio->kern_rel_offset = 0;
8958 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8960 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8966 ctl_cnw_cont(union ctl_io *io)
8968 struct ctl_scsiio *ctsio;
8969 struct ctl_lun *lun;
8970 struct ctl_lba_len_flags *lbalen;
8973 ctsio = &io->scsiio;
8974 ctsio->io_hdr.status = CTL_STATUS_NONE;
8975 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8977 lbalen = (struct ctl_lba_len_flags *)
8978 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8979 lbalen->flags = CTL_LLF_WRITE;
8981 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8982 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8987 ctl_cnw(struct ctl_scsiio *ctsio)
8989 struct ctl_lun *lun;
8990 struct ctl_lba_len_flags *lbalen;
8992 uint32_t num_blocks;
8996 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8998 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9003 retval = CTL_RETVAL_COMPLETE;
9005 switch (ctsio->cdb[0]) {
9006 case COMPARE_AND_WRITE: {
9007 struct scsi_compare_and_write *cdb;
9009 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9011 if (cdb->byte2 & SRW10_FUA)
9013 if (cdb->byte2 & SRW10_DPO)
9015 lba = scsi_8btou64(cdb->addr);
9016 num_blocks = cdb->length;
9021 * We got a command we don't support. This shouldn't
9022 * happen, commands should be filtered out above us.
9024 ctl_set_invalid_opcode(ctsio);
9025 ctl_done((union ctl_io *)ctsio);
9027 return (CTL_RETVAL_COMPLETE);
9028 break; /* NOTREACHED */
9032 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9033 * interesting for us, but if RAIDCore is in write-back mode,
9034 * getting it to do write-through for a particular transaction may
9039 * The first check is to make sure we're in bounds, the second
9040 * check is to catch wrap-around problems. If the lba + num blocks
9041 * is less than the lba, then we've wrapped around and the block
9042 * range is invalid anyway.
9044 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9045 || ((lba + num_blocks) < lba)) {
9046 ctl_set_lba_out_of_range(ctsio);
9047 ctl_done((union ctl_io *)ctsio);
9048 return (CTL_RETVAL_COMPLETE);
9052 * According to SBC-3, a transfer length of 0 is not an error.
9054 if (num_blocks == 0) {
9055 ctl_set_success(ctsio);
9056 ctl_done((union ctl_io *)ctsio);
9057 return (CTL_RETVAL_COMPLETE);
9060 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9061 ctsio->kern_rel_offset = 0;
9064 * Set the IO_CONT flag, so that if this I/O gets passed to
9065 * ctl_data_submit_done(), it'll get passed back to
9066 * ctl_ctl_cnw_cont() for further processing.
9068 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9069 ctsio->io_cont = ctl_cnw_cont;
9071 lbalen = (struct ctl_lba_len_flags *)
9072 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9074 lbalen->len = num_blocks;
9075 lbalen->flags = CTL_LLF_COMPARE;
9077 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9078 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9083 ctl_verify(struct ctl_scsiio *ctsio)
9085 struct ctl_lun *lun;
9086 struct ctl_lba_len_flags *lbalen;
9088 uint32_t num_blocks;
9092 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9094 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9098 retval = CTL_RETVAL_COMPLETE;
9100 switch (ctsio->cdb[0]) {
9102 struct scsi_verify_10 *cdb;
9104 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9105 if (cdb->byte2 & SVFY_BYTCHK)
9107 if (cdb->byte2 & SVFY_DPO)
9109 lba = scsi_4btoul(cdb->addr);
9110 num_blocks = scsi_2btoul(cdb->length);
9114 struct scsi_verify_12 *cdb;
9116 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9117 if (cdb->byte2 & SVFY_BYTCHK)
9119 if (cdb->byte2 & SVFY_DPO)
9121 lba = scsi_4btoul(cdb->addr);
9122 num_blocks = scsi_4btoul(cdb->length);
9126 struct scsi_rw_16 *cdb;
9128 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9129 if (cdb->byte2 & SVFY_BYTCHK)
9131 if (cdb->byte2 & SVFY_DPO)
9133 lba = scsi_8btou64(cdb->addr);
9134 num_blocks = scsi_4btoul(cdb->length);
9139 * We got a command we don't support. This shouldn't
9140 * happen, commands should be filtered out above us.
9142 ctl_set_invalid_opcode(ctsio);
9143 ctl_done((union ctl_io *)ctsio);
9144 return (CTL_RETVAL_COMPLETE);
9148 * The first check is to make sure we're in bounds, the second
9149 * check is to catch wrap-around problems. If the lba + num blocks
9150 * is less than the lba, then we've wrapped around and the block
9151 * range is invalid anyway.
9153 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9154 || ((lba + num_blocks) < lba)) {
9155 ctl_set_lba_out_of_range(ctsio);
9156 ctl_done((union ctl_io *)ctsio);
9157 return (CTL_RETVAL_COMPLETE);
9161 * According to SBC-3, a transfer length of 0 is not an error.
9163 if (num_blocks == 0) {
9164 ctl_set_success(ctsio);
9165 ctl_done((union ctl_io *)ctsio);
9166 return (CTL_RETVAL_COMPLETE);
9169 lbalen = (struct ctl_lba_len_flags *)
9170 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9172 lbalen->len = num_blocks;
9174 lbalen->flags = CTL_LLF_COMPARE;
9175 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9177 lbalen->flags = CTL_LLF_VERIFY;
9178 ctsio->kern_total_len = 0;
9180 ctsio->kern_rel_offset = 0;
9182 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9183 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9188 ctl_report_luns(struct ctl_scsiio *ctsio)
9190 struct scsi_report_luns *cdb;
9191 struct scsi_report_luns_data *lun_data;
9192 struct ctl_lun *lun, *request_lun;
9193 int num_luns, retval;
9194 uint32_t alloc_len, lun_datalen;
9195 int num_filled, well_known;
9196 uint32_t initidx, targ_lun_id, lun_id;
9198 retval = CTL_RETVAL_COMPLETE;
9201 cdb = (struct scsi_report_luns *)ctsio->cdb;
9203 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9205 mtx_lock(&control_softc->ctl_lock);
9206 num_luns = control_softc->num_luns;
9207 mtx_unlock(&control_softc->ctl_lock);
9209 switch (cdb->select_report) {
9210 case RPL_REPORT_DEFAULT:
9211 case RPL_REPORT_ALL:
9213 case RPL_REPORT_WELLKNOWN:
9218 ctl_set_invalid_field(ctsio,
9224 ctl_done((union ctl_io *)ctsio);
9226 break; /* NOTREACHED */
9229 alloc_len = scsi_4btoul(cdb->length);
9231 * The initiator has to allocate at least 16 bytes for this request,
9232 * so he can at least get the header and the first LUN. Otherwise
9233 * we reject the request (per SPC-3 rev 14, section 6.21).
9235 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9236 sizeof(struct scsi_report_luns_lundata))) {
9237 ctl_set_invalid_field(ctsio,
9243 ctl_done((union ctl_io *)ctsio);
9247 request_lun = (struct ctl_lun *)
9248 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9250 lun_datalen = sizeof(*lun_data) +
9251 (num_luns * sizeof(struct scsi_report_luns_lundata));
9253 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9254 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9255 ctsio->kern_sg_entries = 0;
9257 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9259 mtx_lock(&control_softc->ctl_lock);
9260 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9261 lun_id = targ_lun_id;
9262 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9263 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9264 if (lun_id >= CTL_MAX_LUNS)
9266 lun = control_softc->ctl_luns[lun_id];
9270 if (targ_lun_id <= 0xff) {
9272 * Peripheral addressing method, bus number 0.
9274 lun_data->luns[num_filled].lundata[0] =
9275 RPL_LUNDATA_ATYP_PERIPH;
9276 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9278 } else if (targ_lun_id <= 0x3fff) {
9280 * Flat addressing method.
9282 lun_data->luns[num_filled].lundata[0] =
9283 RPL_LUNDATA_ATYP_FLAT |
9284 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9285 #ifdef OLDCTLHEADERS
9286 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9287 (targ_lun_id & SRLD_BUS_LUN_MASK);
9289 lun_data->luns[num_filled].lundata[1] =
9290 #ifdef OLDCTLHEADERS
9291 targ_lun_id >> SRLD_BUS_LUN_BITS;
9293 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9296 printf("ctl_report_luns: bogus LUN number %jd, "
9297 "skipping\n", (intmax_t)targ_lun_id);
9300 * According to SPC-3, rev 14 section 6.21:
9302 * "The execution of a REPORT LUNS command to any valid and
9303 * installed logical unit shall clear the REPORTED LUNS DATA
9304 * HAS CHANGED unit attention condition for all logical
9305 * units of that target with respect to the requesting
9306 * initiator. A valid and installed logical unit is one
9307 * having a PERIPHERAL QUALIFIER of 000b in the standard
9308 * INQUIRY data (see 6.4.2)."
9310 * If request_lun is NULL, the LUN this report luns command
9311 * was issued to is either disabled or doesn't exist. In that
9312 * case, we shouldn't clear any pending lun change unit
9315 if (request_lun != NULL) {
9316 mtx_lock(&lun->lun_lock);
9317 lun->pending_sense[initidx].ua_pending &=
9319 mtx_unlock(&lun->lun_lock);
9322 mtx_unlock(&control_softc->ctl_lock);
9325 * It's quite possible that we've returned fewer LUNs than we allocated
9326 * space for. Trim it.
9328 lun_datalen = sizeof(*lun_data) +
9329 (num_filled * sizeof(struct scsi_report_luns_lundata));
9331 if (lun_datalen < alloc_len) {
9332 ctsio->residual = alloc_len - lun_datalen;
9333 ctsio->kern_data_len = lun_datalen;
9334 ctsio->kern_total_len = lun_datalen;
9336 ctsio->residual = 0;
9337 ctsio->kern_data_len = alloc_len;
9338 ctsio->kern_total_len = alloc_len;
9340 ctsio->kern_data_resid = 0;
9341 ctsio->kern_rel_offset = 0;
9342 ctsio->kern_sg_entries = 0;
9345 * We set this to the actual data length, regardless of how much
9346 * space we actually have to return results. If the user looks at
9347 * this value, he'll know whether or not he allocated enough space
9348 * and reissue the command if necessary. We don't support well
9349 * known logical units, so if the user asks for that, return none.
9351 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9354 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9357 ctsio->scsi_status = SCSI_STATUS_OK;
9359 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9360 ctsio->be_move_done = ctl_config_move_done;
9361 ctl_datamove((union ctl_io *)ctsio);
9367 ctl_request_sense(struct ctl_scsiio *ctsio)
9369 struct scsi_request_sense *cdb;
9370 struct scsi_sense_data *sense_ptr;
9371 struct ctl_lun *lun;
9374 scsi_sense_data_type sense_format;
9376 cdb = (struct scsi_request_sense *)ctsio->cdb;
9378 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9380 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9383 * Determine which sense format the user wants.
9385 if (cdb->byte2 & SRS_DESC)
9386 sense_format = SSD_TYPE_DESC;
9388 sense_format = SSD_TYPE_FIXED;
9390 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9391 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9392 ctsio->kern_sg_entries = 0;
9395 * struct scsi_sense_data, which is currently set to 256 bytes, is
9396 * larger than the largest allowed value for the length field in the
9397 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9399 ctsio->residual = 0;
9400 ctsio->kern_data_len = cdb->length;
9401 ctsio->kern_total_len = cdb->length;
9403 ctsio->kern_data_resid = 0;
9404 ctsio->kern_rel_offset = 0;
9405 ctsio->kern_sg_entries = 0;
9408 * If we don't have a LUN, we don't have any pending sense.
9414 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9416 * Check for pending sense, and then for pending unit attentions.
9417 * Pending sense gets returned first, then pending unit attentions.
9419 mtx_lock(&lun->lun_lock);
9420 if (ctl_is_set(lun->have_ca, initidx)) {
9421 scsi_sense_data_type stored_format;
9424 * Check to see which sense format was used for the stored
9427 stored_format = scsi_sense_type(
9428 &lun->pending_sense[initidx].sense);
9431 * If the user requested a different sense format than the
9432 * one we stored, then we need to convert it to the other
9433 * format. If we're going from descriptor to fixed format
9434 * sense data, we may lose things in translation, depending
9435 * on what options were used.
9437 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9438 * for some reason we'll just copy it out as-is.
9440 if ((stored_format == SSD_TYPE_FIXED)
9441 && (sense_format == SSD_TYPE_DESC))
9442 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9443 &lun->pending_sense[initidx].sense,
9444 (struct scsi_sense_data_desc *)sense_ptr);
9445 else if ((stored_format == SSD_TYPE_DESC)
9446 && (sense_format == SSD_TYPE_FIXED))
9447 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9448 &lun->pending_sense[initidx].sense,
9449 (struct scsi_sense_data_fixed *)sense_ptr);
9451 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9452 ctl_min(sizeof(*sense_ptr),
9453 sizeof(lun->pending_sense[initidx].sense)));
9455 ctl_clear_mask(lun->have_ca, initidx);
9457 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9458 ctl_ua_type ua_type;
9460 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9461 sense_ptr, sense_format);
9462 if (ua_type != CTL_UA_NONE) {
9464 /* We're reporting this UA, so clear it */
9465 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9468 mtx_unlock(&lun->lun_lock);
9471 * We already have a pending error, return it.
9473 if (have_error != 0) {
9475 * We report the SCSI status as OK, since the status of the
9476 * request sense command itself is OK.
9478 ctsio->scsi_status = SCSI_STATUS_OK;
9481 * We report 0 for the sense length, because we aren't doing
9482 * autosense in this case. We're reporting sense as
9485 ctsio->sense_len = 0;
9486 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9487 ctsio->be_move_done = ctl_config_move_done;
9488 ctl_datamove((union ctl_io *)ctsio);
9490 return (CTL_RETVAL_COMPLETE);
9496 * No sense information to report, so we report that everything is
9499 ctl_set_sense_data(sense_ptr,
9502 /*current_error*/ 1,
9503 /*sense_key*/ SSD_KEY_NO_SENSE,
9508 ctsio->scsi_status = SCSI_STATUS_OK;
9511 * We report 0 for the sense length, because we aren't doing
9512 * autosense in this case. We're reporting sense as parameter data.
9514 ctsio->sense_len = 0;
9515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9516 ctsio->be_move_done = ctl_config_move_done;
9517 ctl_datamove((union ctl_io *)ctsio);
9519 return (CTL_RETVAL_COMPLETE);
9523 ctl_tur(struct ctl_scsiio *ctsio)
9525 struct ctl_lun *lun;
9527 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9529 CTL_DEBUG_PRINT(("ctl_tur\n"));
9534 ctsio->scsi_status = SCSI_STATUS_OK;
9535 ctsio->io_hdr.status = CTL_SUCCESS;
9537 ctl_done((union ctl_io *)ctsio);
9539 return (CTL_RETVAL_COMPLETE);
9544 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9551 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9553 struct scsi_vpd_supported_pages *pages;
9555 struct ctl_lun *lun;
9557 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9559 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9560 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9561 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9562 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9563 ctsio->kern_sg_entries = 0;
9565 if (sup_page_size < alloc_len) {
9566 ctsio->residual = alloc_len - sup_page_size;
9567 ctsio->kern_data_len = sup_page_size;
9568 ctsio->kern_total_len = sup_page_size;
9570 ctsio->residual = 0;
9571 ctsio->kern_data_len = alloc_len;
9572 ctsio->kern_total_len = alloc_len;
9574 ctsio->kern_data_resid = 0;
9575 ctsio->kern_rel_offset = 0;
9576 ctsio->kern_sg_entries = 0;
9579 * The control device is always connected. The disk device, on the
9580 * other hand, may not be online all the time. Need to change this
9581 * to figure out whether the disk device is actually online or not.
9584 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9585 lun->be_lun->lun_type;
9587 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9589 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9590 /* Supported VPD pages */
9591 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9593 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9594 /* Device Identification */
9595 pages->page_list[2] = SVPD_DEVICE_ID;
9597 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9598 /* Logical Block Provisioning */
9599 pages->page_list[4] = SVPD_LBP;
9601 ctsio->scsi_status = SCSI_STATUS_OK;
9603 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9604 ctsio->be_move_done = ctl_config_move_done;
9605 ctl_datamove((union ctl_io *)ctsio);
9607 return (CTL_RETVAL_COMPLETE);
9611 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9613 struct scsi_vpd_unit_serial_number *sn_ptr;
9614 struct ctl_lun *lun;
9616 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9618 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9619 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9620 ctsio->kern_sg_entries = 0;
9622 if (sizeof(*sn_ptr) < alloc_len) {
9623 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9624 ctsio->kern_data_len = sizeof(*sn_ptr);
9625 ctsio->kern_total_len = sizeof(*sn_ptr);
9627 ctsio->residual = 0;
9628 ctsio->kern_data_len = alloc_len;
9629 ctsio->kern_total_len = alloc_len;
9631 ctsio->kern_data_resid = 0;
9632 ctsio->kern_rel_offset = 0;
9633 ctsio->kern_sg_entries = 0;
9636 * The control device is always connected. The disk device, on the
9637 * other hand, may not be online all the time. Need to change this
9638 * to figure out whether the disk device is actually online or not.
9641 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9642 lun->be_lun->lun_type;
9644 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9646 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9647 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9649 * If we don't have a LUN, we just leave the serial number as
9652 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9654 strncpy((char *)sn_ptr->serial_num,
9655 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9657 ctsio->scsi_status = SCSI_STATUS_OK;
9659 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9660 ctsio->be_move_done = ctl_config_move_done;
9661 ctl_datamove((union ctl_io *)ctsio);
9663 return (CTL_RETVAL_COMPLETE);
9668 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9670 struct scsi_vpd_device_id *devid_ptr;
9671 struct scsi_vpd_id_descriptor *desc;
9672 struct ctl_softc *ctl_softc;
9673 struct ctl_lun *lun;
9674 struct ctl_port *port;
9678 ctl_softc = control_softc;
9680 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9681 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9683 data_len = sizeof(struct scsi_vpd_device_id) +
9684 sizeof(struct scsi_vpd_id_descriptor) +
9685 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9686 sizeof(struct scsi_vpd_id_descriptor) +
9687 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9688 if (lun && lun->lun_devid)
9689 data_len += lun->lun_devid->len;
9690 if (port->port_devid)
9691 data_len += port->port_devid->len;
9692 if (port->target_devid)
9693 data_len += port->target_devid->len;
9695 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9696 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9697 ctsio->kern_sg_entries = 0;
9699 if (data_len < alloc_len) {
9700 ctsio->residual = alloc_len - data_len;
9701 ctsio->kern_data_len = data_len;
9702 ctsio->kern_total_len = data_len;
9704 ctsio->residual = 0;
9705 ctsio->kern_data_len = alloc_len;
9706 ctsio->kern_total_len = alloc_len;
9708 ctsio->kern_data_resid = 0;
9709 ctsio->kern_rel_offset = 0;
9710 ctsio->kern_sg_entries = 0;
9713 * The control device is always connected. The disk device, on the
9714 * other hand, may not be online all the time.
9717 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9718 lun->be_lun->lun_type;
9720 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9721 devid_ptr->page_code = SVPD_DEVICE_ID;
9722 scsi_ulto2b(data_len - 4, devid_ptr->length);
9724 if (port->port_type == CTL_PORT_FC)
9725 proto = SCSI_PROTO_FC << 4;
9726 else if (port->port_type == CTL_PORT_ISCSI)
9727 proto = SCSI_PROTO_ISCSI << 4;
9729 proto = SCSI_PROTO_SPI << 4;
9730 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9733 * We're using a LUN association here. i.e., this device ID is a
9734 * per-LUN identifier.
9736 if (lun && lun->lun_devid) {
9737 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9738 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9739 lun->lun_devid->len);
9743 * This is for the WWPN which is a port association.
9745 if (port->port_devid) {
9746 memcpy(desc, port->port_devid->data, port->port_devid->len);
9747 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9748 port->port_devid->len);
9752 * This is for the Relative Target Port(type 4h) identifier
9754 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9755 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9756 SVPD_ID_TYPE_RELTARG;
9758 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9759 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9760 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9763 * This is for the Target Port Group(type 5h) identifier
9765 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9766 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9767 SVPD_ID_TYPE_TPORTGRP;
9769 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9770 scsi_ulto2b(1, &desc->identifier[2]);
9772 scsi_ulto2b(2, &desc->identifier[2]);
9773 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9774 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9777 * This is for the Target identifier
9779 if (port->target_devid) {
9780 memcpy(desc, port->target_devid->data, port->target_devid->len);
9783 ctsio->scsi_status = SCSI_STATUS_OK;
9784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9785 ctsio->be_move_done = ctl_config_move_done;
9786 ctl_datamove((union ctl_io *)ctsio);
9788 return (CTL_RETVAL_COMPLETE);
9792 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9794 struct scsi_vpd_block_limits *bl_ptr;
9795 struct ctl_lun *lun;
9798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9799 bs = lun->be_lun->blocksize;
9801 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9802 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9803 ctsio->kern_sg_entries = 0;
9805 if (sizeof(*bl_ptr) < alloc_len) {
9806 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9807 ctsio->kern_data_len = sizeof(*bl_ptr);
9808 ctsio->kern_total_len = sizeof(*bl_ptr);
9810 ctsio->residual = 0;
9811 ctsio->kern_data_len = alloc_len;
9812 ctsio->kern_total_len = alloc_len;
9814 ctsio->kern_data_resid = 0;
9815 ctsio->kern_rel_offset = 0;
9816 ctsio->kern_sg_entries = 0;
9819 * The control device is always connected. The disk device, on the
9820 * other hand, may not be online all the time. Need to change this
9821 * to figure out whether the disk device is actually online or not.
9824 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9825 lun->be_lun->lun_type;
9827 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9829 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9830 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9831 bl_ptr->max_cmp_write_len = 0xff;
9832 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9833 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9834 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9835 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9836 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9838 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9840 ctsio->scsi_status = SCSI_STATUS_OK;
9841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9842 ctsio->be_move_done = ctl_config_move_done;
9843 ctl_datamove((union ctl_io *)ctsio);
9845 return (CTL_RETVAL_COMPLETE);
9849 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9851 struct scsi_vpd_logical_block_prov *lbp_ptr;
9852 struct ctl_lun *lun;
9855 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9856 bs = lun->be_lun->blocksize;
9858 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9859 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9860 ctsio->kern_sg_entries = 0;
9862 if (sizeof(*lbp_ptr) < alloc_len) {
9863 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9864 ctsio->kern_data_len = sizeof(*lbp_ptr);
9865 ctsio->kern_total_len = sizeof(*lbp_ptr);
9867 ctsio->residual = 0;
9868 ctsio->kern_data_len = alloc_len;
9869 ctsio->kern_total_len = alloc_len;
9871 ctsio->kern_data_resid = 0;
9872 ctsio->kern_rel_offset = 0;
9873 ctsio->kern_sg_entries = 0;
9876 * The control device is always connected. The disk device, on the
9877 * other hand, may not be online all the time. Need to change this
9878 * to figure out whether the disk device is actually online or not.
9881 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9882 lun->be_lun->lun_type;
9884 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9886 lbp_ptr->page_code = SVPD_LBP;
9887 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9888 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9890 ctsio->scsi_status = SCSI_STATUS_OK;
9891 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9892 ctsio->be_move_done = ctl_config_move_done;
9893 ctl_datamove((union ctl_io *)ctsio);
9895 return (CTL_RETVAL_COMPLETE);
9899 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9901 struct scsi_inquiry *cdb;
9902 struct ctl_lun *lun;
9903 int alloc_len, retval;
9905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9906 cdb = (struct scsi_inquiry *)ctsio->cdb;
9908 retval = CTL_RETVAL_COMPLETE;
9910 alloc_len = scsi_2btoul(cdb->length);
9912 switch (cdb->page_code) {
9913 case SVPD_SUPPORTED_PAGES:
9914 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9916 case SVPD_UNIT_SERIAL_NUMBER:
9917 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9919 case SVPD_DEVICE_ID:
9920 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9922 case SVPD_BLOCK_LIMITS:
9923 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9926 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9929 ctl_set_invalid_field(ctsio,
9935 ctl_done((union ctl_io *)ctsio);
9936 retval = CTL_RETVAL_COMPLETE;
9944 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9946 struct scsi_inquiry_data *inq_ptr;
9947 struct scsi_inquiry *cdb;
9948 struct ctl_softc *ctl_softc;
9949 struct ctl_lun *lun;
9954 ctl_softc = control_softc;
9957 * Figure out whether we're talking to a Fibre Channel port or not.
9958 * We treat the ioctl front end, and any SCSI adapters, as packetized
9961 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9967 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9968 cdb = (struct scsi_inquiry *)ctsio->cdb;
9969 alloc_len = scsi_2btoul(cdb->length);
9972 * We malloc the full inquiry data size here and fill it
9973 * in. If the user only asks for less, we'll give him
9976 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9977 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9978 ctsio->kern_sg_entries = 0;
9979 ctsio->kern_data_resid = 0;
9980 ctsio->kern_rel_offset = 0;
9982 if (sizeof(*inq_ptr) < alloc_len) {
9983 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9984 ctsio->kern_data_len = sizeof(*inq_ptr);
9985 ctsio->kern_total_len = sizeof(*inq_ptr);
9987 ctsio->residual = 0;
9988 ctsio->kern_data_len = alloc_len;
9989 ctsio->kern_total_len = alloc_len;
9993 * If we have a LUN configured, report it as connected. Otherwise,
9994 * report that it is offline or no device is supported, depending
9995 * on the value of inquiry_pq_no_lun.
9997 * According to the spec (SPC-4 r34), the peripheral qualifier
9998 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10000 * "A peripheral device having the specified peripheral device type
10001 * is not connected to this logical unit. However, the device
10002 * server is capable of supporting the specified peripheral device
10003 * type on this logical unit."
10005 * According to the same spec, the peripheral qualifier
10006 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10008 * "The device server is not capable of supporting a peripheral
10009 * device on this logical unit. For this peripheral qualifier the
10010 * peripheral device type shall be set to 1Fh. All other peripheral
10011 * device type values are reserved for this peripheral qualifier."
10013 * Given the text, it would seem that we probably want to report that
10014 * the LUN is offline here. There is no LUN connected, but we can
10015 * support a LUN at the given LUN number.
10017 * In the real world, though, it sounds like things are a little
10020 * - Linux, when presented with a LUN with the offline peripheral
10021 * qualifier, will create an sg driver instance for it. So when
10022 * you attach it to CTL, you wind up with a ton of sg driver
10023 * instances. (One for every LUN that Linux bothered to probe.)
10024 * Linux does this despite the fact that it issues a REPORT LUNs
10025 * to LUN 0 to get the inventory of supported LUNs.
10027 * - There is other anecdotal evidence (from Emulex folks) about
10028 * arrays that use the offline peripheral qualifier for LUNs that
10029 * are on the "passive" path in an active/passive array.
10031 * So the solution is provide a hopefully reasonable default
10032 * (return bad/no LUN) and allow the user to change the behavior
10033 * with a tunable/sysctl variable.
10036 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10037 lun->be_lun->lun_type;
10038 else if (ctl_softc->inquiry_pq_no_lun == 0)
10039 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10041 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10043 /* RMB in byte 2 is 0 */
10044 inq_ptr->version = SCSI_REV_SPC3;
10047 * According to SAM-3, even if a device only supports a single
10048 * level of LUN addressing, it should still set the HISUP bit:
10050 * 4.9.1 Logical unit numbers overview
10052 * All logical unit number formats described in this standard are
10053 * hierarchical in structure even when only a single level in that
10054 * hierarchy is used. The HISUP bit shall be set to one in the
10055 * standard INQUIRY data (see SPC-2) when any logical unit number
10056 * format described in this standard is used. Non-hierarchical
10057 * formats are outside the scope of this standard.
10059 * Therefore we set the HiSup bit here.
10061 * The reponse format is 2, per SPC-3.
10063 inq_ptr->response_format = SID_HiSup | 2;
10065 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
10066 CTL_DEBUG_PRINT(("additional_length = %d\n",
10067 inq_ptr->additional_length));
10069 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
10070 /* 16 bit addressing */
10072 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10073 /* XXX set the SID_MultiP bit here if we're actually going to
10074 respond on multiple ports */
10075 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10077 /* 16 bit data bus, synchronous transfers */
10078 /* XXX these flags don't apply for FC */
10080 inq_ptr->flags = SID_WBus16 | SID_Sync;
10082 * XXX KDM do we want to support tagged queueing on the control
10086 || (lun->be_lun->lun_type != T_PROCESSOR))
10087 inq_ptr->flags |= SID_CmdQue;
10089 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10090 * We have 8 bytes for the vendor name, and 16 bytes for the device
10091 * name and 4 bytes for the revision.
10093 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10094 "vendor")) == NULL) {
10095 strcpy(inq_ptr->vendor, CTL_VENDOR);
10097 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10098 strncpy(inq_ptr->vendor, val,
10099 min(sizeof(inq_ptr->vendor), strlen(val)));
10102 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10103 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10104 switch (lun->be_lun->lun_type) {
10106 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10109 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10112 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10116 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10117 strncpy(inq_ptr->product, val,
10118 min(sizeof(inq_ptr->product), strlen(val)));
10122 * XXX make this a macro somewhere so it automatically gets
10123 * incremented when we make changes.
10125 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10126 "revision")) == NULL) {
10127 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10129 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10130 strncpy(inq_ptr->revision, val,
10131 min(sizeof(inq_ptr->revision), strlen(val)));
10135 * For parallel SCSI, we support double transition and single
10136 * transition clocking. We also support QAS (Quick Arbitration
10137 * and Selection) and Information Unit transfers on both the
10138 * control and array devices.
10141 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10145 scsi_ulto2b(0x0060, inq_ptr->version1);
10146 /* SPC-3 (no version claimed) XXX should we claim a version? */
10147 scsi_ulto2b(0x0300, inq_ptr->version2);
10149 /* FCP-2 ANSI INCITS.350:2003 */
10150 scsi_ulto2b(0x0917, inq_ptr->version3);
10152 /* SPI-4 ANSI INCITS.362:200x */
10153 scsi_ulto2b(0x0B56, inq_ptr->version3);
10157 /* SBC-2 (no version claimed) XXX should we claim a version? */
10158 scsi_ulto2b(0x0320, inq_ptr->version4);
10160 switch (lun->be_lun->lun_type) {
10163 * SBC-2 (no version claimed) XXX should we claim a
10166 scsi_ulto2b(0x0320, inq_ptr->version4);
10174 ctsio->scsi_status = SCSI_STATUS_OK;
10175 if (ctsio->kern_data_len > 0) {
10176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10177 ctsio->be_move_done = ctl_config_move_done;
10178 ctl_datamove((union ctl_io *)ctsio);
10180 ctsio->io_hdr.status = CTL_SUCCESS;
10181 ctl_done((union ctl_io *)ctsio);
10184 return (CTL_RETVAL_COMPLETE);
10188 ctl_inquiry(struct ctl_scsiio *ctsio)
10190 struct scsi_inquiry *cdb;
10193 cdb = (struct scsi_inquiry *)ctsio->cdb;
10197 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10200 * Right now, we don't support the CmdDt inquiry information.
10201 * This would be nice to support in the future. When we do
10202 * support it, we should change this test so that it checks to make
10203 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10206 if (((cdb->byte2 & SI_EVPD)
10207 && (cdb->byte2 & SI_CMDDT)))
10209 if (cdb->byte2 & SI_CMDDT) {
10211 * Point to the SI_CMDDT bit. We might change this
10212 * when we support SI_CMDDT, but since both bits would be
10213 * "wrong", this should probably just stay as-is then.
10215 ctl_set_invalid_field(ctsio,
10221 ctl_done((union ctl_io *)ctsio);
10222 return (CTL_RETVAL_COMPLETE);
10224 if (cdb->byte2 & SI_EVPD)
10225 retval = ctl_inquiry_evpd(ctsio);
10227 else if (cdb->byte2 & SI_CMDDT)
10228 retval = ctl_inquiry_cmddt(ctsio);
10231 retval = ctl_inquiry_std(ctsio);
10237 * For known CDB types, parse the LBA and length.
10240 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10242 if (io->io_hdr.io_type != CTL_IO_SCSI)
10245 switch (io->scsiio.cdb[0]) {
10246 case COMPARE_AND_WRITE: {
10247 struct scsi_compare_and_write *cdb;
10249 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10251 *lba = scsi_8btou64(cdb->addr);
10252 *len = cdb->length;
10257 struct scsi_rw_6 *cdb;
10259 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10261 *lba = scsi_3btoul(cdb->addr);
10262 /* only 5 bits are valid in the most significant address byte */
10264 *len = cdb->length;
10269 struct scsi_rw_10 *cdb;
10271 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10273 *lba = scsi_4btoul(cdb->addr);
10274 *len = scsi_2btoul(cdb->length);
10277 case WRITE_VERIFY_10: {
10278 struct scsi_write_verify_10 *cdb;
10280 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10282 *lba = scsi_4btoul(cdb->addr);
10283 *len = scsi_2btoul(cdb->length);
10288 struct scsi_rw_12 *cdb;
10290 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10292 *lba = scsi_4btoul(cdb->addr);
10293 *len = scsi_4btoul(cdb->length);
10296 case WRITE_VERIFY_12: {
10297 struct scsi_write_verify_12 *cdb;
10299 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10301 *lba = scsi_4btoul(cdb->addr);
10302 *len = scsi_4btoul(cdb->length);
10307 struct scsi_rw_16 *cdb;
10309 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10311 *lba = scsi_8btou64(cdb->addr);
10312 *len = scsi_4btoul(cdb->length);
10315 case WRITE_VERIFY_16: {
10316 struct scsi_write_verify_16 *cdb;
10318 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10321 *lba = scsi_8btou64(cdb->addr);
10322 *len = scsi_4btoul(cdb->length);
10325 case WRITE_SAME_10: {
10326 struct scsi_write_same_10 *cdb;
10328 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10330 *lba = scsi_4btoul(cdb->addr);
10331 *len = scsi_2btoul(cdb->length);
10334 case WRITE_SAME_16: {
10335 struct scsi_write_same_16 *cdb;
10337 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10339 *lba = scsi_8btou64(cdb->addr);
10340 *len = scsi_4btoul(cdb->length);
10344 struct scsi_verify_10 *cdb;
10346 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10348 *lba = scsi_4btoul(cdb->addr);
10349 *len = scsi_2btoul(cdb->length);
10353 struct scsi_verify_12 *cdb;
10355 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10357 *lba = scsi_4btoul(cdb->addr);
10358 *len = scsi_4btoul(cdb->length);
10362 struct scsi_verify_16 *cdb;
10364 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10366 *lba = scsi_8btou64(cdb->addr);
10367 *len = scsi_4btoul(cdb->length);
10372 break; /* NOTREACHED */
10379 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10381 uint64_t endlba1, endlba2;
10383 endlba1 = lba1 + len1 - 1;
10384 endlba2 = lba2 + len2 - 1;
10386 if ((endlba1 < lba2)
10387 || (endlba2 < lba1))
10388 return (CTL_ACTION_PASS);
10390 return (CTL_ACTION_BLOCK);
10394 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10396 uint64_t lba1, lba2;
10397 uint32_t len1, len2;
10400 retval = ctl_get_lba_len(io1, &lba1, &len1);
10402 return (CTL_ACTION_ERROR);
10404 retval = ctl_get_lba_len(io2, &lba2, &len2);
10406 return (CTL_ACTION_ERROR);
10408 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10412 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10414 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10415 ctl_serialize_action *serialize_row;
10418 * The initiator attempted multiple untagged commands at the same
10419 * time. Can't do that.
10421 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10422 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10423 && ((pending_io->io_hdr.nexus.targ_port ==
10424 ooa_io->io_hdr.nexus.targ_port)
10425 && (pending_io->io_hdr.nexus.initid.id ==
10426 ooa_io->io_hdr.nexus.initid.id))
10427 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10428 return (CTL_ACTION_OVERLAP);
10431 * The initiator attempted to send multiple tagged commands with
10432 * the same ID. (It's fine if different initiators have the same
10435 * Even if all of those conditions are true, we don't kill the I/O
10436 * if the command ahead of us has been aborted. We won't end up
10437 * sending it to the FETD, and it's perfectly legal to resend a
10438 * command with the same tag number as long as the previous
10439 * instance of this tag number has been aborted somehow.
10441 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10442 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10443 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10444 && ((pending_io->io_hdr.nexus.targ_port ==
10445 ooa_io->io_hdr.nexus.targ_port)
10446 && (pending_io->io_hdr.nexus.initid.id ==
10447 ooa_io->io_hdr.nexus.initid.id))
10448 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10449 return (CTL_ACTION_OVERLAP_TAG);
10452 * If we get a head of queue tag, SAM-3 says that we should
10453 * immediately execute it.
10455 * What happens if this command would normally block for some other
10456 * reason? e.g. a request sense with a head of queue tag
10457 * immediately after a write. Normally that would block, but this
10458 * will result in its getting executed immediately...
10460 * We currently return "pass" instead of "skip", so we'll end up
10461 * going through the rest of the queue to check for overlapped tags.
10463 * XXX KDM check for other types of blockage first??
10465 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10466 return (CTL_ACTION_PASS);
10469 * Ordered tags have to block until all items ahead of them
10470 * have completed. If we get called with an ordered tag, we always
10471 * block, if something else is ahead of us in the queue.
10473 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10474 return (CTL_ACTION_BLOCK);
10477 * Simple tags get blocked until all head of queue and ordered tags
10478 * ahead of them have completed. I'm lumping untagged commands in
10479 * with simple tags here. XXX KDM is that the right thing to do?
10481 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10482 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10483 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10484 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10485 return (CTL_ACTION_BLOCK);
10487 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10488 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10490 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10492 switch (serialize_row[pending_entry->seridx]) {
10493 case CTL_SER_BLOCK:
10494 return (CTL_ACTION_BLOCK);
10495 break; /* NOTREACHED */
10496 case CTL_SER_EXTENT:
10497 return (ctl_extent_check(pending_io, ooa_io));
10498 break; /* NOTREACHED */
10500 return (CTL_ACTION_PASS);
10501 break; /* NOTREACHED */
10503 return (CTL_ACTION_SKIP);
10506 panic("invalid serialization value %d",
10507 serialize_row[pending_entry->seridx]);
10508 break; /* NOTREACHED */
10511 return (CTL_ACTION_ERROR);
10515 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10517 * - pending_io is generally either incoming, or on the blocked queue
10518 * - starting I/O is the I/O we want to start the check with.
10521 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10522 union ctl_io *starting_io)
10524 union ctl_io *ooa_io;
10527 mtx_assert(&lun->lun_lock, MA_OWNED);
10530 * Run back along the OOA queue, starting with the current
10531 * blocked I/O and going through every I/O before it on the
10532 * queue. If starting_io is NULL, we'll just end up returning
10535 for (ooa_io = starting_io; ooa_io != NULL;
10536 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10540 * This routine just checks to see whether
10541 * cur_blocked is blocked by ooa_io, which is ahead
10542 * of it in the queue. It doesn't queue/dequeue
10545 action = ctl_check_for_blockage(pending_io, ooa_io);
10547 case CTL_ACTION_BLOCK:
10548 case CTL_ACTION_OVERLAP:
10549 case CTL_ACTION_OVERLAP_TAG:
10550 case CTL_ACTION_SKIP:
10551 case CTL_ACTION_ERROR:
10553 break; /* NOTREACHED */
10554 case CTL_ACTION_PASS:
10557 panic("invalid action %d", action);
10558 break; /* NOTREACHED */
10562 return (CTL_ACTION_PASS);
10567 * - An I/O has just completed, and has been removed from the per-LUN OOA
10568 * queue, so some items on the blocked queue may now be unblocked.
10571 ctl_check_blocked(struct ctl_lun *lun)
10573 union ctl_io *cur_blocked, *next_blocked;
10575 mtx_assert(&lun->lun_lock, MA_OWNED);
10578 * Run forward from the head of the blocked queue, checking each
10579 * entry against the I/Os prior to it on the OOA queue to see if
10580 * there is still any blockage.
10582 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10583 * with our removing a variable on it while it is traversing the
10586 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10587 cur_blocked != NULL; cur_blocked = next_blocked) {
10588 union ctl_io *prev_ooa;
10591 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10594 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10595 ctl_ooaq, ooa_links);
10598 * If cur_blocked happens to be the first item in the OOA
10599 * queue now, prev_ooa will be NULL, and the action
10600 * returned will just be CTL_ACTION_PASS.
10602 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10605 case CTL_ACTION_BLOCK:
10606 /* Nothing to do here, still blocked */
10608 case CTL_ACTION_OVERLAP:
10609 case CTL_ACTION_OVERLAP_TAG:
10611 * This shouldn't happen! In theory we've already
10612 * checked this command for overlap...
10615 case CTL_ACTION_PASS:
10616 case CTL_ACTION_SKIP: {
10617 struct ctl_softc *softc;
10618 const struct ctl_cmd_entry *entry;
10623 * The skip case shouldn't happen, this transaction
10624 * should have never made it onto the blocked queue.
10627 * This I/O is no longer blocked, we can remove it
10628 * from the blocked queue. Since this is a TAILQ
10629 * (doubly linked list), we can do O(1) removals
10630 * from any place on the list.
10632 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10634 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10636 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10638 * Need to send IO back to original side to
10641 union ctl_ha_msg msg_info;
10643 msg_info.hdr.original_sc =
10644 cur_blocked->io_hdr.original_sc;
10645 msg_info.hdr.serializing_sc = cur_blocked;
10646 msg_info.hdr.msg_type = CTL_MSG_R2R;
10647 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10648 &msg_info, sizeof(msg_info), 0)) >
10649 CTL_HA_STATUS_SUCCESS) {
10650 printf("CTL:Check Blocked error from "
10651 "ctl_ha_msg_send %d\n",
10656 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10657 softc = control_softc;
10659 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10662 * Check this I/O for LUN state changes that may
10663 * have happened while this command was blocked.
10664 * The LUN state may have been changed by a command
10665 * ahead of us in the queue, so we need to re-check
10666 * for any states that can be caused by SCSI
10669 if (ctl_scsiio_lun_check(softc, lun, entry,
10670 &cur_blocked->scsiio) == 0) {
10671 cur_blocked->io_hdr.flags |=
10672 CTL_FLAG_IS_WAS_ON_RTR;
10673 ctl_enqueue_rtr(cur_blocked);
10675 ctl_done(cur_blocked);
10680 * This probably shouldn't happen -- we shouldn't
10681 * get CTL_ACTION_ERROR, or anything else.
10687 return (CTL_RETVAL_COMPLETE);
10691 * This routine (with one exception) checks LUN flags that can be set by
10692 * commands ahead of us in the OOA queue. These flags have to be checked
10693 * when a command initially comes in, and when we pull a command off the
10694 * blocked queue and are preparing to execute it. The reason we have to
10695 * check these flags for commands on the blocked queue is that the LUN
10696 * state may have been changed by a command ahead of us while we're on the
10699 * Ordering is somewhat important with these checks, so please pay
10700 * careful attention to the placement of any new checks.
10703 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10704 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10710 mtx_assert(&lun->lun_lock, MA_OWNED);
10713 * If this shelf is a secondary shelf controller, we have to reject
10714 * any media access commands.
10717 /* No longer needed for HA */
10718 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10719 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10720 ctl_set_lun_standby(ctsio);
10727 * Check for a reservation conflict. If this command isn't allowed
10728 * even on reserved LUNs, and if this initiator isn't the one who
10729 * reserved us, reject the command with a reservation conflict.
10731 if ((lun->flags & CTL_LUN_RESERVED)
10732 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10733 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10734 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10735 || (ctsio->io_hdr.nexus.targ_target.id !=
10736 lun->rsv_nexus.targ_target.id)) {
10737 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10738 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10744 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10745 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10748 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10750 * if we aren't registered or it's a res holder type
10751 * reservation and this isn't the res holder then set a
10753 * NOTE: Commands which might be allowed on write exclusive
10754 * type reservations are checked in the particular command
10755 * for a conflict. Read and SSU are the only ones.
10757 if (!lun->per_res[residx].registered
10758 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10759 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10760 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10767 if ((lun->flags & CTL_LUN_OFFLINE)
10768 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10769 ctl_set_lun_not_ready(ctsio);
10775 * If the LUN is stopped, see if this particular command is allowed
10776 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10778 if ((lun->flags & CTL_LUN_STOPPED)
10779 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10780 /* "Logical unit not ready, initializing cmd. required" */
10781 ctl_set_lun_stopped(ctsio);
10786 if ((lun->flags & CTL_LUN_INOPERABLE)
10787 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10788 /* "Medium format corrupted" */
10789 ctl_set_medium_format_corrupted(ctsio);
10800 ctl_failover_io(union ctl_io *io, int have_lock)
10802 ctl_set_busy(&io->scsiio);
10809 struct ctl_lun *lun;
10810 struct ctl_softc *ctl_softc;
10811 union ctl_io *next_io, *pending_io;
10816 ctl_softc = control_softc;
10818 mtx_lock(&ctl_softc->ctl_lock);
10820 * Remove any cmds from the other SC from the rtr queue. These
10821 * will obviously only be for LUNs for which we're the primary.
10822 * We can't send status or get/send data for these commands.
10823 * Since they haven't been executed yet, we can just remove them.
10824 * We'll either abort them or delete them below, depending on
10825 * which HA mode we're in.
10828 mtx_lock(&ctl_softc->queue_lock);
10829 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10830 io != NULL; io = next_io) {
10831 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10832 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10833 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10834 ctl_io_hdr, links);
10836 mtx_unlock(&ctl_softc->queue_lock);
10839 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10840 lun = ctl_softc->ctl_luns[lun_idx];
10845 * Processor LUNs are primary on both sides.
10846 * XXX will this always be true?
10848 if (lun->be_lun->lun_type == T_PROCESSOR)
10851 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10852 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10853 printf("FAILOVER: primary lun %d\n", lun_idx);
10855 * Remove all commands from the other SC. First from the
10856 * blocked queue then from the ooa queue. Once we have
10857 * removed them. Call ctl_check_blocked to see if there
10858 * is anything that can run.
10860 for (io = (union ctl_io *)TAILQ_FIRST(
10861 &lun->blocked_queue); io != NULL; io = next_io) {
10863 next_io = (union ctl_io *)TAILQ_NEXT(
10864 &io->io_hdr, blocked_links);
10866 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10867 TAILQ_REMOVE(&lun->blocked_queue,
10868 &io->io_hdr,blocked_links);
10869 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10870 TAILQ_REMOVE(&lun->ooa_queue,
10871 &io->io_hdr, ooa_links);
10877 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10878 io != NULL; io = next_io) {
10880 next_io = (union ctl_io *)TAILQ_NEXT(
10881 &io->io_hdr, ooa_links);
10883 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10885 TAILQ_REMOVE(&lun->ooa_queue,
10892 ctl_check_blocked(lun);
10893 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10894 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10896 printf("FAILOVER: primary lun %d\n", lun_idx);
10898 * Abort all commands from the other SC. We can't
10899 * send status back for them now. These should get
10900 * cleaned up when they are completed or come out
10901 * for a datamove operation.
10903 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10904 io != NULL; io = next_io) {
10905 next_io = (union ctl_io *)TAILQ_NEXT(
10906 &io->io_hdr, ooa_links);
10908 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10909 io->io_hdr.flags |= CTL_FLAG_ABORT;
10911 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10912 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10914 printf("FAILOVER: secondary lun %d\n", lun_idx);
10916 lun->flags |= CTL_LUN_PRIMARY_SC;
10919 * We send all I/O that was sent to this controller
10920 * and redirected to the other side back with
10921 * busy status, and have the initiator retry it.
10922 * Figuring out how much data has been transferred,
10923 * etc. and picking up where we left off would be
10926 * XXX KDM need to remove I/O from the blocked
10929 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10930 &lun->ooa_queue); pending_io != NULL;
10931 pending_io = next_io) {
10933 next_io = (union ctl_io *)TAILQ_NEXT(
10934 &pending_io->io_hdr, ooa_links);
10936 pending_io->io_hdr.flags &=
10937 ~CTL_FLAG_SENT_2OTHER_SC;
10939 if (pending_io->io_hdr.flags &
10940 CTL_FLAG_IO_ACTIVE) {
10941 pending_io->io_hdr.flags |=
10944 ctl_set_busy(&pending_io->scsiio);
10945 ctl_done(pending_io);
10950 * Build Unit Attention
10952 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10953 lun->pending_sense[i].ua_pending |=
10954 CTL_UA_ASYM_ACC_CHANGE;
10956 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10957 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10958 printf("FAILOVER: secondary lun %d\n", lun_idx);
10960 * if the first io on the OOA is not on the RtR queue
10963 lun->flags |= CTL_LUN_PRIMARY_SC;
10965 pending_io = (union ctl_io *)TAILQ_FIRST(
10967 if (pending_io==NULL) {
10968 printf("Nothing on OOA queue\n");
10972 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10973 if ((pending_io->io_hdr.flags &
10974 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10975 pending_io->io_hdr.flags |=
10976 CTL_FLAG_IS_WAS_ON_RTR;
10977 ctl_enqueue_rtr(pending_io);
10982 printf("Tag 0x%04x is running\n",
10983 pending_io->scsiio.tag_num);
10987 next_io = (union ctl_io *)TAILQ_NEXT(
10988 &pending_io->io_hdr, ooa_links);
10989 for (pending_io=next_io; pending_io != NULL;
10990 pending_io = next_io) {
10991 pending_io->io_hdr.flags &=
10992 ~CTL_FLAG_SENT_2OTHER_SC;
10993 next_io = (union ctl_io *)TAILQ_NEXT(
10994 &pending_io->io_hdr, ooa_links);
10995 if (pending_io->io_hdr.flags &
10996 CTL_FLAG_IS_WAS_ON_RTR) {
10998 printf("Tag 0x%04x is running\n",
10999 pending_io->scsiio.tag_num);
11004 switch (ctl_check_ooa(lun, pending_io,
11005 (union ctl_io *)TAILQ_PREV(
11006 &pending_io->io_hdr, ctl_ooaq,
11009 case CTL_ACTION_BLOCK:
11010 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11011 &pending_io->io_hdr,
11013 pending_io->io_hdr.flags |=
11016 case CTL_ACTION_PASS:
11017 case CTL_ACTION_SKIP:
11018 pending_io->io_hdr.flags |=
11019 CTL_FLAG_IS_WAS_ON_RTR;
11020 ctl_enqueue_rtr(pending_io);
11022 case CTL_ACTION_OVERLAP:
11023 ctl_set_overlapped_cmd(
11024 (struct ctl_scsiio *)pending_io);
11025 ctl_done(pending_io);
11027 case CTL_ACTION_OVERLAP_TAG:
11028 ctl_set_overlapped_tag(
11029 (struct ctl_scsiio *)pending_io,
11030 pending_io->scsiio.tag_num & 0xff);
11031 ctl_done(pending_io);
11033 case CTL_ACTION_ERROR:
11035 ctl_set_internal_failure(
11036 (struct ctl_scsiio *)pending_io,
11039 ctl_done(pending_io);
11045 * Build Unit Attention
11047 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11048 lun->pending_sense[i].ua_pending |=
11049 CTL_UA_ASYM_ACC_CHANGE;
11052 panic("Unhandled HA mode failover, LUN flags = %#x, "
11053 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11057 mtx_unlock(&ctl_softc->ctl_lock);
11061 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11063 struct ctl_lun *lun;
11064 const struct ctl_cmd_entry *entry;
11065 uint32_t initidx, targ_lun;
11072 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11073 if ((targ_lun < CTL_MAX_LUNS)
11074 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11075 lun = ctl_softc->ctl_luns[targ_lun];
11077 * If the LUN is invalid, pretend that it doesn't exist.
11078 * It will go away as soon as all pending I/O has been
11081 if (lun->flags & CTL_LUN_DISABLED) {
11084 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11085 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11087 if (lun->be_lun->lun_type == T_PROCESSOR) {
11088 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11092 * Every I/O goes into the OOA queue for a
11093 * particular LUN, and stays there until completion.
11095 mtx_lock(&lun->lun_lock);
11096 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11100 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11101 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11104 /* Get command entry and return error if it is unsuppotyed. */
11105 entry = ctl_validate_command(ctsio);
11106 if (entry == NULL) {
11108 mtx_unlock(&lun->lun_lock);
11112 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11113 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11116 * Check to see whether we can send this command to LUNs that don't
11117 * exist. This should pretty much only be the case for inquiry
11118 * and request sense. Further checks, below, really require having
11119 * a LUN, so we can't really check the command anymore. Just put
11120 * it on the rtr queue.
11123 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11124 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11125 ctl_enqueue_rtr((union ctl_io *)ctsio);
11129 ctl_set_unsupported_lun(ctsio);
11130 ctl_done((union ctl_io *)ctsio);
11131 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11135 * Make sure we support this particular command on this LUN.
11136 * e.g., we don't support writes to the control LUN.
11138 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11139 mtx_unlock(&lun->lun_lock);
11140 ctl_set_invalid_opcode(ctsio);
11141 ctl_done((union ctl_io *)ctsio);
11146 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11149 * If we've got a request sense, it'll clear the contingent
11150 * allegiance condition. Otherwise, if we have a CA condition for
11151 * this initiator, clear it, because it sent down a command other
11152 * than request sense.
11154 if ((ctsio->cdb[0] != REQUEST_SENSE)
11155 && (ctl_is_set(lun->have_ca, initidx)))
11156 ctl_clear_mask(lun->have_ca, initidx);
11159 * If the command has this flag set, it handles its own unit
11160 * attention reporting, we shouldn't do anything. Otherwise we
11161 * check for any pending unit attentions, and send them back to the
11162 * initiator. We only do this when a command initially comes in,
11163 * not when we pull it off the blocked queue.
11165 * According to SAM-3, section 5.3.2, the order that things get
11166 * presented back to the host is basically unit attentions caused
11167 * by some sort of reset event, busy status, reservation conflicts
11168 * or task set full, and finally any other status.
11170 * One issue here is that some of the unit attentions we report
11171 * don't fall into the "reset" category (e.g. "reported luns data
11172 * has changed"). So reporting it here, before the reservation
11173 * check, may be technically wrong. I guess the only thing to do
11174 * would be to check for and report the reset events here, and then
11175 * check for the other unit attention types after we check for a
11176 * reservation conflict.
11178 * XXX KDM need to fix this
11180 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11181 ctl_ua_type ua_type;
11183 ua_type = lun->pending_sense[initidx].ua_pending;
11184 if (ua_type != CTL_UA_NONE) {
11185 scsi_sense_data_type sense_format;
11188 sense_format = (lun->flags &
11189 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11192 sense_format = SSD_TYPE_FIXED;
11194 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11196 if (ua_type != CTL_UA_NONE) {
11197 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11198 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11200 ctsio->sense_len = SSD_FULL_SIZE;
11201 lun->pending_sense[initidx].ua_pending &=
11203 mtx_unlock(&lun->lun_lock);
11204 ctl_done((union ctl_io *)ctsio);
11211 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11212 mtx_unlock(&lun->lun_lock);
11213 ctl_done((union ctl_io *)ctsio);
11218 * XXX CHD this is where we want to send IO to other side if
11219 * this LUN is secondary on this SC. We will need to make a copy
11220 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11221 * the copy we send as FROM_OTHER.
11222 * We also need to stuff the address of the original IO so we can
11223 * find it easily. Something similar will need be done on the other
11224 * side so when we are done we can find the copy.
11226 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11227 union ctl_ha_msg msg_info;
11230 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11232 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11233 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11235 printf("1. ctsio %p\n", ctsio);
11237 msg_info.hdr.serializing_sc = NULL;
11238 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11239 msg_info.scsi.tag_num = ctsio->tag_num;
11240 msg_info.scsi.tag_type = ctsio->tag_type;
11241 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11243 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11245 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11246 (void *)&msg_info, sizeof(msg_info), 0)) >
11247 CTL_HA_STATUS_SUCCESS) {
11248 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11250 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11253 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11258 * XXX KDM this I/O is off the incoming queue, but hasn't
11259 * been inserted on any other queue. We may need to come
11260 * up with a holding queue while we wait for serialization
11261 * so that we have an idea of what we're waiting for from
11264 mtx_unlock(&lun->lun_lock);
11268 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11269 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11270 ctl_ooaq, ooa_links))) {
11271 case CTL_ACTION_BLOCK:
11272 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11273 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11275 mtx_unlock(&lun->lun_lock);
11277 case CTL_ACTION_PASS:
11278 case CTL_ACTION_SKIP:
11279 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11280 mtx_unlock(&lun->lun_lock);
11281 ctl_enqueue_rtr((union ctl_io *)ctsio);
11283 case CTL_ACTION_OVERLAP:
11284 mtx_unlock(&lun->lun_lock);
11285 ctl_set_overlapped_cmd(ctsio);
11286 ctl_done((union ctl_io *)ctsio);
11288 case CTL_ACTION_OVERLAP_TAG:
11289 mtx_unlock(&lun->lun_lock);
11290 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11291 ctl_done((union ctl_io *)ctsio);
11293 case CTL_ACTION_ERROR:
11295 mtx_unlock(&lun->lun_lock);
11296 ctl_set_internal_failure(ctsio,
11298 /*retry_count*/ 0);
11299 ctl_done((union ctl_io *)ctsio);
11305 const struct ctl_cmd_entry *
11306 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11308 const struct ctl_cmd_entry *entry;
11309 int service_action;
11311 entry = &ctl_cmd_table[ctsio->cdb[0]];
11312 if (entry->flags & CTL_CMD_FLAG_SA5) {
11313 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11314 entry = &((const struct ctl_cmd_entry *)
11315 entry->execute)[service_action];
11320 const struct ctl_cmd_entry *
11321 ctl_validate_command(struct ctl_scsiio *ctsio)
11323 const struct ctl_cmd_entry *entry;
11327 entry = ctl_get_cmd_entry(ctsio);
11328 if (entry->execute == NULL) {
11329 ctl_set_invalid_opcode(ctsio);
11330 ctl_done((union ctl_io *)ctsio);
11333 KASSERT(entry->length > 0,
11334 ("Not defined length for command 0x%02x/0x%02x",
11335 ctsio->cdb[0], ctsio->cdb[1]));
11336 for (i = 1; i < entry->length; i++) {
11337 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11340 ctl_set_invalid_field(ctsio,
11345 /*bit*/ fls(diff) - 1);
11346 ctl_done((union ctl_io *)ctsio);
11353 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11356 switch (lun_type) {
11358 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11359 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11363 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11364 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11374 ctl_scsiio(struct ctl_scsiio *ctsio)
11377 const struct ctl_cmd_entry *entry;
11379 retval = CTL_RETVAL_COMPLETE;
11381 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11383 entry = ctl_get_cmd_entry(ctsio);
11386 * If this I/O has been aborted, just send it straight to
11387 * ctl_done() without executing it.
11389 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11390 ctl_done((union ctl_io *)ctsio);
11395 * All the checks should have been handled by ctl_scsiio_precheck().
11396 * We should be clear now to just execute the I/O.
11398 retval = entry->execute(ctsio);
11405 * Since we only implement one target right now, a bus reset simply resets
11406 * our single target.
11409 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11411 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11415 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11416 ctl_ua_type ua_type)
11418 struct ctl_lun *lun;
11421 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11422 union ctl_ha_msg msg_info;
11424 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11425 msg_info.hdr.nexus = io->io_hdr.nexus;
11426 if (ua_type==CTL_UA_TARG_RESET)
11427 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11429 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11430 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11431 msg_info.hdr.original_sc = NULL;
11432 msg_info.hdr.serializing_sc = NULL;
11433 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11434 (void *)&msg_info, sizeof(msg_info), 0)) {
11439 mtx_lock(&ctl_softc->ctl_lock);
11440 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11441 retval += ctl_lun_reset(lun, io, ua_type);
11442 mtx_unlock(&ctl_softc->ctl_lock);
11448 * The LUN should always be set. The I/O is optional, and is used to
11449 * distinguish between I/Os sent by this initiator, and by other
11450 * initiators. We set unit attention for initiators other than this one.
11451 * SAM-3 is vague on this point. It does say that a unit attention should
11452 * be established for other initiators when a LUN is reset (see section
11453 * 5.7.3), but it doesn't specifically say that the unit attention should
11454 * be established for this particular initiator when a LUN is reset. Here
11455 * is the relevant text, from SAM-3 rev 8:
11457 * 5.7.2 When a SCSI initiator port aborts its own tasks
11459 * When a SCSI initiator port causes its own task(s) to be aborted, no
11460 * notification that the task(s) have been aborted shall be returned to
11461 * the SCSI initiator port other than the completion response for the
11462 * command or task management function action that caused the task(s) to
11463 * be aborted and notification(s) associated with related effects of the
11464 * action (e.g., a reset unit attention condition).
11466 * XXX KDM for now, we're setting unit attention for all initiators.
11469 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11473 uint32_t initindex;
11477 mtx_lock(&lun->lun_lock);
11479 * Run through the OOA queue and abort each I/O.
11482 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11484 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11485 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11486 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11490 * This version sets unit attention for every
11493 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11494 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11495 if (initindex == i)
11497 lun->pending_sense[i].ua_pending |= ua_type;
11502 * A reset (any kind, really) clears reservations established with
11503 * RESERVE/RELEASE. It does not clear reservations established
11504 * with PERSISTENT RESERVE OUT, but we don't support that at the
11505 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11506 * reservations made with the RESERVE/RELEASE commands, because
11507 * those commands are obsolete in SPC-3.
11509 lun->flags &= ~CTL_LUN_RESERVED;
11511 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11512 ctl_clear_mask(lun->have_ca, i);
11513 lun->pending_sense[i].ua_pending |= ua_type;
11515 mtx_unlock(&lun->lun_lock);
11521 ctl_abort_task(union ctl_io *io)
11524 struct ctl_lun *lun;
11525 struct ctl_softc *ctl_softc;
11528 char printbuf[128];
11533 ctl_softc = control_softc;
11539 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11540 mtx_lock(&ctl_softc->ctl_lock);
11541 if ((targ_lun < CTL_MAX_LUNS)
11542 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11543 lun = ctl_softc->ctl_luns[targ_lun];
11545 mtx_unlock(&ctl_softc->ctl_lock);
11550 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11551 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11554 mtx_lock(&lun->lun_lock);
11555 mtx_unlock(&ctl_softc->ctl_lock);
11557 * Run through the OOA queue and attempt to find the given I/O.
11558 * The target port, initiator ID, tag type and tag number have to
11559 * match the values that we got from the initiator. If we have an
11560 * untagged command to abort, simply abort the first untagged command
11561 * we come to. We only allow one untagged command at a time of course.
11564 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11566 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11567 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11569 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11571 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11572 lun->lun, xio->scsiio.tag_num,
11573 xio->scsiio.tag_type,
11574 (xio->io_hdr.blocked_links.tqe_prev
11575 == NULL) ? "" : " BLOCKED",
11576 (xio->io_hdr.flags &
11577 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11578 (xio->io_hdr.flags &
11579 CTL_FLAG_ABORT) ? " ABORT" : "",
11580 (xio->io_hdr.flags &
11581 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11582 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11584 printf("%s\n", sbuf_data(&sb));
11587 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11588 && (xio->io_hdr.nexus.initid.id ==
11589 io->io_hdr.nexus.initid.id)) {
11591 * If the abort says that the task is untagged, the
11592 * task in the queue must be untagged. Otherwise,
11593 * we just check to see whether the tag numbers
11594 * match. This is because the QLogic firmware
11595 * doesn't pass back the tag type in an abort
11599 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11600 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11601 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11604 * XXX KDM we've got problems with FC, because it
11605 * doesn't send down a tag type with aborts. So we
11606 * can only really go by the tag number...
11607 * This may cause problems with parallel SCSI.
11608 * Need to figure that out!!
11610 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11611 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11613 if ((io->io_hdr.flags &
11614 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11615 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11616 union ctl_ha_msg msg_info;
11618 io->io_hdr.flags |=
11619 CTL_FLAG_SENT_2OTHER_SC;
11620 msg_info.hdr.nexus = io->io_hdr.nexus;
11621 msg_info.task.task_action =
11622 CTL_TASK_ABORT_TASK;
11623 msg_info.task.tag_num =
11624 io->taskio.tag_num;
11625 msg_info.task.tag_type =
11626 io->taskio.tag_type;
11627 msg_info.hdr.msg_type =
11628 CTL_MSG_MANAGE_TASKS;
11629 msg_info.hdr.original_sc = NULL;
11630 msg_info.hdr.serializing_sc = NULL;
11632 printf("Sent Abort to other side\n");
11634 if (CTL_HA_STATUS_SUCCESS !=
11635 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11637 sizeof(msg_info), 0)) {
11641 printf("ctl_abort_task: found I/O to abort\n");
11647 mtx_unlock(&lun->lun_lock);
11653 * This isn't really an error. It's entirely possible for
11654 * the abort and command completion to cross on the wire.
11655 * This is more of an informative/diagnostic error.
11658 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11659 "%d:%d:%d:%d tag %d type %d\n",
11660 io->io_hdr.nexus.initid.id,
11661 io->io_hdr.nexus.targ_port,
11662 io->io_hdr.nexus.targ_target.id,
11663 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11664 io->taskio.tag_type);
11672 ctl_run_task(union ctl_io *io)
11674 struct ctl_softc *ctl_softc;
11676 const char *task_desc;
11678 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11680 ctl_softc = control_softc;
11683 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11684 ("ctl_run_task: Unextected io_type %d\n",
11685 io->io_hdr.io_type));
11687 task_desc = ctl_scsi_task_string(&io->taskio);
11688 if (task_desc != NULL) {
11690 csevent_log(CSC_CTL | CSC_SHELF_SW |
11692 csevent_LogType_Trace,
11693 csevent_Severity_Information,
11694 csevent_AlertLevel_Green,
11695 csevent_FRU_Firmware,
11696 csevent_FRU_Unknown,
11697 "CTL: received task: %s",task_desc);
11701 csevent_log(CSC_CTL | CSC_SHELF_SW |
11703 csevent_LogType_Trace,
11704 csevent_Severity_Information,
11705 csevent_AlertLevel_Green,
11706 csevent_FRU_Firmware,
11707 csevent_FRU_Unknown,
11708 "CTL: received unknown task "
11710 io->taskio.task_action,
11711 io->taskio.task_action);
11714 switch (io->taskio.task_action) {
11715 case CTL_TASK_ABORT_TASK:
11716 retval = ctl_abort_task(io);
11718 case CTL_TASK_ABORT_TASK_SET:
11720 case CTL_TASK_CLEAR_ACA:
11722 case CTL_TASK_CLEAR_TASK_SET:
11724 case CTL_TASK_LUN_RESET: {
11725 struct ctl_lun *lun;
11729 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11730 mtx_lock(&ctl_softc->ctl_lock);
11731 if ((targ_lun < CTL_MAX_LUNS)
11732 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11733 lun = ctl_softc->ctl_luns[targ_lun];
11735 mtx_unlock(&ctl_softc->ctl_lock);
11740 if (!(io->io_hdr.flags &
11741 CTL_FLAG_FROM_OTHER_SC)) {
11742 union ctl_ha_msg msg_info;
11744 io->io_hdr.flags |=
11745 CTL_FLAG_SENT_2OTHER_SC;
11746 msg_info.hdr.msg_type =
11747 CTL_MSG_MANAGE_TASKS;
11748 msg_info.hdr.nexus = io->io_hdr.nexus;
11749 msg_info.task.task_action =
11750 CTL_TASK_LUN_RESET;
11751 msg_info.hdr.original_sc = NULL;
11752 msg_info.hdr.serializing_sc = NULL;
11753 if (CTL_HA_STATUS_SUCCESS !=
11754 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11756 sizeof(msg_info), 0)) {
11760 retval = ctl_lun_reset(lun, io,
11762 mtx_unlock(&ctl_softc->ctl_lock);
11765 case CTL_TASK_TARGET_RESET:
11766 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
11768 case CTL_TASK_BUS_RESET:
11769 retval = ctl_bus_reset(ctl_softc, io);
11771 case CTL_TASK_PORT_LOGIN:
11773 case CTL_TASK_PORT_LOGOUT:
11776 printf("ctl_run_task: got unknown task management event %d\n",
11777 io->taskio.task_action);
11781 io->io_hdr.status = CTL_SUCCESS;
11783 io->io_hdr.status = CTL_ERROR;
11786 * This will queue this I/O to the done queue, but the
11787 * work thread won't be able to process it until we
11788 * return and the lock is released.
11794 * For HA operation. Handle commands that come in from the other
11798 ctl_handle_isc(union ctl_io *io)
11801 struct ctl_lun *lun;
11802 struct ctl_softc *ctl_softc;
11805 ctl_softc = control_softc;
11807 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11808 lun = ctl_softc->ctl_luns[targ_lun];
11810 switch (io->io_hdr.msg_type) {
11811 case CTL_MSG_SERIALIZE:
11812 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11814 case CTL_MSG_R2R: {
11815 const struct ctl_cmd_entry *entry;
11818 * This is only used in SER_ONLY mode.
11821 entry = ctl_get_cmd_entry(&io->scsiio);
11822 mtx_lock(&lun->lun_lock);
11823 if (ctl_scsiio_lun_check(ctl_softc, lun,
11824 entry, (struct ctl_scsiio *)io) != 0) {
11825 mtx_unlock(&lun->lun_lock);
11829 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11830 mtx_unlock(&lun->lun_lock);
11831 ctl_enqueue_rtr(io);
11834 case CTL_MSG_FINISH_IO:
11835 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11840 mtx_lock(&lun->lun_lock);
11841 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11843 ctl_check_blocked(lun);
11844 mtx_unlock(&lun->lun_lock);
11847 case CTL_MSG_PERS_ACTION:
11848 ctl_hndl_per_res_out_on_other_sc(
11849 (union ctl_ha_msg *)&io->presio.pr_msg);
11852 case CTL_MSG_BAD_JUJU:
11856 case CTL_MSG_DATAMOVE:
11857 /* Only used in XFER mode */
11859 ctl_datamove_remote(io);
11861 case CTL_MSG_DATAMOVE_DONE:
11862 /* Only used in XFER mode */
11864 io->scsiio.be_move_done(io);
11868 printf("%s: Invalid message type %d\n",
11869 __func__, io->io_hdr.msg_type);
11879 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11880 * there is no match.
11882 static ctl_lun_error_pattern
11883 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11885 const struct ctl_cmd_entry *entry;
11886 ctl_lun_error_pattern filtered_pattern, pattern;
11888 pattern = desc->error_pattern;
11891 * XXX KDM we need more data passed into this function to match a
11892 * custom pattern, and we actually need to implement custom pattern
11895 if (pattern & CTL_LUN_PAT_CMD)
11896 return (CTL_LUN_PAT_CMD);
11898 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11899 return (CTL_LUN_PAT_ANY);
11901 entry = ctl_get_cmd_entry(ctsio);
11903 filtered_pattern = entry->pattern & pattern;
11906 * If the user requested specific flags in the pattern (e.g.
11907 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11910 * If the user did not specify any flags, it doesn't matter whether
11911 * or not the command supports the flags.
11913 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11914 (pattern & ~CTL_LUN_PAT_MASK))
11915 return (CTL_LUN_PAT_NONE);
11918 * If the user asked for a range check, see if the requested LBA
11919 * range overlaps with this command's LBA range.
11921 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11927 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11929 return (CTL_LUN_PAT_NONE);
11931 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11932 desc->lba_range.len);
11934 * A "pass" means that the LBA ranges don't overlap, so
11935 * this doesn't match the user's range criteria.
11937 if (action == CTL_ACTION_PASS)
11938 return (CTL_LUN_PAT_NONE);
11941 return (filtered_pattern);
11945 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11947 struct ctl_error_desc *desc, *desc2;
11949 mtx_assert(&lun->lun_lock, MA_OWNED);
11951 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11952 ctl_lun_error_pattern pattern;
11954 * Check to see whether this particular command matches
11955 * the pattern in the descriptor.
11957 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11958 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11961 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11962 case CTL_LUN_INJ_ABORTED:
11963 ctl_set_aborted(&io->scsiio);
11965 case CTL_LUN_INJ_MEDIUM_ERR:
11966 ctl_set_medium_error(&io->scsiio);
11968 case CTL_LUN_INJ_UA:
11969 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11971 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11973 case CTL_LUN_INJ_CUSTOM:
11975 * We're assuming the user knows what he is doing.
11976 * Just copy the sense information without doing
11979 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11980 ctl_min(sizeof(desc->custom_sense),
11981 sizeof(io->scsiio.sense_data)));
11982 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11983 io->scsiio.sense_len = SSD_FULL_SIZE;
11984 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11986 case CTL_LUN_INJ_NONE:
11989 * If this is an error injection type we don't know
11990 * about, clear the continuous flag (if it is set)
11991 * so it will get deleted below.
11993 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11997 * By default, each error injection action is a one-shot
11999 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12002 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12008 #ifdef CTL_IO_DELAY
12010 ctl_datamove_timer_wakeup(void *arg)
12014 io = (union ctl_io *)arg;
12018 #endif /* CTL_IO_DELAY */
12021 ctl_datamove(union ctl_io *io)
12023 void (*fe_datamove)(union ctl_io *io);
12025 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12027 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12030 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12035 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12036 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12038 sbuf_cat(&sb, path_str);
12039 switch (io->io_hdr.io_type) {
12041 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12042 sbuf_printf(&sb, "\n");
12043 sbuf_cat(&sb, path_str);
12044 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12045 io->scsiio.tag_num, io->scsiio.tag_type);
12048 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12049 "Tag Type: %d\n", io->taskio.task_action,
12050 io->taskio.tag_num, io->taskio.tag_type);
12053 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12054 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12057 sbuf_cat(&sb, path_str);
12058 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12059 (intmax_t)time_uptime - io->io_hdr.start_time);
12061 printf("%s", sbuf_data(&sb));
12063 #endif /* CTL_TIME_IO */
12065 #ifdef CTL_IO_DELAY
12066 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12067 struct ctl_lun *lun;
12069 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12071 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12073 struct ctl_lun *lun;
12075 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12077 && (lun->delay_info.datamove_delay > 0)) {
12078 struct callout *callout;
12080 callout = (struct callout *)&io->io_hdr.timer_bytes;
12081 callout_init(callout, /*mpsafe*/ 1);
12082 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12083 callout_reset(callout,
12084 lun->delay_info.datamove_delay * hz,
12085 ctl_datamove_timer_wakeup, io);
12086 if (lun->delay_info.datamove_type ==
12087 CTL_DELAY_TYPE_ONESHOT)
12088 lun->delay_info.datamove_delay = 0;
12095 * This command has been aborted. Set the port status, so we fail
12098 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12099 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12100 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12101 io->io_hdr.nexus.targ_port,
12102 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12103 io->io_hdr.nexus.targ_lun);
12104 io->io_hdr.status = CTL_CMD_ABORTED;
12105 io->io_hdr.port_status = 31337;
12107 * Note that the backend, in this case, will get the
12108 * callback in its context. In other cases it may get
12109 * called in the frontend's interrupt thread context.
12111 io->scsiio.be_move_done(io);
12116 * If we're in XFER mode and this I/O is from the other shelf
12117 * controller, we need to send the DMA to the other side to
12118 * actually transfer the data to/from the host. In serialize only
12119 * mode the transfer happens below CTL and ctl_datamove() is only
12120 * called on the machine that originally received the I/O.
12122 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12123 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12124 union ctl_ha_msg msg;
12125 uint32_t sg_entries_sent;
12129 memset(&msg, 0, sizeof(msg));
12130 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12131 msg.hdr.original_sc = io->io_hdr.original_sc;
12132 msg.hdr.serializing_sc = io;
12133 msg.hdr.nexus = io->io_hdr.nexus;
12134 msg.dt.flags = io->io_hdr.flags;
12136 * We convert everything into a S/G list here. We can't
12137 * pass by reference, only by value between controllers.
12138 * So we can't pass a pointer to the S/G list, only as many
12139 * S/G entries as we can fit in here. If it's possible for
12140 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12141 * then we need to break this up into multiple transfers.
12143 if (io->scsiio.kern_sg_entries == 0) {
12144 msg.dt.kern_sg_entries = 1;
12146 * If this is in cached memory, flush the cache
12147 * before we send the DMA request to the other
12148 * controller. We want to do this in either the
12149 * read or the write case. The read case is
12150 * straightforward. In the write case, we want to
12151 * make sure nothing is in the local cache that
12152 * could overwrite the DMAed data.
12154 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12156 * XXX KDM use bus_dmamap_sync() here.
12161 * Convert to a physical address if this is a
12164 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12165 msg.dt.sg_list[0].addr =
12166 io->scsiio.kern_data_ptr;
12169 * XXX KDM use busdma here!
12172 msg.dt.sg_list[0].addr = (void *)
12173 vtophys(io->scsiio.kern_data_ptr);
12177 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12180 struct ctl_sg_entry *sgl;
12183 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12184 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12185 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12187 * XXX KDM use bus_dmamap_sync() here.
12192 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12193 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12194 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12195 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12196 msg.dt.sg_sequence = 0;
12199 * Loop until we've sent all of the S/G entries. On the
12200 * other end, we'll recompose these S/G entries into one
12201 * contiguous list before passing it to the
12203 for (sg_entries_sent = 0; sg_entries_sent <
12204 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12205 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12206 sizeof(msg.dt.sg_list[0])),
12207 msg.dt.kern_sg_entries - sg_entries_sent);
12209 if (do_sg_copy != 0) {
12210 struct ctl_sg_entry *sgl;
12213 sgl = (struct ctl_sg_entry *)
12214 io->scsiio.kern_data_ptr;
12216 * If this is in cached memory, flush the cache
12217 * before we send the DMA request to the other
12218 * controller. We want to do this in either
12219 * the * read or the write case. The read
12220 * case is straightforward. In the write
12221 * case, we want to make sure nothing is
12222 * in the local cache that could overwrite
12226 for (i = sg_entries_sent, j = 0;
12227 i < msg.dt.cur_sg_entries; i++, j++) {
12228 if ((io->io_hdr.flags &
12229 CTL_FLAG_NO_DATASYNC) == 0) {
12231 * XXX KDM use bus_dmamap_sync()
12234 if ((io->io_hdr.flags &
12235 CTL_FLAG_BUS_ADDR) == 0) {
12237 * XXX KDM use busdma.
12240 msg.dt.sg_list[j].addr =(void *)
12241 vtophys(sgl[i].addr);
12244 msg.dt.sg_list[j].addr =
12247 msg.dt.sg_list[j].len = sgl[i].len;
12251 sg_entries_sent += msg.dt.cur_sg_entries;
12252 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12253 msg.dt.sg_last = 1;
12255 msg.dt.sg_last = 0;
12258 * XXX KDM drop and reacquire the lock here?
12260 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12261 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12263 * XXX do something here.
12267 msg.dt.sent_sg_entries = sg_entries_sent;
12269 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12270 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12271 ctl_failover_io(io, /*have_lock*/ 0);
12276 * Lookup the fe_datamove() function for this particular
12280 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12287 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12289 union ctl_ha_msg msg;
12292 memset(&msg, 0, sizeof(msg));
12294 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12295 msg.hdr.original_sc = io;
12296 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12297 msg.hdr.nexus = io->io_hdr.nexus;
12298 msg.hdr.status = io->io_hdr.status;
12299 msg.scsi.tag_num = io->scsiio.tag_num;
12300 msg.scsi.tag_type = io->scsiio.tag_type;
12301 msg.scsi.scsi_status = io->scsiio.scsi_status;
12302 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12303 sizeof(io->scsiio.sense_data));
12304 msg.scsi.sense_len = io->scsiio.sense_len;
12305 msg.scsi.sense_residual = io->scsiio.sense_residual;
12306 msg.scsi.fetd_status = io->io_hdr.port_status;
12307 msg.scsi.residual = io->scsiio.residual;
12308 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12310 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12311 ctl_failover_io(io, /*have_lock*/ have_lock);
12315 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12316 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12317 /* XXX do something if this fails */
12323 * The DMA to the remote side is done, now we need to tell the other side
12324 * we're done so it can continue with its data movement.
12327 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12333 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12334 printf("%s: ISC DMA write failed with error %d", __func__,
12336 ctl_set_internal_failure(&io->scsiio,
12338 /*retry_count*/ rq->ret);
12341 ctl_dt_req_free(rq);
12344 * In this case, we had to malloc the memory locally. Free it.
12346 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12348 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12349 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12352 * The data is in local and remote memory, so now we need to send
12353 * status (good or back) back to the other side.
12355 ctl_send_datamove_done(io, /*have_lock*/ 0);
12359 * We've moved the data from the host/controller into local memory. Now we
12360 * need to push it over to the remote controller's memory.
12363 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12369 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12370 ctl_datamove_remote_write_cb);
12376 ctl_datamove_remote_write(union ctl_io *io)
12379 void (*fe_datamove)(union ctl_io *io);
12382 * - Get the data from the host/HBA into local memory.
12383 * - DMA memory from the local controller to the remote controller.
12384 * - Send status back to the remote controller.
12387 retval = ctl_datamove_remote_sgl_setup(io);
12391 /* Switch the pointer over so the FETD knows what to do */
12392 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12395 * Use a custom move done callback, since we need to send completion
12396 * back to the other controller, not to the backend on this side.
12398 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12400 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12409 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12418 * In this case, we had to malloc the memory locally. Free it.
12420 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12422 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12423 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12427 scsi_path_string(io, path_str, sizeof(path_str));
12428 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12429 sbuf_cat(&sb, path_str);
12430 scsi_command_string(&io->scsiio, NULL, &sb);
12431 sbuf_printf(&sb, "\n");
12432 sbuf_cat(&sb, path_str);
12433 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12434 io->scsiio.tag_num, io->scsiio.tag_type);
12435 sbuf_cat(&sb, path_str);
12436 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12437 io->io_hdr.flags, io->io_hdr.status);
12439 printk("%s", sbuf_data(&sb));
12444 * The read is done, now we need to send status (good or bad) back
12445 * to the other side.
12447 ctl_send_datamove_done(io, /*have_lock*/ 0);
12453 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12456 void (*fe_datamove)(union ctl_io *io);
12460 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12461 printf("%s: ISC DMA read failed with error %d", __func__,
12463 ctl_set_internal_failure(&io->scsiio,
12465 /*retry_count*/ rq->ret);
12468 ctl_dt_req_free(rq);
12470 /* Switch the pointer over so the FETD knows what to do */
12471 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12474 * Use a custom move done callback, since we need to send completion
12475 * back to the other controller, not to the backend on this side.
12477 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12479 /* XXX KDM add checks like the ones in ctl_datamove? */
12481 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12487 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12489 struct ctl_sg_entry *local_sglist, *remote_sglist;
12490 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12491 struct ctl_softc *softc;
12496 softc = control_softc;
12498 local_sglist = io->io_hdr.local_sglist;
12499 local_dma_sglist = io->io_hdr.local_dma_sglist;
12500 remote_sglist = io->io_hdr.remote_sglist;
12501 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12503 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12504 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12505 local_sglist[i].len = remote_sglist[i].len;
12508 * XXX Detect the situation where the RS-level I/O
12509 * redirector on the other side has already read the
12510 * data off of the AOR RS on this side, and
12511 * transferred it to remote (mirror) memory on the
12512 * other side. Since we already have the data in
12513 * memory here, we just need to use it.
12515 * XXX KDM this can probably be removed once we
12516 * get the cache device code in and take the
12517 * current AOR implementation out.
12520 if ((remote_sglist[i].addr >=
12521 (void *)vtophys(softc->mirr->addr))
12522 && (remote_sglist[i].addr <
12523 ((void *)vtophys(softc->mirr->addr) +
12524 CacheMirrorOffset))) {
12525 local_sglist[i].addr = remote_sglist[i].addr -
12527 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12529 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12531 local_sglist[i].addr = remote_sglist[i].addr +
12536 printf("%s: local %p, remote %p, len %d\n",
12537 __func__, local_sglist[i].addr,
12538 remote_sglist[i].addr, local_sglist[i].len);
12542 uint32_t len_to_go;
12545 * In this case, we don't have automatically allocated
12546 * memory for this I/O on this controller. This typically
12547 * happens with internal CTL I/O -- e.g. inquiry, mode
12548 * sense, etc. Anything coming from RAIDCore will have
12549 * a mirror area available.
12551 len_to_go = io->scsiio.kern_data_len;
12554 * Clear the no datasync flag, we have to use malloced
12557 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12560 * The difficult thing here is that the size of the various
12561 * S/G segments may be different than the size from the
12562 * remote controller. That'll make it harder when DMAing
12563 * the data back to the other side.
12565 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12566 sizeof(io->io_hdr.remote_sglist[0])) &&
12567 (len_to_go > 0); i++) {
12568 local_sglist[i].len = ctl_min(len_to_go, 131072);
12569 CTL_SIZE_8B(local_dma_sglist[i].len,
12570 local_sglist[i].len);
12571 local_sglist[i].addr =
12572 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12574 local_dma_sglist[i].addr = local_sglist[i].addr;
12576 if (local_sglist[i].addr == NULL) {
12579 printf("malloc failed for %zd bytes!",
12580 local_dma_sglist[i].len);
12581 for (j = 0; j < i; j++) {
12582 free(local_sglist[j].addr, M_CTL);
12584 ctl_set_internal_failure(&io->scsiio,
12586 /*retry_count*/ 4857);
12588 goto bailout_error;
12591 /* XXX KDM do we need a sync here? */
12593 len_to_go -= local_sglist[i].len;
12596 * Reset the number of S/G entries accordingly. The
12597 * original number of S/G entries is available in
12600 io->scsiio.kern_sg_entries = i;
12603 printf("%s: kern_sg_entries = %d\n", __func__,
12604 io->scsiio.kern_sg_entries);
12605 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12606 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12607 local_sglist[i].addr, local_sglist[i].len,
12608 local_dma_sglist[i].len);
12617 ctl_send_datamove_done(io, /*have_lock*/ 0);
12623 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12624 ctl_ha_dt_cb callback)
12626 struct ctl_ha_dt_req *rq;
12627 struct ctl_sg_entry *remote_sglist, *local_sglist;
12628 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12629 uint32_t local_used, remote_used, total_used;
12635 rq = ctl_dt_req_alloc();
12638 * If we failed to allocate the request, and if the DMA didn't fail
12639 * anyway, set busy status. This is just a resource allocation
12643 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12644 ctl_set_busy(&io->scsiio);
12646 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12649 ctl_dt_req_free(rq);
12652 * The data move failed. We need to return status back
12653 * to the other controller. No point in trying to DMA
12654 * data to the remote controller.
12657 ctl_send_datamove_done(io, /*have_lock*/ 0);
12664 local_sglist = io->io_hdr.local_sglist;
12665 local_dma_sglist = io->io_hdr.local_dma_sglist;
12666 remote_sglist = io->io_hdr.remote_sglist;
12667 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12672 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12673 rq->ret = CTL_HA_STATUS_SUCCESS;
12680 * Pull/push the data over the wire from/to the other controller.
12681 * This takes into account the possibility that the local and
12682 * remote sglists may not be identical in terms of the size of
12683 * the elements and the number of elements.
12685 * One fundamental assumption here is that the length allocated for
12686 * both the local and remote sglists is identical. Otherwise, we've
12687 * essentially got a coding error of some sort.
12689 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12691 uint32_t cur_len, dma_length;
12694 rq->id = CTL_HA_DATA_CTL;
12695 rq->command = command;
12699 * Both pointers should be aligned. But it is possible
12700 * that the allocation length is not. They should both
12701 * also have enough slack left over at the end, though,
12702 * to round up to the next 8 byte boundary.
12704 cur_len = ctl_min(local_sglist[i].len - local_used,
12705 remote_sglist[j].len - remote_used);
12708 * In this case, we have a size issue and need to decrease
12709 * the size, except in the case where we actually have less
12710 * than 8 bytes left. In that case, we need to increase
12711 * the DMA length to get the last bit.
12713 if ((cur_len & 0x7) != 0) {
12714 if (cur_len > 0x7) {
12715 cur_len = cur_len - (cur_len & 0x7);
12716 dma_length = cur_len;
12718 CTL_SIZE_8B(dma_length, cur_len);
12722 dma_length = cur_len;
12725 * If we had to allocate memory for this I/O, instead of using
12726 * the non-cached mirror memory, we'll need to flush the cache
12727 * before trying to DMA to the other controller.
12729 * We could end up doing this multiple times for the same
12730 * segment if we have a larger local segment than remote
12731 * segment. That shouldn't be an issue.
12733 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12735 * XXX KDM use bus_dmamap_sync() here.
12739 rq->size = dma_length;
12741 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12742 tmp_ptr += local_used;
12744 /* Use physical addresses when talking to ISC hardware */
12745 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12746 /* XXX KDM use busdma */
12748 rq->local = vtophys(tmp_ptr);
12751 rq->local = tmp_ptr;
12753 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12754 tmp_ptr += remote_used;
12755 rq->remote = tmp_ptr;
12757 rq->callback = NULL;
12759 local_used += cur_len;
12760 if (local_used >= local_sglist[i].len) {
12765 remote_used += cur_len;
12766 if (remote_used >= remote_sglist[j].len) {
12770 total_used += cur_len;
12772 if (total_used >= io->scsiio.kern_data_len)
12773 rq->callback = callback;
12775 if ((rq->size & 0x7) != 0) {
12776 printf("%s: warning: size %d is not on 8b boundary\n",
12777 __func__, rq->size);
12779 if (((uintptr_t)rq->local & 0x7) != 0) {
12780 printf("%s: warning: local %p not on 8b boundary\n",
12781 __func__, rq->local);
12783 if (((uintptr_t)rq->remote & 0x7) != 0) {
12784 printf("%s: warning: remote %p not on 8b boundary\n",
12785 __func__, rq->local);
12788 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12789 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12790 rq->local, rq->remote, rq->size);
12793 isc_ret = ctl_dt_single(rq);
12794 if (isc_ret == CTL_HA_STATUS_WAIT)
12797 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12798 rq->ret = CTL_HA_STATUS_SUCCESS;
12812 ctl_datamove_remote_read(union ctl_io *io)
12818 * This will send an error to the other controller in the case of a
12821 retval = ctl_datamove_remote_sgl_setup(io);
12825 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12826 ctl_datamove_remote_read_cb);
12828 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12830 * Make sure we free memory if there was an error.. The
12831 * ctl_datamove_remote_xfer() function will send the
12832 * datamove done message, or call the callback with an
12833 * error if there is a problem.
12835 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12836 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12843 * Process a datamove request from the other controller. This is used for
12844 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12845 * first. Once that is complete, the data gets DMAed into the remote
12846 * controller's memory. For reads, we DMA from the remote controller's
12847 * memory into our memory first, and then move it out to the FETD.
12850 ctl_datamove_remote(union ctl_io *io)
12852 struct ctl_softc *softc;
12854 softc = control_softc;
12856 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12859 * Note that we look for an aborted I/O here, but don't do some of
12860 * the other checks that ctl_datamove() normally does. We don't
12861 * need to run the task queue, because this I/O is on the ISC
12862 * queue, which is executed by the work thread after the task queue.
12863 * We don't need to run the datamove delay code, since that should
12864 * have been done if need be on the other controller.
12866 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12868 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12869 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12870 io->io_hdr.nexus.targ_port,
12871 io->io_hdr.nexus.targ_target.id,
12872 io->io_hdr.nexus.targ_lun);
12873 io->io_hdr.status = CTL_CMD_ABORTED;
12874 io->io_hdr.port_status = 31338;
12876 ctl_send_datamove_done(io, /*have_lock*/ 0);
12881 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12882 ctl_datamove_remote_write(io);
12883 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12884 ctl_datamove_remote_read(io);
12886 union ctl_ha_msg msg;
12887 struct scsi_sense_data *sense;
12891 memset(&msg, 0, sizeof(msg));
12893 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12894 msg.hdr.status = CTL_SCSI_ERROR;
12895 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12897 retry_count = 4243;
12899 sense = &msg.scsi.sense_data;
12900 sks[0] = SSD_SCS_VALID;
12901 sks[1] = (retry_count >> 8) & 0xff;
12902 sks[2] = retry_count & 0xff;
12904 /* "Internal target failure" */
12905 scsi_set_sense_data(sense,
12906 /*sense_format*/ SSD_TYPE_NONE,
12907 /*current_error*/ 1,
12908 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12911 /*type*/ SSD_ELEM_SKS,
12912 /*size*/ sizeof(sks),
12916 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12917 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12918 ctl_failover_io(io, /*have_lock*/ 1);
12922 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12923 CTL_HA_STATUS_SUCCESS) {
12924 /* XXX KDM what to do if this fails? */
12932 ctl_process_done(union ctl_io *io)
12934 struct ctl_lun *lun;
12935 struct ctl_softc *ctl_softc;
12936 void (*fe_done)(union ctl_io *io);
12937 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12939 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12942 control_softc->ctl_ports[targ_port]->fe_done;
12945 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12950 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12951 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12953 sbuf_cat(&sb, path_str);
12954 switch (io->io_hdr.io_type) {
12956 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12957 sbuf_printf(&sb, "\n");
12958 sbuf_cat(&sb, path_str);
12959 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12960 io->scsiio.tag_num, io->scsiio.tag_type);
12963 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12964 "Tag Type: %d\n", io->taskio.task_action,
12965 io->taskio.tag_num, io->taskio.tag_type);
12968 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12969 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12972 sbuf_cat(&sb, path_str);
12973 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12974 (intmax_t)time_uptime - io->io_hdr.start_time);
12976 printf("%s", sbuf_data(&sb));
12978 #endif /* CTL_TIME_IO */
12980 switch (io->io_hdr.io_type) {
12984 if (bootverbose || verbose > 0)
12985 ctl_io_error_print(io, NULL);
12986 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12990 return (CTL_RETVAL_COMPLETE);
12993 printf("ctl_process_done: invalid io type %d\n",
12994 io->io_hdr.io_type);
12995 panic("ctl_process_done: invalid io type %d\n",
12996 io->io_hdr.io_type);
12997 break; /* NOTREACHED */
13000 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13002 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13003 io->io_hdr.nexus.targ_mapped_lun));
13007 ctl_softc = lun->ctl_softc;
13009 mtx_lock(&lun->lun_lock);
13012 * Check to see if we have any errors to inject here. We only
13013 * inject errors for commands that don't already have errors set.
13015 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13016 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13017 ctl_inject_error(lun, io);
13020 * XXX KDM how do we treat commands that aren't completed
13023 * XXX KDM should we also track I/O latency?
13025 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13026 io->io_hdr.io_type == CTL_IO_SCSI) {
13028 struct bintime cur_bt;
13032 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13034 type = CTL_STATS_READ;
13035 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13037 type = CTL_STATS_WRITE;
13039 type = CTL_STATS_NO_IO;
13041 lun->stats.ports[targ_port].bytes[type] +=
13042 io->scsiio.kern_total_len;
13043 lun->stats.ports[targ_port].operations[type]++;
13045 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13046 &io->io_hdr.dma_bt);
13047 lun->stats.ports[targ_port].num_dmas[type] +=
13048 io->io_hdr.num_dmas;
13049 getbintime(&cur_bt);
13050 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13051 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13056 * Remove this from the OOA queue.
13058 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13061 * Run through the blocked queue on this LUN and see if anything
13062 * has become unblocked, now that this transaction is done.
13064 ctl_check_blocked(lun);
13067 * If the LUN has been invalidated, free it if there is nothing
13068 * left on its OOA queue.
13070 if ((lun->flags & CTL_LUN_INVALID)
13071 && TAILQ_EMPTY(&lun->ooa_queue)) {
13072 mtx_unlock(&lun->lun_lock);
13073 mtx_lock(&ctl_softc->ctl_lock);
13075 mtx_unlock(&ctl_softc->ctl_lock);
13077 mtx_unlock(&lun->lun_lock);
13080 * If this command has been aborted, make sure we set the status
13081 * properly. The FETD is responsible for freeing the I/O and doing
13082 * whatever it needs to do to clean up its state.
13084 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13085 io->io_hdr.status = CTL_CMD_ABORTED;
13088 * We print out status for every task management command. For SCSI
13089 * commands, we filter out any unit attention errors; they happen
13090 * on every boot, and would clutter up the log. Note: task
13091 * management commands aren't printed here, they are printed above,
13092 * since they should never even make it down here.
13094 switch (io->io_hdr.io_type) {
13095 case CTL_IO_SCSI: {
13096 int error_code, sense_key, asc, ascq;
13100 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13101 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13103 * Since this is just for printing, no need to
13104 * show errors here.
13106 scsi_extract_sense_len(&io->scsiio.sense_data,
13107 io->scsiio.sense_len,
13112 /*show_errors*/ 0);
13115 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13116 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13117 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13118 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13120 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13121 ctl_softc->skipped_prints++;
13123 uint32_t skipped_prints;
13125 skipped_prints = ctl_softc->skipped_prints;
13127 ctl_softc->skipped_prints = 0;
13128 ctl_softc->last_print_jiffies = time_uptime;
13130 if (skipped_prints > 0) {
13132 csevent_log(CSC_CTL | CSC_SHELF_SW |
13134 csevent_LogType_Trace,
13135 csevent_Severity_Information,
13136 csevent_AlertLevel_Green,
13137 csevent_FRU_Firmware,
13138 csevent_FRU_Unknown,
13139 "High CTL error volume, %d prints "
13140 "skipped", skipped_prints);
13143 if (bootverbose || verbose > 0)
13144 ctl_io_error_print(io, NULL);
13150 if (bootverbose || verbose > 0)
13151 ctl_io_error_print(io, NULL);
13158 * Tell the FETD or the other shelf controller we're done with this
13159 * command. Note that only SCSI commands get to this point. Task
13160 * management commands are completed above.
13162 * We only send status to the other controller if we're in XFER
13163 * mode. In SER_ONLY mode, the I/O is done on the controller that
13164 * received the I/O (from CTL's perspective), and so the status is
13167 * XXX KDM if we hold the lock here, we could cause a deadlock
13168 * if the frontend comes back in in this context to queue
13171 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13172 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13173 union ctl_ha_msg msg;
13175 memset(&msg, 0, sizeof(msg));
13176 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13177 msg.hdr.original_sc = io->io_hdr.original_sc;
13178 msg.hdr.nexus = io->io_hdr.nexus;
13179 msg.hdr.status = io->io_hdr.status;
13180 msg.scsi.scsi_status = io->scsiio.scsi_status;
13181 msg.scsi.tag_num = io->scsiio.tag_num;
13182 msg.scsi.tag_type = io->scsiio.tag_type;
13183 msg.scsi.sense_len = io->scsiio.sense_len;
13184 msg.scsi.sense_residual = io->scsiio.sense_residual;
13185 msg.scsi.residual = io->scsiio.residual;
13186 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13187 sizeof(io->scsiio.sense_data));
13189 * We copy this whether or not this is an I/O-related
13190 * command. Otherwise, we'd have to go and check to see
13191 * whether it's a read/write command, and it really isn't
13194 memcpy(&msg.scsi.lbalen,
13195 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13196 sizeof(msg.scsi.lbalen));
13198 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13199 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13200 /* XXX do something here */
13209 return (CTL_RETVAL_COMPLETE);
13213 * Front end should call this if it doesn't do autosense. When the request
13214 * sense comes back in from the initiator, we'll dequeue this and send it.
13217 ctl_queue_sense(union ctl_io *io)
13219 struct ctl_lun *lun;
13220 struct ctl_softc *ctl_softc;
13221 uint32_t initidx, targ_lun;
13223 ctl_softc = control_softc;
13225 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13228 * LUN lookup will likely move to the ctl_work_thread() once we
13229 * have our new queueing infrastructure (that doesn't put things on
13230 * a per-LUN queue initially). That is so that we can handle
13231 * things like an INQUIRY to a LUN that we don't have enabled. We
13232 * can't deal with that right now.
13234 mtx_lock(&ctl_softc->ctl_lock);
13237 * If we don't have a LUN for this, just toss the sense
13240 targ_lun = io->io_hdr.nexus.targ_lun;
13241 if (io->io_hdr.nexus.lun_map_fn != NULL)
13242 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13243 if ((targ_lun < CTL_MAX_LUNS)
13244 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13245 lun = ctl_softc->ctl_luns[targ_lun];
13249 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13251 mtx_lock(&lun->lun_lock);
13253 * Already have CA set for this LUN...toss the sense information.
13255 if (ctl_is_set(lun->have_ca, initidx)) {
13256 mtx_unlock(&lun->lun_lock);
13260 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13261 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13262 sizeof(io->scsiio.sense_data)));
13263 ctl_set_mask(lun->have_ca, initidx);
13264 mtx_unlock(&lun->lun_lock);
13267 mtx_unlock(&ctl_softc->ctl_lock);
13271 return (CTL_RETVAL_COMPLETE);
13275 * Primary command inlet from frontend ports. All SCSI and task I/O
13276 * requests must go through this function.
13279 ctl_queue(union ctl_io *io)
13281 struct ctl_softc *ctl_softc;
13283 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13285 ctl_softc = control_softc;
13288 io->io_hdr.start_time = time_uptime;
13289 getbintime(&io->io_hdr.start_bt);
13290 #endif /* CTL_TIME_IO */
13292 /* Map FE-specific LUN ID into global one. */
13293 if (io->io_hdr.nexus.lun_map_fn != NULL)
13294 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn(
13295 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun);
13297 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun;
13299 switch (io->io_hdr.io_type) {
13302 ctl_enqueue_incoming(io);
13305 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13309 return (CTL_RETVAL_COMPLETE);
13312 #ifdef CTL_IO_DELAY
13314 ctl_done_timer_wakeup(void *arg)
13318 io = (union ctl_io *)arg;
13321 #endif /* CTL_IO_DELAY */
13324 ctl_done(union ctl_io *io)
13326 struct ctl_softc *ctl_softc;
13328 ctl_softc = control_softc;
13331 * Enable this to catch duplicate completion issues.
13334 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13335 printf("%s: type %d msg %d cdb %x iptl: "
13336 "%d:%d:%d:%d tag 0x%04x "
13337 "flag %#x status %x\n",
13339 io->io_hdr.io_type,
13340 io->io_hdr.msg_type,
13342 io->io_hdr.nexus.initid.id,
13343 io->io_hdr.nexus.targ_port,
13344 io->io_hdr.nexus.targ_target.id,
13345 io->io_hdr.nexus.targ_lun,
13346 (io->io_hdr.io_type ==
13348 io->taskio.tag_num :
13349 io->scsiio.tag_num,
13351 io->io_hdr.status);
13353 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13357 * This is an internal copy of an I/O, and should not go through
13358 * the normal done processing logic.
13360 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13364 * We need to send a msg to the serializing shelf to finish the IO
13365 * as well. We don't send a finish message to the other shelf if
13366 * this is a task management command. Task management commands
13367 * aren't serialized in the OOA queue, but rather just executed on
13368 * both shelf controllers for commands that originated on that
13371 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13372 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13373 union ctl_ha_msg msg_io;
13375 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13376 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13377 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13378 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13380 /* continue on to finish IO */
13382 #ifdef CTL_IO_DELAY
13383 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13384 struct ctl_lun *lun;
13386 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13388 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13390 struct ctl_lun *lun;
13392 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13395 && (lun->delay_info.done_delay > 0)) {
13396 struct callout *callout;
13398 callout = (struct callout *)&io->io_hdr.timer_bytes;
13399 callout_init(callout, /*mpsafe*/ 1);
13400 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13401 callout_reset(callout,
13402 lun->delay_info.done_delay * hz,
13403 ctl_done_timer_wakeup, io);
13404 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13405 lun->delay_info.done_delay = 0;
13409 #endif /* CTL_IO_DELAY */
13411 ctl_enqueue_done(io);
13415 ctl_isc(struct ctl_scsiio *ctsio)
13417 struct ctl_lun *lun;
13420 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13422 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13424 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13426 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13433 ctl_work_thread(void *arg)
13435 struct ctl_thread *thr = (struct ctl_thread *)arg;
13436 struct ctl_softc *softc = thr->ctl_softc;
13440 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13446 * We handle the queues in this order:
13448 * - done queue (to free up resources, unblock other commands)
13452 * If those queues are empty, we break out of the loop and
13455 mtx_lock(&thr->queue_lock);
13456 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13458 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13459 mtx_unlock(&thr->queue_lock);
13460 ctl_handle_isc(io);
13463 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13465 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13466 /* clear any blocked commands, call fe_done */
13467 mtx_unlock(&thr->queue_lock);
13468 retval = ctl_process_done(io);
13471 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13473 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13474 mtx_unlock(&thr->queue_lock);
13475 if (io->io_hdr.io_type == CTL_IO_TASK)
13478 ctl_scsiio_precheck(softc, &io->scsiio);
13481 if (!ctl_pause_rtr) {
13482 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13484 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13485 mtx_unlock(&thr->queue_lock);
13486 retval = ctl_scsiio(&io->scsiio);
13487 if (retval != CTL_RETVAL_COMPLETE)
13488 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13493 /* Sleep until we have something to do. */
13494 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13499 ctl_lun_thread(void *arg)
13501 struct ctl_softc *softc = (struct ctl_softc *)arg;
13502 struct ctl_be_lun *be_lun;
13505 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13509 mtx_lock(&softc->ctl_lock);
13510 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13511 if (be_lun != NULL) {
13512 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13513 mtx_unlock(&softc->ctl_lock);
13514 ctl_create_lun(be_lun);
13518 /* Sleep until we have something to do. */
13519 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13520 PDROP | PRIBIO, "-", 0);
13525 ctl_enqueue_incoming(union ctl_io *io)
13527 struct ctl_softc *softc = control_softc;
13528 struct ctl_thread *thr;
13531 idx = (io->io_hdr.nexus.targ_port * 127 +
13532 io->io_hdr.nexus.initid.id) % worker_threads;
13533 thr = &softc->threads[idx];
13534 mtx_lock(&thr->queue_lock);
13535 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13536 mtx_unlock(&thr->queue_lock);
13541 ctl_enqueue_rtr(union ctl_io *io)
13543 struct ctl_softc *softc = control_softc;
13544 struct ctl_thread *thr;
13546 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13547 mtx_lock(&thr->queue_lock);
13548 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13549 mtx_unlock(&thr->queue_lock);
13554 ctl_enqueue_done(union ctl_io *io)
13556 struct ctl_softc *softc = control_softc;
13557 struct ctl_thread *thr;
13559 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13560 mtx_lock(&thr->queue_lock);
13561 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13562 mtx_unlock(&thr->queue_lock);
13567 ctl_enqueue_isc(union ctl_io *io)
13569 struct ctl_softc *softc = control_softc;
13570 struct ctl_thread *thr;
13572 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13573 mtx_lock(&thr->queue_lock);
13574 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13575 mtx_unlock(&thr->queue_lock);
13579 /* Initialization and failover */
13582 ctl_init_isc_msg(void)
13584 printf("CTL: Still calling this thing\n");
13589 * Initializes component into configuration defined by bootMode
13591 * returns hasc_Status:
13593 * ERROR - fatal error
13595 static ctl_ha_comp_status
13596 ctl_isc_init(struct ctl_ha_component *c)
13598 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13605 * Starts component in state requested. If component starts successfully,
13606 * it must set its own state to the requestrd state
13607 * When requested state is HASC_STATE_HA, the component may refine it
13608 * by adding _SLAVE or _MASTER flags.
13609 * Currently allowed state transitions are:
13610 * UNKNOWN->HA - initial startup
13611 * UNKNOWN->SINGLE - initial startup when no parter detected
13612 * HA->SINGLE - failover
13613 * returns ctl_ha_comp_status:
13614 * OK - component successfully started in requested state
13615 * FAILED - could not start the requested state, failover may
13617 * ERROR - fatal error detected, no future startup possible
13619 static ctl_ha_comp_status
13620 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13622 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13624 printf("%s: go\n", __func__);
13626 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13627 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13629 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13630 != CTL_HA_STATUS_SUCCESS) {
13631 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13632 ret = CTL_HA_COMP_STATUS_ERROR;
13634 } else if (CTL_HA_STATE_IS_HA(c->state)
13635 && CTL_HA_STATE_IS_SINGLE(state)){
13636 // HA->SINGLE transition
13640 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13642 ret = CTL_HA_COMP_STATUS_ERROR;
13644 if (CTL_HA_STATE_IS_SINGLE(state))
13653 * Quiesce component
13654 * The component must clear any error conditions (set status to OK) and
13655 * prepare itself to another Start call
13656 * returns ctl_ha_comp_status:
13660 static ctl_ha_comp_status
13661 ctl_isc_quiesce(struct ctl_ha_component *c)
13663 int ret = CTL_HA_COMP_STATUS_OK;
13670 struct ctl_ha_component ctl_ha_component_ctlisc =
13673 .state = CTL_HA_STATE_UNKNOWN,
13674 .init = ctl_isc_init,
13675 .start = ctl_isc_start,
13676 .quiesce = ctl_isc_quiesce