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 int lun_number, i, lun_malloced;
4304 * We currently only support Direct Access or Processor LUN types.
4306 switch (be_lun->lun_type) {
4314 be_lun->lun_config_status(be_lun->be_lun,
4315 CTL_LUN_CONFIG_FAILURE);
4318 if (ctl_lun == NULL) {
4319 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4326 memset(lun, 0, sizeof(*lun));
4328 lun->flags = CTL_LUN_MALLOCED;
4330 mtx_lock(&ctl_softc->ctl_lock);
4332 * See if the caller requested a particular LUN number. If so, see
4333 * if it is available. Otherwise, allocate the first available LUN.
4335 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4336 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4337 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4338 mtx_unlock(&ctl_softc->ctl_lock);
4339 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4340 printf("ctl: requested LUN ID %d is higher "
4341 "than CTL_MAX_LUNS - 1 (%d)\n",
4342 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4345 * XXX KDM return an error, or just assign
4346 * another LUN ID in this case??
4348 printf("ctl: requested LUN ID %d is already "
4349 "in use\n", be_lun->req_lun_id);
4351 if (lun->flags & CTL_LUN_MALLOCED)
4353 be_lun->lun_config_status(be_lun->be_lun,
4354 CTL_LUN_CONFIG_FAILURE);
4357 lun_number = be_lun->req_lun_id;
4359 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4360 if (lun_number == -1) {
4361 mtx_unlock(&ctl_softc->ctl_lock);
4362 printf("ctl: can't allocate LUN on target %ju, out of "
4363 "LUNs\n", (uintmax_t)target_id.id);
4364 if (lun->flags & CTL_LUN_MALLOCED)
4366 be_lun->lun_config_status(be_lun->be_lun,
4367 CTL_LUN_CONFIG_FAILURE);
4371 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4373 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4374 lun->target = target_id;
4375 lun->lun = lun_number;
4376 lun->be_lun = be_lun;
4378 * The processor LUN is always enabled. Disk LUNs come on line
4379 * disabled, and must be enabled by the backend.
4381 lun->flags |= CTL_LUN_DISABLED;
4382 lun->backend = be_lun->be;
4383 be_lun->ctl_lun = lun;
4384 be_lun->lun_id = lun_number;
4385 atomic_add_int(&be_lun->be->num_luns, 1);
4386 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4387 lun->flags |= CTL_LUN_STOPPED;
4389 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4390 lun->flags |= CTL_LUN_INOPERABLE;
4392 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4393 lun->flags |= CTL_LUN_PRIMARY_SC;
4395 lun->ctl_softc = ctl_softc;
4396 TAILQ_INIT(&lun->ooa_queue);
4397 TAILQ_INIT(&lun->blocked_queue);
4398 STAILQ_INIT(&lun->error_list);
4401 * Initialize the mode page index.
4403 ctl_init_page_index(lun);
4406 * Set the poweron UA for all initiators on this LUN only.
4408 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4409 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4412 * Now, before we insert this lun on the lun list, set the lun
4413 * inventory changed UA for all other luns.
4415 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4416 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4417 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4421 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4423 ctl_softc->ctl_luns[lun_number] = lun;
4425 ctl_softc->num_luns++;
4427 /* Setup statistics gathering */
4428 lun->stats.device_type = be_lun->lun_type;
4429 lun->stats.lun_number = lun_number;
4430 if (lun->stats.device_type == T_DIRECT)
4431 lun->stats.blocksize = be_lun->blocksize;
4433 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4434 for (i = 0;i < CTL_MAX_PORTS;i++)
4435 lun->stats.ports[i].targ_port = i;
4437 mtx_unlock(&ctl_softc->ctl_lock);
4439 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4442 * Run through each registered FETD and bring it online if it isn't
4443 * already. Enable the target ID if it hasn't been enabled, and
4444 * enable this particular LUN.
4446 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4449 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4451 printf("ctl_alloc_lun: FETD %s port %d returned error "
4452 "%d for lun_enable on target %ju lun %d\n",
4453 port->port_name, port->targ_port, retval,
4454 (uintmax_t)target_id.id, lun_number);
4456 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4464 * - LUN has already been marked invalid and any pending I/O has been taken
4468 ctl_free_lun(struct ctl_lun *lun)
4470 struct ctl_softc *softc;
4472 struct ctl_port *port;
4474 struct ctl_lun *nlun;
4477 softc = lun->ctl_softc;
4479 mtx_assert(&softc->ctl_lock, MA_OWNED);
4481 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4483 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4485 softc->ctl_luns[lun->lun] = NULL;
4487 if (!TAILQ_EMPTY(&lun->ooa_queue))
4488 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4493 * XXX KDM this scheme only works for a single target/multiple LUN
4494 * setup. It needs to be revamped for a multiple target scheme.
4496 * XXX KDM this results in port->lun_disable() getting called twice,
4497 * once when ctl_disable_lun() is called, and a second time here.
4498 * We really need to re-think the LUN disable semantics. There
4499 * should probably be several steps/levels to LUN removal:
4504 * Right now we only have a disable method when communicating to
4505 * the front end ports, at least for individual LUNs.
4508 STAILQ_FOREACH(port, &softc->port_list, links) {
4511 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4514 printf("ctl_free_lun: FETD %s port %d returned error "
4515 "%d for lun_disable on target %ju lun %jd\n",
4516 port->port_name, port->targ_port, retval,
4517 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4520 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4521 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4523 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4525 printf("ctl_free_lun: FETD %s port %d "
4526 "returned error %d for targ_disable on "
4527 "target %ju\n", port->port_name,
4528 port->targ_port, retval,
4529 (uintmax_t)lun->target.id);
4531 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4533 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4537 port->port_offline(port->onoff_arg);
4538 port->status &= ~CTL_PORT_STATUS_ONLINE;
4545 * Tell the backend to free resources, if this LUN has a backend.
4547 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4548 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4550 mtx_destroy(&lun->lun_lock);
4551 if (lun->flags & CTL_LUN_MALLOCED)
4554 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4555 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4556 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4564 ctl_create_lun(struct ctl_be_lun *be_lun)
4566 struct ctl_softc *ctl_softc;
4568 ctl_softc = control_softc;
4571 * ctl_alloc_lun() should handle all potential failure cases.
4573 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4577 ctl_add_lun(struct ctl_be_lun *be_lun)
4579 struct ctl_softc *ctl_softc = control_softc;
4581 mtx_lock(&ctl_softc->ctl_lock);
4582 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4583 mtx_unlock(&ctl_softc->ctl_lock);
4584 wakeup(&ctl_softc->pending_lun_queue);
4590 ctl_enable_lun(struct ctl_be_lun *be_lun)
4592 struct ctl_softc *ctl_softc;
4593 struct ctl_port *port, *nport;
4594 struct ctl_lun *lun;
4597 ctl_softc = control_softc;
4599 lun = (struct ctl_lun *)be_lun->ctl_lun;
4601 mtx_lock(&ctl_softc->ctl_lock);
4602 mtx_lock(&lun->lun_lock);
4603 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4605 * eh? Why did we get called if the LUN is already
4608 mtx_unlock(&lun->lun_lock);
4609 mtx_unlock(&ctl_softc->ctl_lock);
4612 lun->flags &= ~CTL_LUN_DISABLED;
4613 mtx_unlock(&lun->lun_lock);
4615 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4616 nport = STAILQ_NEXT(port, links);
4619 * Drop the lock while we call the FETD's enable routine.
4620 * This can lead to a callback into CTL (at least in the
4621 * case of the internal initiator frontend.
4623 mtx_unlock(&ctl_softc->ctl_lock);
4624 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4625 mtx_lock(&ctl_softc->ctl_lock);
4627 printf("%s: FETD %s port %d returned error "
4628 "%d for lun_enable on target %ju lun %jd\n",
4629 __func__, port->port_name, port->targ_port, retval,
4630 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4634 /* NOTE: TODO: why does lun enable affect port status? */
4635 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4640 mtx_unlock(&ctl_softc->ctl_lock);
4646 ctl_disable_lun(struct ctl_be_lun *be_lun)
4648 struct ctl_softc *ctl_softc;
4649 struct ctl_port *port;
4650 struct ctl_lun *lun;
4653 ctl_softc = control_softc;
4655 lun = (struct ctl_lun *)be_lun->ctl_lun;
4657 mtx_lock(&ctl_softc->ctl_lock);
4658 mtx_lock(&lun->lun_lock);
4659 if (lun->flags & CTL_LUN_DISABLED) {
4660 mtx_unlock(&lun->lun_lock);
4661 mtx_unlock(&ctl_softc->ctl_lock);
4664 lun->flags |= CTL_LUN_DISABLED;
4665 mtx_unlock(&lun->lun_lock);
4667 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4668 mtx_unlock(&ctl_softc->ctl_lock);
4670 * Drop the lock before we call the frontend's disable
4671 * routine, to avoid lock order reversals.
4673 * XXX KDM what happens if the frontend list changes while
4674 * we're traversing it? It's unlikely, but should be handled.
4676 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4678 mtx_lock(&ctl_softc->ctl_lock);
4680 printf("ctl_alloc_lun: FETD %s port %d returned error "
4681 "%d for lun_disable on target %ju lun %jd\n",
4682 port->port_name, port->targ_port, retval,
4683 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4687 mtx_unlock(&ctl_softc->ctl_lock);
4693 ctl_start_lun(struct ctl_be_lun *be_lun)
4695 struct ctl_softc *ctl_softc;
4696 struct ctl_lun *lun;
4698 ctl_softc = control_softc;
4700 lun = (struct ctl_lun *)be_lun->ctl_lun;
4702 mtx_lock(&lun->lun_lock);
4703 lun->flags &= ~CTL_LUN_STOPPED;
4704 mtx_unlock(&lun->lun_lock);
4710 ctl_stop_lun(struct ctl_be_lun *be_lun)
4712 struct ctl_softc *ctl_softc;
4713 struct ctl_lun *lun;
4715 ctl_softc = control_softc;
4717 lun = (struct ctl_lun *)be_lun->ctl_lun;
4719 mtx_lock(&lun->lun_lock);
4720 lun->flags |= CTL_LUN_STOPPED;
4721 mtx_unlock(&lun->lun_lock);
4727 ctl_lun_offline(struct ctl_be_lun *be_lun)
4729 struct ctl_softc *ctl_softc;
4730 struct ctl_lun *lun;
4732 ctl_softc = control_softc;
4734 lun = (struct ctl_lun *)be_lun->ctl_lun;
4736 mtx_lock(&lun->lun_lock);
4737 lun->flags |= CTL_LUN_OFFLINE;
4738 mtx_unlock(&lun->lun_lock);
4744 ctl_lun_online(struct ctl_be_lun *be_lun)
4746 struct ctl_softc *ctl_softc;
4747 struct ctl_lun *lun;
4749 ctl_softc = control_softc;
4751 lun = (struct ctl_lun *)be_lun->ctl_lun;
4753 mtx_lock(&lun->lun_lock);
4754 lun->flags &= ~CTL_LUN_OFFLINE;
4755 mtx_unlock(&lun->lun_lock);
4761 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4763 struct ctl_softc *ctl_softc;
4764 struct ctl_lun *lun;
4766 ctl_softc = control_softc;
4768 lun = (struct ctl_lun *)be_lun->ctl_lun;
4770 mtx_lock(&lun->lun_lock);
4773 * The LUN needs to be disabled before it can be marked invalid.
4775 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4776 mtx_unlock(&lun->lun_lock);
4780 * Mark the LUN invalid.
4782 lun->flags |= CTL_LUN_INVALID;
4785 * If there is nothing in the OOA queue, go ahead and free the LUN.
4786 * If we have something in the OOA queue, we'll free it when the
4787 * last I/O completes.
4789 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4790 mtx_unlock(&lun->lun_lock);
4791 mtx_lock(&ctl_softc->ctl_lock);
4793 mtx_unlock(&ctl_softc->ctl_lock);
4795 mtx_unlock(&lun->lun_lock);
4801 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4803 struct ctl_softc *ctl_softc;
4804 struct ctl_lun *lun;
4806 ctl_softc = control_softc;
4807 lun = (struct ctl_lun *)be_lun->ctl_lun;
4809 mtx_lock(&lun->lun_lock);
4810 lun->flags |= CTL_LUN_INOPERABLE;
4811 mtx_unlock(&lun->lun_lock);
4817 ctl_lun_operable(struct ctl_be_lun *be_lun)
4819 struct ctl_softc *ctl_softc;
4820 struct ctl_lun *lun;
4822 ctl_softc = control_softc;
4823 lun = (struct ctl_lun *)be_lun->ctl_lun;
4825 mtx_lock(&lun->lun_lock);
4826 lun->flags &= ~CTL_LUN_INOPERABLE;
4827 mtx_unlock(&lun->lun_lock);
4833 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4836 struct ctl_softc *softc;
4837 struct ctl_lun *lun;
4838 struct copan_aps_subpage *current_sp;
4839 struct ctl_page_index *page_index;
4842 softc = control_softc;
4844 mtx_lock(&softc->ctl_lock);
4846 lun = (struct ctl_lun *)be_lun->ctl_lun;
4847 mtx_lock(&lun->lun_lock);
4850 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4851 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4855 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4857 page_index = &lun->mode_pages.index[i];
4860 if (page_index == NULL) {
4861 mtx_unlock(&lun->lun_lock);
4862 mtx_unlock(&softc->ctl_lock);
4863 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4864 (uintmax_t)lun->lun);
4868 if ((softc->aps_locked_lun != 0)
4869 && (softc->aps_locked_lun != lun->lun)) {
4870 printf("%s: attempt to lock LUN %llu when %llu is already "
4872 mtx_unlock(&lun->lun_lock);
4873 mtx_unlock(&softc->ctl_lock);
4878 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4879 (page_index->page_len * CTL_PAGE_CURRENT));
4882 current_sp->lock_active = APS_LOCK_ACTIVE;
4883 softc->aps_locked_lun = lun->lun;
4885 current_sp->lock_active = 0;
4886 softc->aps_locked_lun = 0;
4891 * If we're in HA mode, try to send the lock message to the other
4894 if (ctl_is_single == 0) {
4896 union ctl_ha_msg lock_msg;
4898 lock_msg.hdr.nexus = *nexus;
4899 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4901 lock_msg.aps.lock_flag = 1;
4903 lock_msg.aps.lock_flag = 0;
4904 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4905 sizeof(lock_msg), 0);
4906 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4907 printf("%s: APS (lock=%d) error returned from "
4908 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4909 mtx_unlock(&lun->lun_lock);
4910 mtx_unlock(&softc->ctl_lock);
4915 mtx_unlock(&lun->lun_lock);
4916 mtx_unlock(&softc->ctl_lock);
4922 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4924 struct ctl_lun *lun;
4925 struct ctl_softc *softc;
4928 softc = control_softc;
4930 lun = (struct ctl_lun *)be_lun->ctl_lun;
4932 mtx_lock(&lun->lun_lock);
4934 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4935 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4937 mtx_unlock(&lun->lun_lock);
4941 * Backend "memory move is complete" callback for requests that never
4942 * make it down to say RAIDCore's configuration code.
4945 ctl_config_move_done(union ctl_io *io)
4949 retval = CTL_RETVAL_COMPLETE;
4952 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4954 * XXX KDM this shouldn't happen, but what if it does?
4956 if (io->io_hdr.io_type != CTL_IO_SCSI)
4957 panic("I/O type isn't CTL_IO_SCSI!");
4959 if ((io->io_hdr.port_status == 0)
4960 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4961 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4962 io->io_hdr.status = CTL_SUCCESS;
4963 else if ((io->io_hdr.port_status != 0)
4964 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4965 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4967 * For hardware error sense keys, the sense key
4968 * specific value is defined to be a retry count,
4969 * but we use it to pass back an internal FETD
4970 * error code. XXX KDM Hopefully the FETD is only
4971 * using 16 bits for an error code, since that's
4972 * all the space we have in the sks field.
4974 ctl_set_internal_failure(&io->scsiio,
4977 io->io_hdr.port_status);
4978 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4979 free(io->scsiio.kern_data_ptr, M_CTL);
4984 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4985 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4986 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4988 * XXX KDM just assuming a single pointer here, and not a
4989 * S/G list. If we start using S/G lists for config data,
4990 * we'll need to know how to clean them up here as well.
4992 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4993 free(io->scsiio.kern_data_ptr, M_CTL);
4994 /* Hopefully the user has already set the status... */
4998 * XXX KDM now we need to continue data movement. Some
5000 * - call ctl_scsiio() again? We don't do this for data
5001 * writes, because for those at least we know ahead of
5002 * time where the write will go and how long it is. For
5003 * config writes, though, that information is largely
5004 * contained within the write itself, thus we need to
5005 * parse out the data again.
5007 * - Call some other function once the data is in?
5011 * XXX KDM call ctl_scsiio() again for now, and check flag
5012 * bits to see whether we're allocated or not.
5014 retval = ctl_scsiio(&io->scsiio);
5021 * This gets called by a backend driver when it is done with a
5022 * data_submit method.
5025 ctl_data_submit_done(union ctl_io *io)
5028 * If the IO_CONT flag is set, we need to call the supplied
5029 * function to continue processing the I/O, instead of completing
5032 * If there is an error, though, we don't want to keep processing.
5033 * Instead, just send status back to the initiator.
5035 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5036 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5037 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5038 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5039 io->scsiio.io_cont(io);
5046 * This gets called by a backend driver when it is done with a
5047 * configuration write.
5050 ctl_config_write_done(union ctl_io *io)
5053 * If the IO_CONT flag is set, we need to call the supplied
5054 * function to continue processing the I/O, instead of completing
5057 * If there is an error, though, we don't want to keep processing.
5058 * Instead, just send status back to the initiator.
5060 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5061 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5062 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5063 io->scsiio.io_cont(io);
5067 * Since a configuration write can be done for commands that actually
5068 * have data allocated, like write buffer, and commands that have
5069 * no data, like start/stop unit, we need to check here.
5071 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5072 free(io->scsiio.kern_data_ptr, M_CTL);
5077 * SCSI release command.
5080 ctl_scsi_release(struct ctl_scsiio *ctsio)
5082 int length, longid, thirdparty_id, resv_id;
5083 struct ctl_softc *ctl_softc;
5084 struct ctl_lun *lun;
5089 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5091 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5092 ctl_softc = control_softc;
5094 switch (ctsio->cdb[0]) {
5096 struct scsi_release_10 *cdb;
5098 cdb = (struct scsi_release_10 *)ctsio->cdb;
5100 if (cdb->byte2 & SR10_LONGID)
5103 thirdparty_id = cdb->thirdparty_id;
5105 resv_id = cdb->resv_id;
5106 length = scsi_2btoul(cdb->length);
5113 * XXX KDM right now, we only support LUN reservation. We don't
5114 * support 3rd party reservations, or extent reservations, which
5115 * might actually need the parameter list. If we've gotten this
5116 * far, we've got a LUN reservation. Anything else got kicked out
5117 * above. So, according to SPC, ignore the length.
5121 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5123 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5124 ctsio->kern_data_len = length;
5125 ctsio->kern_total_len = length;
5126 ctsio->kern_data_resid = 0;
5127 ctsio->kern_rel_offset = 0;
5128 ctsio->kern_sg_entries = 0;
5129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5130 ctsio->be_move_done = ctl_config_move_done;
5131 ctl_datamove((union ctl_io *)ctsio);
5133 return (CTL_RETVAL_COMPLETE);
5137 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5139 mtx_lock(&lun->lun_lock);
5142 * According to SPC, it is not an error for an intiator to attempt
5143 * to release a reservation on a LUN that isn't reserved, or that
5144 * is reserved by another initiator. The reservation can only be
5145 * released, though, by the initiator who made it or by one of
5146 * several reset type events.
5148 if (lun->flags & CTL_LUN_RESERVED) {
5149 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5150 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5151 && (ctsio->io_hdr.nexus.targ_target.id ==
5152 lun->rsv_nexus.targ_target.id)) {
5153 lun->flags &= ~CTL_LUN_RESERVED;
5157 mtx_unlock(&lun->lun_lock);
5159 ctsio->scsi_status = SCSI_STATUS_OK;
5160 ctsio->io_hdr.status = CTL_SUCCESS;
5162 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5163 free(ctsio->kern_data_ptr, M_CTL);
5164 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5167 ctl_done((union ctl_io *)ctsio);
5168 return (CTL_RETVAL_COMPLETE);
5172 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5174 int extent, thirdparty, longid;
5175 int resv_id, length;
5176 uint64_t thirdparty_id;
5177 struct ctl_softc *ctl_softc;
5178 struct ctl_lun *lun;
5187 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5189 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5190 ctl_softc = control_softc;
5192 switch (ctsio->cdb[0]) {
5194 struct scsi_reserve_10 *cdb;
5196 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5198 if (cdb->byte2 & SR10_LONGID)
5201 thirdparty_id = cdb->thirdparty_id;
5203 resv_id = cdb->resv_id;
5204 length = scsi_2btoul(cdb->length);
5210 * XXX KDM right now, we only support LUN reservation. We don't
5211 * support 3rd party reservations, or extent reservations, which
5212 * might actually need the parameter list. If we've gotten this
5213 * far, we've got a LUN reservation. Anything else got kicked out
5214 * above. So, according to SPC, ignore the length.
5218 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5220 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5221 ctsio->kern_data_len = length;
5222 ctsio->kern_total_len = length;
5223 ctsio->kern_data_resid = 0;
5224 ctsio->kern_rel_offset = 0;
5225 ctsio->kern_sg_entries = 0;
5226 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5227 ctsio->be_move_done = ctl_config_move_done;
5228 ctl_datamove((union ctl_io *)ctsio);
5230 return (CTL_RETVAL_COMPLETE);
5234 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5236 mtx_lock(&lun->lun_lock);
5237 if (lun->flags & CTL_LUN_RESERVED) {
5238 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5239 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5240 || (ctsio->io_hdr.nexus.targ_target.id !=
5241 lun->rsv_nexus.targ_target.id)) {
5242 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5243 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5248 lun->flags |= CTL_LUN_RESERVED;
5249 lun->rsv_nexus = ctsio->io_hdr.nexus;
5251 ctsio->scsi_status = SCSI_STATUS_OK;
5252 ctsio->io_hdr.status = CTL_SUCCESS;
5255 mtx_unlock(&lun->lun_lock);
5257 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5258 free(ctsio->kern_data_ptr, M_CTL);
5259 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5262 ctl_done((union ctl_io *)ctsio);
5263 return (CTL_RETVAL_COMPLETE);
5267 ctl_start_stop(struct ctl_scsiio *ctsio)
5269 struct scsi_start_stop_unit *cdb;
5270 struct ctl_lun *lun;
5271 struct ctl_softc *ctl_softc;
5274 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5276 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5277 ctl_softc = control_softc;
5280 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5284 * We don't support the immediate bit on a stop unit. In order to
5285 * do that, we would need to code up a way to know that a stop is
5286 * pending, and hold off any new commands until it completes, one
5287 * way or another. Then we could accept or reject those commands
5288 * depending on its status. We would almost need to do the reverse
5289 * of what we do below for an immediate start -- return the copy of
5290 * the ctl_io to the FETD with status to send to the host (and to
5291 * free the copy!) and then free the original I/O once the stop
5292 * actually completes. That way, the OOA queue mechanism can work
5293 * to block commands that shouldn't proceed. Another alternative
5294 * would be to put the copy in the queue in place of the original,
5295 * and return the original back to the caller. That could be
5298 if ((cdb->byte2 & SSS_IMMED)
5299 && ((cdb->how & SSS_START) == 0)) {
5300 ctl_set_invalid_field(ctsio,
5306 ctl_done((union ctl_io *)ctsio);
5307 return (CTL_RETVAL_COMPLETE);
5310 if ((lun->flags & CTL_LUN_PR_RESERVED)
5311 && ((cdb->how & SSS_START)==0)) {
5314 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5315 if (!lun->per_res[residx].registered
5316 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5318 ctl_set_reservation_conflict(ctsio);
5319 ctl_done((union ctl_io *)ctsio);
5320 return (CTL_RETVAL_COMPLETE);
5325 * If there is no backend on this device, we can't start or stop
5326 * it. In theory we shouldn't get any start/stop commands in the
5327 * first place at this level if the LUN doesn't have a backend.
5328 * That should get stopped by the command decode code.
5330 if (lun->backend == NULL) {
5331 ctl_set_invalid_opcode(ctsio);
5332 ctl_done((union ctl_io *)ctsio);
5333 return (CTL_RETVAL_COMPLETE);
5337 * XXX KDM Copan-specific offline behavior.
5338 * Figure out a reasonable way to port this?
5341 mtx_lock(&lun->lun_lock);
5343 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5344 && (lun->flags & CTL_LUN_OFFLINE)) {
5346 * If the LUN is offline, and the on/offline bit isn't set,
5347 * reject the start or stop. Otherwise, let it through.
5349 mtx_unlock(&lun->lun_lock);
5350 ctl_set_lun_not_ready(ctsio);
5351 ctl_done((union ctl_io *)ctsio);
5353 mtx_unlock(&lun->lun_lock);
5354 #endif /* NEEDTOPORT */
5356 * This could be a start or a stop when we're online,
5357 * or a stop/offline or start/online. A start or stop when
5358 * we're offline is covered in the case above.
5361 * In the non-immediate case, we send the request to
5362 * the backend and return status to the user when
5365 * In the immediate case, we allocate a new ctl_io
5366 * to hold a copy of the request, and send that to
5367 * the backend. We then set good status on the
5368 * user's request and return it immediately.
5370 if (cdb->byte2 & SSS_IMMED) {
5371 union ctl_io *new_io;
5373 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5374 if (new_io == NULL) {
5375 ctl_set_busy(ctsio);
5376 ctl_done((union ctl_io *)ctsio);
5378 ctl_copy_io((union ctl_io *)ctsio,
5380 retval = lun->backend->config_write(new_io);
5381 ctl_set_success(ctsio);
5382 ctl_done((union ctl_io *)ctsio);
5385 retval = lun->backend->config_write(
5386 (union ctl_io *)ctsio);
5395 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5396 * we don't really do anything with the LBA and length fields if the user
5397 * passes them in. Instead we'll just flush out the cache for the entire
5401 ctl_sync_cache(struct ctl_scsiio *ctsio)
5403 struct ctl_lun *lun;
5404 struct ctl_softc *ctl_softc;
5405 uint64_t starting_lba;
5406 uint32_t block_count;
5409 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5411 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5412 ctl_softc = control_softc;
5415 switch (ctsio->cdb[0]) {
5416 case SYNCHRONIZE_CACHE: {
5417 struct scsi_sync_cache *cdb;
5418 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5420 starting_lba = scsi_4btoul(cdb->begin_lba);
5421 block_count = scsi_2btoul(cdb->lb_count);
5424 case SYNCHRONIZE_CACHE_16: {
5425 struct scsi_sync_cache_16 *cdb;
5426 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5428 starting_lba = scsi_8btou64(cdb->begin_lba);
5429 block_count = scsi_4btoul(cdb->lb_count);
5433 ctl_set_invalid_opcode(ctsio);
5434 ctl_done((union ctl_io *)ctsio);
5436 break; /* NOTREACHED */
5440 * We check the LBA and length, but don't do anything with them.
5441 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5442 * get flushed. This check will just help satisfy anyone who wants
5443 * to see an error for an out of range LBA.
5445 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5446 ctl_set_lba_out_of_range(ctsio);
5447 ctl_done((union ctl_io *)ctsio);
5452 * If this LUN has no backend, we can't flush the cache anyway.
5454 if (lun->backend == NULL) {
5455 ctl_set_invalid_opcode(ctsio);
5456 ctl_done((union ctl_io *)ctsio);
5461 * Check to see whether we're configured to send the SYNCHRONIZE
5462 * CACHE command directly to the back end.
5464 mtx_lock(&lun->lun_lock);
5465 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5466 && (++(lun->sync_count) >= lun->sync_interval)) {
5467 lun->sync_count = 0;
5468 mtx_unlock(&lun->lun_lock);
5469 retval = lun->backend->config_write((union ctl_io *)ctsio);
5471 mtx_unlock(&lun->lun_lock);
5472 ctl_set_success(ctsio);
5473 ctl_done((union ctl_io *)ctsio);
5482 ctl_format(struct ctl_scsiio *ctsio)
5484 struct scsi_format *cdb;
5485 struct ctl_lun *lun;
5486 struct ctl_softc *ctl_softc;
5487 int length, defect_list_len;
5489 CTL_DEBUG_PRINT(("ctl_format\n"));
5491 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5492 ctl_softc = control_softc;
5494 cdb = (struct scsi_format *)ctsio->cdb;
5497 if (cdb->byte2 & SF_FMTDATA) {
5498 if (cdb->byte2 & SF_LONGLIST)
5499 length = sizeof(struct scsi_format_header_long);
5501 length = sizeof(struct scsi_format_header_short);
5504 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5506 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5507 ctsio->kern_data_len = length;
5508 ctsio->kern_total_len = length;
5509 ctsio->kern_data_resid = 0;
5510 ctsio->kern_rel_offset = 0;
5511 ctsio->kern_sg_entries = 0;
5512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5513 ctsio->be_move_done = ctl_config_move_done;
5514 ctl_datamove((union ctl_io *)ctsio);
5516 return (CTL_RETVAL_COMPLETE);
5519 defect_list_len = 0;
5521 if (cdb->byte2 & SF_FMTDATA) {
5522 if (cdb->byte2 & SF_LONGLIST) {
5523 struct scsi_format_header_long *header;
5525 header = (struct scsi_format_header_long *)
5526 ctsio->kern_data_ptr;
5528 defect_list_len = scsi_4btoul(header->defect_list_len);
5529 if (defect_list_len != 0) {
5530 ctl_set_invalid_field(ctsio,
5539 struct scsi_format_header_short *header;
5541 header = (struct scsi_format_header_short *)
5542 ctsio->kern_data_ptr;
5544 defect_list_len = scsi_2btoul(header->defect_list_len);
5545 if (defect_list_len != 0) {
5546 ctl_set_invalid_field(ctsio,
5558 * The format command will clear out the "Medium format corrupted"
5559 * status if set by the configuration code. That status is really
5560 * just a way to notify the host that we have lost the media, and
5561 * get them to issue a command that will basically make them think
5562 * they're blowing away the media.
5564 mtx_lock(&lun->lun_lock);
5565 lun->flags &= ~CTL_LUN_INOPERABLE;
5566 mtx_unlock(&lun->lun_lock);
5568 ctsio->scsi_status = SCSI_STATUS_OK;
5569 ctsio->io_hdr.status = CTL_SUCCESS;
5572 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5573 free(ctsio->kern_data_ptr, M_CTL);
5574 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5577 ctl_done((union ctl_io *)ctsio);
5578 return (CTL_RETVAL_COMPLETE);
5582 ctl_read_buffer(struct ctl_scsiio *ctsio)
5584 struct scsi_read_buffer *cdb;
5585 struct ctl_lun *lun;
5586 int buffer_offset, len;
5587 static uint8_t descr[4];
5588 static uint8_t echo_descr[4] = { 0 };
5590 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5593 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5595 if (lun->flags & CTL_LUN_PR_RESERVED) {
5599 * XXX KDM need a lock here.
5601 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5602 if ((lun->res_type == SPR_TYPE_EX_AC
5603 && residx != lun->pr_res_idx)
5604 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5605 || lun->res_type == SPR_TYPE_EX_AC_AR)
5606 && !lun->per_res[residx].registered)) {
5607 ctl_set_reservation_conflict(ctsio);
5608 ctl_done((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5613 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5614 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5615 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5616 ctl_set_invalid_field(ctsio,
5622 ctl_done((union ctl_io *)ctsio);
5623 return (CTL_RETVAL_COMPLETE);
5626 len = scsi_3btoul(cdb->length);
5627 buffer_offset = scsi_3btoul(cdb->offset);
5629 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5630 ctl_set_invalid_field(ctsio,
5636 ctl_done((union ctl_io *)ctsio);
5637 return (CTL_RETVAL_COMPLETE);
5640 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5642 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5643 ctsio->kern_data_ptr = descr;
5644 len = min(len, sizeof(descr));
5645 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5646 ctsio->kern_data_ptr = echo_descr;
5647 len = min(len, sizeof(echo_descr));
5649 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5650 ctsio->kern_data_len = len;
5651 ctsio->kern_total_len = len;
5652 ctsio->kern_data_resid = 0;
5653 ctsio->kern_rel_offset = 0;
5654 ctsio->kern_sg_entries = 0;
5655 ctsio->be_move_done = ctl_config_move_done;
5656 ctl_datamove((union ctl_io *)ctsio);
5658 return (CTL_RETVAL_COMPLETE);
5662 ctl_write_buffer(struct ctl_scsiio *ctsio)
5664 struct scsi_write_buffer *cdb;
5665 struct ctl_lun *lun;
5666 int buffer_offset, len;
5668 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5670 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5671 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5673 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5674 ctl_set_invalid_field(ctsio,
5680 ctl_done((union ctl_io *)ctsio);
5681 return (CTL_RETVAL_COMPLETE);
5684 len = scsi_3btoul(cdb->length);
5685 buffer_offset = scsi_3btoul(cdb->offset);
5687 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5688 ctl_set_invalid_field(ctsio,
5694 ctl_done((union ctl_io *)ctsio);
5695 return (CTL_RETVAL_COMPLETE);
5699 * If we've got a kernel request that hasn't been malloced yet,
5700 * malloc it and tell the caller the data buffer is here.
5702 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5703 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5704 ctsio->kern_data_len = len;
5705 ctsio->kern_total_len = len;
5706 ctsio->kern_data_resid = 0;
5707 ctsio->kern_rel_offset = 0;
5708 ctsio->kern_sg_entries = 0;
5709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5710 ctsio->be_move_done = ctl_config_move_done;
5711 ctl_datamove((union ctl_io *)ctsio);
5713 return (CTL_RETVAL_COMPLETE);
5716 ctl_done((union ctl_io *)ctsio);
5718 return (CTL_RETVAL_COMPLETE);
5722 ctl_write_same(struct ctl_scsiio *ctsio)
5724 struct ctl_lun *lun;
5725 struct ctl_lba_len_flags *lbalen;
5727 uint32_t num_blocks;
5731 retval = CTL_RETVAL_COMPLETE;
5733 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5735 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5737 switch (ctsio->cdb[0]) {
5738 case WRITE_SAME_10: {
5739 struct scsi_write_same_10 *cdb;
5741 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5743 lba = scsi_4btoul(cdb->addr);
5744 num_blocks = scsi_2btoul(cdb->length);
5748 case WRITE_SAME_16: {
5749 struct scsi_write_same_16 *cdb;
5751 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5753 lba = scsi_8btou64(cdb->addr);
5754 num_blocks = scsi_4btoul(cdb->length);
5760 * We got a command we don't support. This shouldn't
5761 * happen, commands should be filtered out above us.
5763 ctl_set_invalid_opcode(ctsio);
5764 ctl_done((union ctl_io *)ctsio);
5766 return (CTL_RETVAL_COMPLETE);
5767 break; /* NOTREACHED */
5771 * The first check is to make sure we're in bounds, the second
5772 * check is to catch wrap-around problems. If the lba + num blocks
5773 * is less than the lba, then we've wrapped around and the block
5774 * range is invalid anyway.
5776 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5777 || ((lba + num_blocks) < lba)) {
5778 ctl_set_lba_out_of_range(ctsio);
5779 ctl_done((union ctl_io *)ctsio);
5780 return (CTL_RETVAL_COMPLETE);
5783 /* Zero number of blocks means "to the last logical block" */
5784 if (num_blocks == 0) {
5785 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5786 ctl_set_invalid_field(ctsio,
5792 ctl_done((union ctl_io *)ctsio);
5793 return (CTL_RETVAL_COMPLETE);
5795 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5798 len = lun->be_lun->blocksize;
5801 * If we've got a kernel request that hasn't been malloced yet,
5802 * malloc it and tell the caller the data buffer is here.
5804 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5805 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5806 ctsio->kern_data_len = len;
5807 ctsio->kern_total_len = len;
5808 ctsio->kern_data_resid = 0;
5809 ctsio->kern_rel_offset = 0;
5810 ctsio->kern_sg_entries = 0;
5811 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5812 ctsio->be_move_done = ctl_config_move_done;
5813 ctl_datamove((union ctl_io *)ctsio);
5815 return (CTL_RETVAL_COMPLETE);
5818 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5820 lbalen->len = num_blocks;
5821 lbalen->flags = byte2;
5822 retval = lun->backend->config_write((union ctl_io *)ctsio);
5828 ctl_unmap(struct ctl_scsiio *ctsio)
5830 struct ctl_lun *lun;
5831 struct scsi_unmap *cdb;
5832 struct ctl_ptr_len_flags *ptrlen;
5833 struct scsi_unmap_header *hdr;
5834 struct scsi_unmap_desc *buf, *end;
5836 uint32_t num_blocks;
5840 retval = CTL_RETVAL_COMPLETE;
5842 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5845 cdb = (struct scsi_unmap *)ctsio->cdb;
5847 len = scsi_2btoul(cdb->length);
5851 * If we've got a kernel request that hasn't been malloced yet,
5852 * malloc it and tell the caller the data buffer is here.
5854 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5855 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5856 ctsio->kern_data_len = len;
5857 ctsio->kern_total_len = len;
5858 ctsio->kern_data_resid = 0;
5859 ctsio->kern_rel_offset = 0;
5860 ctsio->kern_sg_entries = 0;
5861 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5862 ctsio->be_move_done = ctl_config_move_done;
5863 ctl_datamove((union ctl_io *)ctsio);
5865 return (CTL_RETVAL_COMPLETE);
5868 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5869 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5870 if (len < sizeof (*hdr) ||
5871 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5872 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5873 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5874 ctl_set_invalid_field(ctsio,
5880 ctl_done((union ctl_io *)ctsio);
5881 return (CTL_RETVAL_COMPLETE);
5883 len = scsi_2btoul(hdr->desc_length);
5884 buf = (struct scsi_unmap_desc *)(hdr + 1);
5885 end = buf + len / sizeof(*buf);
5887 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5888 ptrlen->ptr = (void *)buf;
5890 ptrlen->flags = byte2;
5892 for (; buf < end; buf++) {
5893 lba = scsi_8btou64(buf->lba);
5894 num_blocks = scsi_4btoul(buf->length);
5895 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5896 || ((lba + num_blocks) < lba)) {
5897 ctl_set_lba_out_of_range(ctsio);
5898 ctl_done((union ctl_io *)ctsio);
5899 return (CTL_RETVAL_COMPLETE);
5903 retval = lun->backend->config_write((union ctl_io *)ctsio);
5909 * Note that this function currently doesn't actually do anything inside
5910 * CTL to enforce things if the DQue bit is turned on.
5912 * Also note that this function can't be used in the default case, because
5913 * the DQue bit isn't set in the changeable mask for the control mode page
5914 * anyway. This is just here as an example for how to implement a page
5915 * handler, and a placeholder in case we want to allow the user to turn
5916 * tagged queueing on and off.
5918 * The D_SENSE bit handling is functional, however, and will turn
5919 * descriptor sense on and off for a given LUN.
5922 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5923 struct ctl_page_index *page_index, uint8_t *page_ptr)
5925 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5926 struct ctl_lun *lun;
5927 struct ctl_softc *softc;
5931 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5932 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5935 user_cp = (struct scsi_control_page *)page_ptr;
5936 current_cp = (struct scsi_control_page *)
5937 (page_index->page_data + (page_index->page_len *
5939 saved_cp = (struct scsi_control_page *)
5940 (page_index->page_data + (page_index->page_len *
5943 softc = control_softc;
5945 mtx_lock(&lun->lun_lock);
5946 if (((current_cp->rlec & SCP_DSENSE) == 0)
5947 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5949 * Descriptor sense is currently turned off and the user
5950 * wants to turn it on.
5952 current_cp->rlec |= SCP_DSENSE;
5953 saved_cp->rlec |= SCP_DSENSE;
5954 lun->flags |= CTL_LUN_SENSE_DESC;
5956 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5957 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5959 * Descriptor sense is currently turned on, and the user
5960 * wants to turn it off.
5962 current_cp->rlec &= ~SCP_DSENSE;
5963 saved_cp->rlec &= ~SCP_DSENSE;
5964 lun->flags &= ~CTL_LUN_SENSE_DESC;
5967 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5968 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5970 csevent_log(CSC_CTL | CSC_SHELF_SW |
5972 csevent_LogType_Trace,
5973 csevent_Severity_Information,
5974 csevent_AlertLevel_Green,
5975 csevent_FRU_Firmware,
5976 csevent_FRU_Unknown,
5977 "Received untagged to untagged transition");
5978 #endif /* NEEDTOPORT */
5981 csevent_log(CSC_CTL | CSC_SHELF_SW |
5983 csevent_LogType_ConfigChange,
5984 csevent_Severity_Information,
5985 csevent_AlertLevel_Green,
5986 csevent_FRU_Firmware,
5987 csevent_FRU_Unknown,
5988 "Received untagged to tagged "
5989 "queueing transition");
5990 #endif /* NEEDTOPORT */
5992 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5993 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5997 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5999 csevent_log(CSC_CTL | CSC_SHELF_SW |
6001 csevent_LogType_ConfigChange,
6002 csevent_Severity_Warning,
6003 csevent_AlertLevel_Yellow,
6004 csevent_FRU_Firmware,
6005 csevent_FRU_Unknown,
6006 "Received tagged queueing to untagged "
6008 #endif /* NEEDTOPORT */
6010 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6011 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6015 csevent_log(CSC_CTL | CSC_SHELF_SW |
6017 csevent_LogType_Trace,
6018 csevent_Severity_Information,
6019 csevent_AlertLevel_Green,
6020 csevent_FRU_Firmware,
6021 csevent_FRU_Unknown,
6022 "Received tagged queueing to tagged "
6023 "queueing transition");
6024 #endif /* NEEDTOPORT */
6030 * Let other initiators know that the mode
6031 * parameters for this LUN have changed.
6033 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6037 lun->pending_sense[i].ua_pending |=
6041 mtx_unlock(&lun->lun_lock);
6047 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6048 struct ctl_page_index *page_index, uint8_t *page_ptr)
6054 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6055 struct ctl_page_index *page_index, int pc)
6057 struct copan_power_subpage *page;
6059 page = (struct copan_power_subpage *)page_index->page_data +
6060 (page_index->page_len * pc);
6063 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6065 * We don't update the changable bits for this page.
6068 case SMS_PAGE_CTRL_CURRENT >> 6:
6069 case SMS_PAGE_CTRL_DEFAULT >> 6:
6070 case SMS_PAGE_CTRL_SAVED >> 6:
6072 ctl_update_power_subpage(page);
6077 EPRINT(0, "Invalid PC %d!!", pc);
6086 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6087 struct ctl_page_index *page_index, uint8_t *page_ptr)
6089 struct copan_aps_subpage *user_sp;
6090 struct copan_aps_subpage *current_sp;
6091 union ctl_modepage_info *modepage_info;
6092 struct ctl_softc *softc;
6093 struct ctl_lun *lun;
6096 retval = CTL_RETVAL_COMPLETE;
6097 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6098 (page_index->page_len * CTL_PAGE_CURRENT));
6099 softc = control_softc;
6100 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6102 user_sp = (struct copan_aps_subpage *)page_ptr;
6104 modepage_info = (union ctl_modepage_info *)
6105 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6107 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6108 modepage_info->header.subpage = page_index->subpage;
6109 modepage_info->aps.lock_active = user_sp->lock_active;
6111 mtx_lock(&softc->ctl_lock);
6114 * If there is a request to lock the LUN and another LUN is locked
6115 * this is an error. If the requested LUN is already locked ignore
6116 * the request. If no LUN is locked attempt to lock it.
6117 * if there is a request to unlock the LUN and the LUN is currently
6118 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6119 * if another LUN is locked or no LUN is locked.
6121 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6122 if (softc->aps_locked_lun == lun->lun) {
6124 * This LUN is already locked, so we're done.
6126 retval = CTL_RETVAL_COMPLETE;
6127 } else if (softc->aps_locked_lun == 0) {
6129 * No one has the lock, pass the request to the
6132 retval = lun->backend->config_write(
6133 (union ctl_io *)ctsio);
6136 * Someone else has the lock, throw out the request.
6138 ctl_set_already_locked(ctsio);
6139 free(ctsio->kern_data_ptr, M_CTL);
6140 ctl_done((union ctl_io *)ctsio);
6143 * Set the return value so that ctl_do_mode_select()
6144 * won't try to complete the command. We already
6145 * completed it here.
6147 retval = CTL_RETVAL_ERROR;
6149 } else if (softc->aps_locked_lun == lun->lun) {
6151 * This LUN is locked, so pass the unlock request to the
6154 retval = lun->backend->config_write((union ctl_io *)ctsio);
6156 mtx_unlock(&softc->ctl_lock);
6162 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6163 struct ctl_page_index *page_index,
6169 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6174 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6175 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6176 printf("page data:");
6178 printf(" %.2x",page_ptr[i]);
6184 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6185 struct ctl_page_index *page_index,
6188 struct copan_debugconf_subpage *page;
6190 page = (struct copan_debugconf_subpage *)page_index->page_data +
6191 (page_index->page_len * pc);
6194 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6195 case SMS_PAGE_CTRL_DEFAULT >> 6:
6196 case SMS_PAGE_CTRL_SAVED >> 6:
6198 * We don't update the changable or default bits for this page.
6201 case SMS_PAGE_CTRL_CURRENT >> 6:
6202 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6203 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6207 EPRINT(0, "Invalid PC %d!!", pc);
6208 #endif /* NEEDTOPORT */
6216 ctl_do_mode_select(union ctl_io *io)
6218 struct scsi_mode_page_header *page_header;
6219 struct ctl_page_index *page_index;
6220 struct ctl_scsiio *ctsio;
6221 int control_dev, page_len;
6222 int page_len_offset, page_len_size;
6223 union ctl_modepage_info *modepage_info;
6224 struct ctl_lun *lun;
6225 int *len_left, *len_used;
6228 ctsio = &io->scsiio;
6231 retval = CTL_RETVAL_COMPLETE;
6233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6235 if (lun->be_lun->lun_type != T_DIRECT)
6240 modepage_info = (union ctl_modepage_info *)
6241 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6242 len_left = &modepage_info->header.len_left;
6243 len_used = &modepage_info->header.len_used;
6247 page_header = (struct scsi_mode_page_header *)
6248 (ctsio->kern_data_ptr + *len_used);
6250 if (*len_left == 0) {
6251 free(ctsio->kern_data_ptr, M_CTL);
6252 ctl_set_success(ctsio);
6253 ctl_done((union ctl_io *)ctsio);
6254 return (CTL_RETVAL_COMPLETE);
6255 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6257 free(ctsio->kern_data_ptr, M_CTL);
6258 ctl_set_param_len_error(ctsio);
6259 ctl_done((union ctl_io *)ctsio);
6260 return (CTL_RETVAL_COMPLETE);
6262 } else if ((page_header->page_code & SMPH_SPF)
6263 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6265 free(ctsio->kern_data_ptr, M_CTL);
6266 ctl_set_param_len_error(ctsio);
6267 ctl_done((union ctl_io *)ctsio);
6268 return (CTL_RETVAL_COMPLETE);
6273 * XXX KDM should we do something with the block descriptor?
6275 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6277 if ((control_dev != 0)
6278 && (lun->mode_pages.index[i].page_flags &
6279 CTL_PAGE_FLAG_DISK_ONLY))
6282 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6283 (page_header->page_code & SMPH_PC_MASK))
6287 * If neither page has a subpage code, then we've got a
6290 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6291 && ((page_header->page_code & SMPH_SPF) == 0)) {
6292 page_index = &lun->mode_pages.index[i];
6293 page_len = page_header->page_length;
6298 * If both pages have subpages, then the subpage numbers
6301 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6302 && (page_header->page_code & SMPH_SPF)) {
6303 struct scsi_mode_page_header_sp *sph;
6305 sph = (struct scsi_mode_page_header_sp *)page_header;
6307 if (lun->mode_pages.index[i].subpage ==
6309 page_index = &lun->mode_pages.index[i];
6310 page_len = scsi_2btoul(sph->page_length);
6317 * If we couldn't find the page, or if we don't have a mode select
6318 * handler for it, send back an error to the user.
6320 if ((page_index == NULL)
6321 || (page_index->select_handler == NULL)) {
6322 ctl_set_invalid_field(ctsio,
6325 /*field*/ *len_used,
6328 free(ctsio->kern_data_ptr, M_CTL);
6329 ctl_done((union ctl_io *)ctsio);
6330 return (CTL_RETVAL_COMPLETE);
6333 if (page_index->page_code & SMPH_SPF) {
6334 page_len_offset = 2;
6338 page_len_offset = 1;
6342 * If the length the initiator gives us isn't the one we specify in
6343 * the mode page header, or if they didn't specify enough data in
6344 * the CDB to avoid truncating this page, kick out the request.
6346 if ((page_len != (page_index->page_len - page_len_offset -
6348 || (*len_left < page_index->page_len)) {
6351 ctl_set_invalid_field(ctsio,
6354 /*field*/ *len_used + page_len_offset,
6357 free(ctsio->kern_data_ptr, M_CTL);
6358 ctl_done((union ctl_io *)ctsio);
6359 return (CTL_RETVAL_COMPLETE);
6363 * Run through the mode page, checking to make sure that the bits
6364 * the user changed are actually legal for him to change.
6366 for (i = 0; i < page_index->page_len; i++) {
6367 uint8_t *user_byte, *change_mask, *current_byte;
6371 user_byte = (uint8_t *)page_header + i;
6372 change_mask = page_index->page_data +
6373 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6374 current_byte = page_index->page_data +
6375 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6378 * Check to see whether the user set any bits in this byte
6379 * that he is not allowed to set.
6381 if ((*user_byte & ~(*change_mask)) ==
6382 (*current_byte & ~(*change_mask)))
6386 * Go through bit by bit to determine which one is illegal.
6389 for (j = 7; j >= 0; j--) {
6390 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6391 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6396 ctl_set_invalid_field(ctsio,
6399 /*field*/ *len_used + i,
6402 free(ctsio->kern_data_ptr, M_CTL);
6403 ctl_done((union ctl_io *)ctsio);
6404 return (CTL_RETVAL_COMPLETE);
6408 * Decrement these before we call the page handler, since we may
6409 * end up getting called back one way or another before the handler
6410 * returns to this context.
6412 *len_left -= page_index->page_len;
6413 *len_used += page_index->page_len;
6415 retval = page_index->select_handler(ctsio, page_index,
6416 (uint8_t *)page_header);
6419 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6420 * wait until this queued command completes to finish processing
6421 * the mode page. If it returns anything other than
6422 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6423 * already set the sense information, freed the data pointer, and
6424 * completed the io for us.
6426 if (retval != CTL_RETVAL_COMPLETE)
6427 goto bailout_no_done;
6430 * If the initiator sent us more than one page, parse the next one.
6435 ctl_set_success(ctsio);
6436 free(ctsio->kern_data_ptr, M_CTL);
6437 ctl_done((union ctl_io *)ctsio);
6441 return (CTL_RETVAL_COMPLETE);
6446 ctl_mode_select(struct ctl_scsiio *ctsio)
6448 int param_len, pf, sp;
6449 int header_size, bd_len;
6450 int len_left, len_used;
6451 struct ctl_page_index *page_index;
6452 struct ctl_lun *lun;
6453 int control_dev, page_len;
6454 union ctl_modepage_info *modepage_info;
6466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6468 if (lun->be_lun->lun_type != T_DIRECT)
6473 switch (ctsio->cdb[0]) {
6474 case MODE_SELECT_6: {
6475 struct scsi_mode_select_6 *cdb;
6477 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6479 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6480 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6482 param_len = cdb->length;
6483 header_size = sizeof(struct scsi_mode_header_6);
6486 case MODE_SELECT_10: {
6487 struct scsi_mode_select_10 *cdb;
6489 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6491 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6492 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6494 param_len = scsi_2btoul(cdb->length);
6495 header_size = sizeof(struct scsi_mode_header_10);
6499 ctl_set_invalid_opcode(ctsio);
6500 ctl_done((union ctl_io *)ctsio);
6501 return (CTL_RETVAL_COMPLETE);
6502 break; /* NOTREACHED */
6507 * "A parameter list length of zero indicates that the Data-Out Buffer
6508 * shall be empty. This condition shall not be considered as an error."
6510 if (param_len == 0) {
6511 ctl_set_success(ctsio);
6512 ctl_done((union ctl_io *)ctsio);
6513 return (CTL_RETVAL_COMPLETE);
6517 * Since we'll hit this the first time through, prior to
6518 * allocation, we don't need to free a data buffer here.
6520 if (param_len < header_size) {
6521 ctl_set_param_len_error(ctsio);
6522 ctl_done((union ctl_io *)ctsio);
6523 return (CTL_RETVAL_COMPLETE);
6527 * Allocate the data buffer and grab the user's data. In theory,
6528 * we shouldn't have to sanity check the parameter list length here
6529 * because the maximum size is 64K. We should be able to malloc
6530 * that much without too many problems.
6532 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6533 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6534 ctsio->kern_data_len = param_len;
6535 ctsio->kern_total_len = param_len;
6536 ctsio->kern_data_resid = 0;
6537 ctsio->kern_rel_offset = 0;
6538 ctsio->kern_sg_entries = 0;
6539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6540 ctsio->be_move_done = ctl_config_move_done;
6541 ctl_datamove((union ctl_io *)ctsio);
6543 return (CTL_RETVAL_COMPLETE);
6546 switch (ctsio->cdb[0]) {
6547 case MODE_SELECT_6: {
6548 struct scsi_mode_header_6 *mh6;
6550 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6551 bd_len = mh6->blk_desc_len;
6554 case MODE_SELECT_10: {
6555 struct scsi_mode_header_10 *mh10;
6557 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6558 bd_len = scsi_2btoul(mh10->blk_desc_len);
6562 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6566 if (param_len < (header_size + bd_len)) {
6567 free(ctsio->kern_data_ptr, M_CTL);
6568 ctl_set_param_len_error(ctsio);
6569 ctl_done((union ctl_io *)ctsio);
6570 return (CTL_RETVAL_COMPLETE);
6574 * Set the IO_CONT flag, so that if this I/O gets passed to
6575 * ctl_config_write_done(), it'll get passed back to
6576 * ctl_do_mode_select() for further processing, or completion if
6579 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6580 ctsio->io_cont = ctl_do_mode_select;
6582 modepage_info = (union ctl_modepage_info *)
6583 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6585 memset(modepage_info, 0, sizeof(*modepage_info));
6587 len_left = param_len - header_size - bd_len;
6588 len_used = header_size + bd_len;
6590 modepage_info->header.len_left = len_left;
6591 modepage_info->header.len_used = len_used;
6593 return (ctl_do_mode_select((union ctl_io *)ctsio));
6597 ctl_mode_sense(struct ctl_scsiio *ctsio)
6599 struct ctl_lun *lun;
6600 int pc, page_code, dbd, llba, subpage;
6601 int alloc_len, page_len, header_len, total_len;
6602 struct scsi_mode_block_descr *block_desc;
6603 struct ctl_page_index *page_index;
6611 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6615 if (lun->be_lun->lun_type != T_DIRECT)
6620 if (lun->flags & CTL_LUN_PR_RESERVED) {
6624 * XXX KDM need a lock here.
6626 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6627 if ((lun->res_type == SPR_TYPE_EX_AC
6628 && residx != lun->pr_res_idx)
6629 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6630 || lun->res_type == SPR_TYPE_EX_AC_AR)
6631 && !lun->per_res[residx].registered)) {
6632 ctl_set_reservation_conflict(ctsio);
6633 ctl_done((union ctl_io *)ctsio);
6634 return (CTL_RETVAL_COMPLETE);
6638 switch (ctsio->cdb[0]) {
6639 case MODE_SENSE_6: {
6640 struct scsi_mode_sense_6 *cdb;
6642 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6644 header_len = sizeof(struct scsi_mode_hdr_6);
6645 if (cdb->byte2 & SMS_DBD)
6648 header_len += sizeof(struct scsi_mode_block_descr);
6650 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6651 page_code = cdb->page & SMS_PAGE_CODE;
6652 subpage = cdb->subpage;
6653 alloc_len = cdb->length;
6656 case MODE_SENSE_10: {
6657 struct scsi_mode_sense_10 *cdb;
6659 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6661 header_len = sizeof(struct scsi_mode_hdr_10);
6663 if (cdb->byte2 & SMS_DBD)
6666 header_len += sizeof(struct scsi_mode_block_descr);
6667 if (cdb->byte2 & SMS10_LLBAA)
6669 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6670 page_code = cdb->page & SMS_PAGE_CODE;
6671 subpage = cdb->subpage;
6672 alloc_len = scsi_2btoul(cdb->length);
6676 ctl_set_invalid_opcode(ctsio);
6677 ctl_done((union ctl_io *)ctsio);
6678 return (CTL_RETVAL_COMPLETE);
6679 break; /* NOTREACHED */
6683 * We have to make a first pass through to calculate the size of
6684 * the pages that match the user's query. Then we allocate enough
6685 * memory to hold it, and actually copy the data into the buffer.
6687 switch (page_code) {
6688 case SMS_ALL_PAGES_PAGE: {
6694 * At the moment, values other than 0 and 0xff here are
6695 * reserved according to SPC-3.
6697 if ((subpage != SMS_SUBPAGE_PAGE_0)
6698 && (subpage != SMS_SUBPAGE_ALL)) {
6699 ctl_set_invalid_field(ctsio,
6705 ctl_done((union ctl_io *)ctsio);
6706 return (CTL_RETVAL_COMPLETE);
6709 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6710 if ((control_dev != 0)
6711 && (lun->mode_pages.index[i].page_flags &
6712 CTL_PAGE_FLAG_DISK_ONLY))
6716 * We don't use this subpage if the user didn't
6717 * request all subpages.
6719 if ((lun->mode_pages.index[i].subpage != 0)
6720 && (subpage == SMS_SUBPAGE_PAGE_0))
6724 printf("found page %#x len %d\n",
6725 lun->mode_pages.index[i].page_code &
6727 lun->mode_pages.index[i].page_len);
6729 page_len += lun->mode_pages.index[i].page_len;
6738 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6739 /* Look for the right page code */
6740 if ((lun->mode_pages.index[i].page_code &
6741 SMPH_PC_MASK) != page_code)
6744 /* Look for the right subpage or the subpage wildcard*/
6745 if ((lun->mode_pages.index[i].subpage != subpage)
6746 && (subpage != SMS_SUBPAGE_ALL))
6749 /* Make sure the page is supported for this dev type */
6750 if ((control_dev != 0)
6751 && (lun->mode_pages.index[i].page_flags &
6752 CTL_PAGE_FLAG_DISK_ONLY))
6756 printf("found page %#x len %d\n",
6757 lun->mode_pages.index[i].page_code &
6759 lun->mode_pages.index[i].page_len);
6762 page_len += lun->mode_pages.index[i].page_len;
6765 if (page_len == 0) {
6766 ctl_set_invalid_field(ctsio,
6772 ctl_done((union ctl_io *)ctsio);
6773 return (CTL_RETVAL_COMPLETE);
6779 total_len = header_len + page_len;
6781 printf("header_len = %d, page_len = %d, total_len = %d\n",
6782 header_len, page_len, total_len);
6785 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6786 ctsio->kern_sg_entries = 0;
6787 ctsio->kern_data_resid = 0;
6788 ctsio->kern_rel_offset = 0;
6789 if (total_len < alloc_len) {
6790 ctsio->residual = alloc_len - total_len;
6791 ctsio->kern_data_len = total_len;
6792 ctsio->kern_total_len = total_len;
6794 ctsio->residual = 0;
6795 ctsio->kern_data_len = alloc_len;
6796 ctsio->kern_total_len = alloc_len;
6799 switch (ctsio->cdb[0]) {
6800 case MODE_SENSE_6: {
6801 struct scsi_mode_hdr_6 *header;
6803 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6805 header->datalen = ctl_min(total_len - 1, 254);
6808 header->block_descr_len = 0;
6810 header->block_descr_len =
6811 sizeof(struct scsi_mode_block_descr);
6812 block_desc = (struct scsi_mode_block_descr *)&header[1];
6815 case MODE_SENSE_10: {
6816 struct scsi_mode_hdr_10 *header;
6819 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6821 datalen = ctl_min(total_len - 2, 65533);
6822 scsi_ulto2b(datalen, header->datalen);
6824 scsi_ulto2b(0, header->block_descr_len);
6826 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6827 header->block_descr_len);
6828 block_desc = (struct scsi_mode_block_descr *)&header[1];
6832 panic("invalid CDB type %#x", ctsio->cdb[0]);
6833 break; /* NOTREACHED */
6837 * If we've got a disk, use its blocksize in the block
6838 * descriptor. Otherwise, just set it to 0.
6841 if (control_dev != 0)
6842 scsi_ulto3b(lun->be_lun->blocksize,
6843 block_desc->block_len);
6845 scsi_ulto3b(0, block_desc->block_len);
6848 switch (page_code) {
6849 case SMS_ALL_PAGES_PAGE: {
6852 data_used = header_len;
6853 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6854 struct ctl_page_index *page_index;
6856 page_index = &lun->mode_pages.index[i];
6858 if ((control_dev != 0)
6859 && (page_index->page_flags &
6860 CTL_PAGE_FLAG_DISK_ONLY))
6864 * We don't use this subpage if the user didn't
6865 * request all subpages. We already checked (above)
6866 * to make sure the user only specified a subpage
6867 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6869 if ((page_index->subpage != 0)
6870 && (subpage == SMS_SUBPAGE_PAGE_0))
6874 * Call the handler, if it exists, to update the
6875 * page to the latest values.
6877 if (page_index->sense_handler != NULL)
6878 page_index->sense_handler(ctsio, page_index,pc);
6880 memcpy(ctsio->kern_data_ptr + data_used,
6881 page_index->page_data +
6882 (page_index->page_len * pc),
6883 page_index->page_len);
6884 data_used += page_index->page_len;
6891 data_used = header_len;
6893 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6894 struct ctl_page_index *page_index;
6896 page_index = &lun->mode_pages.index[i];
6898 /* Look for the right page code */
6899 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6902 /* Look for the right subpage or the subpage wildcard*/
6903 if ((page_index->subpage != subpage)
6904 && (subpage != SMS_SUBPAGE_ALL))
6907 /* Make sure the page is supported for this dev type */
6908 if ((control_dev != 0)
6909 && (page_index->page_flags &
6910 CTL_PAGE_FLAG_DISK_ONLY))
6914 * Call the handler, if it exists, to update the
6915 * page to the latest values.
6917 if (page_index->sense_handler != NULL)
6918 page_index->sense_handler(ctsio, page_index,pc);
6920 memcpy(ctsio->kern_data_ptr + data_used,
6921 page_index->page_data +
6922 (page_index->page_len * pc),
6923 page_index->page_len);
6924 data_used += page_index->page_len;
6930 ctsio->scsi_status = SCSI_STATUS_OK;
6932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6933 ctsio->be_move_done = ctl_config_move_done;
6934 ctl_datamove((union ctl_io *)ctsio);
6936 return (CTL_RETVAL_COMPLETE);
6940 ctl_read_capacity(struct ctl_scsiio *ctsio)
6942 struct scsi_read_capacity *cdb;
6943 struct scsi_read_capacity_data *data;
6944 struct ctl_lun *lun;
6947 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6949 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6951 lba = scsi_4btoul(cdb->addr);
6952 if (((cdb->pmi & SRC_PMI) == 0)
6954 ctl_set_invalid_field(/*ctsio*/ ctsio,
6960 ctl_done((union ctl_io *)ctsio);
6961 return (CTL_RETVAL_COMPLETE);
6964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6966 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6967 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6968 ctsio->residual = 0;
6969 ctsio->kern_data_len = sizeof(*data);
6970 ctsio->kern_total_len = sizeof(*data);
6971 ctsio->kern_data_resid = 0;
6972 ctsio->kern_rel_offset = 0;
6973 ctsio->kern_sg_entries = 0;
6976 * If the maximum LBA is greater than 0xfffffffe, the user must
6977 * issue a SERVICE ACTION IN (16) command, with the read capacity
6978 * serivce action set.
6980 if (lun->be_lun->maxlba > 0xfffffffe)
6981 scsi_ulto4b(0xffffffff, data->addr);
6983 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6986 * XXX KDM this may not be 512 bytes...
6988 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6990 ctsio->scsi_status = SCSI_STATUS_OK;
6992 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6993 ctsio->be_move_done = ctl_config_move_done;
6994 ctl_datamove((union ctl_io *)ctsio);
6996 return (CTL_RETVAL_COMPLETE);
7000 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7002 struct scsi_read_capacity_16 *cdb;
7003 struct scsi_read_capacity_data_long *data;
7004 struct ctl_lun *lun;
7008 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7010 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7012 alloc_len = scsi_4btoul(cdb->alloc_len);
7013 lba = scsi_8btou64(cdb->addr);
7015 if ((cdb->reladr & SRC16_PMI)
7017 ctl_set_invalid_field(/*ctsio*/ ctsio,
7023 ctl_done((union ctl_io *)ctsio);
7024 return (CTL_RETVAL_COMPLETE);
7027 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7029 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7030 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7032 if (sizeof(*data) < alloc_len) {
7033 ctsio->residual = alloc_len - sizeof(*data);
7034 ctsio->kern_data_len = sizeof(*data);
7035 ctsio->kern_total_len = sizeof(*data);
7037 ctsio->residual = 0;
7038 ctsio->kern_data_len = alloc_len;
7039 ctsio->kern_total_len = alloc_len;
7041 ctsio->kern_data_resid = 0;
7042 ctsio->kern_rel_offset = 0;
7043 ctsio->kern_sg_entries = 0;
7045 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7046 /* XXX KDM this may not be 512 bytes... */
7047 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7048 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7049 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7050 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7051 data->lalba_lbp[0] |= SRC16_LBPME;
7053 ctsio->scsi_status = SCSI_STATUS_OK;
7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7056 ctsio->be_move_done = ctl_config_move_done;
7057 ctl_datamove((union ctl_io *)ctsio);
7059 return (CTL_RETVAL_COMPLETE);
7063 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7065 struct scsi_maintenance_in *cdb;
7067 int alloc_len, total_len = 0;
7068 int num_target_port_groups, single;
7069 struct ctl_lun *lun;
7070 struct ctl_softc *softc;
7071 struct scsi_target_group_data *rtg_ptr;
7072 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
7073 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
7074 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
7076 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7078 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7079 softc = control_softc;
7080 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7082 retval = CTL_RETVAL_COMPLETE;
7084 single = ctl_is_single;
7086 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
7088 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7090 total_len = sizeof(struct scsi_target_group_data) +
7091 sizeof(struct scsi_target_port_group_descriptor) *
7092 num_target_port_groups +
7093 sizeof(struct scsi_target_port_descriptor) *
7094 NUM_PORTS_PER_GRP * num_target_port_groups;
7096 alloc_len = scsi_4btoul(cdb->length);
7098 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7100 ctsio->kern_sg_entries = 0;
7102 if (total_len < alloc_len) {
7103 ctsio->residual = alloc_len - total_len;
7104 ctsio->kern_data_len = total_len;
7105 ctsio->kern_total_len = total_len;
7107 ctsio->residual = 0;
7108 ctsio->kern_data_len = alloc_len;
7109 ctsio->kern_total_len = alloc_len;
7111 ctsio->kern_data_resid = 0;
7112 ctsio->kern_rel_offset = 0;
7114 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7116 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7117 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7118 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7119 &tp_desc_ptr1_1->desc_list[0];
7122 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7123 &tp_desc_ptr1_2->desc_list[0];
7124 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7125 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7126 &tp_desc_ptr2_1->desc_list[0];
7128 tpg_desc_ptr2 = NULL;
7129 tp_desc_ptr2_1 = NULL;
7130 tp_desc_ptr2_2 = NULL;
7133 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7135 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7136 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7137 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7138 tpg_desc_ptr2->pref_state =
7139 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7141 tpg_desc_ptr1->pref_state =
7142 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7143 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7146 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7147 tpg_desc_ptr1->pref_state =
7148 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7149 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7151 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7152 tpg_desc_ptr2->pref_state =
7153 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7157 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7159 tpg_desc_ptr1->support = 0;
7160 tpg_desc_ptr1->target_port_group[1] = 1;
7161 tpg_desc_ptr1->status = TPG_IMPLICIT;
7162 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7165 tpg_desc_ptr2->support = 0;
7166 tpg_desc_ptr2->target_port_group[1] = 2;
7167 tpg_desc_ptr2->status = TPG_IMPLICIT;
7168 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7170 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7171 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7173 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7174 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7176 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7177 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7178 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7180 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7181 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7185 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7186 ctsio->be_move_done = ctl_config_move_done;
7188 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7189 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7190 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7191 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7192 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7194 ctl_datamove((union ctl_io *)ctsio);
7199 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7201 struct ctl_lun *lun;
7202 struct scsi_report_supported_opcodes *cdb;
7203 const struct ctl_cmd_entry *entry, *sentry;
7204 struct scsi_report_supported_opcodes_all *all;
7205 struct scsi_report_supported_opcodes_descr *descr;
7206 struct scsi_report_supported_opcodes_one *one;
7208 int alloc_len, total_len;
7209 int opcode, service_action, i, j, num;
7211 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7213 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7214 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7216 retval = CTL_RETVAL_COMPLETE;
7218 opcode = cdb->requested_opcode;
7219 service_action = scsi_2btoul(cdb->requested_service_action);
7220 switch (cdb->options & RSO_OPTIONS_MASK) {
7221 case RSO_OPTIONS_ALL:
7223 for (i = 0; i < 256; i++) {
7224 entry = &ctl_cmd_table[i];
7225 if (entry->flags & CTL_CMD_FLAG_SA5) {
7226 for (j = 0; j < 32; j++) {
7227 sentry = &((const struct ctl_cmd_entry *)
7229 if (ctl_cmd_applicable(
7230 lun->be_lun->lun_type, sentry))
7234 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7239 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7240 num * sizeof(struct scsi_report_supported_opcodes_descr);
7242 case RSO_OPTIONS_OC:
7243 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7244 ctl_set_invalid_field(/*ctsio*/ ctsio,
7250 ctl_done((union ctl_io *)ctsio);
7251 return (CTL_RETVAL_COMPLETE);
7253 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7255 case RSO_OPTIONS_OC_SA:
7256 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7257 service_action >= 32) {
7258 ctl_set_invalid_field(/*ctsio*/ ctsio,
7264 ctl_done((union ctl_io *)ctsio);
7265 return (CTL_RETVAL_COMPLETE);
7267 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7270 ctl_set_invalid_field(/*ctsio*/ ctsio,
7276 ctl_done((union ctl_io *)ctsio);
7277 return (CTL_RETVAL_COMPLETE);
7280 alloc_len = scsi_4btoul(cdb->length);
7282 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7284 ctsio->kern_sg_entries = 0;
7286 if (total_len < alloc_len) {
7287 ctsio->residual = alloc_len - total_len;
7288 ctsio->kern_data_len = total_len;
7289 ctsio->kern_total_len = total_len;
7291 ctsio->residual = 0;
7292 ctsio->kern_data_len = alloc_len;
7293 ctsio->kern_total_len = alloc_len;
7295 ctsio->kern_data_resid = 0;
7296 ctsio->kern_rel_offset = 0;
7298 switch (cdb->options & RSO_OPTIONS_MASK) {
7299 case RSO_OPTIONS_ALL:
7300 all = (struct scsi_report_supported_opcodes_all *)
7301 ctsio->kern_data_ptr;
7303 for (i = 0; i < 256; i++) {
7304 entry = &ctl_cmd_table[i];
7305 if (entry->flags & CTL_CMD_FLAG_SA5) {
7306 for (j = 0; j < 32; j++) {
7307 sentry = &((const struct ctl_cmd_entry *)
7309 if (!ctl_cmd_applicable(
7310 lun->be_lun->lun_type, sentry))
7312 descr = &all->descr[num++];
7314 scsi_ulto2b(j, descr->service_action);
7315 descr->flags = RSO_SERVACTV;
7316 scsi_ulto2b(sentry->length,
7320 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7323 descr = &all->descr[num++];
7325 scsi_ulto2b(0, descr->service_action);
7327 scsi_ulto2b(entry->length, descr->cdb_length);
7331 num * sizeof(struct scsi_report_supported_opcodes_descr),
7334 case RSO_OPTIONS_OC:
7335 one = (struct scsi_report_supported_opcodes_one *)
7336 ctsio->kern_data_ptr;
7337 entry = &ctl_cmd_table[opcode];
7339 case RSO_OPTIONS_OC_SA:
7340 one = (struct scsi_report_supported_opcodes_one *)
7341 ctsio->kern_data_ptr;
7342 entry = &ctl_cmd_table[opcode];
7343 entry = &((const struct ctl_cmd_entry *)
7344 entry->execute)[service_action];
7346 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7348 scsi_ulto2b(entry->length, one->cdb_length);
7349 one->cdb_usage[0] = opcode;
7350 memcpy(&one->cdb_usage[1], entry->usage,
7357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7358 ctsio->be_move_done = ctl_config_move_done;
7360 ctl_datamove((union ctl_io *)ctsio);
7365 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7367 struct ctl_lun *lun;
7368 struct scsi_report_supported_tmf *cdb;
7369 struct scsi_report_supported_tmf_data *data;
7371 int alloc_len, total_len;
7373 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7375 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7378 retval = CTL_RETVAL_COMPLETE;
7380 total_len = sizeof(struct scsi_report_supported_tmf_data);
7381 alloc_len = scsi_4btoul(cdb->length);
7383 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7385 ctsio->kern_sg_entries = 0;
7387 if (total_len < alloc_len) {
7388 ctsio->residual = alloc_len - total_len;
7389 ctsio->kern_data_len = total_len;
7390 ctsio->kern_total_len = total_len;
7392 ctsio->residual = 0;
7393 ctsio->kern_data_len = alloc_len;
7394 ctsio->kern_total_len = alloc_len;
7396 ctsio->kern_data_resid = 0;
7397 ctsio->kern_rel_offset = 0;
7399 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7400 data->byte1 |= RST_ATS | RST_LURS | RST_TRS;
7402 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7403 ctsio->be_move_done = ctl_config_move_done;
7405 ctl_datamove((union ctl_io *)ctsio);
7410 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7412 struct ctl_lun *lun;
7413 struct scsi_report_timestamp *cdb;
7414 struct scsi_report_timestamp_data *data;
7418 int alloc_len, total_len;
7420 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7422 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7423 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7425 retval = CTL_RETVAL_COMPLETE;
7427 total_len = sizeof(struct scsi_report_timestamp_data);
7428 alloc_len = scsi_4btoul(cdb->length);
7430 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7432 ctsio->kern_sg_entries = 0;
7434 if (total_len < alloc_len) {
7435 ctsio->residual = alloc_len - total_len;
7436 ctsio->kern_data_len = total_len;
7437 ctsio->kern_total_len = total_len;
7439 ctsio->residual = 0;
7440 ctsio->kern_data_len = alloc_len;
7441 ctsio->kern_total_len = alloc_len;
7443 ctsio->kern_data_resid = 0;
7444 ctsio->kern_rel_offset = 0;
7446 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7447 scsi_ulto2b(sizeof(*data) - 2, data->length);
7448 data->origin = RTS_ORIG_OUTSIDE;
7450 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7451 scsi_ulto4b(timestamp >> 16, data->timestamp);
7452 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7454 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7455 ctsio->be_move_done = ctl_config_move_done;
7457 ctl_datamove((union ctl_io *)ctsio);
7462 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7464 struct scsi_per_res_in *cdb;
7465 int alloc_len, total_len = 0;
7466 /* struct scsi_per_res_in_rsrv in_data; */
7467 struct ctl_lun *lun;
7468 struct ctl_softc *softc;
7470 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7472 softc = control_softc;
7474 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7476 alloc_len = scsi_2btoul(cdb->length);
7478 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7481 mtx_lock(&lun->lun_lock);
7482 switch (cdb->action) {
7483 case SPRI_RK: /* read keys */
7484 total_len = sizeof(struct scsi_per_res_in_keys) +
7486 sizeof(struct scsi_per_res_key);
7488 case SPRI_RR: /* read reservation */
7489 if (lun->flags & CTL_LUN_PR_RESERVED)
7490 total_len = sizeof(struct scsi_per_res_in_rsrv);
7492 total_len = sizeof(struct scsi_per_res_in_header);
7494 case SPRI_RC: /* report capabilities */
7495 total_len = sizeof(struct scsi_per_res_cap);
7498 panic("Invalid PR type %x", cdb->action);
7500 mtx_unlock(&lun->lun_lock);
7502 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7504 if (total_len < alloc_len) {
7505 ctsio->residual = alloc_len - total_len;
7506 ctsio->kern_data_len = total_len;
7507 ctsio->kern_total_len = total_len;
7509 ctsio->residual = 0;
7510 ctsio->kern_data_len = alloc_len;
7511 ctsio->kern_total_len = alloc_len;
7514 ctsio->kern_data_resid = 0;
7515 ctsio->kern_rel_offset = 0;
7516 ctsio->kern_sg_entries = 0;
7518 mtx_lock(&lun->lun_lock);
7519 switch (cdb->action) {
7520 case SPRI_RK: { // read keys
7521 struct scsi_per_res_in_keys *res_keys;
7524 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7527 * We had to drop the lock to allocate our buffer, which
7528 * leaves time for someone to come in with another
7529 * persistent reservation. (That is unlikely, though,
7530 * since this should be the only persistent reservation
7531 * command active right now.)
7533 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7534 (lun->pr_key_count *
7535 sizeof(struct scsi_per_res_key)))){
7536 mtx_unlock(&lun->lun_lock);
7537 free(ctsio->kern_data_ptr, M_CTL);
7538 printf("%s: reservation length changed, retrying\n",
7543 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7545 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7546 lun->pr_key_count, res_keys->header.length);
7548 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7549 if (!lun->per_res[i].registered)
7553 * We used lun->pr_key_count to calculate the
7554 * size to allocate. If it turns out the number of
7555 * initiators with the registered flag set is
7556 * larger than that (i.e. they haven't been kept in
7557 * sync), we've got a problem.
7559 if (key_count >= lun->pr_key_count) {
7561 csevent_log(CSC_CTL | CSC_SHELF_SW |
7563 csevent_LogType_Fault,
7564 csevent_AlertLevel_Yellow,
7565 csevent_FRU_ShelfController,
7566 csevent_FRU_Firmware,
7567 csevent_FRU_Unknown,
7568 "registered keys %d >= key "
7569 "count %d", key_count,
7575 memcpy(res_keys->keys[key_count].key,
7576 lun->per_res[i].res_key.key,
7577 ctl_min(sizeof(res_keys->keys[key_count].key),
7578 sizeof(lun->per_res[i].res_key)));
7583 case SPRI_RR: { // read reservation
7584 struct scsi_per_res_in_rsrv *res;
7585 int tmp_len, header_only;
7587 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7589 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7591 if (lun->flags & CTL_LUN_PR_RESERVED)
7593 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7594 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7595 res->header.length);
7598 tmp_len = sizeof(struct scsi_per_res_in_header);
7599 scsi_ulto4b(0, res->header.length);
7604 * We had to drop the lock to allocate our buffer, which
7605 * leaves time for someone to come in with another
7606 * persistent reservation. (That is unlikely, though,
7607 * since this should be the only persistent reservation
7608 * command active right now.)
7610 if (tmp_len != total_len) {
7611 mtx_unlock(&lun->lun_lock);
7612 free(ctsio->kern_data_ptr, M_CTL);
7613 printf("%s: reservation status changed, retrying\n",
7619 * No reservation held, so we're done.
7621 if (header_only != 0)
7625 * If the registration is an All Registrants type, the key
7626 * is 0, since it doesn't really matter.
7628 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7629 memcpy(res->data.reservation,
7630 &lun->per_res[lun->pr_res_idx].res_key,
7631 sizeof(struct scsi_per_res_key));
7633 res->data.scopetype = lun->res_type;
7636 case SPRI_RC: //report capabilities
7638 struct scsi_per_res_cap *res_cap;
7641 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7642 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7643 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7644 type_mask = SPRI_TM_WR_EX_AR |
7650 scsi_ulto2b(type_mask, res_cap->type_mask);
7653 case SPRI_RS: //read full status
7656 * This is a bug, because we just checked for this above,
7657 * and should have returned an error.
7659 panic("Invalid PR type %x", cdb->action);
7660 break; /* NOTREACHED */
7662 mtx_unlock(&lun->lun_lock);
7664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7665 ctsio->be_move_done = ctl_config_move_done;
7667 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7668 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7669 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7670 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7671 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7673 ctl_datamove((union ctl_io *)ctsio);
7675 return (CTL_RETVAL_COMPLETE);
7679 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7683 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7684 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7685 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7686 struct scsi_per_res_out_parms* param)
7688 union ctl_ha_msg persis_io;
7694 mtx_lock(&lun->lun_lock);
7695 if (sa_res_key == 0) {
7696 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7697 /* validate scope and type */
7698 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7700 mtx_unlock(&lun->lun_lock);
7701 ctl_set_invalid_field(/*ctsio*/ ctsio,
7707 ctl_done((union ctl_io *)ctsio);
7711 if (type>8 || type==2 || type==4 || type==0) {
7712 mtx_unlock(&lun->lun_lock);
7713 ctl_set_invalid_field(/*ctsio*/ ctsio,
7719 ctl_done((union ctl_io *)ctsio);
7723 /* temporarily unregister this nexus */
7724 lun->per_res[residx].registered = 0;
7727 * Unregister everybody else and build UA for
7730 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7731 if (lun->per_res[i].registered == 0)
7735 && i <CTL_MAX_INITIATORS)
7736 lun->pending_sense[i].ua_pending |=
7738 else if (persis_offset
7739 && i >= persis_offset)
7740 lun->pending_sense[i-persis_offset
7743 lun->per_res[i].registered = 0;
7744 memset(&lun->per_res[i].res_key, 0,
7745 sizeof(struct scsi_per_res_key));
7747 lun->per_res[residx].registered = 1;
7748 lun->pr_key_count = 1;
7749 lun->res_type = type;
7750 if (lun->res_type != SPR_TYPE_WR_EX_AR
7751 && lun->res_type != SPR_TYPE_EX_AC_AR)
7752 lun->pr_res_idx = residx;
7754 /* send msg to other side */
7755 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7756 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7757 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7758 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7759 persis_io.pr.pr_info.res_type = type;
7760 memcpy(persis_io.pr.pr_info.sa_res_key,
7761 param->serv_act_res_key,
7762 sizeof(param->serv_act_res_key));
7763 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7764 &persis_io, sizeof(persis_io), 0)) >
7765 CTL_HA_STATUS_SUCCESS) {
7766 printf("CTL:Persis Out error returned "
7767 "from ctl_ha_msg_send %d\n",
7771 /* not all registrants */
7772 mtx_unlock(&lun->lun_lock);
7773 free(ctsio->kern_data_ptr, M_CTL);
7774 ctl_set_invalid_field(ctsio,
7780 ctl_done((union ctl_io *)ctsio);
7783 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7784 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7787 if (res_key == sa_res_key) {
7790 * The spec implies this is not good but doesn't
7791 * say what to do. There are two choices either
7792 * generate a res conflict or check condition
7793 * with illegal field in parameter data. Since
7794 * that is what is done when the sa_res_key is
7795 * zero I'll take that approach since this has
7796 * to do with the sa_res_key.
7798 mtx_unlock(&lun->lun_lock);
7799 free(ctsio->kern_data_ptr, M_CTL);
7800 ctl_set_invalid_field(ctsio,
7806 ctl_done((union ctl_io *)ctsio);
7810 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7811 if (lun->per_res[i].registered
7812 && memcmp(param->serv_act_res_key,
7813 lun->per_res[i].res_key.key,
7814 sizeof(struct scsi_per_res_key)) != 0)
7818 lun->per_res[i].registered = 0;
7819 memset(&lun->per_res[i].res_key, 0,
7820 sizeof(struct scsi_per_res_key));
7821 lun->pr_key_count--;
7824 && i < CTL_MAX_INITIATORS)
7825 lun->pending_sense[i].ua_pending |=
7827 else if (persis_offset
7828 && i >= persis_offset)
7829 lun->pending_sense[i-persis_offset].ua_pending|=
7833 mtx_unlock(&lun->lun_lock);
7834 free(ctsio->kern_data_ptr, M_CTL);
7835 ctl_set_reservation_conflict(ctsio);
7836 ctl_done((union ctl_io *)ctsio);
7837 return (CTL_RETVAL_COMPLETE);
7839 /* send msg to other side */
7840 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7841 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7842 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7843 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7844 persis_io.pr.pr_info.res_type = type;
7845 memcpy(persis_io.pr.pr_info.sa_res_key,
7846 param->serv_act_res_key,
7847 sizeof(param->serv_act_res_key));
7848 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7849 &persis_io, sizeof(persis_io), 0)) >
7850 CTL_HA_STATUS_SUCCESS) {
7851 printf("CTL:Persis Out error returned from "
7852 "ctl_ha_msg_send %d\n", isc_retval);
7855 /* Reserved but not all registrants */
7856 /* sa_res_key is res holder */
7857 if (memcmp(param->serv_act_res_key,
7858 lun->per_res[lun->pr_res_idx].res_key.key,
7859 sizeof(struct scsi_per_res_key)) == 0) {
7860 /* validate scope and type */
7861 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7863 mtx_unlock(&lun->lun_lock);
7864 ctl_set_invalid_field(/*ctsio*/ ctsio,
7870 ctl_done((union ctl_io *)ctsio);
7874 if (type>8 || type==2 || type==4 || type==0) {
7875 mtx_unlock(&lun->lun_lock);
7876 ctl_set_invalid_field(/*ctsio*/ ctsio,
7882 ctl_done((union ctl_io *)ctsio);
7888 * if sa_res_key != res_key remove all
7889 * registrants w/sa_res_key and generate UA
7890 * for these registrants(Registrations
7891 * Preempted) if it wasn't an exclusive
7892 * reservation generate UA(Reservations
7893 * Preempted) for all other registered nexuses
7894 * if the type has changed. Establish the new
7895 * reservation and holder. If res_key and
7896 * sa_res_key are the same do the above
7897 * except don't unregister the res holder.
7901 * Temporarily unregister so it won't get
7902 * removed or UA generated
7904 lun->per_res[residx].registered = 0;
7905 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7906 if (lun->per_res[i].registered == 0)
7909 if (memcmp(param->serv_act_res_key,
7910 lun->per_res[i].res_key.key,
7911 sizeof(struct scsi_per_res_key)) == 0) {
7912 lun->per_res[i].registered = 0;
7913 memset(&lun->per_res[i].res_key,
7915 sizeof(struct scsi_per_res_key));
7916 lun->pr_key_count--;
7919 && i < CTL_MAX_INITIATORS)
7920 lun->pending_sense[i
7923 else if (persis_offset
7924 && i >= persis_offset)
7926 i-persis_offset].ua_pending |=
7928 } else if (type != lun->res_type
7929 && (lun->res_type == SPR_TYPE_WR_EX_RO
7930 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7932 && i < CTL_MAX_INITIATORS)
7933 lun->pending_sense[i
7936 else if (persis_offset
7937 && i >= persis_offset)
7944 lun->per_res[residx].registered = 1;
7945 lun->res_type = type;
7946 if (lun->res_type != SPR_TYPE_WR_EX_AR
7947 && lun->res_type != SPR_TYPE_EX_AC_AR)
7948 lun->pr_res_idx = residx;
7951 CTL_PR_ALL_REGISTRANTS;
7953 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7954 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7955 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7956 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7957 persis_io.pr.pr_info.res_type = type;
7958 memcpy(persis_io.pr.pr_info.sa_res_key,
7959 param->serv_act_res_key,
7960 sizeof(param->serv_act_res_key));
7961 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7962 &persis_io, sizeof(persis_io), 0)) >
7963 CTL_HA_STATUS_SUCCESS) {
7964 printf("CTL:Persis Out error returned "
7965 "from ctl_ha_msg_send %d\n",
7970 * sa_res_key is not the res holder just
7971 * remove registrants
7975 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7976 if (memcmp(param->serv_act_res_key,
7977 lun->per_res[i].res_key.key,
7978 sizeof(struct scsi_per_res_key)) != 0)
7982 lun->per_res[i].registered = 0;
7983 memset(&lun->per_res[i].res_key, 0,
7984 sizeof(struct scsi_per_res_key));
7985 lun->pr_key_count--;
7988 && i < CTL_MAX_INITIATORS)
7989 lun->pending_sense[i].ua_pending |=
7991 else if (persis_offset
7992 && i >= persis_offset)
7994 i-persis_offset].ua_pending |=
7999 mtx_unlock(&lun->lun_lock);
8000 free(ctsio->kern_data_ptr, M_CTL);
8001 ctl_set_reservation_conflict(ctsio);
8002 ctl_done((union ctl_io *)ctsio);
8005 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8006 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8007 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8008 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8009 persis_io.pr.pr_info.res_type = type;
8010 memcpy(persis_io.pr.pr_info.sa_res_key,
8011 param->serv_act_res_key,
8012 sizeof(param->serv_act_res_key));
8013 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8014 &persis_io, sizeof(persis_io), 0)) >
8015 CTL_HA_STATUS_SUCCESS) {
8016 printf("CTL:Persis Out error returned "
8017 "from ctl_ha_msg_send %d\n",
8023 lun->PRGeneration++;
8024 mtx_unlock(&lun->lun_lock);
8030 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8034 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8035 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8036 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8037 msg->pr.pr_info.sa_res_key,
8038 sizeof(struct scsi_per_res_key)) != 0) {
8039 uint64_t sa_res_key;
8040 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8042 if (sa_res_key == 0) {
8043 /* temporarily unregister this nexus */
8044 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8047 * Unregister everybody else and build UA for
8050 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8051 if (lun->per_res[i].registered == 0)
8055 && i < CTL_MAX_INITIATORS)
8056 lun->pending_sense[i].ua_pending |=
8058 else if (persis_offset && i >= persis_offset)
8059 lun->pending_sense[i -
8060 persis_offset].ua_pending |=
8062 lun->per_res[i].registered = 0;
8063 memset(&lun->per_res[i].res_key, 0,
8064 sizeof(struct scsi_per_res_key));
8067 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8068 lun->pr_key_count = 1;
8069 lun->res_type = msg->pr.pr_info.res_type;
8070 if (lun->res_type != SPR_TYPE_WR_EX_AR
8071 && lun->res_type != SPR_TYPE_EX_AC_AR)
8072 lun->pr_res_idx = msg->pr.pr_info.residx;
8074 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8075 if (memcmp(msg->pr.pr_info.sa_res_key,
8076 lun->per_res[i].res_key.key,
8077 sizeof(struct scsi_per_res_key)) != 0)
8080 lun->per_res[i].registered = 0;
8081 memset(&lun->per_res[i].res_key, 0,
8082 sizeof(struct scsi_per_res_key));
8083 lun->pr_key_count--;
8086 && i < persis_offset)
8087 lun->pending_sense[i].ua_pending |=
8089 else if (persis_offset
8090 && i >= persis_offset)
8091 lun->pending_sense[i -
8092 persis_offset].ua_pending |=
8098 * Temporarily unregister so it won't get removed
8101 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8102 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8103 if (lun->per_res[i].registered == 0)
8106 if (memcmp(msg->pr.pr_info.sa_res_key,
8107 lun->per_res[i].res_key.key,
8108 sizeof(struct scsi_per_res_key)) == 0) {
8109 lun->per_res[i].registered = 0;
8110 memset(&lun->per_res[i].res_key, 0,
8111 sizeof(struct scsi_per_res_key));
8112 lun->pr_key_count--;
8114 && i < CTL_MAX_INITIATORS)
8115 lun->pending_sense[i].ua_pending |=
8117 else if (persis_offset
8118 && i >= persis_offset)
8119 lun->pending_sense[i -
8120 persis_offset].ua_pending |=
8122 } else if (msg->pr.pr_info.res_type != lun->res_type
8123 && (lun->res_type == SPR_TYPE_WR_EX_RO
8124 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8126 && i < persis_offset)
8127 lun->pending_sense[i
8130 else if (persis_offset
8131 && i >= persis_offset)
8132 lun->pending_sense[i -
8133 persis_offset].ua_pending |=
8137 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8138 lun->res_type = msg->pr.pr_info.res_type;
8139 if (lun->res_type != SPR_TYPE_WR_EX_AR
8140 && lun->res_type != SPR_TYPE_EX_AC_AR)
8141 lun->pr_res_idx = msg->pr.pr_info.residx;
8143 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8145 lun->PRGeneration++;
8151 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8155 u_int32_t param_len;
8156 struct scsi_per_res_out *cdb;
8157 struct ctl_lun *lun;
8158 struct scsi_per_res_out_parms* param;
8159 struct ctl_softc *softc;
8161 uint64_t res_key, sa_res_key;
8163 union ctl_ha_msg persis_io;
8166 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8168 retval = CTL_RETVAL_COMPLETE;
8170 softc = control_softc;
8172 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8176 * We only support whole-LUN scope. The scope & type are ignored for
8177 * register, register and ignore existing key and clear.
8178 * We sometimes ignore scope and type on preempts too!!
8179 * Verify reservation type here as well.
8181 type = cdb->scope_type & SPR_TYPE_MASK;
8182 if ((cdb->action == SPRO_RESERVE)
8183 || (cdb->action == SPRO_RELEASE)) {
8184 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8185 ctl_set_invalid_field(/*ctsio*/ ctsio,
8191 ctl_done((union ctl_io *)ctsio);
8192 return (CTL_RETVAL_COMPLETE);
8195 if (type>8 || type==2 || type==4 || type==0) {
8196 ctl_set_invalid_field(/*ctsio*/ ctsio,
8202 ctl_done((union ctl_io *)ctsio);
8203 return (CTL_RETVAL_COMPLETE);
8207 param_len = scsi_4btoul(cdb->length);
8209 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8210 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8211 ctsio->kern_data_len = param_len;
8212 ctsio->kern_total_len = param_len;
8213 ctsio->kern_data_resid = 0;
8214 ctsio->kern_rel_offset = 0;
8215 ctsio->kern_sg_entries = 0;
8216 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8217 ctsio->be_move_done = ctl_config_move_done;
8218 ctl_datamove((union ctl_io *)ctsio);
8220 return (CTL_RETVAL_COMPLETE);
8223 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8225 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8226 res_key = scsi_8btou64(param->res_key.key);
8227 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8230 * Validate the reservation key here except for SPRO_REG_IGNO
8231 * This must be done for all other service actions
8233 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8234 mtx_lock(&lun->lun_lock);
8235 if (lun->per_res[residx].registered) {
8236 if (memcmp(param->res_key.key,
8237 lun->per_res[residx].res_key.key,
8238 ctl_min(sizeof(param->res_key),
8239 sizeof(lun->per_res[residx].res_key))) != 0) {
8241 * The current key passed in doesn't match
8242 * the one the initiator previously
8245 mtx_unlock(&lun->lun_lock);
8246 free(ctsio->kern_data_ptr, M_CTL);
8247 ctl_set_reservation_conflict(ctsio);
8248 ctl_done((union ctl_io *)ctsio);
8249 return (CTL_RETVAL_COMPLETE);
8251 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8253 * We are not registered
8255 mtx_unlock(&lun->lun_lock);
8256 free(ctsio->kern_data_ptr, M_CTL);
8257 ctl_set_reservation_conflict(ctsio);
8258 ctl_done((union ctl_io *)ctsio);
8259 return (CTL_RETVAL_COMPLETE);
8260 } else if (res_key != 0) {
8262 * We are not registered and trying to register but
8263 * the register key isn't zero.
8265 mtx_unlock(&lun->lun_lock);
8266 free(ctsio->kern_data_ptr, M_CTL);
8267 ctl_set_reservation_conflict(ctsio);
8268 ctl_done((union ctl_io *)ctsio);
8269 return (CTL_RETVAL_COMPLETE);
8271 mtx_unlock(&lun->lun_lock);
8274 switch (cdb->action & SPRO_ACTION_MASK) {
8276 case SPRO_REG_IGNO: {
8279 printf("Registration received\n");
8283 * We don't support any of these options, as we report in
8284 * the read capabilities request (see
8285 * ctl_persistent_reserve_in(), above).
8287 if ((param->flags & SPR_SPEC_I_PT)
8288 || (param->flags & SPR_ALL_TG_PT)
8289 || (param->flags & SPR_APTPL)) {
8292 if (param->flags & SPR_APTPL)
8294 else if (param->flags & SPR_ALL_TG_PT)
8296 else /* SPR_SPEC_I_PT */
8299 free(ctsio->kern_data_ptr, M_CTL);
8300 ctl_set_invalid_field(ctsio,
8306 ctl_done((union ctl_io *)ctsio);
8307 return (CTL_RETVAL_COMPLETE);
8310 mtx_lock(&lun->lun_lock);
8313 * The initiator wants to clear the
8316 if (sa_res_key == 0) {
8318 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8319 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8320 && !lun->per_res[residx].registered)) {
8321 mtx_unlock(&lun->lun_lock);
8325 lun->per_res[residx].registered = 0;
8326 memset(&lun->per_res[residx].res_key,
8327 0, sizeof(lun->per_res[residx].res_key));
8328 lun->pr_key_count--;
8330 if (residx == lun->pr_res_idx) {
8331 lun->flags &= ~CTL_LUN_PR_RESERVED;
8332 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8334 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8335 || lun->res_type == SPR_TYPE_EX_AC_RO)
8336 && lun->pr_key_count) {
8338 * If the reservation is a registrants
8339 * only type we need to generate a UA
8340 * for other registered inits. The
8341 * sense code should be RESERVATIONS
8345 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8347 i+persis_offset].registered
8350 lun->pending_sense[i
8356 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8357 if (lun->pr_key_count==0) {
8358 lun->flags &= ~CTL_LUN_PR_RESERVED;
8360 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8363 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8364 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8365 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8366 persis_io.pr.pr_info.residx = residx;
8367 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8368 &persis_io, sizeof(persis_io), 0 )) >
8369 CTL_HA_STATUS_SUCCESS) {
8370 printf("CTL:Persis Out error returned from "
8371 "ctl_ha_msg_send %d\n", isc_retval);
8373 } else /* sa_res_key != 0 */ {
8376 * If we aren't registered currently then increment
8377 * the key count and set the registered flag.
8379 if (!lun->per_res[residx].registered) {
8380 lun->pr_key_count++;
8381 lun->per_res[residx].registered = 1;
8384 memcpy(&lun->per_res[residx].res_key,
8385 param->serv_act_res_key,
8386 ctl_min(sizeof(param->serv_act_res_key),
8387 sizeof(lun->per_res[residx].res_key)));
8389 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8390 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8391 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8392 persis_io.pr.pr_info.residx = residx;
8393 memcpy(persis_io.pr.pr_info.sa_res_key,
8394 param->serv_act_res_key,
8395 sizeof(param->serv_act_res_key));
8396 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8397 &persis_io, sizeof(persis_io), 0)) >
8398 CTL_HA_STATUS_SUCCESS) {
8399 printf("CTL:Persis Out error returned from "
8400 "ctl_ha_msg_send %d\n", isc_retval);
8403 lun->PRGeneration++;
8404 mtx_unlock(&lun->lun_lock);
8410 printf("Reserve executed type %d\n", type);
8412 mtx_lock(&lun->lun_lock);
8413 if (lun->flags & CTL_LUN_PR_RESERVED) {
8415 * if this isn't the reservation holder and it's
8416 * not a "all registrants" type or if the type is
8417 * different then we have a conflict
8419 if ((lun->pr_res_idx != residx
8420 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8421 || lun->res_type != type) {
8422 mtx_unlock(&lun->lun_lock);
8423 free(ctsio->kern_data_ptr, M_CTL);
8424 ctl_set_reservation_conflict(ctsio);
8425 ctl_done((union ctl_io *)ctsio);
8426 return (CTL_RETVAL_COMPLETE);
8428 mtx_unlock(&lun->lun_lock);
8429 } else /* create a reservation */ {
8431 * If it's not an "all registrants" type record
8432 * reservation holder
8434 if (type != SPR_TYPE_WR_EX_AR
8435 && type != SPR_TYPE_EX_AC_AR)
8436 lun->pr_res_idx = residx; /* Res holder */
8438 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8440 lun->flags |= CTL_LUN_PR_RESERVED;
8441 lun->res_type = type;
8443 mtx_unlock(&lun->lun_lock);
8445 /* send msg to other side */
8446 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8447 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8448 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8449 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8450 persis_io.pr.pr_info.res_type = type;
8451 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8452 &persis_io, sizeof(persis_io), 0)) >
8453 CTL_HA_STATUS_SUCCESS) {
8454 printf("CTL:Persis Out error returned from "
8455 "ctl_ha_msg_send %d\n", isc_retval);
8461 mtx_lock(&lun->lun_lock);
8462 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8463 /* No reservation exists return good status */
8464 mtx_unlock(&lun->lun_lock);
8468 * Is this nexus a reservation holder?
8470 if (lun->pr_res_idx != residx
8471 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8473 * not a res holder return good status but
8476 mtx_unlock(&lun->lun_lock);
8480 if (lun->res_type != type) {
8481 mtx_unlock(&lun->lun_lock);
8482 free(ctsio->kern_data_ptr, M_CTL);
8483 ctl_set_illegal_pr_release(ctsio);
8484 ctl_done((union ctl_io *)ctsio);
8485 return (CTL_RETVAL_COMPLETE);
8488 /* okay to release */
8489 lun->flags &= ~CTL_LUN_PR_RESERVED;
8490 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8494 * if this isn't an exclusive access
8495 * res generate UA for all other
8498 if (type != SPR_TYPE_EX_AC
8499 && type != SPR_TYPE_WR_EX) {
8501 * temporarily unregister so we don't generate UA
8503 lun->per_res[residx].registered = 0;
8505 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8506 if (lun->per_res[i+persis_offset].registered
8509 lun->pending_sense[i].ua_pending |=
8513 lun->per_res[residx].registered = 1;
8515 mtx_unlock(&lun->lun_lock);
8516 /* Send msg to other side */
8517 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8518 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8519 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8520 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8521 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8522 printf("CTL:Persis Out error returned from "
8523 "ctl_ha_msg_send %d\n", isc_retval);
8528 /* send msg to other side */
8530 mtx_lock(&lun->lun_lock);
8531 lun->flags &= ~CTL_LUN_PR_RESERVED;
8533 lun->pr_key_count = 0;
8534 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8537 memset(&lun->per_res[residx].res_key,
8538 0, sizeof(lun->per_res[residx].res_key));
8539 lun->per_res[residx].registered = 0;
8541 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8542 if (lun->per_res[i].registered) {
8543 if (!persis_offset && i < CTL_MAX_INITIATORS)
8544 lun->pending_sense[i].ua_pending |=
8546 else if (persis_offset && i >= persis_offset)
8547 lun->pending_sense[i-persis_offset
8548 ].ua_pending |= CTL_UA_RES_PREEMPT;
8550 memset(&lun->per_res[i].res_key,
8551 0, sizeof(struct scsi_per_res_key));
8552 lun->per_res[i].registered = 0;
8554 lun->PRGeneration++;
8555 mtx_unlock(&lun->lun_lock);
8556 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8557 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8558 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8559 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8560 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8561 printf("CTL:Persis Out error returned from "
8562 "ctl_ha_msg_send %d\n", isc_retval);
8566 case SPRO_PREEMPT: {
8569 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8570 residx, ctsio, cdb, param);
8572 return (CTL_RETVAL_COMPLETE);
8576 panic("Invalid PR type %x", cdb->action);
8580 free(ctsio->kern_data_ptr, M_CTL);
8581 ctl_set_success(ctsio);
8582 ctl_done((union ctl_io *)ctsio);
8588 * This routine is for handling a message from the other SC pertaining to
8589 * persistent reserve out. All the error checking will have been done
8590 * so only perorming the action need be done here to keep the two
8594 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8596 struct ctl_lun *lun;
8597 struct ctl_softc *softc;
8601 softc = control_softc;
8603 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8604 lun = softc->ctl_luns[targ_lun];
8605 mtx_lock(&lun->lun_lock);
8606 switch(msg->pr.pr_info.action) {
8607 case CTL_PR_REG_KEY:
8608 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8609 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8610 lun->pr_key_count++;
8612 lun->PRGeneration++;
8613 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8614 msg->pr.pr_info.sa_res_key,
8615 sizeof(struct scsi_per_res_key));
8618 case CTL_PR_UNREG_KEY:
8619 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8620 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8621 0, sizeof(struct scsi_per_res_key));
8622 lun->pr_key_count--;
8624 /* XXX Need to see if the reservation has been released */
8625 /* if so do we need to generate UA? */
8626 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8627 lun->flags &= ~CTL_LUN_PR_RESERVED;
8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8630 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8631 || lun->res_type == SPR_TYPE_EX_AC_RO)
8632 && lun->pr_key_count) {
8634 * If the reservation is a registrants
8635 * only type we need to generate a UA
8636 * for other registered inits. The
8637 * sense code should be RESERVATIONS
8641 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8643 persis_offset].registered == 0)
8646 lun->pending_sense[i
8652 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8653 if (lun->pr_key_count==0) {
8654 lun->flags &= ~CTL_LUN_PR_RESERVED;
8656 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8659 lun->PRGeneration++;
8662 case CTL_PR_RESERVE:
8663 lun->flags |= CTL_LUN_PR_RESERVED;
8664 lun->res_type = msg->pr.pr_info.res_type;
8665 lun->pr_res_idx = msg->pr.pr_info.residx;
8669 case CTL_PR_RELEASE:
8671 * if this isn't an exclusive access res generate UA for all
8672 * other registrants.
8674 if (lun->res_type != SPR_TYPE_EX_AC
8675 && lun->res_type != SPR_TYPE_WR_EX) {
8676 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8677 if (lun->per_res[i+persis_offset].registered)
8678 lun->pending_sense[i].ua_pending |=
8682 lun->flags &= ~CTL_LUN_PR_RESERVED;
8683 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8687 case CTL_PR_PREEMPT:
8688 ctl_pro_preempt_other(lun, msg);
8691 lun->flags &= ~CTL_LUN_PR_RESERVED;
8693 lun->pr_key_count = 0;
8694 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8696 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8697 if (lun->per_res[i].registered == 0)
8700 && i < CTL_MAX_INITIATORS)
8701 lun->pending_sense[i].ua_pending |=
8703 else if (persis_offset
8704 && i >= persis_offset)
8705 lun->pending_sense[i-persis_offset].ua_pending|=
8707 memset(&lun->per_res[i].res_key, 0,
8708 sizeof(struct scsi_per_res_key));
8709 lun->per_res[i].registered = 0;
8711 lun->PRGeneration++;
8715 mtx_unlock(&lun->lun_lock);
8719 ctl_read_write(struct ctl_scsiio *ctsio)
8721 struct ctl_lun *lun;
8722 struct ctl_lba_len_flags *lbalen;
8724 uint32_t num_blocks;
8729 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8731 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8736 retval = CTL_RETVAL_COMPLETE;
8738 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8739 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8740 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8744 * XXX KDM need a lock here.
8746 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8747 if ((lun->res_type == SPR_TYPE_EX_AC
8748 && residx != lun->pr_res_idx)
8749 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8750 || lun->res_type == SPR_TYPE_EX_AC_AR)
8751 && !lun->per_res[residx].registered)) {
8752 ctl_set_reservation_conflict(ctsio);
8753 ctl_done((union ctl_io *)ctsio);
8754 return (CTL_RETVAL_COMPLETE);
8758 switch (ctsio->cdb[0]) {
8761 struct scsi_rw_6 *cdb;
8763 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8765 lba = scsi_3btoul(cdb->addr);
8766 /* only 5 bits are valid in the most significant address byte */
8768 num_blocks = cdb->length;
8770 * This is correct according to SBC-2.
8772 if (num_blocks == 0)
8778 struct scsi_rw_10 *cdb;
8780 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8782 if (cdb->byte2 & SRW10_FUA)
8784 if (cdb->byte2 & SRW10_DPO)
8787 lba = scsi_4btoul(cdb->addr);
8788 num_blocks = scsi_2btoul(cdb->length);
8791 case WRITE_VERIFY_10: {
8792 struct scsi_write_verify_10 *cdb;
8794 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8797 * XXX KDM we should do actual write verify support at some
8798 * point. This is obviously fake, we're just translating
8799 * things to a write. So we don't even bother checking the
8800 * BYTCHK field, since we don't do any verification. If
8801 * the user asks for it, we'll just pretend we did it.
8803 if (cdb->byte2 & SWV_DPO)
8806 lba = scsi_4btoul(cdb->addr);
8807 num_blocks = scsi_2btoul(cdb->length);
8812 struct scsi_rw_12 *cdb;
8814 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8816 if (cdb->byte2 & SRW12_FUA)
8818 if (cdb->byte2 & SRW12_DPO)
8820 lba = scsi_4btoul(cdb->addr);
8821 num_blocks = scsi_4btoul(cdb->length);
8824 case WRITE_VERIFY_12: {
8825 struct scsi_write_verify_12 *cdb;
8827 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8829 if (cdb->byte2 & SWV_DPO)
8832 lba = scsi_4btoul(cdb->addr);
8833 num_blocks = scsi_4btoul(cdb->length);
8839 struct scsi_rw_16 *cdb;
8841 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8843 if (cdb->byte2 & SRW12_FUA)
8845 if (cdb->byte2 & SRW12_DPO)
8848 lba = scsi_8btou64(cdb->addr);
8849 num_blocks = scsi_4btoul(cdb->length);
8852 case WRITE_VERIFY_16: {
8853 struct scsi_write_verify_16 *cdb;
8855 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8857 if (cdb->byte2 & SWV_DPO)
8860 lba = scsi_8btou64(cdb->addr);
8861 num_blocks = scsi_4btoul(cdb->length);
8866 * We got a command we don't support. This shouldn't
8867 * happen, commands should be filtered out above us.
8869 ctl_set_invalid_opcode(ctsio);
8870 ctl_done((union ctl_io *)ctsio);
8872 return (CTL_RETVAL_COMPLETE);
8873 break; /* NOTREACHED */
8877 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8878 * interesting for us, but if RAIDCore is in write-back mode,
8879 * getting it to do write-through for a particular transaction may
8884 * The first check is to make sure we're in bounds, the second
8885 * check is to catch wrap-around problems. If the lba + num blocks
8886 * is less than the lba, then we've wrapped around and the block
8887 * range is invalid anyway.
8889 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8890 || ((lba + num_blocks) < lba)) {
8891 ctl_set_lba_out_of_range(ctsio);
8892 ctl_done((union ctl_io *)ctsio);
8893 return (CTL_RETVAL_COMPLETE);
8897 * According to SBC-3, a transfer length of 0 is not an error.
8898 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8899 * translates to 256 blocks for those commands.
8901 if (num_blocks == 0) {
8902 ctl_set_success(ctsio);
8903 ctl_done((union ctl_io *)ctsio);
8904 return (CTL_RETVAL_COMPLETE);
8907 lbalen = (struct ctl_lba_len_flags *)
8908 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8910 lbalen->len = num_blocks;
8911 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8913 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8914 ctsio->kern_rel_offset = 0;
8916 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8918 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8924 ctl_cnw_cont(union ctl_io *io)
8926 struct ctl_scsiio *ctsio;
8927 struct ctl_lun *lun;
8928 struct ctl_lba_len_flags *lbalen;
8931 ctsio = &io->scsiio;
8932 ctsio->io_hdr.status = CTL_STATUS_NONE;
8933 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8934 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8935 lbalen = (struct ctl_lba_len_flags *)
8936 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8937 lbalen->flags = CTL_LLF_WRITE;
8939 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8940 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8945 ctl_cnw(struct ctl_scsiio *ctsio)
8947 struct ctl_lun *lun;
8948 struct ctl_lba_len_flags *lbalen;
8950 uint32_t num_blocks;
8954 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8956 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8961 retval = CTL_RETVAL_COMPLETE;
8963 switch (ctsio->cdb[0]) {
8964 case COMPARE_AND_WRITE: {
8965 struct scsi_compare_and_write *cdb;
8967 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8969 if (cdb->byte2 & SRW10_FUA)
8971 if (cdb->byte2 & SRW10_DPO)
8973 lba = scsi_8btou64(cdb->addr);
8974 num_blocks = cdb->length;
8979 * We got a command we don't support. This shouldn't
8980 * happen, commands should be filtered out above us.
8982 ctl_set_invalid_opcode(ctsio);
8983 ctl_done((union ctl_io *)ctsio);
8985 return (CTL_RETVAL_COMPLETE);
8986 break; /* NOTREACHED */
8990 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8991 * interesting for us, but if RAIDCore is in write-back mode,
8992 * getting it to do write-through for a particular transaction may
8997 * The first check is to make sure we're in bounds, the second
8998 * check is to catch wrap-around problems. If the lba + num blocks
8999 * is less than the lba, then we've wrapped around and the block
9000 * range is invalid anyway.
9002 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9003 || ((lba + num_blocks) < lba)) {
9004 ctl_set_lba_out_of_range(ctsio);
9005 ctl_done((union ctl_io *)ctsio);
9006 return (CTL_RETVAL_COMPLETE);
9010 * According to SBC-3, a transfer length of 0 is not an error.
9012 if (num_blocks == 0) {
9013 ctl_set_success(ctsio);
9014 ctl_done((union ctl_io *)ctsio);
9015 return (CTL_RETVAL_COMPLETE);
9018 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9019 ctsio->kern_rel_offset = 0;
9022 * Set the IO_CONT flag, so that if this I/O gets passed to
9023 * ctl_data_submit_done(), it'll get passed back to
9024 * ctl_ctl_cnw_cont() for further processing.
9026 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9027 ctsio->io_cont = ctl_cnw_cont;
9029 lbalen = (struct ctl_lba_len_flags *)
9030 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9032 lbalen->len = num_blocks;
9033 lbalen->flags = CTL_LLF_COMPARE;
9035 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9036 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9041 ctl_verify(struct ctl_scsiio *ctsio)
9043 struct ctl_lun *lun;
9044 struct ctl_lba_len_flags *lbalen;
9046 uint32_t num_blocks;
9050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9052 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9056 retval = CTL_RETVAL_COMPLETE;
9058 switch (ctsio->cdb[0]) {
9060 struct scsi_verify_10 *cdb;
9062 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9063 if (cdb->byte2 & SVFY_BYTCHK)
9065 if (cdb->byte2 & SVFY_DPO)
9067 lba = scsi_4btoul(cdb->addr);
9068 num_blocks = scsi_2btoul(cdb->length);
9072 struct scsi_verify_12 *cdb;
9074 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9075 if (cdb->byte2 & SVFY_BYTCHK)
9077 if (cdb->byte2 & SVFY_DPO)
9079 lba = scsi_4btoul(cdb->addr);
9080 num_blocks = scsi_4btoul(cdb->length);
9084 struct scsi_rw_16 *cdb;
9086 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9087 if (cdb->byte2 & SVFY_BYTCHK)
9089 if (cdb->byte2 & SVFY_DPO)
9091 lba = scsi_8btou64(cdb->addr);
9092 num_blocks = scsi_4btoul(cdb->length);
9097 * We got a command we don't support. This shouldn't
9098 * happen, commands should be filtered out above us.
9100 ctl_set_invalid_opcode(ctsio);
9101 ctl_done((union ctl_io *)ctsio);
9102 return (CTL_RETVAL_COMPLETE);
9106 * The first check is to make sure we're in bounds, the second
9107 * check is to catch wrap-around problems. If the lba + num blocks
9108 * is less than the lba, then we've wrapped around and the block
9109 * range is invalid anyway.
9111 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9112 || ((lba + num_blocks) < lba)) {
9113 ctl_set_lba_out_of_range(ctsio);
9114 ctl_done((union ctl_io *)ctsio);
9115 return (CTL_RETVAL_COMPLETE);
9119 * According to SBC-3, a transfer length of 0 is not an error.
9121 if (num_blocks == 0) {
9122 ctl_set_success(ctsio);
9123 ctl_done((union ctl_io *)ctsio);
9124 return (CTL_RETVAL_COMPLETE);
9127 lbalen = (struct ctl_lba_len_flags *)
9128 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9130 lbalen->len = num_blocks;
9132 lbalen->flags = CTL_LLF_COMPARE;
9133 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9135 lbalen->flags = CTL_LLF_VERIFY;
9136 ctsio->kern_total_len = 0;
9138 ctsio->kern_rel_offset = 0;
9140 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9141 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9146 ctl_report_luns(struct ctl_scsiio *ctsio)
9148 struct scsi_report_luns *cdb;
9149 struct scsi_report_luns_data *lun_data;
9150 struct ctl_lun *lun, *request_lun;
9151 int num_luns, retval;
9152 uint32_t alloc_len, lun_datalen;
9153 int num_filled, well_known;
9154 uint32_t initidx, targ_lun_id, lun_id;
9156 retval = CTL_RETVAL_COMPLETE;
9159 cdb = (struct scsi_report_luns *)ctsio->cdb;
9161 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9163 mtx_lock(&control_softc->ctl_lock);
9164 num_luns = control_softc->num_luns;
9165 mtx_unlock(&control_softc->ctl_lock);
9167 switch (cdb->select_report) {
9168 case RPL_REPORT_DEFAULT:
9169 case RPL_REPORT_ALL:
9171 case RPL_REPORT_WELLKNOWN:
9176 ctl_set_invalid_field(ctsio,
9182 ctl_done((union ctl_io *)ctsio);
9184 break; /* NOTREACHED */
9187 alloc_len = scsi_4btoul(cdb->length);
9189 * The initiator has to allocate at least 16 bytes for this request,
9190 * so he can at least get the header and the first LUN. Otherwise
9191 * we reject the request (per SPC-3 rev 14, section 6.21).
9193 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9194 sizeof(struct scsi_report_luns_lundata))) {
9195 ctl_set_invalid_field(ctsio,
9201 ctl_done((union ctl_io *)ctsio);
9205 request_lun = (struct ctl_lun *)
9206 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9208 lun_datalen = sizeof(*lun_data) +
9209 (num_luns * sizeof(struct scsi_report_luns_lundata));
9211 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9212 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9213 ctsio->kern_sg_entries = 0;
9215 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9217 mtx_lock(&control_softc->ctl_lock);
9218 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9219 lun_id = targ_lun_id;
9220 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9221 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9222 if (lun_id >= CTL_MAX_LUNS)
9224 lun = control_softc->ctl_luns[lun_id];
9228 if (targ_lun_id <= 0xff) {
9230 * Peripheral addressing method, bus number 0.
9232 lun_data->luns[num_filled].lundata[0] =
9233 RPL_LUNDATA_ATYP_PERIPH;
9234 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9236 } else if (targ_lun_id <= 0x3fff) {
9238 * Flat addressing method.
9240 lun_data->luns[num_filled].lundata[0] =
9241 RPL_LUNDATA_ATYP_FLAT |
9242 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9243 #ifdef OLDCTLHEADERS
9244 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9245 (targ_lun_id & SRLD_BUS_LUN_MASK);
9247 lun_data->luns[num_filled].lundata[1] =
9248 #ifdef OLDCTLHEADERS
9249 targ_lun_id >> SRLD_BUS_LUN_BITS;
9251 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9254 printf("ctl_report_luns: bogus LUN number %jd, "
9255 "skipping\n", (intmax_t)targ_lun_id);
9258 * According to SPC-3, rev 14 section 6.21:
9260 * "The execution of a REPORT LUNS command to any valid and
9261 * installed logical unit shall clear the REPORTED LUNS DATA
9262 * HAS CHANGED unit attention condition for all logical
9263 * units of that target with respect to the requesting
9264 * initiator. A valid and installed logical unit is one
9265 * having a PERIPHERAL QUALIFIER of 000b in the standard
9266 * INQUIRY data (see 6.4.2)."
9268 * If request_lun is NULL, the LUN this report luns command
9269 * was issued to is either disabled or doesn't exist. In that
9270 * case, we shouldn't clear any pending lun change unit
9273 if (request_lun != NULL) {
9274 mtx_lock(&lun->lun_lock);
9275 lun->pending_sense[initidx].ua_pending &=
9277 mtx_unlock(&lun->lun_lock);
9280 mtx_unlock(&control_softc->ctl_lock);
9283 * It's quite possible that we've returned fewer LUNs than we allocated
9284 * space for. Trim it.
9286 lun_datalen = sizeof(*lun_data) +
9287 (num_filled * sizeof(struct scsi_report_luns_lundata));
9289 if (lun_datalen < alloc_len) {
9290 ctsio->residual = alloc_len - lun_datalen;
9291 ctsio->kern_data_len = lun_datalen;
9292 ctsio->kern_total_len = lun_datalen;
9294 ctsio->residual = 0;
9295 ctsio->kern_data_len = alloc_len;
9296 ctsio->kern_total_len = alloc_len;
9298 ctsio->kern_data_resid = 0;
9299 ctsio->kern_rel_offset = 0;
9300 ctsio->kern_sg_entries = 0;
9303 * We set this to the actual data length, regardless of how much
9304 * space we actually have to return results. If the user looks at
9305 * this value, he'll know whether or not he allocated enough space
9306 * and reissue the command if necessary. We don't support well
9307 * known logical units, so if the user asks for that, return none.
9309 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9312 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9315 ctsio->scsi_status = SCSI_STATUS_OK;
9317 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9318 ctsio->be_move_done = ctl_config_move_done;
9319 ctl_datamove((union ctl_io *)ctsio);
9325 ctl_request_sense(struct ctl_scsiio *ctsio)
9327 struct scsi_request_sense *cdb;
9328 struct scsi_sense_data *sense_ptr;
9329 struct ctl_lun *lun;
9332 scsi_sense_data_type sense_format;
9334 cdb = (struct scsi_request_sense *)ctsio->cdb;
9336 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9338 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9341 * Determine which sense format the user wants.
9343 if (cdb->byte2 & SRS_DESC)
9344 sense_format = SSD_TYPE_DESC;
9346 sense_format = SSD_TYPE_FIXED;
9348 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9349 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9350 ctsio->kern_sg_entries = 0;
9353 * struct scsi_sense_data, which is currently set to 256 bytes, is
9354 * larger than the largest allowed value for the length field in the
9355 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9357 ctsio->residual = 0;
9358 ctsio->kern_data_len = cdb->length;
9359 ctsio->kern_total_len = cdb->length;
9361 ctsio->kern_data_resid = 0;
9362 ctsio->kern_rel_offset = 0;
9363 ctsio->kern_sg_entries = 0;
9366 * If we don't have a LUN, we don't have any pending sense.
9372 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9374 * Check for pending sense, and then for pending unit attentions.
9375 * Pending sense gets returned first, then pending unit attentions.
9377 mtx_lock(&lun->lun_lock);
9378 if (ctl_is_set(lun->have_ca, initidx)) {
9379 scsi_sense_data_type stored_format;
9382 * Check to see which sense format was used for the stored
9385 stored_format = scsi_sense_type(
9386 &lun->pending_sense[initidx].sense);
9389 * If the user requested a different sense format than the
9390 * one we stored, then we need to convert it to the other
9391 * format. If we're going from descriptor to fixed format
9392 * sense data, we may lose things in translation, depending
9393 * on what options were used.
9395 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9396 * for some reason we'll just copy it out as-is.
9398 if ((stored_format == SSD_TYPE_FIXED)
9399 && (sense_format == SSD_TYPE_DESC))
9400 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9401 &lun->pending_sense[initidx].sense,
9402 (struct scsi_sense_data_desc *)sense_ptr);
9403 else if ((stored_format == SSD_TYPE_DESC)
9404 && (sense_format == SSD_TYPE_FIXED))
9405 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9406 &lun->pending_sense[initidx].sense,
9407 (struct scsi_sense_data_fixed *)sense_ptr);
9409 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9410 ctl_min(sizeof(*sense_ptr),
9411 sizeof(lun->pending_sense[initidx].sense)));
9413 ctl_clear_mask(lun->have_ca, initidx);
9415 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9416 ctl_ua_type ua_type;
9418 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9419 sense_ptr, sense_format);
9420 if (ua_type != CTL_UA_NONE) {
9422 /* We're reporting this UA, so clear it */
9423 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9426 mtx_unlock(&lun->lun_lock);
9429 * We already have a pending error, return it.
9431 if (have_error != 0) {
9433 * We report the SCSI status as OK, since the status of the
9434 * request sense command itself is OK.
9436 ctsio->scsi_status = SCSI_STATUS_OK;
9439 * We report 0 for the sense length, because we aren't doing
9440 * autosense in this case. We're reporting sense as
9443 ctsio->sense_len = 0;
9444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9445 ctsio->be_move_done = ctl_config_move_done;
9446 ctl_datamove((union ctl_io *)ctsio);
9448 return (CTL_RETVAL_COMPLETE);
9454 * No sense information to report, so we report that everything is
9457 ctl_set_sense_data(sense_ptr,
9460 /*current_error*/ 1,
9461 /*sense_key*/ SSD_KEY_NO_SENSE,
9466 ctsio->scsi_status = SCSI_STATUS_OK;
9469 * We report 0 for the sense length, because we aren't doing
9470 * autosense in this case. We're reporting sense as parameter data.
9472 ctsio->sense_len = 0;
9473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9474 ctsio->be_move_done = ctl_config_move_done;
9475 ctl_datamove((union ctl_io *)ctsio);
9477 return (CTL_RETVAL_COMPLETE);
9481 ctl_tur(struct ctl_scsiio *ctsio)
9483 struct ctl_lun *lun;
9485 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9487 CTL_DEBUG_PRINT(("ctl_tur\n"));
9492 ctsio->scsi_status = SCSI_STATUS_OK;
9493 ctsio->io_hdr.status = CTL_SUCCESS;
9495 ctl_done((union ctl_io *)ctsio);
9497 return (CTL_RETVAL_COMPLETE);
9502 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9509 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9511 struct scsi_vpd_supported_pages *pages;
9513 struct ctl_lun *lun;
9515 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9517 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9518 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9519 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9520 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9521 ctsio->kern_sg_entries = 0;
9523 if (sup_page_size < alloc_len) {
9524 ctsio->residual = alloc_len - sup_page_size;
9525 ctsio->kern_data_len = sup_page_size;
9526 ctsio->kern_total_len = sup_page_size;
9528 ctsio->residual = 0;
9529 ctsio->kern_data_len = alloc_len;
9530 ctsio->kern_total_len = alloc_len;
9532 ctsio->kern_data_resid = 0;
9533 ctsio->kern_rel_offset = 0;
9534 ctsio->kern_sg_entries = 0;
9537 * The control device is always connected. The disk device, on the
9538 * other hand, may not be online all the time. Need to change this
9539 * to figure out whether the disk device is actually online or not.
9542 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9543 lun->be_lun->lun_type;
9545 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9547 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9548 /* Supported VPD pages */
9549 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9551 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9552 /* Device Identification */
9553 pages->page_list[2] = SVPD_DEVICE_ID;
9555 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9556 /* Logical Block Provisioning */
9557 pages->page_list[4] = SVPD_LBP;
9559 ctsio->scsi_status = SCSI_STATUS_OK;
9561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9562 ctsio->be_move_done = ctl_config_move_done;
9563 ctl_datamove((union ctl_io *)ctsio);
9565 return (CTL_RETVAL_COMPLETE);
9569 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9571 struct scsi_vpd_unit_serial_number *sn_ptr;
9572 struct ctl_lun *lun;
9574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9576 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9577 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9578 ctsio->kern_sg_entries = 0;
9580 if (sizeof(*sn_ptr) < alloc_len) {
9581 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9582 ctsio->kern_data_len = sizeof(*sn_ptr);
9583 ctsio->kern_total_len = sizeof(*sn_ptr);
9585 ctsio->residual = 0;
9586 ctsio->kern_data_len = alloc_len;
9587 ctsio->kern_total_len = alloc_len;
9589 ctsio->kern_data_resid = 0;
9590 ctsio->kern_rel_offset = 0;
9591 ctsio->kern_sg_entries = 0;
9594 * The control device is always connected. The disk device, on the
9595 * other hand, may not be online all the time. Need to change this
9596 * to figure out whether the disk device is actually online or not.
9599 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9600 lun->be_lun->lun_type;
9602 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9604 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9605 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9607 * If we don't have a LUN, we just leave the serial number as
9610 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9612 strncpy((char *)sn_ptr->serial_num,
9613 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9615 ctsio->scsi_status = SCSI_STATUS_OK;
9617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9618 ctsio->be_move_done = ctl_config_move_done;
9619 ctl_datamove((union ctl_io *)ctsio);
9621 return (CTL_RETVAL_COMPLETE);
9626 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9628 struct scsi_vpd_device_id *devid_ptr;
9629 struct scsi_vpd_id_descriptor *desc, *desc1;
9630 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9631 struct scsi_vpd_id_t10 *t10id;
9632 struct ctl_softc *ctl_softc;
9633 struct ctl_lun *lun;
9634 struct ctl_port *port;
9636 int data_len, devid_len;
9638 ctl_softc = control_softc;
9640 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9642 if (port->devid != NULL)
9643 return ((port->devid)(ctsio, alloc_len));
9645 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9648 devid_len = CTL_DEVID_MIN_LEN;
9650 devid_len = max(CTL_DEVID_MIN_LEN,
9651 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN));
9654 data_len = sizeof(struct scsi_vpd_device_id) +
9655 sizeof(struct scsi_vpd_id_descriptor) +
9656 sizeof(struct scsi_vpd_id_t10) + devid_len +
9657 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9658 sizeof(struct scsi_vpd_id_descriptor) +
9659 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9660 sizeof(struct scsi_vpd_id_descriptor) +
9661 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9663 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9664 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9665 ctsio->kern_sg_entries = 0;
9667 if (data_len < alloc_len) {
9668 ctsio->residual = alloc_len - data_len;
9669 ctsio->kern_data_len = data_len;
9670 ctsio->kern_total_len = data_len;
9672 ctsio->residual = 0;
9673 ctsio->kern_data_len = alloc_len;
9674 ctsio->kern_total_len = alloc_len;
9676 ctsio->kern_data_resid = 0;
9677 ctsio->kern_rel_offset = 0;
9678 ctsio->kern_sg_entries = 0;
9680 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9681 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9682 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9683 sizeof(struct scsi_vpd_id_t10) + devid_len);
9684 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9686 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9687 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9690 * The control device is always connected. The disk device, on the
9691 * other hand, may not be online all the time.
9694 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9695 lun->be_lun->lun_type;
9697 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9699 devid_ptr->page_code = SVPD_DEVICE_ID;
9701 scsi_ulto2b(data_len - 4, devid_ptr->length);
9704 * For Fibre channel,
9706 if (port->port_type == CTL_PORT_FC)
9708 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9709 SVPD_ID_CODESET_ASCII;
9710 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9711 SVPD_ID_CODESET_BINARY;
9715 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9716 SVPD_ID_CODESET_ASCII;
9717 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9718 SVPD_ID_CODESET_BINARY;
9720 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9723 * We're using a LUN association here. i.e., this device ID is a
9724 * per-LUN identifier.
9726 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9727 desc->length = sizeof(*t10id) + devid_len;
9728 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
9729 "vendor")) == NULL) {
9730 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9732 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9733 strncpy(t10id->vendor, val,
9734 min(sizeof(t10id->vendor), strlen(val)));
9738 * desc1 is for the WWPN which is a port asscociation.
9740 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9741 desc1->length = CTL_WWPN_LEN;
9742 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9743 /* For testing just create the WWPN */
9745 ddb_GetWWNN((char *)desc1->identifier);
9747 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9748 /* This is so Copancontrol will return something sane */
9749 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9750 ctsio->io_hdr.nexus.targ_port!=8)
9751 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9753 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9756 be64enc(desc1->identifier, port->wwpn);
9759 * desc2 is for the Relative Target Port(type 4h) identifier
9761 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9762 | SVPD_ID_TYPE_RELTARG;
9765 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9766 /* This is so Copancontrol will return something sane */
9767 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9768 ctsio->io_hdr.nexus.targ_port!=8)
9769 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9771 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9775 * desc3 is for the Target Port Group(type 5h) identifier
9777 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9778 | SVPD_ID_TYPE_TPORTGRP;
9780 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9781 desc3->identifier[3] = 1;
9783 desc3->identifier[3] = 2;
9786 * If we've actually got a backend, copy the device id from the
9787 * per-LUN data. Otherwise, set it to all spaces.
9791 * Copy the backend's LUN ID.
9793 strncpy((char *)t10id->vendor_spec_id,
9794 (char *)lun->be_lun->device_id, devid_len);
9797 * No backend, set this to spaces.
9799 memset(t10id->vendor_spec_id, 0x20, devid_len);
9802 ctsio->scsi_status = SCSI_STATUS_OK;
9804 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9805 ctsio->be_move_done = ctl_config_move_done;
9806 ctl_datamove((union ctl_io *)ctsio);
9808 return (CTL_RETVAL_COMPLETE);
9812 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9814 struct scsi_vpd_block_limits *bl_ptr;
9815 struct ctl_lun *lun;
9818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9819 bs = lun->be_lun->blocksize;
9821 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9822 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9823 ctsio->kern_sg_entries = 0;
9825 if (sizeof(*bl_ptr) < alloc_len) {
9826 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9827 ctsio->kern_data_len = sizeof(*bl_ptr);
9828 ctsio->kern_total_len = sizeof(*bl_ptr);
9830 ctsio->residual = 0;
9831 ctsio->kern_data_len = alloc_len;
9832 ctsio->kern_total_len = alloc_len;
9834 ctsio->kern_data_resid = 0;
9835 ctsio->kern_rel_offset = 0;
9836 ctsio->kern_sg_entries = 0;
9839 * The control device is always connected. The disk device, on the
9840 * other hand, may not be online all the time. Need to change this
9841 * to figure out whether the disk device is actually online or not.
9844 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9845 lun->be_lun->lun_type;
9847 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9849 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9850 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9851 bl_ptr->max_cmp_write_len = 0xff;
9852 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9853 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9854 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9855 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9856 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9858 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9860 ctsio->scsi_status = SCSI_STATUS_OK;
9861 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9862 ctsio->be_move_done = ctl_config_move_done;
9863 ctl_datamove((union ctl_io *)ctsio);
9865 return (CTL_RETVAL_COMPLETE);
9869 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9871 struct scsi_vpd_logical_block_prov *lbp_ptr;
9872 struct ctl_lun *lun;
9875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9876 bs = lun->be_lun->blocksize;
9878 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9879 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9880 ctsio->kern_sg_entries = 0;
9882 if (sizeof(*lbp_ptr) < alloc_len) {
9883 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9884 ctsio->kern_data_len = sizeof(*lbp_ptr);
9885 ctsio->kern_total_len = sizeof(*lbp_ptr);
9887 ctsio->residual = 0;
9888 ctsio->kern_data_len = alloc_len;
9889 ctsio->kern_total_len = alloc_len;
9891 ctsio->kern_data_resid = 0;
9892 ctsio->kern_rel_offset = 0;
9893 ctsio->kern_sg_entries = 0;
9896 * The control device is always connected. The disk device, on the
9897 * other hand, may not be online all the time. Need to change this
9898 * to figure out whether the disk device is actually online or not.
9901 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9902 lun->be_lun->lun_type;
9904 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9906 lbp_ptr->page_code = SVPD_LBP;
9907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9908 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9910 ctsio->scsi_status = SCSI_STATUS_OK;
9911 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9912 ctsio->be_move_done = ctl_config_move_done;
9913 ctl_datamove((union ctl_io *)ctsio);
9915 return (CTL_RETVAL_COMPLETE);
9919 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9921 struct scsi_inquiry *cdb;
9922 struct ctl_lun *lun;
9923 int alloc_len, retval;
9925 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9926 cdb = (struct scsi_inquiry *)ctsio->cdb;
9928 retval = CTL_RETVAL_COMPLETE;
9930 alloc_len = scsi_2btoul(cdb->length);
9932 switch (cdb->page_code) {
9933 case SVPD_SUPPORTED_PAGES:
9934 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9936 case SVPD_UNIT_SERIAL_NUMBER:
9937 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9939 case SVPD_DEVICE_ID:
9940 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9942 case SVPD_BLOCK_LIMITS:
9943 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9946 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9949 ctl_set_invalid_field(ctsio,
9955 ctl_done((union ctl_io *)ctsio);
9956 retval = CTL_RETVAL_COMPLETE;
9964 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9966 struct scsi_inquiry_data *inq_ptr;
9967 struct scsi_inquiry *cdb;
9968 struct ctl_softc *ctl_softc;
9969 struct ctl_lun *lun;
9974 ctl_softc = control_softc;
9977 * Figure out whether we're talking to a Fibre Channel port or not.
9978 * We treat the ioctl front end, and any SCSI adapters, as packetized
9981 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9987 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9988 cdb = (struct scsi_inquiry *)ctsio->cdb;
9989 alloc_len = scsi_2btoul(cdb->length);
9992 * We malloc the full inquiry data size here and fill it
9993 * in. If the user only asks for less, we'll give him
9996 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9997 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9998 ctsio->kern_sg_entries = 0;
9999 ctsio->kern_data_resid = 0;
10000 ctsio->kern_rel_offset = 0;
10002 if (sizeof(*inq_ptr) < alloc_len) {
10003 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10004 ctsio->kern_data_len = sizeof(*inq_ptr);
10005 ctsio->kern_total_len = sizeof(*inq_ptr);
10007 ctsio->residual = 0;
10008 ctsio->kern_data_len = alloc_len;
10009 ctsio->kern_total_len = alloc_len;
10013 * If we have a LUN configured, report it as connected. Otherwise,
10014 * report that it is offline or no device is supported, depending
10015 * on the value of inquiry_pq_no_lun.
10017 * According to the spec (SPC-4 r34), the peripheral qualifier
10018 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10020 * "A peripheral device having the specified peripheral device type
10021 * is not connected to this logical unit. However, the device
10022 * server is capable of supporting the specified peripheral device
10023 * type on this logical unit."
10025 * According to the same spec, the peripheral qualifier
10026 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10028 * "The device server is not capable of supporting a peripheral
10029 * device on this logical unit. For this peripheral qualifier the
10030 * peripheral device type shall be set to 1Fh. All other peripheral
10031 * device type values are reserved for this peripheral qualifier."
10033 * Given the text, it would seem that we probably want to report that
10034 * the LUN is offline here. There is no LUN connected, but we can
10035 * support a LUN at the given LUN number.
10037 * In the real world, though, it sounds like things are a little
10040 * - Linux, when presented with a LUN with the offline peripheral
10041 * qualifier, will create an sg driver instance for it. So when
10042 * you attach it to CTL, you wind up with a ton of sg driver
10043 * instances. (One for every LUN that Linux bothered to probe.)
10044 * Linux does this despite the fact that it issues a REPORT LUNs
10045 * to LUN 0 to get the inventory of supported LUNs.
10047 * - There is other anecdotal evidence (from Emulex folks) about
10048 * arrays that use the offline peripheral qualifier for LUNs that
10049 * are on the "passive" path in an active/passive array.
10051 * So the solution is provide a hopefully reasonable default
10052 * (return bad/no LUN) and allow the user to change the behavior
10053 * with a tunable/sysctl variable.
10056 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10057 lun->be_lun->lun_type;
10058 else if (ctl_softc->inquiry_pq_no_lun == 0)
10059 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10061 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10063 /* RMB in byte 2 is 0 */
10064 inq_ptr->version = SCSI_REV_SPC3;
10067 * According to SAM-3, even if a device only supports a single
10068 * level of LUN addressing, it should still set the HISUP bit:
10070 * 4.9.1 Logical unit numbers overview
10072 * All logical unit number formats described in this standard are
10073 * hierarchical in structure even when only a single level in that
10074 * hierarchy is used. The HISUP bit shall be set to one in the
10075 * standard INQUIRY data (see SPC-2) when any logical unit number
10076 * format described in this standard is used. Non-hierarchical
10077 * formats are outside the scope of this standard.
10079 * Therefore we set the HiSup bit here.
10081 * The reponse format is 2, per SPC-3.
10083 inq_ptr->response_format = SID_HiSup | 2;
10085 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
10086 CTL_DEBUG_PRINT(("additional_length = %d\n",
10087 inq_ptr->additional_length));
10089 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
10090 /* 16 bit addressing */
10092 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10093 /* XXX set the SID_MultiP bit here if we're actually going to
10094 respond on multiple ports */
10095 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10097 /* 16 bit data bus, synchronous transfers */
10098 /* XXX these flags don't apply for FC */
10100 inq_ptr->flags = SID_WBus16 | SID_Sync;
10102 * XXX KDM do we want to support tagged queueing on the control
10106 || (lun->be_lun->lun_type != T_PROCESSOR))
10107 inq_ptr->flags |= SID_CmdQue;
10109 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10110 * We have 8 bytes for the vendor name, and 16 bytes for the device
10111 * name and 4 bytes for the revision.
10113 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10114 "vendor")) == NULL) {
10115 strcpy(inq_ptr->vendor, CTL_VENDOR);
10117 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10118 strncpy(inq_ptr->vendor, val,
10119 min(sizeof(inq_ptr->vendor), strlen(val)));
10122 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10123 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10124 switch (lun->be_lun->lun_type) {
10126 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10129 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10132 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10136 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10137 strncpy(inq_ptr->product, val,
10138 min(sizeof(inq_ptr->product), strlen(val)));
10142 * XXX make this a macro somewhere so it automatically gets
10143 * incremented when we make changes.
10145 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10146 "revision")) == NULL) {
10147 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10149 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10150 strncpy(inq_ptr->revision, val,
10151 min(sizeof(inq_ptr->revision), strlen(val)));
10155 * For parallel SCSI, we support double transition and single
10156 * transition clocking. We also support QAS (Quick Arbitration
10157 * and Selection) and Information Unit transfers on both the
10158 * control and array devices.
10161 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10165 scsi_ulto2b(0x0060, inq_ptr->version1);
10166 /* SPC-3 (no version claimed) XXX should we claim a version? */
10167 scsi_ulto2b(0x0300, inq_ptr->version2);
10169 /* FCP-2 ANSI INCITS.350:2003 */
10170 scsi_ulto2b(0x0917, inq_ptr->version3);
10172 /* SPI-4 ANSI INCITS.362:200x */
10173 scsi_ulto2b(0x0B56, inq_ptr->version3);
10177 /* SBC-2 (no version claimed) XXX should we claim a version? */
10178 scsi_ulto2b(0x0320, inq_ptr->version4);
10180 switch (lun->be_lun->lun_type) {
10183 * SBC-2 (no version claimed) XXX should we claim a
10186 scsi_ulto2b(0x0320, inq_ptr->version4);
10194 ctsio->scsi_status = SCSI_STATUS_OK;
10195 if (ctsio->kern_data_len > 0) {
10196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10197 ctsio->be_move_done = ctl_config_move_done;
10198 ctl_datamove((union ctl_io *)ctsio);
10200 ctsio->io_hdr.status = CTL_SUCCESS;
10201 ctl_done((union ctl_io *)ctsio);
10204 return (CTL_RETVAL_COMPLETE);
10208 ctl_inquiry(struct ctl_scsiio *ctsio)
10210 struct scsi_inquiry *cdb;
10213 cdb = (struct scsi_inquiry *)ctsio->cdb;
10217 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10220 * Right now, we don't support the CmdDt inquiry information.
10221 * This would be nice to support in the future. When we do
10222 * support it, we should change this test so that it checks to make
10223 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10226 if (((cdb->byte2 & SI_EVPD)
10227 && (cdb->byte2 & SI_CMDDT)))
10229 if (cdb->byte2 & SI_CMDDT) {
10231 * Point to the SI_CMDDT bit. We might change this
10232 * when we support SI_CMDDT, but since both bits would be
10233 * "wrong", this should probably just stay as-is then.
10235 ctl_set_invalid_field(ctsio,
10241 ctl_done((union ctl_io *)ctsio);
10242 return (CTL_RETVAL_COMPLETE);
10244 if (cdb->byte2 & SI_EVPD)
10245 retval = ctl_inquiry_evpd(ctsio);
10247 else if (cdb->byte2 & SI_CMDDT)
10248 retval = ctl_inquiry_cmddt(ctsio);
10251 retval = ctl_inquiry_std(ctsio);
10257 * For known CDB types, parse the LBA and length.
10260 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10262 if (io->io_hdr.io_type != CTL_IO_SCSI)
10265 switch (io->scsiio.cdb[0]) {
10266 case COMPARE_AND_WRITE: {
10267 struct scsi_compare_and_write *cdb;
10269 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10271 *lba = scsi_8btou64(cdb->addr);
10272 *len = cdb->length;
10277 struct scsi_rw_6 *cdb;
10279 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10281 *lba = scsi_3btoul(cdb->addr);
10282 /* only 5 bits are valid in the most significant address byte */
10284 *len = cdb->length;
10289 struct scsi_rw_10 *cdb;
10291 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10293 *lba = scsi_4btoul(cdb->addr);
10294 *len = scsi_2btoul(cdb->length);
10297 case WRITE_VERIFY_10: {
10298 struct scsi_write_verify_10 *cdb;
10300 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10302 *lba = scsi_4btoul(cdb->addr);
10303 *len = scsi_2btoul(cdb->length);
10308 struct scsi_rw_12 *cdb;
10310 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10312 *lba = scsi_4btoul(cdb->addr);
10313 *len = scsi_4btoul(cdb->length);
10316 case WRITE_VERIFY_12: {
10317 struct scsi_write_verify_12 *cdb;
10319 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10321 *lba = scsi_4btoul(cdb->addr);
10322 *len = scsi_4btoul(cdb->length);
10327 struct scsi_rw_16 *cdb;
10329 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10331 *lba = scsi_8btou64(cdb->addr);
10332 *len = scsi_4btoul(cdb->length);
10335 case WRITE_VERIFY_16: {
10336 struct scsi_write_verify_16 *cdb;
10338 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10341 *lba = scsi_8btou64(cdb->addr);
10342 *len = scsi_4btoul(cdb->length);
10345 case WRITE_SAME_10: {
10346 struct scsi_write_same_10 *cdb;
10348 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10350 *lba = scsi_4btoul(cdb->addr);
10351 *len = scsi_2btoul(cdb->length);
10354 case WRITE_SAME_16: {
10355 struct scsi_write_same_16 *cdb;
10357 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10359 *lba = scsi_8btou64(cdb->addr);
10360 *len = scsi_4btoul(cdb->length);
10364 struct scsi_verify_10 *cdb;
10366 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10368 *lba = scsi_4btoul(cdb->addr);
10369 *len = scsi_2btoul(cdb->length);
10373 struct scsi_verify_12 *cdb;
10375 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10377 *lba = scsi_4btoul(cdb->addr);
10378 *len = scsi_4btoul(cdb->length);
10382 struct scsi_verify_16 *cdb;
10384 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10386 *lba = scsi_8btou64(cdb->addr);
10387 *len = scsi_4btoul(cdb->length);
10392 break; /* NOTREACHED */
10399 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10401 uint64_t endlba1, endlba2;
10403 endlba1 = lba1 + len1 - 1;
10404 endlba2 = lba2 + len2 - 1;
10406 if ((endlba1 < lba2)
10407 || (endlba2 < lba1))
10408 return (CTL_ACTION_PASS);
10410 return (CTL_ACTION_BLOCK);
10414 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10416 uint64_t lba1, lba2;
10417 uint32_t len1, len2;
10420 retval = ctl_get_lba_len(io1, &lba1, &len1);
10422 return (CTL_ACTION_ERROR);
10424 retval = ctl_get_lba_len(io2, &lba2, &len2);
10426 return (CTL_ACTION_ERROR);
10428 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10432 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10434 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10435 ctl_serialize_action *serialize_row;
10438 * The initiator attempted multiple untagged commands at the same
10439 * time. Can't do that.
10441 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10442 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10443 && ((pending_io->io_hdr.nexus.targ_port ==
10444 ooa_io->io_hdr.nexus.targ_port)
10445 && (pending_io->io_hdr.nexus.initid.id ==
10446 ooa_io->io_hdr.nexus.initid.id))
10447 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10448 return (CTL_ACTION_OVERLAP);
10451 * The initiator attempted to send multiple tagged commands with
10452 * the same ID. (It's fine if different initiators have the same
10455 * Even if all of those conditions are true, we don't kill the I/O
10456 * if the command ahead of us has been aborted. We won't end up
10457 * sending it to the FETD, and it's perfectly legal to resend a
10458 * command with the same tag number as long as the previous
10459 * instance of this tag number has been aborted somehow.
10461 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10462 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10463 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10464 && ((pending_io->io_hdr.nexus.targ_port ==
10465 ooa_io->io_hdr.nexus.targ_port)
10466 && (pending_io->io_hdr.nexus.initid.id ==
10467 ooa_io->io_hdr.nexus.initid.id))
10468 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10469 return (CTL_ACTION_OVERLAP_TAG);
10472 * If we get a head of queue tag, SAM-3 says that we should
10473 * immediately execute it.
10475 * What happens if this command would normally block for some other
10476 * reason? e.g. a request sense with a head of queue tag
10477 * immediately after a write. Normally that would block, but this
10478 * will result in its getting executed immediately...
10480 * We currently return "pass" instead of "skip", so we'll end up
10481 * going through the rest of the queue to check for overlapped tags.
10483 * XXX KDM check for other types of blockage first??
10485 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10486 return (CTL_ACTION_PASS);
10489 * Ordered tags have to block until all items ahead of them
10490 * have completed. If we get called with an ordered tag, we always
10491 * block, if something else is ahead of us in the queue.
10493 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10494 return (CTL_ACTION_BLOCK);
10497 * Simple tags get blocked until all head of queue and ordered tags
10498 * ahead of them have completed. I'm lumping untagged commands in
10499 * with simple tags here. XXX KDM is that the right thing to do?
10501 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10502 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10503 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10504 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10505 return (CTL_ACTION_BLOCK);
10507 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10508 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10510 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10512 switch (serialize_row[pending_entry->seridx]) {
10513 case CTL_SER_BLOCK:
10514 return (CTL_ACTION_BLOCK);
10515 break; /* NOTREACHED */
10516 case CTL_SER_EXTENT:
10517 return (ctl_extent_check(pending_io, ooa_io));
10518 break; /* NOTREACHED */
10520 return (CTL_ACTION_PASS);
10521 break; /* NOTREACHED */
10523 return (CTL_ACTION_SKIP);
10526 panic("invalid serialization value %d",
10527 serialize_row[pending_entry->seridx]);
10528 break; /* NOTREACHED */
10531 return (CTL_ACTION_ERROR);
10535 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10537 * - pending_io is generally either incoming, or on the blocked queue
10538 * - starting I/O is the I/O we want to start the check with.
10541 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10542 union ctl_io *starting_io)
10544 union ctl_io *ooa_io;
10547 mtx_assert(&lun->lun_lock, MA_OWNED);
10550 * Run back along the OOA queue, starting with the current
10551 * blocked I/O and going through every I/O before it on the
10552 * queue. If starting_io is NULL, we'll just end up returning
10555 for (ooa_io = starting_io; ooa_io != NULL;
10556 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10560 * This routine just checks to see whether
10561 * cur_blocked is blocked by ooa_io, which is ahead
10562 * of it in the queue. It doesn't queue/dequeue
10565 action = ctl_check_for_blockage(pending_io, ooa_io);
10567 case CTL_ACTION_BLOCK:
10568 case CTL_ACTION_OVERLAP:
10569 case CTL_ACTION_OVERLAP_TAG:
10570 case CTL_ACTION_SKIP:
10571 case CTL_ACTION_ERROR:
10573 break; /* NOTREACHED */
10574 case CTL_ACTION_PASS:
10577 panic("invalid action %d", action);
10578 break; /* NOTREACHED */
10582 return (CTL_ACTION_PASS);
10587 * - An I/O has just completed, and has been removed from the per-LUN OOA
10588 * queue, so some items on the blocked queue may now be unblocked.
10591 ctl_check_blocked(struct ctl_lun *lun)
10593 union ctl_io *cur_blocked, *next_blocked;
10595 mtx_assert(&lun->lun_lock, MA_OWNED);
10598 * Run forward from the head of the blocked queue, checking each
10599 * entry against the I/Os prior to it on the OOA queue to see if
10600 * there is still any blockage.
10602 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10603 * with our removing a variable on it while it is traversing the
10606 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10607 cur_blocked != NULL; cur_blocked = next_blocked) {
10608 union ctl_io *prev_ooa;
10611 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10614 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10615 ctl_ooaq, ooa_links);
10618 * If cur_blocked happens to be the first item in the OOA
10619 * queue now, prev_ooa will be NULL, and the action
10620 * returned will just be CTL_ACTION_PASS.
10622 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10625 case CTL_ACTION_BLOCK:
10626 /* Nothing to do here, still blocked */
10628 case CTL_ACTION_OVERLAP:
10629 case CTL_ACTION_OVERLAP_TAG:
10631 * This shouldn't happen! In theory we've already
10632 * checked this command for overlap...
10635 case CTL_ACTION_PASS:
10636 case CTL_ACTION_SKIP: {
10637 struct ctl_softc *softc;
10638 const struct ctl_cmd_entry *entry;
10643 * The skip case shouldn't happen, this transaction
10644 * should have never made it onto the blocked queue.
10647 * This I/O is no longer blocked, we can remove it
10648 * from the blocked queue. Since this is a TAILQ
10649 * (doubly linked list), we can do O(1) removals
10650 * from any place on the list.
10652 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10654 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10656 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10658 * Need to send IO back to original side to
10661 union ctl_ha_msg msg_info;
10663 msg_info.hdr.original_sc =
10664 cur_blocked->io_hdr.original_sc;
10665 msg_info.hdr.serializing_sc = cur_blocked;
10666 msg_info.hdr.msg_type = CTL_MSG_R2R;
10667 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10668 &msg_info, sizeof(msg_info), 0)) >
10669 CTL_HA_STATUS_SUCCESS) {
10670 printf("CTL:Check Blocked error from "
10671 "ctl_ha_msg_send %d\n",
10676 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10677 softc = control_softc;
10679 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10682 * Check this I/O for LUN state changes that may
10683 * have happened while this command was blocked.
10684 * The LUN state may have been changed by a command
10685 * ahead of us in the queue, so we need to re-check
10686 * for any states that can be caused by SCSI
10689 if (ctl_scsiio_lun_check(softc, lun, entry,
10690 &cur_blocked->scsiio) == 0) {
10691 cur_blocked->io_hdr.flags |=
10692 CTL_FLAG_IS_WAS_ON_RTR;
10693 ctl_enqueue_rtr(cur_blocked);
10695 ctl_done(cur_blocked);
10700 * This probably shouldn't happen -- we shouldn't
10701 * get CTL_ACTION_ERROR, or anything else.
10707 return (CTL_RETVAL_COMPLETE);
10711 * This routine (with one exception) checks LUN flags that can be set by
10712 * commands ahead of us in the OOA queue. These flags have to be checked
10713 * when a command initially comes in, and when we pull a command off the
10714 * blocked queue and are preparing to execute it. The reason we have to
10715 * check these flags for commands on the blocked queue is that the LUN
10716 * state may have been changed by a command ahead of us while we're on the
10719 * Ordering is somewhat important with these checks, so please pay
10720 * careful attention to the placement of any new checks.
10723 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10724 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10730 mtx_assert(&lun->lun_lock, MA_OWNED);
10733 * If this shelf is a secondary shelf controller, we have to reject
10734 * any media access commands.
10737 /* No longer needed for HA */
10738 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10739 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10740 ctl_set_lun_standby(ctsio);
10747 * Check for a reservation conflict. If this command isn't allowed
10748 * even on reserved LUNs, and if this initiator isn't the one who
10749 * reserved us, reject the command with a reservation conflict.
10751 if ((lun->flags & CTL_LUN_RESERVED)
10752 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10753 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10754 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10755 || (ctsio->io_hdr.nexus.targ_target.id !=
10756 lun->rsv_nexus.targ_target.id)) {
10757 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10758 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10764 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10765 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10768 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10770 * if we aren't registered or it's a res holder type
10771 * reservation and this isn't the res holder then set a
10773 * NOTE: Commands which might be allowed on write exclusive
10774 * type reservations are checked in the particular command
10775 * for a conflict. Read and SSU are the only ones.
10777 if (!lun->per_res[residx].registered
10778 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10779 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10780 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10787 if ((lun->flags & CTL_LUN_OFFLINE)
10788 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10789 ctl_set_lun_not_ready(ctsio);
10795 * If the LUN is stopped, see if this particular command is allowed
10796 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10798 if ((lun->flags & CTL_LUN_STOPPED)
10799 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10800 /* "Logical unit not ready, initializing cmd. required" */
10801 ctl_set_lun_stopped(ctsio);
10806 if ((lun->flags & CTL_LUN_INOPERABLE)
10807 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10808 /* "Medium format corrupted" */
10809 ctl_set_medium_format_corrupted(ctsio);
10820 ctl_failover_io(union ctl_io *io, int have_lock)
10822 ctl_set_busy(&io->scsiio);
10829 struct ctl_lun *lun;
10830 struct ctl_softc *ctl_softc;
10831 union ctl_io *next_io, *pending_io;
10836 ctl_softc = control_softc;
10838 mtx_lock(&ctl_softc->ctl_lock);
10840 * Remove any cmds from the other SC from the rtr queue. These
10841 * will obviously only be for LUNs for which we're the primary.
10842 * We can't send status or get/send data for these commands.
10843 * Since they haven't been executed yet, we can just remove them.
10844 * We'll either abort them or delete them below, depending on
10845 * which HA mode we're in.
10848 mtx_lock(&ctl_softc->queue_lock);
10849 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10850 io != NULL; io = next_io) {
10851 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10852 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10853 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10854 ctl_io_hdr, links);
10856 mtx_unlock(&ctl_softc->queue_lock);
10859 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10860 lun = ctl_softc->ctl_luns[lun_idx];
10865 * Processor LUNs are primary on both sides.
10866 * XXX will this always be true?
10868 if (lun->be_lun->lun_type == T_PROCESSOR)
10871 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10872 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10873 printf("FAILOVER: primary lun %d\n", lun_idx);
10875 * Remove all commands from the other SC. First from the
10876 * blocked queue then from the ooa queue. Once we have
10877 * removed them. Call ctl_check_blocked to see if there
10878 * is anything that can run.
10880 for (io = (union ctl_io *)TAILQ_FIRST(
10881 &lun->blocked_queue); io != NULL; io = next_io) {
10883 next_io = (union ctl_io *)TAILQ_NEXT(
10884 &io->io_hdr, blocked_links);
10886 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10887 TAILQ_REMOVE(&lun->blocked_queue,
10888 &io->io_hdr,blocked_links);
10889 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10890 TAILQ_REMOVE(&lun->ooa_queue,
10891 &io->io_hdr, ooa_links);
10897 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10898 io != NULL; io = next_io) {
10900 next_io = (union ctl_io *)TAILQ_NEXT(
10901 &io->io_hdr, ooa_links);
10903 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10905 TAILQ_REMOVE(&lun->ooa_queue,
10912 ctl_check_blocked(lun);
10913 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10914 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10916 printf("FAILOVER: primary lun %d\n", lun_idx);
10918 * Abort all commands from the other SC. We can't
10919 * send status back for them now. These should get
10920 * cleaned up when they are completed or come out
10921 * for a datamove operation.
10923 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10924 io != NULL; io = next_io) {
10925 next_io = (union ctl_io *)TAILQ_NEXT(
10926 &io->io_hdr, ooa_links);
10928 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10929 io->io_hdr.flags |= CTL_FLAG_ABORT;
10931 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10932 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10934 printf("FAILOVER: secondary lun %d\n", lun_idx);
10936 lun->flags |= CTL_LUN_PRIMARY_SC;
10939 * We send all I/O that was sent to this controller
10940 * and redirected to the other side back with
10941 * busy status, and have the initiator retry it.
10942 * Figuring out how much data has been transferred,
10943 * etc. and picking up where we left off would be
10946 * XXX KDM need to remove I/O from the blocked
10949 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10950 &lun->ooa_queue); pending_io != NULL;
10951 pending_io = next_io) {
10953 next_io = (union ctl_io *)TAILQ_NEXT(
10954 &pending_io->io_hdr, ooa_links);
10956 pending_io->io_hdr.flags &=
10957 ~CTL_FLAG_SENT_2OTHER_SC;
10959 if (pending_io->io_hdr.flags &
10960 CTL_FLAG_IO_ACTIVE) {
10961 pending_io->io_hdr.flags |=
10964 ctl_set_busy(&pending_io->scsiio);
10965 ctl_done(pending_io);
10970 * Build Unit Attention
10972 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10973 lun->pending_sense[i].ua_pending |=
10974 CTL_UA_ASYM_ACC_CHANGE;
10976 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10977 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10978 printf("FAILOVER: secondary lun %d\n", lun_idx);
10980 * if the first io on the OOA is not on the RtR queue
10983 lun->flags |= CTL_LUN_PRIMARY_SC;
10985 pending_io = (union ctl_io *)TAILQ_FIRST(
10987 if (pending_io==NULL) {
10988 printf("Nothing on OOA queue\n");
10992 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10993 if ((pending_io->io_hdr.flags &
10994 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10995 pending_io->io_hdr.flags |=
10996 CTL_FLAG_IS_WAS_ON_RTR;
10997 ctl_enqueue_rtr(pending_io);
11002 printf("Tag 0x%04x is running\n",
11003 pending_io->scsiio.tag_num);
11007 next_io = (union ctl_io *)TAILQ_NEXT(
11008 &pending_io->io_hdr, ooa_links);
11009 for (pending_io=next_io; pending_io != NULL;
11010 pending_io = next_io) {
11011 pending_io->io_hdr.flags &=
11012 ~CTL_FLAG_SENT_2OTHER_SC;
11013 next_io = (union ctl_io *)TAILQ_NEXT(
11014 &pending_io->io_hdr, ooa_links);
11015 if (pending_io->io_hdr.flags &
11016 CTL_FLAG_IS_WAS_ON_RTR) {
11018 printf("Tag 0x%04x is running\n",
11019 pending_io->scsiio.tag_num);
11024 switch (ctl_check_ooa(lun, pending_io,
11025 (union ctl_io *)TAILQ_PREV(
11026 &pending_io->io_hdr, ctl_ooaq,
11029 case CTL_ACTION_BLOCK:
11030 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11031 &pending_io->io_hdr,
11033 pending_io->io_hdr.flags |=
11036 case CTL_ACTION_PASS:
11037 case CTL_ACTION_SKIP:
11038 pending_io->io_hdr.flags |=
11039 CTL_FLAG_IS_WAS_ON_RTR;
11040 ctl_enqueue_rtr(pending_io);
11042 case CTL_ACTION_OVERLAP:
11043 ctl_set_overlapped_cmd(
11044 (struct ctl_scsiio *)pending_io);
11045 ctl_done(pending_io);
11047 case CTL_ACTION_OVERLAP_TAG:
11048 ctl_set_overlapped_tag(
11049 (struct ctl_scsiio *)pending_io,
11050 pending_io->scsiio.tag_num & 0xff);
11051 ctl_done(pending_io);
11053 case CTL_ACTION_ERROR:
11055 ctl_set_internal_failure(
11056 (struct ctl_scsiio *)pending_io,
11059 ctl_done(pending_io);
11065 * Build Unit Attention
11067 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11068 lun->pending_sense[i].ua_pending |=
11069 CTL_UA_ASYM_ACC_CHANGE;
11072 panic("Unhandled HA mode failover, LUN flags = %#x, "
11073 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11077 mtx_unlock(&ctl_softc->ctl_lock);
11081 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11083 struct ctl_lun *lun;
11084 const struct ctl_cmd_entry *entry;
11085 uint32_t initidx, targ_lun;
11092 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11093 if ((targ_lun < CTL_MAX_LUNS)
11094 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11095 lun = ctl_softc->ctl_luns[targ_lun];
11097 * If the LUN is invalid, pretend that it doesn't exist.
11098 * It will go away as soon as all pending I/O has been
11101 if (lun->flags & CTL_LUN_DISABLED) {
11104 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11105 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11107 if (lun->be_lun->lun_type == T_PROCESSOR) {
11108 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11112 * Every I/O goes into the OOA queue for a
11113 * particular LUN, and stays there until completion.
11115 mtx_lock(&lun->lun_lock);
11116 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11120 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11121 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11124 /* Get command entry and return error if it is unsuppotyed. */
11125 entry = ctl_validate_command(ctsio);
11126 if (entry == NULL) {
11128 mtx_unlock(&lun->lun_lock);
11132 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11133 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11136 * Check to see whether we can send this command to LUNs that don't
11137 * exist. This should pretty much only be the case for inquiry
11138 * and request sense. Further checks, below, really require having
11139 * a LUN, so we can't really check the command anymore. Just put
11140 * it on the rtr queue.
11143 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11144 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11145 ctl_enqueue_rtr((union ctl_io *)ctsio);
11149 ctl_set_unsupported_lun(ctsio);
11150 ctl_done((union ctl_io *)ctsio);
11151 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11155 * Make sure we support this particular command on this LUN.
11156 * e.g., we don't support writes to the control LUN.
11158 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11159 mtx_unlock(&lun->lun_lock);
11160 ctl_set_invalid_opcode(ctsio);
11161 ctl_done((union ctl_io *)ctsio);
11166 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11169 * If we've got a request sense, it'll clear the contingent
11170 * allegiance condition. Otherwise, if we have a CA condition for
11171 * this initiator, clear it, because it sent down a command other
11172 * than request sense.
11174 if ((ctsio->cdb[0] != REQUEST_SENSE)
11175 && (ctl_is_set(lun->have_ca, initidx)))
11176 ctl_clear_mask(lun->have_ca, initidx);
11179 * If the command has this flag set, it handles its own unit
11180 * attention reporting, we shouldn't do anything. Otherwise we
11181 * check for any pending unit attentions, and send them back to the
11182 * initiator. We only do this when a command initially comes in,
11183 * not when we pull it off the blocked queue.
11185 * According to SAM-3, section 5.3.2, the order that things get
11186 * presented back to the host is basically unit attentions caused
11187 * by some sort of reset event, busy status, reservation conflicts
11188 * or task set full, and finally any other status.
11190 * One issue here is that some of the unit attentions we report
11191 * don't fall into the "reset" category (e.g. "reported luns data
11192 * has changed"). So reporting it here, before the reservation
11193 * check, may be technically wrong. I guess the only thing to do
11194 * would be to check for and report the reset events here, and then
11195 * check for the other unit attention types after we check for a
11196 * reservation conflict.
11198 * XXX KDM need to fix this
11200 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11201 ctl_ua_type ua_type;
11203 ua_type = lun->pending_sense[initidx].ua_pending;
11204 if (ua_type != CTL_UA_NONE) {
11205 scsi_sense_data_type sense_format;
11208 sense_format = (lun->flags &
11209 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11212 sense_format = SSD_TYPE_FIXED;
11214 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11216 if (ua_type != CTL_UA_NONE) {
11217 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11218 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11220 ctsio->sense_len = SSD_FULL_SIZE;
11221 lun->pending_sense[initidx].ua_pending &=
11223 mtx_unlock(&lun->lun_lock);
11224 ctl_done((union ctl_io *)ctsio);
11231 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11232 mtx_unlock(&lun->lun_lock);
11233 ctl_done((union ctl_io *)ctsio);
11238 * XXX CHD this is where we want to send IO to other side if
11239 * this LUN is secondary on this SC. We will need to make a copy
11240 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11241 * the copy we send as FROM_OTHER.
11242 * We also need to stuff the address of the original IO so we can
11243 * find it easily. Something similar will need be done on the other
11244 * side so when we are done we can find the copy.
11246 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11247 union ctl_ha_msg msg_info;
11250 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11252 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11253 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11255 printf("1. ctsio %p\n", ctsio);
11257 msg_info.hdr.serializing_sc = NULL;
11258 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11259 msg_info.scsi.tag_num = ctsio->tag_num;
11260 msg_info.scsi.tag_type = ctsio->tag_type;
11261 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11263 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11265 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11266 (void *)&msg_info, sizeof(msg_info), 0)) >
11267 CTL_HA_STATUS_SUCCESS) {
11268 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11270 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11273 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11278 * XXX KDM this I/O is off the incoming queue, but hasn't
11279 * been inserted on any other queue. We may need to come
11280 * up with a holding queue while we wait for serialization
11281 * so that we have an idea of what we're waiting for from
11284 mtx_unlock(&lun->lun_lock);
11288 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11289 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11290 ctl_ooaq, ooa_links))) {
11291 case CTL_ACTION_BLOCK:
11292 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11293 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11295 mtx_unlock(&lun->lun_lock);
11297 case CTL_ACTION_PASS:
11298 case CTL_ACTION_SKIP:
11299 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11300 mtx_unlock(&lun->lun_lock);
11301 ctl_enqueue_rtr((union ctl_io *)ctsio);
11303 case CTL_ACTION_OVERLAP:
11304 mtx_unlock(&lun->lun_lock);
11305 ctl_set_overlapped_cmd(ctsio);
11306 ctl_done((union ctl_io *)ctsio);
11308 case CTL_ACTION_OVERLAP_TAG:
11309 mtx_unlock(&lun->lun_lock);
11310 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11311 ctl_done((union ctl_io *)ctsio);
11313 case CTL_ACTION_ERROR:
11315 mtx_unlock(&lun->lun_lock);
11316 ctl_set_internal_failure(ctsio,
11318 /*retry_count*/ 0);
11319 ctl_done((union ctl_io *)ctsio);
11325 const struct ctl_cmd_entry *
11326 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11328 const struct ctl_cmd_entry *entry;
11329 int service_action;
11331 entry = &ctl_cmd_table[ctsio->cdb[0]];
11332 if (entry->flags & CTL_CMD_FLAG_SA5) {
11333 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11334 entry = &((const struct ctl_cmd_entry *)
11335 entry->execute)[service_action];
11340 const struct ctl_cmd_entry *
11341 ctl_validate_command(struct ctl_scsiio *ctsio)
11343 const struct ctl_cmd_entry *entry;
11347 entry = ctl_get_cmd_entry(ctsio);
11348 if (entry->execute == NULL) {
11349 ctl_set_invalid_opcode(ctsio);
11350 ctl_done((union ctl_io *)ctsio);
11353 KASSERT(entry->length > 0,
11354 ("Not defined length for command 0x%02x/0x%02x",
11355 ctsio->cdb[0], ctsio->cdb[1]));
11356 for (i = 1; i < entry->length; i++) {
11357 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11360 ctl_set_invalid_field(ctsio,
11365 /*bit*/ fls(diff) - 1);
11366 ctl_done((union ctl_io *)ctsio);
11373 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11376 switch (lun_type) {
11378 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11379 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11383 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11384 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11394 ctl_scsiio(struct ctl_scsiio *ctsio)
11397 const struct ctl_cmd_entry *entry;
11399 retval = CTL_RETVAL_COMPLETE;
11401 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11403 entry = ctl_get_cmd_entry(ctsio);
11406 * If this I/O has been aborted, just send it straight to
11407 * ctl_done() without executing it.
11409 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11410 ctl_done((union ctl_io *)ctsio);
11415 * All the checks should have been handled by ctl_scsiio_precheck().
11416 * We should be clear now to just execute the I/O.
11418 retval = entry->execute(ctsio);
11425 * Since we only implement one target right now, a bus reset simply resets
11426 * our single target.
11429 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11431 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11435 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11436 ctl_ua_type ua_type)
11438 struct ctl_lun *lun;
11441 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11442 union ctl_ha_msg msg_info;
11444 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11445 msg_info.hdr.nexus = io->io_hdr.nexus;
11446 if (ua_type==CTL_UA_TARG_RESET)
11447 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11449 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11450 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11451 msg_info.hdr.original_sc = NULL;
11452 msg_info.hdr.serializing_sc = NULL;
11453 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11454 (void *)&msg_info, sizeof(msg_info), 0)) {
11459 mtx_lock(&ctl_softc->ctl_lock);
11460 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11461 retval += ctl_lun_reset(lun, io, ua_type);
11462 mtx_unlock(&ctl_softc->ctl_lock);
11468 * The LUN should always be set. The I/O is optional, and is used to
11469 * distinguish between I/Os sent by this initiator, and by other
11470 * initiators. We set unit attention for initiators other than this one.
11471 * SAM-3 is vague on this point. It does say that a unit attention should
11472 * be established for other initiators when a LUN is reset (see section
11473 * 5.7.3), but it doesn't specifically say that the unit attention should
11474 * be established for this particular initiator when a LUN is reset. Here
11475 * is the relevant text, from SAM-3 rev 8:
11477 * 5.7.2 When a SCSI initiator port aborts its own tasks
11479 * When a SCSI initiator port causes its own task(s) to be aborted, no
11480 * notification that the task(s) have been aborted shall be returned to
11481 * the SCSI initiator port other than the completion response for the
11482 * command or task management function action that caused the task(s) to
11483 * be aborted and notification(s) associated with related effects of the
11484 * action (e.g., a reset unit attention condition).
11486 * XXX KDM for now, we're setting unit attention for all initiators.
11489 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11493 uint32_t initindex;
11497 mtx_lock(&lun->lun_lock);
11499 * Run through the OOA queue and abort each I/O.
11502 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11504 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11505 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11506 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11510 * This version sets unit attention for every
11513 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11514 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11515 if (initindex == i)
11517 lun->pending_sense[i].ua_pending |= ua_type;
11522 * A reset (any kind, really) clears reservations established with
11523 * RESERVE/RELEASE. It does not clear reservations established
11524 * with PERSISTENT RESERVE OUT, but we don't support that at the
11525 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11526 * reservations made with the RESERVE/RELEASE commands, because
11527 * those commands are obsolete in SPC-3.
11529 lun->flags &= ~CTL_LUN_RESERVED;
11531 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11532 ctl_clear_mask(lun->have_ca, i);
11533 lun->pending_sense[i].ua_pending |= ua_type;
11535 mtx_unlock(&lun->lun_lock);
11541 ctl_abort_task(union ctl_io *io)
11544 struct ctl_lun *lun;
11545 struct ctl_softc *ctl_softc;
11548 char printbuf[128];
11553 ctl_softc = control_softc;
11559 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11560 mtx_lock(&ctl_softc->ctl_lock);
11561 if ((targ_lun < CTL_MAX_LUNS)
11562 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11563 lun = ctl_softc->ctl_luns[targ_lun];
11565 mtx_unlock(&ctl_softc->ctl_lock);
11570 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11571 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11574 mtx_lock(&lun->lun_lock);
11575 mtx_unlock(&ctl_softc->ctl_lock);
11577 * Run through the OOA queue and attempt to find the given I/O.
11578 * The target port, initiator ID, tag type and tag number have to
11579 * match the values that we got from the initiator. If we have an
11580 * untagged command to abort, simply abort the first untagged command
11581 * we come to. We only allow one untagged command at a time of course.
11584 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11586 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11587 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11589 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11591 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11592 lun->lun, xio->scsiio.tag_num,
11593 xio->scsiio.tag_type,
11594 (xio->io_hdr.blocked_links.tqe_prev
11595 == NULL) ? "" : " BLOCKED",
11596 (xio->io_hdr.flags &
11597 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11598 (xio->io_hdr.flags &
11599 CTL_FLAG_ABORT) ? " ABORT" : "",
11600 (xio->io_hdr.flags &
11601 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11602 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11604 printf("%s\n", sbuf_data(&sb));
11607 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11608 && (xio->io_hdr.nexus.initid.id ==
11609 io->io_hdr.nexus.initid.id)) {
11611 * If the abort says that the task is untagged, the
11612 * task in the queue must be untagged. Otherwise,
11613 * we just check to see whether the tag numbers
11614 * match. This is because the QLogic firmware
11615 * doesn't pass back the tag type in an abort
11619 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11620 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11621 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11624 * XXX KDM we've got problems with FC, because it
11625 * doesn't send down a tag type with aborts. So we
11626 * can only really go by the tag number...
11627 * This may cause problems with parallel SCSI.
11628 * Need to figure that out!!
11630 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11631 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11633 if ((io->io_hdr.flags &
11634 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11635 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11636 union ctl_ha_msg msg_info;
11638 io->io_hdr.flags |=
11639 CTL_FLAG_SENT_2OTHER_SC;
11640 msg_info.hdr.nexus = io->io_hdr.nexus;
11641 msg_info.task.task_action =
11642 CTL_TASK_ABORT_TASK;
11643 msg_info.task.tag_num =
11644 io->taskio.tag_num;
11645 msg_info.task.tag_type =
11646 io->taskio.tag_type;
11647 msg_info.hdr.msg_type =
11648 CTL_MSG_MANAGE_TASKS;
11649 msg_info.hdr.original_sc = NULL;
11650 msg_info.hdr.serializing_sc = NULL;
11652 printf("Sent Abort to other side\n");
11654 if (CTL_HA_STATUS_SUCCESS !=
11655 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11657 sizeof(msg_info), 0)) {
11661 printf("ctl_abort_task: found I/O to abort\n");
11667 mtx_unlock(&lun->lun_lock);
11673 * This isn't really an error. It's entirely possible for
11674 * the abort and command completion to cross on the wire.
11675 * This is more of an informative/diagnostic error.
11678 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11679 "%d:%d:%d:%d tag %d type %d\n",
11680 io->io_hdr.nexus.initid.id,
11681 io->io_hdr.nexus.targ_port,
11682 io->io_hdr.nexus.targ_target.id,
11683 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11684 io->taskio.tag_type);
11692 ctl_run_task(union ctl_io *io)
11694 struct ctl_softc *ctl_softc;
11696 const char *task_desc;
11698 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11700 ctl_softc = control_softc;
11703 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11704 ("ctl_run_task: Unextected io_type %d\n",
11705 io->io_hdr.io_type));
11707 task_desc = ctl_scsi_task_string(&io->taskio);
11708 if (task_desc != NULL) {
11710 csevent_log(CSC_CTL | CSC_SHELF_SW |
11712 csevent_LogType_Trace,
11713 csevent_Severity_Information,
11714 csevent_AlertLevel_Green,
11715 csevent_FRU_Firmware,
11716 csevent_FRU_Unknown,
11717 "CTL: received task: %s",task_desc);
11721 csevent_log(CSC_CTL | CSC_SHELF_SW |
11723 csevent_LogType_Trace,
11724 csevent_Severity_Information,
11725 csevent_AlertLevel_Green,
11726 csevent_FRU_Firmware,
11727 csevent_FRU_Unknown,
11728 "CTL: received unknown task "
11730 io->taskio.task_action,
11731 io->taskio.task_action);
11734 switch (io->taskio.task_action) {
11735 case CTL_TASK_ABORT_TASK:
11736 retval = ctl_abort_task(io);
11738 case CTL_TASK_ABORT_TASK_SET:
11740 case CTL_TASK_CLEAR_ACA:
11742 case CTL_TASK_CLEAR_TASK_SET:
11744 case CTL_TASK_LUN_RESET: {
11745 struct ctl_lun *lun;
11749 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11750 mtx_lock(&ctl_softc->ctl_lock);
11751 if ((targ_lun < CTL_MAX_LUNS)
11752 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11753 lun = ctl_softc->ctl_luns[targ_lun];
11755 mtx_unlock(&ctl_softc->ctl_lock);
11760 if (!(io->io_hdr.flags &
11761 CTL_FLAG_FROM_OTHER_SC)) {
11762 union ctl_ha_msg msg_info;
11764 io->io_hdr.flags |=
11765 CTL_FLAG_SENT_2OTHER_SC;
11766 msg_info.hdr.msg_type =
11767 CTL_MSG_MANAGE_TASKS;
11768 msg_info.hdr.nexus = io->io_hdr.nexus;
11769 msg_info.task.task_action =
11770 CTL_TASK_LUN_RESET;
11771 msg_info.hdr.original_sc = NULL;
11772 msg_info.hdr.serializing_sc = NULL;
11773 if (CTL_HA_STATUS_SUCCESS !=
11774 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11776 sizeof(msg_info), 0)) {
11780 retval = ctl_lun_reset(lun, io,
11782 mtx_unlock(&ctl_softc->ctl_lock);
11785 case CTL_TASK_TARGET_RESET:
11786 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
11788 case CTL_TASK_BUS_RESET:
11789 retval = ctl_bus_reset(ctl_softc, io);
11791 case CTL_TASK_PORT_LOGIN:
11793 case CTL_TASK_PORT_LOGOUT:
11796 printf("ctl_run_task: got unknown task management event %d\n",
11797 io->taskio.task_action);
11801 io->io_hdr.status = CTL_SUCCESS;
11803 io->io_hdr.status = CTL_ERROR;
11806 * This will queue this I/O to the done queue, but the
11807 * work thread won't be able to process it until we
11808 * return and the lock is released.
11814 * For HA operation. Handle commands that come in from the other
11818 ctl_handle_isc(union ctl_io *io)
11821 struct ctl_lun *lun;
11822 struct ctl_softc *ctl_softc;
11825 ctl_softc = control_softc;
11827 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11828 lun = ctl_softc->ctl_luns[targ_lun];
11830 switch (io->io_hdr.msg_type) {
11831 case CTL_MSG_SERIALIZE:
11832 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11834 case CTL_MSG_R2R: {
11835 const struct ctl_cmd_entry *entry;
11838 * This is only used in SER_ONLY mode.
11841 entry = ctl_get_cmd_entry(&io->scsiio);
11842 mtx_lock(&lun->lun_lock);
11843 if (ctl_scsiio_lun_check(ctl_softc, lun,
11844 entry, (struct ctl_scsiio *)io) != 0) {
11845 mtx_unlock(&lun->lun_lock);
11849 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11850 mtx_unlock(&lun->lun_lock);
11851 ctl_enqueue_rtr(io);
11854 case CTL_MSG_FINISH_IO:
11855 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11860 mtx_lock(&lun->lun_lock);
11861 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11863 ctl_check_blocked(lun);
11864 mtx_unlock(&lun->lun_lock);
11867 case CTL_MSG_PERS_ACTION:
11868 ctl_hndl_per_res_out_on_other_sc(
11869 (union ctl_ha_msg *)&io->presio.pr_msg);
11872 case CTL_MSG_BAD_JUJU:
11876 case CTL_MSG_DATAMOVE:
11877 /* Only used in XFER mode */
11879 ctl_datamove_remote(io);
11881 case CTL_MSG_DATAMOVE_DONE:
11882 /* Only used in XFER mode */
11884 io->scsiio.be_move_done(io);
11888 printf("%s: Invalid message type %d\n",
11889 __func__, io->io_hdr.msg_type);
11899 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11900 * there is no match.
11902 static ctl_lun_error_pattern
11903 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11905 const struct ctl_cmd_entry *entry;
11906 ctl_lun_error_pattern filtered_pattern, pattern;
11908 pattern = desc->error_pattern;
11911 * XXX KDM we need more data passed into this function to match a
11912 * custom pattern, and we actually need to implement custom pattern
11915 if (pattern & CTL_LUN_PAT_CMD)
11916 return (CTL_LUN_PAT_CMD);
11918 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11919 return (CTL_LUN_PAT_ANY);
11921 entry = ctl_get_cmd_entry(ctsio);
11923 filtered_pattern = entry->pattern & pattern;
11926 * If the user requested specific flags in the pattern (e.g.
11927 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11930 * If the user did not specify any flags, it doesn't matter whether
11931 * or not the command supports the flags.
11933 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11934 (pattern & ~CTL_LUN_PAT_MASK))
11935 return (CTL_LUN_PAT_NONE);
11938 * If the user asked for a range check, see if the requested LBA
11939 * range overlaps with this command's LBA range.
11941 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11947 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11949 return (CTL_LUN_PAT_NONE);
11951 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11952 desc->lba_range.len);
11954 * A "pass" means that the LBA ranges don't overlap, so
11955 * this doesn't match the user's range criteria.
11957 if (action == CTL_ACTION_PASS)
11958 return (CTL_LUN_PAT_NONE);
11961 return (filtered_pattern);
11965 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11967 struct ctl_error_desc *desc, *desc2;
11969 mtx_assert(&lun->lun_lock, MA_OWNED);
11971 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11972 ctl_lun_error_pattern pattern;
11974 * Check to see whether this particular command matches
11975 * the pattern in the descriptor.
11977 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11978 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11981 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11982 case CTL_LUN_INJ_ABORTED:
11983 ctl_set_aborted(&io->scsiio);
11985 case CTL_LUN_INJ_MEDIUM_ERR:
11986 ctl_set_medium_error(&io->scsiio);
11988 case CTL_LUN_INJ_UA:
11989 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11991 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11993 case CTL_LUN_INJ_CUSTOM:
11995 * We're assuming the user knows what he is doing.
11996 * Just copy the sense information without doing
11999 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12000 ctl_min(sizeof(desc->custom_sense),
12001 sizeof(io->scsiio.sense_data)));
12002 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12003 io->scsiio.sense_len = SSD_FULL_SIZE;
12004 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12006 case CTL_LUN_INJ_NONE:
12009 * If this is an error injection type we don't know
12010 * about, clear the continuous flag (if it is set)
12011 * so it will get deleted below.
12013 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12017 * By default, each error injection action is a one-shot
12019 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12022 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12028 #ifdef CTL_IO_DELAY
12030 ctl_datamove_timer_wakeup(void *arg)
12034 io = (union ctl_io *)arg;
12038 #endif /* CTL_IO_DELAY */
12041 ctl_datamove(union ctl_io *io)
12043 void (*fe_datamove)(union ctl_io *io);
12045 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12047 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12050 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12055 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12056 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12058 sbuf_cat(&sb, path_str);
12059 switch (io->io_hdr.io_type) {
12061 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12062 sbuf_printf(&sb, "\n");
12063 sbuf_cat(&sb, path_str);
12064 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12065 io->scsiio.tag_num, io->scsiio.tag_type);
12068 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12069 "Tag Type: %d\n", io->taskio.task_action,
12070 io->taskio.tag_num, io->taskio.tag_type);
12073 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12074 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12077 sbuf_cat(&sb, path_str);
12078 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12079 (intmax_t)time_uptime - io->io_hdr.start_time);
12081 printf("%s", sbuf_data(&sb));
12083 #endif /* CTL_TIME_IO */
12085 #ifdef CTL_IO_DELAY
12086 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12087 struct ctl_lun *lun;
12089 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12091 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12093 struct ctl_lun *lun;
12095 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12097 && (lun->delay_info.datamove_delay > 0)) {
12098 struct callout *callout;
12100 callout = (struct callout *)&io->io_hdr.timer_bytes;
12101 callout_init(callout, /*mpsafe*/ 1);
12102 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12103 callout_reset(callout,
12104 lun->delay_info.datamove_delay * hz,
12105 ctl_datamove_timer_wakeup, io);
12106 if (lun->delay_info.datamove_type ==
12107 CTL_DELAY_TYPE_ONESHOT)
12108 lun->delay_info.datamove_delay = 0;
12115 * This command has been aborted. Set the port status, so we fail
12118 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12119 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12120 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12121 io->io_hdr.nexus.targ_port,
12122 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12123 io->io_hdr.nexus.targ_lun);
12124 io->io_hdr.status = CTL_CMD_ABORTED;
12125 io->io_hdr.port_status = 31337;
12127 * Note that the backend, in this case, will get the
12128 * callback in its context. In other cases it may get
12129 * called in the frontend's interrupt thread context.
12131 io->scsiio.be_move_done(io);
12136 * If we're in XFER mode and this I/O is from the other shelf
12137 * controller, we need to send the DMA to the other side to
12138 * actually transfer the data to/from the host. In serialize only
12139 * mode the transfer happens below CTL and ctl_datamove() is only
12140 * called on the machine that originally received the I/O.
12142 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12143 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12144 union ctl_ha_msg msg;
12145 uint32_t sg_entries_sent;
12149 memset(&msg, 0, sizeof(msg));
12150 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12151 msg.hdr.original_sc = io->io_hdr.original_sc;
12152 msg.hdr.serializing_sc = io;
12153 msg.hdr.nexus = io->io_hdr.nexus;
12154 msg.dt.flags = io->io_hdr.flags;
12156 * We convert everything into a S/G list here. We can't
12157 * pass by reference, only by value between controllers.
12158 * So we can't pass a pointer to the S/G list, only as many
12159 * S/G entries as we can fit in here. If it's possible for
12160 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12161 * then we need to break this up into multiple transfers.
12163 if (io->scsiio.kern_sg_entries == 0) {
12164 msg.dt.kern_sg_entries = 1;
12166 * If this is in cached memory, flush the cache
12167 * before we send the DMA request to the other
12168 * controller. We want to do this in either the
12169 * read or the write case. The read case is
12170 * straightforward. In the write case, we want to
12171 * make sure nothing is in the local cache that
12172 * could overwrite the DMAed data.
12174 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12176 * XXX KDM use bus_dmamap_sync() here.
12181 * Convert to a physical address if this is a
12184 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12185 msg.dt.sg_list[0].addr =
12186 io->scsiio.kern_data_ptr;
12189 * XXX KDM use busdma here!
12192 msg.dt.sg_list[0].addr = (void *)
12193 vtophys(io->scsiio.kern_data_ptr);
12197 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12200 struct ctl_sg_entry *sgl;
12203 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12204 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12205 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12207 * XXX KDM use bus_dmamap_sync() here.
12212 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12213 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12214 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12215 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12216 msg.dt.sg_sequence = 0;
12219 * Loop until we've sent all of the S/G entries. On the
12220 * other end, we'll recompose these S/G entries into one
12221 * contiguous list before passing it to the
12223 for (sg_entries_sent = 0; sg_entries_sent <
12224 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12225 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12226 sizeof(msg.dt.sg_list[0])),
12227 msg.dt.kern_sg_entries - sg_entries_sent);
12229 if (do_sg_copy != 0) {
12230 struct ctl_sg_entry *sgl;
12233 sgl = (struct ctl_sg_entry *)
12234 io->scsiio.kern_data_ptr;
12236 * If this is in cached memory, flush the cache
12237 * before we send the DMA request to the other
12238 * controller. We want to do this in either
12239 * the * read or the write case. The read
12240 * case is straightforward. In the write
12241 * case, we want to make sure nothing is
12242 * in the local cache that could overwrite
12246 for (i = sg_entries_sent, j = 0;
12247 i < msg.dt.cur_sg_entries; i++, j++) {
12248 if ((io->io_hdr.flags &
12249 CTL_FLAG_NO_DATASYNC) == 0) {
12251 * XXX KDM use bus_dmamap_sync()
12254 if ((io->io_hdr.flags &
12255 CTL_FLAG_BUS_ADDR) == 0) {
12257 * XXX KDM use busdma.
12260 msg.dt.sg_list[j].addr =(void *)
12261 vtophys(sgl[i].addr);
12264 msg.dt.sg_list[j].addr =
12267 msg.dt.sg_list[j].len = sgl[i].len;
12271 sg_entries_sent += msg.dt.cur_sg_entries;
12272 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12273 msg.dt.sg_last = 1;
12275 msg.dt.sg_last = 0;
12278 * XXX KDM drop and reacquire the lock here?
12280 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12281 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12283 * XXX do something here.
12287 msg.dt.sent_sg_entries = sg_entries_sent;
12289 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12290 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12291 ctl_failover_io(io, /*have_lock*/ 0);
12296 * Lookup the fe_datamove() function for this particular
12300 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12307 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12309 union ctl_ha_msg msg;
12312 memset(&msg, 0, sizeof(msg));
12314 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12315 msg.hdr.original_sc = io;
12316 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12317 msg.hdr.nexus = io->io_hdr.nexus;
12318 msg.hdr.status = io->io_hdr.status;
12319 msg.scsi.tag_num = io->scsiio.tag_num;
12320 msg.scsi.tag_type = io->scsiio.tag_type;
12321 msg.scsi.scsi_status = io->scsiio.scsi_status;
12322 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12323 sizeof(io->scsiio.sense_data));
12324 msg.scsi.sense_len = io->scsiio.sense_len;
12325 msg.scsi.sense_residual = io->scsiio.sense_residual;
12326 msg.scsi.fetd_status = io->io_hdr.port_status;
12327 msg.scsi.residual = io->scsiio.residual;
12328 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12330 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12331 ctl_failover_io(io, /*have_lock*/ have_lock);
12335 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12336 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12337 /* XXX do something if this fails */
12343 * The DMA to the remote side is done, now we need to tell the other side
12344 * we're done so it can continue with its data movement.
12347 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12353 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12354 printf("%s: ISC DMA write failed with error %d", __func__,
12356 ctl_set_internal_failure(&io->scsiio,
12358 /*retry_count*/ rq->ret);
12361 ctl_dt_req_free(rq);
12364 * In this case, we had to malloc the memory locally. Free it.
12366 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12368 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12369 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12372 * The data is in local and remote memory, so now we need to send
12373 * status (good or back) back to the other side.
12375 ctl_send_datamove_done(io, /*have_lock*/ 0);
12379 * We've moved the data from the host/controller into local memory. Now we
12380 * need to push it over to the remote controller's memory.
12383 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12389 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12390 ctl_datamove_remote_write_cb);
12396 ctl_datamove_remote_write(union ctl_io *io)
12399 void (*fe_datamove)(union ctl_io *io);
12402 * - Get the data from the host/HBA into local memory.
12403 * - DMA memory from the local controller to the remote controller.
12404 * - Send status back to the remote controller.
12407 retval = ctl_datamove_remote_sgl_setup(io);
12411 /* Switch the pointer over so the FETD knows what to do */
12412 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12415 * Use a custom move done callback, since we need to send completion
12416 * back to the other controller, not to the backend on this side.
12418 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12420 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12429 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12438 * In this case, we had to malloc the memory locally. Free it.
12440 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12442 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12443 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12447 scsi_path_string(io, path_str, sizeof(path_str));
12448 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12449 sbuf_cat(&sb, path_str);
12450 scsi_command_string(&io->scsiio, NULL, &sb);
12451 sbuf_printf(&sb, "\n");
12452 sbuf_cat(&sb, path_str);
12453 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12454 io->scsiio.tag_num, io->scsiio.tag_type);
12455 sbuf_cat(&sb, path_str);
12456 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12457 io->io_hdr.flags, io->io_hdr.status);
12459 printk("%s", sbuf_data(&sb));
12464 * The read is done, now we need to send status (good or bad) back
12465 * to the other side.
12467 ctl_send_datamove_done(io, /*have_lock*/ 0);
12473 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12476 void (*fe_datamove)(union ctl_io *io);
12480 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12481 printf("%s: ISC DMA read failed with error %d", __func__,
12483 ctl_set_internal_failure(&io->scsiio,
12485 /*retry_count*/ rq->ret);
12488 ctl_dt_req_free(rq);
12490 /* Switch the pointer over so the FETD knows what to do */
12491 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12494 * Use a custom move done callback, since we need to send completion
12495 * back to the other controller, not to the backend on this side.
12497 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12499 /* XXX KDM add checks like the ones in ctl_datamove? */
12501 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12507 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12509 struct ctl_sg_entry *local_sglist, *remote_sglist;
12510 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12511 struct ctl_softc *softc;
12516 softc = control_softc;
12518 local_sglist = io->io_hdr.local_sglist;
12519 local_dma_sglist = io->io_hdr.local_dma_sglist;
12520 remote_sglist = io->io_hdr.remote_sglist;
12521 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12523 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12524 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12525 local_sglist[i].len = remote_sglist[i].len;
12528 * XXX Detect the situation where the RS-level I/O
12529 * redirector on the other side has already read the
12530 * data off of the AOR RS on this side, and
12531 * transferred it to remote (mirror) memory on the
12532 * other side. Since we already have the data in
12533 * memory here, we just need to use it.
12535 * XXX KDM this can probably be removed once we
12536 * get the cache device code in and take the
12537 * current AOR implementation out.
12540 if ((remote_sglist[i].addr >=
12541 (void *)vtophys(softc->mirr->addr))
12542 && (remote_sglist[i].addr <
12543 ((void *)vtophys(softc->mirr->addr) +
12544 CacheMirrorOffset))) {
12545 local_sglist[i].addr = remote_sglist[i].addr -
12547 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12549 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12551 local_sglist[i].addr = remote_sglist[i].addr +
12556 printf("%s: local %p, remote %p, len %d\n",
12557 __func__, local_sglist[i].addr,
12558 remote_sglist[i].addr, local_sglist[i].len);
12562 uint32_t len_to_go;
12565 * In this case, we don't have automatically allocated
12566 * memory for this I/O on this controller. This typically
12567 * happens with internal CTL I/O -- e.g. inquiry, mode
12568 * sense, etc. Anything coming from RAIDCore will have
12569 * a mirror area available.
12571 len_to_go = io->scsiio.kern_data_len;
12574 * Clear the no datasync flag, we have to use malloced
12577 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12580 * The difficult thing here is that the size of the various
12581 * S/G segments may be different than the size from the
12582 * remote controller. That'll make it harder when DMAing
12583 * the data back to the other side.
12585 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12586 sizeof(io->io_hdr.remote_sglist[0])) &&
12587 (len_to_go > 0); i++) {
12588 local_sglist[i].len = ctl_min(len_to_go, 131072);
12589 CTL_SIZE_8B(local_dma_sglist[i].len,
12590 local_sglist[i].len);
12591 local_sglist[i].addr =
12592 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12594 local_dma_sglist[i].addr = local_sglist[i].addr;
12596 if (local_sglist[i].addr == NULL) {
12599 printf("malloc failed for %zd bytes!",
12600 local_dma_sglist[i].len);
12601 for (j = 0; j < i; j++) {
12602 free(local_sglist[j].addr, M_CTL);
12604 ctl_set_internal_failure(&io->scsiio,
12606 /*retry_count*/ 4857);
12608 goto bailout_error;
12611 /* XXX KDM do we need a sync here? */
12613 len_to_go -= local_sglist[i].len;
12616 * Reset the number of S/G entries accordingly. The
12617 * original number of S/G entries is available in
12620 io->scsiio.kern_sg_entries = i;
12623 printf("%s: kern_sg_entries = %d\n", __func__,
12624 io->scsiio.kern_sg_entries);
12625 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12626 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12627 local_sglist[i].addr, local_sglist[i].len,
12628 local_dma_sglist[i].len);
12637 ctl_send_datamove_done(io, /*have_lock*/ 0);
12643 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12644 ctl_ha_dt_cb callback)
12646 struct ctl_ha_dt_req *rq;
12647 struct ctl_sg_entry *remote_sglist, *local_sglist;
12648 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12649 uint32_t local_used, remote_used, total_used;
12655 rq = ctl_dt_req_alloc();
12658 * If we failed to allocate the request, and if the DMA didn't fail
12659 * anyway, set busy status. This is just a resource allocation
12663 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12664 ctl_set_busy(&io->scsiio);
12666 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12669 ctl_dt_req_free(rq);
12672 * The data move failed. We need to return status back
12673 * to the other controller. No point in trying to DMA
12674 * data to the remote controller.
12677 ctl_send_datamove_done(io, /*have_lock*/ 0);
12684 local_sglist = io->io_hdr.local_sglist;
12685 local_dma_sglist = io->io_hdr.local_dma_sglist;
12686 remote_sglist = io->io_hdr.remote_sglist;
12687 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12692 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12693 rq->ret = CTL_HA_STATUS_SUCCESS;
12700 * Pull/push the data over the wire from/to the other controller.
12701 * This takes into account the possibility that the local and
12702 * remote sglists may not be identical in terms of the size of
12703 * the elements and the number of elements.
12705 * One fundamental assumption here is that the length allocated for
12706 * both the local and remote sglists is identical. Otherwise, we've
12707 * essentially got a coding error of some sort.
12709 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12711 uint32_t cur_len, dma_length;
12714 rq->id = CTL_HA_DATA_CTL;
12715 rq->command = command;
12719 * Both pointers should be aligned. But it is possible
12720 * that the allocation length is not. They should both
12721 * also have enough slack left over at the end, though,
12722 * to round up to the next 8 byte boundary.
12724 cur_len = ctl_min(local_sglist[i].len - local_used,
12725 remote_sglist[j].len - remote_used);
12728 * In this case, we have a size issue and need to decrease
12729 * the size, except in the case where we actually have less
12730 * than 8 bytes left. In that case, we need to increase
12731 * the DMA length to get the last bit.
12733 if ((cur_len & 0x7) != 0) {
12734 if (cur_len > 0x7) {
12735 cur_len = cur_len - (cur_len & 0x7);
12736 dma_length = cur_len;
12738 CTL_SIZE_8B(dma_length, cur_len);
12742 dma_length = cur_len;
12745 * If we had to allocate memory for this I/O, instead of using
12746 * the non-cached mirror memory, we'll need to flush the cache
12747 * before trying to DMA to the other controller.
12749 * We could end up doing this multiple times for the same
12750 * segment if we have a larger local segment than remote
12751 * segment. That shouldn't be an issue.
12753 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12755 * XXX KDM use bus_dmamap_sync() here.
12759 rq->size = dma_length;
12761 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12762 tmp_ptr += local_used;
12764 /* Use physical addresses when talking to ISC hardware */
12765 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12766 /* XXX KDM use busdma */
12768 rq->local = vtophys(tmp_ptr);
12771 rq->local = tmp_ptr;
12773 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12774 tmp_ptr += remote_used;
12775 rq->remote = tmp_ptr;
12777 rq->callback = NULL;
12779 local_used += cur_len;
12780 if (local_used >= local_sglist[i].len) {
12785 remote_used += cur_len;
12786 if (remote_used >= remote_sglist[j].len) {
12790 total_used += cur_len;
12792 if (total_used >= io->scsiio.kern_data_len)
12793 rq->callback = callback;
12795 if ((rq->size & 0x7) != 0) {
12796 printf("%s: warning: size %d is not on 8b boundary\n",
12797 __func__, rq->size);
12799 if (((uintptr_t)rq->local & 0x7) != 0) {
12800 printf("%s: warning: local %p not on 8b boundary\n",
12801 __func__, rq->local);
12803 if (((uintptr_t)rq->remote & 0x7) != 0) {
12804 printf("%s: warning: remote %p not on 8b boundary\n",
12805 __func__, rq->local);
12808 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12809 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12810 rq->local, rq->remote, rq->size);
12813 isc_ret = ctl_dt_single(rq);
12814 if (isc_ret == CTL_HA_STATUS_WAIT)
12817 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12818 rq->ret = CTL_HA_STATUS_SUCCESS;
12832 ctl_datamove_remote_read(union ctl_io *io)
12838 * This will send an error to the other controller in the case of a
12841 retval = ctl_datamove_remote_sgl_setup(io);
12845 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12846 ctl_datamove_remote_read_cb);
12848 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12850 * Make sure we free memory if there was an error.. The
12851 * ctl_datamove_remote_xfer() function will send the
12852 * datamove done message, or call the callback with an
12853 * error if there is a problem.
12855 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12856 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12863 * Process a datamove request from the other controller. This is used for
12864 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12865 * first. Once that is complete, the data gets DMAed into the remote
12866 * controller's memory. For reads, we DMA from the remote controller's
12867 * memory into our memory first, and then move it out to the FETD.
12870 ctl_datamove_remote(union ctl_io *io)
12872 struct ctl_softc *softc;
12874 softc = control_softc;
12876 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12879 * Note that we look for an aborted I/O here, but don't do some of
12880 * the other checks that ctl_datamove() normally does. We don't
12881 * need to run the task queue, because this I/O is on the ISC
12882 * queue, which is executed by the work thread after the task queue.
12883 * We don't need to run the datamove delay code, since that should
12884 * have been done if need be on the other controller.
12886 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12888 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12889 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12890 io->io_hdr.nexus.targ_port,
12891 io->io_hdr.nexus.targ_target.id,
12892 io->io_hdr.nexus.targ_lun);
12893 io->io_hdr.status = CTL_CMD_ABORTED;
12894 io->io_hdr.port_status = 31338;
12896 ctl_send_datamove_done(io, /*have_lock*/ 0);
12901 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12902 ctl_datamove_remote_write(io);
12903 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12904 ctl_datamove_remote_read(io);
12906 union ctl_ha_msg msg;
12907 struct scsi_sense_data *sense;
12911 memset(&msg, 0, sizeof(msg));
12913 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12914 msg.hdr.status = CTL_SCSI_ERROR;
12915 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12917 retry_count = 4243;
12919 sense = &msg.scsi.sense_data;
12920 sks[0] = SSD_SCS_VALID;
12921 sks[1] = (retry_count >> 8) & 0xff;
12922 sks[2] = retry_count & 0xff;
12924 /* "Internal target failure" */
12925 scsi_set_sense_data(sense,
12926 /*sense_format*/ SSD_TYPE_NONE,
12927 /*current_error*/ 1,
12928 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12931 /*type*/ SSD_ELEM_SKS,
12932 /*size*/ sizeof(sks),
12936 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12937 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12938 ctl_failover_io(io, /*have_lock*/ 1);
12942 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12943 CTL_HA_STATUS_SUCCESS) {
12944 /* XXX KDM what to do if this fails? */
12952 ctl_process_done(union ctl_io *io)
12954 struct ctl_lun *lun;
12955 struct ctl_softc *ctl_softc;
12956 void (*fe_done)(union ctl_io *io);
12957 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12959 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12962 control_softc->ctl_ports[targ_port]->fe_done;
12965 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12970 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12971 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12973 sbuf_cat(&sb, path_str);
12974 switch (io->io_hdr.io_type) {
12976 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12977 sbuf_printf(&sb, "\n");
12978 sbuf_cat(&sb, path_str);
12979 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12980 io->scsiio.tag_num, io->scsiio.tag_type);
12983 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12984 "Tag Type: %d\n", io->taskio.task_action,
12985 io->taskio.tag_num, io->taskio.tag_type);
12988 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12989 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12992 sbuf_cat(&sb, path_str);
12993 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12994 (intmax_t)time_uptime - io->io_hdr.start_time);
12996 printf("%s", sbuf_data(&sb));
12998 #endif /* CTL_TIME_IO */
13000 switch (io->io_hdr.io_type) {
13004 if (bootverbose || verbose > 0)
13005 ctl_io_error_print(io, NULL);
13006 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13010 return (CTL_RETVAL_COMPLETE);
13013 printf("ctl_process_done: invalid io type %d\n",
13014 io->io_hdr.io_type);
13015 panic("ctl_process_done: invalid io type %d\n",
13016 io->io_hdr.io_type);
13017 break; /* NOTREACHED */
13020 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13022 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13023 io->io_hdr.nexus.targ_mapped_lun));
13027 ctl_softc = lun->ctl_softc;
13029 mtx_lock(&lun->lun_lock);
13032 * Check to see if we have any errors to inject here. We only
13033 * inject errors for commands that don't already have errors set.
13035 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13036 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13037 ctl_inject_error(lun, io);
13040 * XXX KDM how do we treat commands that aren't completed
13043 * XXX KDM should we also track I/O latency?
13045 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13046 io->io_hdr.io_type == CTL_IO_SCSI) {
13048 struct bintime cur_bt;
13052 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13054 type = CTL_STATS_READ;
13055 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13057 type = CTL_STATS_WRITE;
13059 type = CTL_STATS_NO_IO;
13061 lun->stats.ports[targ_port].bytes[type] +=
13062 io->scsiio.kern_total_len;
13063 lun->stats.ports[targ_port].operations[type]++;
13065 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13066 &io->io_hdr.dma_bt);
13067 lun->stats.ports[targ_port].num_dmas[type] +=
13068 io->io_hdr.num_dmas;
13069 getbintime(&cur_bt);
13070 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13071 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13076 * Remove this from the OOA queue.
13078 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13081 * Run through the blocked queue on this LUN and see if anything
13082 * has become unblocked, now that this transaction is done.
13084 ctl_check_blocked(lun);
13087 * If the LUN has been invalidated, free it if there is nothing
13088 * left on its OOA queue.
13090 if ((lun->flags & CTL_LUN_INVALID)
13091 && TAILQ_EMPTY(&lun->ooa_queue)) {
13092 mtx_unlock(&lun->lun_lock);
13093 mtx_lock(&ctl_softc->ctl_lock);
13095 mtx_unlock(&ctl_softc->ctl_lock);
13097 mtx_unlock(&lun->lun_lock);
13100 * If this command has been aborted, make sure we set the status
13101 * properly. The FETD is responsible for freeing the I/O and doing
13102 * whatever it needs to do to clean up its state.
13104 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13105 io->io_hdr.status = CTL_CMD_ABORTED;
13108 * We print out status for every task management command. For SCSI
13109 * commands, we filter out any unit attention errors; they happen
13110 * on every boot, and would clutter up the log. Note: task
13111 * management commands aren't printed here, they are printed above,
13112 * since they should never even make it down here.
13114 switch (io->io_hdr.io_type) {
13115 case CTL_IO_SCSI: {
13116 int error_code, sense_key, asc, ascq;
13120 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13121 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13123 * Since this is just for printing, no need to
13124 * show errors here.
13126 scsi_extract_sense_len(&io->scsiio.sense_data,
13127 io->scsiio.sense_len,
13132 /*show_errors*/ 0);
13135 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13136 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13137 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13138 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13140 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13141 ctl_softc->skipped_prints++;
13143 uint32_t skipped_prints;
13145 skipped_prints = ctl_softc->skipped_prints;
13147 ctl_softc->skipped_prints = 0;
13148 ctl_softc->last_print_jiffies = time_uptime;
13150 if (skipped_prints > 0) {
13152 csevent_log(CSC_CTL | CSC_SHELF_SW |
13154 csevent_LogType_Trace,
13155 csevent_Severity_Information,
13156 csevent_AlertLevel_Green,
13157 csevent_FRU_Firmware,
13158 csevent_FRU_Unknown,
13159 "High CTL error volume, %d prints "
13160 "skipped", skipped_prints);
13163 if (bootverbose || verbose > 0)
13164 ctl_io_error_print(io, NULL);
13170 if (bootverbose || verbose > 0)
13171 ctl_io_error_print(io, NULL);
13178 * Tell the FETD or the other shelf controller we're done with this
13179 * command. Note that only SCSI commands get to this point. Task
13180 * management commands are completed above.
13182 * We only send status to the other controller if we're in XFER
13183 * mode. In SER_ONLY mode, the I/O is done on the controller that
13184 * received the I/O (from CTL's perspective), and so the status is
13187 * XXX KDM if we hold the lock here, we could cause a deadlock
13188 * if the frontend comes back in in this context to queue
13191 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13192 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13193 union ctl_ha_msg msg;
13195 memset(&msg, 0, sizeof(msg));
13196 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13197 msg.hdr.original_sc = io->io_hdr.original_sc;
13198 msg.hdr.nexus = io->io_hdr.nexus;
13199 msg.hdr.status = io->io_hdr.status;
13200 msg.scsi.scsi_status = io->scsiio.scsi_status;
13201 msg.scsi.tag_num = io->scsiio.tag_num;
13202 msg.scsi.tag_type = io->scsiio.tag_type;
13203 msg.scsi.sense_len = io->scsiio.sense_len;
13204 msg.scsi.sense_residual = io->scsiio.sense_residual;
13205 msg.scsi.residual = io->scsiio.residual;
13206 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13207 sizeof(io->scsiio.sense_data));
13209 * We copy this whether or not this is an I/O-related
13210 * command. Otherwise, we'd have to go and check to see
13211 * whether it's a read/write command, and it really isn't
13214 memcpy(&msg.scsi.lbalen,
13215 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13216 sizeof(msg.scsi.lbalen));
13218 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13219 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13220 /* XXX do something here */
13229 return (CTL_RETVAL_COMPLETE);
13233 * Front end should call this if it doesn't do autosense. When the request
13234 * sense comes back in from the initiator, we'll dequeue this and send it.
13237 ctl_queue_sense(union ctl_io *io)
13239 struct ctl_lun *lun;
13240 struct ctl_softc *ctl_softc;
13241 uint32_t initidx, targ_lun;
13243 ctl_softc = control_softc;
13245 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13248 * LUN lookup will likely move to the ctl_work_thread() once we
13249 * have our new queueing infrastructure (that doesn't put things on
13250 * a per-LUN queue initially). That is so that we can handle
13251 * things like an INQUIRY to a LUN that we don't have enabled. We
13252 * can't deal with that right now.
13254 mtx_lock(&ctl_softc->ctl_lock);
13257 * If we don't have a LUN for this, just toss the sense
13260 targ_lun = io->io_hdr.nexus.targ_lun;
13261 if (io->io_hdr.nexus.lun_map_fn != NULL)
13262 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13263 if ((targ_lun < CTL_MAX_LUNS)
13264 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13265 lun = ctl_softc->ctl_luns[targ_lun];
13269 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13271 mtx_lock(&lun->lun_lock);
13273 * Already have CA set for this LUN...toss the sense information.
13275 if (ctl_is_set(lun->have_ca, initidx)) {
13276 mtx_unlock(&lun->lun_lock);
13280 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13281 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13282 sizeof(io->scsiio.sense_data)));
13283 ctl_set_mask(lun->have_ca, initidx);
13284 mtx_unlock(&lun->lun_lock);
13287 mtx_unlock(&ctl_softc->ctl_lock);
13291 return (CTL_RETVAL_COMPLETE);
13295 * Primary command inlet from frontend ports. All SCSI and task I/O
13296 * requests must go through this function.
13299 ctl_queue(union ctl_io *io)
13301 struct ctl_softc *ctl_softc;
13303 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13305 ctl_softc = control_softc;
13308 io->io_hdr.start_time = time_uptime;
13309 getbintime(&io->io_hdr.start_bt);
13310 #endif /* CTL_TIME_IO */
13312 /* Map FE-specific LUN ID into global one. */
13313 if (io->io_hdr.nexus.lun_map_fn != NULL)
13314 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn(
13315 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun);
13317 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun;
13319 switch (io->io_hdr.io_type) {
13322 ctl_enqueue_incoming(io);
13325 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13329 return (CTL_RETVAL_COMPLETE);
13332 #ifdef CTL_IO_DELAY
13334 ctl_done_timer_wakeup(void *arg)
13338 io = (union ctl_io *)arg;
13341 #endif /* CTL_IO_DELAY */
13344 ctl_done(union ctl_io *io)
13346 struct ctl_softc *ctl_softc;
13348 ctl_softc = control_softc;
13351 * Enable this to catch duplicate completion issues.
13354 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13355 printf("%s: type %d msg %d cdb %x iptl: "
13356 "%d:%d:%d:%d tag 0x%04x "
13357 "flag %#x status %x\n",
13359 io->io_hdr.io_type,
13360 io->io_hdr.msg_type,
13362 io->io_hdr.nexus.initid.id,
13363 io->io_hdr.nexus.targ_port,
13364 io->io_hdr.nexus.targ_target.id,
13365 io->io_hdr.nexus.targ_lun,
13366 (io->io_hdr.io_type ==
13368 io->taskio.tag_num :
13369 io->scsiio.tag_num,
13371 io->io_hdr.status);
13373 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13377 * This is an internal copy of an I/O, and should not go through
13378 * the normal done processing logic.
13380 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13384 * We need to send a msg to the serializing shelf to finish the IO
13385 * as well. We don't send a finish message to the other shelf if
13386 * this is a task management command. Task management commands
13387 * aren't serialized in the OOA queue, but rather just executed on
13388 * both shelf controllers for commands that originated on that
13391 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13392 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13393 union ctl_ha_msg msg_io;
13395 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13396 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13397 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13398 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13400 /* continue on to finish IO */
13402 #ifdef CTL_IO_DELAY
13403 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13404 struct ctl_lun *lun;
13406 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13408 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13410 struct ctl_lun *lun;
13412 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13415 && (lun->delay_info.done_delay > 0)) {
13416 struct callout *callout;
13418 callout = (struct callout *)&io->io_hdr.timer_bytes;
13419 callout_init(callout, /*mpsafe*/ 1);
13420 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13421 callout_reset(callout,
13422 lun->delay_info.done_delay * hz,
13423 ctl_done_timer_wakeup, io);
13424 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13425 lun->delay_info.done_delay = 0;
13429 #endif /* CTL_IO_DELAY */
13431 ctl_enqueue_done(io);
13435 ctl_isc(struct ctl_scsiio *ctsio)
13437 struct ctl_lun *lun;
13440 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13442 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13444 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13446 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13453 ctl_work_thread(void *arg)
13455 struct ctl_thread *thr = (struct ctl_thread *)arg;
13456 struct ctl_softc *softc = thr->ctl_softc;
13460 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13466 * We handle the queues in this order:
13468 * - done queue (to free up resources, unblock other commands)
13472 * If those queues are empty, we break out of the loop and
13475 mtx_lock(&thr->queue_lock);
13476 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13478 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13479 mtx_unlock(&thr->queue_lock);
13480 ctl_handle_isc(io);
13483 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13485 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13486 /* clear any blocked commands, call fe_done */
13487 mtx_unlock(&thr->queue_lock);
13488 retval = ctl_process_done(io);
13491 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13493 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13494 mtx_unlock(&thr->queue_lock);
13495 if (io->io_hdr.io_type == CTL_IO_TASK)
13498 ctl_scsiio_precheck(softc, &io->scsiio);
13501 if (!ctl_pause_rtr) {
13502 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13504 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13505 mtx_unlock(&thr->queue_lock);
13506 retval = ctl_scsiio(&io->scsiio);
13507 if (retval != CTL_RETVAL_COMPLETE)
13508 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13513 /* Sleep until we have something to do. */
13514 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13519 ctl_lun_thread(void *arg)
13521 struct ctl_softc *softc = (struct ctl_softc *)arg;
13522 struct ctl_be_lun *be_lun;
13525 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13529 mtx_lock(&softc->ctl_lock);
13530 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13531 if (be_lun != NULL) {
13532 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13533 mtx_unlock(&softc->ctl_lock);
13534 ctl_create_lun(be_lun);
13538 /* Sleep until we have something to do. */
13539 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13540 PDROP | PRIBIO, "-", 0);
13545 ctl_enqueue_incoming(union ctl_io *io)
13547 struct ctl_softc *softc = control_softc;
13548 struct ctl_thread *thr;
13551 idx = (io->io_hdr.nexus.targ_port * 127 +
13552 io->io_hdr.nexus.initid.id) % worker_threads;
13553 thr = &softc->threads[idx];
13554 mtx_lock(&thr->queue_lock);
13555 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13556 mtx_unlock(&thr->queue_lock);
13561 ctl_enqueue_rtr(union ctl_io *io)
13563 struct ctl_softc *softc = control_softc;
13564 struct ctl_thread *thr;
13566 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13567 mtx_lock(&thr->queue_lock);
13568 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13569 mtx_unlock(&thr->queue_lock);
13574 ctl_enqueue_done(union ctl_io *io)
13576 struct ctl_softc *softc = control_softc;
13577 struct ctl_thread *thr;
13579 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13580 mtx_lock(&thr->queue_lock);
13581 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13582 mtx_unlock(&thr->queue_lock);
13587 ctl_enqueue_isc(union ctl_io *io)
13589 struct ctl_softc *softc = control_softc;
13590 struct ctl_thread *thr;
13592 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13593 mtx_lock(&thr->queue_lock);
13594 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13595 mtx_unlock(&thr->queue_lock);
13599 /* Initialization and failover */
13602 ctl_init_isc_msg(void)
13604 printf("CTL: Still calling this thing\n");
13609 * Initializes component into configuration defined by bootMode
13611 * returns hasc_Status:
13613 * ERROR - fatal error
13615 static ctl_ha_comp_status
13616 ctl_isc_init(struct ctl_ha_component *c)
13618 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13625 * Starts component in state requested. If component starts successfully,
13626 * it must set its own state to the requestrd state
13627 * When requested state is HASC_STATE_HA, the component may refine it
13628 * by adding _SLAVE or _MASTER flags.
13629 * Currently allowed state transitions are:
13630 * UNKNOWN->HA - initial startup
13631 * UNKNOWN->SINGLE - initial startup when no parter detected
13632 * HA->SINGLE - failover
13633 * returns ctl_ha_comp_status:
13634 * OK - component successfully started in requested state
13635 * FAILED - could not start the requested state, failover may
13637 * ERROR - fatal error detected, no future startup possible
13639 static ctl_ha_comp_status
13640 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13642 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13644 printf("%s: go\n", __func__);
13646 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13647 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13649 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13650 != CTL_HA_STATUS_SUCCESS) {
13651 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13652 ret = CTL_HA_COMP_STATUS_ERROR;
13654 } else if (CTL_HA_STATE_IS_HA(c->state)
13655 && CTL_HA_STATE_IS_SINGLE(state)){
13656 // HA->SINGLE transition
13660 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13662 ret = CTL_HA_COMP_STATUS_ERROR;
13664 if (CTL_HA_STATE_IS_SINGLE(state))
13673 * Quiesce component
13674 * The component must clear any error conditions (set status to OK) and
13675 * prepare itself to another Start call
13676 * returns ctl_ha_comp_status:
13680 static ctl_ha_comp_status
13681 ctl_isc_quiesce(struct ctl_ha_component *c)
13683 int ret = CTL_HA_COMP_STATUS_OK;
13690 struct ctl_ha_component ctl_ha_component_ctlisc =
13693 .state = CTL_HA_STATE_UNKNOWN,
13694 .init = ctl_isc_init,
13695 .start = ctl_isc_start,
13696 .quiesce = ctl_isc_quiesce