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 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
312 &worker_threads, 1, "Number of worker threads");
313 static int verbose = 0;
314 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
315 &verbose, 0, "Show SCSI errors returned to initiator");
318 * Serial number (0x80), device id (0x83), supported pages (0x00),
319 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
321 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
323 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
325 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
326 static int ctl_init(void);
327 void ctl_shutdown(void);
328 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
329 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
330 static void ctl_ioctl_online(void *arg);
331 static void ctl_ioctl_offline(void *arg);
332 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
333 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
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");
459 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
461 static moduledata_t ctl_moduledata = {
463 ctl_module_event_handler,
467 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
468 MODULE_VERSION(ctl, 1);
471 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
472 union ctl_ha_msg *msg_info)
474 struct ctl_scsiio *ctsio;
476 if (msg_info->hdr.original_sc == NULL) {
477 printf("%s: original_sc == NULL!\n", __func__);
478 /* XXX KDM now what? */
482 ctsio = &msg_info->hdr.original_sc->scsiio;
483 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
484 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
485 ctsio->io_hdr.status = msg_info->hdr.status;
486 ctsio->scsi_status = msg_info->scsi.scsi_status;
487 ctsio->sense_len = msg_info->scsi.sense_len;
488 ctsio->sense_residual = msg_info->scsi.sense_residual;
489 ctsio->residual = msg_info->scsi.residual;
490 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
491 sizeof(ctsio->sense_data));
492 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
493 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
494 ctl_enqueue_isc((union ctl_io *)ctsio);
498 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
499 union ctl_ha_msg *msg_info)
501 struct ctl_scsiio *ctsio;
503 if (msg_info->hdr.serializing_sc == NULL) {
504 printf("%s: serializing_sc == NULL!\n", __func__);
505 /* XXX KDM now what? */
509 ctsio = &msg_info->hdr.serializing_sc->scsiio;
512 * Attempt to catch the situation where an I/O has
513 * been freed, and we're using it again.
515 if (ctsio->io_hdr.io_type == 0xff) {
516 union ctl_io *tmp_io;
517 tmp_io = (union ctl_io *)ctsio;
518 printf("%s: %p use after free!\n", __func__,
520 printf("%s: type %d msg %d cdb %x iptl: "
521 "%d:%d:%d:%d tag 0x%04x "
522 "flag %#x status %x\n",
524 tmp_io->io_hdr.io_type,
525 tmp_io->io_hdr.msg_type,
526 tmp_io->scsiio.cdb[0],
527 tmp_io->io_hdr.nexus.initid.id,
528 tmp_io->io_hdr.nexus.targ_port,
529 tmp_io->io_hdr.nexus.targ_target.id,
530 tmp_io->io_hdr.nexus.targ_lun,
531 (tmp_io->io_hdr.io_type ==
533 tmp_io->taskio.tag_num :
534 tmp_io->scsiio.tag_num,
535 tmp_io->io_hdr.flags,
536 tmp_io->io_hdr.status);
539 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
540 ctl_enqueue_isc((union ctl_io *)ctsio);
544 * ISC (Inter Shelf Communication) event handler. Events from the HA
545 * subsystem come in here.
548 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
550 struct ctl_softc *ctl_softc;
552 struct ctl_prio *presio;
553 ctl_ha_status isc_status;
555 ctl_softc = control_softc;
560 printf("CTL: Isc Msg event %d\n", event);
562 if (event == CTL_HA_EVT_MSG_RECV) {
563 union ctl_ha_msg msg_info;
565 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
566 sizeof(msg_info), /*wait*/ 0);
568 printf("CTL: msg_type %d\n", msg_info.msg_type);
570 if (isc_status != 0) {
571 printf("Error receiving message, status = %d\n",
576 switch (msg_info.hdr.msg_type) {
577 case CTL_MSG_SERIALIZE:
579 printf("Serialize\n");
581 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
583 printf("ctl_isc_event_handler: can't allocate "
586 /* Need to set busy and send msg back */
587 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
588 msg_info.hdr.status = CTL_SCSI_ERROR;
589 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
590 msg_info.scsi.sense_len = 0;
591 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
592 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
597 // populate ctsio from msg_info
598 io->io_hdr.io_type = CTL_IO_SCSI;
599 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
600 io->io_hdr.original_sc = msg_info.hdr.original_sc;
602 printf("pOrig %x\n", (int)msg_info.original_sc);
604 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
607 * If we're in serialization-only mode, we don't
608 * want to go through full done processing. Thus
611 * XXX KDM add another flag that is more specific.
613 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
614 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
615 io->io_hdr.nexus = msg_info.hdr.nexus;
617 printf("targ %d, port %d, iid %d, lun %d\n",
618 io->io_hdr.nexus.targ_target.id,
619 io->io_hdr.nexus.targ_port,
620 io->io_hdr.nexus.initid.id,
621 io->io_hdr.nexus.targ_lun);
623 io->scsiio.tag_num = msg_info.scsi.tag_num;
624 io->scsiio.tag_type = msg_info.scsi.tag_type;
625 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
627 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
628 const struct ctl_cmd_entry *entry;
630 entry = ctl_get_cmd_entry(&io->scsiio);
631 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
633 entry->flags & CTL_FLAG_DATA_MASK;
638 /* Performed on the Originating SC, XFER mode only */
639 case CTL_MSG_DATAMOVE: {
640 struct ctl_sg_entry *sgl;
643 io = msg_info.hdr.original_sc;
645 printf("%s: original_sc == NULL!\n", __func__);
646 /* XXX KDM do something here */
649 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
650 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
652 * Keep track of this, we need to send it back over
653 * when the datamove is complete.
655 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
657 if (msg_info.dt.sg_sequence == 0) {
659 * XXX KDM we use the preallocated S/G list
660 * here, but we'll need to change this to
661 * dynamic allocation if we need larger S/G
664 if (msg_info.dt.kern_sg_entries >
665 sizeof(io->io_hdr.remote_sglist) /
666 sizeof(io->io_hdr.remote_sglist[0])) {
667 printf("%s: number of S/G entries "
668 "needed %u > allocated num %zd\n",
670 msg_info.dt.kern_sg_entries,
671 sizeof(io->io_hdr.remote_sglist)/
672 sizeof(io->io_hdr.remote_sglist[0]));
675 * XXX KDM send a message back to
676 * the other side to shut down the
677 * DMA. The error will come back
678 * through via the normal channel.
682 sgl = io->io_hdr.remote_sglist;
684 sizeof(io->io_hdr.remote_sglist));
686 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
688 io->scsiio.kern_sg_entries =
689 msg_info.dt.kern_sg_entries;
690 io->scsiio.rem_sg_entries =
691 msg_info.dt.kern_sg_entries;
692 io->scsiio.kern_data_len =
693 msg_info.dt.kern_data_len;
694 io->scsiio.kern_total_len =
695 msg_info.dt.kern_total_len;
696 io->scsiio.kern_data_resid =
697 msg_info.dt.kern_data_resid;
698 io->scsiio.kern_rel_offset =
699 msg_info.dt.kern_rel_offset;
701 * Clear out per-DMA flags.
703 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
705 * Add per-DMA flags that are set for this
706 * particular DMA request.
708 io->io_hdr.flags |= msg_info.dt.flags &
711 sgl = (struct ctl_sg_entry *)
712 io->scsiio.kern_data_ptr;
714 for (i = msg_info.dt.sent_sg_entries, j = 0;
715 i < (msg_info.dt.sent_sg_entries +
716 msg_info.dt.cur_sg_entries); i++, j++) {
717 sgl[i].addr = msg_info.dt.sg_list[j].addr;
718 sgl[i].len = msg_info.dt.sg_list[j].len;
721 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
723 msg_info.dt.sg_list[j].addr,
724 msg_info.dt.sg_list[j].len,
725 sgl[i].addr, sgl[i].len, j, i);
729 memcpy(&sgl[msg_info.dt.sent_sg_entries],
731 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
735 * If this is the last piece of the I/O, we've got
736 * the full S/G list. Queue processing in the thread.
737 * Otherwise wait for the next piece.
739 if (msg_info.dt.sg_last != 0)
743 /* Performed on the Serializing (primary) SC, XFER mode only */
744 case CTL_MSG_DATAMOVE_DONE: {
745 if (msg_info.hdr.serializing_sc == NULL) {
746 printf("%s: serializing_sc == NULL!\n",
748 /* XXX KDM now what? */
752 * We grab the sense information here in case
753 * there was a failure, so we can return status
754 * back to the initiator.
756 io = msg_info.hdr.serializing_sc;
757 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
758 io->io_hdr.status = msg_info.hdr.status;
759 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
760 io->scsiio.sense_len = msg_info.scsi.sense_len;
761 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
762 io->io_hdr.port_status = msg_info.scsi.fetd_status;
763 io->scsiio.residual = msg_info.scsi.residual;
764 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
765 sizeof(io->scsiio.sense_data));
770 /* Preformed on Originating SC, SER_ONLY mode */
772 io = msg_info.hdr.original_sc;
774 printf("%s: Major Bummer\n", __func__);
778 printf("pOrig %x\n",(int) ctsio);
781 io->io_hdr.msg_type = CTL_MSG_R2R;
782 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
787 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
789 * Performed on the Originating (i.e. secondary) SC in XFER
792 case CTL_MSG_FINISH_IO:
793 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
794 ctl_isc_handler_finish_xfer(ctl_softc,
797 ctl_isc_handler_finish_ser_only(ctl_softc,
801 /* Preformed on Originating SC */
802 case CTL_MSG_BAD_JUJU:
803 io = msg_info.hdr.original_sc;
805 printf("%s: Bad JUJU!, original_sc is NULL!\n",
809 ctl_copy_sense_data(&msg_info, io);
811 * IO should have already been cleaned up on other
812 * SC so clear this flag so we won't send a message
813 * back to finish the IO there.
815 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
816 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
818 /* io = msg_info.hdr.serializing_sc; */
819 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
823 /* Handle resets sent from the other side */
824 case CTL_MSG_MANAGE_TASKS: {
825 struct ctl_taskio *taskio;
826 taskio = (struct ctl_taskio *)ctl_alloc_io(
827 (void *)ctl_softc->othersc_pool);
828 if (taskio == NULL) {
829 printf("ctl_isc_event_handler: can't allocate "
832 /* should I just call the proper reset func
836 ctl_zero_io((union ctl_io *)taskio);
837 taskio->io_hdr.io_type = CTL_IO_TASK;
838 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
839 taskio->io_hdr.nexus = msg_info.hdr.nexus;
840 taskio->task_action = msg_info.task.task_action;
841 taskio->tag_num = msg_info.task.tag_num;
842 taskio->tag_type = msg_info.task.tag_type;
844 taskio->io_hdr.start_time = time_uptime;
845 getbintime(&taskio->io_hdr.start_bt);
847 cs_prof_gettime(&taskio->io_hdr.start_ticks);
849 #endif /* CTL_TIME_IO */
850 ctl_run_task((union ctl_io *)taskio);
853 /* Persistent Reserve action which needs attention */
854 case CTL_MSG_PERS_ACTION:
855 presio = (struct ctl_prio *)ctl_alloc_io(
856 (void *)ctl_softc->othersc_pool);
857 if (presio == NULL) {
858 printf("ctl_isc_event_handler: can't allocate "
861 /* Need to set busy and send msg back */
864 ctl_zero_io((union ctl_io *)presio);
865 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
866 presio->pr_msg = msg_info.pr;
867 ctl_enqueue_isc((union ctl_io *)presio);
869 case CTL_MSG_SYNC_FE:
872 case CTL_MSG_APS_LOCK: {
873 // It's quicker to execute this then to
876 struct ctl_page_index *page_index;
877 struct copan_aps_subpage *current_sp;
880 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
881 lun = ctl_softc->ctl_luns[targ_lun];
882 mtx_lock(&lun->lun_lock);
883 page_index = &lun->mode_pages.index[index_to_aps_page];
884 current_sp = (struct copan_aps_subpage *)
885 (page_index->page_data +
886 (page_index->page_len * CTL_PAGE_CURRENT));
888 current_sp->lock_active = msg_info.aps.lock_flag;
889 mtx_unlock(&lun->lun_lock);
893 printf("How did I get here?\n");
895 } else if (event == CTL_HA_EVT_MSG_SENT) {
896 if (param != CTL_HA_STATUS_SUCCESS) {
897 printf("Bad status from ctl_ha_msg_send status %d\n",
901 } else if (event == CTL_HA_EVT_DISCONNECT) {
902 printf("CTL: Got a disconnect from Isc\n");
905 printf("ctl_isc_event_handler: Unknown event %d\n", event);
914 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
916 struct scsi_sense_data *sense;
918 sense = &dest->scsiio.sense_data;
919 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
920 dest->scsiio.scsi_status = src->scsi.scsi_status;
921 dest->scsiio.sense_len = src->scsi.sense_len;
922 dest->io_hdr.status = src->hdr.status;
928 struct ctl_softc *softc;
929 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
930 struct ctl_frontend *fe;
932 int i, error, retval;
939 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
941 softc = control_softc;
943 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
946 softc->dev->si_drv1 = softc;
949 * By default, return a "bad LUN" peripheral qualifier for unknown
950 * LUNs. The user can override this default using the tunable or
951 * sysctl. See the comment in ctl_inquiry_std() for more details.
953 softc->inquiry_pq_no_lun = 1;
954 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
955 &softc->inquiry_pq_no_lun);
956 sysctl_ctx_init(&softc->sysctl_ctx);
957 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
958 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
959 CTLFLAG_RD, 0, "CAM Target Layer");
961 if (softc->sysctl_tree == NULL) {
962 printf("%s: unable to allocate sysctl tree\n", __func__);
963 destroy_dev(softc->dev);
964 free(control_softc, M_DEVBUF);
965 control_softc = NULL;
969 SYSCTL_ADD_INT(&softc->sysctl_ctx,
970 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
971 "inquiry_pq_no_lun", CTLFLAG_RW,
972 &softc->inquiry_pq_no_lun, 0,
973 "Report no lun possible for invalid LUNs");
975 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
976 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
977 softc->open_count = 0;
980 * Default to actually sending a SYNCHRONIZE CACHE command down to
983 softc->flags = CTL_FLAG_REAL_SYNC;
986 * In Copan's HA scheme, the "master" and "slave" roles are
987 * figured out through the slot the controller is in. Although it
988 * is an active/active system, someone has to be in charge.
991 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
995 softc->flags |= CTL_FLAG_MASTER_SHELF;
998 persis_offset = CTL_MAX_INITIATORS;
1001 * XXX KDM need to figure out where we want to get our target ID
1002 * and WWID. Is it different on each port?
1004 softc->target.id = 0;
1005 softc->target.wwid[0] = 0x12345678;
1006 softc->target.wwid[1] = 0x87654321;
1007 STAILQ_INIT(&softc->lun_list);
1008 STAILQ_INIT(&softc->pending_lun_queue);
1009 STAILQ_INIT(&softc->fe_list);
1010 STAILQ_INIT(&softc->be_list);
1011 STAILQ_INIT(&softc->io_pools);
1013 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1014 &internal_pool)!= 0){
1015 printf("ctl: can't allocate %d entry internal pool, "
1016 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1020 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1021 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1022 printf("ctl: can't allocate %d entry emergency pool, "
1023 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1024 ctl_pool_free(internal_pool);
1028 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1031 printf("ctl: can't allocate %d entry other SC pool, "
1032 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1033 ctl_pool_free(internal_pool);
1034 ctl_pool_free(emergency_pool);
1038 softc->internal_pool = internal_pool;
1039 softc->emergency_pool = emergency_pool;
1040 softc->othersc_pool = other_pool;
1042 if (worker_threads <= 0)
1043 worker_threads = max(1, mp_ncpus / 4);
1044 if (worker_threads > CTL_MAX_THREADS)
1045 worker_threads = CTL_MAX_THREADS;
1047 for (i = 0; i < worker_threads; i++) {
1048 struct ctl_thread *thr = &softc->threads[i];
1050 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1051 thr->ctl_softc = softc;
1052 STAILQ_INIT(&thr->incoming_queue);
1053 STAILQ_INIT(&thr->rtr_queue);
1054 STAILQ_INIT(&thr->done_queue);
1055 STAILQ_INIT(&thr->isc_queue);
1057 error = kproc_kthread_add(ctl_work_thread, thr,
1058 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1060 printf("error creating CTL work thread!\n");
1061 ctl_pool_free(internal_pool);
1062 ctl_pool_free(emergency_pool);
1063 ctl_pool_free(other_pool);
1067 error = kproc_kthread_add(ctl_lun_thread, softc,
1068 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1070 printf("error creating CTL lun thread!\n");
1071 ctl_pool_free(internal_pool);
1072 ctl_pool_free(emergency_pool);
1073 ctl_pool_free(other_pool);
1077 printf("ctl: CAM Target Layer loaded\n");
1080 * Initialize the initiator and portname mappings
1082 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1085 * Initialize the ioctl front end.
1087 fe = &softc->ioctl_info.fe;
1088 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1089 fe->port_type = CTL_PORT_IOCTL;
1090 fe->num_requested_ctl_io = 100;
1091 fe->port_name = softc->ioctl_info.port_name;
1092 fe->port_online = ctl_ioctl_online;
1093 fe->port_offline = ctl_ioctl_offline;
1094 fe->onoff_arg = &softc->ioctl_info;
1095 fe->targ_enable = ctl_ioctl_targ_enable;
1096 fe->targ_disable = ctl_ioctl_targ_disable;
1097 fe->lun_enable = ctl_ioctl_lun_enable;
1098 fe->lun_disable = ctl_ioctl_lun_disable;
1099 fe->targ_lun_arg = &softc->ioctl_info;
1100 fe->fe_datamove = ctl_ioctl_datamove;
1101 fe->fe_done = ctl_ioctl_done;
1102 fe->max_targets = 15;
1103 fe->max_target_id = 15;
1105 if (ctl_frontend_register(&softc->ioctl_info.fe,
1106 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1107 printf("ctl: ioctl front end registration failed, will "
1108 "continue anyway\n");
1112 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1113 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1114 sizeof(struct callout), CTL_TIMER_BYTES);
1117 #endif /* CTL_IO_DELAY */
1125 struct ctl_softc *softc;
1126 struct ctl_lun *lun, *next_lun;
1127 struct ctl_io_pool *pool;
1129 softc = (struct ctl_softc *)control_softc;
1131 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1132 printf("ctl: ioctl front end deregistration failed\n");
1134 mtx_lock(&softc->ctl_lock);
1139 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1140 next_lun = STAILQ_NEXT(lun, links);
1144 mtx_unlock(&softc->ctl_lock);
1147 * This will rip the rug out from under any FETDs or anyone else
1148 * that has a pool allocated. Since we increment our module
1149 * refcount any time someone outside the main CTL module allocates
1150 * a pool, we shouldn't have any problems here. The user won't be
1151 * able to unload the CTL module until client modules have
1152 * successfully unloaded.
1154 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1155 ctl_pool_free(pool);
1158 ctl_shutdown_thread(softc->work_thread);
1159 mtx_destroy(&softc->queue_lock);
1162 mtx_destroy(&softc->pool_lock);
1163 mtx_destroy(&softc->ctl_lock);
1165 destroy_dev(softc->dev);
1167 sysctl_ctx_free(&softc->sysctl_ctx);
1169 free(control_softc, M_DEVBUF);
1170 control_softc = NULL;
1173 printf("ctl: CAM Target Layer unloaded\n");
1177 ctl_module_event_handler(module_t mod, int what, void *arg)
1182 return (ctl_init());
1186 return (EOPNOTSUPP);
1191 * XXX KDM should we do some access checks here? Bump a reference count to
1192 * prevent a CTL module from being unloaded while someone has it open?
1195 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1201 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1207 ctl_port_enable(ctl_port_type port_type)
1209 struct ctl_softc *softc;
1210 struct ctl_frontend *fe;
1212 if (ctl_is_single == 0) {
1213 union ctl_ha_msg msg_info;
1217 printf("%s: HA mode, synchronizing frontend enable\n",
1220 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1221 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1222 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1223 printf("Sync msg send error retval %d\n", isc_retval);
1225 if (!rcv_sync_msg) {
1226 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1227 sizeof(msg_info), 1);
1230 printf("CTL:Frontend Enable\n");
1232 printf("%s: single mode, skipping frontend synchronization\n",
1237 softc = control_softc;
1239 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1240 if (port_type & fe->port_type)
1243 printf("port %d\n", fe->targ_port);
1245 ctl_frontend_online(fe);
1253 ctl_port_disable(ctl_port_type port_type)
1255 struct ctl_softc *softc;
1256 struct ctl_frontend *fe;
1258 softc = control_softc;
1260 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1261 if (port_type & fe->port_type)
1262 ctl_frontend_offline(fe);
1269 * Returns 0 for success, 1 for failure.
1270 * Currently the only failure mode is if there aren't enough entries
1271 * allocated. So, in case of a failure, look at num_entries_dropped,
1272 * reallocate and try again.
1275 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1276 int *num_entries_filled, int *num_entries_dropped,
1277 ctl_port_type port_type, int no_virtual)
1279 struct ctl_softc *softc;
1280 struct ctl_frontend *fe;
1281 int entries_dropped, entries_filled;
1285 softc = control_softc;
1289 entries_dropped = 0;
1292 mtx_lock(&softc->ctl_lock);
1293 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1294 struct ctl_port_entry *entry;
1296 if ((fe->port_type & port_type) == 0)
1299 if ((no_virtual != 0)
1300 && (fe->virtual_port != 0))
1303 if (entries_filled >= num_entries_alloced) {
1307 entry = &entries[i];
1309 entry->port_type = fe->port_type;
1310 strlcpy(entry->port_name, fe->port_name,
1311 sizeof(entry->port_name));
1312 entry->physical_port = fe->physical_port;
1313 entry->virtual_port = fe->virtual_port;
1314 entry->wwnn = fe->wwnn;
1315 entry->wwpn = fe->wwpn;
1321 mtx_unlock(&softc->ctl_lock);
1323 if (entries_dropped > 0)
1326 *num_entries_dropped = entries_dropped;
1327 *num_entries_filled = entries_filled;
1333 ctl_ioctl_online(void *arg)
1335 struct ctl_ioctl_info *ioctl_info;
1337 ioctl_info = (struct ctl_ioctl_info *)arg;
1339 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1343 ctl_ioctl_offline(void *arg)
1345 struct ctl_ioctl_info *ioctl_info;
1347 ioctl_info = (struct ctl_ioctl_info *)arg;
1349 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1353 * Remove an initiator by port number and initiator ID.
1354 * Returns 0 for success, 1 for failure.
1357 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1359 struct ctl_softc *softc;
1361 softc = control_softc;
1363 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1366 || (targ_port > CTL_MAX_PORTS)) {
1367 printf("%s: invalid port number %d\n", __func__, targ_port);
1370 if (iid > CTL_MAX_INIT_PER_PORT) {
1371 printf("%s: initiator ID %u > maximun %u!\n",
1372 __func__, iid, CTL_MAX_INIT_PER_PORT);
1376 mtx_lock(&softc->ctl_lock);
1378 softc->wwpn_iid[targ_port][iid].in_use = 0;
1380 mtx_unlock(&softc->ctl_lock);
1386 * Add an initiator to the initiator map.
1387 * Returns 0 for success, 1 for failure.
1390 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1392 struct ctl_softc *softc;
1395 softc = control_softc;
1397 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1402 || (targ_port > CTL_MAX_PORTS)) {
1403 printf("%s: invalid port number %d\n", __func__, targ_port);
1406 if (iid > CTL_MAX_INIT_PER_PORT) {
1407 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1408 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1412 mtx_lock(&softc->ctl_lock);
1414 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1416 * We don't treat this as an error.
1418 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1419 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1420 __func__, targ_port, iid, (uintmax_t)wwpn);
1425 * This is an error, but what do we do about it? The
1426 * driver is telling us we have a new WWPN for this
1427 * initiator ID, so we pretty much need to use it.
1429 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1430 "still at that address\n", __func__, targ_port, iid,
1432 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1435 * XXX KDM clear have_ca and ua_pending on each LUN for
1439 softc->wwpn_iid[targ_port][iid].in_use = 1;
1440 softc->wwpn_iid[targ_port][iid].iid = iid;
1441 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1442 softc->wwpn_iid[targ_port][iid].port = targ_port;
1446 mtx_unlock(&softc->ctl_lock);
1452 * XXX KDM should we pretend to do something in the target/lun
1453 * enable/disable functions?
1456 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1462 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1468 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1474 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1480 * Data movement routine for the CTL ioctl frontend port.
1483 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1485 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1486 struct ctl_sg_entry ext_entry, kern_entry;
1487 int ext_sglen, ext_sg_entries, kern_sg_entries;
1488 int ext_sg_start, ext_offset;
1489 int len_to_copy, len_copied;
1490 int kern_watermark, ext_watermark;
1491 int ext_sglist_malloced;
1494 ext_sglist_malloced = 0;
1498 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1501 * If this flag is set, fake the data transfer.
1503 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1504 ctsio->ext_data_filled = ctsio->ext_data_len;
1509 * To simplify things here, if we have a single buffer, stick it in
1510 * a S/G entry and just make it a single entry S/G list.
1512 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1515 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1517 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1519 ext_sglist_malloced = 1;
1520 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1522 ctl_set_internal_failure(ctsio,
1527 ext_sg_entries = ctsio->ext_sg_entries;
1529 for (i = 0; i < ext_sg_entries; i++) {
1530 if ((len_seen + ext_sglist[i].len) >=
1531 ctsio->ext_data_filled) {
1533 ext_offset = ctsio->ext_data_filled - len_seen;
1536 len_seen += ext_sglist[i].len;
1539 ext_sglist = &ext_entry;
1540 ext_sglist->addr = ctsio->ext_data_ptr;
1541 ext_sglist->len = ctsio->ext_data_len;
1544 ext_offset = ctsio->ext_data_filled;
1547 if (ctsio->kern_sg_entries > 0) {
1548 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1549 kern_sg_entries = ctsio->kern_sg_entries;
1551 kern_sglist = &kern_entry;
1552 kern_sglist->addr = ctsio->kern_data_ptr;
1553 kern_sglist->len = ctsio->kern_data_len;
1554 kern_sg_entries = 1;
1559 ext_watermark = ext_offset;
1561 for (i = ext_sg_start, j = 0;
1562 i < ext_sg_entries && j < kern_sg_entries;) {
1563 uint8_t *ext_ptr, *kern_ptr;
1565 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1566 kern_sglist[j].len - kern_watermark);
1568 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1569 ext_ptr = ext_ptr + ext_watermark;
1570 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1574 panic("need to implement bus address support");
1576 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1579 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1580 kern_ptr = kern_ptr + kern_watermark;
1582 kern_watermark += len_to_copy;
1583 ext_watermark += len_to_copy;
1585 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1587 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1588 "bytes to user\n", len_to_copy));
1589 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1590 "to %p\n", kern_ptr, ext_ptr));
1591 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1592 ctl_set_internal_failure(ctsio,
1598 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1599 "bytes from user\n", len_to_copy));
1600 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1601 "to %p\n", ext_ptr, kern_ptr));
1602 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1603 ctl_set_internal_failure(ctsio,
1610 len_copied += len_to_copy;
1612 if (ext_sglist[i].len == ext_watermark) {
1617 if (kern_sglist[j].len == kern_watermark) {
1623 ctsio->ext_data_filled += len_copied;
1625 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1626 "kern_sg_entries: %d\n", ext_sg_entries,
1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1629 "kern_data_len = %d\n", ctsio->ext_data_len,
1630 ctsio->kern_data_len));
1633 /* XXX KDM set residual?? */
1636 if (ext_sglist_malloced != 0)
1637 free(ext_sglist, M_CTL);
1639 return (CTL_RETVAL_COMPLETE);
1643 * Serialize a command that went down the "wrong" side, and so was sent to
1644 * this controller for execution. The logic is a little different than the
1645 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1646 * sent back to the other side, but in the success case, we execute the
1647 * command on this side (XFER mode) or tell the other side to execute it
1651 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1653 struct ctl_softc *ctl_softc;
1654 union ctl_ha_msg msg_info;
1655 struct ctl_lun *lun;
1659 ctl_softc = control_softc;
1661 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1662 lun = ctl_softc->ctl_luns[targ_lun];
1666 * Why isn't LUN defined? The other side wouldn't
1667 * send a cmd if the LUN is undefined.
1669 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1671 /* "Logical unit not supported" */
1672 ctl_set_sense_data(&msg_info.scsi.sense_data,
1674 /*sense_format*/SSD_TYPE_NONE,
1675 /*current_error*/ 1,
1676 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1681 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1682 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1683 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1684 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1685 msg_info.hdr.serializing_sc = NULL;
1686 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1687 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1688 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1694 mtx_lock(&lun->lun_lock);
1695 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1697 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1698 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1700 case CTL_ACTION_BLOCK:
1701 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1702 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1705 case CTL_ACTION_PASS:
1706 case CTL_ACTION_SKIP:
1707 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1708 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1709 ctl_enqueue_rtr((union ctl_io *)ctsio);
1712 /* send msg back to other side */
1713 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1714 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1715 msg_info.hdr.msg_type = CTL_MSG_R2R;
1717 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1719 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1720 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1724 case CTL_ACTION_OVERLAP:
1725 /* OVERLAPPED COMMANDS ATTEMPTED */
1726 ctl_set_sense_data(&msg_info.scsi.sense_data,
1728 /*sense_format*/SSD_TYPE_NONE,
1729 /*current_error*/ 1,
1730 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1735 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1736 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1737 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1738 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1739 msg_info.hdr.serializing_sc = NULL;
1740 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1742 printf("BAD JUJU:Major Bummer Overlap\n");
1744 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1746 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1747 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1750 case CTL_ACTION_OVERLAP_TAG:
1751 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1752 ctl_set_sense_data(&msg_info.scsi.sense_data,
1754 /*sense_format*/SSD_TYPE_NONE,
1755 /*current_error*/ 1,
1756 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1758 /*ascq*/ ctsio->tag_num & 0xff,
1761 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1762 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1763 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1764 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1765 msg_info.hdr.serializing_sc = NULL;
1766 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1768 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1770 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1772 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1773 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1776 case CTL_ACTION_ERROR:
1778 /* "Internal target failure" */
1779 ctl_set_sense_data(&msg_info.scsi.sense_data,
1781 /*sense_format*/SSD_TYPE_NONE,
1782 /*current_error*/ 1,
1783 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1788 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1789 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1790 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1791 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1792 msg_info.hdr.serializing_sc = NULL;
1793 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1795 printf("BAD JUJU:Major Bummer HW Error\n");
1797 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1800 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1804 mtx_unlock(&lun->lun_lock);
1809 ctl_ioctl_submit_wait(union ctl_io *io)
1811 struct ctl_fe_ioctl_params params;
1812 ctl_fe_ioctl_state last_state;
1817 bzero(¶ms, sizeof(params));
1819 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1820 cv_init(¶ms.sem, "ctlioccv");
1821 params.state = CTL_IOCTL_INPROG;
1822 last_state = params.state;
1824 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1826 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1828 /* This shouldn't happen */
1829 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1835 mtx_lock(¶ms.ioctl_mtx);
1837 * Check the state here, and don't sleep if the state has
1838 * already changed (i.e. wakeup has already occured, but we
1839 * weren't waiting yet).
1841 if (params.state == last_state) {
1842 /* XXX KDM cv_wait_sig instead? */
1843 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1845 last_state = params.state;
1847 switch (params.state) {
1848 case CTL_IOCTL_INPROG:
1849 /* Why did we wake up? */
1850 /* XXX KDM error here? */
1851 mtx_unlock(¶ms.ioctl_mtx);
1853 case CTL_IOCTL_DATAMOVE:
1854 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1857 * change last_state back to INPROG to avoid
1858 * deadlock on subsequent data moves.
1860 params.state = last_state = CTL_IOCTL_INPROG;
1862 mtx_unlock(¶ms.ioctl_mtx);
1863 ctl_ioctl_do_datamove(&io->scsiio);
1865 * Note that in some cases, most notably writes,
1866 * this will queue the I/O and call us back later.
1867 * In other cases, generally reads, this routine
1868 * will immediately call back and wake us up,
1869 * probably using our own context.
1871 io->scsiio.be_move_done(io);
1873 case CTL_IOCTL_DONE:
1874 mtx_unlock(¶ms.ioctl_mtx);
1875 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1879 mtx_unlock(¶ms.ioctl_mtx);
1880 /* XXX KDM error here? */
1883 } while (done == 0);
1885 mtx_destroy(¶ms.ioctl_mtx);
1886 cv_destroy(¶ms.sem);
1888 return (CTL_RETVAL_COMPLETE);
1892 ctl_ioctl_datamove(union ctl_io *io)
1894 struct ctl_fe_ioctl_params *params;
1896 params = (struct ctl_fe_ioctl_params *)
1897 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1899 mtx_lock(¶ms->ioctl_mtx);
1900 params->state = CTL_IOCTL_DATAMOVE;
1901 cv_broadcast(¶ms->sem);
1902 mtx_unlock(¶ms->ioctl_mtx);
1906 ctl_ioctl_done(union ctl_io *io)
1908 struct ctl_fe_ioctl_params *params;
1910 params = (struct ctl_fe_ioctl_params *)
1911 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1913 mtx_lock(¶ms->ioctl_mtx);
1914 params->state = CTL_IOCTL_DONE;
1915 cv_broadcast(¶ms->sem);
1916 mtx_unlock(¶ms->ioctl_mtx);
1920 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1922 struct ctl_fe_ioctl_startstop_info *sd_info;
1924 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1926 sd_info->hs_info.status = metatask->status;
1927 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1928 sd_info->hs_info.luns_complete =
1929 metatask->taskinfo.startstop.luns_complete;
1930 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1932 cv_broadcast(&sd_info->sem);
1936 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1938 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1940 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1942 mtx_lock(fe_bbr_info->lock);
1943 fe_bbr_info->bbr_info->status = metatask->status;
1944 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1945 fe_bbr_info->wakeup_done = 1;
1946 mtx_unlock(fe_bbr_info->lock);
1948 cv_broadcast(&fe_bbr_info->sem);
1952 * Returns 0 for success, errno for failure.
1955 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1956 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1963 mtx_lock(&lun->lun_lock);
1964 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1965 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1967 struct ctl_ooa_entry *entry;
1970 * If we've got more than we can fit, just count the
1971 * remaining entries.
1973 if (*cur_fill_num >= ooa_hdr->alloc_num)
1976 entry = &kern_entries[*cur_fill_num];
1978 entry->tag_num = io->scsiio.tag_num;
1979 entry->lun_num = lun->lun;
1981 entry->start_bt = io->io_hdr.start_bt;
1983 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1984 entry->cdb_len = io->scsiio.cdb_len;
1985 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1986 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1988 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1989 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1991 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1992 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1994 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1995 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1997 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1998 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2000 mtx_unlock(&lun->lun_lock);
2006 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2007 size_t error_str_len)
2011 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2013 if (copyin(user_addr, kptr, len) != 0) {
2014 snprintf(error_str, error_str_len, "Error copying %d bytes "
2015 "from user address %p to kernel address %p", len,
2025 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2029 if (be_args == NULL)
2032 for (i = 0; i < num_be_args; i++) {
2033 free(be_args[i].kname, M_CTL);
2034 free(be_args[i].kvalue, M_CTL);
2037 free(be_args, M_CTL);
2040 static struct ctl_be_arg *
2041 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2042 char *error_str, size_t error_str_len)
2044 struct ctl_be_arg *args;
2047 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2048 error_str, error_str_len);
2053 for (i = 0; i < num_be_args; i++) {
2054 args[i].kname = NULL;
2055 args[i].kvalue = NULL;
2058 for (i = 0; i < num_be_args; i++) {
2061 args[i].kname = ctl_copyin_alloc(args[i].name,
2062 args[i].namelen, error_str, error_str_len);
2063 if (args[i].kname == NULL)
2066 if (args[i].kname[args[i].namelen - 1] != '\0') {
2067 snprintf(error_str, error_str_len, "Argument %d "
2068 "name is not NUL-terminated", i);
2072 args[i].kvalue = NULL;
2074 tmpptr = ctl_copyin_alloc(args[i].value,
2075 args[i].vallen, error_str, error_str_len);
2079 args[i].kvalue = tmpptr;
2081 if ((args[i].flags & CTL_BEARG_ASCII)
2082 && (tmpptr[args[i].vallen - 1] != '\0')) {
2083 snprintf(error_str, error_str_len, "Argument %d "
2084 "value is not NUL-terminated", i);
2092 ctl_free_args(num_be_args, args);
2098 * Escape characters that are illegal or not recommended in XML.
2101 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2107 for (; *str; str++) {
2110 retval = sbuf_printf(sb, "&");
2113 retval = sbuf_printf(sb, ">");
2116 retval = sbuf_printf(sb, "<");
2119 retval = sbuf_putc(sb, *str);
2132 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2135 struct ctl_softc *softc;
2138 softc = control_softc;
2148 * If we haven't been "enabled", don't allow any SCSI I/O
2151 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2156 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2158 printf("ctl_ioctl: can't allocate ctl_io!\n");
2164 * Need to save the pool reference so it doesn't get
2165 * spammed by the user's ctl_io.
2167 pool_tmp = io->io_hdr.pool;
2169 memcpy(io, (void *)addr, sizeof(*io));
2171 io->io_hdr.pool = pool_tmp;
2173 * No status yet, so make sure the status is set properly.
2175 io->io_hdr.status = CTL_STATUS_NONE;
2178 * The user sets the initiator ID, target and LUN IDs.
2180 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2181 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2182 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2183 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2184 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2186 retval = ctl_ioctl_submit_wait(io);
2193 memcpy((void *)addr, io, sizeof(*io));
2195 /* return this to our pool */
2200 case CTL_ENABLE_PORT:
2201 case CTL_DISABLE_PORT:
2202 case CTL_SET_PORT_WWNS: {
2203 struct ctl_frontend *fe;
2204 struct ctl_port_entry *entry;
2206 entry = (struct ctl_port_entry *)addr;
2208 mtx_lock(&softc->ctl_lock);
2209 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2215 if ((entry->port_type == CTL_PORT_NONE)
2216 && (entry->targ_port == fe->targ_port)) {
2218 * If the user only wants to enable or
2219 * disable or set WWNs on a specific port,
2220 * do the operation and we're done.
2224 } else if (entry->port_type & fe->port_type) {
2226 * Compare the user's type mask with the
2227 * particular frontend type to see if we
2234 * Make sure the user isn't trying to set
2235 * WWNs on multiple ports at the same time.
2237 if (cmd == CTL_SET_PORT_WWNS) {
2238 printf("%s: Can't set WWNs on "
2239 "multiple ports\n", __func__);
2246 * XXX KDM we have to drop the lock here,
2247 * because the online/offline operations
2248 * can potentially block. We need to
2249 * reference count the frontends so they
2252 mtx_unlock(&softc->ctl_lock);
2254 if (cmd == CTL_ENABLE_PORT) {
2255 struct ctl_lun *lun;
2257 STAILQ_FOREACH(lun, &softc->lun_list,
2259 fe->lun_enable(fe->targ_lun_arg,
2264 ctl_frontend_online(fe);
2265 } else if (cmd == CTL_DISABLE_PORT) {
2266 struct ctl_lun *lun;
2268 ctl_frontend_offline(fe);
2270 STAILQ_FOREACH(lun, &softc->lun_list,
2279 mtx_lock(&softc->ctl_lock);
2281 if (cmd == CTL_SET_PORT_WWNS)
2282 ctl_frontend_set_wwns(fe,
2283 (entry->flags & CTL_PORT_WWNN_VALID) ?
2285 (entry->flags & CTL_PORT_WWPN_VALID) ?
2286 1 : 0, entry->wwpn);
2291 mtx_unlock(&softc->ctl_lock);
2294 case CTL_GET_PORT_LIST: {
2295 struct ctl_frontend *fe;
2296 struct ctl_port_list *list;
2299 list = (struct ctl_port_list *)addr;
2301 if (list->alloc_len != (list->alloc_num *
2302 sizeof(struct ctl_port_entry))) {
2303 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2304 "alloc_num %u * sizeof(struct ctl_port_entry) "
2305 "%zu\n", __func__, list->alloc_len,
2306 list->alloc_num, sizeof(struct ctl_port_entry));
2312 list->dropped_num = 0;
2314 mtx_lock(&softc->ctl_lock);
2315 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2316 struct ctl_port_entry entry, *list_entry;
2318 if (list->fill_num >= list->alloc_num) {
2319 list->dropped_num++;
2323 entry.port_type = fe->port_type;
2324 strlcpy(entry.port_name, fe->port_name,
2325 sizeof(entry.port_name));
2326 entry.targ_port = fe->targ_port;
2327 entry.physical_port = fe->physical_port;
2328 entry.virtual_port = fe->virtual_port;
2329 entry.wwnn = fe->wwnn;
2330 entry.wwpn = fe->wwpn;
2331 if (fe->status & CTL_PORT_STATUS_ONLINE)
2336 list_entry = &list->entries[i];
2338 retval = copyout(&entry, list_entry, sizeof(entry));
2340 printf("%s: CTL_GET_PORT_LIST: copyout "
2341 "returned %d\n", __func__, retval);
2346 list->fill_len += sizeof(entry);
2348 mtx_unlock(&softc->ctl_lock);
2351 * If this is non-zero, we had a copyout fault, so there's
2352 * probably no point in attempting to set the status inside
2358 if (list->dropped_num > 0)
2359 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2361 list->status = CTL_PORT_LIST_OK;
2364 case CTL_DUMP_OOA: {
2365 struct ctl_lun *lun;
2370 mtx_lock(&softc->ctl_lock);
2371 printf("Dumping OOA queues:\n");
2372 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2373 mtx_lock(&lun->lun_lock);
2374 for (io = (union ctl_io *)TAILQ_FIRST(
2375 &lun->ooa_queue); io != NULL;
2376 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2378 sbuf_new(&sb, printbuf, sizeof(printbuf),
2380 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2384 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2386 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2388 CTL_FLAG_ABORT) ? " ABORT" : "",
2390 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2391 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2393 printf("%s\n", sbuf_data(&sb));
2395 mtx_unlock(&lun->lun_lock);
2397 printf("OOA queues dump done\n");
2398 mtx_unlock(&softc->ctl_lock);
2402 struct ctl_lun *lun;
2403 struct ctl_ooa *ooa_hdr;
2404 struct ctl_ooa_entry *entries;
2405 uint32_t cur_fill_num;
2407 ooa_hdr = (struct ctl_ooa *)addr;
2409 if ((ooa_hdr->alloc_len == 0)
2410 || (ooa_hdr->alloc_num == 0)) {
2411 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2412 "must be non-zero\n", __func__,
2413 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2418 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2419 sizeof(struct ctl_ooa_entry))) {
2420 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2421 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2422 __func__, ooa_hdr->alloc_len,
2423 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2428 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2429 if (entries == NULL) {
2430 printf("%s: could not allocate %d bytes for OOA "
2431 "dump\n", __func__, ooa_hdr->alloc_len);
2436 mtx_lock(&softc->ctl_lock);
2437 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2438 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2439 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2440 mtx_unlock(&softc->ctl_lock);
2441 free(entries, M_CTL);
2442 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2443 __func__, (uintmax_t)ooa_hdr->lun_num);
2450 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2451 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2452 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2458 mtx_unlock(&softc->ctl_lock);
2459 free(entries, M_CTL);
2463 lun = softc->ctl_luns[ooa_hdr->lun_num];
2465 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2468 mtx_unlock(&softc->ctl_lock);
2470 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2471 ooa_hdr->fill_len = ooa_hdr->fill_num *
2472 sizeof(struct ctl_ooa_entry);
2473 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2475 printf("%s: error copying out %d bytes for OOA dump\n",
2476 __func__, ooa_hdr->fill_len);
2479 getbintime(&ooa_hdr->cur_bt);
2481 if (cur_fill_num > ooa_hdr->alloc_num) {
2482 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2483 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2485 ooa_hdr->dropped_num = 0;
2486 ooa_hdr->status = CTL_OOA_OK;
2489 free(entries, M_CTL);
2492 case CTL_CHECK_OOA: {
2494 struct ctl_lun *lun;
2495 struct ctl_ooa_info *ooa_info;
2498 ooa_info = (struct ctl_ooa_info *)addr;
2500 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2501 ooa_info->status = CTL_OOA_INVALID_LUN;
2504 mtx_lock(&softc->ctl_lock);
2505 lun = softc->ctl_luns[ooa_info->lun_id];
2507 mtx_unlock(&softc->ctl_lock);
2508 ooa_info->status = CTL_OOA_INVALID_LUN;
2511 mtx_lock(&lun->lun_lock);
2512 mtx_unlock(&softc->ctl_lock);
2513 ooa_info->num_entries = 0;
2514 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2515 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2516 &io->io_hdr, ooa_links)) {
2517 ooa_info->num_entries++;
2519 mtx_unlock(&lun->lun_lock);
2521 ooa_info->status = CTL_OOA_SUCCESS;
2525 case CTL_HARD_START:
2526 case CTL_HARD_STOP: {
2527 struct ctl_fe_ioctl_startstop_info ss_info;
2528 struct cfi_metatask *metatask;
2531 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2533 cv_init(&ss_info.sem, "hard start/stop cv" );
2535 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2536 if (metatask == NULL) {
2538 mtx_destroy(&hs_mtx);
2542 if (cmd == CTL_HARD_START)
2543 metatask->tasktype = CFI_TASK_STARTUP;
2545 metatask->tasktype = CFI_TASK_SHUTDOWN;
2547 metatask->callback = ctl_ioctl_hard_startstop_callback;
2548 metatask->callback_arg = &ss_info;
2550 cfi_action(metatask);
2552 /* Wait for the callback */
2554 cv_wait_sig(&ss_info.sem, &hs_mtx);
2555 mtx_unlock(&hs_mtx);
2558 * All information has been copied from the metatask by the
2559 * time cv_broadcast() is called, so we free the metatask here.
2561 cfi_free_metatask(metatask);
2563 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2565 mtx_destroy(&hs_mtx);
2569 struct ctl_bbrread_info *bbr_info;
2570 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2572 struct cfi_metatask *metatask;
2574 bbr_info = (struct ctl_bbrread_info *)addr;
2576 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2578 bzero(&bbr_mtx, sizeof(bbr_mtx));
2579 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2581 fe_bbr_info.bbr_info = bbr_info;
2582 fe_bbr_info.lock = &bbr_mtx;
2584 cv_init(&fe_bbr_info.sem, "BBR read cv");
2585 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2587 if (metatask == NULL) {
2588 mtx_destroy(&bbr_mtx);
2589 cv_destroy(&fe_bbr_info.sem);
2593 metatask->tasktype = CFI_TASK_BBRREAD;
2594 metatask->callback = ctl_ioctl_bbrread_callback;
2595 metatask->callback_arg = &fe_bbr_info;
2596 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2597 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2598 metatask->taskinfo.bbrread.len = bbr_info->len;
2600 cfi_action(metatask);
2603 while (fe_bbr_info.wakeup_done == 0)
2604 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2605 mtx_unlock(&bbr_mtx);
2607 bbr_info->status = metatask->status;
2608 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2609 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2610 memcpy(&bbr_info->sense_data,
2611 &metatask->taskinfo.bbrread.sense_data,
2612 ctl_min(sizeof(bbr_info->sense_data),
2613 sizeof(metatask->taskinfo.bbrread.sense_data)));
2615 cfi_free_metatask(metatask);
2617 mtx_destroy(&bbr_mtx);
2618 cv_destroy(&fe_bbr_info.sem);
2622 case CTL_DELAY_IO: {
2623 struct ctl_io_delay_info *delay_info;
2625 struct ctl_lun *lun;
2626 #endif /* CTL_IO_DELAY */
2628 delay_info = (struct ctl_io_delay_info *)addr;
2631 mtx_lock(&softc->ctl_lock);
2633 if ((delay_info->lun_id > CTL_MAX_LUNS)
2634 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2635 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2637 lun = softc->ctl_luns[delay_info->lun_id];
2638 mtx_lock(&lun->lun_lock);
2640 delay_info->status = CTL_DELAY_STATUS_OK;
2642 switch (delay_info->delay_type) {
2643 case CTL_DELAY_TYPE_CONT:
2645 case CTL_DELAY_TYPE_ONESHOT:
2648 delay_info->status =
2649 CTL_DELAY_STATUS_INVALID_TYPE;
2653 switch (delay_info->delay_loc) {
2654 case CTL_DELAY_LOC_DATAMOVE:
2655 lun->delay_info.datamove_type =
2656 delay_info->delay_type;
2657 lun->delay_info.datamove_delay =
2658 delay_info->delay_secs;
2660 case CTL_DELAY_LOC_DONE:
2661 lun->delay_info.done_type =
2662 delay_info->delay_type;
2663 lun->delay_info.done_delay =
2664 delay_info->delay_secs;
2667 delay_info->status =
2668 CTL_DELAY_STATUS_INVALID_LOC;
2671 mtx_unlock(&lun->lun_lock);
2674 mtx_unlock(&softc->ctl_lock);
2676 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2677 #endif /* CTL_IO_DELAY */
2680 case CTL_REALSYNC_SET: {
2683 syncstate = (int *)addr;
2685 mtx_lock(&softc->ctl_lock);
2686 switch (*syncstate) {
2688 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2691 softc->flags |= CTL_FLAG_REAL_SYNC;
2697 mtx_unlock(&softc->ctl_lock);
2700 case CTL_REALSYNC_GET: {
2703 syncstate = (int*)addr;
2705 mtx_lock(&softc->ctl_lock);
2706 if (softc->flags & CTL_FLAG_REAL_SYNC)
2710 mtx_unlock(&softc->ctl_lock);
2716 struct ctl_sync_info *sync_info;
2717 struct ctl_lun *lun;
2719 sync_info = (struct ctl_sync_info *)addr;
2721 mtx_lock(&softc->ctl_lock);
2722 lun = softc->ctl_luns[sync_info->lun_id];
2724 mtx_unlock(&softc->ctl_lock);
2725 sync_info->status = CTL_GS_SYNC_NO_LUN;
2728 * Get or set the sync interval. We're not bounds checking
2729 * in the set case, hopefully the user won't do something
2732 mtx_lock(&lun->lun_lock);
2733 mtx_unlock(&softc->ctl_lock);
2734 if (cmd == CTL_GETSYNC)
2735 sync_info->sync_interval = lun->sync_interval;
2737 lun->sync_interval = sync_info->sync_interval;
2738 mtx_unlock(&lun->lun_lock);
2740 sync_info->status = CTL_GS_SYNC_OK;
2744 case CTL_GETSTATS: {
2745 struct ctl_stats *stats;
2746 struct ctl_lun *lun;
2749 stats = (struct ctl_stats *)addr;
2751 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2753 stats->status = CTL_SS_NEED_MORE_SPACE;
2754 stats->num_luns = softc->num_luns;
2758 * XXX KDM no locking here. If the LUN list changes,
2759 * things can blow up.
2761 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2762 i++, lun = STAILQ_NEXT(lun, links)) {
2763 retval = copyout(&lun->stats, &stats->lun_stats[i],
2764 sizeof(lun->stats));
2768 stats->num_luns = softc->num_luns;
2769 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2771 stats->status = CTL_SS_OK;
2773 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2775 stats->flags = CTL_STATS_FLAG_NONE;
2777 getnanouptime(&stats->timestamp);
2780 case CTL_ERROR_INJECT: {
2781 struct ctl_error_desc *err_desc, *new_err_desc;
2782 struct ctl_lun *lun;
2784 err_desc = (struct ctl_error_desc *)addr;
2786 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2788 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2790 mtx_lock(&softc->ctl_lock);
2791 lun = softc->ctl_luns[err_desc->lun_id];
2793 mtx_unlock(&softc->ctl_lock);
2794 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2795 __func__, (uintmax_t)err_desc->lun_id);
2799 mtx_lock(&lun->lun_lock);
2800 mtx_unlock(&softc->ctl_lock);
2803 * We could do some checking here to verify the validity
2804 * of the request, but given the complexity of error
2805 * injection requests, the checking logic would be fairly
2808 * For now, if the request is invalid, it just won't get
2809 * executed and might get deleted.
2811 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2814 * XXX KDM check to make sure the serial number is unique,
2815 * in case we somehow manage to wrap. That shouldn't
2816 * happen for a very long time, but it's the right thing to
2819 new_err_desc->serial = lun->error_serial;
2820 err_desc->serial = lun->error_serial;
2821 lun->error_serial++;
2823 mtx_unlock(&lun->lun_lock);
2826 case CTL_ERROR_INJECT_DELETE: {
2827 struct ctl_error_desc *delete_desc, *desc, *desc2;
2828 struct ctl_lun *lun;
2831 delete_desc = (struct ctl_error_desc *)addr;
2834 mtx_lock(&softc->ctl_lock);
2835 lun = softc->ctl_luns[delete_desc->lun_id];
2837 mtx_unlock(&softc->ctl_lock);
2838 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2839 __func__, (uintmax_t)delete_desc->lun_id);
2843 mtx_lock(&lun->lun_lock);
2844 mtx_unlock(&softc->ctl_lock);
2845 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2846 if (desc->serial != delete_desc->serial)
2849 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2854 mtx_unlock(&lun->lun_lock);
2855 if (delete_done == 0) {
2856 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2857 "error serial %ju on LUN %u\n", __func__,
2858 delete_desc->serial, delete_desc->lun_id);
2864 case CTL_DUMP_STRUCTS: {
2866 struct ctl_frontend *fe;
2868 printf("CTL IID to WWPN map start:\n");
2869 for (i = 0; i < CTL_MAX_PORTS; i++) {
2870 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2871 if (softc->wwpn_iid[i][j].in_use == 0)
2874 printf("port %d iid %u WWPN %#jx\n",
2875 softc->wwpn_iid[i][j].port,
2876 softc->wwpn_iid[i][j].iid,
2877 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2880 printf("CTL IID to WWPN map end\n");
2881 printf("CTL Persistent Reservation information start:\n");
2882 for (i = 0; i < CTL_MAX_LUNS; i++) {
2883 struct ctl_lun *lun;
2885 lun = softc->ctl_luns[i];
2888 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2891 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2892 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2893 if (lun->per_res[j+k].registered == 0)
2895 printf("LUN %d port %d iid %d key "
2897 (uintmax_t)scsi_8btou64(
2898 lun->per_res[j+k].res_key.key));
2902 printf("CTL Persistent Reservation information end\n");
2903 printf("CTL Frontends:\n");
2905 * XXX KDM calling this without a lock. We'd likely want
2906 * to drop the lock before calling the frontend's dump
2909 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2910 printf("Frontend %s Type %u pport %d vport %d WWNN "
2911 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2912 fe->physical_port, fe->virtual_port,
2913 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2916 * Frontends are not required to support the dump
2919 if (fe->fe_dump == NULL)
2924 printf("CTL Frontend information end\n");
2928 struct ctl_lun_req *lun_req;
2929 struct ctl_backend_driver *backend;
2931 lun_req = (struct ctl_lun_req *)addr;
2933 backend = ctl_backend_find(lun_req->backend);
2934 if (backend == NULL) {
2935 lun_req->status = CTL_LUN_ERROR;
2936 snprintf(lun_req->error_str,
2937 sizeof(lun_req->error_str),
2938 "Backend \"%s\" not found.",
2942 if (lun_req->num_be_args > 0) {
2943 lun_req->kern_be_args = ctl_copyin_args(
2944 lun_req->num_be_args,
2947 sizeof(lun_req->error_str));
2948 if (lun_req->kern_be_args == NULL) {
2949 lun_req->status = CTL_LUN_ERROR;
2954 retval = backend->ioctl(dev, cmd, addr, flag, td);
2956 if (lun_req->num_be_args > 0) {
2957 ctl_free_args(lun_req->num_be_args,
2958 lun_req->kern_be_args);
2962 case CTL_LUN_LIST: {
2964 struct ctl_lun *lun;
2965 struct ctl_lun_list *list;
2966 struct ctl_be_lun_option *opt;
2968 list = (struct ctl_lun_list *)addr;
2971 * Allocate a fixed length sbuf here, based on the length
2972 * of the user's buffer. We could allocate an auto-extending
2973 * buffer, and then tell the user how much larger our
2974 * amount of data is than his buffer, but that presents
2977 * 1. The sbuf(9) routines use a blocking malloc, and so
2978 * we can't hold a lock while calling them with an
2979 * auto-extending buffer.
2981 * 2. There is not currently a LUN reference counting
2982 * mechanism, outside of outstanding transactions on
2983 * the LUN's OOA queue. So a LUN could go away on us
2984 * while we're getting the LUN number, backend-specific
2985 * information, etc. Thus, given the way things
2986 * currently work, we need to hold the CTL lock while
2987 * grabbing LUN information.
2989 * So, from the user's standpoint, the best thing to do is
2990 * allocate what he thinks is a reasonable buffer length,
2991 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2992 * double the buffer length and try again. (And repeat
2993 * that until he succeeds.)
2995 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2997 list->status = CTL_LUN_LIST_ERROR;
2998 snprintf(list->error_str, sizeof(list->error_str),
2999 "Unable to allocate %d bytes for LUN list",
3004 sbuf_printf(sb, "<ctllunlist>\n");
3006 mtx_lock(&softc->ctl_lock);
3007 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3008 mtx_lock(&lun->lun_lock);
3009 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3010 (uintmax_t)lun->lun);
3013 * Bail out as soon as we see that we've overfilled
3019 retval = sbuf_printf(sb, "<backend_type>%s"
3020 "</backend_type>\n",
3021 (lun->backend == NULL) ? "none" :
3022 lun->backend->name);
3027 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3028 lun->be_lun->lun_type);
3033 if (lun->backend == NULL) {
3034 retval = sbuf_printf(sb, "</lun>\n");
3040 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3041 (lun->be_lun->maxlba > 0) ?
3042 lun->be_lun->maxlba + 1 : 0);
3047 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3048 lun->be_lun->blocksize);
3053 retval = sbuf_printf(sb, "<serial_number>");
3058 retval = ctl_sbuf_printf_esc(sb,
3059 lun->be_lun->serial_num);
3064 retval = sbuf_printf(sb, "</serial_number>\n");
3069 retval = sbuf_printf(sb, "<device_id>");
3074 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3079 retval = sbuf_printf(sb, "</device_id>\n");
3084 if (lun->backend->lun_info != NULL) {
3085 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3089 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3090 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3095 retval = sbuf_printf(sb, "</lun>\n");
3099 mtx_unlock(&lun->lun_lock);
3102 mtx_unlock(&lun->lun_lock);
3103 mtx_unlock(&softc->ctl_lock);
3106 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3109 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3110 snprintf(list->error_str, sizeof(list->error_str),
3111 "Out of space, %d bytes is too small",
3118 retval = copyout(sbuf_data(sb), list->lun_xml,
3121 list->fill_len = sbuf_len(sb) + 1;
3122 list->status = CTL_LUN_LIST_OK;
3127 struct ctl_iscsi *ci;
3128 struct ctl_frontend *fe;
3130 ci = (struct ctl_iscsi *)addr;
3132 mtx_lock(&softc->ctl_lock);
3133 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3134 if (strcmp(fe->port_name, "iscsi") == 0)
3137 mtx_unlock(&softc->ctl_lock);
3140 ci->status = CTL_ISCSI_ERROR;
3141 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3145 retval = fe->ioctl(dev, cmd, addr, flag, td);
3149 /* XXX KDM should we fix this? */
3151 struct ctl_backend_driver *backend;
3158 * We encode the backend type as the ioctl type for backend
3159 * ioctls. So parse it out here, and then search for a
3160 * backend of this type.
3162 type = _IOC_TYPE(cmd);
3164 STAILQ_FOREACH(backend, &softc->be_list, links) {
3165 if (backend->type == type) {
3171 printf("ctl: unknown ioctl command %#lx or backend "
3176 retval = backend->ioctl(dev, cmd, addr, flag, td);
3186 ctl_get_initindex(struct ctl_nexus *nexus)
3188 if (nexus->targ_port < CTL_MAX_PORTS)
3189 return (nexus->initid.id +
3190 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3192 return (nexus->initid.id +
3193 ((nexus->targ_port - CTL_MAX_PORTS) *
3194 CTL_MAX_INIT_PER_PORT));
3198 ctl_get_resindex(struct ctl_nexus *nexus)
3200 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3204 ctl_port_idx(int port_num)
3206 if (port_num < CTL_MAX_PORTS)
3209 return(port_num - CTL_MAX_PORTS);
3213 * Note: This only works for bitmask sizes that are at least 32 bits, and
3214 * that are a power of 2.
3217 ctl_ffz(uint32_t *mask, uint32_t size)
3219 uint32_t num_chunks, num_pieces;
3222 num_chunks = (size >> 5);
3223 if (num_chunks == 0)
3225 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3227 for (i = 0; i < num_chunks; i++) {
3228 for (j = 0; j < num_pieces; j++) {
3229 if ((mask[i] & (1 << j)) == 0)
3230 return ((i << 5) + j);
3238 ctl_set_mask(uint32_t *mask, uint32_t bit)
3240 uint32_t chunk, piece;
3243 piece = bit % (sizeof(uint32_t) * 8);
3245 if ((mask[chunk] & (1 << piece)) != 0)
3248 mask[chunk] |= (1 << piece);
3254 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3256 uint32_t chunk, piece;
3259 piece = bit % (sizeof(uint32_t) * 8);
3261 if ((mask[chunk] & (1 << piece)) == 0)
3264 mask[chunk] &= ~(1 << piece);
3270 ctl_is_set(uint32_t *mask, uint32_t bit)
3272 uint32_t chunk, piece;
3275 piece = bit % (sizeof(uint32_t) * 8);
3277 if ((mask[chunk] & (1 << piece)) == 0)
3285 * The bus, target and lun are optional, they can be filled in later.
3286 * can_wait is used to determine whether we can wait on the malloc or not.
3289 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3290 uint32_t targ_lun, int can_wait)
3295 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3297 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3300 io->io_hdr.io_type = io_type;
3301 io->io_hdr.targ_port = targ_port;
3303 * XXX KDM this needs to change/go away. We need to move
3304 * to a preallocated pool of ctl_scsiio structures.
3306 io->io_hdr.nexus.targ_target.id = targ_target;
3307 io->io_hdr.nexus.targ_lun = targ_lun;
3314 ctl_kfree_io(union ctl_io *io)
3321 * ctl_softc, pool_type, total_ctl_io are passed in.
3322 * npool is passed out.
3325 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3326 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3329 union ctl_io *cur_io, *next_io;
3330 struct ctl_io_pool *pool;
3335 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3342 pool->type = pool_type;
3343 pool->ctl_softc = ctl_softc;
3345 mtx_lock(&ctl_softc->pool_lock);
3346 pool->id = ctl_softc->cur_pool_id++;
3347 mtx_unlock(&ctl_softc->pool_lock);
3349 pool->flags = CTL_POOL_FLAG_NONE;
3350 pool->refcount = 1; /* Reference for validity. */
3351 STAILQ_INIT(&pool->free_queue);
3354 * XXX KDM other options here:
3355 * - allocate a page at a time
3356 * - allocate one big chunk of memory.
3357 * Page allocation might work well, but would take a little more
3360 for (i = 0; i < total_ctl_io; i++) {
3361 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3363 if (cur_io == NULL) {
3367 cur_io->io_hdr.pool = pool;
3368 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3369 pool->total_ctl_io++;
3370 pool->free_ctl_io++;
3374 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3375 cur_io != NULL; cur_io = next_io) {
3376 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3378 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3380 free(cur_io, M_CTL);
3386 mtx_lock(&ctl_softc->pool_lock);
3387 ctl_softc->num_pools++;
3388 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3390 * Increment our usage count if this is an external consumer, so we
3391 * can't get unloaded until the external consumer (most likely a
3392 * FETD) unloads and frees his pool.
3394 * XXX KDM will this increment the caller's module use count, or
3398 if ((pool_type != CTL_POOL_EMERGENCY)
3399 && (pool_type != CTL_POOL_INTERNAL)
3400 && (pool_type != CTL_POOL_IOCTL)
3401 && (pool_type != CTL_POOL_4OTHERSC))
3405 mtx_unlock(&ctl_softc->pool_lock);
3415 ctl_pool_acquire(struct ctl_io_pool *pool)
3418 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3420 if (pool->flags & CTL_POOL_FLAG_INVALID)
3429 ctl_pool_release(struct ctl_io_pool *pool)
3431 struct ctl_softc *ctl_softc = pool->ctl_softc;
3434 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3436 if (--pool->refcount != 0)
3439 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3440 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3445 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3446 ctl_softc->num_pools--;
3449 * XXX KDM will this decrement the caller's usage count or mine?
3452 if ((pool->type != CTL_POOL_EMERGENCY)
3453 && (pool->type != CTL_POOL_INTERNAL)
3454 && (pool->type != CTL_POOL_IOCTL))
3462 ctl_pool_free(struct ctl_io_pool *pool)
3464 struct ctl_softc *ctl_softc;
3469 ctl_softc = pool->ctl_softc;
3470 mtx_lock(&ctl_softc->pool_lock);
3471 pool->flags |= CTL_POOL_FLAG_INVALID;
3472 ctl_pool_release(pool);
3473 mtx_unlock(&ctl_softc->pool_lock);
3477 * This routine does not block (except for spinlocks of course).
3478 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3482 ctl_alloc_io(void *pool_ref)
3485 struct ctl_softc *ctl_softc;
3486 struct ctl_io_pool *pool, *npool;
3487 struct ctl_io_pool *emergency_pool;
3489 pool = (struct ctl_io_pool *)pool_ref;
3492 printf("%s: pool is NULL\n", __func__);
3496 emergency_pool = NULL;
3498 ctl_softc = pool->ctl_softc;
3500 mtx_lock(&ctl_softc->pool_lock);
3502 * First, try to get the io structure from the user's pool.
3504 if (ctl_pool_acquire(pool) == 0) {
3505 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3507 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3508 pool->total_allocated++;
3509 pool->free_ctl_io--;
3510 mtx_unlock(&ctl_softc->pool_lock);
3513 ctl_pool_release(pool);
3516 * If he doesn't have any io structures left, search for an
3517 * emergency pool and grab one from there.
3519 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3520 if (npool->type != CTL_POOL_EMERGENCY)
3523 if (ctl_pool_acquire(npool) != 0)
3526 emergency_pool = npool;
3528 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3530 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3531 npool->total_allocated++;
3532 npool->free_ctl_io--;
3533 mtx_unlock(&ctl_softc->pool_lock);
3536 ctl_pool_release(npool);
3539 /* Drop the spinlock before we malloc */
3540 mtx_unlock(&ctl_softc->pool_lock);
3543 * The emergency pool (if it exists) didn't have one, so try an
3544 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3546 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3549 * If the emergency pool exists but is empty, add this
3550 * ctl_io to its list when it gets freed.
3552 if (emergency_pool != NULL) {
3553 mtx_lock(&ctl_softc->pool_lock);
3554 if (ctl_pool_acquire(emergency_pool) == 0) {
3555 io->io_hdr.pool = emergency_pool;
3556 emergency_pool->total_ctl_io++;
3558 * Need to bump this, otherwise
3559 * total_allocated and total_freed won't
3560 * match when we no longer have anything
3563 emergency_pool->total_allocated++;
3565 mtx_unlock(&ctl_softc->pool_lock);
3567 io->io_hdr.pool = NULL;
3574 ctl_free_io(union ctl_io *io)
3580 * If this ctl_io has a pool, return it to that pool.
3582 if (io->io_hdr.pool != NULL) {
3583 struct ctl_io_pool *pool;
3585 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3586 mtx_lock(&pool->ctl_softc->pool_lock);
3587 io->io_hdr.io_type = 0xff;
3588 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3589 pool->total_freed++;
3590 pool->free_ctl_io++;
3591 ctl_pool_release(pool);
3592 mtx_unlock(&pool->ctl_softc->pool_lock);
3595 * Otherwise, just free it. We probably malloced it and
3596 * the emergency pool wasn't available.
3604 ctl_zero_io(union ctl_io *io)
3612 * May need to preserve linked list pointers at some point too.
3614 pool_ref = io->io_hdr.pool;
3616 memset(io, 0, sizeof(*io));
3618 io->io_hdr.pool = pool_ref;
3622 * This routine is currently used for internal copies of ctl_ios that need
3623 * to persist for some reason after we've already returned status to the
3624 * FETD. (Thus the flag set.)
3627 * Note that this makes a blind copy of all fields in the ctl_io, except
3628 * for the pool reference. This includes any memory that has been
3629 * allocated! That memory will no longer be valid after done has been
3630 * called, so this would be VERY DANGEROUS for command that actually does
3631 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3632 * start and stop commands, which don't transfer any data, so this is not a
3633 * problem. If it is used for anything else, the caller would also need to
3634 * allocate data buffer space and this routine would need to be modified to
3635 * copy the data buffer(s) as well.
3638 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3647 * May need to preserve linked list pointers at some point too.
3649 pool_ref = dest->io_hdr.pool;
3651 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3653 dest->io_hdr.pool = pool_ref;
3655 * We need to know that this is an internal copy, and doesn't need
3656 * to get passed back to the FETD that allocated it.
3658 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3663 ctl_update_power_subpage(struct copan_power_subpage *page)
3665 int num_luns, num_partitions, config_type;
3666 struct ctl_softc *softc;
3667 cs_BOOL_t aor_present, shelf_50pct_power;
3668 cs_raidset_personality_t rs_type;
3669 int max_active_luns;
3671 softc = control_softc;
3673 /* subtract out the processor LUN */
3674 num_luns = softc->num_luns - 1;
3676 * Default to 7 LUNs active, which was the only number we allowed
3679 max_active_luns = 7;
3681 num_partitions = config_GetRsPartitionInfo();
3682 config_type = config_GetConfigType();
3683 shelf_50pct_power = config_GetShelfPowerMode();
3684 aor_present = config_IsAorRsPresent();
3686 rs_type = ddb_GetRsRaidType(1);
3687 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3688 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3689 EPRINT(0, "Unsupported RS type %d!", rs_type);
3693 page->total_luns = num_luns;
3695 switch (config_type) {
3698 * In a 40 drive configuration, it doesn't matter what DC
3699 * cards we have, whether we have AOR enabled or not,
3700 * partitioning or not, or what type of RAIDset we have.
3701 * In that scenario, we can power up every LUN we present
3704 max_active_luns = num_luns;
3708 if (shelf_50pct_power == CS_FALSE) {
3710 if (aor_present == CS_TRUE) {
3712 CS_RAIDSET_PERSONALITY_RAID5) {
3713 max_active_luns = 7;
3714 } else if (rs_type ==
3715 CS_RAIDSET_PERSONALITY_RAID1){
3716 max_active_luns = 14;
3718 /* XXX KDM now what?? */
3722 CS_RAIDSET_PERSONALITY_RAID5) {
3723 max_active_luns = 8;
3724 } else if (rs_type ==
3725 CS_RAIDSET_PERSONALITY_RAID1){
3726 max_active_luns = 16;
3728 /* XXX KDM now what?? */
3734 * With 50% power in a 64 drive configuration, we
3735 * can power all LUNs we present.
3737 max_active_luns = num_luns;
3741 if (shelf_50pct_power == CS_FALSE) {
3743 if (aor_present == CS_TRUE) {
3745 CS_RAIDSET_PERSONALITY_RAID5) {
3746 max_active_luns = 7;
3747 } else if (rs_type ==
3748 CS_RAIDSET_PERSONALITY_RAID1){
3749 max_active_luns = 14;
3751 /* XXX KDM now what?? */
3755 CS_RAIDSET_PERSONALITY_RAID5) {
3756 max_active_luns = 8;
3757 } else if (rs_type ==
3758 CS_RAIDSET_PERSONALITY_RAID1){
3759 max_active_luns = 16;
3761 /* XXX KDM now what?? */
3766 if (aor_present == CS_TRUE) {
3768 CS_RAIDSET_PERSONALITY_RAID5) {
3769 max_active_luns = 14;
3770 } else if (rs_type ==
3771 CS_RAIDSET_PERSONALITY_RAID1){
3773 * We're assuming here that disk
3774 * caching is enabled, and so we're
3775 * able to power up half of each
3776 * LUN, and cache all writes.
3778 max_active_luns = num_luns;
3780 /* XXX KDM now what?? */
3784 CS_RAIDSET_PERSONALITY_RAID5) {
3785 max_active_luns = 15;
3786 } else if (rs_type ==
3787 CS_RAIDSET_PERSONALITY_RAID1){
3788 max_active_luns = 30;
3790 /* XXX KDM now what?? */
3797 * In this case, we have an unknown configuration, so we
3798 * just use the default from above.
3803 page->max_active_luns = max_active_luns;
3805 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3806 page->total_luns, page->max_active_luns);
3809 #endif /* NEEDTOPORT */
3812 * This routine could be used in the future to load default and/or saved
3813 * mode page parameters for a particuar lun.
3816 ctl_init_page_index(struct ctl_lun *lun)
3819 struct ctl_page_index *page_index;
3820 struct ctl_softc *softc;
3822 memcpy(&lun->mode_pages.index, page_index_template,
3823 sizeof(page_index_template));
3825 softc = lun->ctl_softc;
3827 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3829 page_index = &lun->mode_pages.index[i];
3831 * If this is a disk-only mode page, there's no point in
3832 * setting it up. For some pages, we have to have some
3833 * basic information about the disk in order to calculate the
3836 if ((lun->be_lun->lun_type != T_DIRECT)
3837 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3840 switch (page_index->page_code & SMPH_PC_MASK) {
3841 case SMS_FORMAT_DEVICE_PAGE: {
3842 struct scsi_format_page *format_page;
3844 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3845 panic("subpage is incorrect!");
3848 * Sectors per track are set above. Bytes per
3849 * sector need to be set here on a per-LUN basis.
3851 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3852 &format_page_default,
3853 sizeof(format_page_default));
3854 memcpy(&lun->mode_pages.format_page[
3855 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3856 sizeof(format_page_changeable));
3857 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3858 &format_page_default,
3859 sizeof(format_page_default));
3860 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3861 &format_page_default,
3862 sizeof(format_page_default));
3864 format_page = &lun->mode_pages.format_page[
3866 scsi_ulto2b(lun->be_lun->blocksize,
3867 format_page->bytes_per_sector);
3869 format_page = &lun->mode_pages.format_page[
3871 scsi_ulto2b(lun->be_lun->blocksize,
3872 format_page->bytes_per_sector);
3874 format_page = &lun->mode_pages.format_page[
3876 scsi_ulto2b(lun->be_lun->blocksize,
3877 format_page->bytes_per_sector);
3879 page_index->page_data =
3880 (uint8_t *)lun->mode_pages.format_page;
3883 case SMS_RIGID_DISK_PAGE: {
3884 struct scsi_rigid_disk_page *rigid_disk_page;
3885 uint32_t sectors_per_cylinder;
3889 #endif /* !__XSCALE__ */
3891 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3892 panic("invalid subpage value %d",
3893 page_index->subpage);
3896 * Rotation rate and sectors per track are set
3897 * above. We calculate the cylinders here based on
3898 * capacity. Due to the number of heads and
3899 * sectors per track we're using, smaller arrays
3900 * may turn out to have 0 cylinders. Linux and
3901 * FreeBSD don't pay attention to these mode pages
3902 * to figure out capacity, but Solaris does. It
3903 * seems to deal with 0 cylinders just fine, and
3904 * works out a fake geometry based on the capacity.
3906 memcpy(&lun->mode_pages.rigid_disk_page[
3907 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3908 sizeof(rigid_disk_page_default));
3909 memcpy(&lun->mode_pages.rigid_disk_page[
3910 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3911 sizeof(rigid_disk_page_changeable));
3912 memcpy(&lun->mode_pages.rigid_disk_page[
3913 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3914 sizeof(rigid_disk_page_default));
3915 memcpy(&lun->mode_pages.rigid_disk_page[
3916 CTL_PAGE_SAVED], &rigid_disk_page_default,
3917 sizeof(rigid_disk_page_default));
3919 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3923 * The divide method here will be more accurate,
3924 * probably, but results in floating point being
3925 * used in the kernel on i386 (__udivdi3()). On the
3926 * XScale, though, __udivdi3() is implemented in
3929 * The shift method for cylinder calculation is
3930 * accurate if sectors_per_cylinder is a power of
3931 * 2. Otherwise it might be slightly off -- you
3932 * might have a bit of a truncation problem.
3935 cylinders = (lun->be_lun->maxlba + 1) /
3936 sectors_per_cylinder;
3938 for (shift = 31; shift > 0; shift--) {
3939 if (sectors_per_cylinder & (1 << shift))
3942 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3946 * We've basically got 3 bytes, or 24 bits for the
3947 * cylinder size in the mode page. If we're over,
3948 * just round down to 2^24.
3950 if (cylinders > 0xffffff)
3951 cylinders = 0xffffff;
3953 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3955 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3957 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3959 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3961 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3963 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3965 page_index->page_data =
3966 (uint8_t *)lun->mode_pages.rigid_disk_page;
3969 case SMS_CACHING_PAGE: {
3971 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3972 panic("invalid subpage value %d",
3973 page_index->subpage);
3975 * Defaults should be okay here, no calculations
3978 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3979 &caching_page_default,
3980 sizeof(caching_page_default));
3981 memcpy(&lun->mode_pages.caching_page[
3982 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3983 sizeof(caching_page_changeable));
3984 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3985 &caching_page_default,
3986 sizeof(caching_page_default));
3987 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3988 &caching_page_default,
3989 sizeof(caching_page_default));
3990 page_index->page_data =
3991 (uint8_t *)lun->mode_pages.caching_page;
3994 case SMS_CONTROL_MODE_PAGE: {
3996 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3997 panic("invalid subpage value %d",
3998 page_index->subpage);
4001 * Defaults should be okay here, no calculations
4004 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4005 &control_page_default,
4006 sizeof(control_page_default));
4007 memcpy(&lun->mode_pages.control_page[
4008 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4009 sizeof(control_page_changeable));
4010 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4011 &control_page_default,
4012 sizeof(control_page_default));
4013 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4014 &control_page_default,
4015 sizeof(control_page_default));
4016 page_index->page_data =
4017 (uint8_t *)lun->mode_pages.control_page;
4021 case SMS_VENDOR_SPECIFIC_PAGE:{
4022 switch (page_index->subpage) {
4023 case PWR_SUBPAGE_CODE: {
4024 struct copan_power_subpage *current_page,
4027 memcpy(&lun->mode_pages.power_subpage[
4029 &power_page_default,
4030 sizeof(power_page_default));
4031 memcpy(&lun->mode_pages.power_subpage[
4032 CTL_PAGE_CHANGEABLE],
4033 &power_page_changeable,
4034 sizeof(power_page_changeable));
4035 memcpy(&lun->mode_pages.power_subpage[
4037 &power_page_default,
4038 sizeof(power_page_default));
4039 memcpy(&lun->mode_pages.power_subpage[
4041 &power_page_default,
4042 sizeof(power_page_default));
4043 page_index->page_data =
4044 (uint8_t *)lun->mode_pages.power_subpage;
4046 current_page = (struct copan_power_subpage *)
4047 (page_index->page_data +
4048 (page_index->page_len *
4050 saved_page = (struct copan_power_subpage *)
4051 (page_index->page_data +
4052 (page_index->page_len *
4056 case APS_SUBPAGE_CODE: {
4057 struct copan_aps_subpage *current_page,
4060 // This gets set multiple times but
4061 // it should always be the same. It's
4062 // only done during init so who cares.
4063 index_to_aps_page = i;
4065 memcpy(&lun->mode_pages.aps_subpage[
4068 sizeof(aps_page_default));
4069 memcpy(&lun->mode_pages.aps_subpage[
4070 CTL_PAGE_CHANGEABLE],
4071 &aps_page_changeable,
4072 sizeof(aps_page_changeable));
4073 memcpy(&lun->mode_pages.aps_subpage[
4076 sizeof(aps_page_default));
4077 memcpy(&lun->mode_pages.aps_subpage[
4080 sizeof(aps_page_default));
4081 page_index->page_data =
4082 (uint8_t *)lun->mode_pages.aps_subpage;
4084 current_page = (struct copan_aps_subpage *)
4085 (page_index->page_data +
4086 (page_index->page_len *
4088 saved_page = (struct copan_aps_subpage *)
4089 (page_index->page_data +
4090 (page_index->page_len *
4094 case DBGCNF_SUBPAGE_CODE: {
4095 struct copan_debugconf_subpage *current_page,
4098 memcpy(&lun->mode_pages.debugconf_subpage[
4100 &debugconf_page_default,
4101 sizeof(debugconf_page_default));
4102 memcpy(&lun->mode_pages.debugconf_subpage[
4103 CTL_PAGE_CHANGEABLE],
4104 &debugconf_page_changeable,
4105 sizeof(debugconf_page_changeable));
4106 memcpy(&lun->mode_pages.debugconf_subpage[
4108 &debugconf_page_default,
4109 sizeof(debugconf_page_default));
4110 memcpy(&lun->mode_pages.debugconf_subpage[
4112 &debugconf_page_default,
4113 sizeof(debugconf_page_default));
4114 page_index->page_data =
4115 (uint8_t *)lun->mode_pages.debugconf_subpage;
4117 current_page = (struct copan_debugconf_subpage *)
4118 (page_index->page_data +
4119 (page_index->page_len *
4121 saved_page = (struct copan_debugconf_subpage *)
4122 (page_index->page_data +
4123 (page_index->page_len *
4128 panic("invalid subpage value %d",
4129 page_index->subpage);
4135 panic("invalid page value %d",
4136 page_index->page_code & SMPH_PC_MASK);
4141 return (CTL_RETVAL_COMPLETE);
4148 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4149 * wants us to allocate the LUN and he can block.
4150 * - ctl_softc is always set
4151 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4153 * Returns 0 for success, non-zero (errno) for failure.
4156 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4157 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4159 struct ctl_lun *nlun, *lun;
4160 struct ctl_frontend *fe;
4161 int lun_number, i, lun_malloced;
4167 * We currently only support Direct Access or Processor LUN types.
4169 switch (be_lun->lun_type) {
4177 be_lun->lun_config_status(be_lun->be_lun,
4178 CTL_LUN_CONFIG_FAILURE);
4181 if (ctl_lun == NULL) {
4182 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4189 memset(lun, 0, sizeof(*lun));
4191 lun->flags = CTL_LUN_MALLOCED;
4193 mtx_lock(&ctl_softc->ctl_lock);
4195 * See if the caller requested a particular LUN number. If so, see
4196 * if it is available. Otherwise, allocate the first available LUN.
4198 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4199 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4200 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4201 mtx_unlock(&ctl_softc->ctl_lock);
4202 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4203 printf("ctl: requested LUN ID %d is higher "
4204 "than CTL_MAX_LUNS - 1 (%d)\n",
4205 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4208 * XXX KDM return an error, or just assign
4209 * another LUN ID in this case??
4211 printf("ctl: requested LUN ID %d is already "
4212 "in use\n", be_lun->req_lun_id);
4214 if (lun->flags & CTL_LUN_MALLOCED)
4216 be_lun->lun_config_status(be_lun->be_lun,
4217 CTL_LUN_CONFIG_FAILURE);
4220 lun_number = be_lun->req_lun_id;
4222 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4223 if (lun_number == -1) {
4224 mtx_unlock(&ctl_softc->ctl_lock);
4225 printf("ctl: can't allocate LUN on target %ju, out of "
4226 "LUNs\n", (uintmax_t)target_id.id);
4227 if (lun->flags & CTL_LUN_MALLOCED)
4229 be_lun->lun_config_status(be_lun->be_lun,
4230 CTL_LUN_CONFIG_FAILURE);
4234 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4236 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4237 lun->target = target_id;
4238 lun->lun = lun_number;
4239 lun->be_lun = be_lun;
4241 * The processor LUN is always enabled. Disk LUNs come on line
4242 * disabled, and must be enabled by the backend.
4244 lun->flags |= CTL_LUN_DISABLED;
4245 lun->backend = be_lun->be;
4246 be_lun->ctl_lun = lun;
4247 be_lun->lun_id = lun_number;
4248 atomic_add_int(&be_lun->be->num_luns, 1);
4249 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4250 lun->flags |= CTL_LUN_STOPPED;
4252 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4253 lun->flags |= CTL_LUN_INOPERABLE;
4255 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4256 lun->flags |= CTL_LUN_PRIMARY_SC;
4258 lun->ctl_softc = ctl_softc;
4259 TAILQ_INIT(&lun->ooa_queue);
4260 TAILQ_INIT(&lun->blocked_queue);
4261 STAILQ_INIT(&lun->error_list);
4264 * Initialize the mode page index.
4266 ctl_init_page_index(lun);
4269 * Set the poweron UA for all initiators on this LUN only.
4271 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4272 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4275 * Now, before we insert this lun on the lun list, set the lun
4276 * inventory changed UA for all other luns.
4278 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4279 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4280 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4284 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4286 ctl_softc->ctl_luns[lun_number] = lun;
4288 ctl_softc->num_luns++;
4290 /* Setup statistics gathering */
4291 lun->stats.device_type = be_lun->lun_type;
4292 lun->stats.lun_number = lun_number;
4293 if (lun->stats.device_type == T_DIRECT)
4294 lun->stats.blocksize = be_lun->blocksize;
4296 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4297 for (i = 0;i < CTL_MAX_PORTS;i++)
4298 lun->stats.ports[i].targ_port = i;
4300 mtx_unlock(&ctl_softc->ctl_lock);
4302 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4305 * Run through each registered FETD and bring it online if it isn't
4306 * already. Enable the target ID if it hasn't been enabled, and
4307 * enable this particular LUN.
4309 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4313 * XXX KDM this only works for ONE TARGET ID. We'll need
4314 * to do things differently if we go to a multiple target
4317 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4319 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4321 printf("ctl_alloc_lun: FETD %s port %d "
4322 "returned error %d for targ_enable on "
4323 "target %ju\n", fe->port_name,
4324 fe->targ_port, retval,
4325 (uintmax_t)target_id.id);
4327 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4330 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4332 printf("ctl_alloc_lun: FETD %s port %d returned error "
4333 "%d for lun_enable on target %ju lun %d\n",
4334 fe->port_name, fe->targ_port, retval,
4335 (uintmax_t)target_id.id, lun_number);
4337 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4345 * - LUN has already been marked invalid and any pending I/O has been taken
4349 ctl_free_lun(struct ctl_lun *lun)
4351 struct ctl_softc *softc;
4353 struct ctl_frontend *fe;
4355 struct ctl_lun *nlun;
4358 softc = lun->ctl_softc;
4360 mtx_assert(&softc->ctl_lock, MA_OWNED);
4362 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4364 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4366 softc->ctl_luns[lun->lun] = NULL;
4368 if (!TAILQ_EMPTY(&lun->ooa_queue))
4369 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4374 * XXX KDM this scheme only works for a single target/multiple LUN
4375 * setup. It needs to be revamped for a multiple target scheme.
4377 * XXX KDM this results in fe->lun_disable() getting called twice,
4378 * once when ctl_disable_lun() is called, and a second time here.
4379 * We really need to re-think the LUN disable semantics. There
4380 * should probably be several steps/levels to LUN removal:
4385 * Right now we only have a disable method when communicating to
4386 * the front end ports, at least for individual LUNs.
4389 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4392 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4395 printf("ctl_free_lun: FETD %s port %d returned error "
4396 "%d for lun_disable on target %ju lun %jd\n",
4397 fe->port_name, fe->targ_port, retval,
4398 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4401 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4402 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4404 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4406 printf("ctl_free_lun: FETD %s port %d "
4407 "returned error %d for targ_disable on "
4408 "target %ju\n", fe->port_name,
4409 fe->targ_port, retval,
4410 (uintmax_t)lun->target.id);
4412 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4414 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4418 fe->port_offline(fe->onoff_arg);
4419 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4426 * Tell the backend to free resources, if this LUN has a backend.
4428 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4429 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4431 mtx_destroy(&lun->lun_lock);
4432 if (lun->flags & CTL_LUN_MALLOCED)
4435 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4436 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4437 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4445 ctl_create_lun(struct ctl_be_lun *be_lun)
4447 struct ctl_softc *ctl_softc;
4449 ctl_softc = control_softc;
4452 * ctl_alloc_lun() should handle all potential failure cases.
4454 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4458 ctl_add_lun(struct ctl_be_lun *be_lun)
4460 struct ctl_softc *ctl_softc = control_softc;
4462 mtx_lock(&ctl_softc->ctl_lock);
4463 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4464 mtx_unlock(&ctl_softc->ctl_lock);
4465 wakeup(&ctl_softc->pending_lun_queue);
4471 ctl_enable_lun(struct ctl_be_lun *be_lun)
4473 struct ctl_softc *ctl_softc;
4474 struct ctl_frontend *fe, *nfe;
4475 struct ctl_lun *lun;
4478 ctl_softc = control_softc;
4480 lun = (struct ctl_lun *)be_lun->ctl_lun;
4482 mtx_lock(&ctl_softc->ctl_lock);
4483 mtx_lock(&lun->lun_lock);
4484 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4486 * eh? Why did we get called if the LUN is already
4489 mtx_unlock(&lun->lun_lock);
4490 mtx_unlock(&ctl_softc->ctl_lock);
4493 lun->flags &= ~CTL_LUN_DISABLED;
4494 mtx_unlock(&lun->lun_lock);
4496 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4497 nfe = STAILQ_NEXT(fe, links);
4500 * Drop the lock while we call the FETD's enable routine.
4501 * This can lead to a callback into CTL (at least in the
4502 * case of the internal initiator frontend.
4504 mtx_unlock(&ctl_softc->ctl_lock);
4505 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4506 mtx_lock(&ctl_softc->ctl_lock);
4508 printf("%s: FETD %s port %d returned error "
4509 "%d for lun_enable on target %ju lun %jd\n",
4510 __func__, fe->port_name, fe->targ_port, retval,
4511 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4515 /* NOTE: TODO: why does lun enable affect port status? */
4516 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4521 mtx_unlock(&ctl_softc->ctl_lock);
4527 ctl_disable_lun(struct ctl_be_lun *be_lun)
4529 struct ctl_softc *ctl_softc;
4530 struct ctl_frontend *fe;
4531 struct ctl_lun *lun;
4534 ctl_softc = control_softc;
4536 lun = (struct ctl_lun *)be_lun->ctl_lun;
4538 mtx_lock(&ctl_softc->ctl_lock);
4539 mtx_lock(&lun->lun_lock);
4540 if (lun->flags & CTL_LUN_DISABLED) {
4541 mtx_unlock(&lun->lun_lock);
4542 mtx_unlock(&ctl_softc->ctl_lock);
4545 lun->flags |= CTL_LUN_DISABLED;
4546 mtx_unlock(&lun->lun_lock);
4548 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4549 mtx_unlock(&ctl_softc->ctl_lock);
4551 * Drop the lock before we call the frontend's disable
4552 * routine, to avoid lock order reversals.
4554 * XXX KDM what happens if the frontend list changes while
4555 * we're traversing it? It's unlikely, but should be handled.
4557 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4559 mtx_lock(&ctl_softc->ctl_lock);
4561 printf("ctl_alloc_lun: FETD %s port %d returned error "
4562 "%d for lun_disable on target %ju lun %jd\n",
4563 fe->port_name, fe->targ_port, retval,
4564 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4568 mtx_unlock(&ctl_softc->ctl_lock);
4574 ctl_start_lun(struct ctl_be_lun *be_lun)
4576 struct ctl_softc *ctl_softc;
4577 struct ctl_lun *lun;
4579 ctl_softc = control_softc;
4581 lun = (struct ctl_lun *)be_lun->ctl_lun;
4583 mtx_lock(&lun->lun_lock);
4584 lun->flags &= ~CTL_LUN_STOPPED;
4585 mtx_unlock(&lun->lun_lock);
4591 ctl_stop_lun(struct ctl_be_lun *be_lun)
4593 struct ctl_softc *ctl_softc;
4594 struct ctl_lun *lun;
4596 ctl_softc = control_softc;
4598 lun = (struct ctl_lun *)be_lun->ctl_lun;
4600 mtx_lock(&lun->lun_lock);
4601 lun->flags |= CTL_LUN_STOPPED;
4602 mtx_unlock(&lun->lun_lock);
4608 ctl_lun_offline(struct ctl_be_lun *be_lun)
4610 struct ctl_softc *ctl_softc;
4611 struct ctl_lun *lun;
4613 ctl_softc = control_softc;
4615 lun = (struct ctl_lun *)be_lun->ctl_lun;
4617 mtx_lock(&lun->lun_lock);
4618 lun->flags |= CTL_LUN_OFFLINE;
4619 mtx_unlock(&lun->lun_lock);
4625 ctl_lun_online(struct ctl_be_lun *be_lun)
4627 struct ctl_softc *ctl_softc;
4628 struct ctl_lun *lun;
4630 ctl_softc = control_softc;
4632 lun = (struct ctl_lun *)be_lun->ctl_lun;
4634 mtx_lock(&lun->lun_lock);
4635 lun->flags &= ~CTL_LUN_OFFLINE;
4636 mtx_unlock(&lun->lun_lock);
4642 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4644 struct ctl_softc *ctl_softc;
4645 struct ctl_lun *lun;
4647 ctl_softc = control_softc;
4649 lun = (struct ctl_lun *)be_lun->ctl_lun;
4651 mtx_lock(&lun->lun_lock);
4654 * The LUN needs to be disabled before it can be marked invalid.
4656 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4657 mtx_unlock(&lun->lun_lock);
4661 * Mark the LUN invalid.
4663 lun->flags |= CTL_LUN_INVALID;
4666 * If there is nothing in the OOA queue, go ahead and free the LUN.
4667 * If we have something in the OOA queue, we'll free it when the
4668 * last I/O completes.
4670 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4671 mtx_unlock(&lun->lun_lock);
4672 mtx_lock(&ctl_softc->ctl_lock);
4674 mtx_unlock(&ctl_softc->ctl_lock);
4676 mtx_unlock(&lun->lun_lock);
4682 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4684 struct ctl_softc *ctl_softc;
4685 struct ctl_lun *lun;
4687 ctl_softc = control_softc;
4688 lun = (struct ctl_lun *)be_lun->ctl_lun;
4690 mtx_lock(&lun->lun_lock);
4691 lun->flags |= CTL_LUN_INOPERABLE;
4692 mtx_unlock(&lun->lun_lock);
4698 ctl_lun_operable(struct ctl_be_lun *be_lun)
4700 struct ctl_softc *ctl_softc;
4701 struct ctl_lun *lun;
4703 ctl_softc = control_softc;
4704 lun = (struct ctl_lun *)be_lun->ctl_lun;
4706 mtx_lock(&lun->lun_lock);
4707 lun->flags &= ~CTL_LUN_INOPERABLE;
4708 mtx_unlock(&lun->lun_lock);
4714 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4717 struct ctl_softc *softc;
4718 struct ctl_lun *lun;
4719 struct copan_aps_subpage *current_sp;
4720 struct ctl_page_index *page_index;
4723 softc = control_softc;
4725 mtx_lock(&softc->ctl_lock);
4727 lun = (struct ctl_lun *)be_lun->ctl_lun;
4728 mtx_lock(&lun->lun_lock);
4731 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4732 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4736 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4738 page_index = &lun->mode_pages.index[i];
4741 if (page_index == NULL) {
4742 mtx_unlock(&lun->lun_lock);
4743 mtx_unlock(&softc->ctl_lock);
4744 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4745 (uintmax_t)lun->lun);
4749 if ((softc->aps_locked_lun != 0)
4750 && (softc->aps_locked_lun != lun->lun)) {
4751 printf("%s: attempt to lock LUN %llu when %llu is already "
4753 mtx_unlock(&lun->lun_lock);
4754 mtx_unlock(&softc->ctl_lock);
4759 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4760 (page_index->page_len * CTL_PAGE_CURRENT));
4763 current_sp->lock_active = APS_LOCK_ACTIVE;
4764 softc->aps_locked_lun = lun->lun;
4766 current_sp->lock_active = 0;
4767 softc->aps_locked_lun = 0;
4772 * If we're in HA mode, try to send the lock message to the other
4775 if (ctl_is_single == 0) {
4777 union ctl_ha_msg lock_msg;
4779 lock_msg.hdr.nexus = *nexus;
4780 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4782 lock_msg.aps.lock_flag = 1;
4784 lock_msg.aps.lock_flag = 0;
4785 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4786 sizeof(lock_msg), 0);
4787 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4788 printf("%s: APS (lock=%d) error returned from "
4789 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4790 mtx_unlock(&lun->lun_lock);
4791 mtx_unlock(&softc->ctl_lock);
4796 mtx_unlock(&lun->lun_lock);
4797 mtx_unlock(&softc->ctl_lock);
4803 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4805 struct ctl_lun *lun;
4806 struct ctl_softc *softc;
4809 softc = control_softc;
4811 lun = (struct ctl_lun *)be_lun->ctl_lun;
4813 mtx_lock(&lun->lun_lock);
4815 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4816 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4818 mtx_unlock(&lun->lun_lock);
4822 * Backend "memory move is complete" callback for requests that never
4823 * make it down to say RAIDCore's configuration code.
4826 ctl_config_move_done(union ctl_io *io)
4830 retval = CTL_RETVAL_COMPLETE;
4833 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4835 * XXX KDM this shouldn't happen, but what if it does?
4837 if (io->io_hdr.io_type != CTL_IO_SCSI)
4838 panic("I/O type isn't CTL_IO_SCSI!");
4840 if ((io->io_hdr.port_status == 0)
4841 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4842 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4843 io->io_hdr.status = CTL_SUCCESS;
4844 else if ((io->io_hdr.port_status != 0)
4845 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4846 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4848 * For hardware error sense keys, the sense key
4849 * specific value is defined to be a retry count,
4850 * but we use it to pass back an internal FETD
4851 * error code. XXX KDM Hopefully the FETD is only
4852 * using 16 bits for an error code, since that's
4853 * all the space we have in the sks field.
4855 ctl_set_internal_failure(&io->scsiio,
4858 io->io_hdr.port_status);
4859 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4860 free(io->scsiio.kern_data_ptr, M_CTL);
4865 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4866 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4867 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4869 * XXX KDM just assuming a single pointer here, and not a
4870 * S/G list. If we start using S/G lists for config data,
4871 * we'll need to know how to clean them up here as well.
4873 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4874 free(io->scsiio.kern_data_ptr, M_CTL);
4875 /* Hopefully the user has already set the status... */
4879 * XXX KDM now we need to continue data movement. Some
4881 * - call ctl_scsiio() again? We don't do this for data
4882 * writes, because for those at least we know ahead of
4883 * time where the write will go and how long it is. For
4884 * config writes, though, that information is largely
4885 * contained within the write itself, thus we need to
4886 * parse out the data again.
4888 * - Call some other function once the data is in?
4892 * XXX KDM call ctl_scsiio() again for now, and check flag
4893 * bits to see whether we're allocated or not.
4895 retval = ctl_scsiio(&io->scsiio);
4902 * This gets called by a backend driver when it is done with a
4903 * data_submit method.
4906 ctl_data_submit_done(union ctl_io *io)
4909 * If the IO_CONT flag is set, we need to call the supplied
4910 * function to continue processing the I/O, instead of completing
4913 * If there is an error, though, we don't want to keep processing.
4914 * Instead, just send status back to the initiator.
4916 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4917 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4918 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4919 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4920 io->scsiio.io_cont(io);
4927 * This gets called by a backend driver when it is done with a
4928 * configuration write.
4931 ctl_config_write_done(union ctl_io *io)
4934 * If the IO_CONT flag is set, we need to call the supplied
4935 * function to continue processing the I/O, instead of completing
4938 * If there is an error, though, we don't want to keep processing.
4939 * Instead, just send status back to the initiator.
4941 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
4942 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
4943 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
4944 io->scsiio.io_cont(io);
4948 * Since a configuration write can be done for commands that actually
4949 * have data allocated, like write buffer, and commands that have
4950 * no data, like start/stop unit, we need to check here.
4952 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4953 free(io->scsiio.kern_data_ptr, M_CTL);
4958 * SCSI release command.
4961 ctl_scsi_release(struct ctl_scsiio *ctsio)
4963 int length, longid, thirdparty_id, resv_id;
4964 struct ctl_softc *ctl_softc;
4965 struct ctl_lun *lun;
4970 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4972 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4973 ctl_softc = control_softc;
4975 switch (ctsio->cdb[0]) {
4977 struct scsi_release_10 *cdb;
4979 cdb = (struct scsi_release_10 *)ctsio->cdb;
4981 if (cdb->byte2 & SR10_LONGID)
4984 thirdparty_id = cdb->thirdparty_id;
4986 resv_id = cdb->resv_id;
4987 length = scsi_2btoul(cdb->length);
4994 * XXX KDM right now, we only support LUN reservation. We don't
4995 * support 3rd party reservations, or extent reservations, which
4996 * might actually need the parameter list. If we've gotten this
4997 * far, we've got a LUN reservation. Anything else got kicked out
4998 * above. So, according to SPC, ignore the length.
5002 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5004 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5005 ctsio->kern_data_len = length;
5006 ctsio->kern_total_len = length;
5007 ctsio->kern_data_resid = 0;
5008 ctsio->kern_rel_offset = 0;
5009 ctsio->kern_sg_entries = 0;
5010 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5011 ctsio->be_move_done = ctl_config_move_done;
5012 ctl_datamove((union ctl_io *)ctsio);
5014 return (CTL_RETVAL_COMPLETE);
5018 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5020 mtx_lock(&lun->lun_lock);
5023 * According to SPC, it is not an error for an intiator to attempt
5024 * to release a reservation on a LUN that isn't reserved, or that
5025 * is reserved by another initiator. The reservation can only be
5026 * released, though, by the initiator who made it or by one of
5027 * several reset type events.
5029 if (lun->flags & CTL_LUN_RESERVED) {
5030 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5031 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5032 && (ctsio->io_hdr.nexus.targ_target.id ==
5033 lun->rsv_nexus.targ_target.id)) {
5034 lun->flags &= ~CTL_LUN_RESERVED;
5038 mtx_unlock(&lun->lun_lock);
5040 ctsio->scsi_status = SCSI_STATUS_OK;
5041 ctsio->io_hdr.status = CTL_SUCCESS;
5043 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5044 free(ctsio->kern_data_ptr, M_CTL);
5045 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5048 ctl_done((union ctl_io *)ctsio);
5049 return (CTL_RETVAL_COMPLETE);
5053 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5055 int extent, thirdparty, longid;
5056 int resv_id, length;
5057 uint64_t thirdparty_id;
5058 struct ctl_softc *ctl_softc;
5059 struct ctl_lun *lun;
5068 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5071 ctl_softc = control_softc;
5073 switch (ctsio->cdb[0]) {
5075 struct scsi_reserve_10 *cdb;
5077 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5079 if (cdb->byte2 & SR10_LONGID)
5082 thirdparty_id = cdb->thirdparty_id;
5084 resv_id = cdb->resv_id;
5085 length = scsi_2btoul(cdb->length);
5091 * XXX KDM right now, we only support LUN reservation. We don't
5092 * support 3rd party reservations, or extent reservations, which
5093 * might actually need the parameter list. If we've gotten this
5094 * far, we've got a LUN reservation. Anything else got kicked out
5095 * above. So, according to SPC, ignore the length.
5099 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5101 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5102 ctsio->kern_data_len = length;
5103 ctsio->kern_total_len = length;
5104 ctsio->kern_data_resid = 0;
5105 ctsio->kern_rel_offset = 0;
5106 ctsio->kern_sg_entries = 0;
5107 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5108 ctsio->be_move_done = ctl_config_move_done;
5109 ctl_datamove((union ctl_io *)ctsio);
5111 return (CTL_RETVAL_COMPLETE);
5115 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5117 mtx_lock(&lun->lun_lock);
5118 if (lun->flags & CTL_LUN_RESERVED) {
5119 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5120 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5121 || (ctsio->io_hdr.nexus.targ_target.id !=
5122 lun->rsv_nexus.targ_target.id)) {
5123 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5124 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5129 lun->flags |= CTL_LUN_RESERVED;
5130 lun->rsv_nexus = ctsio->io_hdr.nexus;
5132 ctsio->scsi_status = SCSI_STATUS_OK;
5133 ctsio->io_hdr.status = CTL_SUCCESS;
5136 mtx_unlock(&lun->lun_lock);
5138 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5139 free(ctsio->kern_data_ptr, M_CTL);
5140 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5143 ctl_done((union ctl_io *)ctsio);
5144 return (CTL_RETVAL_COMPLETE);
5148 ctl_start_stop(struct ctl_scsiio *ctsio)
5150 struct scsi_start_stop_unit *cdb;
5151 struct ctl_lun *lun;
5152 struct ctl_softc *ctl_softc;
5155 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5157 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5158 ctl_softc = control_softc;
5161 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5165 * We don't support the immediate bit on a stop unit. In order to
5166 * do that, we would need to code up a way to know that a stop is
5167 * pending, and hold off any new commands until it completes, one
5168 * way or another. Then we could accept or reject those commands
5169 * depending on its status. We would almost need to do the reverse
5170 * of what we do below for an immediate start -- return the copy of
5171 * the ctl_io to the FETD with status to send to the host (and to
5172 * free the copy!) and then free the original I/O once the stop
5173 * actually completes. That way, the OOA queue mechanism can work
5174 * to block commands that shouldn't proceed. Another alternative
5175 * would be to put the copy in the queue in place of the original,
5176 * and return the original back to the caller. That could be
5179 if ((cdb->byte2 & SSS_IMMED)
5180 && ((cdb->how & SSS_START) == 0)) {
5181 ctl_set_invalid_field(ctsio,
5187 ctl_done((union ctl_io *)ctsio);
5188 return (CTL_RETVAL_COMPLETE);
5191 if ((lun->flags & CTL_LUN_PR_RESERVED)
5192 && ((cdb->how & SSS_START)==0)) {
5195 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5196 if (!lun->per_res[residx].registered
5197 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5199 ctl_set_reservation_conflict(ctsio);
5200 ctl_done((union ctl_io *)ctsio);
5201 return (CTL_RETVAL_COMPLETE);
5206 * If there is no backend on this device, we can't start or stop
5207 * it. In theory we shouldn't get any start/stop commands in the
5208 * first place at this level if the LUN doesn't have a backend.
5209 * That should get stopped by the command decode code.
5211 if (lun->backend == NULL) {
5212 ctl_set_invalid_opcode(ctsio);
5213 ctl_done((union ctl_io *)ctsio);
5214 return (CTL_RETVAL_COMPLETE);
5218 * XXX KDM Copan-specific offline behavior.
5219 * Figure out a reasonable way to port this?
5222 mtx_lock(&lun->lun_lock);
5224 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5225 && (lun->flags & CTL_LUN_OFFLINE)) {
5227 * If the LUN is offline, and the on/offline bit isn't set,
5228 * reject the start or stop. Otherwise, let it through.
5230 mtx_unlock(&lun->lun_lock);
5231 ctl_set_lun_not_ready(ctsio);
5232 ctl_done((union ctl_io *)ctsio);
5234 mtx_unlock(&lun->lun_lock);
5235 #endif /* NEEDTOPORT */
5237 * This could be a start or a stop when we're online,
5238 * or a stop/offline or start/online. A start or stop when
5239 * we're offline is covered in the case above.
5242 * In the non-immediate case, we send the request to
5243 * the backend and return status to the user when
5246 * In the immediate case, we allocate a new ctl_io
5247 * to hold a copy of the request, and send that to
5248 * the backend. We then set good status on the
5249 * user's request and return it immediately.
5251 if (cdb->byte2 & SSS_IMMED) {
5252 union ctl_io *new_io;
5254 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5255 if (new_io == NULL) {
5256 ctl_set_busy(ctsio);
5257 ctl_done((union ctl_io *)ctsio);
5259 ctl_copy_io((union ctl_io *)ctsio,
5261 retval = lun->backend->config_write(new_io);
5262 ctl_set_success(ctsio);
5263 ctl_done((union ctl_io *)ctsio);
5266 retval = lun->backend->config_write(
5267 (union ctl_io *)ctsio);
5276 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5277 * we don't really do anything with the LBA and length fields if the user
5278 * passes them in. Instead we'll just flush out the cache for the entire
5282 ctl_sync_cache(struct ctl_scsiio *ctsio)
5284 struct ctl_lun *lun;
5285 struct ctl_softc *ctl_softc;
5286 uint64_t starting_lba;
5287 uint32_t block_count;
5290 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5292 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5293 ctl_softc = control_softc;
5296 switch (ctsio->cdb[0]) {
5297 case SYNCHRONIZE_CACHE: {
5298 struct scsi_sync_cache *cdb;
5299 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5301 starting_lba = scsi_4btoul(cdb->begin_lba);
5302 block_count = scsi_2btoul(cdb->lb_count);
5305 case SYNCHRONIZE_CACHE_16: {
5306 struct scsi_sync_cache_16 *cdb;
5307 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5309 starting_lba = scsi_8btou64(cdb->begin_lba);
5310 block_count = scsi_4btoul(cdb->lb_count);
5314 ctl_set_invalid_opcode(ctsio);
5315 ctl_done((union ctl_io *)ctsio);
5317 break; /* NOTREACHED */
5321 * We check the LBA and length, but don't do anything with them.
5322 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5323 * get flushed. This check will just help satisfy anyone who wants
5324 * to see an error for an out of range LBA.
5326 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5327 ctl_set_lba_out_of_range(ctsio);
5328 ctl_done((union ctl_io *)ctsio);
5333 * If this LUN has no backend, we can't flush the cache anyway.
5335 if (lun->backend == NULL) {
5336 ctl_set_invalid_opcode(ctsio);
5337 ctl_done((union ctl_io *)ctsio);
5342 * Check to see whether we're configured to send the SYNCHRONIZE
5343 * CACHE command directly to the back end.
5345 mtx_lock(&lun->lun_lock);
5346 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5347 && (++(lun->sync_count) >= lun->sync_interval)) {
5348 lun->sync_count = 0;
5349 mtx_unlock(&lun->lun_lock);
5350 retval = lun->backend->config_write((union ctl_io *)ctsio);
5352 mtx_unlock(&lun->lun_lock);
5353 ctl_set_success(ctsio);
5354 ctl_done((union ctl_io *)ctsio);
5363 ctl_format(struct ctl_scsiio *ctsio)
5365 struct scsi_format *cdb;
5366 struct ctl_lun *lun;
5367 struct ctl_softc *ctl_softc;
5368 int length, defect_list_len;
5370 CTL_DEBUG_PRINT(("ctl_format\n"));
5372 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5373 ctl_softc = control_softc;
5375 cdb = (struct scsi_format *)ctsio->cdb;
5378 if (cdb->byte2 & SF_FMTDATA) {
5379 if (cdb->byte2 & SF_LONGLIST)
5380 length = sizeof(struct scsi_format_header_long);
5382 length = sizeof(struct scsi_format_header_short);
5385 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5387 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5388 ctsio->kern_data_len = length;
5389 ctsio->kern_total_len = length;
5390 ctsio->kern_data_resid = 0;
5391 ctsio->kern_rel_offset = 0;
5392 ctsio->kern_sg_entries = 0;
5393 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5394 ctsio->be_move_done = ctl_config_move_done;
5395 ctl_datamove((union ctl_io *)ctsio);
5397 return (CTL_RETVAL_COMPLETE);
5400 defect_list_len = 0;
5402 if (cdb->byte2 & SF_FMTDATA) {
5403 if (cdb->byte2 & SF_LONGLIST) {
5404 struct scsi_format_header_long *header;
5406 header = (struct scsi_format_header_long *)
5407 ctsio->kern_data_ptr;
5409 defect_list_len = scsi_4btoul(header->defect_list_len);
5410 if (defect_list_len != 0) {
5411 ctl_set_invalid_field(ctsio,
5420 struct scsi_format_header_short *header;
5422 header = (struct scsi_format_header_short *)
5423 ctsio->kern_data_ptr;
5425 defect_list_len = scsi_2btoul(header->defect_list_len);
5426 if (defect_list_len != 0) {
5427 ctl_set_invalid_field(ctsio,
5439 * The format command will clear out the "Medium format corrupted"
5440 * status if set by the configuration code. That status is really
5441 * just a way to notify the host that we have lost the media, and
5442 * get them to issue a command that will basically make them think
5443 * they're blowing away the media.
5445 mtx_lock(&lun->lun_lock);
5446 lun->flags &= ~CTL_LUN_INOPERABLE;
5447 mtx_unlock(&lun->lun_lock);
5449 ctsio->scsi_status = SCSI_STATUS_OK;
5450 ctsio->io_hdr.status = CTL_SUCCESS;
5453 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5454 free(ctsio->kern_data_ptr, M_CTL);
5455 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5458 ctl_done((union ctl_io *)ctsio);
5459 return (CTL_RETVAL_COMPLETE);
5463 ctl_read_buffer(struct ctl_scsiio *ctsio)
5465 struct scsi_read_buffer *cdb;
5466 struct ctl_lun *lun;
5467 int buffer_offset, len;
5468 static uint8_t descr[4];
5469 static uint8_t echo_descr[4] = { 0 };
5471 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5473 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5474 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5476 if (lun->flags & CTL_LUN_PR_RESERVED) {
5480 * XXX KDM need a lock here.
5482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5483 if ((lun->res_type == SPR_TYPE_EX_AC
5484 && residx != lun->pr_res_idx)
5485 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5486 || lun->res_type == SPR_TYPE_EX_AC_AR)
5487 && !lun->per_res[residx].registered)) {
5488 ctl_set_reservation_conflict(ctsio);
5489 ctl_done((union ctl_io *)ctsio);
5490 return (CTL_RETVAL_COMPLETE);
5494 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5495 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5496 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5497 ctl_set_invalid_field(ctsio,
5503 ctl_done((union ctl_io *)ctsio);
5504 return (CTL_RETVAL_COMPLETE);
5507 len = scsi_3btoul(cdb->length);
5508 buffer_offset = scsi_3btoul(cdb->offset);
5510 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5511 ctl_set_invalid_field(ctsio,
5517 ctl_done((union ctl_io *)ctsio);
5518 return (CTL_RETVAL_COMPLETE);
5521 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5523 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5524 ctsio->kern_data_ptr = descr;
5525 len = min(len, sizeof(descr));
5526 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5527 ctsio->kern_data_ptr = echo_descr;
5528 len = min(len, sizeof(echo_descr));
5530 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5531 ctsio->kern_data_len = len;
5532 ctsio->kern_total_len = len;
5533 ctsio->kern_data_resid = 0;
5534 ctsio->kern_rel_offset = 0;
5535 ctsio->kern_sg_entries = 0;
5536 ctsio->be_move_done = ctl_config_move_done;
5537 ctl_datamove((union ctl_io *)ctsio);
5539 return (CTL_RETVAL_COMPLETE);
5543 ctl_write_buffer(struct ctl_scsiio *ctsio)
5545 struct scsi_write_buffer *cdb;
5546 struct ctl_lun *lun;
5547 int buffer_offset, len;
5549 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5551 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5552 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5554 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5555 ctl_set_invalid_field(ctsio,
5561 ctl_done((union ctl_io *)ctsio);
5562 return (CTL_RETVAL_COMPLETE);
5565 len = scsi_3btoul(cdb->length);
5566 buffer_offset = scsi_3btoul(cdb->offset);
5568 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5569 ctl_set_invalid_field(ctsio,
5575 ctl_done((union ctl_io *)ctsio);
5576 return (CTL_RETVAL_COMPLETE);
5580 * If we've got a kernel request that hasn't been malloced yet,
5581 * malloc it and tell the caller the data buffer is here.
5583 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5584 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5585 ctsio->kern_data_len = len;
5586 ctsio->kern_total_len = len;
5587 ctsio->kern_data_resid = 0;
5588 ctsio->kern_rel_offset = 0;
5589 ctsio->kern_sg_entries = 0;
5590 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5591 ctsio->be_move_done = ctl_config_move_done;
5592 ctl_datamove((union ctl_io *)ctsio);
5594 return (CTL_RETVAL_COMPLETE);
5597 ctl_done((union ctl_io *)ctsio);
5599 return (CTL_RETVAL_COMPLETE);
5603 ctl_write_same(struct ctl_scsiio *ctsio)
5605 struct ctl_lun *lun;
5606 struct ctl_lba_len_flags *lbalen;
5608 uint32_t num_blocks;
5612 retval = CTL_RETVAL_COMPLETE;
5614 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5616 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5618 switch (ctsio->cdb[0]) {
5619 case WRITE_SAME_10: {
5620 struct scsi_write_same_10 *cdb;
5622 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5624 lba = scsi_4btoul(cdb->addr);
5625 num_blocks = scsi_2btoul(cdb->length);
5629 case WRITE_SAME_16: {
5630 struct scsi_write_same_16 *cdb;
5632 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5634 lba = scsi_8btou64(cdb->addr);
5635 num_blocks = scsi_4btoul(cdb->length);
5641 * We got a command we don't support. This shouldn't
5642 * happen, commands should be filtered out above us.
5644 ctl_set_invalid_opcode(ctsio);
5645 ctl_done((union ctl_io *)ctsio);
5647 return (CTL_RETVAL_COMPLETE);
5648 break; /* NOTREACHED */
5652 * The first check is to make sure we're in bounds, the second
5653 * check is to catch wrap-around problems. If the lba + num blocks
5654 * is less than the lba, then we've wrapped around and the block
5655 * range is invalid anyway.
5657 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5658 || ((lba + num_blocks) < lba)) {
5659 ctl_set_lba_out_of_range(ctsio);
5660 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5664 /* Zero number of blocks means "to the last logical block" */
5665 if (num_blocks == 0) {
5666 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5667 ctl_set_invalid_field(ctsio,
5673 ctl_done((union ctl_io *)ctsio);
5674 return (CTL_RETVAL_COMPLETE);
5676 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5679 len = lun->be_lun->blocksize;
5682 * If we've got a kernel request that hasn't been malloced yet,
5683 * malloc it and tell the caller the data buffer is here.
5685 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5686 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5687 ctsio->kern_data_len = len;
5688 ctsio->kern_total_len = len;
5689 ctsio->kern_data_resid = 0;
5690 ctsio->kern_rel_offset = 0;
5691 ctsio->kern_sg_entries = 0;
5692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5693 ctsio->be_move_done = ctl_config_move_done;
5694 ctl_datamove((union ctl_io *)ctsio);
5696 return (CTL_RETVAL_COMPLETE);
5699 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5701 lbalen->len = num_blocks;
5702 lbalen->flags = byte2;
5703 retval = lun->backend->config_write((union ctl_io *)ctsio);
5709 ctl_unmap(struct ctl_scsiio *ctsio)
5711 struct ctl_lun *lun;
5712 struct scsi_unmap *cdb;
5713 struct ctl_ptr_len_flags *ptrlen;
5714 struct scsi_unmap_header *hdr;
5715 struct scsi_unmap_desc *buf, *end;
5717 uint32_t num_blocks;
5721 retval = CTL_RETVAL_COMPLETE;
5723 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5725 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5726 cdb = (struct scsi_unmap *)ctsio->cdb;
5728 len = scsi_2btoul(cdb->length);
5732 * If we've got a kernel request that hasn't been malloced yet,
5733 * malloc it and tell the caller the data buffer is here.
5735 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5736 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5737 ctsio->kern_data_len = len;
5738 ctsio->kern_total_len = len;
5739 ctsio->kern_data_resid = 0;
5740 ctsio->kern_rel_offset = 0;
5741 ctsio->kern_sg_entries = 0;
5742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5743 ctsio->be_move_done = ctl_config_move_done;
5744 ctl_datamove((union ctl_io *)ctsio);
5746 return (CTL_RETVAL_COMPLETE);
5749 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5750 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5751 if (len < sizeof (*hdr) ||
5752 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5753 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5754 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5755 ctl_set_invalid_field(ctsio,
5761 ctl_done((union ctl_io *)ctsio);
5762 return (CTL_RETVAL_COMPLETE);
5764 len = scsi_2btoul(hdr->desc_length);
5765 buf = (struct scsi_unmap_desc *)(hdr + 1);
5766 end = buf + len / sizeof(*buf);
5768 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5769 ptrlen->ptr = (void *)buf;
5771 ptrlen->flags = byte2;
5773 for (; buf < end; buf++) {
5774 lba = scsi_8btou64(buf->lba);
5775 num_blocks = scsi_4btoul(buf->length);
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);
5784 retval = lun->backend->config_write((union ctl_io *)ctsio);
5790 * Note that this function currently doesn't actually do anything inside
5791 * CTL to enforce things if the DQue bit is turned on.
5793 * Also note that this function can't be used in the default case, because
5794 * the DQue bit isn't set in the changeable mask for the control mode page
5795 * anyway. This is just here as an example for how to implement a page
5796 * handler, and a placeholder in case we want to allow the user to turn
5797 * tagged queueing on and off.
5799 * The D_SENSE bit handling is functional, however, and will turn
5800 * descriptor sense on and off for a given LUN.
5803 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5804 struct ctl_page_index *page_index, uint8_t *page_ptr)
5806 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5807 struct ctl_lun *lun;
5808 struct ctl_softc *softc;
5812 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5813 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5816 user_cp = (struct scsi_control_page *)page_ptr;
5817 current_cp = (struct scsi_control_page *)
5818 (page_index->page_data + (page_index->page_len *
5820 saved_cp = (struct scsi_control_page *)
5821 (page_index->page_data + (page_index->page_len *
5824 softc = control_softc;
5826 mtx_lock(&lun->lun_lock);
5827 if (((current_cp->rlec & SCP_DSENSE) == 0)
5828 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5830 * Descriptor sense is currently turned off and the user
5831 * wants to turn it on.
5833 current_cp->rlec |= SCP_DSENSE;
5834 saved_cp->rlec |= SCP_DSENSE;
5835 lun->flags |= CTL_LUN_SENSE_DESC;
5837 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5838 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5840 * Descriptor sense is currently turned on, and the user
5841 * wants to turn it off.
5843 current_cp->rlec &= ~SCP_DSENSE;
5844 saved_cp->rlec &= ~SCP_DSENSE;
5845 lun->flags &= ~CTL_LUN_SENSE_DESC;
5848 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5849 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5851 csevent_log(CSC_CTL | CSC_SHELF_SW |
5853 csevent_LogType_Trace,
5854 csevent_Severity_Information,
5855 csevent_AlertLevel_Green,
5856 csevent_FRU_Firmware,
5857 csevent_FRU_Unknown,
5858 "Received untagged to untagged transition");
5859 #endif /* NEEDTOPORT */
5862 csevent_log(CSC_CTL | CSC_SHELF_SW |
5864 csevent_LogType_ConfigChange,
5865 csevent_Severity_Information,
5866 csevent_AlertLevel_Green,
5867 csevent_FRU_Firmware,
5868 csevent_FRU_Unknown,
5869 "Received untagged to tagged "
5870 "queueing transition");
5871 #endif /* NEEDTOPORT */
5873 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5874 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5878 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5880 csevent_log(CSC_CTL | CSC_SHELF_SW |
5882 csevent_LogType_ConfigChange,
5883 csevent_Severity_Warning,
5884 csevent_AlertLevel_Yellow,
5885 csevent_FRU_Firmware,
5886 csevent_FRU_Unknown,
5887 "Received tagged queueing to untagged "
5889 #endif /* NEEDTOPORT */
5891 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5892 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5896 csevent_log(CSC_CTL | CSC_SHELF_SW |
5898 csevent_LogType_Trace,
5899 csevent_Severity_Information,
5900 csevent_AlertLevel_Green,
5901 csevent_FRU_Firmware,
5902 csevent_FRU_Unknown,
5903 "Received tagged queueing to tagged "
5904 "queueing transition");
5905 #endif /* NEEDTOPORT */
5911 * Let other initiators know that the mode
5912 * parameters for this LUN have changed.
5914 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5918 lun->pending_sense[i].ua_pending |=
5922 mtx_unlock(&lun->lun_lock);
5928 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5929 struct ctl_page_index *page_index, uint8_t *page_ptr)
5935 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5936 struct ctl_page_index *page_index, int pc)
5938 struct copan_power_subpage *page;
5940 page = (struct copan_power_subpage *)page_index->page_data +
5941 (page_index->page_len * pc);
5944 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5946 * We don't update the changable bits for this page.
5949 case SMS_PAGE_CTRL_CURRENT >> 6:
5950 case SMS_PAGE_CTRL_DEFAULT >> 6:
5951 case SMS_PAGE_CTRL_SAVED >> 6:
5953 ctl_update_power_subpage(page);
5958 EPRINT(0, "Invalid PC %d!!", pc);
5967 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5968 struct ctl_page_index *page_index, uint8_t *page_ptr)
5970 struct copan_aps_subpage *user_sp;
5971 struct copan_aps_subpage *current_sp;
5972 union ctl_modepage_info *modepage_info;
5973 struct ctl_softc *softc;
5974 struct ctl_lun *lun;
5977 retval = CTL_RETVAL_COMPLETE;
5978 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5979 (page_index->page_len * CTL_PAGE_CURRENT));
5980 softc = control_softc;
5981 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5983 user_sp = (struct copan_aps_subpage *)page_ptr;
5985 modepage_info = (union ctl_modepage_info *)
5986 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5988 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5989 modepage_info->header.subpage = page_index->subpage;
5990 modepage_info->aps.lock_active = user_sp->lock_active;
5992 mtx_lock(&softc->ctl_lock);
5995 * If there is a request to lock the LUN and another LUN is locked
5996 * this is an error. If the requested LUN is already locked ignore
5997 * the request. If no LUN is locked attempt to lock it.
5998 * if there is a request to unlock the LUN and the LUN is currently
5999 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6000 * if another LUN is locked or no LUN is locked.
6002 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6003 if (softc->aps_locked_lun == lun->lun) {
6005 * This LUN is already locked, so we're done.
6007 retval = CTL_RETVAL_COMPLETE;
6008 } else if (softc->aps_locked_lun == 0) {
6010 * No one has the lock, pass the request to the
6013 retval = lun->backend->config_write(
6014 (union ctl_io *)ctsio);
6017 * Someone else has the lock, throw out the request.
6019 ctl_set_already_locked(ctsio);
6020 free(ctsio->kern_data_ptr, M_CTL);
6021 ctl_done((union ctl_io *)ctsio);
6024 * Set the return value so that ctl_do_mode_select()
6025 * won't try to complete the command. We already
6026 * completed it here.
6028 retval = CTL_RETVAL_ERROR;
6030 } else if (softc->aps_locked_lun == lun->lun) {
6032 * This LUN is locked, so pass the unlock request to the
6035 retval = lun->backend->config_write((union ctl_io *)ctsio);
6037 mtx_unlock(&softc->ctl_lock);
6043 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6044 struct ctl_page_index *page_index,
6050 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6055 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6056 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6057 printf("page data:");
6059 printf(" %.2x",page_ptr[i]);
6065 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6066 struct ctl_page_index *page_index,
6069 struct copan_debugconf_subpage *page;
6071 page = (struct copan_debugconf_subpage *)page_index->page_data +
6072 (page_index->page_len * pc);
6075 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6076 case SMS_PAGE_CTRL_DEFAULT >> 6:
6077 case SMS_PAGE_CTRL_SAVED >> 6:
6079 * We don't update the changable or default bits for this page.
6082 case SMS_PAGE_CTRL_CURRENT >> 6:
6083 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6084 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6088 EPRINT(0, "Invalid PC %d!!", pc);
6089 #endif /* NEEDTOPORT */
6097 ctl_do_mode_select(union ctl_io *io)
6099 struct scsi_mode_page_header *page_header;
6100 struct ctl_page_index *page_index;
6101 struct ctl_scsiio *ctsio;
6102 int control_dev, page_len;
6103 int page_len_offset, page_len_size;
6104 union ctl_modepage_info *modepage_info;
6105 struct ctl_lun *lun;
6106 int *len_left, *len_used;
6109 ctsio = &io->scsiio;
6112 retval = CTL_RETVAL_COMPLETE;
6114 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6116 if (lun->be_lun->lun_type != T_DIRECT)
6121 modepage_info = (union ctl_modepage_info *)
6122 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6123 len_left = &modepage_info->header.len_left;
6124 len_used = &modepage_info->header.len_used;
6128 page_header = (struct scsi_mode_page_header *)
6129 (ctsio->kern_data_ptr + *len_used);
6131 if (*len_left == 0) {
6132 free(ctsio->kern_data_ptr, M_CTL);
6133 ctl_set_success(ctsio);
6134 ctl_done((union ctl_io *)ctsio);
6135 return (CTL_RETVAL_COMPLETE);
6136 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6138 free(ctsio->kern_data_ptr, M_CTL);
6139 ctl_set_param_len_error(ctsio);
6140 ctl_done((union ctl_io *)ctsio);
6141 return (CTL_RETVAL_COMPLETE);
6143 } else if ((page_header->page_code & SMPH_SPF)
6144 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6146 free(ctsio->kern_data_ptr, M_CTL);
6147 ctl_set_param_len_error(ctsio);
6148 ctl_done((union ctl_io *)ctsio);
6149 return (CTL_RETVAL_COMPLETE);
6154 * XXX KDM should we do something with the block descriptor?
6156 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6158 if ((control_dev != 0)
6159 && (lun->mode_pages.index[i].page_flags &
6160 CTL_PAGE_FLAG_DISK_ONLY))
6163 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6164 (page_header->page_code & SMPH_PC_MASK))
6168 * If neither page has a subpage code, then we've got a
6171 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6172 && ((page_header->page_code & SMPH_SPF) == 0)) {
6173 page_index = &lun->mode_pages.index[i];
6174 page_len = page_header->page_length;
6179 * If both pages have subpages, then the subpage numbers
6182 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6183 && (page_header->page_code & SMPH_SPF)) {
6184 struct scsi_mode_page_header_sp *sph;
6186 sph = (struct scsi_mode_page_header_sp *)page_header;
6188 if (lun->mode_pages.index[i].subpage ==
6190 page_index = &lun->mode_pages.index[i];
6191 page_len = scsi_2btoul(sph->page_length);
6198 * If we couldn't find the page, or if we don't have a mode select
6199 * handler for it, send back an error to the user.
6201 if ((page_index == NULL)
6202 || (page_index->select_handler == NULL)) {
6203 ctl_set_invalid_field(ctsio,
6206 /*field*/ *len_used,
6209 free(ctsio->kern_data_ptr, M_CTL);
6210 ctl_done((union ctl_io *)ctsio);
6211 return (CTL_RETVAL_COMPLETE);
6214 if (page_index->page_code & SMPH_SPF) {
6215 page_len_offset = 2;
6219 page_len_offset = 1;
6223 * If the length the initiator gives us isn't the one we specify in
6224 * the mode page header, or if they didn't specify enough data in
6225 * the CDB to avoid truncating this page, kick out the request.
6227 if ((page_len != (page_index->page_len - page_len_offset -
6229 || (*len_left < page_index->page_len)) {
6232 ctl_set_invalid_field(ctsio,
6235 /*field*/ *len_used + page_len_offset,
6238 free(ctsio->kern_data_ptr, M_CTL);
6239 ctl_done((union ctl_io *)ctsio);
6240 return (CTL_RETVAL_COMPLETE);
6244 * Run through the mode page, checking to make sure that the bits
6245 * the user changed are actually legal for him to change.
6247 for (i = 0; i < page_index->page_len; i++) {
6248 uint8_t *user_byte, *change_mask, *current_byte;
6252 user_byte = (uint8_t *)page_header + i;
6253 change_mask = page_index->page_data +
6254 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6255 current_byte = page_index->page_data +
6256 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6259 * Check to see whether the user set any bits in this byte
6260 * that he is not allowed to set.
6262 if ((*user_byte & ~(*change_mask)) ==
6263 (*current_byte & ~(*change_mask)))
6267 * Go through bit by bit to determine which one is illegal.
6270 for (j = 7; j >= 0; j--) {
6271 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6272 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6277 ctl_set_invalid_field(ctsio,
6280 /*field*/ *len_used + i,
6283 free(ctsio->kern_data_ptr, M_CTL);
6284 ctl_done((union ctl_io *)ctsio);
6285 return (CTL_RETVAL_COMPLETE);
6289 * Decrement these before we call the page handler, since we may
6290 * end up getting called back one way or another before the handler
6291 * returns to this context.
6293 *len_left -= page_index->page_len;
6294 *len_used += page_index->page_len;
6296 retval = page_index->select_handler(ctsio, page_index,
6297 (uint8_t *)page_header);
6300 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6301 * wait until this queued command completes to finish processing
6302 * the mode page. If it returns anything other than
6303 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6304 * already set the sense information, freed the data pointer, and
6305 * completed the io for us.
6307 if (retval != CTL_RETVAL_COMPLETE)
6308 goto bailout_no_done;
6311 * If the initiator sent us more than one page, parse the next one.
6316 ctl_set_success(ctsio);
6317 free(ctsio->kern_data_ptr, M_CTL);
6318 ctl_done((union ctl_io *)ctsio);
6322 return (CTL_RETVAL_COMPLETE);
6327 ctl_mode_select(struct ctl_scsiio *ctsio)
6329 int param_len, pf, sp;
6330 int header_size, bd_len;
6331 int len_left, len_used;
6332 struct ctl_page_index *page_index;
6333 struct ctl_lun *lun;
6334 int control_dev, page_len;
6335 union ctl_modepage_info *modepage_info;
6347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6349 if (lun->be_lun->lun_type != T_DIRECT)
6354 switch (ctsio->cdb[0]) {
6355 case MODE_SELECT_6: {
6356 struct scsi_mode_select_6 *cdb;
6358 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6360 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6361 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6363 param_len = cdb->length;
6364 header_size = sizeof(struct scsi_mode_header_6);
6367 case MODE_SELECT_10: {
6368 struct scsi_mode_select_10 *cdb;
6370 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6372 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6373 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6375 param_len = scsi_2btoul(cdb->length);
6376 header_size = sizeof(struct scsi_mode_header_10);
6380 ctl_set_invalid_opcode(ctsio);
6381 ctl_done((union ctl_io *)ctsio);
6382 return (CTL_RETVAL_COMPLETE);
6383 break; /* NOTREACHED */
6388 * "A parameter list length of zero indicates that the Data-Out Buffer
6389 * shall be empty. This condition shall not be considered as an error."
6391 if (param_len == 0) {
6392 ctl_set_success(ctsio);
6393 ctl_done((union ctl_io *)ctsio);
6394 return (CTL_RETVAL_COMPLETE);
6398 * Since we'll hit this the first time through, prior to
6399 * allocation, we don't need to free a data buffer here.
6401 if (param_len < header_size) {
6402 ctl_set_param_len_error(ctsio);
6403 ctl_done((union ctl_io *)ctsio);
6404 return (CTL_RETVAL_COMPLETE);
6408 * Allocate the data buffer and grab the user's data. In theory,
6409 * we shouldn't have to sanity check the parameter list length here
6410 * because the maximum size is 64K. We should be able to malloc
6411 * that much without too many problems.
6413 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6414 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6415 ctsio->kern_data_len = param_len;
6416 ctsio->kern_total_len = param_len;
6417 ctsio->kern_data_resid = 0;
6418 ctsio->kern_rel_offset = 0;
6419 ctsio->kern_sg_entries = 0;
6420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6421 ctsio->be_move_done = ctl_config_move_done;
6422 ctl_datamove((union ctl_io *)ctsio);
6424 return (CTL_RETVAL_COMPLETE);
6427 switch (ctsio->cdb[0]) {
6428 case MODE_SELECT_6: {
6429 struct scsi_mode_header_6 *mh6;
6431 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6432 bd_len = mh6->blk_desc_len;
6435 case MODE_SELECT_10: {
6436 struct scsi_mode_header_10 *mh10;
6438 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6439 bd_len = scsi_2btoul(mh10->blk_desc_len);
6443 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6447 if (param_len < (header_size + bd_len)) {
6448 free(ctsio->kern_data_ptr, M_CTL);
6449 ctl_set_param_len_error(ctsio);
6450 ctl_done((union ctl_io *)ctsio);
6451 return (CTL_RETVAL_COMPLETE);
6455 * Set the IO_CONT flag, so that if this I/O gets passed to
6456 * ctl_config_write_done(), it'll get passed back to
6457 * ctl_do_mode_select() for further processing, or completion if
6460 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6461 ctsio->io_cont = ctl_do_mode_select;
6463 modepage_info = (union ctl_modepage_info *)
6464 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6466 memset(modepage_info, 0, sizeof(*modepage_info));
6468 len_left = param_len - header_size - bd_len;
6469 len_used = header_size + bd_len;
6471 modepage_info->header.len_left = len_left;
6472 modepage_info->header.len_used = len_used;
6474 return (ctl_do_mode_select((union ctl_io *)ctsio));
6478 ctl_mode_sense(struct ctl_scsiio *ctsio)
6480 struct ctl_lun *lun;
6481 int pc, page_code, dbd, llba, subpage;
6482 int alloc_len, page_len, header_len, total_len;
6483 struct scsi_mode_block_descr *block_desc;
6484 struct ctl_page_index *page_index;
6492 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6494 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6496 if (lun->be_lun->lun_type != T_DIRECT)
6501 if (lun->flags & CTL_LUN_PR_RESERVED) {
6505 * XXX KDM need a lock here.
6507 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6508 if ((lun->res_type == SPR_TYPE_EX_AC
6509 && residx != lun->pr_res_idx)
6510 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6511 || lun->res_type == SPR_TYPE_EX_AC_AR)
6512 && !lun->per_res[residx].registered)) {
6513 ctl_set_reservation_conflict(ctsio);
6514 ctl_done((union ctl_io *)ctsio);
6515 return (CTL_RETVAL_COMPLETE);
6519 switch (ctsio->cdb[0]) {
6520 case MODE_SENSE_6: {
6521 struct scsi_mode_sense_6 *cdb;
6523 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6525 header_len = sizeof(struct scsi_mode_hdr_6);
6526 if (cdb->byte2 & SMS_DBD)
6529 header_len += sizeof(struct scsi_mode_block_descr);
6531 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6532 page_code = cdb->page & SMS_PAGE_CODE;
6533 subpage = cdb->subpage;
6534 alloc_len = cdb->length;
6537 case MODE_SENSE_10: {
6538 struct scsi_mode_sense_10 *cdb;
6540 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6542 header_len = sizeof(struct scsi_mode_hdr_10);
6544 if (cdb->byte2 & SMS_DBD)
6547 header_len += sizeof(struct scsi_mode_block_descr);
6548 if (cdb->byte2 & SMS10_LLBAA)
6550 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6551 page_code = cdb->page & SMS_PAGE_CODE;
6552 subpage = cdb->subpage;
6553 alloc_len = scsi_2btoul(cdb->length);
6557 ctl_set_invalid_opcode(ctsio);
6558 ctl_done((union ctl_io *)ctsio);
6559 return (CTL_RETVAL_COMPLETE);
6560 break; /* NOTREACHED */
6564 * We have to make a first pass through to calculate the size of
6565 * the pages that match the user's query. Then we allocate enough
6566 * memory to hold it, and actually copy the data into the buffer.
6568 switch (page_code) {
6569 case SMS_ALL_PAGES_PAGE: {
6575 * At the moment, values other than 0 and 0xff here are
6576 * reserved according to SPC-3.
6578 if ((subpage != SMS_SUBPAGE_PAGE_0)
6579 && (subpage != SMS_SUBPAGE_ALL)) {
6580 ctl_set_invalid_field(ctsio,
6586 ctl_done((union ctl_io *)ctsio);
6587 return (CTL_RETVAL_COMPLETE);
6590 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6591 if ((control_dev != 0)
6592 && (lun->mode_pages.index[i].page_flags &
6593 CTL_PAGE_FLAG_DISK_ONLY))
6597 * We don't use this subpage if the user didn't
6598 * request all subpages.
6600 if ((lun->mode_pages.index[i].subpage != 0)
6601 && (subpage == SMS_SUBPAGE_PAGE_0))
6605 printf("found page %#x len %d\n",
6606 lun->mode_pages.index[i].page_code &
6608 lun->mode_pages.index[i].page_len);
6610 page_len += lun->mode_pages.index[i].page_len;
6619 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6620 /* Look for the right page code */
6621 if ((lun->mode_pages.index[i].page_code &
6622 SMPH_PC_MASK) != page_code)
6625 /* Look for the right subpage or the subpage wildcard*/
6626 if ((lun->mode_pages.index[i].subpage != subpage)
6627 && (subpage != SMS_SUBPAGE_ALL))
6630 /* Make sure the page is supported for this dev type */
6631 if ((control_dev != 0)
6632 && (lun->mode_pages.index[i].page_flags &
6633 CTL_PAGE_FLAG_DISK_ONLY))
6637 printf("found page %#x len %d\n",
6638 lun->mode_pages.index[i].page_code &
6640 lun->mode_pages.index[i].page_len);
6643 page_len += lun->mode_pages.index[i].page_len;
6646 if (page_len == 0) {
6647 ctl_set_invalid_field(ctsio,
6653 ctl_done((union ctl_io *)ctsio);
6654 return (CTL_RETVAL_COMPLETE);
6660 total_len = header_len + page_len;
6662 printf("header_len = %d, page_len = %d, total_len = %d\n",
6663 header_len, page_len, total_len);
6666 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6667 ctsio->kern_sg_entries = 0;
6668 ctsio->kern_data_resid = 0;
6669 ctsio->kern_rel_offset = 0;
6670 if (total_len < alloc_len) {
6671 ctsio->residual = alloc_len - total_len;
6672 ctsio->kern_data_len = total_len;
6673 ctsio->kern_total_len = total_len;
6675 ctsio->residual = 0;
6676 ctsio->kern_data_len = alloc_len;
6677 ctsio->kern_total_len = alloc_len;
6680 switch (ctsio->cdb[0]) {
6681 case MODE_SENSE_6: {
6682 struct scsi_mode_hdr_6 *header;
6684 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6686 header->datalen = ctl_min(total_len - 1, 254);
6689 header->block_descr_len = 0;
6691 header->block_descr_len =
6692 sizeof(struct scsi_mode_block_descr);
6693 block_desc = (struct scsi_mode_block_descr *)&header[1];
6696 case MODE_SENSE_10: {
6697 struct scsi_mode_hdr_10 *header;
6700 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6702 datalen = ctl_min(total_len - 2, 65533);
6703 scsi_ulto2b(datalen, header->datalen);
6705 scsi_ulto2b(0, header->block_descr_len);
6707 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6708 header->block_descr_len);
6709 block_desc = (struct scsi_mode_block_descr *)&header[1];
6713 panic("invalid CDB type %#x", ctsio->cdb[0]);
6714 break; /* NOTREACHED */
6718 * If we've got a disk, use its blocksize in the block
6719 * descriptor. Otherwise, just set it to 0.
6722 if (control_dev != 0)
6723 scsi_ulto3b(lun->be_lun->blocksize,
6724 block_desc->block_len);
6726 scsi_ulto3b(0, block_desc->block_len);
6729 switch (page_code) {
6730 case SMS_ALL_PAGES_PAGE: {
6733 data_used = header_len;
6734 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6735 struct ctl_page_index *page_index;
6737 page_index = &lun->mode_pages.index[i];
6739 if ((control_dev != 0)
6740 && (page_index->page_flags &
6741 CTL_PAGE_FLAG_DISK_ONLY))
6745 * We don't use this subpage if the user didn't
6746 * request all subpages. We already checked (above)
6747 * to make sure the user only specified a subpage
6748 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6750 if ((page_index->subpage != 0)
6751 && (subpage == SMS_SUBPAGE_PAGE_0))
6755 * Call the handler, if it exists, to update the
6756 * page to the latest values.
6758 if (page_index->sense_handler != NULL)
6759 page_index->sense_handler(ctsio, page_index,pc);
6761 memcpy(ctsio->kern_data_ptr + data_used,
6762 page_index->page_data +
6763 (page_index->page_len * pc),
6764 page_index->page_len);
6765 data_used += page_index->page_len;
6772 data_used = header_len;
6774 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6775 struct ctl_page_index *page_index;
6777 page_index = &lun->mode_pages.index[i];
6779 /* Look for the right page code */
6780 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6783 /* Look for the right subpage or the subpage wildcard*/
6784 if ((page_index->subpage != subpage)
6785 && (subpage != SMS_SUBPAGE_ALL))
6788 /* Make sure the page is supported for this dev type */
6789 if ((control_dev != 0)
6790 && (page_index->page_flags &
6791 CTL_PAGE_FLAG_DISK_ONLY))
6795 * Call the handler, if it exists, to update the
6796 * page to the latest values.
6798 if (page_index->sense_handler != NULL)
6799 page_index->sense_handler(ctsio, page_index,pc);
6801 memcpy(ctsio->kern_data_ptr + data_used,
6802 page_index->page_data +
6803 (page_index->page_len * pc),
6804 page_index->page_len);
6805 data_used += page_index->page_len;
6811 ctsio->scsi_status = SCSI_STATUS_OK;
6813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6814 ctsio->be_move_done = ctl_config_move_done;
6815 ctl_datamove((union ctl_io *)ctsio);
6817 return (CTL_RETVAL_COMPLETE);
6821 ctl_read_capacity(struct ctl_scsiio *ctsio)
6823 struct scsi_read_capacity *cdb;
6824 struct scsi_read_capacity_data *data;
6825 struct ctl_lun *lun;
6828 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6830 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6832 lba = scsi_4btoul(cdb->addr);
6833 if (((cdb->pmi & SRC_PMI) == 0)
6835 ctl_set_invalid_field(/*ctsio*/ ctsio,
6841 ctl_done((union ctl_io *)ctsio);
6842 return (CTL_RETVAL_COMPLETE);
6845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6847 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6848 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6849 ctsio->residual = 0;
6850 ctsio->kern_data_len = sizeof(*data);
6851 ctsio->kern_total_len = sizeof(*data);
6852 ctsio->kern_data_resid = 0;
6853 ctsio->kern_rel_offset = 0;
6854 ctsio->kern_sg_entries = 0;
6857 * If the maximum LBA is greater than 0xfffffffe, the user must
6858 * issue a SERVICE ACTION IN (16) command, with the read capacity
6859 * serivce action set.
6861 if (lun->be_lun->maxlba > 0xfffffffe)
6862 scsi_ulto4b(0xffffffff, data->addr);
6864 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6867 * XXX KDM this may not be 512 bytes...
6869 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6871 ctsio->scsi_status = SCSI_STATUS_OK;
6873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6874 ctsio->be_move_done = ctl_config_move_done;
6875 ctl_datamove((union ctl_io *)ctsio);
6877 return (CTL_RETVAL_COMPLETE);
6881 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6883 struct scsi_read_capacity_16 *cdb;
6884 struct scsi_read_capacity_data_long *data;
6885 struct ctl_lun *lun;
6889 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6891 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6893 alloc_len = scsi_4btoul(cdb->alloc_len);
6894 lba = scsi_8btou64(cdb->addr);
6896 if ((cdb->reladr & SRC16_PMI)
6898 ctl_set_invalid_field(/*ctsio*/ ctsio,
6904 ctl_done((union ctl_io *)ctsio);
6905 return (CTL_RETVAL_COMPLETE);
6908 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6910 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6911 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6913 if (sizeof(*data) < alloc_len) {
6914 ctsio->residual = alloc_len - sizeof(*data);
6915 ctsio->kern_data_len = sizeof(*data);
6916 ctsio->kern_total_len = sizeof(*data);
6918 ctsio->residual = 0;
6919 ctsio->kern_data_len = alloc_len;
6920 ctsio->kern_total_len = alloc_len;
6922 ctsio->kern_data_resid = 0;
6923 ctsio->kern_rel_offset = 0;
6924 ctsio->kern_sg_entries = 0;
6926 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6927 /* XXX KDM this may not be 512 bytes... */
6928 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6929 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6930 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6931 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6932 data->lalba_lbp[0] |= SRC16_LBPME;
6934 ctsio->scsi_status = SCSI_STATUS_OK;
6936 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6937 ctsio->be_move_done = ctl_config_move_done;
6938 ctl_datamove((union ctl_io *)ctsio);
6940 return (CTL_RETVAL_COMPLETE);
6944 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
6946 struct scsi_maintenance_in *cdb;
6948 int alloc_len, total_len = 0;
6949 int num_target_port_groups, single;
6950 struct ctl_lun *lun;
6951 struct ctl_softc *softc;
6952 struct scsi_target_group_data *rtg_ptr;
6953 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6954 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6955 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6957 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
6959 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6960 softc = control_softc;
6961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6963 retval = CTL_RETVAL_COMPLETE;
6965 single = ctl_is_single;
6967 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6969 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6971 total_len = sizeof(struct scsi_target_group_data) +
6972 sizeof(struct scsi_target_port_group_descriptor) *
6973 num_target_port_groups +
6974 sizeof(struct scsi_target_port_descriptor) *
6975 NUM_PORTS_PER_GRP * num_target_port_groups;
6977 alloc_len = scsi_4btoul(cdb->length);
6979 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6981 ctsio->kern_sg_entries = 0;
6983 if (total_len < alloc_len) {
6984 ctsio->residual = alloc_len - total_len;
6985 ctsio->kern_data_len = total_len;
6986 ctsio->kern_total_len = total_len;
6988 ctsio->residual = 0;
6989 ctsio->kern_data_len = alloc_len;
6990 ctsio->kern_total_len = alloc_len;
6992 ctsio->kern_data_resid = 0;
6993 ctsio->kern_rel_offset = 0;
6995 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
6997 tpg_desc_ptr1 = &rtg_ptr->groups[0];
6998 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
6999 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7000 &tp_desc_ptr1_1->desc_list[0];
7003 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7004 &tp_desc_ptr1_2->desc_list[0];
7005 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7006 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7007 &tp_desc_ptr2_1->desc_list[0];
7009 tpg_desc_ptr2 = NULL;
7010 tp_desc_ptr2_1 = NULL;
7011 tp_desc_ptr2_2 = NULL;
7014 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7016 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7017 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7018 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7019 tpg_desc_ptr2->pref_state =
7020 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7022 tpg_desc_ptr1->pref_state =
7023 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7024 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7027 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7028 tpg_desc_ptr1->pref_state =
7029 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7030 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7032 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7033 tpg_desc_ptr2->pref_state =
7034 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7038 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7040 tpg_desc_ptr1->support = 0;
7041 tpg_desc_ptr1->target_port_group[1] = 1;
7042 tpg_desc_ptr1->status = TPG_IMPLICIT;
7043 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7046 tpg_desc_ptr2->support = 0;
7047 tpg_desc_ptr2->target_port_group[1] = 2;
7048 tpg_desc_ptr2->status = TPG_IMPLICIT;
7049 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7051 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7052 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7054 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7055 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7057 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7058 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7059 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7061 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7062 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7066 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7067 ctsio->be_move_done = ctl_config_move_done;
7069 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7070 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7071 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7072 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7073 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7075 ctl_datamove((union ctl_io *)ctsio);
7080 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7082 struct ctl_lun *lun;
7083 struct scsi_report_supported_opcodes *cdb;
7084 const struct ctl_cmd_entry *entry, *sentry;
7085 struct scsi_report_supported_opcodes_all *all;
7086 struct scsi_report_supported_opcodes_descr *descr;
7087 struct scsi_report_supported_opcodes_one *one;
7089 int alloc_len, total_len;
7090 int opcode, service_action, i, j, num;
7092 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7094 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7095 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7097 retval = CTL_RETVAL_COMPLETE;
7099 opcode = cdb->requested_opcode;
7100 service_action = scsi_2btoul(cdb->requested_service_action);
7101 switch (cdb->options & RSO_OPTIONS_MASK) {
7102 case RSO_OPTIONS_ALL:
7104 for (i = 0; i < 256; i++) {
7105 entry = &ctl_cmd_table[i];
7106 if (entry->flags & CTL_CMD_FLAG_SA5) {
7107 for (j = 0; j < 32; j++) {
7108 sentry = &((const struct ctl_cmd_entry *)
7110 if (ctl_cmd_applicable(
7111 lun->be_lun->lun_type, sentry))
7115 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7120 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7121 num * sizeof(struct scsi_report_supported_opcodes_descr);
7123 case RSO_OPTIONS_OC:
7124 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7125 ctl_set_invalid_field(/*ctsio*/ ctsio,
7131 ctl_done((union ctl_io *)ctsio);
7132 return (CTL_RETVAL_COMPLETE);
7134 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7136 case RSO_OPTIONS_OC_SA:
7137 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7138 service_action >= 32) {
7139 ctl_set_invalid_field(/*ctsio*/ ctsio,
7145 ctl_done((union ctl_io *)ctsio);
7146 return (CTL_RETVAL_COMPLETE);
7148 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7151 ctl_set_invalid_field(/*ctsio*/ ctsio,
7157 ctl_done((union ctl_io *)ctsio);
7158 return (CTL_RETVAL_COMPLETE);
7161 alloc_len = scsi_4btoul(cdb->length);
7163 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7165 ctsio->kern_sg_entries = 0;
7167 if (total_len < alloc_len) {
7168 ctsio->residual = alloc_len - total_len;
7169 ctsio->kern_data_len = total_len;
7170 ctsio->kern_total_len = total_len;
7172 ctsio->residual = 0;
7173 ctsio->kern_data_len = alloc_len;
7174 ctsio->kern_total_len = alloc_len;
7176 ctsio->kern_data_resid = 0;
7177 ctsio->kern_rel_offset = 0;
7179 switch (cdb->options & RSO_OPTIONS_MASK) {
7180 case RSO_OPTIONS_ALL:
7181 all = (struct scsi_report_supported_opcodes_all *)
7182 ctsio->kern_data_ptr;
7184 for (i = 0; i < 256; i++) {
7185 entry = &ctl_cmd_table[i];
7186 if (entry->flags & CTL_CMD_FLAG_SA5) {
7187 for (j = 0; j < 32; j++) {
7188 sentry = &((const struct ctl_cmd_entry *)
7190 if (!ctl_cmd_applicable(
7191 lun->be_lun->lun_type, sentry))
7193 descr = &all->descr[num++];
7195 scsi_ulto2b(j, descr->service_action);
7196 descr->flags = RSO_SERVACTV;
7197 scsi_ulto2b(sentry->length,
7201 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7204 descr = &all->descr[num++];
7206 scsi_ulto2b(0, descr->service_action);
7208 scsi_ulto2b(entry->length, descr->cdb_length);
7212 num * sizeof(struct scsi_report_supported_opcodes_descr),
7215 case RSO_OPTIONS_OC:
7216 one = (struct scsi_report_supported_opcodes_one *)
7217 ctsio->kern_data_ptr;
7218 entry = &ctl_cmd_table[opcode];
7220 case RSO_OPTIONS_OC_SA:
7221 one = (struct scsi_report_supported_opcodes_one *)
7222 ctsio->kern_data_ptr;
7223 entry = &ctl_cmd_table[opcode];
7224 entry = &((const struct ctl_cmd_entry *)
7225 entry->execute)[service_action];
7227 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7229 scsi_ulto2b(entry->length, one->cdb_length);
7230 one->cdb_usage[0] = opcode;
7231 memcpy(&one->cdb_usage[1], entry->usage,
7238 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7239 ctsio->be_move_done = ctl_config_move_done;
7241 ctl_datamove((union ctl_io *)ctsio);
7246 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7248 struct ctl_lun *lun;
7249 struct scsi_report_supported_tmf *cdb;
7250 struct scsi_report_supported_tmf_data *data;
7252 int alloc_len, total_len;
7254 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7256 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7257 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7259 retval = CTL_RETVAL_COMPLETE;
7261 total_len = sizeof(struct scsi_report_supported_tmf_data);
7262 alloc_len = scsi_4btoul(cdb->length);
7264 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7266 ctsio->kern_sg_entries = 0;
7268 if (total_len < alloc_len) {
7269 ctsio->residual = alloc_len - total_len;
7270 ctsio->kern_data_len = total_len;
7271 ctsio->kern_total_len = total_len;
7273 ctsio->residual = 0;
7274 ctsio->kern_data_len = alloc_len;
7275 ctsio->kern_total_len = alloc_len;
7277 ctsio->kern_data_resid = 0;
7278 ctsio->kern_rel_offset = 0;
7280 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7281 data->byte1 |= RST_ATS | RST_LURS | RST_TRS;
7283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7284 ctsio->be_move_done = ctl_config_move_done;
7286 ctl_datamove((union ctl_io *)ctsio);
7291 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7293 struct ctl_lun *lun;
7294 struct scsi_report_timestamp *cdb;
7295 struct scsi_report_timestamp_data *data;
7299 int alloc_len, total_len;
7301 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7303 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7304 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7306 retval = CTL_RETVAL_COMPLETE;
7308 total_len = sizeof(struct scsi_report_timestamp_data);
7309 alloc_len = scsi_4btoul(cdb->length);
7311 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7313 ctsio->kern_sg_entries = 0;
7315 if (total_len < alloc_len) {
7316 ctsio->residual = alloc_len - total_len;
7317 ctsio->kern_data_len = total_len;
7318 ctsio->kern_total_len = total_len;
7320 ctsio->residual = 0;
7321 ctsio->kern_data_len = alloc_len;
7322 ctsio->kern_total_len = alloc_len;
7324 ctsio->kern_data_resid = 0;
7325 ctsio->kern_rel_offset = 0;
7327 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7328 scsi_ulto2b(sizeof(*data) - 2, data->length);
7329 data->origin = RTS_ORIG_OUTSIDE;
7331 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7332 scsi_ulto4b(timestamp >> 16, data->timestamp);
7333 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7336 ctsio->be_move_done = ctl_config_move_done;
7338 ctl_datamove((union ctl_io *)ctsio);
7343 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7345 struct scsi_per_res_in *cdb;
7346 int alloc_len, total_len = 0;
7347 /* struct scsi_per_res_in_rsrv in_data; */
7348 struct ctl_lun *lun;
7349 struct ctl_softc *softc;
7351 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7353 softc = control_softc;
7355 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7357 alloc_len = scsi_2btoul(cdb->length);
7359 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7362 mtx_lock(&lun->lun_lock);
7363 switch (cdb->action) {
7364 case SPRI_RK: /* read keys */
7365 total_len = sizeof(struct scsi_per_res_in_keys) +
7367 sizeof(struct scsi_per_res_key);
7369 case SPRI_RR: /* read reservation */
7370 if (lun->flags & CTL_LUN_PR_RESERVED)
7371 total_len = sizeof(struct scsi_per_res_in_rsrv);
7373 total_len = sizeof(struct scsi_per_res_in_header);
7375 case SPRI_RC: /* report capabilities */
7376 total_len = sizeof(struct scsi_per_res_cap);
7379 panic("Invalid PR type %x", cdb->action);
7381 mtx_unlock(&lun->lun_lock);
7383 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7385 if (total_len < alloc_len) {
7386 ctsio->residual = alloc_len - total_len;
7387 ctsio->kern_data_len = total_len;
7388 ctsio->kern_total_len = total_len;
7390 ctsio->residual = 0;
7391 ctsio->kern_data_len = alloc_len;
7392 ctsio->kern_total_len = alloc_len;
7395 ctsio->kern_data_resid = 0;
7396 ctsio->kern_rel_offset = 0;
7397 ctsio->kern_sg_entries = 0;
7399 mtx_lock(&lun->lun_lock);
7400 switch (cdb->action) {
7401 case SPRI_RK: { // read keys
7402 struct scsi_per_res_in_keys *res_keys;
7405 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7408 * We had to drop the lock to allocate our buffer, which
7409 * leaves time for someone to come in with another
7410 * persistent reservation. (That is unlikely, though,
7411 * since this should be the only persistent reservation
7412 * command active right now.)
7414 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7415 (lun->pr_key_count *
7416 sizeof(struct scsi_per_res_key)))){
7417 mtx_unlock(&lun->lun_lock);
7418 free(ctsio->kern_data_ptr, M_CTL);
7419 printf("%s: reservation length changed, retrying\n",
7424 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7426 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7427 lun->pr_key_count, res_keys->header.length);
7429 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7430 if (!lun->per_res[i].registered)
7434 * We used lun->pr_key_count to calculate the
7435 * size to allocate. If it turns out the number of
7436 * initiators with the registered flag set is
7437 * larger than that (i.e. they haven't been kept in
7438 * sync), we've got a problem.
7440 if (key_count >= lun->pr_key_count) {
7442 csevent_log(CSC_CTL | CSC_SHELF_SW |
7444 csevent_LogType_Fault,
7445 csevent_AlertLevel_Yellow,
7446 csevent_FRU_ShelfController,
7447 csevent_FRU_Firmware,
7448 csevent_FRU_Unknown,
7449 "registered keys %d >= key "
7450 "count %d", key_count,
7456 memcpy(res_keys->keys[key_count].key,
7457 lun->per_res[i].res_key.key,
7458 ctl_min(sizeof(res_keys->keys[key_count].key),
7459 sizeof(lun->per_res[i].res_key)));
7464 case SPRI_RR: { // read reservation
7465 struct scsi_per_res_in_rsrv *res;
7466 int tmp_len, header_only;
7468 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7470 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7472 if (lun->flags & CTL_LUN_PR_RESERVED)
7474 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7475 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7476 res->header.length);
7479 tmp_len = sizeof(struct scsi_per_res_in_header);
7480 scsi_ulto4b(0, res->header.length);
7485 * We had to drop the lock to allocate our buffer, which
7486 * leaves time for someone to come in with another
7487 * persistent reservation. (That is unlikely, though,
7488 * since this should be the only persistent reservation
7489 * command active right now.)
7491 if (tmp_len != total_len) {
7492 mtx_unlock(&lun->lun_lock);
7493 free(ctsio->kern_data_ptr, M_CTL);
7494 printf("%s: reservation status changed, retrying\n",
7500 * No reservation held, so we're done.
7502 if (header_only != 0)
7506 * If the registration is an All Registrants type, the key
7507 * is 0, since it doesn't really matter.
7509 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7510 memcpy(res->data.reservation,
7511 &lun->per_res[lun->pr_res_idx].res_key,
7512 sizeof(struct scsi_per_res_key));
7514 res->data.scopetype = lun->res_type;
7517 case SPRI_RC: //report capabilities
7519 struct scsi_per_res_cap *res_cap;
7522 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7523 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7524 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7525 type_mask = SPRI_TM_WR_EX_AR |
7531 scsi_ulto2b(type_mask, res_cap->type_mask);
7534 case SPRI_RS: //read full status
7537 * This is a bug, because we just checked for this above,
7538 * and should have returned an error.
7540 panic("Invalid PR type %x", cdb->action);
7541 break; /* NOTREACHED */
7543 mtx_unlock(&lun->lun_lock);
7545 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7546 ctsio->be_move_done = ctl_config_move_done;
7548 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7549 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7550 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7551 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7552 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7554 ctl_datamove((union ctl_io *)ctsio);
7556 return (CTL_RETVAL_COMPLETE);
7560 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7564 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7565 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7566 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7567 struct scsi_per_res_out_parms* param)
7569 union ctl_ha_msg persis_io;
7575 mtx_lock(&lun->lun_lock);
7576 if (sa_res_key == 0) {
7577 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7578 /* validate scope and type */
7579 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7581 mtx_unlock(&lun->lun_lock);
7582 ctl_set_invalid_field(/*ctsio*/ ctsio,
7588 ctl_done((union ctl_io *)ctsio);
7592 if (type>8 || type==2 || type==4 || type==0) {
7593 mtx_unlock(&lun->lun_lock);
7594 ctl_set_invalid_field(/*ctsio*/ ctsio,
7600 ctl_done((union ctl_io *)ctsio);
7604 /* temporarily unregister this nexus */
7605 lun->per_res[residx].registered = 0;
7608 * Unregister everybody else and build UA for
7611 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7612 if (lun->per_res[i].registered == 0)
7616 && i <CTL_MAX_INITIATORS)
7617 lun->pending_sense[i].ua_pending |=
7619 else if (persis_offset
7620 && i >= persis_offset)
7621 lun->pending_sense[i-persis_offset
7624 lun->per_res[i].registered = 0;
7625 memset(&lun->per_res[i].res_key, 0,
7626 sizeof(struct scsi_per_res_key));
7628 lun->per_res[residx].registered = 1;
7629 lun->pr_key_count = 1;
7630 lun->res_type = type;
7631 if (lun->res_type != SPR_TYPE_WR_EX_AR
7632 && lun->res_type != SPR_TYPE_EX_AC_AR)
7633 lun->pr_res_idx = residx;
7635 /* send msg to other side */
7636 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7637 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7638 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7639 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7640 persis_io.pr.pr_info.res_type = type;
7641 memcpy(persis_io.pr.pr_info.sa_res_key,
7642 param->serv_act_res_key,
7643 sizeof(param->serv_act_res_key));
7644 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7645 &persis_io, sizeof(persis_io), 0)) >
7646 CTL_HA_STATUS_SUCCESS) {
7647 printf("CTL:Persis Out error returned "
7648 "from ctl_ha_msg_send %d\n",
7652 /* not all registrants */
7653 mtx_unlock(&lun->lun_lock);
7654 free(ctsio->kern_data_ptr, M_CTL);
7655 ctl_set_invalid_field(ctsio,
7661 ctl_done((union ctl_io *)ctsio);
7664 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7665 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7668 if (res_key == sa_res_key) {
7671 * The spec implies this is not good but doesn't
7672 * say what to do. There are two choices either
7673 * generate a res conflict or check condition
7674 * with illegal field in parameter data. Since
7675 * that is what is done when the sa_res_key is
7676 * zero I'll take that approach since this has
7677 * to do with the sa_res_key.
7679 mtx_unlock(&lun->lun_lock);
7680 free(ctsio->kern_data_ptr, M_CTL);
7681 ctl_set_invalid_field(ctsio,
7687 ctl_done((union ctl_io *)ctsio);
7691 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7692 if (lun->per_res[i].registered
7693 && memcmp(param->serv_act_res_key,
7694 lun->per_res[i].res_key.key,
7695 sizeof(struct scsi_per_res_key)) != 0)
7699 lun->per_res[i].registered = 0;
7700 memset(&lun->per_res[i].res_key, 0,
7701 sizeof(struct scsi_per_res_key));
7702 lun->pr_key_count--;
7705 && i < CTL_MAX_INITIATORS)
7706 lun->pending_sense[i].ua_pending |=
7708 else if (persis_offset
7709 && i >= persis_offset)
7710 lun->pending_sense[i-persis_offset].ua_pending|=
7714 mtx_unlock(&lun->lun_lock);
7715 free(ctsio->kern_data_ptr, M_CTL);
7716 ctl_set_reservation_conflict(ctsio);
7717 ctl_done((union ctl_io *)ctsio);
7718 return (CTL_RETVAL_COMPLETE);
7720 /* send msg to other side */
7721 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7722 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7723 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7724 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7725 persis_io.pr.pr_info.res_type = type;
7726 memcpy(persis_io.pr.pr_info.sa_res_key,
7727 param->serv_act_res_key,
7728 sizeof(param->serv_act_res_key));
7729 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7730 &persis_io, sizeof(persis_io), 0)) >
7731 CTL_HA_STATUS_SUCCESS) {
7732 printf("CTL:Persis Out error returned from "
7733 "ctl_ha_msg_send %d\n", isc_retval);
7736 /* Reserved but not all registrants */
7737 /* sa_res_key is res holder */
7738 if (memcmp(param->serv_act_res_key,
7739 lun->per_res[lun->pr_res_idx].res_key.key,
7740 sizeof(struct scsi_per_res_key)) == 0) {
7741 /* validate scope and type */
7742 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7744 mtx_unlock(&lun->lun_lock);
7745 ctl_set_invalid_field(/*ctsio*/ ctsio,
7751 ctl_done((union ctl_io *)ctsio);
7755 if (type>8 || type==2 || type==4 || type==0) {
7756 mtx_unlock(&lun->lun_lock);
7757 ctl_set_invalid_field(/*ctsio*/ ctsio,
7763 ctl_done((union ctl_io *)ctsio);
7769 * if sa_res_key != res_key remove all
7770 * registrants w/sa_res_key and generate UA
7771 * for these registrants(Registrations
7772 * Preempted) if it wasn't an exclusive
7773 * reservation generate UA(Reservations
7774 * Preempted) for all other registered nexuses
7775 * if the type has changed. Establish the new
7776 * reservation and holder. If res_key and
7777 * sa_res_key are the same do the above
7778 * except don't unregister the res holder.
7782 * Temporarily unregister so it won't get
7783 * removed or UA generated
7785 lun->per_res[residx].registered = 0;
7786 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7787 if (lun->per_res[i].registered == 0)
7790 if (memcmp(param->serv_act_res_key,
7791 lun->per_res[i].res_key.key,
7792 sizeof(struct scsi_per_res_key)) == 0) {
7793 lun->per_res[i].registered = 0;
7794 memset(&lun->per_res[i].res_key,
7796 sizeof(struct scsi_per_res_key));
7797 lun->pr_key_count--;
7800 && i < CTL_MAX_INITIATORS)
7801 lun->pending_sense[i
7804 else if (persis_offset
7805 && i >= persis_offset)
7807 i-persis_offset].ua_pending |=
7809 } else if (type != lun->res_type
7810 && (lun->res_type == SPR_TYPE_WR_EX_RO
7811 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7813 && i < CTL_MAX_INITIATORS)
7814 lun->pending_sense[i
7817 else if (persis_offset
7818 && i >= persis_offset)
7825 lun->per_res[residx].registered = 1;
7826 lun->res_type = type;
7827 if (lun->res_type != SPR_TYPE_WR_EX_AR
7828 && lun->res_type != SPR_TYPE_EX_AC_AR)
7829 lun->pr_res_idx = residx;
7832 CTL_PR_ALL_REGISTRANTS;
7834 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7835 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7836 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7837 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7838 persis_io.pr.pr_info.res_type = type;
7839 memcpy(persis_io.pr.pr_info.sa_res_key,
7840 param->serv_act_res_key,
7841 sizeof(param->serv_act_res_key));
7842 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7843 &persis_io, sizeof(persis_io), 0)) >
7844 CTL_HA_STATUS_SUCCESS) {
7845 printf("CTL:Persis Out error returned "
7846 "from ctl_ha_msg_send %d\n",
7851 * sa_res_key is not the res holder just
7852 * remove registrants
7856 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7857 if (memcmp(param->serv_act_res_key,
7858 lun->per_res[i].res_key.key,
7859 sizeof(struct scsi_per_res_key)) != 0)
7863 lun->per_res[i].registered = 0;
7864 memset(&lun->per_res[i].res_key, 0,
7865 sizeof(struct scsi_per_res_key));
7866 lun->pr_key_count--;
7869 && i < CTL_MAX_INITIATORS)
7870 lun->pending_sense[i].ua_pending |=
7872 else if (persis_offset
7873 && i >= persis_offset)
7875 i-persis_offset].ua_pending |=
7880 mtx_unlock(&lun->lun_lock);
7881 free(ctsio->kern_data_ptr, M_CTL);
7882 ctl_set_reservation_conflict(ctsio);
7883 ctl_done((union ctl_io *)ctsio);
7886 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7887 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7888 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7889 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7890 persis_io.pr.pr_info.res_type = type;
7891 memcpy(persis_io.pr.pr_info.sa_res_key,
7892 param->serv_act_res_key,
7893 sizeof(param->serv_act_res_key));
7894 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7895 &persis_io, sizeof(persis_io), 0)) >
7896 CTL_HA_STATUS_SUCCESS) {
7897 printf("CTL:Persis Out error returned "
7898 "from ctl_ha_msg_send %d\n",
7904 lun->PRGeneration++;
7905 mtx_unlock(&lun->lun_lock);
7911 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7915 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7916 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7917 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7918 msg->pr.pr_info.sa_res_key,
7919 sizeof(struct scsi_per_res_key)) != 0) {
7920 uint64_t sa_res_key;
7921 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7923 if (sa_res_key == 0) {
7924 /* temporarily unregister this nexus */
7925 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7928 * Unregister everybody else and build UA for
7931 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7932 if (lun->per_res[i].registered == 0)
7936 && i < CTL_MAX_INITIATORS)
7937 lun->pending_sense[i].ua_pending |=
7939 else if (persis_offset && i >= persis_offset)
7940 lun->pending_sense[i -
7941 persis_offset].ua_pending |=
7943 lun->per_res[i].registered = 0;
7944 memset(&lun->per_res[i].res_key, 0,
7945 sizeof(struct scsi_per_res_key));
7948 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7949 lun->pr_key_count = 1;
7950 lun->res_type = msg->pr.pr_info.res_type;
7951 if (lun->res_type != SPR_TYPE_WR_EX_AR
7952 && lun->res_type != SPR_TYPE_EX_AC_AR)
7953 lun->pr_res_idx = msg->pr.pr_info.residx;
7955 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7956 if (memcmp(msg->pr.pr_info.sa_res_key,
7957 lun->per_res[i].res_key.key,
7958 sizeof(struct scsi_per_res_key)) != 0)
7961 lun->per_res[i].registered = 0;
7962 memset(&lun->per_res[i].res_key, 0,
7963 sizeof(struct scsi_per_res_key));
7964 lun->pr_key_count--;
7967 && i < persis_offset)
7968 lun->pending_sense[i].ua_pending |=
7970 else if (persis_offset
7971 && i >= persis_offset)
7972 lun->pending_sense[i -
7973 persis_offset].ua_pending |=
7979 * Temporarily unregister so it won't get removed
7982 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7983 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7984 if (lun->per_res[i].registered == 0)
7987 if (memcmp(msg->pr.pr_info.sa_res_key,
7988 lun->per_res[i].res_key.key,
7989 sizeof(struct scsi_per_res_key)) == 0) {
7990 lun->per_res[i].registered = 0;
7991 memset(&lun->per_res[i].res_key, 0,
7992 sizeof(struct scsi_per_res_key));
7993 lun->pr_key_count--;
7995 && i < CTL_MAX_INITIATORS)
7996 lun->pending_sense[i].ua_pending |=
7998 else if (persis_offset
7999 && i >= persis_offset)
8000 lun->pending_sense[i -
8001 persis_offset].ua_pending |=
8003 } else if (msg->pr.pr_info.res_type != lun->res_type
8004 && (lun->res_type == SPR_TYPE_WR_EX_RO
8005 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8007 && i < persis_offset)
8008 lun->pending_sense[i
8011 else if (persis_offset
8012 && i >= persis_offset)
8013 lun->pending_sense[i -
8014 persis_offset].ua_pending |=
8018 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8019 lun->res_type = msg->pr.pr_info.res_type;
8020 if (lun->res_type != SPR_TYPE_WR_EX_AR
8021 && lun->res_type != SPR_TYPE_EX_AC_AR)
8022 lun->pr_res_idx = msg->pr.pr_info.residx;
8024 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8026 lun->PRGeneration++;
8032 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8036 u_int32_t param_len;
8037 struct scsi_per_res_out *cdb;
8038 struct ctl_lun *lun;
8039 struct scsi_per_res_out_parms* param;
8040 struct ctl_softc *softc;
8042 uint64_t res_key, sa_res_key;
8044 union ctl_ha_msg persis_io;
8047 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8049 retval = CTL_RETVAL_COMPLETE;
8051 softc = control_softc;
8053 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8054 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8057 * We only support whole-LUN scope. The scope & type are ignored for
8058 * register, register and ignore existing key and clear.
8059 * We sometimes ignore scope and type on preempts too!!
8060 * Verify reservation type here as well.
8062 type = cdb->scope_type & SPR_TYPE_MASK;
8063 if ((cdb->action == SPRO_RESERVE)
8064 || (cdb->action == SPRO_RELEASE)) {
8065 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8066 ctl_set_invalid_field(/*ctsio*/ ctsio,
8072 ctl_done((union ctl_io *)ctsio);
8073 return (CTL_RETVAL_COMPLETE);
8076 if (type>8 || type==2 || type==4 || type==0) {
8077 ctl_set_invalid_field(/*ctsio*/ ctsio,
8083 ctl_done((union ctl_io *)ctsio);
8084 return (CTL_RETVAL_COMPLETE);
8088 param_len = scsi_4btoul(cdb->length);
8090 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8091 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8092 ctsio->kern_data_len = param_len;
8093 ctsio->kern_total_len = param_len;
8094 ctsio->kern_data_resid = 0;
8095 ctsio->kern_rel_offset = 0;
8096 ctsio->kern_sg_entries = 0;
8097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8098 ctsio->be_move_done = ctl_config_move_done;
8099 ctl_datamove((union ctl_io *)ctsio);
8101 return (CTL_RETVAL_COMPLETE);
8104 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8106 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8107 res_key = scsi_8btou64(param->res_key.key);
8108 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8111 * Validate the reservation key here except for SPRO_REG_IGNO
8112 * This must be done for all other service actions
8114 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8115 mtx_lock(&lun->lun_lock);
8116 if (lun->per_res[residx].registered) {
8117 if (memcmp(param->res_key.key,
8118 lun->per_res[residx].res_key.key,
8119 ctl_min(sizeof(param->res_key),
8120 sizeof(lun->per_res[residx].res_key))) != 0) {
8122 * The current key passed in doesn't match
8123 * the one the initiator previously
8126 mtx_unlock(&lun->lun_lock);
8127 free(ctsio->kern_data_ptr, M_CTL);
8128 ctl_set_reservation_conflict(ctsio);
8129 ctl_done((union ctl_io *)ctsio);
8130 return (CTL_RETVAL_COMPLETE);
8132 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8134 * We are not registered
8136 mtx_unlock(&lun->lun_lock);
8137 free(ctsio->kern_data_ptr, M_CTL);
8138 ctl_set_reservation_conflict(ctsio);
8139 ctl_done((union ctl_io *)ctsio);
8140 return (CTL_RETVAL_COMPLETE);
8141 } else if (res_key != 0) {
8143 * We are not registered and trying to register but
8144 * the register key isn't zero.
8146 mtx_unlock(&lun->lun_lock);
8147 free(ctsio->kern_data_ptr, M_CTL);
8148 ctl_set_reservation_conflict(ctsio);
8149 ctl_done((union ctl_io *)ctsio);
8150 return (CTL_RETVAL_COMPLETE);
8152 mtx_unlock(&lun->lun_lock);
8155 switch (cdb->action & SPRO_ACTION_MASK) {
8157 case SPRO_REG_IGNO: {
8160 printf("Registration received\n");
8164 * We don't support any of these options, as we report in
8165 * the read capabilities request (see
8166 * ctl_persistent_reserve_in(), above).
8168 if ((param->flags & SPR_SPEC_I_PT)
8169 || (param->flags & SPR_ALL_TG_PT)
8170 || (param->flags & SPR_APTPL)) {
8173 if (param->flags & SPR_APTPL)
8175 else if (param->flags & SPR_ALL_TG_PT)
8177 else /* SPR_SPEC_I_PT */
8180 free(ctsio->kern_data_ptr, M_CTL);
8181 ctl_set_invalid_field(ctsio,
8187 ctl_done((union ctl_io *)ctsio);
8188 return (CTL_RETVAL_COMPLETE);
8191 mtx_lock(&lun->lun_lock);
8194 * The initiator wants to clear the
8197 if (sa_res_key == 0) {
8199 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8200 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8201 && !lun->per_res[residx].registered)) {
8202 mtx_unlock(&lun->lun_lock);
8206 lun->per_res[residx].registered = 0;
8207 memset(&lun->per_res[residx].res_key,
8208 0, sizeof(lun->per_res[residx].res_key));
8209 lun->pr_key_count--;
8211 if (residx == lun->pr_res_idx) {
8212 lun->flags &= ~CTL_LUN_PR_RESERVED;
8213 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8215 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8216 || lun->res_type == SPR_TYPE_EX_AC_RO)
8217 && lun->pr_key_count) {
8219 * If the reservation is a registrants
8220 * only type we need to generate a UA
8221 * for other registered inits. The
8222 * sense code should be RESERVATIONS
8226 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8228 i+persis_offset].registered
8231 lun->pending_sense[i
8237 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8238 if (lun->pr_key_count==0) {
8239 lun->flags &= ~CTL_LUN_PR_RESERVED;
8241 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8244 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8245 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8246 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8247 persis_io.pr.pr_info.residx = residx;
8248 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8249 &persis_io, sizeof(persis_io), 0 )) >
8250 CTL_HA_STATUS_SUCCESS) {
8251 printf("CTL:Persis Out error returned from "
8252 "ctl_ha_msg_send %d\n", isc_retval);
8254 } else /* sa_res_key != 0 */ {
8257 * If we aren't registered currently then increment
8258 * the key count and set the registered flag.
8260 if (!lun->per_res[residx].registered) {
8261 lun->pr_key_count++;
8262 lun->per_res[residx].registered = 1;
8265 memcpy(&lun->per_res[residx].res_key,
8266 param->serv_act_res_key,
8267 ctl_min(sizeof(param->serv_act_res_key),
8268 sizeof(lun->per_res[residx].res_key)));
8270 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8271 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8272 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8273 persis_io.pr.pr_info.residx = residx;
8274 memcpy(persis_io.pr.pr_info.sa_res_key,
8275 param->serv_act_res_key,
8276 sizeof(param->serv_act_res_key));
8277 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8278 &persis_io, sizeof(persis_io), 0)) >
8279 CTL_HA_STATUS_SUCCESS) {
8280 printf("CTL:Persis Out error returned from "
8281 "ctl_ha_msg_send %d\n", isc_retval);
8284 lun->PRGeneration++;
8285 mtx_unlock(&lun->lun_lock);
8291 printf("Reserve executed type %d\n", type);
8293 mtx_lock(&lun->lun_lock);
8294 if (lun->flags & CTL_LUN_PR_RESERVED) {
8296 * if this isn't the reservation holder and it's
8297 * not a "all registrants" type or if the type is
8298 * different then we have a conflict
8300 if ((lun->pr_res_idx != residx
8301 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8302 || lun->res_type != type) {
8303 mtx_unlock(&lun->lun_lock);
8304 free(ctsio->kern_data_ptr, M_CTL);
8305 ctl_set_reservation_conflict(ctsio);
8306 ctl_done((union ctl_io *)ctsio);
8307 return (CTL_RETVAL_COMPLETE);
8309 mtx_unlock(&lun->lun_lock);
8310 } else /* create a reservation */ {
8312 * If it's not an "all registrants" type record
8313 * reservation holder
8315 if (type != SPR_TYPE_WR_EX_AR
8316 && type != SPR_TYPE_EX_AC_AR)
8317 lun->pr_res_idx = residx; /* Res holder */
8319 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8321 lun->flags |= CTL_LUN_PR_RESERVED;
8322 lun->res_type = type;
8324 mtx_unlock(&lun->lun_lock);
8326 /* send msg to other side */
8327 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8328 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8329 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8330 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8331 persis_io.pr.pr_info.res_type = type;
8332 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8333 &persis_io, sizeof(persis_io), 0)) >
8334 CTL_HA_STATUS_SUCCESS) {
8335 printf("CTL:Persis Out error returned from "
8336 "ctl_ha_msg_send %d\n", isc_retval);
8342 mtx_lock(&lun->lun_lock);
8343 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8344 /* No reservation exists return good status */
8345 mtx_unlock(&lun->lun_lock);
8349 * Is this nexus a reservation holder?
8351 if (lun->pr_res_idx != residx
8352 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8354 * not a res holder return good status but
8357 mtx_unlock(&lun->lun_lock);
8361 if (lun->res_type != type) {
8362 mtx_unlock(&lun->lun_lock);
8363 free(ctsio->kern_data_ptr, M_CTL);
8364 ctl_set_illegal_pr_release(ctsio);
8365 ctl_done((union ctl_io *)ctsio);
8366 return (CTL_RETVAL_COMPLETE);
8369 /* okay to release */
8370 lun->flags &= ~CTL_LUN_PR_RESERVED;
8371 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8375 * if this isn't an exclusive access
8376 * res generate UA for all other
8379 if (type != SPR_TYPE_EX_AC
8380 && type != SPR_TYPE_WR_EX) {
8382 * temporarily unregister so we don't generate UA
8384 lun->per_res[residx].registered = 0;
8386 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8387 if (lun->per_res[i+persis_offset].registered
8390 lun->pending_sense[i].ua_pending |=
8394 lun->per_res[residx].registered = 1;
8396 mtx_unlock(&lun->lun_lock);
8397 /* Send msg to other side */
8398 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8399 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8400 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8401 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8402 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8403 printf("CTL:Persis Out error returned from "
8404 "ctl_ha_msg_send %d\n", isc_retval);
8409 /* send msg to other side */
8411 mtx_lock(&lun->lun_lock);
8412 lun->flags &= ~CTL_LUN_PR_RESERVED;
8414 lun->pr_key_count = 0;
8415 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8418 memset(&lun->per_res[residx].res_key,
8419 0, sizeof(lun->per_res[residx].res_key));
8420 lun->per_res[residx].registered = 0;
8422 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8423 if (lun->per_res[i].registered) {
8424 if (!persis_offset && i < CTL_MAX_INITIATORS)
8425 lun->pending_sense[i].ua_pending |=
8427 else if (persis_offset && i >= persis_offset)
8428 lun->pending_sense[i-persis_offset
8429 ].ua_pending |= CTL_UA_RES_PREEMPT;
8431 memset(&lun->per_res[i].res_key,
8432 0, sizeof(struct scsi_per_res_key));
8433 lun->per_res[i].registered = 0;
8435 lun->PRGeneration++;
8436 mtx_unlock(&lun->lun_lock);
8437 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8438 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8439 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8440 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8441 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8442 printf("CTL:Persis Out error returned from "
8443 "ctl_ha_msg_send %d\n", isc_retval);
8447 case SPRO_PREEMPT: {
8450 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8451 residx, ctsio, cdb, param);
8453 return (CTL_RETVAL_COMPLETE);
8457 panic("Invalid PR type %x", cdb->action);
8461 free(ctsio->kern_data_ptr, M_CTL);
8462 ctl_set_success(ctsio);
8463 ctl_done((union ctl_io *)ctsio);
8469 * This routine is for handling a message from the other SC pertaining to
8470 * persistent reserve out. All the error checking will have been done
8471 * so only perorming the action need be done here to keep the two
8475 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8477 struct ctl_lun *lun;
8478 struct ctl_softc *softc;
8482 softc = control_softc;
8484 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8485 lun = softc->ctl_luns[targ_lun];
8486 mtx_lock(&lun->lun_lock);
8487 switch(msg->pr.pr_info.action) {
8488 case CTL_PR_REG_KEY:
8489 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8490 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8491 lun->pr_key_count++;
8493 lun->PRGeneration++;
8494 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8495 msg->pr.pr_info.sa_res_key,
8496 sizeof(struct scsi_per_res_key));
8499 case CTL_PR_UNREG_KEY:
8500 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8501 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8502 0, sizeof(struct scsi_per_res_key));
8503 lun->pr_key_count--;
8505 /* XXX Need to see if the reservation has been released */
8506 /* if so do we need to generate UA? */
8507 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8508 lun->flags &= ~CTL_LUN_PR_RESERVED;
8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8511 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8512 || lun->res_type == SPR_TYPE_EX_AC_RO)
8513 && lun->pr_key_count) {
8515 * If the reservation is a registrants
8516 * only type we need to generate a UA
8517 * for other registered inits. The
8518 * sense code should be RESERVATIONS
8522 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8524 persis_offset].registered == 0)
8527 lun->pending_sense[i
8533 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8534 if (lun->pr_key_count==0) {
8535 lun->flags &= ~CTL_LUN_PR_RESERVED;
8537 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8540 lun->PRGeneration++;
8543 case CTL_PR_RESERVE:
8544 lun->flags |= CTL_LUN_PR_RESERVED;
8545 lun->res_type = msg->pr.pr_info.res_type;
8546 lun->pr_res_idx = msg->pr.pr_info.residx;
8550 case CTL_PR_RELEASE:
8552 * if this isn't an exclusive access res generate UA for all
8553 * other registrants.
8555 if (lun->res_type != SPR_TYPE_EX_AC
8556 && lun->res_type != SPR_TYPE_WR_EX) {
8557 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8558 if (lun->per_res[i+persis_offset].registered)
8559 lun->pending_sense[i].ua_pending |=
8563 lun->flags &= ~CTL_LUN_PR_RESERVED;
8564 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8568 case CTL_PR_PREEMPT:
8569 ctl_pro_preempt_other(lun, msg);
8572 lun->flags &= ~CTL_LUN_PR_RESERVED;
8574 lun->pr_key_count = 0;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8577 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8578 if (lun->per_res[i].registered == 0)
8581 && i < CTL_MAX_INITIATORS)
8582 lun->pending_sense[i].ua_pending |=
8584 else if (persis_offset
8585 && i >= persis_offset)
8586 lun->pending_sense[i-persis_offset].ua_pending|=
8588 memset(&lun->per_res[i].res_key, 0,
8589 sizeof(struct scsi_per_res_key));
8590 lun->per_res[i].registered = 0;
8592 lun->PRGeneration++;
8596 mtx_unlock(&lun->lun_lock);
8600 ctl_read_write(struct ctl_scsiio *ctsio)
8602 struct ctl_lun *lun;
8603 struct ctl_lba_len_flags *lbalen;
8605 uint32_t num_blocks;
8610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8612 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8617 retval = CTL_RETVAL_COMPLETE;
8619 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8620 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8621 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8625 * XXX KDM need a lock here.
8627 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8628 if ((lun->res_type == SPR_TYPE_EX_AC
8629 && residx != lun->pr_res_idx)
8630 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8631 || lun->res_type == SPR_TYPE_EX_AC_AR)
8632 && !lun->per_res[residx].registered)) {
8633 ctl_set_reservation_conflict(ctsio);
8634 ctl_done((union ctl_io *)ctsio);
8635 return (CTL_RETVAL_COMPLETE);
8639 switch (ctsio->cdb[0]) {
8642 struct scsi_rw_6 *cdb;
8644 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8646 lba = scsi_3btoul(cdb->addr);
8647 /* only 5 bits are valid in the most significant address byte */
8649 num_blocks = cdb->length;
8651 * This is correct according to SBC-2.
8653 if (num_blocks == 0)
8659 struct scsi_rw_10 *cdb;
8661 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8663 if (cdb->byte2 & SRW10_FUA)
8665 if (cdb->byte2 & SRW10_DPO)
8668 lba = scsi_4btoul(cdb->addr);
8669 num_blocks = scsi_2btoul(cdb->length);
8672 case WRITE_VERIFY_10: {
8673 struct scsi_write_verify_10 *cdb;
8675 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8678 * XXX KDM we should do actual write verify support at some
8679 * point. This is obviously fake, we're just translating
8680 * things to a write. So we don't even bother checking the
8681 * BYTCHK field, since we don't do any verification. If
8682 * the user asks for it, we'll just pretend we did it.
8684 if (cdb->byte2 & SWV_DPO)
8687 lba = scsi_4btoul(cdb->addr);
8688 num_blocks = scsi_2btoul(cdb->length);
8693 struct scsi_rw_12 *cdb;
8695 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8697 if (cdb->byte2 & SRW12_FUA)
8699 if (cdb->byte2 & SRW12_DPO)
8701 lba = scsi_4btoul(cdb->addr);
8702 num_blocks = scsi_4btoul(cdb->length);
8705 case WRITE_VERIFY_12: {
8706 struct scsi_write_verify_12 *cdb;
8708 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8710 if (cdb->byte2 & SWV_DPO)
8713 lba = scsi_4btoul(cdb->addr);
8714 num_blocks = scsi_4btoul(cdb->length);
8720 struct scsi_rw_16 *cdb;
8722 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8724 if (cdb->byte2 & SRW12_FUA)
8726 if (cdb->byte2 & SRW12_DPO)
8729 lba = scsi_8btou64(cdb->addr);
8730 num_blocks = scsi_4btoul(cdb->length);
8733 case WRITE_VERIFY_16: {
8734 struct scsi_write_verify_16 *cdb;
8736 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8738 if (cdb->byte2 & SWV_DPO)
8741 lba = scsi_8btou64(cdb->addr);
8742 num_blocks = scsi_4btoul(cdb->length);
8747 * We got a command we don't support. This shouldn't
8748 * happen, commands should be filtered out above us.
8750 ctl_set_invalid_opcode(ctsio);
8751 ctl_done((union ctl_io *)ctsio);
8753 return (CTL_RETVAL_COMPLETE);
8754 break; /* NOTREACHED */
8758 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8759 * interesting for us, but if RAIDCore is in write-back mode,
8760 * getting it to do write-through for a particular transaction may
8765 * The first check is to make sure we're in bounds, the second
8766 * check is to catch wrap-around problems. If the lba + num blocks
8767 * is less than the lba, then we've wrapped around and the block
8768 * range is invalid anyway.
8770 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8771 || ((lba + num_blocks) < lba)) {
8772 ctl_set_lba_out_of_range(ctsio);
8773 ctl_done((union ctl_io *)ctsio);
8774 return (CTL_RETVAL_COMPLETE);
8778 * According to SBC-3, a transfer length of 0 is not an error.
8779 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8780 * translates to 256 blocks for those commands.
8782 if (num_blocks == 0) {
8783 ctl_set_success(ctsio);
8784 ctl_done((union ctl_io *)ctsio);
8785 return (CTL_RETVAL_COMPLETE);
8788 lbalen = (struct ctl_lba_len_flags *)
8789 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8791 lbalen->len = num_blocks;
8792 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8794 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8795 ctsio->kern_rel_offset = 0;
8797 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8799 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8805 ctl_cnw_cont(union ctl_io *io)
8807 struct ctl_scsiio *ctsio;
8808 struct ctl_lun *lun;
8809 struct ctl_lba_len_flags *lbalen;
8812 ctsio = &io->scsiio;
8813 ctsio->io_hdr.status = CTL_STATUS_NONE;
8814 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8816 lbalen = (struct ctl_lba_len_flags *)
8817 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8818 lbalen->flags = CTL_LLF_WRITE;
8820 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8821 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8826 ctl_cnw(struct ctl_scsiio *ctsio)
8828 struct ctl_lun *lun;
8829 struct ctl_lba_len_flags *lbalen;
8831 uint32_t num_blocks;
8835 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8837 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8842 retval = CTL_RETVAL_COMPLETE;
8844 switch (ctsio->cdb[0]) {
8845 case COMPARE_AND_WRITE: {
8846 struct scsi_compare_and_write *cdb;
8848 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8850 if (cdb->byte2 & SRW10_FUA)
8852 if (cdb->byte2 & SRW10_DPO)
8854 lba = scsi_8btou64(cdb->addr);
8855 num_blocks = cdb->length;
8860 * We got a command we don't support. This shouldn't
8861 * happen, commands should be filtered out above us.
8863 ctl_set_invalid_opcode(ctsio);
8864 ctl_done((union ctl_io *)ctsio);
8866 return (CTL_RETVAL_COMPLETE);
8867 break; /* NOTREACHED */
8871 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8872 * interesting for us, but if RAIDCore is in write-back mode,
8873 * getting it to do write-through for a particular transaction may
8878 * The first check is to make sure we're in bounds, the second
8879 * check is to catch wrap-around problems. If the lba + num blocks
8880 * is less than the lba, then we've wrapped around and the block
8881 * range is invalid anyway.
8883 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8884 || ((lba + num_blocks) < lba)) {
8885 ctl_set_lba_out_of_range(ctsio);
8886 ctl_done((union ctl_io *)ctsio);
8887 return (CTL_RETVAL_COMPLETE);
8891 * According to SBC-3, a transfer length of 0 is not an error.
8893 if (num_blocks == 0) {
8894 ctl_set_success(ctsio);
8895 ctl_done((union ctl_io *)ctsio);
8896 return (CTL_RETVAL_COMPLETE);
8899 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8900 ctsio->kern_rel_offset = 0;
8903 * Set the IO_CONT flag, so that if this I/O gets passed to
8904 * ctl_data_submit_done(), it'll get passed back to
8905 * ctl_ctl_cnw_cont() for further processing.
8907 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8908 ctsio->io_cont = ctl_cnw_cont;
8910 lbalen = (struct ctl_lba_len_flags *)
8911 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8913 lbalen->len = num_blocks;
8914 lbalen->flags = CTL_LLF_COMPARE;
8916 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8917 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8922 ctl_verify(struct ctl_scsiio *ctsio)
8924 struct ctl_lun *lun;
8925 struct ctl_lba_len_flags *lbalen;
8927 uint32_t num_blocks;
8931 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8933 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8937 retval = CTL_RETVAL_COMPLETE;
8939 switch (ctsio->cdb[0]) {
8941 struct scsi_verify_10 *cdb;
8943 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8944 if (cdb->byte2 & SVFY_BYTCHK)
8946 if (cdb->byte2 & SVFY_DPO)
8948 lba = scsi_4btoul(cdb->addr);
8949 num_blocks = scsi_2btoul(cdb->length);
8953 struct scsi_verify_12 *cdb;
8955 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8956 if (cdb->byte2 & SVFY_BYTCHK)
8958 if (cdb->byte2 & SVFY_DPO)
8960 lba = scsi_4btoul(cdb->addr);
8961 num_blocks = scsi_4btoul(cdb->length);
8965 struct scsi_rw_16 *cdb;
8967 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8968 if (cdb->byte2 & SVFY_BYTCHK)
8970 if (cdb->byte2 & SVFY_DPO)
8972 lba = scsi_8btou64(cdb->addr);
8973 num_blocks = scsi_4btoul(cdb->length);
8978 * We got a command we don't support. This shouldn't
8979 * happen, commands should be filtered out above us.
8981 ctl_set_invalid_opcode(ctsio);
8982 ctl_done((union ctl_io *)ctsio);
8983 return (CTL_RETVAL_COMPLETE);
8987 * The first check is to make sure we're in bounds, the second
8988 * check is to catch wrap-around problems. If the lba + num blocks
8989 * is less than the lba, then we've wrapped around and the block
8990 * range is invalid anyway.
8992 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8993 || ((lba + num_blocks) < lba)) {
8994 ctl_set_lba_out_of_range(ctsio);
8995 ctl_done((union ctl_io *)ctsio);
8996 return (CTL_RETVAL_COMPLETE);
9000 * According to SBC-3, a transfer length of 0 is not an error.
9002 if (num_blocks == 0) {
9003 ctl_set_success(ctsio);
9004 ctl_done((union ctl_io *)ctsio);
9005 return (CTL_RETVAL_COMPLETE);
9008 lbalen = (struct ctl_lba_len_flags *)
9009 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9011 lbalen->len = num_blocks;
9013 lbalen->flags = CTL_LLF_COMPARE;
9014 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9016 lbalen->flags = CTL_LLF_VERIFY;
9017 ctsio->kern_total_len = 0;
9019 ctsio->kern_rel_offset = 0;
9021 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9022 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9027 ctl_report_luns(struct ctl_scsiio *ctsio)
9029 struct scsi_report_luns *cdb;
9030 struct scsi_report_luns_data *lun_data;
9031 struct ctl_lun *lun, *request_lun;
9032 int num_luns, retval;
9033 uint32_t alloc_len, lun_datalen;
9034 int num_filled, well_known;
9035 uint32_t initidx, targ_lun_id, lun_id;
9037 retval = CTL_RETVAL_COMPLETE;
9040 cdb = (struct scsi_report_luns *)ctsio->cdb;
9042 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9044 mtx_lock(&control_softc->ctl_lock);
9045 num_luns = control_softc->num_luns;
9046 mtx_unlock(&control_softc->ctl_lock);
9048 switch (cdb->select_report) {
9049 case RPL_REPORT_DEFAULT:
9050 case RPL_REPORT_ALL:
9052 case RPL_REPORT_WELLKNOWN:
9057 ctl_set_invalid_field(ctsio,
9063 ctl_done((union ctl_io *)ctsio);
9065 break; /* NOTREACHED */
9068 alloc_len = scsi_4btoul(cdb->length);
9070 * The initiator has to allocate at least 16 bytes for this request,
9071 * so he can at least get the header and the first LUN. Otherwise
9072 * we reject the request (per SPC-3 rev 14, section 6.21).
9074 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9075 sizeof(struct scsi_report_luns_lundata))) {
9076 ctl_set_invalid_field(ctsio,
9082 ctl_done((union ctl_io *)ctsio);
9086 request_lun = (struct ctl_lun *)
9087 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9089 lun_datalen = sizeof(*lun_data) +
9090 (num_luns * sizeof(struct scsi_report_luns_lundata));
9092 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9093 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9094 ctsio->kern_sg_entries = 0;
9096 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9098 mtx_lock(&control_softc->ctl_lock);
9099 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9100 lun_id = targ_lun_id;
9101 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9102 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9103 if (lun_id >= CTL_MAX_LUNS)
9105 lun = control_softc->ctl_luns[lun_id];
9109 if (targ_lun_id <= 0xff) {
9111 * Peripheral addressing method, bus number 0.
9113 lun_data->luns[num_filled].lundata[0] =
9114 RPL_LUNDATA_ATYP_PERIPH;
9115 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9117 } else if (targ_lun_id <= 0x3fff) {
9119 * Flat addressing method.
9121 lun_data->luns[num_filled].lundata[0] =
9122 RPL_LUNDATA_ATYP_FLAT |
9123 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9124 #ifdef OLDCTLHEADERS
9125 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9126 (targ_lun_id & SRLD_BUS_LUN_MASK);
9128 lun_data->luns[num_filled].lundata[1] =
9129 #ifdef OLDCTLHEADERS
9130 targ_lun_id >> SRLD_BUS_LUN_BITS;
9132 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9135 printf("ctl_report_luns: bogus LUN number %jd, "
9136 "skipping\n", (intmax_t)targ_lun_id);
9139 * According to SPC-3, rev 14 section 6.21:
9141 * "The execution of a REPORT LUNS command to any valid and
9142 * installed logical unit shall clear the REPORTED LUNS DATA
9143 * HAS CHANGED unit attention condition for all logical
9144 * units of that target with respect to the requesting
9145 * initiator. A valid and installed logical unit is one
9146 * having a PERIPHERAL QUALIFIER of 000b in the standard
9147 * INQUIRY data (see 6.4.2)."
9149 * If request_lun is NULL, the LUN this report luns command
9150 * was issued to is either disabled or doesn't exist. In that
9151 * case, we shouldn't clear any pending lun change unit
9154 if (request_lun != NULL) {
9155 mtx_lock(&lun->lun_lock);
9156 lun->pending_sense[initidx].ua_pending &=
9158 mtx_unlock(&lun->lun_lock);
9161 mtx_unlock(&control_softc->ctl_lock);
9164 * It's quite possible that we've returned fewer LUNs than we allocated
9165 * space for. Trim it.
9167 lun_datalen = sizeof(*lun_data) +
9168 (num_filled * sizeof(struct scsi_report_luns_lundata));
9170 if (lun_datalen < alloc_len) {
9171 ctsio->residual = alloc_len - lun_datalen;
9172 ctsio->kern_data_len = lun_datalen;
9173 ctsio->kern_total_len = lun_datalen;
9175 ctsio->residual = 0;
9176 ctsio->kern_data_len = alloc_len;
9177 ctsio->kern_total_len = alloc_len;
9179 ctsio->kern_data_resid = 0;
9180 ctsio->kern_rel_offset = 0;
9181 ctsio->kern_sg_entries = 0;
9184 * We set this to the actual data length, regardless of how much
9185 * space we actually have to return results. If the user looks at
9186 * this value, he'll know whether or not he allocated enough space
9187 * and reissue the command if necessary. We don't support well
9188 * known logical units, so if the user asks for that, return none.
9190 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9193 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9196 ctsio->scsi_status = SCSI_STATUS_OK;
9198 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9199 ctsio->be_move_done = ctl_config_move_done;
9200 ctl_datamove((union ctl_io *)ctsio);
9206 ctl_request_sense(struct ctl_scsiio *ctsio)
9208 struct scsi_request_sense *cdb;
9209 struct scsi_sense_data *sense_ptr;
9210 struct ctl_lun *lun;
9213 scsi_sense_data_type sense_format;
9215 cdb = (struct scsi_request_sense *)ctsio->cdb;
9217 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9219 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9222 * Determine which sense format the user wants.
9224 if (cdb->byte2 & SRS_DESC)
9225 sense_format = SSD_TYPE_DESC;
9227 sense_format = SSD_TYPE_FIXED;
9229 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9230 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9231 ctsio->kern_sg_entries = 0;
9234 * struct scsi_sense_data, which is currently set to 256 bytes, is
9235 * larger than the largest allowed value for the length field in the
9236 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9238 ctsio->residual = 0;
9239 ctsio->kern_data_len = cdb->length;
9240 ctsio->kern_total_len = cdb->length;
9242 ctsio->kern_data_resid = 0;
9243 ctsio->kern_rel_offset = 0;
9244 ctsio->kern_sg_entries = 0;
9247 * If we don't have a LUN, we don't have any pending sense.
9253 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9255 * Check for pending sense, and then for pending unit attentions.
9256 * Pending sense gets returned first, then pending unit attentions.
9258 mtx_lock(&lun->lun_lock);
9259 if (ctl_is_set(lun->have_ca, initidx)) {
9260 scsi_sense_data_type stored_format;
9263 * Check to see which sense format was used for the stored
9266 stored_format = scsi_sense_type(
9267 &lun->pending_sense[initidx].sense);
9270 * If the user requested a different sense format than the
9271 * one we stored, then we need to convert it to the other
9272 * format. If we're going from descriptor to fixed format
9273 * sense data, we may lose things in translation, depending
9274 * on what options were used.
9276 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9277 * for some reason we'll just copy it out as-is.
9279 if ((stored_format == SSD_TYPE_FIXED)
9280 && (sense_format == SSD_TYPE_DESC))
9281 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9282 &lun->pending_sense[initidx].sense,
9283 (struct scsi_sense_data_desc *)sense_ptr);
9284 else if ((stored_format == SSD_TYPE_DESC)
9285 && (sense_format == SSD_TYPE_FIXED))
9286 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9287 &lun->pending_sense[initidx].sense,
9288 (struct scsi_sense_data_fixed *)sense_ptr);
9290 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9291 ctl_min(sizeof(*sense_ptr),
9292 sizeof(lun->pending_sense[initidx].sense)));
9294 ctl_clear_mask(lun->have_ca, initidx);
9296 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9297 ctl_ua_type ua_type;
9299 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9300 sense_ptr, sense_format);
9301 if (ua_type != CTL_UA_NONE) {
9303 /* We're reporting this UA, so clear it */
9304 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9307 mtx_unlock(&lun->lun_lock);
9310 * We already have a pending error, return it.
9312 if (have_error != 0) {
9314 * We report the SCSI status as OK, since the status of the
9315 * request sense command itself is OK.
9317 ctsio->scsi_status = SCSI_STATUS_OK;
9320 * We report 0 for the sense length, because we aren't doing
9321 * autosense in this case. We're reporting sense as
9324 ctsio->sense_len = 0;
9325 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9326 ctsio->be_move_done = ctl_config_move_done;
9327 ctl_datamove((union ctl_io *)ctsio);
9329 return (CTL_RETVAL_COMPLETE);
9335 * No sense information to report, so we report that everything is
9338 ctl_set_sense_data(sense_ptr,
9341 /*current_error*/ 1,
9342 /*sense_key*/ SSD_KEY_NO_SENSE,
9347 ctsio->scsi_status = SCSI_STATUS_OK;
9350 * We report 0 for the sense length, because we aren't doing
9351 * autosense in this case. We're reporting sense as parameter data.
9353 ctsio->sense_len = 0;
9354 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9355 ctsio->be_move_done = ctl_config_move_done;
9356 ctl_datamove((union ctl_io *)ctsio);
9358 return (CTL_RETVAL_COMPLETE);
9362 ctl_tur(struct ctl_scsiio *ctsio)
9364 struct ctl_lun *lun;
9366 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9368 CTL_DEBUG_PRINT(("ctl_tur\n"));
9373 ctsio->scsi_status = SCSI_STATUS_OK;
9374 ctsio->io_hdr.status = CTL_SUCCESS;
9376 ctl_done((union ctl_io *)ctsio);
9378 return (CTL_RETVAL_COMPLETE);
9383 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9390 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9392 struct scsi_vpd_supported_pages *pages;
9394 struct ctl_lun *lun;
9396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9398 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9399 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9400 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9401 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9402 ctsio->kern_sg_entries = 0;
9404 if (sup_page_size < alloc_len) {
9405 ctsio->residual = alloc_len - sup_page_size;
9406 ctsio->kern_data_len = sup_page_size;
9407 ctsio->kern_total_len = sup_page_size;
9409 ctsio->residual = 0;
9410 ctsio->kern_data_len = alloc_len;
9411 ctsio->kern_total_len = alloc_len;
9413 ctsio->kern_data_resid = 0;
9414 ctsio->kern_rel_offset = 0;
9415 ctsio->kern_sg_entries = 0;
9418 * The control device is always connected. The disk device, on the
9419 * other hand, may not be online all the time. Need to change this
9420 * to figure out whether the disk device is actually online or not.
9423 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9424 lun->be_lun->lun_type;
9426 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9428 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9429 /* Supported VPD pages */
9430 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9432 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9433 /* Device Identification */
9434 pages->page_list[2] = SVPD_DEVICE_ID;
9436 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9437 /* Logical Block Provisioning */
9438 pages->page_list[4] = SVPD_LBP;
9440 ctsio->scsi_status = SCSI_STATUS_OK;
9442 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9443 ctsio->be_move_done = ctl_config_move_done;
9444 ctl_datamove((union ctl_io *)ctsio);
9446 return (CTL_RETVAL_COMPLETE);
9450 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9452 struct scsi_vpd_unit_serial_number *sn_ptr;
9453 struct ctl_lun *lun;
9455 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9457 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9458 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9459 ctsio->kern_sg_entries = 0;
9461 if (sizeof(*sn_ptr) < alloc_len) {
9462 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9463 ctsio->kern_data_len = sizeof(*sn_ptr);
9464 ctsio->kern_total_len = sizeof(*sn_ptr);
9466 ctsio->residual = 0;
9467 ctsio->kern_data_len = alloc_len;
9468 ctsio->kern_total_len = alloc_len;
9470 ctsio->kern_data_resid = 0;
9471 ctsio->kern_rel_offset = 0;
9472 ctsio->kern_sg_entries = 0;
9475 * The control device is always connected. The disk device, on the
9476 * other hand, may not be online all the time. Need to change this
9477 * to figure out whether the disk device is actually online or not.
9480 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9481 lun->be_lun->lun_type;
9483 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9485 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9486 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9488 * If we don't have a LUN, we just leave the serial number as
9491 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9493 strncpy((char *)sn_ptr->serial_num,
9494 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9496 ctsio->scsi_status = SCSI_STATUS_OK;
9498 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9499 ctsio->be_move_done = ctl_config_move_done;
9500 ctl_datamove((union ctl_io *)ctsio);
9502 return (CTL_RETVAL_COMPLETE);
9507 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9509 struct scsi_vpd_device_id *devid_ptr;
9510 struct scsi_vpd_id_descriptor *desc, *desc1;
9511 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9512 struct scsi_vpd_id_t10 *t10id;
9513 struct ctl_softc *ctl_softc;
9514 struct ctl_lun *lun;
9515 struct ctl_frontend *fe;
9517 int data_len, devid_len;
9519 ctl_softc = control_softc;
9521 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9523 if (fe->devid != NULL)
9524 return ((fe->devid)(ctsio, alloc_len));
9526 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9529 devid_len = CTL_DEVID_MIN_LEN;
9531 devid_len = max(CTL_DEVID_MIN_LEN,
9532 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN));
9535 data_len = sizeof(struct scsi_vpd_device_id) +
9536 sizeof(struct scsi_vpd_id_descriptor) +
9537 sizeof(struct scsi_vpd_id_t10) + devid_len +
9538 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9539 sizeof(struct scsi_vpd_id_descriptor) +
9540 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9541 sizeof(struct scsi_vpd_id_descriptor) +
9542 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9544 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9545 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9546 ctsio->kern_sg_entries = 0;
9548 if (data_len < alloc_len) {
9549 ctsio->residual = alloc_len - data_len;
9550 ctsio->kern_data_len = data_len;
9551 ctsio->kern_total_len = data_len;
9553 ctsio->residual = 0;
9554 ctsio->kern_data_len = alloc_len;
9555 ctsio->kern_total_len = alloc_len;
9557 ctsio->kern_data_resid = 0;
9558 ctsio->kern_rel_offset = 0;
9559 ctsio->kern_sg_entries = 0;
9561 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9562 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9563 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9564 sizeof(struct scsi_vpd_id_t10) + devid_len);
9565 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9567 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9568 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9571 * The control device is always connected. The disk device, on the
9572 * other hand, may not be online all the time.
9575 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9576 lun->be_lun->lun_type;
9578 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9580 devid_ptr->page_code = SVPD_DEVICE_ID;
9582 scsi_ulto2b(data_len - 4, devid_ptr->length);
9585 * For Fibre channel,
9587 if (fe->port_type == CTL_PORT_FC)
9589 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9590 SVPD_ID_CODESET_ASCII;
9591 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9592 SVPD_ID_CODESET_BINARY;
9596 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9597 SVPD_ID_CODESET_ASCII;
9598 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9599 SVPD_ID_CODESET_BINARY;
9601 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9604 * We're using a LUN association here. i.e., this device ID is a
9605 * per-LUN identifier.
9607 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9608 desc->length = sizeof(*t10id) + devid_len;
9609 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9610 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9612 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9613 strncpy(t10id->vendor, val,
9614 min(sizeof(t10id->vendor), strlen(val)));
9618 * desc1 is for the WWPN which is a port asscociation.
9620 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9621 desc1->length = CTL_WWPN_LEN;
9622 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9623 /* For testing just create the WWPN */
9625 ddb_GetWWNN((char *)desc1->identifier);
9627 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9628 /* This is so Copancontrol will return something sane */
9629 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9630 ctsio->io_hdr.nexus.targ_port!=8)
9631 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9633 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9636 be64enc(desc1->identifier, fe->wwpn);
9639 * desc2 is for the Relative Target Port(type 4h) identifier
9641 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9642 | SVPD_ID_TYPE_RELTARG;
9645 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9646 /* This is so Copancontrol will return something sane */
9647 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9648 ctsio->io_hdr.nexus.targ_port!=8)
9649 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9651 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9655 * desc3 is for the Target Port Group(type 5h) identifier
9657 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9658 | SVPD_ID_TYPE_TPORTGRP;
9660 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9661 desc3->identifier[3] = 1;
9663 desc3->identifier[3] = 2;
9666 * If we've actually got a backend, copy the device id from the
9667 * per-LUN data. Otherwise, set it to all spaces.
9671 * Copy the backend's LUN ID.
9673 strncpy((char *)t10id->vendor_spec_id,
9674 (char *)lun->be_lun->device_id, devid_len);
9677 * No backend, set this to spaces.
9679 memset(t10id->vendor_spec_id, 0x20, devid_len);
9682 ctsio->scsi_status = SCSI_STATUS_OK;
9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9685 ctsio->be_move_done = ctl_config_move_done;
9686 ctl_datamove((union ctl_io *)ctsio);
9688 return (CTL_RETVAL_COMPLETE);
9692 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9694 struct scsi_vpd_block_limits *bl_ptr;
9695 struct ctl_lun *lun;
9698 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9699 bs = lun->be_lun->blocksize;
9701 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9702 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9703 ctsio->kern_sg_entries = 0;
9705 if (sizeof(*bl_ptr) < alloc_len) {
9706 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9707 ctsio->kern_data_len = sizeof(*bl_ptr);
9708 ctsio->kern_total_len = sizeof(*bl_ptr);
9710 ctsio->residual = 0;
9711 ctsio->kern_data_len = alloc_len;
9712 ctsio->kern_total_len = alloc_len;
9714 ctsio->kern_data_resid = 0;
9715 ctsio->kern_rel_offset = 0;
9716 ctsio->kern_sg_entries = 0;
9719 * The control device is always connected. The disk device, on the
9720 * other hand, may not be online all the time. Need to change this
9721 * to figure out whether the disk device is actually online or not.
9724 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9725 lun->be_lun->lun_type;
9727 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9729 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9730 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9731 bl_ptr->max_cmp_write_len = 0xff;
9732 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9733 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9734 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9735 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9736 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9738 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9740 ctsio->scsi_status = SCSI_STATUS_OK;
9741 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9742 ctsio->be_move_done = ctl_config_move_done;
9743 ctl_datamove((union ctl_io *)ctsio);
9745 return (CTL_RETVAL_COMPLETE);
9749 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9751 struct scsi_vpd_logical_block_prov *lbp_ptr;
9752 struct ctl_lun *lun;
9755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9756 bs = lun->be_lun->blocksize;
9758 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9759 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9760 ctsio->kern_sg_entries = 0;
9762 if (sizeof(*lbp_ptr) < alloc_len) {
9763 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9764 ctsio->kern_data_len = sizeof(*lbp_ptr);
9765 ctsio->kern_total_len = sizeof(*lbp_ptr);
9767 ctsio->residual = 0;
9768 ctsio->kern_data_len = alloc_len;
9769 ctsio->kern_total_len = alloc_len;
9771 ctsio->kern_data_resid = 0;
9772 ctsio->kern_rel_offset = 0;
9773 ctsio->kern_sg_entries = 0;
9776 * The control device is always connected. The disk device, on the
9777 * other hand, may not be online all the time. Need to change this
9778 * to figure out whether the disk device is actually online or not.
9781 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9782 lun->be_lun->lun_type;
9784 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9786 lbp_ptr->page_code = SVPD_LBP;
9787 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9788 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9790 ctsio->scsi_status = SCSI_STATUS_OK;
9791 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9792 ctsio->be_move_done = ctl_config_move_done;
9793 ctl_datamove((union ctl_io *)ctsio);
9795 return (CTL_RETVAL_COMPLETE);
9799 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9801 struct scsi_inquiry *cdb;
9802 struct ctl_lun *lun;
9803 int alloc_len, retval;
9805 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9806 cdb = (struct scsi_inquiry *)ctsio->cdb;
9808 retval = CTL_RETVAL_COMPLETE;
9810 alloc_len = scsi_2btoul(cdb->length);
9812 switch (cdb->page_code) {
9813 case SVPD_SUPPORTED_PAGES:
9814 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9816 case SVPD_UNIT_SERIAL_NUMBER:
9817 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9819 case SVPD_DEVICE_ID:
9820 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9822 case SVPD_BLOCK_LIMITS:
9823 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9826 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9829 ctl_set_invalid_field(ctsio,
9835 ctl_done((union ctl_io *)ctsio);
9836 retval = CTL_RETVAL_COMPLETE;
9844 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9846 struct scsi_inquiry_data *inq_ptr;
9847 struct scsi_inquiry *cdb;
9848 struct ctl_softc *ctl_softc;
9849 struct ctl_lun *lun;
9854 ctl_softc = control_softc;
9857 * Figure out whether we're talking to a Fibre Channel port or not.
9858 * We treat the ioctl front end, and any SCSI adapters, as packetized
9861 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9867 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9868 cdb = (struct scsi_inquiry *)ctsio->cdb;
9869 alloc_len = scsi_2btoul(cdb->length);
9872 * We malloc the full inquiry data size here and fill it
9873 * in. If the user only asks for less, we'll give him
9876 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9877 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9878 ctsio->kern_sg_entries = 0;
9879 ctsio->kern_data_resid = 0;
9880 ctsio->kern_rel_offset = 0;
9882 if (sizeof(*inq_ptr) < alloc_len) {
9883 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9884 ctsio->kern_data_len = sizeof(*inq_ptr);
9885 ctsio->kern_total_len = sizeof(*inq_ptr);
9887 ctsio->residual = 0;
9888 ctsio->kern_data_len = alloc_len;
9889 ctsio->kern_total_len = alloc_len;
9893 * If we have a LUN configured, report it as connected. Otherwise,
9894 * report that it is offline or no device is supported, depending
9895 * on the value of inquiry_pq_no_lun.
9897 * According to the spec (SPC-4 r34), the peripheral qualifier
9898 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9900 * "A peripheral device having the specified peripheral device type
9901 * is not connected to this logical unit. However, the device
9902 * server is capable of supporting the specified peripheral device
9903 * type on this logical unit."
9905 * According to the same spec, the peripheral qualifier
9906 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9908 * "The device server is not capable of supporting a peripheral
9909 * device on this logical unit. For this peripheral qualifier the
9910 * peripheral device type shall be set to 1Fh. All other peripheral
9911 * device type values are reserved for this peripheral qualifier."
9913 * Given the text, it would seem that we probably want to report that
9914 * the LUN is offline here. There is no LUN connected, but we can
9915 * support a LUN at the given LUN number.
9917 * In the real world, though, it sounds like things are a little
9920 * - Linux, when presented with a LUN with the offline peripheral
9921 * qualifier, will create an sg driver instance for it. So when
9922 * you attach it to CTL, you wind up with a ton of sg driver
9923 * instances. (One for every LUN that Linux bothered to probe.)
9924 * Linux does this despite the fact that it issues a REPORT LUNs
9925 * to LUN 0 to get the inventory of supported LUNs.
9927 * - There is other anecdotal evidence (from Emulex folks) about
9928 * arrays that use the offline peripheral qualifier for LUNs that
9929 * are on the "passive" path in an active/passive array.
9931 * So the solution is provide a hopefully reasonable default
9932 * (return bad/no LUN) and allow the user to change the behavior
9933 * with a tunable/sysctl variable.
9936 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9937 lun->be_lun->lun_type;
9938 else if (ctl_softc->inquiry_pq_no_lun == 0)
9939 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9941 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9943 /* RMB in byte 2 is 0 */
9944 inq_ptr->version = SCSI_REV_SPC3;
9947 * According to SAM-3, even if a device only supports a single
9948 * level of LUN addressing, it should still set the HISUP bit:
9950 * 4.9.1 Logical unit numbers overview
9952 * All logical unit number formats described in this standard are
9953 * hierarchical in structure even when only a single level in that
9954 * hierarchy is used. The HISUP bit shall be set to one in the
9955 * standard INQUIRY data (see SPC-2) when any logical unit number
9956 * format described in this standard is used. Non-hierarchical
9957 * formats are outside the scope of this standard.
9959 * Therefore we set the HiSup bit here.
9961 * The reponse format is 2, per SPC-3.
9963 inq_ptr->response_format = SID_HiSup | 2;
9965 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9966 CTL_DEBUG_PRINT(("additional_length = %d\n",
9967 inq_ptr->additional_length));
9969 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9970 /* 16 bit addressing */
9972 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9973 /* XXX set the SID_MultiP bit here if we're actually going to
9974 respond on multiple ports */
9975 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9977 /* 16 bit data bus, synchronous transfers */
9978 /* XXX these flags don't apply for FC */
9980 inq_ptr->flags = SID_WBus16 | SID_Sync;
9982 * XXX KDM do we want to support tagged queueing on the control
9986 || (lun->be_lun->lun_type != T_PROCESSOR))
9987 inq_ptr->flags |= SID_CmdQue;
9989 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9990 * We have 8 bytes for the vendor name, and 16 bytes for the device
9991 * name and 4 bytes for the revision.
9993 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
9994 strcpy(inq_ptr->vendor, CTL_VENDOR);
9996 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9997 strncpy(inq_ptr->vendor, val,
9998 min(sizeof(inq_ptr->vendor), strlen(val)));
10001 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10002 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) {
10003 switch (lun->be_lun->lun_type) {
10005 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10008 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10011 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10015 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10016 strncpy(inq_ptr->product, val,
10017 min(sizeof(inq_ptr->product), strlen(val)));
10021 * XXX make this a macro somewhere so it automatically gets
10022 * incremented when we make changes.
10024 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) {
10025 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10027 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10028 strncpy(inq_ptr->revision, val,
10029 min(sizeof(inq_ptr->revision), strlen(val)));
10033 * For parallel SCSI, we support double transition and single
10034 * transition clocking. We also support QAS (Quick Arbitration
10035 * and Selection) and Information Unit transfers on both the
10036 * control and array devices.
10039 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10043 scsi_ulto2b(0x0060, inq_ptr->version1);
10044 /* SPC-3 (no version claimed) XXX should we claim a version? */
10045 scsi_ulto2b(0x0300, inq_ptr->version2);
10047 /* FCP-2 ANSI INCITS.350:2003 */
10048 scsi_ulto2b(0x0917, inq_ptr->version3);
10050 /* SPI-4 ANSI INCITS.362:200x */
10051 scsi_ulto2b(0x0B56, inq_ptr->version3);
10055 /* SBC-2 (no version claimed) XXX should we claim a version? */
10056 scsi_ulto2b(0x0320, inq_ptr->version4);
10058 switch (lun->be_lun->lun_type) {
10061 * SBC-2 (no version claimed) XXX should we claim a
10064 scsi_ulto2b(0x0320, inq_ptr->version4);
10072 ctsio->scsi_status = SCSI_STATUS_OK;
10073 if (ctsio->kern_data_len > 0) {
10074 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10075 ctsio->be_move_done = ctl_config_move_done;
10076 ctl_datamove((union ctl_io *)ctsio);
10078 ctsio->io_hdr.status = CTL_SUCCESS;
10079 ctl_done((union ctl_io *)ctsio);
10082 return (CTL_RETVAL_COMPLETE);
10086 ctl_inquiry(struct ctl_scsiio *ctsio)
10088 struct scsi_inquiry *cdb;
10091 cdb = (struct scsi_inquiry *)ctsio->cdb;
10095 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10098 * Right now, we don't support the CmdDt inquiry information.
10099 * This would be nice to support in the future. When we do
10100 * support it, we should change this test so that it checks to make
10101 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10104 if (((cdb->byte2 & SI_EVPD)
10105 && (cdb->byte2 & SI_CMDDT)))
10107 if (cdb->byte2 & SI_CMDDT) {
10109 * Point to the SI_CMDDT bit. We might change this
10110 * when we support SI_CMDDT, but since both bits would be
10111 * "wrong", this should probably just stay as-is then.
10113 ctl_set_invalid_field(ctsio,
10119 ctl_done((union ctl_io *)ctsio);
10120 return (CTL_RETVAL_COMPLETE);
10122 if (cdb->byte2 & SI_EVPD)
10123 retval = ctl_inquiry_evpd(ctsio);
10125 else if (cdb->byte2 & SI_CMDDT)
10126 retval = ctl_inquiry_cmddt(ctsio);
10129 retval = ctl_inquiry_std(ctsio);
10135 * For known CDB types, parse the LBA and length.
10138 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10140 if (io->io_hdr.io_type != CTL_IO_SCSI)
10143 switch (io->scsiio.cdb[0]) {
10144 case COMPARE_AND_WRITE: {
10145 struct scsi_compare_and_write *cdb;
10147 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10149 *lba = scsi_8btou64(cdb->addr);
10150 *len = cdb->length;
10155 struct scsi_rw_6 *cdb;
10157 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10159 *lba = scsi_3btoul(cdb->addr);
10160 /* only 5 bits are valid in the most significant address byte */
10162 *len = cdb->length;
10167 struct scsi_rw_10 *cdb;
10169 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10171 *lba = scsi_4btoul(cdb->addr);
10172 *len = scsi_2btoul(cdb->length);
10175 case WRITE_VERIFY_10: {
10176 struct scsi_write_verify_10 *cdb;
10178 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10180 *lba = scsi_4btoul(cdb->addr);
10181 *len = scsi_2btoul(cdb->length);
10186 struct scsi_rw_12 *cdb;
10188 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10190 *lba = scsi_4btoul(cdb->addr);
10191 *len = scsi_4btoul(cdb->length);
10194 case WRITE_VERIFY_12: {
10195 struct scsi_write_verify_12 *cdb;
10197 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10199 *lba = scsi_4btoul(cdb->addr);
10200 *len = scsi_4btoul(cdb->length);
10205 struct scsi_rw_16 *cdb;
10207 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10209 *lba = scsi_8btou64(cdb->addr);
10210 *len = scsi_4btoul(cdb->length);
10213 case WRITE_VERIFY_16: {
10214 struct scsi_write_verify_16 *cdb;
10216 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10219 *lba = scsi_8btou64(cdb->addr);
10220 *len = scsi_4btoul(cdb->length);
10223 case WRITE_SAME_10: {
10224 struct scsi_write_same_10 *cdb;
10226 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10228 *lba = scsi_4btoul(cdb->addr);
10229 *len = scsi_2btoul(cdb->length);
10232 case WRITE_SAME_16: {
10233 struct scsi_write_same_16 *cdb;
10235 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10237 *lba = scsi_8btou64(cdb->addr);
10238 *len = scsi_4btoul(cdb->length);
10242 struct scsi_verify_10 *cdb;
10244 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10246 *lba = scsi_4btoul(cdb->addr);
10247 *len = scsi_2btoul(cdb->length);
10251 struct scsi_verify_12 *cdb;
10253 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10255 *lba = scsi_4btoul(cdb->addr);
10256 *len = scsi_4btoul(cdb->length);
10260 struct scsi_verify_16 *cdb;
10262 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10264 *lba = scsi_8btou64(cdb->addr);
10265 *len = scsi_4btoul(cdb->length);
10270 break; /* NOTREACHED */
10277 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10279 uint64_t endlba1, endlba2;
10281 endlba1 = lba1 + len1 - 1;
10282 endlba2 = lba2 + len2 - 1;
10284 if ((endlba1 < lba2)
10285 || (endlba2 < lba1))
10286 return (CTL_ACTION_PASS);
10288 return (CTL_ACTION_BLOCK);
10292 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10294 uint64_t lba1, lba2;
10295 uint32_t len1, len2;
10298 retval = ctl_get_lba_len(io1, &lba1, &len1);
10300 return (CTL_ACTION_ERROR);
10302 retval = ctl_get_lba_len(io2, &lba2, &len2);
10304 return (CTL_ACTION_ERROR);
10306 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10310 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10312 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10313 ctl_serialize_action *serialize_row;
10316 * The initiator attempted multiple untagged commands at the same
10317 * time. Can't do that.
10319 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10320 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10321 && ((pending_io->io_hdr.nexus.targ_port ==
10322 ooa_io->io_hdr.nexus.targ_port)
10323 && (pending_io->io_hdr.nexus.initid.id ==
10324 ooa_io->io_hdr.nexus.initid.id))
10325 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10326 return (CTL_ACTION_OVERLAP);
10329 * The initiator attempted to send multiple tagged commands with
10330 * the same ID. (It's fine if different initiators have the same
10333 * Even if all of those conditions are true, we don't kill the I/O
10334 * if the command ahead of us has been aborted. We won't end up
10335 * sending it to the FETD, and it's perfectly legal to resend a
10336 * command with the same tag number as long as the previous
10337 * instance of this tag number has been aborted somehow.
10339 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10340 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10341 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10342 && ((pending_io->io_hdr.nexus.targ_port ==
10343 ooa_io->io_hdr.nexus.targ_port)
10344 && (pending_io->io_hdr.nexus.initid.id ==
10345 ooa_io->io_hdr.nexus.initid.id))
10346 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10347 return (CTL_ACTION_OVERLAP_TAG);
10350 * If we get a head of queue tag, SAM-3 says that we should
10351 * immediately execute it.
10353 * What happens if this command would normally block for some other
10354 * reason? e.g. a request sense with a head of queue tag
10355 * immediately after a write. Normally that would block, but this
10356 * will result in its getting executed immediately...
10358 * We currently return "pass" instead of "skip", so we'll end up
10359 * going through the rest of the queue to check for overlapped tags.
10361 * XXX KDM check for other types of blockage first??
10363 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10364 return (CTL_ACTION_PASS);
10367 * Ordered tags have to block until all items ahead of them
10368 * have completed. If we get called with an ordered tag, we always
10369 * block, if something else is ahead of us in the queue.
10371 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10372 return (CTL_ACTION_BLOCK);
10375 * Simple tags get blocked until all head of queue and ordered tags
10376 * ahead of them have completed. I'm lumping untagged commands in
10377 * with simple tags here. XXX KDM is that the right thing to do?
10379 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10380 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10381 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10382 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10383 return (CTL_ACTION_BLOCK);
10385 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10386 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10388 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10390 switch (serialize_row[pending_entry->seridx]) {
10391 case CTL_SER_BLOCK:
10392 return (CTL_ACTION_BLOCK);
10393 break; /* NOTREACHED */
10394 case CTL_SER_EXTENT:
10395 return (ctl_extent_check(pending_io, ooa_io));
10396 break; /* NOTREACHED */
10398 return (CTL_ACTION_PASS);
10399 break; /* NOTREACHED */
10401 return (CTL_ACTION_SKIP);
10404 panic("invalid serialization value %d",
10405 serialize_row[pending_entry->seridx]);
10406 break; /* NOTREACHED */
10409 return (CTL_ACTION_ERROR);
10413 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10415 * - pending_io is generally either incoming, or on the blocked queue
10416 * - starting I/O is the I/O we want to start the check with.
10419 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10420 union ctl_io *starting_io)
10422 union ctl_io *ooa_io;
10425 mtx_assert(&lun->lun_lock, MA_OWNED);
10428 * Run back along the OOA queue, starting with the current
10429 * blocked I/O and going through every I/O before it on the
10430 * queue. If starting_io is NULL, we'll just end up returning
10433 for (ooa_io = starting_io; ooa_io != NULL;
10434 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10438 * This routine just checks to see whether
10439 * cur_blocked is blocked by ooa_io, which is ahead
10440 * of it in the queue. It doesn't queue/dequeue
10443 action = ctl_check_for_blockage(pending_io, ooa_io);
10445 case CTL_ACTION_BLOCK:
10446 case CTL_ACTION_OVERLAP:
10447 case CTL_ACTION_OVERLAP_TAG:
10448 case CTL_ACTION_SKIP:
10449 case CTL_ACTION_ERROR:
10451 break; /* NOTREACHED */
10452 case CTL_ACTION_PASS:
10455 panic("invalid action %d", action);
10456 break; /* NOTREACHED */
10460 return (CTL_ACTION_PASS);
10465 * - An I/O has just completed, and has been removed from the per-LUN OOA
10466 * queue, so some items on the blocked queue may now be unblocked.
10469 ctl_check_blocked(struct ctl_lun *lun)
10471 union ctl_io *cur_blocked, *next_blocked;
10473 mtx_assert(&lun->lun_lock, MA_OWNED);
10476 * Run forward from the head of the blocked queue, checking each
10477 * entry against the I/Os prior to it on the OOA queue to see if
10478 * there is still any blockage.
10480 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10481 * with our removing a variable on it while it is traversing the
10484 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10485 cur_blocked != NULL; cur_blocked = next_blocked) {
10486 union ctl_io *prev_ooa;
10489 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10492 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10493 ctl_ooaq, ooa_links);
10496 * If cur_blocked happens to be the first item in the OOA
10497 * queue now, prev_ooa will be NULL, and the action
10498 * returned will just be CTL_ACTION_PASS.
10500 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10503 case CTL_ACTION_BLOCK:
10504 /* Nothing to do here, still blocked */
10506 case CTL_ACTION_OVERLAP:
10507 case CTL_ACTION_OVERLAP_TAG:
10509 * This shouldn't happen! In theory we've already
10510 * checked this command for overlap...
10513 case CTL_ACTION_PASS:
10514 case CTL_ACTION_SKIP: {
10515 struct ctl_softc *softc;
10516 const struct ctl_cmd_entry *entry;
10521 * The skip case shouldn't happen, this transaction
10522 * should have never made it onto the blocked queue.
10525 * This I/O is no longer blocked, we can remove it
10526 * from the blocked queue. Since this is a TAILQ
10527 * (doubly linked list), we can do O(1) removals
10528 * from any place on the list.
10530 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10532 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10534 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10536 * Need to send IO back to original side to
10539 union ctl_ha_msg msg_info;
10541 msg_info.hdr.original_sc =
10542 cur_blocked->io_hdr.original_sc;
10543 msg_info.hdr.serializing_sc = cur_blocked;
10544 msg_info.hdr.msg_type = CTL_MSG_R2R;
10545 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10546 &msg_info, sizeof(msg_info), 0)) >
10547 CTL_HA_STATUS_SUCCESS) {
10548 printf("CTL:Check Blocked error from "
10549 "ctl_ha_msg_send %d\n",
10554 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10555 softc = control_softc;
10557 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10560 * Check this I/O for LUN state changes that may
10561 * have happened while this command was blocked.
10562 * The LUN state may have been changed by a command
10563 * ahead of us in the queue, so we need to re-check
10564 * for any states that can be caused by SCSI
10567 if (ctl_scsiio_lun_check(softc, lun, entry,
10568 &cur_blocked->scsiio) == 0) {
10569 cur_blocked->io_hdr.flags |=
10570 CTL_FLAG_IS_WAS_ON_RTR;
10571 ctl_enqueue_rtr(cur_blocked);
10573 ctl_done(cur_blocked);
10578 * This probably shouldn't happen -- we shouldn't
10579 * get CTL_ACTION_ERROR, or anything else.
10585 return (CTL_RETVAL_COMPLETE);
10589 * This routine (with one exception) checks LUN flags that can be set by
10590 * commands ahead of us in the OOA queue. These flags have to be checked
10591 * when a command initially comes in, and when we pull a command off the
10592 * blocked queue and are preparing to execute it. The reason we have to
10593 * check these flags for commands on the blocked queue is that the LUN
10594 * state may have been changed by a command ahead of us while we're on the
10597 * Ordering is somewhat important with these checks, so please pay
10598 * careful attention to the placement of any new checks.
10601 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10602 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10608 mtx_assert(&lun->lun_lock, MA_OWNED);
10611 * If this shelf is a secondary shelf controller, we have to reject
10612 * any media access commands.
10615 /* No longer needed for HA */
10616 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10617 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10618 ctl_set_lun_standby(ctsio);
10625 * Check for a reservation conflict. If this command isn't allowed
10626 * even on reserved LUNs, and if this initiator isn't the one who
10627 * reserved us, reject the command with a reservation conflict.
10629 if ((lun->flags & CTL_LUN_RESERVED)
10630 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10631 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10632 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10633 || (ctsio->io_hdr.nexus.targ_target.id !=
10634 lun->rsv_nexus.targ_target.id)) {
10635 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10636 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10642 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10643 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10646 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10648 * if we aren't registered or it's a res holder type
10649 * reservation and this isn't the res holder then set a
10651 * NOTE: Commands which might be allowed on write exclusive
10652 * type reservations are checked in the particular command
10653 * for a conflict. Read and SSU are the only ones.
10655 if (!lun->per_res[residx].registered
10656 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10657 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10658 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10665 if ((lun->flags & CTL_LUN_OFFLINE)
10666 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10667 ctl_set_lun_not_ready(ctsio);
10673 * If the LUN is stopped, see if this particular command is allowed
10674 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10676 if ((lun->flags & CTL_LUN_STOPPED)
10677 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10678 /* "Logical unit not ready, initializing cmd. required" */
10679 ctl_set_lun_stopped(ctsio);
10684 if ((lun->flags & CTL_LUN_INOPERABLE)
10685 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10686 /* "Medium format corrupted" */
10687 ctl_set_medium_format_corrupted(ctsio);
10698 ctl_failover_io(union ctl_io *io, int have_lock)
10700 ctl_set_busy(&io->scsiio);
10707 struct ctl_lun *lun;
10708 struct ctl_softc *ctl_softc;
10709 union ctl_io *next_io, *pending_io;
10714 ctl_softc = control_softc;
10716 mtx_lock(&ctl_softc->ctl_lock);
10718 * Remove any cmds from the other SC from the rtr queue. These
10719 * will obviously only be for LUNs for which we're the primary.
10720 * We can't send status or get/send data for these commands.
10721 * Since they haven't been executed yet, we can just remove them.
10722 * We'll either abort them or delete them below, depending on
10723 * which HA mode we're in.
10726 mtx_lock(&ctl_softc->queue_lock);
10727 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10728 io != NULL; io = next_io) {
10729 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10730 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10731 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10732 ctl_io_hdr, links);
10734 mtx_unlock(&ctl_softc->queue_lock);
10737 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10738 lun = ctl_softc->ctl_luns[lun_idx];
10743 * Processor LUNs are primary on both sides.
10744 * XXX will this always be true?
10746 if (lun->be_lun->lun_type == T_PROCESSOR)
10749 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10750 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10751 printf("FAILOVER: primary lun %d\n", lun_idx);
10753 * Remove all commands from the other SC. First from the
10754 * blocked queue then from the ooa queue. Once we have
10755 * removed them. Call ctl_check_blocked to see if there
10756 * is anything that can run.
10758 for (io = (union ctl_io *)TAILQ_FIRST(
10759 &lun->blocked_queue); io != NULL; io = next_io) {
10761 next_io = (union ctl_io *)TAILQ_NEXT(
10762 &io->io_hdr, blocked_links);
10764 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10765 TAILQ_REMOVE(&lun->blocked_queue,
10766 &io->io_hdr,blocked_links);
10767 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10768 TAILQ_REMOVE(&lun->ooa_queue,
10769 &io->io_hdr, ooa_links);
10775 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10776 io != NULL; io = next_io) {
10778 next_io = (union ctl_io *)TAILQ_NEXT(
10779 &io->io_hdr, ooa_links);
10781 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10783 TAILQ_REMOVE(&lun->ooa_queue,
10790 ctl_check_blocked(lun);
10791 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10792 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10794 printf("FAILOVER: primary lun %d\n", lun_idx);
10796 * Abort all commands from the other SC. We can't
10797 * send status back for them now. These should get
10798 * cleaned up when they are completed or come out
10799 * for a datamove operation.
10801 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10802 io != NULL; io = next_io) {
10803 next_io = (union ctl_io *)TAILQ_NEXT(
10804 &io->io_hdr, ooa_links);
10806 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10807 io->io_hdr.flags |= CTL_FLAG_ABORT;
10809 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10810 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10812 printf("FAILOVER: secondary lun %d\n", lun_idx);
10814 lun->flags |= CTL_LUN_PRIMARY_SC;
10817 * We send all I/O that was sent to this controller
10818 * and redirected to the other side back with
10819 * busy status, and have the initiator retry it.
10820 * Figuring out how much data has been transferred,
10821 * etc. and picking up where we left off would be
10824 * XXX KDM need to remove I/O from the blocked
10827 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10828 &lun->ooa_queue); pending_io != NULL;
10829 pending_io = next_io) {
10831 next_io = (union ctl_io *)TAILQ_NEXT(
10832 &pending_io->io_hdr, ooa_links);
10834 pending_io->io_hdr.flags &=
10835 ~CTL_FLAG_SENT_2OTHER_SC;
10837 if (pending_io->io_hdr.flags &
10838 CTL_FLAG_IO_ACTIVE) {
10839 pending_io->io_hdr.flags |=
10842 ctl_set_busy(&pending_io->scsiio);
10843 ctl_done(pending_io);
10848 * Build Unit Attention
10850 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10851 lun->pending_sense[i].ua_pending |=
10852 CTL_UA_ASYM_ACC_CHANGE;
10854 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10855 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10856 printf("FAILOVER: secondary lun %d\n", lun_idx);
10858 * if the first io on the OOA is not on the RtR queue
10861 lun->flags |= CTL_LUN_PRIMARY_SC;
10863 pending_io = (union ctl_io *)TAILQ_FIRST(
10865 if (pending_io==NULL) {
10866 printf("Nothing on OOA queue\n");
10870 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10871 if ((pending_io->io_hdr.flags &
10872 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10873 pending_io->io_hdr.flags |=
10874 CTL_FLAG_IS_WAS_ON_RTR;
10875 ctl_enqueue_rtr(pending_io);
10880 printf("Tag 0x%04x is running\n",
10881 pending_io->scsiio.tag_num);
10885 next_io = (union ctl_io *)TAILQ_NEXT(
10886 &pending_io->io_hdr, ooa_links);
10887 for (pending_io=next_io; pending_io != NULL;
10888 pending_io = next_io) {
10889 pending_io->io_hdr.flags &=
10890 ~CTL_FLAG_SENT_2OTHER_SC;
10891 next_io = (union ctl_io *)TAILQ_NEXT(
10892 &pending_io->io_hdr, ooa_links);
10893 if (pending_io->io_hdr.flags &
10894 CTL_FLAG_IS_WAS_ON_RTR) {
10896 printf("Tag 0x%04x is running\n",
10897 pending_io->scsiio.tag_num);
10902 switch (ctl_check_ooa(lun, pending_io,
10903 (union ctl_io *)TAILQ_PREV(
10904 &pending_io->io_hdr, ctl_ooaq,
10907 case CTL_ACTION_BLOCK:
10908 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10909 &pending_io->io_hdr,
10911 pending_io->io_hdr.flags |=
10914 case CTL_ACTION_PASS:
10915 case CTL_ACTION_SKIP:
10916 pending_io->io_hdr.flags |=
10917 CTL_FLAG_IS_WAS_ON_RTR;
10918 ctl_enqueue_rtr(pending_io);
10920 case CTL_ACTION_OVERLAP:
10921 ctl_set_overlapped_cmd(
10922 (struct ctl_scsiio *)pending_io);
10923 ctl_done(pending_io);
10925 case CTL_ACTION_OVERLAP_TAG:
10926 ctl_set_overlapped_tag(
10927 (struct ctl_scsiio *)pending_io,
10928 pending_io->scsiio.tag_num & 0xff);
10929 ctl_done(pending_io);
10931 case CTL_ACTION_ERROR:
10933 ctl_set_internal_failure(
10934 (struct ctl_scsiio *)pending_io,
10937 ctl_done(pending_io);
10943 * Build Unit Attention
10945 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10946 lun->pending_sense[i].ua_pending |=
10947 CTL_UA_ASYM_ACC_CHANGE;
10950 panic("Unhandled HA mode failover, LUN flags = %#x, "
10951 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10955 mtx_unlock(&ctl_softc->ctl_lock);
10959 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10961 struct ctl_lun *lun;
10962 const struct ctl_cmd_entry *entry;
10963 uint32_t initidx, targ_lun;
10970 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
10971 if ((targ_lun < CTL_MAX_LUNS)
10972 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10973 lun = ctl_softc->ctl_luns[targ_lun];
10975 * If the LUN is invalid, pretend that it doesn't exist.
10976 * It will go away as soon as all pending I/O has been
10979 if (lun->flags & CTL_LUN_DISABLED) {
10982 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10983 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10985 if (lun->be_lun->lun_type == T_PROCESSOR) {
10986 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10990 * Every I/O goes into the OOA queue for a
10991 * particular LUN, and stays there until completion.
10993 mtx_lock(&lun->lun_lock);
10994 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
10998 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10999 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11002 /* Get command entry and return error if it is unsuppotyed. */
11003 entry = ctl_validate_command(ctsio);
11004 if (entry == NULL) {
11006 mtx_unlock(&lun->lun_lock);
11010 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11011 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11014 * Check to see whether we can send this command to LUNs that don't
11015 * exist. This should pretty much only be the case for inquiry
11016 * and request sense. Further checks, below, really require having
11017 * a LUN, so we can't really check the command anymore. Just put
11018 * it on the rtr queue.
11021 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11022 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11023 ctl_enqueue_rtr((union ctl_io *)ctsio);
11027 ctl_set_unsupported_lun(ctsio);
11028 ctl_done((union ctl_io *)ctsio);
11029 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11033 * Make sure we support this particular command on this LUN.
11034 * e.g., we don't support writes to the control LUN.
11036 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11037 mtx_unlock(&lun->lun_lock);
11038 ctl_set_invalid_opcode(ctsio);
11039 ctl_done((union ctl_io *)ctsio);
11044 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11047 * If we've got a request sense, it'll clear the contingent
11048 * allegiance condition. Otherwise, if we have a CA condition for
11049 * this initiator, clear it, because it sent down a command other
11050 * than request sense.
11052 if ((ctsio->cdb[0] != REQUEST_SENSE)
11053 && (ctl_is_set(lun->have_ca, initidx)))
11054 ctl_clear_mask(lun->have_ca, initidx);
11057 * If the command has this flag set, it handles its own unit
11058 * attention reporting, we shouldn't do anything. Otherwise we
11059 * check for any pending unit attentions, and send them back to the
11060 * initiator. We only do this when a command initially comes in,
11061 * not when we pull it off the blocked queue.
11063 * According to SAM-3, section 5.3.2, the order that things get
11064 * presented back to the host is basically unit attentions caused
11065 * by some sort of reset event, busy status, reservation conflicts
11066 * or task set full, and finally any other status.
11068 * One issue here is that some of the unit attentions we report
11069 * don't fall into the "reset" category (e.g. "reported luns data
11070 * has changed"). So reporting it here, before the reservation
11071 * check, may be technically wrong. I guess the only thing to do
11072 * would be to check for and report the reset events here, and then
11073 * check for the other unit attention types after we check for a
11074 * reservation conflict.
11076 * XXX KDM need to fix this
11078 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11079 ctl_ua_type ua_type;
11081 ua_type = lun->pending_sense[initidx].ua_pending;
11082 if (ua_type != CTL_UA_NONE) {
11083 scsi_sense_data_type sense_format;
11086 sense_format = (lun->flags &
11087 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11090 sense_format = SSD_TYPE_FIXED;
11092 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11094 if (ua_type != CTL_UA_NONE) {
11095 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11096 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11098 ctsio->sense_len = SSD_FULL_SIZE;
11099 lun->pending_sense[initidx].ua_pending &=
11101 mtx_unlock(&lun->lun_lock);
11102 ctl_done((union ctl_io *)ctsio);
11109 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11110 mtx_unlock(&lun->lun_lock);
11111 ctl_done((union ctl_io *)ctsio);
11116 * XXX CHD this is where we want to send IO to other side if
11117 * this LUN is secondary on this SC. We will need to make a copy
11118 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11119 * the copy we send as FROM_OTHER.
11120 * We also need to stuff the address of the original IO so we can
11121 * find it easily. Something similar will need be done on the other
11122 * side so when we are done we can find the copy.
11124 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11125 union ctl_ha_msg msg_info;
11128 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11130 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11131 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11133 printf("1. ctsio %p\n", ctsio);
11135 msg_info.hdr.serializing_sc = NULL;
11136 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11137 msg_info.scsi.tag_num = ctsio->tag_num;
11138 msg_info.scsi.tag_type = ctsio->tag_type;
11139 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11141 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11143 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11144 (void *)&msg_info, sizeof(msg_info), 0)) >
11145 CTL_HA_STATUS_SUCCESS) {
11146 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11148 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11151 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11156 * XXX KDM this I/O is off the incoming queue, but hasn't
11157 * been inserted on any other queue. We may need to come
11158 * up with a holding queue while we wait for serialization
11159 * so that we have an idea of what we're waiting for from
11162 mtx_unlock(&lun->lun_lock);
11166 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11167 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11168 ctl_ooaq, ooa_links))) {
11169 case CTL_ACTION_BLOCK:
11170 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11171 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11173 mtx_unlock(&lun->lun_lock);
11175 case CTL_ACTION_PASS:
11176 case CTL_ACTION_SKIP:
11177 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11178 mtx_unlock(&lun->lun_lock);
11179 ctl_enqueue_rtr((union ctl_io *)ctsio);
11181 case CTL_ACTION_OVERLAP:
11182 mtx_unlock(&lun->lun_lock);
11183 ctl_set_overlapped_cmd(ctsio);
11184 ctl_done((union ctl_io *)ctsio);
11186 case CTL_ACTION_OVERLAP_TAG:
11187 mtx_unlock(&lun->lun_lock);
11188 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11189 ctl_done((union ctl_io *)ctsio);
11191 case CTL_ACTION_ERROR:
11193 mtx_unlock(&lun->lun_lock);
11194 ctl_set_internal_failure(ctsio,
11196 /*retry_count*/ 0);
11197 ctl_done((union ctl_io *)ctsio);
11203 const struct ctl_cmd_entry *
11204 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11206 const struct ctl_cmd_entry *entry;
11207 int service_action;
11209 entry = &ctl_cmd_table[ctsio->cdb[0]];
11210 if (entry->flags & CTL_CMD_FLAG_SA5) {
11211 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11212 entry = &((const struct ctl_cmd_entry *)
11213 entry->execute)[service_action];
11218 const struct ctl_cmd_entry *
11219 ctl_validate_command(struct ctl_scsiio *ctsio)
11221 const struct ctl_cmd_entry *entry;
11225 entry = ctl_get_cmd_entry(ctsio);
11226 if (entry->execute == NULL) {
11227 ctl_set_invalid_opcode(ctsio);
11228 ctl_done((union ctl_io *)ctsio);
11231 KASSERT(entry->length > 0,
11232 ("Not defined length for command 0x%02x/0x%02x",
11233 ctsio->cdb[0], ctsio->cdb[1]));
11234 for (i = 1; i < entry->length; i++) {
11235 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11238 ctl_set_invalid_field(ctsio,
11243 /*bit*/ fls(diff) - 1);
11244 ctl_done((union ctl_io *)ctsio);
11251 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11254 switch (lun_type) {
11256 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11257 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11261 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11262 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11272 ctl_scsiio(struct ctl_scsiio *ctsio)
11275 const struct ctl_cmd_entry *entry;
11277 retval = CTL_RETVAL_COMPLETE;
11279 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11281 entry = ctl_get_cmd_entry(ctsio);
11284 * If this I/O has been aborted, just send it straight to
11285 * ctl_done() without executing it.
11287 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11288 ctl_done((union ctl_io *)ctsio);
11293 * All the checks should have been handled by ctl_scsiio_precheck().
11294 * We should be clear now to just execute the I/O.
11296 retval = entry->execute(ctsio);
11303 * Since we only implement one target right now, a bus reset simply resets
11304 * our single target.
11307 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11309 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11313 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11314 ctl_ua_type ua_type)
11316 struct ctl_lun *lun;
11319 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11320 union ctl_ha_msg msg_info;
11322 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11323 msg_info.hdr.nexus = io->io_hdr.nexus;
11324 if (ua_type==CTL_UA_TARG_RESET)
11325 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11327 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11328 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11329 msg_info.hdr.original_sc = NULL;
11330 msg_info.hdr.serializing_sc = NULL;
11331 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11332 (void *)&msg_info, sizeof(msg_info), 0)) {
11337 mtx_lock(&ctl_softc->ctl_lock);
11338 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11339 retval += ctl_lun_reset(lun, io, ua_type);
11340 mtx_unlock(&ctl_softc->ctl_lock);
11346 * The LUN should always be set. The I/O is optional, and is used to
11347 * distinguish between I/Os sent by this initiator, and by other
11348 * initiators. We set unit attention for initiators other than this one.
11349 * SAM-3 is vague on this point. It does say that a unit attention should
11350 * be established for other initiators when a LUN is reset (see section
11351 * 5.7.3), but it doesn't specifically say that the unit attention should
11352 * be established for this particular initiator when a LUN is reset. Here
11353 * is the relevant text, from SAM-3 rev 8:
11355 * 5.7.2 When a SCSI initiator port aborts its own tasks
11357 * When a SCSI initiator port causes its own task(s) to be aborted, no
11358 * notification that the task(s) have been aborted shall be returned to
11359 * the SCSI initiator port other than the completion response for the
11360 * command or task management function action that caused the task(s) to
11361 * be aborted and notification(s) associated with related effects of the
11362 * action (e.g., a reset unit attention condition).
11364 * XXX KDM for now, we're setting unit attention for all initiators.
11367 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11371 uint32_t initindex;
11375 mtx_lock(&lun->lun_lock);
11377 * Run through the OOA queue and abort each I/O.
11380 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11382 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11383 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11384 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11388 * This version sets unit attention for every
11391 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11392 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11393 if (initindex == i)
11395 lun->pending_sense[i].ua_pending |= ua_type;
11400 * A reset (any kind, really) clears reservations established with
11401 * RESERVE/RELEASE. It does not clear reservations established
11402 * with PERSISTENT RESERVE OUT, but we don't support that at the
11403 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11404 * reservations made with the RESERVE/RELEASE commands, because
11405 * those commands are obsolete in SPC-3.
11407 lun->flags &= ~CTL_LUN_RESERVED;
11409 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11410 ctl_clear_mask(lun->have_ca, i);
11411 lun->pending_sense[i].ua_pending |= ua_type;
11413 mtx_lock(&lun->lun_lock);
11419 ctl_abort_task(union ctl_io *io)
11422 struct ctl_lun *lun;
11423 struct ctl_softc *ctl_softc;
11426 char printbuf[128];
11431 ctl_softc = control_softc;
11437 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11438 mtx_lock(&ctl_softc->ctl_lock);
11439 if ((targ_lun < CTL_MAX_LUNS)
11440 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11441 lun = ctl_softc->ctl_luns[targ_lun];
11443 mtx_unlock(&ctl_softc->ctl_lock);
11448 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11449 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11452 mtx_lock(&lun->lun_lock);
11453 mtx_unlock(&ctl_softc->ctl_lock);
11455 * Run through the OOA queue and attempt to find the given I/O.
11456 * The target port, initiator ID, tag type and tag number have to
11457 * match the values that we got from the initiator. If we have an
11458 * untagged command to abort, simply abort the first untagged command
11459 * we come to. We only allow one untagged command at a time of course.
11462 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11464 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11465 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11467 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11469 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11470 lun->lun, xio->scsiio.tag_num,
11471 xio->scsiio.tag_type,
11472 (xio->io_hdr.blocked_links.tqe_prev
11473 == NULL) ? "" : " BLOCKED",
11474 (xio->io_hdr.flags &
11475 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11476 (xio->io_hdr.flags &
11477 CTL_FLAG_ABORT) ? " ABORT" : "",
11478 (xio->io_hdr.flags &
11479 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11480 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11482 printf("%s\n", sbuf_data(&sb));
11485 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11486 && (xio->io_hdr.nexus.initid.id ==
11487 io->io_hdr.nexus.initid.id)) {
11489 * If the abort says that the task is untagged, the
11490 * task in the queue must be untagged. Otherwise,
11491 * we just check to see whether the tag numbers
11492 * match. This is because the QLogic firmware
11493 * doesn't pass back the tag type in an abort
11497 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11498 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11499 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11502 * XXX KDM we've got problems with FC, because it
11503 * doesn't send down a tag type with aborts. So we
11504 * can only really go by the tag number...
11505 * This may cause problems with parallel SCSI.
11506 * Need to figure that out!!
11508 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11509 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11511 if ((io->io_hdr.flags &
11512 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11513 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11514 union ctl_ha_msg msg_info;
11516 io->io_hdr.flags |=
11517 CTL_FLAG_SENT_2OTHER_SC;
11518 msg_info.hdr.nexus = io->io_hdr.nexus;
11519 msg_info.task.task_action =
11520 CTL_TASK_ABORT_TASK;
11521 msg_info.task.tag_num =
11522 io->taskio.tag_num;
11523 msg_info.task.tag_type =
11524 io->taskio.tag_type;
11525 msg_info.hdr.msg_type =
11526 CTL_MSG_MANAGE_TASKS;
11527 msg_info.hdr.original_sc = NULL;
11528 msg_info.hdr.serializing_sc = NULL;
11530 printf("Sent Abort to other side\n");
11532 if (CTL_HA_STATUS_SUCCESS !=
11533 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11535 sizeof(msg_info), 0)) {
11539 printf("ctl_abort_task: found I/O to abort\n");
11545 mtx_unlock(&lun->lun_lock);
11551 * This isn't really an error. It's entirely possible for
11552 * the abort and command completion to cross on the wire.
11553 * This is more of an informative/diagnostic error.
11556 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11557 "%d:%d:%d:%d tag %d type %d\n",
11558 io->io_hdr.nexus.initid.id,
11559 io->io_hdr.nexus.targ_port,
11560 io->io_hdr.nexus.targ_target.id,
11561 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11562 io->taskio.tag_type);
11570 * This routine cannot block! It must be callable from an interrupt
11571 * handler as well as from the work thread.
11574 ctl_run_task(union ctl_io *io)
11576 struct ctl_softc *ctl_softc;
11578 const char *task_desc;
11580 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11582 ctl_softc = control_softc;
11585 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11586 ("ctl_run_task: Unextected io_type %d\n",
11587 io->io_hdr.io_type));
11589 task_desc = ctl_scsi_task_string(&io->taskio);
11590 if (task_desc != NULL) {
11592 csevent_log(CSC_CTL | CSC_SHELF_SW |
11594 csevent_LogType_Trace,
11595 csevent_Severity_Information,
11596 csevent_AlertLevel_Green,
11597 csevent_FRU_Firmware,
11598 csevent_FRU_Unknown,
11599 "CTL: received task: %s",task_desc);
11603 csevent_log(CSC_CTL | CSC_SHELF_SW |
11605 csevent_LogType_Trace,
11606 csevent_Severity_Information,
11607 csevent_AlertLevel_Green,
11608 csevent_FRU_Firmware,
11609 csevent_FRU_Unknown,
11610 "CTL: received unknown task "
11612 io->taskio.task_action,
11613 io->taskio.task_action);
11616 switch (io->taskio.task_action) {
11617 case CTL_TASK_ABORT_TASK:
11618 retval = ctl_abort_task(io);
11620 case CTL_TASK_ABORT_TASK_SET:
11622 case CTL_TASK_CLEAR_ACA:
11624 case CTL_TASK_CLEAR_TASK_SET:
11626 case CTL_TASK_LUN_RESET: {
11627 struct ctl_lun *lun;
11631 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11632 mtx_lock(&ctl_softc->ctl_lock);
11633 if ((targ_lun < CTL_MAX_LUNS)
11634 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11635 lun = ctl_softc->ctl_luns[targ_lun];
11637 mtx_unlock(&ctl_softc->ctl_lock);
11642 if (!(io->io_hdr.flags &
11643 CTL_FLAG_FROM_OTHER_SC)) {
11644 union ctl_ha_msg msg_info;
11646 io->io_hdr.flags |=
11647 CTL_FLAG_SENT_2OTHER_SC;
11648 msg_info.hdr.msg_type =
11649 CTL_MSG_MANAGE_TASKS;
11650 msg_info.hdr.nexus = io->io_hdr.nexus;
11651 msg_info.task.task_action =
11652 CTL_TASK_LUN_RESET;
11653 msg_info.hdr.original_sc = NULL;
11654 msg_info.hdr.serializing_sc = NULL;
11655 if (CTL_HA_STATUS_SUCCESS !=
11656 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11658 sizeof(msg_info), 0)) {
11662 retval = ctl_lun_reset(lun, io,
11664 mtx_unlock(&ctl_softc->ctl_lock);
11667 case CTL_TASK_TARGET_RESET:
11668 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
11670 case CTL_TASK_BUS_RESET:
11671 retval = ctl_bus_reset(ctl_softc, io);
11673 case CTL_TASK_PORT_LOGIN:
11675 case CTL_TASK_PORT_LOGOUT:
11678 printf("ctl_run_task: got unknown task management event %d\n",
11679 io->taskio.task_action);
11683 io->io_hdr.status = CTL_SUCCESS;
11685 io->io_hdr.status = CTL_ERROR;
11688 * This will queue this I/O to the done queue, but the
11689 * work thread won't be able to process it until we
11690 * return and the lock is released.
11696 * For HA operation. Handle commands that come in from the other
11700 ctl_handle_isc(union ctl_io *io)
11703 struct ctl_lun *lun;
11704 struct ctl_softc *ctl_softc;
11707 ctl_softc = control_softc;
11709 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11710 lun = ctl_softc->ctl_luns[targ_lun];
11712 switch (io->io_hdr.msg_type) {
11713 case CTL_MSG_SERIALIZE:
11714 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11716 case CTL_MSG_R2R: {
11717 const struct ctl_cmd_entry *entry;
11720 * This is only used in SER_ONLY mode.
11723 entry = ctl_get_cmd_entry(&io->scsiio);
11724 mtx_lock(&lun->lun_lock);
11725 if (ctl_scsiio_lun_check(ctl_softc, lun,
11726 entry, (struct ctl_scsiio *)io) != 0) {
11727 mtx_unlock(&lun->lun_lock);
11731 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11732 mtx_unlock(&lun->lun_lock);
11733 ctl_enqueue_rtr(io);
11736 case CTL_MSG_FINISH_IO:
11737 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11742 mtx_lock(&lun->lun_lock);
11743 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11745 ctl_check_blocked(lun);
11746 mtx_unlock(&lun->lun_lock);
11749 case CTL_MSG_PERS_ACTION:
11750 ctl_hndl_per_res_out_on_other_sc(
11751 (union ctl_ha_msg *)&io->presio.pr_msg);
11754 case CTL_MSG_BAD_JUJU:
11758 case CTL_MSG_DATAMOVE:
11759 /* Only used in XFER mode */
11761 ctl_datamove_remote(io);
11763 case CTL_MSG_DATAMOVE_DONE:
11764 /* Only used in XFER mode */
11766 io->scsiio.be_move_done(io);
11770 printf("%s: Invalid message type %d\n",
11771 __func__, io->io_hdr.msg_type);
11781 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11782 * there is no match.
11784 static ctl_lun_error_pattern
11785 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11787 const struct ctl_cmd_entry *entry;
11788 ctl_lun_error_pattern filtered_pattern, pattern;
11790 pattern = desc->error_pattern;
11793 * XXX KDM we need more data passed into this function to match a
11794 * custom pattern, and we actually need to implement custom pattern
11797 if (pattern & CTL_LUN_PAT_CMD)
11798 return (CTL_LUN_PAT_CMD);
11800 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11801 return (CTL_LUN_PAT_ANY);
11803 entry = ctl_get_cmd_entry(ctsio);
11805 filtered_pattern = entry->pattern & pattern;
11808 * If the user requested specific flags in the pattern (e.g.
11809 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11812 * If the user did not specify any flags, it doesn't matter whether
11813 * or not the command supports the flags.
11815 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11816 (pattern & ~CTL_LUN_PAT_MASK))
11817 return (CTL_LUN_PAT_NONE);
11820 * If the user asked for a range check, see if the requested LBA
11821 * range overlaps with this command's LBA range.
11823 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11829 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11831 return (CTL_LUN_PAT_NONE);
11833 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11834 desc->lba_range.len);
11836 * A "pass" means that the LBA ranges don't overlap, so
11837 * this doesn't match the user's range criteria.
11839 if (action == CTL_ACTION_PASS)
11840 return (CTL_LUN_PAT_NONE);
11843 return (filtered_pattern);
11847 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11849 struct ctl_error_desc *desc, *desc2;
11851 mtx_assert(&lun->lun_lock, MA_OWNED);
11853 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11854 ctl_lun_error_pattern pattern;
11856 * Check to see whether this particular command matches
11857 * the pattern in the descriptor.
11859 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11860 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11863 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11864 case CTL_LUN_INJ_ABORTED:
11865 ctl_set_aborted(&io->scsiio);
11867 case CTL_LUN_INJ_MEDIUM_ERR:
11868 ctl_set_medium_error(&io->scsiio);
11870 case CTL_LUN_INJ_UA:
11871 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11873 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11875 case CTL_LUN_INJ_CUSTOM:
11877 * We're assuming the user knows what he is doing.
11878 * Just copy the sense information without doing
11881 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11882 ctl_min(sizeof(desc->custom_sense),
11883 sizeof(io->scsiio.sense_data)));
11884 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11885 io->scsiio.sense_len = SSD_FULL_SIZE;
11886 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11888 case CTL_LUN_INJ_NONE:
11891 * If this is an error injection type we don't know
11892 * about, clear the continuous flag (if it is set)
11893 * so it will get deleted below.
11895 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11899 * By default, each error injection action is a one-shot
11901 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11904 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11910 #ifdef CTL_IO_DELAY
11912 ctl_datamove_timer_wakeup(void *arg)
11916 io = (union ctl_io *)arg;
11920 #endif /* CTL_IO_DELAY */
11923 ctl_datamove(union ctl_io *io)
11925 void (*fe_datamove)(union ctl_io *io);
11927 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11929 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11932 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11937 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11938 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11940 sbuf_cat(&sb, path_str);
11941 switch (io->io_hdr.io_type) {
11943 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11944 sbuf_printf(&sb, "\n");
11945 sbuf_cat(&sb, path_str);
11946 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11947 io->scsiio.tag_num, io->scsiio.tag_type);
11950 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11951 "Tag Type: %d\n", io->taskio.task_action,
11952 io->taskio.tag_num, io->taskio.tag_type);
11955 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11956 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11959 sbuf_cat(&sb, path_str);
11960 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11961 (intmax_t)time_uptime - io->io_hdr.start_time);
11963 printf("%s", sbuf_data(&sb));
11965 #endif /* CTL_TIME_IO */
11967 #ifdef CTL_IO_DELAY
11968 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11969 struct ctl_lun *lun;
11971 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11973 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11975 struct ctl_lun *lun;
11977 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11979 && (lun->delay_info.datamove_delay > 0)) {
11980 struct callout *callout;
11982 callout = (struct callout *)&io->io_hdr.timer_bytes;
11983 callout_init(callout, /*mpsafe*/ 1);
11984 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11985 callout_reset(callout,
11986 lun->delay_info.datamove_delay * hz,
11987 ctl_datamove_timer_wakeup, io);
11988 if (lun->delay_info.datamove_type ==
11989 CTL_DELAY_TYPE_ONESHOT)
11990 lun->delay_info.datamove_delay = 0;
11997 * This command has been aborted. Set the port status, so we fail
12000 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12001 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12002 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12003 io->io_hdr.nexus.targ_port,
12004 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12005 io->io_hdr.nexus.targ_lun);
12006 io->io_hdr.status = CTL_CMD_ABORTED;
12007 io->io_hdr.port_status = 31337;
12009 * Note that the backend, in this case, will get the
12010 * callback in its context. In other cases it may get
12011 * called in the frontend's interrupt thread context.
12013 io->scsiio.be_move_done(io);
12018 * If we're in XFER mode and this I/O is from the other shelf
12019 * controller, we need to send the DMA to the other side to
12020 * actually transfer the data to/from the host. In serialize only
12021 * mode the transfer happens below CTL and ctl_datamove() is only
12022 * called on the machine that originally received the I/O.
12024 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12025 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12026 union ctl_ha_msg msg;
12027 uint32_t sg_entries_sent;
12031 memset(&msg, 0, sizeof(msg));
12032 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12033 msg.hdr.original_sc = io->io_hdr.original_sc;
12034 msg.hdr.serializing_sc = io;
12035 msg.hdr.nexus = io->io_hdr.nexus;
12036 msg.dt.flags = io->io_hdr.flags;
12038 * We convert everything into a S/G list here. We can't
12039 * pass by reference, only by value between controllers.
12040 * So we can't pass a pointer to the S/G list, only as many
12041 * S/G entries as we can fit in here. If it's possible for
12042 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12043 * then we need to break this up into multiple transfers.
12045 if (io->scsiio.kern_sg_entries == 0) {
12046 msg.dt.kern_sg_entries = 1;
12048 * If this is in cached memory, flush the cache
12049 * before we send the DMA request to the other
12050 * controller. We want to do this in either the
12051 * read or the write case. The read case is
12052 * straightforward. In the write case, we want to
12053 * make sure nothing is in the local cache that
12054 * could overwrite the DMAed data.
12056 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12058 * XXX KDM use bus_dmamap_sync() here.
12063 * Convert to a physical address if this is a
12066 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12067 msg.dt.sg_list[0].addr =
12068 io->scsiio.kern_data_ptr;
12071 * XXX KDM use busdma here!
12074 msg.dt.sg_list[0].addr = (void *)
12075 vtophys(io->scsiio.kern_data_ptr);
12079 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12082 struct ctl_sg_entry *sgl;
12085 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12086 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12087 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12089 * XXX KDM use bus_dmamap_sync() here.
12094 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12095 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12096 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12097 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12098 msg.dt.sg_sequence = 0;
12101 * Loop until we've sent all of the S/G entries. On the
12102 * other end, we'll recompose these S/G entries into one
12103 * contiguous list before passing it to the
12105 for (sg_entries_sent = 0; sg_entries_sent <
12106 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12107 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12108 sizeof(msg.dt.sg_list[0])),
12109 msg.dt.kern_sg_entries - sg_entries_sent);
12111 if (do_sg_copy != 0) {
12112 struct ctl_sg_entry *sgl;
12115 sgl = (struct ctl_sg_entry *)
12116 io->scsiio.kern_data_ptr;
12118 * If this is in cached memory, flush the cache
12119 * before we send the DMA request to the other
12120 * controller. We want to do this in either
12121 * the * read or the write case. The read
12122 * case is straightforward. In the write
12123 * case, we want to make sure nothing is
12124 * in the local cache that could overwrite
12128 for (i = sg_entries_sent, j = 0;
12129 i < msg.dt.cur_sg_entries; i++, j++) {
12130 if ((io->io_hdr.flags &
12131 CTL_FLAG_NO_DATASYNC) == 0) {
12133 * XXX KDM use bus_dmamap_sync()
12136 if ((io->io_hdr.flags &
12137 CTL_FLAG_BUS_ADDR) == 0) {
12139 * XXX KDM use busdma.
12142 msg.dt.sg_list[j].addr =(void *)
12143 vtophys(sgl[i].addr);
12146 msg.dt.sg_list[j].addr =
12149 msg.dt.sg_list[j].len = sgl[i].len;
12153 sg_entries_sent += msg.dt.cur_sg_entries;
12154 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12155 msg.dt.sg_last = 1;
12157 msg.dt.sg_last = 0;
12160 * XXX KDM drop and reacquire the lock here?
12162 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12163 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12165 * XXX do something here.
12169 msg.dt.sent_sg_entries = sg_entries_sent;
12171 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12172 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12173 ctl_failover_io(io, /*have_lock*/ 0);
12178 * Lookup the fe_datamove() function for this particular
12182 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12189 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12191 union ctl_ha_msg msg;
12194 memset(&msg, 0, sizeof(msg));
12196 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12197 msg.hdr.original_sc = io;
12198 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12199 msg.hdr.nexus = io->io_hdr.nexus;
12200 msg.hdr.status = io->io_hdr.status;
12201 msg.scsi.tag_num = io->scsiio.tag_num;
12202 msg.scsi.tag_type = io->scsiio.tag_type;
12203 msg.scsi.scsi_status = io->scsiio.scsi_status;
12204 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12205 sizeof(io->scsiio.sense_data));
12206 msg.scsi.sense_len = io->scsiio.sense_len;
12207 msg.scsi.sense_residual = io->scsiio.sense_residual;
12208 msg.scsi.fetd_status = io->io_hdr.port_status;
12209 msg.scsi.residual = io->scsiio.residual;
12210 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12212 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12213 ctl_failover_io(io, /*have_lock*/ have_lock);
12217 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12218 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12219 /* XXX do something if this fails */
12225 * The DMA to the remote side is done, now we need to tell the other side
12226 * we're done so it can continue with its data movement.
12229 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12235 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12236 printf("%s: ISC DMA write failed with error %d", __func__,
12238 ctl_set_internal_failure(&io->scsiio,
12240 /*retry_count*/ rq->ret);
12243 ctl_dt_req_free(rq);
12246 * In this case, we had to malloc the memory locally. Free it.
12248 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12250 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12251 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12254 * The data is in local and remote memory, so now we need to send
12255 * status (good or back) back to the other side.
12257 ctl_send_datamove_done(io, /*have_lock*/ 0);
12261 * We've moved the data from the host/controller into local memory. Now we
12262 * need to push it over to the remote controller's memory.
12265 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12271 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12272 ctl_datamove_remote_write_cb);
12278 ctl_datamove_remote_write(union ctl_io *io)
12281 void (*fe_datamove)(union ctl_io *io);
12284 * - Get the data from the host/HBA into local memory.
12285 * - DMA memory from the local controller to the remote controller.
12286 * - Send status back to the remote controller.
12289 retval = ctl_datamove_remote_sgl_setup(io);
12293 /* Switch the pointer over so the FETD knows what to do */
12294 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12297 * Use a custom move done callback, since we need to send completion
12298 * back to the other controller, not to the backend on this side.
12300 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12302 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12311 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12320 * In this case, we had to malloc the memory locally. Free it.
12322 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12324 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12325 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12329 scsi_path_string(io, path_str, sizeof(path_str));
12330 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12331 sbuf_cat(&sb, path_str);
12332 scsi_command_string(&io->scsiio, NULL, &sb);
12333 sbuf_printf(&sb, "\n");
12334 sbuf_cat(&sb, path_str);
12335 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12336 io->scsiio.tag_num, io->scsiio.tag_type);
12337 sbuf_cat(&sb, path_str);
12338 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12339 io->io_hdr.flags, io->io_hdr.status);
12341 printk("%s", sbuf_data(&sb));
12346 * The read is done, now we need to send status (good or bad) back
12347 * to the other side.
12349 ctl_send_datamove_done(io, /*have_lock*/ 0);
12355 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12358 void (*fe_datamove)(union ctl_io *io);
12362 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12363 printf("%s: ISC DMA read failed with error %d", __func__,
12365 ctl_set_internal_failure(&io->scsiio,
12367 /*retry_count*/ rq->ret);
12370 ctl_dt_req_free(rq);
12372 /* Switch the pointer over so the FETD knows what to do */
12373 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12376 * Use a custom move done callback, since we need to send completion
12377 * back to the other controller, not to the backend on this side.
12379 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12381 /* XXX KDM add checks like the ones in ctl_datamove? */
12383 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12389 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12391 struct ctl_sg_entry *local_sglist, *remote_sglist;
12392 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12393 struct ctl_softc *softc;
12398 softc = control_softc;
12400 local_sglist = io->io_hdr.local_sglist;
12401 local_dma_sglist = io->io_hdr.local_dma_sglist;
12402 remote_sglist = io->io_hdr.remote_sglist;
12403 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12405 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12406 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12407 local_sglist[i].len = remote_sglist[i].len;
12410 * XXX Detect the situation where the RS-level I/O
12411 * redirector on the other side has already read the
12412 * data off of the AOR RS on this side, and
12413 * transferred it to remote (mirror) memory on the
12414 * other side. Since we already have the data in
12415 * memory here, we just need to use it.
12417 * XXX KDM this can probably be removed once we
12418 * get the cache device code in and take the
12419 * current AOR implementation out.
12422 if ((remote_sglist[i].addr >=
12423 (void *)vtophys(softc->mirr->addr))
12424 && (remote_sglist[i].addr <
12425 ((void *)vtophys(softc->mirr->addr) +
12426 CacheMirrorOffset))) {
12427 local_sglist[i].addr = remote_sglist[i].addr -
12429 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12431 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12433 local_sglist[i].addr = remote_sglist[i].addr +
12438 printf("%s: local %p, remote %p, len %d\n",
12439 __func__, local_sglist[i].addr,
12440 remote_sglist[i].addr, local_sglist[i].len);
12444 uint32_t len_to_go;
12447 * In this case, we don't have automatically allocated
12448 * memory for this I/O on this controller. This typically
12449 * happens with internal CTL I/O -- e.g. inquiry, mode
12450 * sense, etc. Anything coming from RAIDCore will have
12451 * a mirror area available.
12453 len_to_go = io->scsiio.kern_data_len;
12456 * Clear the no datasync flag, we have to use malloced
12459 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12462 * The difficult thing here is that the size of the various
12463 * S/G segments may be different than the size from the
12464 * remote controller. That'll make it harder when DMAing
12465 * the data back to the other side.
12467 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12468 sizeof(io->io_hdr.remote_sglist[0])) &&
12469 (len_to_go > 0); i++) {
12470 local_sglist[i].len = ctl_min(len_to_go, 131072);
12471 CTL_SIZE_8B(local_dma_sglist[i].len,
12472 local_sglist[i].len);
12473 local_sglist[i].addr =
12474 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12476 local_dma_sglist[i].addr = local_sglist[i].addr;
12478 if (local_sglist[i].addr == NULL) {
12481 printf("malloc failed for %zd bytes!",
12482 local_dma_sglist[i].len);
12483 for (j = 0; j < i; j++) {
12484 free(local_sglist[j].addr, M_CTL);
12486 ctl_set_internal_failure(&io->scsiio,
12488 /*retry_count*/ 4857);
12490 goto bailout_error;
12493 /* XXX KDM do we need a sync here? */
12495 len_to_go -= local_sglist[i].len;
12498 * Reset the number of S/G entries accordingly. The
12499 * original number of S/G entries is available in
12502 io->scsiio.kern_sg_entries = i;
12505 printf("%s: kern_sg_entries = %d\n", __func__,
12506 io->scsiio.kern_sg_entries);
12507 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12508 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12509 local_sglist[i].addr, local_sglist[i].len,
12510 local_dma_sglist[i].len);
12519 ctl_send_datamove_done(io, /*have_lock*/ 0);
12525 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12526 ctl_ha_dt_cb callback)
12528 struct ctl_ha_dt_req *rq;
12529 struct ctl_sg_entry *remote_sglist, *local_sglist;
12530 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12531 uint32_t local_used, remote_used, total_used;
12537 rq = ctl_dt_req_alloc();
12540 * If we failed to allocate the request, and if the DMA didn't fail
12541 * anyway, set busy status. This is just a resource allocation
12545 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12546 ctl_set_busy(&io->scsiio);
12548 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12551 ctl_dt_req_free(rq);
12554 * The data move failed. We need to return status back
12555 * to the other controller. No point in trying to DMA
12556 * data to the remote controller.
12559 ctl_send_datamove_done(io, /*have_lock*/ 0);
12566 local_sglist = io->io_hdr.local_sglist;
12567 local_dma_sglist = io->io_hdr.local_dma_sglist;
12568 remote_sglist = io->io_hdr.remote_sglist;
12569 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12574 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12575 rq->ret = CTL_HA_STATUS_SUCCESS;
12582 * Pull/push the data over the wire from/to the other controller.
12583 * This takes into account the possibility that the local and
12584 * remote sglists may not be identical in terms of the size of
12585 * the elements and the number of elements.
12587 * One fundamental assumption here is that the length allocated for
12588 * both the local and remote sglists is identical. Otherwise, we've
12589 * essentially got a coding error of some sort.
12591 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12593 uint32_t cur_len, dma_length;
12596 rq->id = CTL_HA_DATA_CTL;
12597 rq->command = command;
12601 * Both pointers should be aligned. But it is possible
12602 * that the allocation length is not. They should both
12603 * also have enough slack left over at the end, though,
12604 * to round up to the next 8 byte boundary.
12606 cur_len = ctl_min(local_sglist[i].len - local_used,
12607 remote_sglist[j].len - remote_used);
12610 * In this case, we have a size issue and need to decrease
12611 * the size, except in the case where we actually have less
12612 * than 8 bytes left. In that case, we need to increase
12613 * the DMA length to get the last bit.
12615 if ((cur_len & 0x7) != 0) {
12616 if (cur_len > 0x7) {
12617 cur_len = cur_len - (cur_len & 0x7);
12618 dma_length = cur_len;
12620 CTL_SIZE_8B(dma_length, cur_len);
12624 dma_length = cur_len;
12627 * If we had to allocate memory for this I/O, instead of using
12628 * the non-cached mirror memory, we'll need to flush the cache
12629 * before trying to DMA to the other controller.
12631 * We could end up doing this multiple times for the same
12632 * segment if we have a larger local segment than remote
12633 * segment. That shouldn't be an issue.
12635 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12637 * XXX KDM use bus_dmamap_sync() here.
12641 rq->size = dma_length;
12643 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12644 tmp_ptr += local_used;
12646 /* Use physical addresses when talking to ISC hardware */
12647 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12648 /* XXX KDM use busdma */
12650 rq->local = vtophys(tmp_ptr);
12653 rq->local = tmp_ptr;
12655 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12656 tmp_ptr += remote_used;
12657 rq->remote = tmp_ptr;
12659 rq->callback = NULL;
12661 local_used += cur_len;
12662 if (local_used >= local_sglist[i].len) {
12667 remote_used += cur_len;
12668 if (remote_used >= remote_sglist[j].len) {
12672 total_used += cur_len;
12674 if (total_used >= io->scsiio.kern_data_len)
12675 rq->callback = callback;
12677 if ((rq->size & 0x7) != 0) {
12678 printf("%s: warning: size %d is not on 8b boundary\n",
12679 __func__, rq->size);
12681 if (((uintptr_t)rq->local & 0x7) != 0) {
12682 printf("%s: warning: local %p not on 8b boundary\n",
12683 __func__, rq->local);
12685 if (((uintptr_t)rq->remote & 0x7) != 0) {
12686 printf("%s: warning: remote %p not on 8b boundary\n",
12687 __func__, rq->local);
12690 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12691 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12692 rq->local, rq->remote, rq->size);
12695 isc_ret = ctl_dt_single(rq);
12696 if (isc_ret == CTL_HA_STATUS_WAIT)
12699 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12700 rq->ret = CTL_HA_STATUS_SUCCESS;
12714 ctl_datamove_remote_read(union ctl_io *io)
12720 * This will send an error to the other controller in the case of a
12723 retval = ctl_datamove_remote_sgl_setup(io);
12727 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12728 ctl_datamove_remote_read_cb);
12730 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12732 * Make sure we free memory if there was an error.. The
12733 * ctl_datamove_remote_xfer() function will send the
12734 * datamove done message, or call the callback with an
12735 * error if there is a problem.
12737 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12738 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12745 * Process a datamove request from the other controller. This is used for
12746 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12747 * first. Once that is complete, the data gets DMAed into the remote
12748 * controller's memory. For reads, we DMA from the remote controller's
12749 * memory into our memory first, and then move it out to the FETD.
12752 ctl_datamove_remote(union ctl_io *io)
12754 struct ctl_softc *softc;
12756 softc = control_softc;
12758 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12761 * Note that we look for an aborted I/O here, but don't do some of
12762 * the other checks that ctl_datamove() normally does. We don't
12763 * need to run the task queue, because this I/O is on the ISC
12764 * queue, which is executed by the work thread after the task queue.
12765 * We don't need to run the datamove delay code, since that should
12766 * have been done if need be on the other controller.
12768 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12770 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12771 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12772 io->io_hdr.nexus.targ_port,
12773 io->io_hdr.nexus.targ_target.id,
12774 io->io_hdr.nexus.targ_lun);
12775 io->io_hdr.status = CTL_CMD_ABORTED;
12776 io->io_hdr.port_status = 31338;
12778 ctl_send_datamove_done(io, /*have_lock*/ 0);
12783 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12784 ctl_datamove_remote_write(io);
12785 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12786 ctl_datamove_remote_read(io);
12788 union ctl_ha_msg msg;
12789 struct scsi_sense_data *sense;
12793 memset(&msg, 0, sizeof(msg));
12795 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12796 msg.hdr.status = CTL_SCSI_ERROR;
12797 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12799 retry_count = 4243;
12801 sense = &msg.scsi.sense_data;
12802 sks[0] = SSD_SCS_VALID;
12803 sks[1] = (retry_count >> 8) & 0xff;
12804 sks[2] = retry_count & 0xff;
12806 /* "Internal target failure" */
12807 scsi_set_sense_data(sense,
12808 /*sense_format*/ SSD_TYPE_NONE,
12809 /*current_error*/ 1,
12810 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12813 /*type*/ SSD_ELEM_SKS,
12814 /*size*/ sizeof(sks),
12818 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12819 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12820 ctl_failover_io(io, /*have_lock*/ 1);
12824 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12825 CTL_HA_STATUS_SUCCESS) {
12826 /* XXX KDM what to do if this fails? */
12834 ctl_process_done(union ctl_io *io)
12836 struct ctl_lun *lun;
12837 struct ctl_softc *ctl_softc;
12838 void (*fe_done)(union ctl_io *io);
12839 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12841 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12844 control_softc->ctl_ports[targ_port]->fe_done;
12847 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12852 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12853 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12855 sbuf_cat(&sb, path_str);
12856 switch (io->io_hdr.io_type) {
12858 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12859 sbuf_printf(&sb, "\n");
12860 sbuf_cat(&sb, path_str);
12861 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12862 io->scsiio.tag_num, io->scsiio.tag_type);
12865 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12866 "Tag Type: %d\n", io->taskio.task_action,
12867 io->taskio.tag_num, io->taskio.tag_type);
12870 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12871 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12874 sbuf_cat(&sb, path_str);
12875 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12876 (intmax_t)time_uptime - io->io_hdr.start_time);
12878 printf("%s", sbuf_data(&sb));
12880 #endif /* CTL_TIME_IO */
12882 switch (io->io_hdr.io_type) {
12886 if (bootverbose || verbose > 0)
12887 ctl_io_error_print(io, NULL);
12888 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12892 return (CTL_RETVAL_COMPLETE);
12895 printf("ctl_process_done: invalid io type %d\n",
12896 io->io_hdr.io_type);
12897 panic("ctl_process_done: invalid io type %d\n",
12898 io->io_hdr.io_type);
12899 break; /* NOTREACHED */
12902 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12904 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12905 io->io_hdr.nexus.targ_mapped_lun));
12909 ctl_softc = lun->ctl_softc;
12911 mtx_lock(&lun->lun_lock);
12914 * Check to see if we have any errors to inject here. We only
12915 * inject errors for commands that don't already have errors set.
12917 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12918 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12919 ctl_inject_error(lun, io);
12922 * XXX KDM how do we treat commands that aren't completed
12925 * XXX KDM should we also track I/O latency?
12927 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12928 io->io_hdr.io_type == CTL_IO_SCSI) {
12930 struct bintime cur_bt;
12934 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12936 type = CTL_STATS_READ;
12937 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12939 type = CTL_STATS_WRITE;
12941 type = CTL_STATS_NO_IO;
12943 lun->stats.ports[targ_port].bytes[type] +=
12944 io->scsiio.kern_total_len;
12945 lun->stats.ports[targ_port].operations[type]++;
12947 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12948 &io->io_hdr.dma_bt);
12949 lun->stats.ports[targ_port].num_dmas[type] +=
12950 io->io_hdr.num_dmas;
12951 getbintime(&cur_bt);
12952 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12953 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12958 * Remove this from the OOA queue.
12960 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12963 * Run through the blocked queue on this LUN and see if anything
12964 * has become unblocked, now that this transaction is done.
12966 ctl_check_blocked(lun);
12969 * If the LUN has been invalidated, free it if there is nothing
12970 * left on its OOA queue.
12972 if ((lun->flags & CTL_LUN_INVALID)
12973 && TAILQ_EMPTY(&lun->ooa_queue)) {
12974 mtx_unlock(&lun->lun_lock);
12975 mtx_lock(&ctl_softc->ctl_lock);
12977 mtx_unlock(&ctl_softc->ctl_lock);
12979 mtx_unlock(&lun->lun_lock);
12982 * If this command has been aborted, make sure we set the status
12983 * properly. The FETD is responsible for freeing the I/O and doing
12984 * whatever it needs to do to clean up its state.
12986 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12987 io->io_hdr.status = CTL_CMD_ABORTED;
12990 * We print out status for every task management command. For SCSI
12991 * commands, we filter out any unit attention errors; they happen
12992 * on every boot, and would clutter up the log. Note: task
12993 * management commands aren't printed here, they are printed above,
12994 * since they should never even make it down here.
12996 switch (io->io_hdr.io_type) {
12997 case CTL_IO_SCSI: {
12998 int error_code, sense_key, asc, ascq;
13002 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13003 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13005 * Since this is just for printing, no need to
13006 * show errors here.
13008 scsi_extract_sense_len(&io->scsiio.sense_data,
13009 io->scsiio.sense_len,
13014 /*show_errors*/ 0);
13017 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13018 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13019 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13020 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13022 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13023 ctl_softc->skipped_prints++;
13025 uint32_t skipped_prints;
13027 skipped_prints = ctl_softc->skipped_prints;
13029 ctl_softc->skipped_prints = 0;
13030 ctl_softc->last_print_jiffies = time_uptime;
13032 if (skipped_prints > 0) {
13034 csevent_log(CSC_CTL | CSC_SHELF_SW |
13036 csevent_LogType_Trace,
13037 csevent_Severity_Information,
13038 csevent_AlertLevel_Green,
13039 csevent_FRU_Firmware,
13040 csevent_FRU_Unknown,
13041 "High CTL error volume, %d prints "
13042 "skipped", skipped_prints);
13045 if (bootverbose || verbose > 0)
13046 ctl_io_error_print(io, NULL);
13052 if (bootverbose || verbose > 0)
13053 ctl_io_error_print(io, NULL);
13060 * Tell the FETD or the other shelf controller we're done with this
13061 * command. Note that only SCSI commands get to this point. Task
13062 * management commands are completed above.
13064 * We only send status to the other controller if we're in XFER
13065 * mode. In SER_ONLY mode, the I/O is done on the controller that
13066 * received the I/O (from CTL's perspective), and so the status is
13069 * XXX KDM if we hold the lock here, we could cause a deadlock
13070 * if the frontend comes back in in this context to queue
13073 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13074 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13075 union ctl_ha_msg msg;
13077 memset(&msg, 0, sizeof(msg));
13078 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13079 msg.hdr.original_sc = io->io_hdr.original_sc;
13080 msg.hdr.nexus = io->io_hdr.nexus;
13081 msg.hdr.status = io->io_hdr.status;
13082 msg.scsi.scsi_status = io->scsiio.scsi_status;
13083 msg.scsi.tag_num = io->scsiio.tag_num;
13084 msg.scsi.tag_type = io->scsiio.tag_type;
13085 msg.scsi.sense_len = io->scsiio.sense_len;
13086 msg.scsi.sense_residual = io->scsiio.sense_residual;
13087 msg.scsi.residual = io->scsiio.residual;
13088 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13089 sizeof(io->scsiio.sense_data));
13091 * We copy this whether or not this is an I/O-related
13092 * command. Otherwise, we'd have to go and check to see
13093 * whether it's a read/write command, and it really isn't
13096 memcpy(&msg.scsi.lbalen,
13097 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13098 sizeof(msg.scsi.lbalen));
13100 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13101 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13102 /* XXX do something here */
13111 return (CTL_RETVAL_COMPLETE);
13115 * Front end should call this if it doesn't do autosense. When the request
13116 * sense comes back in from the initiator, we'll dequeue this and send it.
13119 ctl_queue_sense(union ctl_io *io)
13121 struct ctl_lun *lun;
13122 struct ctl_softc *ctl_softc;
13123 uint32_t initidx, targ_lun;
13125 ctl_softc = control_softc;
13127 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13130 * LUN lookup will likely move to the ctl_work_thread() once we
13131 * have our new queueing infrastructure (that doesn't put things on
13132 * a per-LUN queue initially). That is so that we can handle
13133 * things like an INQUIRY to a LUN that we don't have enabled. We
13134 * can't deal with that right now.
13136 mtx_lock(&ctl_softc->ctl_lock);
13139 * If we don't have a LUN for this, just toss the sense
13142 targ_lun = io->io_hdr.nexus.targ_lun;
13143 if (io->io_hdr.nexus.lun_map_fn != NULL)
13144 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13145 if ((targ_lun < CTL_MAX_LUNS)
13146 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13147 lun = ctl_softc->ctl_luns[targ_lun];
13151 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13153 mtx_lock(&lun->lun_lock);
13155 * Already have CA set for this LUN...toss the sense information.
13157 if (ctl_is_set(lun->have_ca, initidx)) {
13158 mtx_unlock(&lun->lun_lock);
13162 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13163 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13164 sizeof(io->scsiio.sense_data)));
13165 ctl_set_mask(lun->have_ca, initidx);
13166 mtx_unlock(&lun->lun_lock);
13169 mtx_unlock(&ctl_softc->ctl_lock);
13173 return (CTL_RETVAL_COMPLETE);
13177 * Primary command inlet from frontend ports. All SCSI and task I/O
13178 * requests must go through this function.
13181 ctl_queue(union ctl_io *io)
13183 struct ctl_softc *ctl_softc;
13185 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13187 ctl_softc = control_softc;
13190 io->io_hdr.start_time = time_uptime;
13191 getbintime(&io->io_hdr.start_bt);
13192 #endif /* CTL_TIME_IO */
13194 /* Map FE-specific LUN ID into global one. */
13195 if (io->io_hdr.nexus.lun_map_fn != NULL)
13196 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn(
13197 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun);
13199 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun;
13201 switch (io->io_hdr.io_type) {
13203 ctl_enqueue_incoming(io);
13209 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13213 return (CTL_RETVAL_COMPLETE);
13216 #ifdef CTL_IO_DELAY
13218 ctl_done_timer_wakeup(void *arg)
13222 io = (union ctl_io *)arg;
13225 #endif /* CTL_IO_DELAY */
13228 ctl_done(union ctl_io *io)
13230 struct ctl_softc *ctl_softc;
13232 ctl_softc = control_softc;
13235 * Enable this to catch duplicate completion issues.
13238 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13239 printf("%s: type %d msg %d cdb %x iptl: "
13240 "%d:%d:%d:%d tag 0x%04x "
13241 "flag %#x status %x\n",
13243 io->io_hdr.io_type,
13244 io->io_hdr.msg_type,
13246 io->io_hdr.nexus.initid.id,
13247 io->io_hdr.nexus.targ_port,
13248 io->io_hdr.nexus.targ_target.id,
13249 io->io_hdr.nexus.targ_lun,
13250 (io->io_hdr.io_type ==
13252 io->taskio.tag_num :
13253 io->scsiio.tag_num,
13255 io->io_hdr.status);
13257 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13261 * This is an internal copy of an I/O, and should not go through
13262 * the normal done processing logic.
13264 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13268 * We need to send a msg to the serializing shelf to finish the IO
13269 * as well. We don't send a finish message to the other shelf if
13270 * this is a task management command. Task management commands
13271 * aren't serialized in the OOA queue, but rather just executed on
13272 * both shelf controllers for commands that originated on that
13275 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13276 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13277 union ctl_ha_msg msg_io;
13279 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13280 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13281 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13282 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13284 /* continue on to finish IO */
13286 #ifdef CTL_IO_DELAY
13287 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13288 struct ctl_lun *lun;
13290 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13292 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13294 struct ctl_lun *lun;
13296 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13299 && (lun->delay_info.done_delay > 0)) {
13300 struct callout *callout;
13302 callout = (struct callout *)&io->io_hdr.timer_bytes;
13303 callout_init(callout, /*mpsafe*/ 1);
13304 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13305 callout_reset(callout,
13306 lun->delay_info.done_delay * hz,
13307 ctl_done_timer_wakeup, io);
13308 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13309 lun->delay_info.done_delay = 0;
13313 #endif /* CTL_IO_DELAY */
13315 ctl_enqueue_done(io);
13319 ctl_isc(struct ctl_scsiio *ctsio)
13321 struct ctl_lun *lun;
13324 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13326 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13328 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13330 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13337 ctl_work_thread(void *arg)
13339 struct ctl_thread *thr = (struct ctl_thread *)arg;
13340 struct ctl_softc *softc = thr->ctl_softc;
13344 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13350 * We handle the queues in this order:
13352 * - done queue (to free up resources, unblock other commands)
13356 * If those queues are empty, we break out of the loop and
13359 mtx_lock(&thr->queue_lock);
13360 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13362 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13363 mtx_unlock(&thr->queue_lock);
13364 ctl_handle_isc(io);
13367 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13369 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13370 /* clear any blocked commands, call fe_done */
13371 mtx_unlock(&thr->queue_lock);
13372 retval = ctl_process_done(io);
13375 if (!ctl_pause_rtr) {
13376 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13378 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13379 mtx_unlock(&thr->queue_lock);
13380 retval = ctl_scsiio(&io->scsiio);
13381 if (retval != CTL_RETVAL_COMPLETE)
13382 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13386 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13388 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13389 mtx_unlock(&thr->queue_lock);
13390 ctl_scsiio_precheck(softc, &io->scsiio);
13394 /* Sleep until we have something to do. */
13395 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13400 ctl_lun_thread(void *arg)
13402 struct ctl_softc *softc = (struct ctl_softc *)arg;
13403 struct ctl_be_lun *be_lun;
13406 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13410 mtx_lock(&softc->ctl_lock);
13411 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13412 if (be_lun != NULL) {
13413 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13414 mtx_unlock(&softc->ctl_lock);
13415 ctl_create_lun(be_lun);
13419 /* Sleep until we have something to do. */
13420 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13421 PDROP | PRIBIO, "-", 0);
13426 ctl_enqueue_incoming(union ctl_io *io)
13428 struct ctl_softc *softc = control_softc;
13429 struct ctl_thread *thr;
13431 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13432 mtx_lock(&thr->queue_lock);
13433 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13434 mtx_unlock(&thr->queue_lock);
13439 ctl_enqueue_rtr(union ctl_io *io)
13441 struct ctl_softc *softc = control_softc;
13442 struct ctl_thread *thr;
13444 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13445 mtx_lock(&thr->queue_lock);
13446 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13447 mtx_unlock(&thr->queue_lock);
13452 ctl_enqueue_done(union ctl_io *io)
13454 struct ctl_softc *softc = control_softc;
13455 struct ctl_thread *thr;
13457 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13458 mtx_lock(&thr->queue_lock);
13459 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13460 mtx_unlock(&thr->queue_lock);
13465 ctl_enqueue_isc(union ctl_io *io)
13467 struct ctl_softc *softc = control_softc;
13468 struct ctl_thread *thr;
13470 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13471 mtx_lock(&thr->queue_lock);
13472 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13473 mtx_unlock(&thr->queue_lock);
13477 /* Initialization and failover */
13480 ctl_init_isc_msg(void)
13482 printf("CTL: Still calling this thing\n");
13487 * Initializes component into configuration defined by bootMode
13489 * returns hasc_Status:
13491 * ERROR - fatal error
13493 static ctl_ha_comp_status
13494 ctl_isc_init(struct ctl_ha_component *c)
13496 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13503 * Starts component in state requested. If component starts successfully,
13504 * it must set its own state to the requestrd state
13505 * When requested state is HASC_STATE_HA, the component may refine it
13506 * by adding _SLAVE or _MASTER flags.
13507 * Currently allowed state transitions are:
13508 * UNKNOWN->HA - initial startup
13509 * UNKNOWN->SINGLE - initial startup when no parter detected
13510 * HA->SINGLE - failover
13511 * returns ctl_ha_comp_status:
13512 * OK - component successfully started in requested state
13513 * FAILED - could not start the requested state, failover may
13515 * ERROR - fatal error detected, no future startup possible
13517 static ctl_ha_comp_status
13518 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13520 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13522 printf("%s: go\n", __func__);
13524 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13525 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13527 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13528 != CTL_HA_STATUS_SUCCESS) {
13529 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13530 ret = CTL_HA_COMP_STATUS_ERROR;
13532 } else if (CTL_HA_STATE_IS_HA(c->state)
13533 && CTL_HA_STATE_IS_SINGLE(state)){
13534 // HA->SINGLE transition
13538 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13540 ret = CTL_HA_COMP_STATUS_ERROR;
13542 if (CTL_HA_STATE_IS_SINGLE(state))
13551 * Quiesce component
13552 * The component must clear any error conditions (set status to OK) and
13553 * prepare itself to another Start call
13554 * returns ctl_ha_comp_status:
13558 static ctl_ha_comp_status
13559 ctl_isc_quiesce(struct ctl_ha_component *c)
13561 int ret = CTL_HA_COMP_STATUS_OK;
13568 struct ctl_ha_component ctl_ha_component_ctlisc =
13571 .state = CTL_HA_STATE_UNKNOWN,
13572 .init = ctl_isc_init,
13573 .start = ctl_isc_start,
13574 .quiesce = ctl_isc_quiesce