2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
83 struct ctl_softc *control_softc = NULL;
86 * Size and alignment macros needed for Copan-specific HA hardware. These
87 * can go away when the HA code is re-written, and uses busdma for any
90 #define CTL_ALIGN_8B(target, source, type) \
91 if (((uint32_t)source & 0x7) != 0) \
92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
94 target = (type)source;
96 #define CTL_SIZE_8B(target, size) \
97 if ((size & 0x7) != 0) \
98 target = size + (0x8 - (size & 0x7)); \
102 #define CTL_ALIGN_8B_MARGIN 16
105 * Template mode pages.
109 * Note that these are default values only. The actual values will be
110 * filled in when the user does a mode sense.
112 static struct copan_power_subpage power_page_default = {
113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
114 /*subpage*/ PWR_SUBPAGE_CODE,
115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
117 /*page_version*/ PWR_VERSION,
119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 static struct copan_power_subpage power_page_changeable = {
126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
127 /*subpage*/ PWR_SUBPAGE_CODE,
128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
132 /* max_active_luns*/ 0,
133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 static struct copan_aps_subpage aps_page_default = {
139 APS_PAGE_CODE | SMPH_SPF, //page_code
140 APS_SUBPAGE_CODE, //subpage
141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
143 APS_VERSION, //page_version
145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0} //reserved
150 static struct copan_aps_subpage aps_page_changeable = {
151 APS_PAGE_CODE | SMPH_SPF, //page_code
152 APS_SUBPAGE_CODE, //subpage
153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0} //reserved
162 static struct copan_debugconf_subpage debugconf_page_default = {
163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
164 DBGCNF_SUBPAGE_CODE, /* subpage */
165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
167 DBGCNF_VERSION, /* page_version */
168 {CTL_TIME_IO_DEFAULT_SECS>>8,
169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
172 static struct copan_debugconf_subpage debugconf_page_changeable = {
173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
174 DBGCNF_SUBPAGE_CODE, /* subpage */
175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
177 0, /* page_version */
178 {0xff,0xff}, /* ctl_time_io_secs */
181 static struct scsi_format_page format_page_default = {
182 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
183 /*page_length*/sizeof(struct scsi_format_page) - 2,
184 /*tracks_per_zone*/ {0, 0},
185 /*alt_sectors_per_zone*/ {0, 0},
186 /*alt_tracks_per_zone*/ {0, 0},
187 /*alt_tracks_per_lun*/ {0, 0},
188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
190 /*bytes_per_sector*/ {0, 0},
191 /*interleave*/ {0, 0},
192 /*track_skew*/ {0, 0},
193 /*cylinder_skew*/ {0, 0},
195 /*reserved*/ {0, 0, 0}
198 static struct scsi_format_page format_page_changeable = {
199 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
200 /*page_length*/sizeof(struct scsi_format_page) - 2,
201 /*tracks_per_zone*/ {0, 0},
202 /*alt_sectors_per_zone*/ {0, 0},
203 /*alt_tracks_per_zone*/ {0, 0},
204 /*alt_tracks_per_lun*/ {0, 0},
205 /*sectors_per_track*/ {0, 0},
206 /*bytes_per_sector*/ {0, 0},
207 /*interleave*/ {0, 0},
208 /*track_skew*/ {0, 0},
209 /*cylinder_skew*/ {0, 0},
211 /*reserved*/ {0, 0, 0}
214 static struct scsi_rigid_disk_page rigid_disk_page_default = {
215 /*page_code*/SMS_RIGID_DISK_PAGE,
216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
217 /*cylinders*/ {0, 0, 0},
218 /*heads*/ CTL_DEFAULT_HEADS,
219 /*start_write_precomp*/ {0, 0, 0},
220 /*start_reduced_current*/ {0, 0, 0},
221 /*step_rate*/ {0, 0},
222 /*landing_zone_cylinder*/ {0, 0, 0},
223 /*rpl*/ SRDP_RPL_DISABLED,
224 /*rotational_offset*/ 0,
226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
227 CTL_DEFAULT_ROTATION_RATE & 0xff},
231 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
232 /*page_code*/SMS_RIGID_DISK_PAGE,
233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
234 /*cylinders*/ {0, 0, 0},
236 /*start_write_precomp*/ {0, 0, 0},
237 /*start_reduced_current*/ {0, 0, 0},
238 /*step_rate*/ {0, 0},
239 /*landing_zone_cylinder*/ {0, 0, 0},
241 /*rotational_offset*/ 0,
243 /*rotation_rate*/ {0, 0},
247 static struct scsi_caching_page caching_page_default = {
248 /*page_code*/SMS_CACHING_PAGE,
249 /*page_length*/sizeof(struct scsi_caching_page) - 2,
250 /*flags1*/ SCP_DISC | SCP_WCE,
252 /*disable_pf_transfer_len*/ {0xff, 0xff},
253 /*min_prefetch*/ {0, 0},
254 /*max_prefetch*/ {0xff, 0xff},
255 /*max_pf_ceiling*/ {0xff, 0xff},
257 /*cache_segments*/ 0,
258 /*cache_seg_size*/ {0, 0},
260 /*non_cache_seg_size*/ {0, 0, 0}
263 static struct scsi_caching_page caching_page_changeable = {
264 /*page_code*/SMS_CACHING_PAGE,
265 /*page_length*/sizeof(struct scsi_caching_page) - 2,
268 /*disable_pf_transfer_len*/ {0, 0},
269 /*min_prefetch*/ {0, 0},
270 /*max_prefetch*/ {0, 0},
271 /*max_pf_ceiling*/ {0, 0},
273 /*cache_segments*/ 0,
274 /*cache_seg_size*/ {0, 0},
276 /*non_cache_seg_size*/ {0, 0, 0}
279 static struct scsi_control_page control_page_default = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE,
281 /*page_length*/sizeof(struct scsi_control_page) - 2,
286 /*aen_holdoff_period*/{0, 0}
289 static struct scsi_control_page control_page_changeable = {
290 /*page_code*/SMS_CONTROL_MODE_PAGE,
291 /*page_length*/sizeof(struct scsi_control_page) - 2,
296 /*aen_holdoff_period*/{0, 0}
301 * XXX KDM move these into the softc.
303 static int rcv_sync_msg;
304 static int persis_offset;
305 static uint8_t ctl_pause_rtr;
306 static int ctl_is_single = 1;
307 static int index_to_aps_page;
309 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
310 static int worker_threads = -1;
311 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
312 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
313 &worker_threads, 1, "Number of worker threads");
314 static int verbose = 0;
315 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
316 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
317 &verbose, 0, "Show SCSI errors returned to initiator");
320 * Serial number (0x80), device id (0x83), supported pages (0x00),
321 * Block limits (0xB0) and Logical Block Provisioning (0xB2)
323 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 5
325 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
327 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
328 static int ctl_init(void);
329 void ctl_shutdown(void);
330 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
331 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
332 static void ctl_ioctl_online(void *arg);
333 static void ctl_ioctl_offline(void *arg);
334 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
335 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
336 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
337 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
338 static int ctl_ioctl_submit_wait(union ctl_io *io);
339 static void ctl_ioctl_datamove(union ctl_io *io);
340 static void ctl_ioctl_done(union ctl_io *io);
341 static void ctl_ioctl_hard_startstop_callback(void *arg,
342 struct cfi_metatask *metatask);
343 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
344 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
345 struct ctl_ooa *ooa_hdr,
346 struct ctl_ooa_entry *kern_entries);
347 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
349 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
350 uint32_t ctl_port_idx(int port_num);
352 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
353 uint32_t targ_target, uint32_t targ_lun,
355 static void ctl_kfree_io(union ctl_io *io);
357 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
358 struct ctl_be_lun *be_lun, struct ctl_id target_id);
359 static int ctl_free_lun(struct ctl_lun *lun);
360 static void ctl_create_lun(struct ctl_be_lun *be_lun);
362 static void ctl_failover_change_pages(struct ctl_softc *softc,
363 struct ctl_scsiio *ctsio, int master);
366 static int ctl_do_mode_select(union ctl_io *io);
367 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
368 uint64_t res_key, uint64_t sa_res_key,
369 uint8_t type, uint32_t residx,
370 struct ctl_scsiio *ctsio,
371 struct scsi_per_res_out *cdb,
372 struct scsi_per_res_out_parms* param);
373 static void ctl_pro_preempt_other(struct ctl_lun *lun,
374 union ctl_ha_msg *msg);
375 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
376 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
377 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
378 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
379 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
381 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
383 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
384 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
385 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
386 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
387 union ctl_io *ooa_io);
388 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
389 union ctl_io *starting_io);
390 static int ctl_check_blocked(struct ctl_lun *lun);
391 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
393 const struct ctl_cmd_entry *entry,
394 struct ctl_scsiio *ctsio);
395 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
396 static void ctl_failover(void);
397 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
398 struct ctl_scsiio *ctsio);
399 static int ctl_scsiio(struct ctl_scsiio *ctsio);
401 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
402 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
403 ctl_ua_type ua_type);
404 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
405 ctl_ua_type ua_type);
406 static int ctl_abort_task(union ctl_io *io);
407 static void ctl_run_task(union ctl_io *io);
409 static void ctl_datamove_timer_wakeup(void *arg);
410 static void ctl_done_timer_wakeup(void *arg);
411 #endif /* CTL_IO_DELAY */
413 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
414 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
415 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
416 static void ctl_datamove_remote_write(union ctl_io *io);
417 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
418 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
419 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
420 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
421 ctl_ha_dt_cb callback);
422 static void ctl_datamove_remote_read(union ctl_io *io);
423 static void ctl_datamove_remote(union ctl_io *io);
424 static int ctl_process_done(union ctl_io *io);
425 static void ctl_lun_thread(void *arg);
426 static void ctl_work_thread(void *arg);
427 static void ctl_enqueue_incoming(union ctl_io *io);
428 static void ctl_enqueue_rtr(union ctl_io *io);
429 static void ctl_enqueue_done(union ctl_io *io);
430 static void ctl_enqueue_isc(union ctl_io *io);
431 static const struct ctl_cmd_entry *
432 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
433 static const struct ctl_cmd_entry *
434 ctl_validate_command(struct ctl_scsiio *ctsio);
435 static int ctl_cmd_applicable(uint8_t lun_type,
436 const struct ctl_cmd_entry *entry);
439 * Load the serialization table. This isn't very pretty, but is probably
440 * the easiest way to do it.
442 #include "ctl_ser_table.c"
445 * We only need to define open, close and ioctl routines for this driver.
447 static struct cdevsw ctl_cdevsw = {
448 .d_version = D_VERSION,
451 .d_close = ctl_close,
452 .d_ioctl = ctl_ioctl,
457 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
458 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
460 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
462 static moduledata_t ctl_moduledata = {
464 ctl_module_event_handler,
468 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
469 MODULE_VERSION(ctl, 1);
471 static struct ctl_frontend ioctl_frontend =
477 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
478 union ctl_ha_msg *msg_info)
480 struct ctl_scsiio *ctsio;
482 if (msg_info->hdr.original_sc == NULL) {
483 printf("%s: original_sc == NULL!\n", __func__);
484 /* XXX KDM now what? */
488 ctsio = &msg_info->hdr.original_sc->scsiio;
489 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
490 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
491 ctsio->io_hdr.status = msg_info->hdr.status;
492 ctsio->scsi_status = msg_info->scsi.scsi_status;
493 ctsio->sense_len = msg_info->scsi.sense_len;
494 ctsio->sense_residual = msg_info->scsi.sense_residual;
495 ctsio->residual = msg_info->scsi.residual;
496 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
497 sizeof(ctsio->sense_data));
498 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
499 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
500 ctl_enqueue_isc((union ctl_io *)ctsio);
504 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
505 union ctl_ha_msg *msg_info)
507 struct ctl_scsiio *ctsio;
509 if (msg_info->hdr.serializing_sc == NULL) {
510 printf("%s: serializing_sc == NULL!\n", __func__);
511 /* XXX KDM now what? */
515 ctsio = &msg_info->hdr.serializing_sc->scsiio;
518 * Attempt to catch the situation where an I/O has
519 * been freed, and we're using it again.
521 if (ctsio->io_hdr.io_type == 0xff) {
522 union ctl_io *tmp_io;
523 tmp_io = (union ctl_io *)ctsio;
524 printf("%s: %p use after free!\n", __func__,
526 printf("%s: type %d msg %d cdb %x iptl: "
527 "%d:%d:%d:%d tag 0x%04x "
528 "flag %#x status %x\n",
530 tmp_io->io_hdr.io_type,
531 tmp_io->io_hdr.msg_type,
532 tmp_io->scsiio.cdb[0],
533 tmp_io->io_hdr.nexus.initid.id,
534 tmp_io->io_hdr.nexus.targ_port,
535 tmp_io->io_hdr.nexus.targ_target.id,
536 tmp_io->io_hdr.nexus.targ_lun,
537 (tmp_io->io_hdr.io_type ==
539 tmp_io->taskio.tag_num :
540 tmp_io->scsiio.tag_num,
541 tmp_io->io_hdr.flags,
542 tmp_io->io_hdr.status);
545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
546 ctl_enqueue_isc((union ctl_io *)ctsio);
550 * ISC (Inter Shelf Communication) event handler. Events from the HA
551 * subsystem come in here.
554 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
556 struct ctl_softc *ctl_softc;
558 struct ctl_prio *presio;
559 ctl_ha_status isc_status;
561 ctl_softc = control_softc;
566 printf("CTL: Isc Msg event %d\n", event);
568 if (event == CTL_HA_EVT_MSG_RECV) {
569 union ctl_ha_msg msg_info;
571 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
572 sizeof(msg_info), /*wait*/ 0);
574 printf("CTL: msg_type %d\n", msg_info.msg_type);
576 if (isc_status != 0) {
577 printf("Error receiving message, status = %d\n",
582 switch (msg_info.hdr.msg_type) {
583 case CTL_MSG_SERIALIZE:
585 printf("Serialize\n");
587 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
589 printf("ctl_isc_event_handler: can't allocate "
592 /* Need to set busy and send msg back */
593 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
594 msg_info.hdr.status = CTL_SCSI_ERROR;
595 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
596 msg_info.scsi.sense_len = 0;
597 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
598 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
603 // populate ctsio from msg_info
604 io->io_hdr.io_type = CTL_IO_SCSI;
605 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
606 io->io_hdr.original_sc = msg_info.hdr.original_sc;
608 printf("pOrig %x\n", (int)msg_info.original_sc);
610 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
613 * If we're in serialization-only mode, we don't
614 * want to go through full done processing. Thus
617 * XXX KDM add another flag that is more specific.
619 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
620 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
621 io->io_hdr.nexus = msg_info.hdr.nexus;
623 printf("targ %d, port %d, iid %d, lun %d\n",
624 io->io_hdr.nexus.targ_target.id,
625 io->io_hdr.nexus.targ_port,
626 io->io_hdr.nexus.initid.id,
627 io->io_hdr.nexus.targ_lun);
629 io->scsiio.tag_num = msg_info.scsi.tag_num;
630 io->scsiio.tag_type = msg_info.scsi.tag_type;
631 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
633 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
634 const struct ctl_cmd_entry *entry;
636 entry = ctl_get_cmd_entry(&io->scsiio);
637 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
639 entry->flags & CTL_FLAG_DATA_MASK;
644 /* Performed on the Originating SC, XFER mode only */
645 case CTL_MSG_DATAMOVE: {
646 struct ctl_sg_entry *sgl;
649 io = msg_info.hdr.original_sc;
651 printf("%s: original_sc == NULL!\n", __func__);
652 /* XXX KDM do something here */
655 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
656 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
658 * Keep track of this, we need to send it back over
659 * when the datamove is complete.
661 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
663 if (msg_info.dt.sg_sequence == 0) {
665 * XXX KDM we use the preallocated S/G list
666 * here, but we'll need to change this to
667 * dynamic allocation if we need larger S/G
670 if (msg_info.dt.kern_sg_entries >
671 sizeof(io->io_hdr.remote_sglist) /
672 sizeof(io->io_hdr.remote_sglist[0])) {
673 printf("%s: number of S/G entries "
674 "needed %u > allocated num %zd\n",
676 msg_info.dt.kern_sg_entries,
677 sizeof(io->io_hdr.remote_sglist)/
678 sizeof(io->io_hdr.remote_sglist[0]));
681 * XXX KDM send a message back to
682 * the other side to shut down the
683 * DMA. The error will come back
684 * through via the normal channel.
688 sgl = io->io_hdr.remote_sglist;
690 sizeof(io->io_hdr.remote_sglist));
692 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
694 io->scsiio.kern_sg_entries =
695 msg_info.dt.kern_sg_entries;
696 io->scsiio.rem_sg_entries =
697 msg_info.dt.kern_sg_entries;
698 io->scsiio.kern_data_len =
699 msg_info.dt.kern_data_len;
700 io->scsiio.kern_total_len =
701 msg_info.dt.kern_total_len;
702 io->scsiio.kern_data_resid =
703 msg_info.dt.kern_data_resid;
704 io->scsiio.kern_rel_offset =
705 msg_info.dt.kern_rel_offset;
707 * Clear out per-DMA flags.
709 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
711 * Add per-DMA flags that are set for this
712 * particular DMA request.
714 io->io_hdr.flags |= msg_info.dt.flags &
717 sgl = (struct ctl_sg_entry *)
718 io->scsiio.kern_data_ptr;
720 for (i = msg_info.dt.sent_sg_entries, j = 0;
721 i < (msg_info.dt.sent_sg_entries +
722 msg_info.dt.cur_sg_entries); i++, j++) {
723 sgl[i].addr = msg_info.dt.sg_list[j].addr;
724 sgl[i].len = msg_info.dt.sg_list[j].len;
727 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
729 msg_info.dt.sg_list[j].addr,
730 msg_info.dt.sg_list[j].len,
731 sgl[i].addr, sgl[i].len, j, i);
735 memcpy(&sgl[msg_info.dt.sent_sg_entries],
737 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
741 * If this is the last piece of the I/O, we've got
742 * the full S/G list. Queue processing in the thread.
743 * Otherwise wait for the next piece.
745 if (msg_info.dt.sg_last != 0)
749 /* Performed on the Serializing (primary) SC, XFER mode only */
750 case CTL_MSG_DATAMOVE_DONE: {
751 if (msg_info.hdr.serializing_sc == NULL) {
752 printf("%s: serializing_sc == NULL!\n",
754 /* XXX KDM now what? */
758 * We grab the sense information here in case
759 * there was a failure, so we can return status
760 * back to the initiator.
762 io = msg_info.hdr.serializing_sc;
763 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
764 io->io_hdr.status = msg_info.hdr.status;
765 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
766 io->scsiio.sense_len = msg_info.scsi.sense_len;
767 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
768 io->io_hdr.port_status = msg_info.scsi.fetd_status;
769 io->scsiio.residual = msg_info.scsi.residual;
770 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
771 sizeof(io->scsiio.sense_data));
776 /* Preformed on Originating SC, SER_ONLY mode */
778 io = msg_info.hdr.original_sc;
780 printf("%s: Major Bummer\n", __func__);
784 printf("pOrig %x\n",(int) ctsio);
787 io->io_hdr.msg_type = CTL_MSG_R2R;
788 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
793 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
795 * Performed on the Originating (i.e. secondary) SC in XFER
798 case CTL_MSG_FINISH_IO:
799 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
800 ctl_isc_handler_finish_xfer(ctl_softc,
803 ctl_isc_handler_finish_ser_only(ctl_softc,
807 /* Preformed on Originating SC */
808 case CTL_MSG_BAD_JUJU:
809 io = msg_info.hdr.original_sc;
811 printf("%s: Bad JUJU!, original_sc is NULL!\n",
815 ctl_copy_sense_data(&msg_info, io);
817 * IO should have already been cleaned up on other
818 * SC so clear this flag so we won't send a message
819 * back to finish the IO there.
821 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
822 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
824 /* io = msg_info.hdr.serializing_sc; */
825 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
829 /* Handle resets sent from the other side */
830 case CTL_MSG_MANAGE_TASKS: {
831 struct ctl_taskio *taskio;
832 taskio = (struct ctl_taskio *)ctl_alloc_io(
833 (void *)ctl_softc->othersc_pool);
834 if (taskio == NULL) {
835 printf("ctl_isc_event_handler: can't allocate "
838 /* should I just call the proper reset func
842 ctl_zero_io((union ctl_io *)taskio);
843 taskio->io_hdr.io_type = CTL_IO_TASK;
844 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
845 taskio->io_hdr.nexus = msg_info.hdr.nexus;
846 taskio->task_action = msg_info.task.task_action;
847 taskio->tag_num = msg_info.task.tag_num;
848 taskio->tag_type = msg_info.task.tag_type;
850 taskio->io_hdr.start_time = time_uptime;
851 getbintime(&taskio->io_hdr.start_bt);
853 cs_prof_gettime(&taskio->io_hdr.start_ticks);
855 #endif /* CTL_TIME_IO */
856 ctl_run_task((union ctl_io *)taskio);
859 /* Persistent Reserve action which needs attention */
860 case CTL_MSG_PERS_ACTION:
861 presio = (struct ctl_prio *)ctl_alloc_io(
862 (void *)ctl_softc->othersc_pool);
863 if (presio == NULL) {
864 printf("ctl_isc_event_handler: can't allocate "
867 /* Need to set busy and send msg back */
870 ctl_zero_io((union ctl_io *)presio);
871 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
872 presio->pr_msg = msg_info.pr;
873 ctl_enqueue_isc((union ctl_io *)presio);
875 case CTL_MSG_SYNC_FE:
878 case CTL_MSG_APS_LOCK: {
879 // It's quicker to execute this then to
882 struct ctl_page_index *page_index;
883 struct copan_aps_subpage *current_sp;
886 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
887 lun = ctl_softc->ctl_luns[targ_lun];
888 mtx_lock(&lun->lun_lock);
889 page_index = &lun->mode_pages.index[index_to_aps_page];
890 current_sp = (struct copan_aps_subpage *)
891 (page_index->page_data +
892 (page_index->page_len * CTL_PAGE_CURRENT));
894 current_sp->lock_active = msg_info.aps.lock_flag;
895 mtx_unlock(&lun->lun_lock);
899 printf("How did I get here?\n");
901 } else if (event == CTL_HA_EVT_MSG_SENT) {
902 if (param != CTL_HA_STATUS_SUCCESS) {
903 printf("Bad status from ctl_ha_msg_send status %d\n",
907 } else if (event == CTL_HA_EVT_DISCONNECT) {
908 printf("CTL: Got a disconnect from Isc\n");
911 printf("ctl_isc_event_handler: Unknown event %d\n", event);
920 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
922 struct scsi_sense_data *sense;
924 sense = &dest->scsiio.sense_data;
925 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
926 dest->scsiio.scsi_status = src->scsi.scsi_status;
927 dest->scsiio.sense_len = src->scsi.sense_len;
928 dest->io_hdr.status = src->hdr.status;
934 struct ctl_softc *softc;
935 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
936 struct ctl_port *port;
938 int i, error, retval;
945 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
947 softc = control_softc;
949 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
952 softc->dev->si_drv1 = softc;
955 * By default, return a "bad LUN" peripheral qualifier for unknown
956 * LUNs. The user can override this default using the tunable or
957 * sysctl. See the comment in ctl_inquiry_std() for more details.
959 softc->inquiry_pq_no_lun = 1;
960 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
961 &softc->inquiry_pq_no_lun);
962 sysctl_ctx_init(&softc->sysctl_ctx);
963 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
964 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
965 CTLFLAG_RD, 0, "CAM Target Layer");
967 if (softc->sysctl_tree == NULL) {
968 printf("%s: unable to allocate sysctl tree\n", __func__);
969 destroy_dev(softc->dev);
970 free(control_softc, M_DEVBUF);
971 control_softc = NULL;
975 SYSCTL_ADD_INT(&softc->sysctl_ctx,
976 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
977 "inquiry_pq_no_lun", CTLFLAG_RW,
978 &softc->inquiry_pq_no_lun, 0,
979 "Report no lun possible for invalid LUNs");
981 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
982 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
983 softc->open_count = 0;
986 * Default to actually sending a SYNCHRONIZE CACHE command down to
989 softc->flags = CTL_FLAG_REAL_SYNC;
992 * In Copan's HA scheme, the "master" and "slave" roles are
993 * figured out through the slot the controller is in. Although it
994 * is an active/active system, someone has to be in charge.
997 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1001 softc->flags |= CTL_FLAG_MASTER_SHELF;
1004 persis_offset = CTL_MAX_INITIATORS;
1007 * XXX KDM need to figure out where we want to get our target ID
1008 * and WWID. Is it different on each port?
1010 softc->target.id = 0;
1011 softc->target.wwid[0] = 0x12345678;
1012 softc->target.wwid[1] = 0x87654321;
1013 STAILQ_INIT(&softc->lun_list);
1014 STAILQ_INIT(&softc->pending_lun_queue);
1015 STAILQ_INIT(&softc->fe_list);
1016 STAILQ_INIT(&softc->port_list);
1017 STAILQ_INIT(&softc->be_list);
1018 STAILQ_INIT(&softc->io_pools);
1020 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1021 &internal_pool)!= 0){
1022 printf("ctl: can't allocate %d entry internal pool, "
1023 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1027 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1028 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1029 printf("ctl: can't allocate %d entry emergency pool, "
1030 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1031 ctl_pool_free(internal_pool);
1035 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1038 printf("ctl: can't allocate %d entry other SC pool, "
1039 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1040 ctl_pool_free(internal_pool);
1041 ctl_pool_free(emergency_pool);
1045 softc->internal_pool = internal_pool;
1046 softc->emergency_pool = emergency_pool;
1047 softc->othersc_pool = other_pool;
1049 if (worker_threads <= 0)
1050 worker_threads = max(1, mp_ncpus / 4);
1051 if (worker_threads > CTL_MAX_THREADS)
1052 worker_threads = CTL_MAX_THREADS;
1054 for (i = 0; i < worker_threads; i++) {
1055 struct ctl_thread *thr = &softc->threads[i];
1057 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1058 thr->ctl_softc = softc;
1059 STAILQ_INIT(&thr->incoming_queue);
1060 STAILQ_INIT(&thr->rtr_queue);
1061 STAILQ_INIT(&thr->done_queue);
1062 STAILQ_INIT(&thr->isc_queue);
1064 error = kproc_kthread_add(ctl_work_thread, thr,
1065 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1067 printf("error creating CTL work thread!\n");
1068 ctl_pool_free(internal_pool);
1069 ctl_pool_free(emergency_pool);
1070 ctl_pool_free(other_pool);
1074 error = kproc_kthread_add(ctl_lun_thread, softc,
1075 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1077 printf("error creating CTL lun thread!\n");
1078 ctl_pool_free(internal_pool);
1079 ctl_pool_free(emergency_pool);
1080 ctl_pool_free(other_pool);
1084 printf("ctl: CAM Target Layer loaded\n");
1087 * Initialize the initiator and portname mappings
1089 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1092 * Initialize the ioctl front end.
1094 ctl_frontend_register(&ioctl_frontend);
1095 port = &softc->ioctl_info.port;
1096 port->frontend = &ioctl_frontend;
1097 sprintf(softc->ioctl_info.port_name, "ioctl");
1098 port->port_type = CTL_PORT_IOCTL;
1099 port->num_requested_ctl_io = 100;
1100 port->port_name = softc->ioctl_info.port_name;
1101 port->port_online = ctl_ioctl_online;
1102 port->port_offline = ctl_ioctl_offline;
1103 port->onoff_arg = &softc->ioctl_info;
1104 port->lun_enable = ctl_ioctl_lun_enable;
1105 port->lun_disable = ctl_ioctl_lun_disable;
1106 port->targ_lun_arg = &softc->ioctl_info;
1107 port->fe_datamove = ctl_ioctl_datamove;
1108 port->fe_done = ctl_ioctl_done;
1109 port->max_targets = 15;
1110 port->max_target_id = 15;
1112 if (ctl_port_register(&softc->ioctl_info.port,
1113 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1114 printf("ctl: ioctl front end registration failed, will "
1115 "continue anyway\n");
1119 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1120 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1121 sizeof(struct callout), CTL_TIMER_BYTES);
1124 #endif /* CTL_IO_DELAY */
1132 struct ctl_softc *softc;
1133 struct ctl_lun *lun, *next_lun;
1134 struct ctl_io_pool *pool;
1136 softc = (struct ctl_softc *)control_softc;
1138 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1139 printf("ctl: ioctl front end deregistration failed\n");
1141 mtx_lock(&softc->ctl_lock);
1146 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1147 next_lun = STAILQ_NEXT(lun, links);
1151 mtx_unlock(&softc->ctl_lock);
1153 ctl_frontend_deregister(&ioctl_frontend);
1156 * This will rip the rug out from under any FETDs or anyone else
1157 * that has a pool allocated. Since we increment our module
1158 * refcount any time someone outside the main CTL module allocates
1159 * a pool, we shouldn't have any problems here. The user won't be
1160 * able to unload the CTL module until client modules have
1161 * successfully unloaded.
1163 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1164 ctl_pool_free(pool);
1167 ctl_shutdown_thread(softc->work_thread);
1168 mtx_destroy(&softc->queue_lock);
1171 mtx_destroy(&softc->pool_lock);
1172 mtx_destroy(&softc->ctl_lock);
1174 destroy_dev(softc->dev);
1176 sysctl_ctx_free(&softc->sysctl_ctx);
1178 free(control_softc, M_DEVBUF);
1179 control_softc = NULL;
1182 printf("ctl: CAM Target Layer unloaded\n");
1186 ctl_module_event_handler(module_t mod, int what, void *arg)
1191 return (ctl_init());
1195 return (EOPNOTSUPP);
1200 * XXX KDM should we do some access checks here? Bump a reference count to
1201 * prevent a CTL module from being unloaded while someone has it open?
1204 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1210 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1216 ctl_port_enable(ctl_port_type port_type)
1218 struct ctl_softc *softc;
1219 struct ctl_port *port;
1221 if (ctl_is_single == 0) {
1222 union ctl_ha_msg msg_info;
1226 printf("%s: HA mode, synchronizing frontend enable\n",
1229 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1230 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1231 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1232 printf("Sync msg send error retval %d\n", isc_retval);
1234 if (!rcv_sync_msg) {
1235 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1236 sizeof(msg_info), 1);
1239 printf("CTL:Frontend Enable\n");
1241 printf("%s: single mode, skipping frontend synchronization\n",
1246 softc = control_softc;
1248 STAILQ_FOREACH(port, &softc->port_list, links) {
1249 if (port_type & port->port_type)
1252 printf("port %d\n", port->targ_port);
1254 ctl_port_online(port);
1262 ctl_port_disable(ctl_port_type port_type)
1264 struct ctl_softc *softc;
1265 struct ctl_port *port;
1267 softc = control_softc;
1269 STAILQ_FOREACH(port, &softc->port_list, links) {
1270 if (port_type & port->port_type)
1271 ctl_port_offline(port);
1278 * Returns 0 for success, 1 for failure.
1279 * Currently the only failure mode is if there aren't enough entries
1280 * allocated. So, in case of a failure, look at num_entries_dropped,
1281 * reallocate and try again.
1284 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1285 int *num_entries_filled, int *num_entries_dropped,
1286 ctl_port_type port_type, int no_virtual)
1288 struct ctl_softc *softc;
1289 struct ctl_port *port;
1290 int entries_dropped, entries_filled;
1294 softc = control_softc;
1298 entries_dropped = 0;
1301 mtx_lock(&softc->ctl_lock);
1302 STAILQ_FOREACH(port, &softc->port_list, links) {
1303 struct ctl_port_entry *entry;
1305 if ((port->port_type & port_type) == 0)
1308 if ((no_virtual != 0)
1309 && (port->virtual_port != 0))
1312 if (entries_filled >= num_entries_alloced) {
1316 entry = &entries[i];
1318 entry->port_type = port->port_type;
1319 strlcpy(entry->port_name, port->port_name,
1320 sizeof(entry->port_name));
1321 entry->physical_port = port->physical_port;
1322 entry->virtual_port = port->virtual_port;
1323 entry->wwnn = port->wwnn;
1324 entry->wwpn = port->wwpn;
1330 mtx_unlock(&softc->ctl_lock);
1332 if (entries_dropped > 0)
1335 *num_entries_dropped = entries_dropped;
1336 *num_entries_filled = entries_filled;
1342 ctl_ioctl_online(void *arg)
1344 struct ctl_ioctl_info *ioctl_info;
1346 ioctl_info = (struct ctl_ioctl_info *)arg;
1348 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1352 ctl_ioctl_offline(void *arg)
1354 struct ctl_ioctl_info *ioctl_info;
1356 ioctl_info = (struct ctl_ioctl_info *)arg;
1358 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1362 * Remove an initiator by port number and initiator ID.
1363 * Returns 0 for success, 1 for failure.
1366 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1368 struct ctl_softc *softc;
1370 softc = control_softc;
1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1375 || (targ_port > CTL_MAX_PORTS)) {
1376 printf("%s: invalid port number %d\n", __func__, targ_port);
1379 if (iid > CTL_MAX_INIT_PER_PORT) {
1380 printf("%s: initiator ID %u > maximun %u!\n",
1381 __func__, iid, CTL_MAX_INIT_PER_PORT);
1385 mtx_lock(&softc->ctl_lock);
1387 softc->wwpn_iid[targ_port][iid].in_use = 0;
1389 mtx_unlock(&softc->ctl_lock);
1395 * Add an initiator to the initiator map.
1396 * Returns 0 for success, 1 for failure.
1399 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1401 struct ctl_softc *softc;
1404 softc = control_softc;
1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1411 || (targ_port > CTL_MAX_PORTS)) {
1412 printf("%s: invalid port number %d\n", __func__, targ_port);
1415 if (iid > CTL_MAX_INIT_PER_PORT) {
1416 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1417 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1421 mtx_lock(&softc->ctl_lock);
1423 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1425 * We don't treat this as an error.
1427 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1428 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1429 __func__, targ_port, iid, (uintmax_t)wwpn);
1434 * This is an error, but what do we do about it? The
1435 * driver is telling us we have a new WWPN for this
1436 * initiator ID, so we pretty much need to use it.
1438 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1439 "still at that address\n", __func__, targ_port, iid,
1441 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1444 * XXX KDM clear have_ca and ua_pending on each LUN for
1448 softc->wwpn_iid[targ_port][iid].in_use = 1;
1449 softc->wwpn_iid[targ_port][iid].iid = iid;
1450 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1451 softc->wwpn_iid[targ_port][iid].port = targ_port;
1455 mtx_unlock(&softc->ctl_lock);
1461 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1467 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1473 * Data movement routine for the CTL ioctl frontend port.
1476 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1478 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1479 struct ctl_sg_entry ext_entry, kern_entry;
1480 int ext_sglen, ext_sg_entries, kern_sg_entries;
1481 int ext_sg_start, ext_offset;
1482 int len_to_copy, len_copied;
1483 int kern_watermark, ext_watermark;
1484 int ext_sglist_malloced;
1487 ext_sglist_malloced = 0;
1491 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1494 * If this flag is set, fake the data transfer.
1496 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1497 ctsio->ext_data_filled = ctsio->ext_data_len;
1502 * To simplify things here, if we have a single buffer, stick it in
1503 * a S/G entry and just make it a single entry S/G list.
1505 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1508 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1510 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1512 ext_sglist_malloced = 1;
1513 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1515 ctl_set_internal_failure(ctsio,
1520 ext_sg_entries = ctsio->ext_sg_entries;
1522 for (i = 0; i < ext_sg_entries; i++) {
1523 if ((len_seen + ext_sglist[i].len) >=
1524 ctsio->ext_data_filled) {
1526 ext_offset = ctsio->ext_data_filled - len_seen;
1529 len_seen += ext_sglist[i].len;
1532 ext_sglist = &ext_entry;
1533 ext_sglist->addr = ctsio->ext_data_ptr;
1534 ext_sglist->len = ctsio->ext_data_len;
1537 ext_offset = ctsio->ext_data_filled;
1540 if (ctsio->kern_sg_entries > 0) {
1541 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1542 kern_sg_entries = ctsio->kern_sg_entries;
1544 kern_sglist = &kern_entry;
1545 kern_sglist->addr = ctsio->kern_data_ptr;
1546 kern_sglist->len = ctsio->kern_data_len;
1547 kern_sg_entries = 1;
1552 ext_watermark = ext_offset;
1554 for (i = ext_sg_start, j = 0;
1555 i < ext_sg_entries && j < kern_sg_entries;) {
1556 uint8_t *ext_ptr, *kern_ptr;
1558 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1559 kern_sglist[j].len - kern_watermark);
1561 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1562 ext_ptr = ext_ptr + ext_watermark;
1563 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1567 panic("need to implement bus address support");
1569 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1572 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1573 kern_ptr = kern_ptr + kern_watermark;
1575 kern_watermark += len_to_copy;
1576 ext_watermark += len_to_copy;
1578 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1580 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1581 "bytes to user\n", len_to_copy));
1582 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1583 "to %p\n", kern_ptr, ext_ptr));
1584 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1585 ctl_set_internal_failure(ctsio,
1591 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1592 "bytes from user\n", len_to_copy));
1593 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1594 "to %p\n", ext_ptr, kern_ptr));
1595 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1596 ctl_set_internal_failure(ctsio,
1603 len_copied += len_to_copy;
1605 if (ext_sglist[i].len == ext_watermark) {
1610 if (kern_sglist[j].len == kern_watermark) {
1616 ctsio->ext_data_filled += len_copied;
1618 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1619 "kern_sg_entries: %d\n", ext_sg_entries,
1621 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1622 "kern_data_len = %d\n", ctsio->ext_data_len,
1623 ctsio->kern_data_len));
1626 /* XXX KDM set residual?? */
1629 if (ext_sglist_malloced != 0)
1630 free(ext_sglist, M_CTL);
1632 return (CTL_RETVAL_COMPLETE);
1636 * Serialize a command that went down the "wrong" side, and so was sent to
1637 * this controller for execution. The logic is a little different than the
1638 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1639 * sent back to the other side, but in the success case, we execute the
1640 * command on this side (XFER mode) or tell the other side to execute it
1644 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1646 struct ctl_softc *ctl_softc;
1647 union ctl_ha_msg msg_info;
1648 struct ctl_lun *lun;
1652 ctl_softc = control_softc;
1654 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1655 lun = ctl_softc->ctl_luns[targ_lun];
1659 * Why isn't LUN defined? The other side wouldn't
1660 * send a cmd if the LUN is undefined.
1662 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1664 /* "Logical unit not supported" */
1665 ctl_set_sense_data(&msg_info.scsi.sense_data,
1667 /*sense_format*/SSD_TYPE_NONE,
1668 /*current_error*/ 1,
1669 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1674 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1675 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1676 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1677 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1678 msg_info.hdr.serializing_sc = NULL;
1679 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1680 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1681 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1687 mtx_lock(&lun->lun_lock);
1688 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1690 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1691 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1693 case CTL_ACTION_BLOCK:
1694 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1695 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1698 case CTL_ACTION_PASS:
1699 case CTL_ACTION_SKIP:
1700 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1701 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1702 ctl_enqueue_rtr((union ctl_io *)ctsio);
1705 /* send msg back to other side */
1706 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1707 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1708 msg_info.hdr.msg_type = CTL_MSG_R2R;
1710 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1712 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1713 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1717 case CTL_ACTION_OVERLAP:
1718 /* OVERLAPPED COMMANDS ATTEMPTED */
1719 ctl_set_sense_data(&msg_info.scsi.sense_data,
1721 /*sense_format*/SSD_TYPE_NONE,
1722 /*current_error*/ 1,
1723 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1728 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1729 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1730 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1731 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1732 msg_info.hdr.serializing_sc = NULL;
1733 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1735 printf("BAD JUJU:Major Bummer Overlap\n");
1737 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1739 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1740 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1743 case CTL_ACTION_OVERLAP_TAG:
1744 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1745 ctl_set_sense_data(&msg_info.scsi.sense_data,
1747 /*sense_format*/SSD_TYPE_NONE,
1748 /*current_error*/ 1,
1749 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1751 /*ascq*/ ctsio->tag_num & 0xff,
1754 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1755 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1756 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1757 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1758 msg_info.hdr.serializing_sc = NULL;
1759 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1761 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1763 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1765 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1766 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1769 case CTL_ACTION_ERROR:
1771 /* "Internal target failure" */
1772 ctl_set_sense_data(&msg_info.scsi.sense_data,
1774 /*sense_format*/SSD_TYPE_NONE,
1775 /*current_error*/ 1,
1776 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1781 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1782 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1783 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1784 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1785 msg_info.hdr.serializing_sc = NULL;
1786 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1788 printf("BAD JUJU:Major Bummer HW Error\n");
1790 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1792 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1793 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1797 mtx_unlock(&lun->lun_lock);
1802 ctl_ioctl_submit_wait(union ctl_io *io)
1804 struct ctl_fe_ioctl_params params;
1805 ctl_fe_ioctl_state last_state;
1810 bzero(¶ms, sizeof(params));
1812 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1813 cv_init(¶ms.sem, "ctlioccv");
1814 params.state = CTL_IOCTL_INPROG;
1815 last_state = params.state;
1817 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1819 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1821 /* This shouldn't happen */
1822 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1828 mtx_lock(¶ms.ioctl_mtx);
1830 * Check the state here, and don't sleep if the state has
1831 * already changed (i.e. wakeup has already occured, but we
1832 * weren't waiting yet).
1834 if (params.state == last_state) {
1835 /* XXX KDM cv_wait_sig instead? */
1836 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1838 last_state = params.state;
1840 switch (params.state) {
1841 case CTL_IOCTL_INPROG:
1842 /* Why did we wake up? */
1843 /* XXX KDM error here? */
1844 mtx_unlock(¶ms.ioctl_mtx);
1846 case CTL_IOCTL_DATAMOVE:
1847 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1850 * change last_state back to INPROG to avoid
1851 * deadlock on subsequent data moves.
1853 params.state = last_state = CTL_IOCTL_INPROG;
1855 mtx_unlock(¶ms.ioctl_mtx);
1856 ctl_ioctl_do_datamove(&io->scsiio);
1858 * Note that in some cases, most notably writes,
1859 * this will queue the I/O and call us back later.
1860 * In other cases, generally reads, this routine
1861 * will immediately call back and wake us up,
1862 * probably using our own context.
1864 io->scsiio.be_move_done(io);
1866 case CTL_IOCTL_DONE:
1867 mtx_unlock(¶ms.ioctl_mtx);
1868 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1872 mtx_unlock(¶ms.ioctl_mtx);
1873 /* XXX KDM error here? */
1876 } while (done == 0);
1878 mtx_destroy(¶ms.ioctl_mtx);
1879 cv_destroy(¶ms.sem);
1881 return (CTL_RETVAL_COMPLETE);
1885 ctl_ioctl_datamove(union ctl_io *io)
1887 struct ctl_fe_ioctl_params *params;
1889 params = (struct ctl_fe_ioctl_params *)
1890 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1892 mtx_lock(¶ms->ioctl_mtx);
1893 params->state = CTL_IOCTL_DATAMOVE;
1894 cv_broadcast(¶ms->sem);
1895 mtx_unlock(¶ms->ioctl_mtx);
1899 ctl_ioctl_done(union ctl_io *io)
1901 struct ctl_fe_ioctl_params *params;
1903 params = (struct ctl_fe_ioctl_params *)
1904 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1906 mtx_lock(¶ms->ioctl_mtx);
1907 params->state = CTL_IOCTL_DONE;
1908 cv_broadcast(¶ms->sem);
1909 mtx_unlock(¶ms->ioctl_mtx);
1913 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1915 struct ctl_fe_ioctl_startstop_info *sd_info;
1917 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1919 sd_info->hs_info.status = metatask->status;
1920 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1921 sd_info->hs_info.luns_complete =
1922 metatask->taskinfo.startstop.luns_complete;
1923 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1925 cv_broadcast(&sd_info->sem);
1929 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1931 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1933 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1935 mtx_lock(fe_bbr_info->lock);
1936 fe_bbr_info->bbr_info->status = metatask->status;
1937 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1938 fe_bbr_info->wakeup_done = 1;
1939 mtx_unlock(fe_bbr_info->lock);
1941 cv_broadcast(&fe_bbr_info->sem);
1945 * Returns 0 for success, errno for failure.
1948 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1949 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1956 mtx_lock(&lun->lun_lock);
1957 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1958 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1960 struct ctl_ooa_entry *entry;
1963 * If we've got more than we can fit, just count the
1964 * remaining entries.
1966 if (*cur_fill_num >= ooa_hdr->alloc_num)
1969 entry = &kern_entries[*cur_fill_num];
1971 entry->tag_num = io->scsiio.tag_num;
1972 entry->lun_num = lun->lun;
1974 entry->start_bt = io->io_hdr.start_bt;
1976 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
1977 entry->cdb_len = io->scsiio.cdb_len;
1978 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
1979 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
1981 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
1982 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
1984 if (io->io_hdr.flags & CTL_FLAG_ABORT)
1985 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
1987 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
1988 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
1990 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
1991 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
1993 mtx_unlock(&lun->lun_lock);
1999 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2000 size_t error_str_len)
2004 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2006 if (copyin(user_addr, kptr, len) != 0) {
2007 snprintf(error_str, error_str_len, "Error copying %d bytes "
2008 "from user address %p to kernel address %p", len,
2018 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2022 if (be_args == NULL)
2025 for (i = 0; i < num_be_args; i++) {
2026 free(be_args[i].kname, M_CTL);
2027 free(be_args[i].kvalue, M_CTL);
2030 free(be_args, M_CTL);
2033 static struct ctl_be_arg *
2034 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2035 char *error_str, size_t error_str_len)
2037 struct ctl_be_arg *args;
2040 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2041 error_str, error_str_len);
2046 for (i = 0; i < num_be_args; i++) {
2047 args[i].kname = NULL;
2048 args[i].kvalue = NULL;
2051 for (i = 0; i < num_be_args; i++) {
2054 args[i].kname = ctl_copyin_alloc(args[i].name,
2055 args[i].namelen, error_str, error_str_len);
2056 if (args[i].kname == NULL)
2059 if (args[i].kname[args[i].namelen - 1] != '\0') {
2060 snprintf(error_str, error_str_len, "Argument %d "
2061 "name is not NUL-terminated", i);
2065 args[i].kvalue = NULL;
2067 tmpptr = ctl_copyin_alloc(args[i].value,
2068 args[i].vallen, error_str, error_str_len);
2072 args[i].kvalue = tmpptr;
2074 if ((args[i].flags & CTL_BEARG_ASCII)
2075 && (tmpptr[args[i].vallen - 1] != '\0')) {
2076 snprintf(error_str, error_str_len, "Argument %d "
2077 "value is not NUL-terminated", i);
2085 ctl_free_args(num_be_args, args);
2091 * Escape characters that are illegal or not recommended in XML.
2094 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2100 for (; *str; str++) {
2103 retval = sbuf_printf(sb, "&");
2106 retval = sbuf_printf(sb, ">");
2109 retval = sbuf_printf(sb, "<");
2112 retval = sbuf_putc(sb, *str);
2125 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2128 struct ctl_softc *softc;
2131 softc = control_softc;
2141 * If we haven't been "enabled", don't allow any SCSI I/O
2144 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2149 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2151 printf("ctl_ioctl: can't allocate ctl_io!\n");
2157 * Need to save the pool reference so it doesn't get
2158 * spammed by the user's ctl_io.
2160 pool_tmp = io->io_hdr.pool;
2162 memcpy(io, (void *)addr, sizeof(*io));
2164 io->io_hdr.pool = pool_tmp;
2166 * No status yet, so make sure the status is set properly.
2168 io->io_hdr.status = CTL_STATUS_NONE;
2171 * The user sets the initiator ID, target and LUN IDs.
2173 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2174 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2175 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2176 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2177 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2179 retval = ctl_ioctl_submit_wait(io);
2186 memcpy((void *)addr, io, sizeof(*io));
2188 /* return this to our pool */
2193 case CTL_ENABLE_PORT:
2194 case CTL_DISABLE_PORT:
2195 case CTL_SET_PORT_WWNS: {
2196 struct ctl_port *port;
2197 struct ctl_port_entry *entry;
2199 entry = (struct ctl_port_entry *)addr;
2201 mtx_lock(&softc->ctl_lock);
2202 STAILQ_FOREACH(port, &softc->port_list, links) {
2208 if ((entry->port_type == CTL_PORT_NONE)
2209 && (entry->targ_port == port->targ_port)) {
2211 * If the user only wants to enable or
2212 * disable or set WWNs on a specific port,
2213 * do the operation and we're done.
2217 } else if (entry->port_type & port->port_type) {
2219 * Compare the user's type mask with the
2220 * particular frontend type to see if we
2227 * Make sure the user isn't trying to set
2228 * WWNs on multiple ports at the same time.
2230 if (cmd == CTL_SET_PORT_WWNS) {
2231 printf("%s: Can't set WWNs on "
2232 "multiple ports\n", __func__);
2239 * XXX KDM we have to drop the lock here,
2240 * because the online/offline operations
2241 * can potentially block. We need to
2242 * reference count the frontends so they
2245 mtx_unlock(&softc->ctl_lock);
2247 if (cmd == CTL_ENABLE_PORT) {
2248 struct ctl_lun *lun;
2250 STAILQ_FOREACH(lun, &softc->lun_list,
2252 port->lun_enable(port->targ_lun_arg,
2257 ctl_port_online(port);
2258 } else if (cmd == CTL_DISABLE_PORT) {
2259 struct ctl_lun *lun;
2261 ctl_port_offline(port);
2263 STAILQ_FOREACH(lun, &softc->lun_list,
2272 mtx_lock(&softc->ctl_lock);
2274 if (cmd == CTL_SET_PORT_WWNS)
2275 ctl_port_set_wwns(port,
2276 (entry->flags & CTL_PORT_WWNN_VALID) ?
2278 (entry->flags & CTL_PORT_WWPN_VALID) ?
2279 1 : 0, entry->wwpn);
2284 mtx_unlock(&softc->ctl_lock);
2287 case CTL_GET_PORT_LIST: {
2288 struct ctl_port *port;
2289 struct ctl_port_list *list;
2292 list = (struct ctl_port_list *)addr;
2294 if (list->alloc_len != (list->alloc_num *
2295 sizeof(struct ctl_port_entry))) {
2296 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2297 "alloc_num %u * sizeof(struct ctl_port_entry) "
2298 "%zu\n", __func__, list->alloc_len,
2299 list->alloc_num, sizeof(struct ctl_port_entry));
2305 list->dropped_num = 0;
2307 mtx_lock(&softc->ctl_lock);
2308 STAILQ_FOREACH(port, &softc->port_list, links) {
2309 struct ctl_port_entry entry, *list_entry;
2311 if (list->fill_num >= list->alloc_num) {
2312 list->dropped_num++;
2316 entry.port_type = port->port_type;
2317 strlcpy(entry.port_name, port->port_name,
2318 sizeof(entry.port_name));
2319 entry.targ_port = port->targ_port;
2320 entry.physical_port = port->physical_port;
2321 entry.virtual_port = port->virtual_port;
2322 entry.wwnn = port->wwnn;
2323 entry.wwpn = port->wwpn;
2324 if (port->status & CTL_PORT_STATUS_ONLINE)
2329 list_entry = &list->entries[i];
2331 retval = copyout(&entry, list_entry, sizeof(entry));
2333 printf("%s: CTL_GET_PORT_LIST: copyout "
2334 "returned %d\n", __func__, retval);
2339 list->fill_len += sizeof(entry);
2341 mtx_unlock(&softc->ctl_lock);
2344 * If this is non-zero, we had a copyout fault, so there's
2345 * probably no point in attempting to set the status inside
2351 if (list->dropped_num > 0)
2352 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2354 list->status = CTL_PORT_LIST_OK;
2357 case CTL_DUMP_OOA: {
2358 struct ctl_lun *lun;
2363 mtx_lock(&softc->ctl_lock);
2364 printf("Dumping OOA queues:\n");
2365 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2366 mtx_lock(&lun->lun_lock);
2367 for (io = (union ctl_io *)TAILQ_FIRST(
2368 &lun->ooa_queue); io != NULL;
2369 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2371 sbuf_new(&sb, printbuf, sizeof(printbuf),
2373 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2377 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2379 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2381 CTL_FLAG_ABORT) ? " ABORT" : "",
2383 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2384 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2386 printf("%s\n", sbuf_data(&sb));
2388 mtx_unlock(&lun->lun_lock);
2390 printf("OOA queues dump done\n");
2391 mtx_unlock(&softc->ctl_lock);
2395 struct ctl_lun *lun;
2396 struct ctl_ooa *ooa_hdr;
2397 struct ctl_ooa_entry *entries;
2398 uint32_t cur_fill_num;
2400 ooa_hdr = (struct ctl_ooa *)addr;
2402 if ((ooa_hdr->alloc_len == 0)
2403 || (ooa_hdr->alloc_num == 0)) {
2404 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2405 "must be non-zero\n", __func__,
2406 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2411 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2412 sizeof(struct ctl_ooa_entry))) {
2413 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2414 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2415 __func__, ooa_hdr->alloc_len,
2416 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2421 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2422 if (entries == NULL) {
2423 printf("%s: could not allocate %d bytes for OOA "
2424 "dump\n", __func__, ooa_hdr->alloc_len);
2429 mtx_lock(&softc->ctl_lock);
2430 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2431 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2432 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2433 mtx_unlock(&softc->ctl_lock);
2434 free(entries, M_CTL);
2435 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2436 __func__, (uintmax_t)ooa_hdr->lun_num);
2443 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2444 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2445 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2451 mtx_unlock(&softc->ctl_lock);
2452 free(entries, M_CTL);
2456 lun = softc->ctl_luns[ooa_hdr->lun_num];
2458 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2461 mtx_unlock(&softc->ctl_lock);
2463 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2464 ooa_hdr->fill_len = ooa_hdr->fill_num *
2465 sizeof(struct ctl_ooa_entry);
2466 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2468 printf("%s: error copying out %d bytes for OOA dump\n",
2469 __func__, ooa_hdr->fill_len);
2472 getbintime(&ooa_hdr->cur_bt);
2474 if (cur_fill_num > ooa_hdr->alloc_num) {
2475 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2476 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2478 ooa_hdr->dropped_num = 0;
2479 ooa_hdr->status = CTL_OOA_OK;
2482 free(entries, M_CTL);
2485 case CTL_CHECK_OOA: {
2487 struct ctl_lun *lun;
2488 struct ctl_ooa_info *ooa_info;
2491 ooa_info = (struct ctl_ooa_info *)addr;
2493 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2494 ooa_info->status = CTL_OOA_INVALID_LUN;
2497 mtx_lock(&softc->ctl_lock);
2498 lun = softc->ctl_luns[ooa_info->lun_id];
2500 mtx_unlock(&softc->ctl_lock);
2501 ooa_info->status = CTL_OOA_INVALID_LUN;
2504 mtx_lock(&lun->lun_lock);
2505 mtx_unlock(&softc->ctl_lock);
2506 ooa_info->num_entries = 0;
2507 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2508 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2509 &io->io_hdr, ooa_links)) {
2510 ooa_info->num_entries++;
2512 mtx_unlock(&lun->lun_lock);
2514 ooa_info->status = CTL_OOA_SUCCESS;
2518 case CTL_HARD_START:
2519 case CTL_HARD_STOP: {
2520 struct ctl_fe_ioctl_startstop_info ss_info;
2521 struct cfi_metatask *metatask;
2524 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2526 cv_init(&ss_info.sem, "hard start/stop cv" );
2528 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2529 if (metatask == NULL) {
2531 mtx_destroy(&hs_mtx);
2535 if (cmd == CTL_HARD_START)
2536 metatask->tasktype = CFI_TASK_STARTUP;
2538 metatask->tasktype = CFI_TASK_SHUTDOWN;
2540 metatask->callback = ctl_ioctl_hard_startstop_callback;
2541 metatask->callback_arg = &ss_info;
2543 cfi_action(metatask);
2545 /* Wait for the callback */
2547 cv_wait_sig(&ss_info.sem, &hs_mtx);
2548 mtx_unlock(&hs_mtx);
2551 * All information has been copied from the metatask by the
2552 * time cv_broadcast() is called, so we free the metatask here.
2554 cfi_free_metatask(metatask);
2556 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2558 mtx_destroy(&hs_mtx);
2562 struct ctl_bbrread_info *bbr_info;
2563 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2565 struct cfi_metatask *metatask;
2567 bbr_info = (struct ctl_bbrread_info *)addr;
2569 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2571 bzero(&bbr_mtx, sizeof(bbr_mtx));
2572 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2574 fe_bbr_info.bbr_info = bbr_info;
2575 fe_bbr_info.lock = &bbr_mtx;
2577 cv_init(&fe_bbr_info.sem, "BBR read cv");
2578 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2580 if (metatask == NULL) {
2581 mtx_destroy(&bbr_mtx);
2582 cv_destroy(&fe_bbr_info.sem);
2586 metatask->tasktype = CFI_TASK_BBRREAD;
2587 metatask->callback = ctl_ioctl_bbrread_callback;
2588 metatask->callback_arg = &fe_bbr_info;
2589 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2590 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2591 metatask->taskinfo.bbrread.len = bbr_info->len;
2593 cfi_action(metatask);
2596 while (fe_bbr_info.wakeup_done == 0)
2597 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2598 mtx_unlock(&bbr_mtx);
2600 bbr_info->status = metatask->status;
2601 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2602 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2603 memcpy(&bbr_info->sense_data,
2604 &metatask->taskinfo.bbrread.sense_data,
2605 ctl_min(sizeof(bbr_info->sense_data),
2606 sizeof(metatask->taskinfo.bbrread.sense_data)));
2608 cfi_free_metatask(metatask);
2610 mtx_destroy(&bbr_mtx);
2611 cv_destroy(&fe_bbr_info.sem);
2615 case CTL_DELAY_IO: {
2616 struct ctl_io_delay_info *delay_info;
2618 struct ctl_lun *lun;
2619 #endif /* CTL_IO_DELAY */
2621 delay_info = (struct ctl_io_delay_info *)addr;
2624 mtx_lock(&softc->ctl_lock);
2626 if ((delay_info->lun_id > CTL_MAX_LUNS)
2627 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2628 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2630 lun = softc->ctl_luns[delay_info->lun_id];
2631 mtx_lock(&lun->lun_lock);
2633 delay_info->status = CTL_DELAY_STATUS_OK;
2635 switch (delay_info->delay_type) {
2636 case CTL_DELAY_TYPE_CONT:
2638 case CTL_DELAY_TYPE_ONESHOT:
2641 delay_info->status =
2642 CTL_DELAY_STATUS_INVALID_TYPE;
2646 switch (delay_info->delay_loc) {
2647 case CTL_DELAY_LOC_DATAMOVE:
2648 lun->delay_info.datamove_type =
2649 delay_info->delay_type;
2650 lun->delay_info.datamove_delay =
2651 delay_info->delay_secs;
2653 case CTL_DELAY_LOC_DONE:
2654 lun->delay_info.done_type =
2655 delay_info->delay_type;
2656 lun->delay_info.done_delay =
2657 delay_info->delay_secs;
2660 delay_info->status =
2661 CTL_DELAY_STATUS_INVALID_LOC;
2664 mtx_unlock(&lun->lun_lock);
2667 mtx_unlock(&softc->ctl_lock);
2669 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2670 #endif /* CTL_IO_DELAY */
2673 case CTL_REALSYNC_SET: {
2676 syncstate = (int *)addr;
2678 mtx_lock(&softc->ctl_lock);
2679 switch (*syncstate) {
2681 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2684 softc->flags |= CTL_FLAG_REAL_SYNC;
2690 mtx_unlock(&softc->ctl_lock);
2693 case CTL_REALSYNC_GET: {
2696 syncstate = (int*)addr;
2698 mtx_lock(&softc->ctl_lock);
2699 if (softc->flags & CTL_FLAG_REAL_SYNC)
2703 mtx_unlock(&softc->ctl_lock);
2709 struct ctl_sync_info *sync_info;
2710 struct ctl_lun *lun;
2712 sync_info = (struct ctl_sync_info *)addr;
2714 mtx_lock(&softc->ctl_lock);
2715 lun = softc->ctl_luns[sync_info->lun_id];
2717 mtx_unlock(&softc->ctl_lock);
2718 sync_info->status = CTL_GS_SYNC_NO_LUN;
2721 * Get or set the sync interval. We're not bounds checking
2722 * in the set case, hopefully the user won't do something
2725 mtx_lock(&lun->lun_lock);
2726 mtx_unlock(&softc->ctl_lock);
2727 if (cmd == CTL_GETSYNC)
2728 sync_info->sync_interval = lun->sync_interval;
2730 lun->sync_interval = sync_info->sync_interval;
2731 mtx_unlock(&lun->lun_lock);
2733 sync_info->status = CTL_GS_SYNC_OK;
2737 case CTL_GETSTATS: {
2738 struct ctl_stats *stats;
2739 struct ctl_lun *lun;
2742 stats = (struct ctl_stats *)addr;
2744 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2746 stats->status = CTL_SS_NEED_MORE_SPACE;
2747 stats->num_luns = softc->num_luns;
2751 * XXX KDM no locking here. If the LUN list changes,
2752 * things can blow up.
2754 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2755 i++, lun = STAILQ_NEXT(lun, links)) {
2756 retval = copyout(&lun->stats, &stats->lun_stats[i],
2757 sizeof(lun->stats));
2761 stats->num_luns = softc->num_luns;
2762 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2764 stats->status = CTL_SS_OK;
2766 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2768 stats->flags = CTL_STATS_FLAG_NONE;
2770 getnanouptime(&stats->timestamp);
2773 case CTL_ERROR_INJECT: {
2774 struct ctl_error_desc *err_desc, *new_err_desc;
2775 struct ctl_lun *lun;
2777 err_desc = (struct ctl_error_desc *)addr;
2779 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2781 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2783 mtx_lock(&softc->ctl_lock);
2784 lun = softc->ctl_luns[err_desc->lun_id];
2786 mtx_unlock(&softc->ctl_lock);
2787 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2788 __func__, (uintmax_t)err_desc->lun_id);
2792 mtx_lock(&lun->lun_lock);
2793 mtx_unlock(&softc->ctl_lock);
2796 * We could do some checking here to verify the validity
2797 * of the request, but given the complexity of error
2798 * injection requests, the checking logic would be fairly
2801 * For now, if the request is invalid, it just won't get
2802 * executed and might get deleted.
2804 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2807 * XXX KDM check to make sure the serial number is unique,
2808 * in case we somehow manage to wrap. That shouldn't
2809 * happen for a very long time, but it's the right thing to
2812 new_err_desc->serial = lun->error_serial;
2813 err_desc->serial = lun->error_serial;
2814 lun->error_serial++;
2816 mtx_unlock(&lun->lun_lock);
2819 case CTL_ERROR_INJECT_DELETE: {
2820 struct ctl_error_desc *delete_desc, *desc, *desc2;
2821 struct ctl_lun *lun;
2824 delete_desc = (struct ctl_error_desc *)addr;
2827 mtx_lock(&softc->ctl_lock);
2828 lun = softc->ctl_luns[delete_desc->lun_id];
2830 mtx_unlock(&softc->ctl_lock);
2831 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2832 __func__, (uintmax_t)delete_desc->lun_id);
2836 mtx_lock(&lun->lun_lock);
2837 mtx_unlock(&softc->ctl_lock);
2838 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2839 if (desc->serial != delete_desc->serial)
2842 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2847 mtx_unlock(&lun->lun_lock);
2848 if (delete_done == 0) {
2849 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2850 "error serial %ju on LUN %u\n", __func__,
2851 delete_desc->serial, delete_desc->lun_id);
2857 case CTL_DUMP_STRUCTS: {
2859 struct ctl_port *port;
2860 struct ctl_frontend *fe;
2862 printf("CTL IID to WWPN map start:\n");
2863 for (i = 0; i < CTL_MAX_PORTS; i++) {
2864 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2865 if (softc->wwpn_iid[i][j].in_use == 0)
2868 printf("port %d iid %u WWPN %#jx\n",
2869 softc->wwpn_iid[i][j].port,
2870 softc->wwpn_iid[i][j].iid,
2871 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2874 printf("CTL IID to WWPN map end\n");
2875 printf("CTL Persistent Reservation information start:\n");
2876 for (i = 0; i < CTL_MAX_LUNS; i++) {
2877 struct ctl_lun *lun;
2879 lun = softc->ctl_luns[i];
2882 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2885 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2886 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2887 if (lun->per_res[j+k].registered == 0)
2889 printf("LUN %d port %d iid %d key "
2891 (uintmax_t)scsi_8btou64(
2892 lun->per_res[j+k].res_key.key));
2896 printf("CTL Persistent Reservation information end\n");
2897 printf("CTL Ports:\n");
2899 * XXX KDM calling this without a lock. We'd likely want
2900 * to drop the lock before calling the frontend's dump
2903 STAILQ_FOREACH(port, &softc->port_list, links) {
2904 printf("Port %s Frontend %s Type %u pport %d vport %d WWNN "
2905 "%#jx WWPN %#jx\n", port->port_name,
2906 port->frontend->name, port->port_type,
2907 port->physical_port, port->virtual_port,
2908 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2910 printf("CTL Port information end\n");
2911 printf("CTL Frontends:\n");
2912 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2913 printf("Frontend %s\n", fe->name);
2914 if (fe->fe_dump != NULL)
2917 printf("CTL Frontend information end\n");
2921 struct ctl_lun_req *lun_req;
2922 struct ctl_backend_driver *backend;
2924 lun_req = (struct ctl_lun_req *)addr;
2926 backend = ctl_backend_find(lun_req->backend);
2927 if (backend == NULL) {
2928 lun_req->status = CTL_LUN_ERROR;
2929 snprintf(lun_req->error_str,
2930 sizeof(lun_req->error_str),
2931 "Backend \"%s\" not found.",
2935 if (lun_req->num_be_args > 0) {
2936 lun_req->kern_be_args = ctl_copyin_args(
2937 lun_req->num_be_args,
2940 sizeof(lun_req->error_str));
2941 if (lun_req->kern_be_args == NULL) {
2942 lun_req->status = CTL_LUN_ERROR;
2947 retval = backend->ioctl(dev, cmd, addr, flag, td);
2949 if (lun_req->num_be_args > 0) {
2950 ctl_free_args(lun_req->num_be_args,
2951 lun_req->kern_be_args);
2955 case CTL_LUN_LIST: {
2957 struct ctl_lun *lun;
2958 struct ctl_lun_list *list;
2959 struct ctl_option *opt;
2961 list = (struct ctl_lun_list *)addr;
2964 * Allocate a fixed length sbuf here, based on the length
2965 * of the user's buffer. We could allocate an auto-extending
2966 * buffer, and then tell the user how much larger our
2967 * amount of data is than his buffer, but that presents
2970 * 1. The sbuf(9) routines use a blocking malloc, and so
2971 * we can't hold a lock while calling them with an
2972 * auto-extending buffer.
2974 * 2. There is not currently a LUN reference counting
2975 * mechanism, outside of outstanding transactions on
2976 * the LUN's OOA queue. So a LUN could go away on us
2977 * while we're getting the LUN number, backend-specific
2978 * information, etc. Thus, given the way things
2979 * currently work, we need to hold the CTL lock while
2980 * grabbing LUN information.
2982 * So, from the user's standpoint, the best thing to do is
2983 * allocate what he thinks is a reasonable buffer length,
2984 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2985 * double the buffer length and try again. (And repeat
2986 * that until he succeeds.)
2988 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
2990 list->status = CTL_LUN_LIST_ERROR;
2991 snprintf(list->error_str, sizeof(list->error_str),
2992 "Unable to allocate %d bytes for LUN list",
2997 sbuf_printf(sb, "<ctllunlist>\n");
2999 mtx_lock(&softc->ctl_lock);
3000 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3001 mtx_lock(&lun->lun_lock);
3002 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3003 (uintmax_t)lun->lun);
3006 * Bail out as soon as we see that we've overfilled
3012 retval = sbuf_printf(sb, "\t<backend_type>%s"
3013 "</backend_type>\n",
3014 (lun->backend == NULL) ? "none" :
3015 lun->backend->name);
3020 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3021 lun->be_lun->lun_type);
3026 if (lun->backend == NULL) {
3027 retval = sbuf_printf(sb, "</lun>\n");
3033 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3034 (lun->be_lun->maxlba > 0) ?
3035 lun->be_lun->maxlba + 1 : 0);
3040 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3041 lun->be_lun->blocksize);
3046 retval = sbuf_printf(sb, "\t<serial_number>");
3051 retval = ctl_sbuf_printf_esc(sb,
3052 lun->be_lun->serial_num);
3057 retval = sbuf_printf(sb, "</serial_number>\n");
3062 retval = sbuf_printf(sb, "\t<device_id>");
3067 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3072 retval = sbuf_printf(sb, "</device_id>\n");
3077 if (lun->backend->lun_info != NULL) {
3078 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3082 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3083 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3084 opt->name, opt->value, opt->name);
3089 retval = sbuf_printf(sb, "</lun>\n");
3093 mtx_unlock(&lun->lun_lock);
3096 mtx_unlock(&lun->lun_lock);
3097 mtx_unlock(&softc->ctl_lock);
3100 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3103 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3104 snprintf(list->error_str, sizeof(list->error_str),
3105 "Out of space, %d bytes is too small",
3112 retval = copyout(sbuf_data(sb), list->lun_xml,
3115 list->fill_len = sbuf_len(sb) + 1;
3116 list->status = CTL_LUN_LIST_OK;
3121 struct ctl_iscsi *ci;
3122 struct ctl_frontend *fe;
3124 ci = (struct ctl_iscsi *)addr;
3126 mtx_lock(&softc->ctl_lock);
3127 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3128 if (strcmp(fe->name, "iscsi") == 0)
3131 mtx_unlock(&softc->ctl_lock);
3134 ci->status = CTL_ISCSI_ERROR;
3135 snprintf(ci->error_str, sizeof(ci->error_str),
3136 "Frontend \"iscsi\" not found.");
3140 retval = fe->ioctl(dev, cmd, addr, flag, td);
3144 /* XXX KDM should we fix this? */
3146 struct ctl_backend_driver *backend;
3153 * We encode the backend type as the ioctl type for backend
3154 * ioctls. So parse it out here, and then search for a
3155 * backend of this type.
3157 type = _IOC_TYPE(cmd);
3159 STAILQ_FOREACH(backend, &softc->be_list, links) {
3160 if (backend->type == type) {
3166 printf("ctl: unknown ioctl command %#lx or backend "
3171 retval = backend->ioctl(dev, cmd, addr, flag, td);
3181 ctl_get_initindex(struct ctl_nexus *nexus)
3183 if (nexus->targ_port < CTL_MAX_PORTS)
3184 return (nexus->initid.id +
3185 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3187 return (nexus->initid.id +
3188 ((nexus->targ_port - CTL_MAX_PORTS) *
3189 CTL_MAX_INIT_PER_PORT));
3193 ctl_get_resindex(struct ctl_nexus *nexus)
3195 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3199 ctl_port_idx(int port_num)
3201 if (port_num < CTL_MAX_PORTS)
3204 return(port_num - CTL_MAX_PORTS);
3208 * Note: This only works for bitmask sizes that are at least 32 bits, and
3209 * that are a power of 2.
3212 ctl_ffz(uint32_t *mask, uint32_t size)
3214 uint32_t num_chunks, num_pieces;
3217 num_chunks = (size >> 5);
3218 if (num_chunks == 0)
3220 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3222 for (i = 0; i < num_chunks; i++) {
3223 for (j = 0; j < num_pieces; j++) {
3224 if ((mask[i] & (1 << j)) == 0)
3225 return ((i << 5) + j);
3233 ctl_set_mask(uint32_t *mask, uint32_t bit)
3235 uint32_t chunk, piece;
3238 piece = bit % (sizeof(uint32_t) * 8);
3240 if ((mask[chunk] & (1 << piece)) != 0)
3243 mask[chunk] |= (1 << piece);
3249 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3251 uint32_t chunk, piece;
3254 piece = bit % (sizeof(uint32_t) * 8);
3256 if ((mask[chunk] & (1 << piece)) == 0)
3259 mask[chunk] &= ~(1 << piece);
3265 ctl_is_set(uint32_t *mask, uint32_t bit)
3267 uint32_t chunk, piece;
3270 piece = bit % (sizeof(uint32_t) * 8);
3272 if ((mask[chunk] & (1 << piece)) == 0)
3280 * The bus, target and lun are optional, they can be filled in later.
3281 * can_wait is used to determine whether we can wait on the malloc or not.
3284 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3285 uint32_t targ_lun, int can_wait)
3290 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3292 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3295 io->io_hdr.io_type = io_type;
3296 io->io_hdr.targ_port = targ_port;
3298 * XXX KDM this needs to change/go away. We need to move
3299 * to a preallocated pool of ctl_scsiio structures.
3301 io->io_hdr.nexus.targ_target.id = targ_target;
3302 io->io_hdr.nexus.targ_lun = targ_lun;
3309 ctl_kfree_io(union ctl_io *io)
3316 * ctl_softc, pool_type, total_ctl_io are passed in.
3317 * npool is passed out.
3320 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3321 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3324 union ctl_io *cur_io, *next_io;
3325 struct ctl_io_pool *pool;
3330 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3337 pool->type = pool_type;
3338 pool->ctl_softc = ctl_softc;
3340 mtx_lock(&ctl_softc->pool_lock);
3341 pool->id = ctl_softc->cur_pool_id++;
3342 mtx_unlock(&ctl_softc->pool_lock);
3344 pool->flags = CTL_POOL_FLAG_NONE;
3345 pool->refcount = 1; /* Reference for validity. */
3346 STAILQ_INIT(&pool->free_queue);
3349 * XXX KDM other options here:
3350 * - allocate a page at a time
3351 * - allocate one big chunk of memory.
3352 * Page allocation might work well, but would take a little more
3355 for (i = 0; i < total_ctl_io; i++) {
3356 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3358 if (cur_io == NULL) {
3362 cur_io->io_hdr.pool = pool;
3363 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3364 pool->total_ctl_io++;
3365 pool->free_ctl_io++;
3369 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3370 cur_io != NULL; cur_io = next_io) {
3371 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3373 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3375 free(cur_io, M_CTLIO);
3381 mtx_lock(&ctl_softc->pool_lock);
3382 ctl_softc->num_pools++;
3383 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3385 * Increment our usage count if this is an external consumer, so we
3386 * can't get unloaded until the external consumer (most likely a
3387 * FETD) unloads and frees his pool.
3389 * XXX KDM will this increment the caller's module use count, or
3393 if ((pool_type != CTL_POOL_EMERGENCY)
3394 && (pool_type != CTL_POOL_INTERNAL)
3395 && (pool_type != CTL_POOL_4OTHERSC))
3399 mtx_unlock(&ctl_softc->pool_lock);
3409 ctl_pool_acquire(struct ctl_io_pool *pool)
3412 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3414 if (pool->flags & CTL_POOL_FLAG_INVALID)
3423 ctl_pool_release(struct ctl_io_pool *pool)
3425 struct ctl_softc *ctl_softc = pool->ctl_softc;
3428 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3430 if (--pool->refcount != 0)
3433 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3434 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3439 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3440 ctl_softc->num_pools--;
3443 * XXX KDM will this decrement the caller's usage count or mine?
3446 if ((pool->type != CTL_POOL_EMERGENCY)
3447 && (pool->type != CTL_POOL_INTERNAL)
3448 && (pool->type != CTL_POOL_4OTHERSC))
3456 ctl_pool_free(struct ctl_io_pool *pool)
3458 struct ctl_softc *ctl_softc;
3463 ctl_softc = pool->ctl_softc;
3464 mtx_lock(&ctl_softc->pool_lock);
3465 pool->flags |= CTL_POOL_FLAG_INVALID;
3466 ctl_pool_release(pool);
3467 mtx_unlock(&ctl_softc->pool_lock);
3471 * This routine does not block (except for spinlocks of course).
3472 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3476 ctl_alloc_io(void *pool_ref)
3479 struct ctl_softc *ctl_softc;
3480 struct ctl_io_pool *pool, *npool;
3481 struct ctl_io_pool *emergency_pool;
3483 pool = (struct ctl_io_pool *)pool_ref;
3486 printf("%s: pool is NULL\n", __func__);
3490 emergency_pool = NULL;
3492 ctl_softc = pool->ctl_softc;
3494 mtx_lock(&ctl_softc->pool_lock);
3496 * First, try to get the io structure from the user's pool.
3498 if (ctl_pool_acquire(pool) == 0) {
3499 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3501 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3502 pool->total_allocated++;
3503 pool->free_ctl_io--;
3504 mtx_unlock(&ctl_softc->pool_lock);
3507 ctl_pool_release(pool);
3510 * If he doesn't have any io structures left, search for an
3511 * emergency pool and grab one from there.
3513 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3514 if (npool->type != CTL_POOL_EMERGENCY)
3517 if (ctl_pool_acquire(npool) != 0)
3520 emergency_pool = npool;
3522 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3524 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3525 npool->total_allocated++;
3526 npool->free_ctl_io--;
3527 mtx_unlock(&ctl_softc->pool_lock);
3530 ctl_pool_release(npool);
3533 /* Drop the spinlock before we malloc */
3534 mtx_unlock(&ctl_softc->pool_lock);
3537 * The emergency pool (if it exists) didn't have one, so try an
3538 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3540 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3543 * If the emergency pool exists but is empty, add this
3544 * ctl_io to its list when it gets freed.
3546 if (emergency_pool != NULL) {
3547 mtx_lock(&ctl_softc->pool_lock);
3548 if (ctl_pool_acquire(emergency_pool) == 0) {
3549 io->io_hdr.pool = emergency_pool;
3550 emergency_pool->total_ctl_io++;
3552 * Need to bump this, otherwise
3553 * total_allocated and total_freed won't
3554 * match when we no longer have anything
3557 emergency_pool->total_allocated++;
3559 mtx_unlock(&ctl_softc->pool_lock);
3561 io->io_hdr.pool = NULL;
3568 ctl_free_io(union ctl_io *io)
3574 * If this ctl_io has a pool, return it to that pool.
3576 if (io->io_hdr.pool != NULL) {
3577 struct ctl_io_pool *pool;
3579 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3580 mtx_lock(&pool->ctl_softc->pool_lock);
3581 io->io_hdr.io_type = 0xff;
3582 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3583 pool->total_freed++;
3584 pool->free_ctl_io++;
3585 ctl_pool_release(pool);
3586 mtx_unlock(&pool->ctl_softc->pool_lock);
3589 * Otherwise, just free it. We probably malloced it and
3590 * the emergency pool wasn't available.
3598 ctl_zero_io(union ctl_io *io)
3606 * May need to preserve linked list pointers at some point too.
3608 pool_ref = io->io_hdr.pool;
3610 memset(io, 0, sizeof(*io));
3612 io->io_hdr.pool = pool_ref;
3616 * This routine is currently used for internal copies of ctl_ios that need
3617 * to persist for some reason after we've already returned status to the
3618 * FETD. (Thus the flag set.)
3621 * Note that this makes a blind copy of all fields in the ctl_io, except
3622 * for the pool reference. This includes any memory that has been
3623 * allocated! That memory will no longer be valid after done has been
3624 * called, so this would be VERY DANGEROUS for command that actually does
3625 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3626 * start and stop commands, which don't transfer any data, so this is not a
3627 * problem. If it is used for anything else, the caller would also need to
3628 * allocate data buffer space and this routine would need to be modified to
3629 * copy the data buffer(s) as well.
3632 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3641 * May need to preserve linked list pointers at some point too.
3643 pool_ref = dest->io_hdr.pool;
3645 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3647 dest->io_hdr.pool = pool_ref;
3649 * We need to know that this is an internal copy, and doesn't need
3650 * to get passed back to the FETD that allocated it.
3652 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3657 ctl_update_power_subpage(struct copan_power_subpage *page)
3659 int num_luns, num_partitions, config_type;
3660 struct ctl_softc *softc;
3661 cs_BOOL_t aor_present, shelf_50pct_power;
3662 cs_raidset_personality_t rs_type;
3663 int max_active_luns;
3665 softc = control_softc;
3667 /* subtract out the processor LUN */
3668 num_luns = softc->num_luns - 1;
3670 * Default to 7 LUNs active, which was the only number we allowed
3673 max_active_luns = 7;
3675 num_partitions = config_GetRsPartitionInfo();
3676 config_type = config_GetConfigType();
3677 shelf_50pct_power = config_GetShelfPowerMode();
3678 aor_present = config_IsAorRsPresent();
3680 rs_type = ddb_GetRsRaidType(1);
3681 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3682 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3683 EPRINT(0, "Unsupported RS type %d!", rs_type);
3687 page->total_luns = num_luns;
3689 switch (config_type) {
3692 * In a 40 drive configuration, it doesn't matter what DC
3693 * cards we have, whether we have AOR enabled or not,
3694 * partitioning or not, or what type of RAIDset we have.
3695 * In that scenario, we can power up every LUN we present
3698 max_active_luns = num_luns;
3702 if (shelf_50pct_power == CS_FALSE) {
3704 if (aor_present == CS_TRUE) {
3706 CS_RAIDSET_PERSONALITY_RAID5) {
3707 max_active_luns = 7;
3708 } else if (rs_type ==
3709 CS_RAIDSET_PERSONALITY_RAID1){
3710 max_active_luns = 14;
3712 /* XXX KDM now what?? */
3716 CS_RAIDSET_PERSONALITY_RAID5) {
3717 max_active_luns = 8;
3718 } else if (rs_type ==
3719 CS_RAIDSET_PERSONALITY_RAID1){
3720 max_active_luns = 16;
3722 /* XXX KDM now what?? */
3728 * With 50% power in a 64 drive configuration, we
3729 * can power all LUNs we present.
3731 max_active_luns = num_luns;
3735 if (shelf_50pct_power == CS_FALSE) {
3737 if (aor_present == CS_TRUE) {
3739 CS_RAIDSET_PERSONALITY_RAID5) {
3740 max_active_luns = 7;
3741 } else if (rs_type ==
3742 CS_RAIDSET_PERSONALITY_RAID1){
3743 max_active_luns = 14;
3745 /* XXX KDM now what?? */
3749 CS_RAIDSET_PERSONALITY_RAID5) {
3750 max_active_luns = 8;
3751 } else if (rs_type ==
3752 CS_RAIDSET_PERSONALITY_RAID1){
3753 max_active_luns = 16;
3755 /* XXX KDM now what?? */
3760 if (aor_present == CS_TRUE) {
3762 CS_RAIDSET_PERSONALITY_RAID5) {
3763 max_active_luns = 14;
3764 } else if (rs_type ==
3765 CS_RAIDSET_PERSONALITY_RAID1){
3767 * We're assuming here that disk
3768 * caching is enabled, and so we're
3769 * able to power up half of each
3770 * LUN, and cache all writes.
3772 max_active_luns = num_luns;
3774 /* XXX KDM now what?? */
3778 CS_RAIDSET_PERSONALITY_RAID5) {
3779 max_active_luns = 15;
3780 } else if (rs_type ==
3781 CS_RAIDSET_PERSONALITY_RAID1){
3782 max_active_luns = 30;
3784 /* XXX KDM now what?? */
3791 * In this case, we have an unknown configuration, so we
3792 * just use the default from above.
3797 page->max_active_luns = max_active_luns;
3799 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3800 page->total_luns, page->max_active_luns);
3803 #endif /* NEEDTOPORT */
3806 * This routine could be used in the future to load default and/or saved
3807 * mode page parameters for a particuar lun.
3810 ctl_init_page_index(struct ctl_lun *lun)
3813 struct ctl_page_index *page_index;
3814 struct ctl_softc *softc;
3816 memcpy(&lun->mode_pages.index, page_index_template,
3817 sizeof(page_index_template));
3819 softc = lun->ctl_softc;
3821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3823 page_index = &lun->mode_pages.index[i];
3825 * If this is a disk-only mode page, there's no point in
3826 * setting it up. For some pages, we have to have some
3827 * basic information about the disk in order to calculate the
3830 if ((lun->be_lun->lun_type != T_DIRECT)
3831 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3834 switch (page_index->page_code & SMPH_PC_MASK) {
3835 case SMS_FORMAT_DEVICE_PAGE: {
3836 struct scsi_format_page *format_page;
3838 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3839 panic("subpage is incorrect!");
3842 * Sectors per track are set above. Bytes per
3843 * sector need to be set here on a per-LUN basis.
3845 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3846 &format_page_default,
3847 sizeof(format_page_default));
3848 memcpy(&lun->mode_pages.format_page[
3849 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3850 sizeof(format_page_changeable));
3851 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3852 &format_page_default,
3853 sizeof(format_page_default));
3854 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3855 &format_page_default,
3856 sizeof(format_page_default));
3858 format_page = &lun->mode_pages.format_page[
3860 scsi_ulto2b(lun->be_lun->blocksize,
3861 format_page->bytes_per_sector);
3863 format_page = &lun->mode_pages.format_page[
3865 scsi_ulto2b(lun->be_lun->blocksize,
3866 format_page->bytes_per_sector);
3868 format_page = &lun->mode_pages.format_page[
3870 scsi_ulto2b(lun->be_lun->blocksize,
3871 format_page->bytes_per_sector);
3873 page_index->page_data =
3874 (uint8_t *)lun->mode_pages.format_page;
3877 case SMS_RIGID_DISK_PAGE: {
3878 struct scsi_rigid_disk_page *rigid_disk_page;
3879 uint32_t sectors_per_cylinder;
3883 #endif /* !__XSCALE__ */
3885 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3886 panic("invalid subpage value %d",
3887 page_index->subpage);
3890 * Rotation rate and sectors per track are set
3891 * above. We calculate the cylinders here based on
3892 * capacity. Due to the number of heads and
3893 * sectors per track we're using, smaller arrays
3894 * may turn out to have 0 cylinders. Linux and
3895 * FreeBSD don't pay attention to these mode pages
3896 * to figure out capacity, but Solaris does. It
3897 * seems to deal with 0 cylinders just fine, and
3898 * works out a fake geometry based on the capacity.
3900 memcpy(&lun->mode_pages.rigid_disk_page[
3901 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3902 sizeof(rigid_disk_page_default));
3903 memcpy(&lun->mode_pages.rigid_disk_page[
3904 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3905 sizeof(rigid_disk_page_changeable));
3906 memcpy(&lun->mode_pages.rigid_disk_page[
3907 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3908 sizeof(rigid_disk_page_default));
3909 memcpy(&lun->mode_pages.rigid_disk_page[
3910 CTL_PAGE_SAVED], &rigid_disk_page_default,
3911 sizeof(rigid_disk_page_default));
3913 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3917 * The divide method here will be more accurate,
3918 * probably, but results in floating point being
3919 * used in the kernel on i386 (__udivdi3()). On the
3920 * XScale, though, __udivdi3() is implemented in
3923 * The shift method for cylinder calculation is
3924 * accurate if sectors_per_cylinder is a power of
3925 * 2. Otherwise it might be slightly off -- you
3926 * might have a bit of a truncation problem.
3929 cylinders = (lun->be_lun->maxlba + 1) /
3930 sectors_per_cylinder;
3932 for (shift = 31; shift > 0; shift--) {
3933 if (sectors_per_cylinder & (1 << shift))
3936 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3940 * We've basically got 3 bytes, or 24 bits for the
3941 * cylinder size in the mode page. If we're over,
3942 * just round down to 2^24.
3944 if (cylinders > 0xffffff)
3945 cylinders = 0xffffff;
3947 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3949 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3951 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3953 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3955 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3957 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3959 page_index->page_data =
3960 (uint8_t *)lun->mode_pages.rigid_disk_page;
3963 case SMS_CACHING_PAGE: {
3965 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3966 panic("invalid subpage value %d",
3967 page_index->subpage);
3969 * Defaults should be okay here, no calculations
3972 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
3973 &caching_page_default,
3974 sizeof(caching_page_default));
3975 memcpy(&lun->mode_pages.caching_page[
3976 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3977 sizeof(caching_page_changeable));
3978 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3979 &caching_page_default,
3980 sizeof(caching_page_default));
3981 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
3982 &caching_page_default,
3983 sizeof(caching_page_default));
3984 page_index->page_data =
3985 (uint8_t *)lun->mode_pages.caching_page;
3988 case SMS_CONTROL_MODE_PAGE: {
3990 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3991 panic("invalid subpage value %d",
3992 page_index->subpage);
3995 * Defaults should be okay here, no calculations
3998 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
3999 &control_page_default,
4000 sizeof(control_page_default));
4001 memcpy(&lun->mode_pages.control_page[
4002 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4003 sizeof(control_page_changeable));
4004 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4005 &control_page_default,
4006 sizeof(control_page_default));
4007 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4008 &control_page_default,
4009 sizeof(control_page_default));
4010 page_index->page_data =
4011 (uint8_t *)lun->mode_pages.control_page;
4015 case SMS_VENDOR_SPECIFIC_PAGE:{
4016 switch (page_index->subpage) {
4017 case PWR_SUBPAGE_CODE: {
4018 struct copan_power_subpage *current_page,
4021 memcpy(&lun->mode_pages.power_subpage[
4023 &power_page_default,
4024 sizeof(power_page_default));
4025 memcpy(&lun->mode_pages.power_subpage[
4026 CTL_PAGE_CHANGEABLE],
4027 &power_page_changeable,
4028 sizeof(power_page_changeable));
4029 memcpy(&lun->mode_pages.power_subpage[
4031 &power_page_default,
4032 sizeof(power_page_default));
4033 memcpy(&lun->mode_pages.power_subpage[
4035 &power_page_default,
4036 sizeof(power_page_default));
4037 page_index->page_data =
4038 (uint8_t *)lun->mode_pages.power_subpage;
4040 current_page = (struct copan_power_subpage *)
4041 (page_index->page_data +
4042 (page_index->page_len *
4044 saved_page = (struct copan_power_subpage *)
4045 (page_index->page_data +
4046 (page_index->page_len *
4050 case APS_SUBPAGE_CODE: {
4051 struct copan_aps_subpage *current_page,
4054 // This gets set multiple times but
4055 // it should always be the same. It's
4056 // only done during init so who cares.
4057 index_to_aps_page = i;
4059 memcpy(&lun->mode_pages.aps_subpage[
4062 sizeof(aps_page_default));
4063 memcpy(&lun->mode_pages.aps_subpage[
4064 CTL_PAGE_CHANGEABLE],
4065 &aps_page_changeable,
4066 sizeof(aps_page_changeable));
4067 memcpy(&lun->mode_pages.aps_subpage[
4070 sizeof(aps_page_default));
4071 memcpy(&lun->mode_pages.aps_subpage[
4074 sizeof(aps_page_default));
4075 page_index->page_data =
4076 (uint8_t *)lun->mode_pages.aps_subpage;
4078 current_page = (struct copan_aps_subpage *)
4079 (page_index->page_data +
4080 (page_index->page_len *
4082 saved_page = (struct copan_aps_subpage *)
4083 (page_index->page_data +
4084 (page_index->page_len *
4088 case DBGCNF_SUBPAGE_CODE: {
4089 struct copan_debugconf_subpage *current_page,
4092 memcpy(&lun->mode_pages.debugconf_subpage[
4094 &debugconf_page_default,
4095 sizeof(debugconf_page_default));
4096 memcpy(&lun->mode_pages.debugconf_subpage[
4097 CTL_PAGE_CHANGEABLE],
4098 &debugconf_page_changeable,
4099 sizeof(debugconf_page_changeable));
4100 memcpy(&lun->mode_pages.debugconf_subpage[
4102 &debugconf_page_default,
4103 sizeof(debugconf_page_default));
4104 memcpy(&lun->mode_pages.debugconf_subpage[
4106 &debugconf_page_default,
4107 sizeof(debugconf_page_default));
4108 page_index->page_data =
4109 (uint8_t *)lun->mode_pages.debugconf_subpage;
4111 current_page = (struct copan_debugconf_subpage *)
4112 (page_index->page_data +
4113 (page_index->page_len *
4115 saved_page = (struct copan_debugconf_subpage *)
4116 (page_index->page_data +
4117 (page_index->page_len *
4122 panic("invalid subpage value %d",
4123 page_index->subpage);
4129 panic("invalid page value %d",
4130 page_index->page_code & SMPH_PC_MASK);
4135 return (CTL_RETVAL_COMPLETE);
4142 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4143 * wants us to allocate the LUN and he can block.
4144 * - ctl_softc is always set
4145 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4147 * Returns 0 for success, non-zero (errno) for failure.
4150 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4151 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4153 struct ctl_lun *nlun, *lun;
4154 struct ctl_port *port;
4155 int lun_number, i, lun_malloced;
4161 * We currently only support Direct Access or Processor LUN types.
4163 switch (be_lun->lun_type) {
4171 be_lun->lun_config_status(be_lun->be_lun,
4172 CTL_LUN_CONFIG_FAILURE);
4175 if (ctl_lun == NULL) {
4176 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4183 memset(lun, 0, sizeof(*lun));
4185 lun->flags = CTL_LUN_MALLOCED;
4187 mtx_lock(&ctl_softc->ctl_lock);
4189 * See if the caller requested a particular LUN number. If so, see
4190 * if it is available. Otherwise, allocate the first available LUN.
4192 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4193 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4194 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4195 mtx_unlock(&ctl_softc->ctl_lock);
4196 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4197 printf("ctl: requested LUN ID %d is higher "
4198 "than CTL_MAX_LUNS - 1 (%d)\n",
4199 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4202 * XXX KDM return an error, or just assign
4203 * another LUN ID in this case??
4205 printf("ctl: requested LUN ID %d is already "
4206 "in use\n", be_lun->req_lun_id);
4208 if (lun->flags & CTL_LUN_MALLOCED)
4210 be_lun->lun_config_status(be_lun->be_lun,
4211 CTL_LUN_CONFIG_FAILURE);
4214 lun_number = be_lun->req_lun_id;
4216 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4217 if (lun_number == -1) {
4218 mtx_unlock(&ctl_softc->ctl_lock);
4219 printf("ctl: can't allocate LUN on target %ju, out of "
4220 "LUNs\n", (uintmax_t)target_id.id);
4221 if (lun->flags & CTL_LUN_MALLOCED)
4223 be_lun->lun_config_status(be_lun->be_lun,
4224 CTL_LUN_CONFIG_FAILURE);
4228 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4230 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4231 lun->target = target_id;
4232 lun->lun = lun_number;
4233 lun->be_lun = be_lun;
4235 * The processor LUN is always enabled. Disk LUNs come on line
4236 * disabled, and must be enabled by the backend.
4238 lun->flags |= CTL_LUN_DISABLED;
4239 lun->backend = be_lun->be;
4240 be_lun->ctl_lun = lun;
4241 be_lun->lun_id = lun_number;
4242 atomic_add_int(&be_lun->be->num_luns, 1);
4243 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4244 lun->flags |= CTL_LUN_STOPPED;
4246 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4247 lun->flags |= CTL_LUN_INOPERABLE;
4249 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4250 lun->flags |= CTL_LUN_PRIMARY_SC;
4252 lun->ctl_softc = ctl_softc;
4253 TAILQ_INIT(&lun->ooa_queue);
4254 TAILQ_INIT(&lun->blocked_queue);
4255 STAILQ_INIT(&lun->error_list);
4258 * Initialize the mode page index.
4260 ctl_init_page_index(lun);
4263 * Set the poweron UA for all initiators on this LUN only.
4265 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4266 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4269 * Now, before we insert this lun on the lun list, set the lun
4270 * inventory changed UA for all other luns.
4272 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4273 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4274 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4278 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4280 ctl_softc->ctl_luns[lun_number] = lun;
4282 ctl_softc->num_luns++;
4284 /* Setup statistics gathering */
4285 lun->stats.device_type = be_lun->lun_type;
4286 lun->stats.lun_number = lun_number;
4287 if (lun->stats.device_type == T_DIRECT)
4288 lun->stats.blocksize = be_lun->blocksize;
4290 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4291 for (i = 0;i < CTL_MAX_PORTS;i++)
4292 lun->stats.ports[i].targ_port = i;
4294 mtx_unlock(&ctl_softc->ctl_lock);
4296 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4299 * Run through each registered FETD and bring it online if it isn't
4300 * already. Enable the target ID if it hasn't been enabled, and
4301 * enable this particular LUN.
4303 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4306 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4308 printf("ctl_alloc_lun: FETD %s port %d returned error "
4309 "%d for lun_enable on target %ju lun %d\n",
4310 port->port_name, port->targ_port, retval,
4311 (uintmax_t)target_id.id, lun_number);
4313 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4321 * - LUN has already been marked invalid and any pending I/O has been taken
4325 ctl_free_lun(struct ctl_lun *lun)
4327 struct ctl_softc *softc;
4329 struct ctl_port *port;
4331 struct ctl_lun *nlun;
4334 softc = lun->ctl_softc;
4336 mtx_assert(&softc->ctl_lock, MA_OWNED);
4338 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4340 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4342 softc->ctl_luns[lun->lun] = NULL;
4344 if (!TAILQ_EMPTY(&lun->ooa_queue))
4345 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4350 * XXX KDM this scheme only works for a single target/multiple LUN
4351 * setup. It needs to be revamped for a multiple target scheme.
4353 * XXX KDM this results in port->lun_disable() getting called twice,
4354 * once when ctl_disable_lun() is called, and a second time here.
4355 * We really need to re-think the LUN disable semantics. There
4356 * should probably be several steps/levels to LUN removal:
4361 * Right now we only have a disable method when communicating to
4362 * the front end ports, at least for individual LUNs.
4365 STAILQ_FOREACH(port, &softc->port_list, links) {
4368 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4371 printf("ctl_free_lun: FETD %s port %d returned error "
4372 "%d for lun_disable on target %ju lun %jd\n",
4373 port->port_name, port->targ_port, retval,
4374 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4377 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4378 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4380 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4382 printf("ctl_free_lun: FETD %s port %d "
4383 "returned error %d for targ_disable on "
4384 "target %ju\n", port->port_name,
4385 port->targ_port, retval,
4386 (uintmax_t)lun->target.id);
4388 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4390 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4394 port->port_offline(port->onoff_arg);
4395 port->status &= ~CTL_PORT_STATUS_ONLINE;
4402 * Tell the backend to free resources, if this LUN has a backend.
4404 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4405 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4407 mtx_destroy(&lun->lun_lock);
4408 if (lun->flags & CTL_LUN_MALLOCED)
4411 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4412 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4413 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4421 ctl_create_lun(struct ctl_be_lun *be_lun)
4423 struct ctl_softc *ctl_softc;
4425 ctl_softc = control_softc;
4428 * ctl_alloc_lun() should handle all potential failure cases.
4430 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4434 ctl_add_lun(struct ctl_be_lun *be_lun)
4436 struct ctl_softc *ctl_softc = control_softc;
4438 mtx_lock(&ctl_softc->ctl_lock);
4439 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4440 mtx_unlock(&ctl_softc->ctl_lock);
4441 wakeup(&ctl_softc->pending_lun_queue);
4447 ctl_enable_lun(struct ctl_be_lun *be_lun)
4449 struct ctl_softc *ctl_softc;
4450 struct ctl_port *port, *nport;
4451 struct ctl_lun *lun;
4454 ctl_softc = control_softc;
4456 lun = (struct ctl_lun *)be_lun->ctl_lun;
4458 mtx_lock(&ctl_softc->ctl_lock);
4459 mtx_lock(&lun->lun_lock);
4460 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4462 * eh? Why did we get called if the LUN is already
4465 mtx_unlock(&lun->lun_lock);
4466 mtx_unlock(&ctl_softc->ctl_lock);
4469 lun->flags &= ~CTL_LUN_DISABLED;
4470 mtx_unlock(&lun->lun_lock);
4472 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4473 nport = STAILQ_NEXT(port, links);
4476 * Drop the lock while we call the FETD's enable routine.
4477 * This can lead to a callback into CTL (at least in the
4478 * case of the internal initiator frontend.
4480 mtx_unlock(&ctl_softc->ctl_lock);
4481 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4482 mtx_lock(&ctl_softc->ctl_lock);
4484 printf("%s: FETD %s port %d returned error "
4485 "%d for lun_enable on target %ju lun %jd\n",
4486 __func__, port->port_name, port->targ_port, retval,
4487 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4491 /* NOTE: TODO: why does lun enable affect port status? */
4492 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4497 mtx_unlock(&ctl_softc->ctl_lock);
4503 ctl_disable_lun(struct ctl_be_lun *be_lun)
4505 struct ctl_softc *ctl_softc;
4506 struct ctl_port *port;
4507 struct ctl_lun *lun;
4510 ctl_softc = control_softc;
4512 lun = (struct ctl_lun *)be_lun->ctl_lun;
4514 mtx_lock(&ctl_softc->ctl_lock);
4515 mtx_lock(&lun->lun_lock);
4516 if (lun->flags & CTL_LUN_DISABLED) {
4517 mtx_unlock(&lun->lun_lock);
4518 mtx_unlock(&ctl_softc->ctl_lock);
4521 lun->flags |= CTL_LUN_DISABLED;
4522 mtx_unlock(&lun->lun_lock);
4524 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4525 mtx_unlock(&ctl_softc->ctl_lock);
4527 * Drop the lock before we call the frontend's disable
4528 * routine, to avoid lock order reversals.
4530 * XXX KDM what happens if the frontend list changes while
4531 * we're traversing it? It's unlikely, but should be handled.
4533 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4535 mtx_lock(&ctl_softc->ctl_lock);
4537 printf("ctl_alloc_lun: FETD %s port %d returned error "
4538 "%d for lun_disable on target %ju lun %jd\n",
4539 port->port_name, port->targ_port, retval,
4540 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4544 mtx_unlock(&ctl_softc->ctl_lock);
4550 ctl_start_lun(struct ctl_be_lun *be_lun)
4552 struct ctl_softc *ctl_softc;
4553 struct ctl_lun *lun;
4555 ctl_softc = control_softc;
4557 lun = (struct ctl_lun *)be_lun->ctl_lun;
4559 mtx_lock(&lun->lun_lock);
4560 lun->flags &= ~CTL_LUN_STOPPED;
4561 mtx_unlock(&lun->lun_lock);
4567 ctl_stop_lun(struct ctl_be_lun *be_lun)
4569 struct ctl_softc *ctl_softc;
4570 struct ctl_lun *lun;
4572 ctl_softc = control_softc;
4574 lun = (struct ctl_lun *)be_lun->ctl_lun;
4576 mtx_lock(&lun->lun_lock);
4577 lun->flags |= CTL_LUN_STOPPED;
4578 mtx_unlock(&lun->lun_lock);
4584 ctl_lun_offline(struct ctl_be_lun *be_lun)
4586 struct ctl_softc *ctl_softc;
4587 struct ctl_lun *lun;
4589 ctl_softc = control_softc;
4591 lun = (struct ctl_lun *)be_lun->ctl_lun;
4593 mtx_lock(&lun->lun_lock);
4594 lun->flags |= CTL_LUN_OFFLINE;
4595 mtx_unlock(&lun->lun_lock);
4601 ctl_lun_online(struct ctl_be_lun *be_lun)
4603 struct ctl_softc *ctl_softc;
4604 struct ctl_lun *lun;
4606 ctl_softc = control_softc;
4608 lun = (struct ctl_lun *)be_lun->ctl_lun;
4610 mtx_lock(&lun->lun_lock);
4611 lun->flags &= ~CTL_LUN_OFFLINE;
4612 mtx_unlock(&lun->lun_lock);
4618 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4620 struct ctl_softc *ctl_softc;
4621 struct ctl_lun *lun;
4623 ctl_softc = control_softc;
4625 lun = (struct ctl_lun *)be_lun->ctl_lun;
4627 mtx_lock(&lun->lun_lock);
4630 * The LUN needs to be disabled before it can be marked invalid.
4632 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4633 mtx_unlock(&lun->lun_lock);
4637 * Mark the LUN invalid.
4639 lun->flags |= CTL_LUN_INVALID;
4642 * If there is nothing in the OOA queue, go ahead and free the LUN.
4643 * If we have something in the OOA queue, we'll free it when the
4644 * last I/O completes.
4646 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4647 mtx_unlock(&lun->lun_lock);
4648 mtx_lock(&ctl_softc->ctl_lock);
4650 mtx_unlock(&ctl_softc->ctl_lock);
4652 mtx_unlock(&lun->lun_lock);
4658 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4660 struct ctl_softc *ctl_softc;
4661 struct ctl_lun *lun;
4663 ctl_softc = control_softc;
4664 lun = (struct ctl_lun *)be_lun->ctl_lun;
4666 mtx_lock(&lun->lun_lock);
4667 lun->flags |= CTL_LUN_INOPERABLE;
4668 mtx_unlock(&lun->lun_lock);
4674 ctl_lun_operable(struct ctl_be_lun *be_lun)
4676 struct ctl_softc *ctl_softc;
4677 struct ctl_lun *lun;
4679 ctl_softc = control_softc;
4680 lun = (struct ctl_lun *)be_lun->ctl_lun;
4682 mtx_lock(&lun->lun_lock);
4683 lun->flags &= ~CTL_LUN_INOPERABLE;
4684 mtx_unlock(&lun->lun_lock);
4690 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4693 struct ctl_softc *softc;
4694 struct ctl_lun *lun;
4695 struct copan_aps_subpage *current_sp;
4696 struct ctl_page_index *page_index;
4699 softc = control_softc;
4701 mtx_lock(&softc->ctl_lock);
4703 lun = (struct ctl_lun *)be_lun->ctl_lun;
4704 mtx_lock(&lun->lun_lock);
4707 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4708 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4712 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4714 page_index = &lun->mode_pages.index[i];
4717 if (page_index == NULL) {
4718 mtx_unlock(&lun->lun_lock);
4719 mtx_unlock(&softc->ctl_lock);
4720 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4721 (uintmax_t)lun->lun);
4725 if ((softc->aps_locked_lun != 0)
4726 && (softc->aps_locked_lun != lun->lun)) {
4727 printf("%s: attempt to lock LUN %llu when %llu is already "
4729 mtx_unlock(&lun->lun_lock);
4730 mtx_unlock(&softc->ctl_lock);
4735 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4736 (page_index->page_len * CTL_PAGE_CURRENT));
4739 current_sp->lock_active = APS_LOCK_ACTIVE;
4740 softc->aps_locked_lun = lun->lun;
4742 current_sp->lock_active = 0;
4743 softc->aps_locked_lun = 0;
4748 * If we're in HA mode, try to send the lock message to the other
4751 if (ctl_is_single == 0) {
4753 union ctl_ha_msg lock_msg;
4755 lock_msg.hdr.nexus = *nexus;
4756 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4758 lock_msg.aps.lock_flag = 1;
4760 lock_msg.aps.lock_flag = 0;
4761 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4762 sizeof(lock_msg), 0);
4763 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4764 printf("%s: APS (lock=%d) error returned from "
4765 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4766 mtx_unlock(&lun->lun_lock);
4767 mtx_unlock(&softc->ctl_lock);
4772 mtx_unlock(&lun->lun_lock);
4773 mtx_unlock(&softc->ctl_lock);
4779 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4781 struct ctl_lun *lun;
4782 struct ctl_softc *softc;
4785 softc = control_softc;
4787 lun = (struct ctl_lun *)be_lun->ctl_lun;
4789 mtx_lock(&lun->lun_lock);
4791 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4792 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4794 mtx_unlock(&lun->lun_lock);
4798 * Backend "memory move is complete" callback for requests that never
4799 * make it down to say RAIDCore's configuration code.
4802 ctl_config_move_done(union ctl_io *io)
4806 retval = CTL_RETVAL_COMPLETE;
4809 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4811 * XXX KDM this shouldn't happen, but what if it does?
4813 if (io->io_hdr.io_type != CTL_IO_SCSI)
4814 panic("I/O type isn't CTL_IO_SCSI!");
4816 if ((io->io_hdr.port_status == 0)
4817 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4818 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4819 io->io_hdr.status = CTL_SUCCESS;
4820 else if ((io->io_hdr.port_status != 0)
4821 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4822 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4824 * For hardware error sense keys, the sense key
4825 * specific value is defined to be a retry count,
4826 * but we use it to pass back an internal FETD
4827 * error code. XXX KDM Hopefully the FETD is only
4828 * using 16 bits for an error code, since that's
4829 * all the space we have in the sks field.
4831 ctl_set_internal_failure(&io->scsiio,
4834 io->io_hdr.port_status);
4835 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4836 free(io->scsiio.kern_data_ptr, M_CTL);
4841 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4842 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4843 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4845 * XXX KDM just assuming a single pointer here, and not a
4846 * S/G list. If we start using S/G lists for config data,
4847 * we'll need to know how to clean them up here as well.
4849 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4850 free(io->scsiio.kern_data_ptr, M_CTL);
4851 /* Hopefully the user has already set the status... */
4855 * XXX KDM now we need to continue data movement. Some
4857 * - call ctl_scsiio() again? We don't do this for data
4858 * writes, because for those at least we know ahead of
4859 * time where the write will go and how long it is. For
4860 * config writes, though, that information is largely
4861 * contained within the write itself, thus we need to
4862 * parse out the data again.
4864 * - Call some other function once the data is in?
4868 * XXX KDM call ctl_scsiio() again for now, and check flag
4869 * bits to see whether we're allocated or not.
4871 retval = ctl_scsiio(&io->scsiio);
4878 * This gets called by a backend driver when it is done with a
4879 * data_submit method.
4882 ctl_data_submit_done(union ctl_io *io)
4885 * If the IO_CONT flag is set, we need to call the supplied
4886 * function to continue processing the I/O, instead of completing
4889 * If there is an error, though, we don't want to keep processing.
4890 * Instead, just send status back to the initiator.
4892 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4893 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4894 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4895 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4896 io->scsiio.io_cont(io);
4903 * This gets called by a backend driver when it is done with a
4904 * configuration write.
4907 ctl_config_write_done(union ctl_io *io)
4910 * If the IO_CONT flag is set, we need to call the supplied
4911 * function to continue processing the I/O, instead of completing
4914 * If there is an error, though, we don't want to keep processing.
4915 * Instead, just send status back to the initiator.
4917 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
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);
4924 * Since a configuration write can be done for commands that actually
4925 * have data allocated, like write buffer, and commands that have
4926 * no data, like start/stop unit, we need to check here.
4928 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4929 free(io->scsiio.kern_data_ptr, M_CTL);
4934 * SCSI release command.
4937 ctl_scsi_release(struct ctl_scsiio *ctsio)
4939 int length, longid, thirdparty_id, resv_id;
4940 struct ctl_softc *ctl_softc;
4941 struct ctl_lun *lun;
4946 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
4948 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
4949 ctl_softc = control_softc;
4951 switch (ctsio->cdb[0]) {
4953 struct scsi_release_10 *cdb;
4955 cdb = (struct scsi_release_10 *)ctsio->cdb;
4957 if (cdb->byte2 & SR10_LONGID)
4960 thirdparty_id = cdb->thirdparty_id;
4962 resv_id = cdb->resv_id;
4963 length = scsi_2btoul(cdb->length);
4970 * XXX KDM right now, we only support LUN reservation. We don't
4971 * support 3rd party reservations, or extent reservations, which
4972 * might actually need the parameter list. If we've gotten this
4973 * far, we've got a LUN reservation. Anything else got kicked out
4974 * above. So, according to SPC, ignore the length.
4978 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
4980 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
4981 ctsio->kern_data_len = length;
4982 ctsio->kern_total_len = length;
4983 ctsio->kern_data_resid = 0;
4984 ctsio->kern_rel_offset = 0;
4985 ctsio->kern_sg_entries = 0;
4986 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
4987 ctsio->be_move_done = ctl_config_move_done;
4988 ctl_datamove((union ctl_io *)ctsio);
4990 return (CTL_RETVAL_COMPLETE);
4994 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
4996 mtx_lock(&lun->lun_lock);
4999 * According to SPC, it is not an error for an intiator to attempt
5000 * to release a reservation on a LUN that isn't reserved, or that
5001 * is reserved by another initiator. The reservation can only be
5002 * released, though, by the initiator who made it or by one of
5003 * several reset type events.
5005 if (lun->flags & CTL_LUN_RESERVED) {
5006 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5007 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5008 && (ctsio->io_hdr.nexus.targ_target.id ==
5009 lun->rsv_nexus.targ_target.id)) {
5010 lun->flags &= ~CTL_LUN_RESERVED;
5014 mtx_unlock(&lun->lun_lock);
5016 ctsio->scsi_status = SCSI_STATUS_OK;
5017 ctsio->io_hdr.status = CTL_SUCCESS;
5019 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5020 free(ctsio->kern_data_ptr, M_CTL);
5021 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5024 ctl_done((union ctl_io *)ctsio);
5025 return (CTL_RETVAL_COMPLETE);
5029 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5031 int extent, thirdparty, longid;
5032 int resv_id, length;
5033 uint64_t thirdparty_id;
5034 struct ctl_softc *ctl_softc;
5035 struct ctl_lun *lun;
5044 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5046 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5047 ctl_softc = control_softc;
5049 switch (ctsio->cdb[0]) {
5051 struct scsi_reserve_10 *cdb;
5053 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5055 if (cdb->byte2 & SR10_LONGID)
5058 thirdparty_id = cdb->thirdparty_id;
5060 resv_id = cdb->resv_id;
5061 length = scsi_2btoul(cdb->length);
5067 * XXX KDM right now, we only support LUN reservation. We don't
5068 * support 3rd party reservations, or extent reservations, which
5069 * might actually need the parameter list. If we've gotten this
5070 * far, we've got a LUN reservation. Anything else got kicked out
5071 * above. So, according to SPC, ignore the length.
5075 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5077 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5078 ctsio->kern_data_len = length;
5079 ctsio->kern_total_len = length;
5080 ctsio->kern_data_resid = 0;
5081 ctsio->kern_rel_offset = 0;
5082 ctsio->kern_sg_entries = 0;
5083 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5084 ctsio->be_move_done = ctl_config_move_done;
5085 ctl_datamove((union ctl_io *)ctsio);
5087 return (CTL_RETVAL_COMPLETE);
5091 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5093 mtx_lock(&lun->lun_lock);
5094 if (lun->flags & CTL_LUN_RESERVED) {
5095 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5096 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5097 || (ctsio->io_hdr.nexus.targ_target.id !=
5098 lun->rsv_nexus.targ_target.id)) {
5099 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5100 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5105 lun->flags |= CTL_LUN_RESERVED;
5106 lun->rsv_nexus = ctsio->io_hdr.nexus;
5108 ctsio->scsi_status = SCSI_STATUS_OK;
5109 ctsio->io_hdr.status = CTL_SUCCESS;
5112 mtx_unlock(&lun->lun_lock);
5114 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5115 free(ctsio->kern_data_ptr, M_CTL);
5116 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5119 ctl_done((union ctl_io *)ctsio);
5120 return (CTL_RETVAL_COMPLETE);
5124 ctl_start_stop(struct ctl_scsiio *ctsio)
5126 struct scsi_start_stop_unit *cdb;
5127 struct ctl_lun *lun;
5128 struct ctl_softc *ctl_softc;
5131 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5133 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5134 ctl_softc = control_softc;
5137 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5141 * We don't support the immediate bit on a stop unit. In order to
5142 * do that, we would need to code up a way to know that a stop is
5143 * pending, and hold off any new commands until it completes, one
5144 * way or another. Then we could accept or reject those commands
5145 * depending on its status. We would almost need to do the reverse
5146 * of what we do below for an immediate start -- return the copy of
5147 * the ctl_io to the FETD with status to send to the host (and to
5148 * free the copy!) and then free the original I/O once the stop
5149 * actually completes. That way, the OOA queue mechanism can work
5150 * to block commands that shouldn't proceed. Another alternative
5151 * would be to put the copy in the queue in place of the original,
5152 * and return the original back to the caller. That could be
5155 if ((cdb->byte2 & SSS_IMMED)
5156 && ((cdb->how & SSS_START) == 0)) {
5157 ctl_set_invalid_field(ctsio,
5163 ctl_done((union ctl_io *)ctsio);
5164 return (CTL_RETVAL_COMPLETE);
5167 if ((lun->flags & CTL_LUN_PR_RESERVED)
5168 && ((cdb->how & SSS_START)==0)) {
5171 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5172 if (!lun->per_res[residx].registered
5173 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5175 ctl_set_reservation_conflict(ctsio);
5176 ctl_done((union ctl_io *)ctsio);
5177 return (CTL_RETVAL_COMPLETE);
5182 * If there is no backend on this device, we can't start or stop
5183 * it. In theory we shouldn't get any start/stop commands in the
5184 * first place at this level if the LUN doesn't have a backend.
5185 * That should get stopped by the command decode code.
5187 if (lun->backend == NULL) {
5188 ctl_set_invalid_opcode(ctsio);
5189 ctl_done((union ctl_io *)ctsio);
5190 return (CTL_RETVAL_COMPLETE);
5194 * XXX KDM Copan-specific offline behavior.
5195 * Figure out a reasonable way to port this?
5198 mtx_lock(&lun->lun_lock);
5200 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5201 && (lun->flags & CTL_LUN_OFFLINE)) {
5203 * If the LUN is offline, and the on/offline bit isn't set,
5204 * reject the start or stop. Otherwise, let it through.
5206 mtx_unlock(&lun->lun_lock);
5207 ctl_set_lun_not_ready(ctsio);
5208 ctl_done((union ctl_io *)ctsio);
5210 mtx_unlock(&lun->lun_lock);
5211 #endif /* NEEDTOPORT */
5213 * This could be a start or a stop when we're online,
5214 * or a stop/offline or start/online. A start or stop when
5215 * we're offline is covered in the case above.
5218 * In the non-immediate case, we send the request to
5219 * the backend and return status to the user when
5222 * In the immediate case, we allocate a new ctl_io
5223 * to hold a copy of the request, and send that to
5224 * the backend. We then set good status on the
5225 * user's request and return it immediately.
5227 if (cdb->byte2 & SSS_IMMED) {
5228 union ctl_io *new_io;
5230 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5231 if (new_io == NULL) {
5232 ctl_set_busy(ctsio);
5233 ctl_done((union ctl_io *)ctsio);
5235 ctl_copy_io((union ctl_io *)ctsio,
5237 retval = lun->backend->config_write(new_io);
5238 ctl_set_success(ctsio);
5239 ctl_done((union ctl_io *)ctsio);
5242 retval = lun->backend->config_write(
5243 (union ctl_io *)ctsio);
5252 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5253 * we don't really do anything with the LBA and length fields if the user
5254 * passes them in. Instead we'll just flush out the cache for the entire
5258 ctl_sync_cache(struct ctl_scsiio *ctsio)
5260 struct ctl_lun *lun;
5261 struct ctl_softc *ctl_softc;
5262 uint64_t starting_lba;
5263 uint32_t block_count;
5266 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5268 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5269 ctl_softc = control_softc;
5272 switch (ctsio->cdb[0]) {
5273 case SYNCHRONIZE_CACHE: {
5274 struct scsi_sync_cache *cdb;
5275 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5277 starting_lba = scsi_4btoul(cdb->begin_lba);
5278 block_count = scsi_2btoul(cdb->lb_count);
5281 case SYNCHRONIZE_CACHE_16: {
5282 struct scsi_sync_cache_16 *cdb;
5283 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5285 starting_lba = scsi_8btou64(cdb->begin_lba);
5286 block_count = scsi_4btoul(cdb->lb_count);
5290 ctl_set_invalid_opcode(ctsio);
5291 ctl_done((union ctl_io *)ctsio);
5293 break; /* NOTREACHED */
5297 * We check the LBA and length, but don't do anything with them.
5298 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5299 * get flushed. This check will just help satisfy anyone who wants
5300 * to see an error for an out of range LBA.
5302 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5303 ctl_set_lba_out_of_range(ctsio);
5304 ctl_done((union ctl_io *)ctsio);
5309 * If this LUN has no backend, we can't flush the cache anyway.
5311 if (lun->backend == NULL) {
5312 ctl_set_invalid_opcode(ctsio);
5313 ctl_done((union ctl_io *)ctsio);
5318 * Check to see whether we're configured to send the SYNCHRONIZE
5319 * CACHE command directly to the back end.
5321 mtx_lock(&lun->lun_lock);
5322 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5323 && (++(lun->sync_count) >= lun->sync_interval)) {
5324 lun->sync_count = 0;
5325 mtx_unlock(&lun->lun_lock);
5326 retval = lun->backend->config_write((union ctl_io *)ctsio);
5328 mtx_unlock(&lun->lun_lock);
5329 ctl_set_success(ctsio);
5330 ctl_done((union ctl_io *)ctsio);
5339 ctl_format(struct ctl_scsiio *ctsio)
5341 struct scsi_format *cdb;
5342 struct ctl_lun *lun;
5343 struct ctl_softc *ctl_softc;
5344 int length, defect_list_len;
5346 CTL_DEBUG_PRINT(("ctl_format\n"));
5348 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5349 ctl_softc = control_softc;
5351 cdb = (struct scsi_format *)ctsio->cdb;
5354 if (cdb->byte2 & SF_FMTDATA) {
5355 if (cdb->byte2 & SF_LONGLIST)
5356 length = sizeof(struct scsi_format_header_long);
5358 length = sizeof(struct scsi_format_header_short);
5361 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5363 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5364 ctsio->kern_data_len = length;
5365 ctsio->kern_total_len = length;
5366 ctsio->kern_data_resid = 0;
5367 ctsio->kern_rel_offset = 0;
5368 ctsio->kern_sg_entries = 0;
5369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5370 ctsio->be_move_done = ctl_config_move_done;
5371 ctl_datamove((union ctl_io *)ctsio);
5373 return (CTL_RETVAL_COMPLETE);
5376 defect_list_len = 0;
5378 if (cdb->byte2 & SF_FMTDATA) {
5379 if (cdb->byte2 & SF_LONGLIST) {
5380 struct scsi_format_header_long *header;
5382 header = (struct scsi_format_header_long *)
5383 ctsio->kern_data_ptr;
5385 defect_list_len = scsi_4btoul(header->defect_list_len);
5386 if (defect_list_len != 0) {
5387 ctl_set_invalid_field(ctsio,
5396 struct scsi_format_header_short *header;
5398 header = (struct scsi_format_header_short *)
5399 ctsio->kern_data_ptr;
5401 defect_list_len = scsi_2btoul(header->defect_list_len);
5402 if (defect_list_len != 0) {
5403 ctl_set_invalid_field(ctsio,
5415 * The format command will clear out the "Medium format corrupted"
5416 * status if set by the configuration code. That status is really
5417 * just a way to notify the host that we have lost the media, and
5418 * get them to issue a command that will basically make them think
5419 * they're blowing away the media.
5421 mtx_lock(&lun->lun_lock);
5422 lun->flags &= ~CTL_LUN_INOPERABLE;
5423 mtx_unlock(&lun->lun_lock);
5425 ctsio->scsi_status = SCSI_STATUS_OK;
5426 ctsio->io_hdr.status = CTL_SUCCESS;
5429 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5430 free(ctsio->kern_data_ptr, M_CTL);
5431 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5434 ctl_done((union ctl_io *)ctsio);
5435 return (CTL_RETVAL_COMPLETE);
5439 ctl_read_buffer(struct ctl_scsiio *ctsio)
5441 struct scsi_read_buffer *cdb;
5442 struct ctl_lun *lun;
5443 int buffer_offset, len;
5444 static uint8_t descr[4];
5445 static uint8_t echo_descr[4] = { 0 };
5447 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5449 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5450 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5452 if (lun->flags & CTL_LUN_PR_RESERVED) {
5456 * XXX KDM need a lock here.
5458 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5459 if ((lun->res_type == SPR_TYPE_EX_AC
5460 && residx != lun->pr_res_idx)
5461 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5462 || lun->res_type == SPR_TYPE_EX_AC_AR)
5463 && !lun->per_res[residx].registered)) {
5464 ctl_set_reservation_conflict(ctsio);
5465 ctl_done((union ctl_io *)ctsio);
5466 return (CTL_RETVAL_COMPLETE);
5470 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5471 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5472 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5473 ctl_set_invalid_field(ctsio,
5479 ctl_done((union ctl_io *)ctsio);
5480 return (CTL_RETVAL_COMPLETE);
5483 len = scsi_3btoul(cdb->length);
5484 buffer_offset = scsi_3btoul(cdb->offset);
5486 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5487 ctl_set_invalid_field(ctsio,
5493 ctl_done((union ctl_io *)ctsio);
5494 return (CTL_RETVAL_COMPLETE);
5497 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5499 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5500 ctsio->kern_data_ptr = descr;
5501 len = min(len, sizeof(descr));
5502 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5503 ctsio->kern_data_ptr = echo_descr;
5504 len = min(len, sizeof(echo_descr));
5506 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5507 ctsio->kern_data_len = len;
5508 ctsio->kern_total_len = len;
5509 ctsio->kern_data_resid = 0;
5510 ctsio->kern_rel_offset = 0;
5511 ctsio->kern_sg_entries = 0;
5512 ctsio->be_move_done = ctl_config_move_done;
5513 ctl_datamove((union ctl_io *)ctsio);
5515 return (CTL_RETVAL_COMPLETE);
5519 ctl_write_buffer(struct ctl_scsiio *ctsio)
5521 struct scsi_write_buffer *cdb;
5522 struct ctl_lun *lun;
5523 int buffer_offset, len;
5525 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5527 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5528 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5530 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5531 ctl_set_invalid_field(ctsio,
5537 ctl_done((union ctl_io *)ctsio);
5538 return (CTL_RETVAL_COMPLETE);
5541 len = scsi_3btoul(cdb->length);
5542 buffer_offset = scsi_3btoul(cdb->offset);
5544 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5545 ctl_set_invalid_field(ctsio,
5551 ctl_done((union ctl_io *)ctsio);
5552 return (CTL_RETVAL_COMPLETE);
5556 * If we've got a kernel request that hasn't been malloced yet,
5557 * malloc it and tell the caller the data buffer is here.
5559 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5560 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5561 ctsio->kern_data_len = len;
5562 ctsio->kern_total_len = len;
5563 ctsio->kern_data_resid = 0;
5564 ctsio->kern_rel_offset = 0;
5565 ctsio->kern_sg_entries = 0;
5566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5567 ctsio->be_move_done = ctl_config_move_done;
5568 ctl_datamove((union ctl_io *)ctsio);
5570 return (CTL_RETVAL_COMPLETE);
5573 ctl_done((union ctl_io *)ctsio);
5575 return (CTL_RETVAL_COMPLETE);
5579 ctl_write_same(struct ctl_scsiio *ctsio)
5581 struct ctl_lun *lun;
5582 struct ctl_lba_len_flags *lbalen;
5584 uint32_t num_blocks;
5588 retval = CTL_RETVAL_COMPLETE;
5590 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5594 switch (ctsio->cdb[0]) {
5595 case WRITE_SAME_10: {
5596 struct scsi_write_same_10 *cdb;
5598 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5600 lba = scsi_4btoul(cdb->addr);
5601 num_blocks = scsi_2btoul(cdb->length);
5605 case WRITE_SAME_16: {
5606 struct scsi_write_same_16 *cdb;
5608 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5610 lba = scsi_8btou64(cdb->addr);
5611 num_blocks = scsi_4btoul(cdb->length);
5617 * We got a command we don't support. This shouldn't
5618 * happen, commands should be filtered out above us.
5620 ctl_set_invalid_opcode(ctsio);
5621 ctl_done((union ctl_io *)ctsio);
5623 return (CTL_RETVAL_COMPLETE);
5624 break; /* NOTREACHED */
5628 * The first check is to make sure we're in bounds, the second
5629 * check is to catch wrap-around problems. If the lba + num blocks
5630 * is less than the lba, then we've wrapped around and the block
5631 * range is invalid anyway.
5633 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5634 || ((lba + num_blocks) < lba)) {
5635 ctl_set_lba_out_of_range(ctsio);
5636 ctl_done((union ctl_io *)ctsio);
5637 return (CTL_RETVAL_COMPLETE);
5640 /* Zero number of blocks means "to the last logical block" */
5641 if (num_blocks == 0) {
5642 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5643 ctl_set_invalid_field(ctsio,
5649 ctl_done((union ctl_io *)ctsio);
5650 return (CTL_RETVAL_COMPLETE);
5652 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5655 len = lun->be_lun->blocksize;
5658 * If we've got a kernel request that hasn't been malloced yet,
5659 * malloc it and tell the caller the data buffer is here.
5661 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5662 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5663 ctsio->kern_data_len = len;
5664 ctsio->kern_total_len = len;
5665 ctsio->kern_data_resid = 0;
5666 ctsio->kern_rel_offset = 0;
5667 ctsio->kern_sg_entries = 0;
5668 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5669 ctsio->be_move_done = ctl_config_move_done;
5670 ctl_datamove((union ctl_io *)ctsio);
5672 return (CTL_RETVAL_COMPLETE);
5675 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5677 lbalen->len = num_blocks;
5678 lbalen->flags = byte2;
5679 retval = lun->backend->config_write((union ctl_io *)ctsio);
5685 ctl_unmap(struct ctl_scsiio *ctsio)
5687 struct ctl_lun *lun;
5688 struct scsi_unmap *cdb;
5689 struct ctl_ptr_len_flags *ptrlen;
5690 struct scsi_unmap_header *hdr;
5691 struct scsi_unmap_desc *buf, *end;
5693 uint32_t num_blocks;
5697 retval = CTL_RETVAL_COMPLETE;
5699 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5702 cdb = (struct scsi_unmap *)ctsio->cdb;
5704 len = scsi_2btoul(cdb->length);
5708 * If we've got a kernel request that hasn't been malloced yet,
5709 * malloc it and tell the caller the data buffer is here.
5711 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5712 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5713 ctsio->kern_data_len = len;
5714 ctsio->kern_total_len = len;
5715 ctsio->kern_data_resid = 0;
5716 ctsio->kern_rel_offset = 0;
5717 ctsio->kern_sg_entries = 0;
5718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5719 ctsio->be_move_done = ctl_config_move_done;
5720 ctl_datamove((union ctl_io *)ctsio);
5722 return (CTL_RETVAL_COMPLETE);
5725 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5726 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5727 if (len < sizeof (*hdr) ||
5728 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5729 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5730 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5731 ctl_set_invalid_field(ctsio,
5737 ctl_done((union ctl_io *)ctsio);
5738 return (CTL_RETVAL_COMPLETE);
5740 len = scsi_2btoul(hdr->desc_length);
5741 buf = (struct scsi_unmap_desc *)(hdr + 1);
5742 end = buf + len / sizeof(*buf);
5744 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5745 ptrlen->ptr = (void *)buf;
5747 ptrlen->flags = byte2;
5749 for (; buf < end; buf++) {
5750 lba = scsi_8btou64(buf->lba);
5751 num_blocks = scsi_4btoul(buf->length);
5752 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5753 || ((lba + num_blocks) < lba)) {
5754 ctl_set_lba_out_of_range(ctsio);
5755 ctl_done((union ctl_io *)ctsio);
5756 return (CTL_RETVAL_COMPLETE);
5760 retval = lun->backend->config_write((union ctl_io *)ctsio);
5766 * Note that this function currently doesn't actually do anything inside
5767 * CTL to enforce things if the DQue bit is turned on.
5769 * Also note that this function can't be used in the default case, because
5770 * the DQue bit isn't set in the changeable mask for the control mode page
5771 * anyway. This is just here as an example for how to implement a page
5772 * handler, and a placeholder in case we want to allow the user to turn
5773 * tagged queueing on and off.
5775 * The D_SENSE bit handling is functional, however, and will turn
5776 * descriptor sense on and off for a given LUN.
5779 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5780 struct ctl_page_index *page_index, uint8_t *page_ptr)
5782 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5783 struct ctl_lun *lun;
5784 struct ctl_softc *softc;
5788 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5789 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5792 user_cp = (struct scsi_control_page *)page_ptr;
5793 current_cp = (struct scsi_control_page *)
5794 (page_index->page_data + (page_index->page_len *
5796 saved_cp = (struct scsi_control_page *)
5797 (page_index->page_data + (page_index->page_len *
5800 softc = control_softc;
5802 mtx_lock(&lun->lun_lock);
5803 if (((current_cp->rlec & SCP_DSENSE) == 0)
5804 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5806 * Descriptor sense is currently turned off and the user
5807 * wants to turn it on.
5809 current_cp->rlec |= SCP_DSENSE;
5810 saved_cp->rlec |= SCP_DSENSE;
5811 lun->flags |= CTL_LUN_SENSE_DESC;
5813 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5814 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5816 * Descriptor sense is currently turned on, and the user
5817 * wants to turn it off.
5819 current_cp->rlec &= ~SCP_DSENSE;
5820 saved_cp->rlec &= ~SCP_DSENSE;
5821 lun->flags &= ~CTL_LUN_SENSE_DESC;
5824 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5825 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5827 csevent_log(CSC_CTL | CSC_SHELF_SW |
5829 csevent_LogType_Trace,
5830 csevent_Severity_Information,
5831 csevent_AlertLevel_Green,
5832 csevent_FRU_Firmware,
5833 csevent_FRU_Unknown,
5834 "Received untagged to untagged transition");
5835 #endif /* NEEDTOPORT */
5838 csevent_log(CSC_CTL | CSC_SHELF_SW |
5840 csevent_LogType_ConfigChange,
5841 csevent_Severity_Information,
5842 csevent_AlertLevel_Green,
5843 csevent_FRU_Firmware,
5844 csevent_FRU_Unknown,
5845 "Received untagged to tagged "
5846 "queueing transition");
5847 #endif /* NEEDTOPORT */
5849 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5850 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5854 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5856 csevent_log(CSC_CTL | CSC_SHELF_SW |
5858 csevent_LogType_ConfigChange,
5859 csevent_Severity_Warning,
5860 csevent_AlertLevel_Yellow,
5861 csevent_FRU_Firmware,
5862 csevent_FRU_Unknown,
5863 "Received tagged queueing to untagged "
5865 #endif /* NEEDTOPORT */
5867 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5868 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5872 csevent_log(CSC_CTL | CSC_SHELF_SW |
5874 csevent_LogType_Trace,
5875 csevent_Severity_Information,
5876 csevent_AlertLevel_Green,
5877 csevent_FRU_Firmware,
5878 csevent_FRU_Unknown,
5879 "Received tagged queueing to tagged "
5880 "queueing transition");
5881 #endif /* NEEDTOPORT */
5887 * Let other initiators know that the mode
5888 * parameters for this LUN have changed.
5890 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5894 lun->pending_sense[i].ua_pending |=
5898 mtx_unlock(&lun->lun_lock);
5904 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5905 struct ctl_page_index *page_index, uint8_t *page_ptr)
5911 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5912 struct ctl_page_index *page_index, int pc)
5914 struct copan_power_subpage *page;
5916 page = (struct copan_power_subpage *)page_index->page_data +
5917 (page_index->page_len * pc);
5920 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5922 * We don't update the changable bits for this page.
5925 case SMS_PAGE_CTRL_CURRENT >> 6:
5926 case SMS_PAGE_CTRL_DEFAULT >> 6:
5927 case SMS_PAGE_CTRL_SAVED >> 6:
5929 ctl_update_power_subpage(page);
5934 EPRINT(0, "Invalid PC %d!!", pc);
5943 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5944 struct ctl_page_index *page_index, uint8_t *page_ptr)
5946 struct copan_aps_subpage *user_sp;
5947 struct copan_aps_subpage *current_sp;
5948 union ctl_modepage_info *modepage_info;
5949 struct ctl_softc *softc;
5950 struct ctl_lun *lun;
5953 retval = CTL_RETVAL_COMPLETE;
5954 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5955 (page_index->page_len * CTL_PAGE_CURRENT));
5956 softc = control_softc;
5957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5959 user_sp = (struct copan_aps_subpage *)page_ptr;
5961 modepage_info = (union ctl_modepage_info *)
5962 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5964 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5965 modepage_info->header.subpage = page_index->subpage;
5966 modepage_info->aps.lock_active = user_sp->lock_active;
5968 mtx_lock(&softc->ctl_lock);
5971 * If there is a request to lock the LUN and another LUN is locked
5972 * this is an error. If the requested LUN is already locked ignore
5973 * the request. If no LUN is locked attempt to lock it.
5974 * if there is a request to unlock the LUN and the LUN is currently
5975 * locked attempt to unlock it. Otherwise ignore the request. i.e.
5976 * if another LUN is locked or no LUN is locked.
5978 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
5979 if (softc->aps_locked_lun == lun->lun) {
5981 * This LUN is already locked, so we're done.
5983 retval = CTL_RETVAL_COMPLETE;
5984 } else if (softc->aps_locked_lun == 0) {
5986 * No one has the lock, pass the request to the
5989 retval = lun->backend->config_write(
5990 (union ctl_io *)ctsio);
5993 * Someone else has the lock, throw out the request.
5995 ctl_set_already_locked(ctsio);
5996 free(ctsio->kern_data_ptr, M_CTL);
5997 ctl_done((union ctl_io *)ctsio);
6000 * Set the return value so that ctl_do_mode_select()
6001 * won't try to complete the command. We already
6002 * completed it here.
6004 retval = CTL_RETVAL_ERROR;
6006 } else if (softc->aps_locked_lun == lun->lun) {
6008 * This LUN is locked, so pass the unlock request to the
6011 retval = lun->backend->config_write((union ctl_io *)ctsio);
6013 mtx_unlock(&softc->ctl_lock);
6019 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6020 struct ctl_page_index *page_index,
6026 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6031 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6032 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6033 printf("page data:");
6035 printf(" %.2x",page_ptr[i]);
6041 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6042 struct ctl_page_index *page_index,
6045 struct copan_debugconf_subpage *page;
6047 page = (struct copan_debugconf_subpage *)page_index->page_data +
6048 (page_index->page_len * pc);
6051 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6052 case SMS_PAGE_CTRL_DEFAULT >> 6:
6053 case SMS_PAGE_CTRL_SAVED >> 6:
6055 * We don't update the changable or default bits for this page.
6058 case SMS_PAGE_CTRL_CURRENT >> 6:
6059 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6060 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6064 EPRINT(0, "Invalid PC %d!!", pc);
6065 #endif /* NEEDTOPORT */
6073 ctl_do_mode_select(union ctl_io *io)
6075 struct scsi_mode_page_header *page_header;
6076 struct ctl_page_index *page_index;
6077 struct ctl_scsiio *ctsio;
6078 int control_dev, page_len;
6079 int page_len_offset, page_len_size;
6080 union ctl_modepage_info *modepage_info;
6081 struct ctl_lun *lun;
6082 int *len_left, *len_used;
6085 ctsio = &io->scsiio;
6088 retval = CTL_RETVAL_COMPLETE;
6090 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6092 if (lun->be_lun->lun_type != T_DIRECT)
6097 modepage_info = (union ctl_modepage_info *)
6098 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6099 len_left = &modepage_info->header.len_left;
6100 len_used = &modepage_info->header.len_used;
6104 page_header = (struct scsi_mode_page_header *)
6105 (ctsio->kern_data_ptr + *len_used);
6107 if (*len_left == 0) {
6108 free(ctsio->kern_data_ptr, M_CTL);
6109 ctl_set_success(ctsio);
6110 ctl_done((union ctl_io *)ctsio);
6111 return (CTL_RETVAL_COMPLETE);
6112 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6114 free(ctsio->kern_data_ptr, M_CTL);
6115 ctl_set_param_len_error(ctsio);
6116 ctl_done((union ctl_io *)ctsio);
6117 return (CTL_RETVAL_COMPLETE);
6119 } else if ((page_header->page_code & SMPH_SPF)
6120 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6122 free(ctsio->kern_data_ptr, M_CTL);
6123 ctl_set_param_len_error(ctsio);
6124 ctl_done((union ctl_io *)ctsio);
6125 return (CTL_RETVAL_COMPLETE);
6130 * XXX KDM should we do something with the block descriptor?
6132 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6134 if ((control_dev != 0)
6135 && (lun->mode_pages.index[i].page_flags &
6136 CTL_PAGE_FLAG_DISK_ONLY))
6139 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6140 (page_header->page_code & SMPH_PC_MASK))
6144 * If neither page has a subpage code, then we've got a
6147 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6148 && ((page_header->page_code & SMPH_SPF) == 0)) {
6149 page_index = &lun->mode_pages.index[i];
6150 page_len = page_header->page_length;
6155 * If both pages have subpages, then the subpage numbers
6158 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6159 && (page_header->page_code & SMPH_SPF)) {
6160 struct scsi_mode_page_header_sp *sph;
6162 sph = (struct scsi_mode_page_header_sp *)page_header;
6164 if (lun->mode_pages.index[i].subpage ==
6166 page_index = &lun->mode_pages.index[i];
6167 page_len = scsi_2btoul(sph->page_length);
6174 * If we couldn't find the page, or if we don't have a mode select
6175 * handler for it, send back an error to the user.
6177 if ((page_index == NULL)
6178 || (page_index->select_handler == NULL)) {
6179 ctl_set_invalid_field(ctsio,
6182 /*field*/ *len_used,
6185 free(ctsio->kern_data_ptr, M_CTL);
6186 ctl_done((union ctl_io *)ctsio);
6187 return (CTL_RETVAL_COMPLETE);
6190 if (page_index->page_code & SMPH_SPF) {
6191 page_len_offset = 2;
6195 page_len_offset = 1;
6199 * If the length the initiator gives us isn't the one we specify in
6200 * the mode page header, or if they didn't specify enough data in
6201 * the CDB to avoid truncating this page, kick out the request.
6203 if ((page_len != (page_index->page_len - page_len_offset -
6205 || (*len_left < page_index->page_len)) {
6208 ctl_set_invalid_field(ctsio,
6211 /*field*/ *len_used + page_len_offset,
6214 free(ctsio->kern_data_ptr, M_CTL);
6215 ctl_done((union ctl_io *)ctsio);
6216 return (CTL_RETVAL_COMPLETE);
6220 * Run through the mode page, checking to make sure that the bits
6221 * the user changed are actually legal for him to change.
6223 for (i = 0; i < page_index->page_len; i++) {
6224 uint8_t *user_byte, *change_mask, *current_byte;
6228 user_byte = (uint8_t *)page_header + i;
6229 change_mask = page_index->page_data +
6230 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6231 current_byte = page_index->page_data +
6232 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6235 * Check to see whether the user set any bits in this byte
6236 * that he is not allowed to set.
6238 if ((*user_byte & ~(*change_mask)) ==
6239 (*current_byte & ~(*change_mask)))
6243 * Go through bit by bit to determine which one is illegal.
6246 for (j = 7; j >= 0; j--) {
6247 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6248 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6253 ctl_set_invalid_field(ctsio,
6256 /*field*/ *len_used + i,
6259 free(ctsio->kern_data_ptr, M_CTL);
6260 ctl_done((union ctl_io *)ctsio);
6261 return (CTL_RETVAL_COMPLETE);
6265 * Decrement these before we call the page handler, since we may
6266 * end up getting called back one way or another before the handler
6267 * returns to this context.
6269 *len_left -= page_index->page_len;
6270 *len_used += page_index->page_len;
6272 retval = page_index->select_handler(ctsio, page_index,
6273 (uint8_t *)page_header);
6276 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6277 * wait until this queued command completes to finish processing
6278 * the mode page. If it returns anything other than
6279 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6280 * already set the sense information, freed the data pointer, and
6281 * completed the io for us.
6283 if (retval != CTL_RETVAL_COMPLETE)
6284 goto bailout_no_done;
6287 * If the initiator sent us more than one page, parse the next one.
6292 ctl_set_success(ctsio);
6293 free(ctsio->kern_data_ptr, M_CTL);
6294 ctl_done((union ctl_io *)ctsio);
6298 return (CTL_RETVAL_COMPLETE);
6303 ctl_mode_select(struct ctl_scsiio *ctsio)
6305 int param_len, pf, sp;
6306 int header_size, bd_len;
6307 int len_left, len_used;
6308 struct ctl_page_index *page_index;
6309 struct ctl_lun *lun;
6310 int control_dev, page_len;
6311 union ctl_modepage_info *modepage_info;
6323 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6325 if (lun->be_lun->lun_type != T_DIRECT)
6330 switch (ctsio->cdb[0]) {
6331 case MODE_SELECT_6: {
6332 struct scsi_mode_select_6 *cdb;
6334 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6336 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6337 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6339 param_len = cdb->length;
6340 header_size = sizeof(struct scsi_mode_header_6);
6343 case MODE_SELECT_10: {
6344 struct scsi_mode_select_10 *cdb;
6346 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6348 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6349 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6351 param_len = scsi_2btoul(cdb->length);
6352 header_size = sizeof(struct scsi_mode_header_10);
6356 ctl_set_invalid_opcode(ctsio);
6357 ctl_done((union ctl_io *)ctsio);
6358 return (CTL_RETVAL_COMPLETE);
6359 break; /* NOTREACHED */
6364 * "A parameter list length of zero indicates that the Data-Out Buffer
6365 * shall be empty. This condition shall not be considered as an error."
6367 if (param_len == 0) {
6368 ctl_set_success(ctsio);
6369 ctl_done((union ctl_io *)ctsio);
6370 return (CTL_RETVAL_COMPLETE);
6374 * Since we'll hit this the first time through, prior to
6375 * allocation, we don't need to free a data buffer here.
6377 if (param_len < header_size) {
6378 ctl_set_param_len_error(ctsio);
6379 ctl_done((union ctl_io *)ctsio);
6380 return (CTL_RETVAL_COMPLETE);
6384 * Allocate the data buffer and grab the user's data. In theory,
6385 * we shouldn't have to sanity check the parameter list length here
6386 * because the maximum size is 64K. We should be able to malloc
6387 * that much without too many problems.
6389 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6390 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6391 ctsio->kern_data_len = param_len;
6392 ctsio->kern_total_len = param_len;
6393 ctsio->kern_data_resid = 0;
6394 ctsio->kern_rel_offset = 0;
6395 ctsio->kern_sg_entries = 0;
6396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6397 ctsio->be_move_done = ctl_config_move_done;
6398 ctl_datamove((union ctl_io *)ctsio);
6400 return (CTL_RETVAL_COMPLETE);
6403 switch (ctsio->cdb[0]) {
6404 case MODE_SELECT_6: {
6405 struct scsi_mode_header_6 *mh6;
6407 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6408 bd_len = mh6->blk_desc_len;
6411 case MODE_SELECT_10: {
6412 struct scsi_mode_header_10 *mh10;
6414 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6415 bd_len = scsi_2btoul(mh10->blk_desc_len);
6419 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6423 if (param_len < (header_size + bd_len)) {
6424 free(ctsio->kern_data_ptr, M_CTL);
6425 ctl_set_param_len_error(ctsio);
6426 ctl_done((union ctl_io *)ctsio);
6427 return (CTL_RETVAL_COMPLETE);
6431 * Set the IO_CONT flag, so that if this I/O gets passed to
6432 * ctl_config_write_done(), it'll get passed back to
6433 * ctl_do_mode_select() for further processing, or completion if
6436 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6437 ctsio->io_cont = ctl_do_mode_select;
6439 modepage_info = (union ctl_modepage_info *)
6440 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6442 memset(modepage_info, 0, sizeof(*modepage_info));
6444 len_left = param_len - header_size - bd_len;
6445 len_used = header_size + bd_len;
6447 modepage_info->header.len_left = len_left;
6448 modepage_info->header.len_used = len_used;
6450 return (ctl_do_mode_select((union ctl_io *)ctsio));
6454 ctl_mode_sense(struct ctl_scsiio *ctsio)
6456 struct ctl_lun *lun;
6457 int pc, page_code, dbd, llba, subpage;
6458 int alloc_len, page_len, header_len, total_len;
6459 struct scsi_mode_block_descr *block_desc;
6460 struct ctl_page_index *page_index;
6468 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6470 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6472 if (lun->be_lun->lun_type != T_DIRECT)
6477 if (lun->flags & CTL_LUN_PR_RESERVED) {
6481 * XXX KDM need a lock here.
6483 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6484 if ((lun->res_type == SPR_TYPE_EX_AC
6485 && residx != lun->pr_res_idx)
6486 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6487 || lun->res_type == SPR_TYPE_EX_AC_AR)
6488 && !lun->per_res[residx].registered)) {
6489 ctl_set_reservation_conflict(ctsio);
6490 ctl_done((union ctl_io *)ctsio);
6491 return (CTL_RETVAL_COMPLETE);
6495 switch (ctsio->cdb[0]) {
6496 case MODE_SENSE_6: {
6497 struct scsi_mode_sense_6 *cdb;
6499 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6501 header_len = sizeof(struct scsi_mode_hdr_6);
6502 if (cdb->byte2 & SMS_DBD)
6505 header_len += sizeof(struct scsi_mode_block_descr);
6507 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6508 page_code = cdb->page & SMS_PAGE_CODE;
6509 subpage = cdb->subpage;
6510 alloc_len = cdb->length;
6513 case MODE_SENSE_10: {
6514 struct scsi_mode_sense_10 *cdb;
6516 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6518 header_len = sizeof(struct scsi_mode_hdr_10);
6520 if (cdb->byte2 & SMS_DBD)
6523 header_len += sizeof(struct scsi_mode_block_descr);
6524 if (cdb->byte2 & SMS10_LLBAA)
6526 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6527 page_code = cdb->page & SMS_PAGE_CODE;
6528 subpage = cdb->subpage;
6529 alloc_len = scsi_2btoul(cdb->length);
6533 ctl_set_invalid_opcode(ctsio);
6534 ctl_done((union ctl_io *)ctsio);
6535 return (CTL_RETVAL_COMPLETE);
6536 break; /* NOTREACHED */
6540 * We have to make a first pass through to calculate the size of
6541 * the pages that match the user's query. Then we allocate enough
6542 * memory to hold it, and actually copy the data into the buffer.
6544 switch (page_code) {
6545 case SMS_ALL_PAGES_PAGE: {
6551 * At the moment, values other than 0 and 0xff here are
6552 * reserved according to SPC-3.
6554 if ((subpage != SMS_SUBPAGE_PAGE_0)
6555 && (subpage != SMS_SUBPAGE_ALL)) {
6556 ctl_set_invalid_field(ctsio,
6562 ctl_done((union ctl_io *)ctsio);
6563 return (CTL_RETVAL_COMPLETE);
6566 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6567 if ((control_dev != 0)
6568 && (lun->mode_pages.index[i].page_flags &
6569 CTL_PAGE_FLAG_DISK_ONLY))
6573 * We don't use this subpage if the user didn't
6574 * request all subpages.
6576 if ((lun->mode_pages.index[i].subpage != 0)
6577 && (subpage == SMS_SUBPAGE_PAGE_0))
6581 printf("found page %#x len %d\n",
6582 lun->mode_pages.index[i].page_code &
6584 lun->mode_pages.index[i].page_len);
6586 page_len += lun->mode_pages.index[i].page_len;
6595 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6596 /* Look for the right page code */
6597 if ((lun->mode_pages.index[i].page_code &
6598 SMPH_PC_MASK) != page_code)
6601 /* Look for the right subpage or the subpage wildcard*/
6602 if ((lun->mode_pages.index[i].subpage != subpage)
6603 && (subpage != SMS_SUBPAGE_ALL))
6606 /* Make sure the page is supported for this dev type */
6607 if ((control_dev != 0)
6608 && (lun->mode_pages.index[i].page_flags &
6609 CTL_PAGE_FLAG_DISK_ONLY))
6613 printf("found page %#x len %d\n",
6614 lun->mode_pages.index[i].page_code &
6616 lun->mode_pages.index[i].page_len);
6619 page_len += lun->mode_pages.index[i].page_len;
6622 if (page_len == 0) {
6623 ctl_set_invalid_field(ctsio,
6629 ctl_done((union ctl_io *)ctsio);
6630 return (CTL_RETVAL_COMPLETE);
6636 total_len = header_len + page_len;
6638 printf("header_len = %d, page_len = %d, total_len = %d\n",
6639 header_len, page_len, total_len);
6642 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6643 ctsio->kern_sg_entries = 0;
6644 ctsio->kern_data_resid = 0;
6645 ctsio->kern_rel_offset = 0;
6646 if (total_len < alloc_len) {
6647 ctsio->residual = alloc_len - total_len;
6648 ctsio->kern_data_len = total_len;
6649 ctsio->kern_total_len = total_len;
6651 ctsio->residual = 0;
6652 ctsio->kern_data_len = alloc_len;
6653 ctsio->kern_total_len = alloc_len;
6656 switch (ctsio->cdb[0]) {
6657 case MODE_SENSE_6: {
6658 struct scsi_mode_hdr_6 *header;
6660 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6662 header->datalen = ctl_min(total_len - 1, 254);
6665 header->block_descr_len = 0;
6667 header->block_descr_len =
6668 sizeof(struct scsi_mode_block_descr);
6669 block_desc = (struct scsi_mode_block_descr *)&header[1];
6672 case MODE_SENSE_10: {
6673 struct scsi_mode_hdr_10 *header;
6676 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6678 datalen = ctl_min(total_len - 2, 65533);
6679 scsi_ulto2b(datalen, header->datalen);
6681 scsi_ulto2b(0, header->block_descr_len);
6683 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6684 header->block_descr_len);
6685 block_desc = (struct scsi_mode_block_descr *)&header[1];
6689 panic("invalid CDB type %#x", ctsio->cdb[0]);
6690 break; /* NOTREACHED */
6694 * If we've got a disk, use its blocksize in the block
6695 * descriptor. Otherwise, just set it to 0.
6698 if (control_dev != 0)
6699 scsi_ulto3b(lun->be_lun->blocksize,
6700 block_desc->block_len);
6702 scsi_ulto3b(0, block_desc->block_len);
6705 switch (page_code) {
6706 case SMS_ALL_PAGES_PAGE: {
6709 data_used = header_len;
6710 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6711 struct ctl_page_index *page_index;
6713 page_index = &lun->mode_pages.index[i];
6715 if ((control_dev != 0)
6716 && (page_index->page_flags &
6717 CTL_PAGE_FLAG_DISK_ONLY))
6721 * We don't use this subpage if the user didn't
6722 * request all subpages. We already checked (above)
6723 * to make sure the user only specified a subpage
6724 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6726 if ((page_index->subpage != 0)
6727 && (subpage == SMS_SUBPAGE_PAGE_0))
6731 * Call the handler, if it exists, to update the
6732 * page to the latest values.
6734 if (page_index->sense_handler != NULL)
6735 page_index->sense_handler(ctsio, page_index,pc);
6737 memcpy(ctsio->kern_data_ptr + data_used,
6738 page_index->page_data +
6739 (page_index->page_len * pc),
6740 page_index->page_len);
6741 data_used += page_index->page_len;
6748 data_used = header_len;
6750 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6751 struct ctl_page_index *page_index;
6753 page_index = &lun->mode_pages.index[i];
6755 /* Look for the right page code */
6756 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6759 /* Look for the right subpage or the subpage wildcard*/
6760 if ((page_index->subpage != subpage)
6761 && (subpage != SMS_SUBPAGE_ALL))
6764 /* Make sure the page is supported for this dev type */
6765 if ((control_dev != 0)
6766 && (page_index->page_flags &
6767 CTL_PAGE_FLAG_DISK_ONLY))
6771 * Call the handler, if it exists, to update the
6772 * page to the latest values.
6774 if (page_index->sense_handler != NULL)
6775 page_index->sense_handler(ctsio, page_index,pc);
6777 memcpy(ctsio->kern_data_ptr + data_used,
6778 page_index->page_data +
6779 (page_index->page_len * pc),
6780 page_index->page_len);
6781 data_used += page_index->page_len;
6787 ctsio->scsi_status = SCSI_STATUS_OK;
6789 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6790 ctsio->be_move_done = ctl_config_move_done;
6791 ctl_datamove((union ctl_io *)ctsio);
6793 return (CTL_RETVAL_COMPLETE);
6797 ctl_read_capacity(struct ctl_scsiio *ctsio)
6799 struct scsi_read_capacity *cdb;
6800 struct scsi_read_capacity_data *data;
6801 struct ctl_lun *lun;
6804 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6806 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6808 lba = scsi_4btoul(cdb->addr);
6809 if (((cdb->pmi & SRC_PMI) == 0)
6811 ctl_set_invalid_field(/*ctsio*/ ctsio,
6817 ctl_done((union ctl_io *)ctsio);
6818 return (CTL_RETVAL_COMPLETE);
6821 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6823 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6824 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6825 ctsio->residual = 0;
6826 ctsio->kern_data_len = sizeof(*data);
6827 ctsio->kern_total_len = sizeof(*data);
6828 ctsio->kern_data_resid = 0;
6829 ctsio->kern_rel_offset = 0;
6830 ctsio->kern_sg_entries = 0;
6833 * If the maximum LBA is greater than 0xfffffffe, the user must
6834 * issue a SERVICE ACTION IN (16) command, with the read capacity
6835 * serivce action set.
6837 if (lun->be_lun->maxlba > 0xfffffffe)
6838 scsi_ulto4b(0xffffffff, data->addr);
6840 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6843 * XXX KDM this may not be 512 bytes...
6845 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6847 ctsio->scsi_status = SCSI_STATUS_OK;
6849 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6850 ctsio->be_move_done = ctl_config_move_done;
6851 ctl_datamove((union ctl_io *)ctsio);
6853 return (CTL_RETVAL_COMPLETE);
6857 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6859 struct scsi_read_capacity_16 *cdb;
6860 struct scsi_read_capacity_data_long *data;
6861 struct ctl_lun *lun;
6865 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6867 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6869 alloc_len = scsi_4btoul(cdb->alloc_len);
6870 lba = scsi_8btou64(cdb->addr);
6872 if ((cdb->reladr & SRC16_PMI)
6874 ctl_set_invalid_field(/*ctsio*/ ctsio,
6880 ctl_done((union ctl_io *)ctsio);
6881 return (CTL_RETVAL_COMPLETE);
6884 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6886 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6887 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6889 if (sizeof(*data) < alloc_len) {
6890 ctsio->residual = alloc_len - sizeof(*data);
6891 ctsio->kern_data_len = sizeof(*data);
6892 ctsio->kern_total_len = sizeof(*data);
6894 ctsio->residual = 0;
6895 ctsio->kern_data_len = alloc_len;
6896 ctsio->kern_total_len = alloc_len;
6898 ctsio->kern_data_resid = 0;
6899 ctsio->kern_rel_offset = 0;
6900 ctsio->kern_sg_entries = 0;
6902 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6903 /* XXX KDM this may not be 512 bytes... */
6904 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6905 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6906 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6908 data->lalba_lbp[0] |= SRC16_LBPME;
6910 ctsio->scsi_status = SCSI_STATUS_OK;
6912 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6913 ctsio->be_move_done = ctl_config_move_done;
6914 ctl_datamove((union ctl_io *)ctsio);
6916 return (CTL_RETVAL_COMPLETE);
6920 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
6922 struct scsi_maintenance_in *cdb;
6924 int alloc_len, total_len = 0;
6925 int num_target_port_groups, single;
6926 struct ctl_lun *lun;
6927 struct ctl_softc *softc;
6928 struct scsi_target_group_data *rtg_ptr;
6929 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6930 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6931 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6933 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
6935 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6936 softc = control_softc;
6937 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6939 retval = CTL_RETVAL_COMPLETE;
6941 single = ctl_is_single;
6943 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6945 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6947 total_len = sizeof(struct scsi_target_group_data) +
6948 sizeof(struct scsi_target_port_group_descriptor) *
6949 num_target_port_groups +
6950 sizeof(struct scsi_target_port_descriptor) *
6951 NUM_PORTS_PER_GRP * num_target_port_groups;
6953 alloc_len = scsi_4btoul(cdb->length);
6955 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6957 ctsio->kern_sg_entries = 0;
6959 if (total_len < alloc_len) {
6960 ctsio->residual = alloc_len - total_len;
6961 ctsio->kern_data_len = total_len;
6962 ctsio->kern_total_len = total_len;
6964 ctsio->residual = 0;
6965 ctsio->kern_data_len = alloc_len;
6966 ctsio->kern_total_len = alloc_len;
6968 ctsio->kern_data_resid = 0;
6969 ctsio->kern_rel_offset = 0;
6971 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
6973 tpg_desc_ptr1 = &rtg_ptr->groups[0];
6974 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
6975 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
6976 &tp_desc_ptr1_1->desc_list[0];
6979 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
6980 &tp_desc_ptr1_2->desc_list[0];
6981 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
6982 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
6983 &tp_desc_ptr2_1->desc_list[0];
6985 tpg_desc_ptr2 = NULL;
6986 tp_desc_ptr2_1 = NULL;
6987 tp_desc_ptr2_2 = NULL;
6990 scsi_ulto4b(total_len - 4, rtg_ptr->length);
6992 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
6993 if (lun->flags & CTL_LUN_PRIMARY_SC) {
6994 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
6995 tpg_desc_ptr2->pref_state =
6996 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6998 tpg_desc_ptr1->pref_state =
6999 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7000 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7003 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7004 tpg_desc_ptr1->pref_state =
7005 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7006 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7008 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7009 tpg_desc_ptr2->pref_state =
7010 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7014 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7016 tpg_desc_ptr1->support = 0;
7017 tpg_desc_ptr1->target_port_group[1] = 1;
7018 tpg_desc_ptr1->status = TPG_IMPLICIT;
7019 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7022 tpg_desc_ptr2->support = 0;
7023 tpg_desc_ptr2->target_port_group[1] = 2;
7024 tpg_desc_ptr2->status = TPG_IMPLICIT;
7025 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7027 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7028 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7030 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7031 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7033 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7034 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7035 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7037 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7038 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7042 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7043 ctsio->be_move_done = ctl_config_move_done;
7045 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7046 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7047 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7048 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7049 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7051 ctl_datamove((union ctl_io *)ctsio);
7056 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7058 struct ctl_lun *lun;
7059 struct scsi_report_supported_opcodes *cdb;
7060 const struct ctl_cmd_entry *entry, *sentry;
7061 struct scsi_report_supported_opcodes_all *all;
7062 struct scsi_report_supported_opcodes_descr *descr;
7063 struct scsi_report_supported_opcodes_one *one;
7065 int alloc_len, total_len;
7066 int opcode, service_action, i, j, num;
7068 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7070 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7073 retval = CTL_RETVAL_COMPLETE;
7075 opcode = cdb->requested_opcode;
7076 service_action = scsi_2btoul(cdb->requested_service_action);
7077 switch (cdb->options & RSO_OPTIONS_MASK) {
7078 case RSO_OPTIONS_ALL:
7080 for (i = 0; i < 256; i++) {
7081 entry = &ctl_cmd_table[i];
7082 if (entry->flags & CTL_CMD_FLAG_SA5) {
7083 for (j = 0; j < 32; j++) {
7084 sentry = &((const struct ctl_cmd_entry *)
7086 if (ctl_cmd_applicable(
7087 lun->be_lun->lun_type, sentry))
7091 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7096 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7097 num * sizeof(struct scsi_report_supported_opcodes_descr);
7099 case RSO_OPTIONS_OC:
7100 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7101 ctl_set_invalid_field(/*ctsio*/ ctsio,
7107 ctl_done((union ctl_io *)ctsio);
7108 return (CTL_RETVAL_COMPLETE);
7110 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7112 case RSO_OPTIONS_OC_SA:
7113 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7114 service_action >= 32) {
7115 ctl_set_invalid_field(/*ctsio*/ ctsio,
7121 ctl_done((union ctl_io *)ctsio);
7122 return (CTL_RETVAL_COMPLETE);
7124 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7127 ctl_set_invalid_field(/*ctsio*/ ctsio,
7133 ctl_done((union ctl_io *)ctsio);
7134 return (CTL_RETVAL_COMPLETE);
7137 alloc_len = scsi_4btoul(cdb->length);
7139 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7141 ctsio->kern_sg_entries = 0;
7143 if (total_len < alloc_len) {
7144 ctsio->residual = alloc_len - total_len;
7145 ctsio->kern_data_len = total_len;
7146 ctsio->kern_total_len = total_len;
7148 ctsio->residual = 0;
7149 ctsio->kern_data_len = alloc_len;
7150 ctsio->kern_total_len = alloc_len;
7152 ctsio->kern_data_resid = 0;
7153 ctsio->kern_rel_offset = 0;
7155 switch (cdb->options & RSO_OPTIONS_MASK) {
7156 case RSO_OPTIONS_ALL:
7157 all = (struct scsi_report_supported_opcodes_all *)
7158 ctsio->kern_data_ptr;
7160 for (i = 0; i < 256; i++) {
7161 entry = &ctl_cmd_table[i];
7162 if (entry->flags & CTL_CMD_FLAG_SA5) {
7163 for (j = 0; j < 32; j++) {
7164 sentry = &((const struct ctl_cmd_entry *)
7166 if (!ctl_cmd_applicable(
7167 lun->be_lun->lun_type, sentry))
7169 descr = &all->descr[num++];
7171 scsi_ulto2b(j, descr->service_action);
7172 descr->flags = RSO_SERVACTV;
7173 scsi_ulto2b(sentry->length,
7177 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7180 descr = &all->descr[num++];
7182 scsi_ulto2b(0, descr->service_action);
7184 scsi_ulto2b(entry->length, descr->cdb_length);
7188 num * sizeof(struct scsi_report_supported_opcodes_descr),
7191 case RSO_OPTIONS_OC:
7192 one = (struct scsi_report_supported_opcodes_one *)
7193 ctsio->kern_data_ptr;
7194 entry = &ctl_cmd_table[opcode];
7196 case RSO_OPTIONS_OC_SA:
7197 one = (struct scsi_report_supported_opcodes_one *)
7198 ctsio->kern_data_ptr;
7199 entry = &ctl_cmd_table[opcode];
7200 entry = &((const struct ctl_cmd_entry *)
7201 entry->execute)[service_action];
7203 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7205 scsi_ulto2b(entry->length, one->cdb_length);
7206 one->cdb_usage[0] = opcode;
7207 memcpy(&one->cdb_usage[1], entry->usage,
7214 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7215 ctsio->be_move_done = ctl_config_move_done;
7217 ctl_datamove((union ctl_io *)ctsio);
7222 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7224 struct ctl_lun *lun;
7225 struct scsi_report_supported_tmf *cdb;
7226 struct scsi_report_supported_tmf_data *data;
7228 int alloc_len, total_len;
7230 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7232 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7235 retval = CTL_RETVAL_COMPLETE;
7237 total_len = sizeof(struct scsi_report_supported_tmf_data);
7238 alloc_len = scsi_4btoul(cdb->length);
7240 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7242 ctsio->kern_sg_entries = 0;
7244 if (total_len < alloc_len) {
7245 ctsio->residual = alloc_len - total_len;
7246 ctsio->kern_data_len = total_len;
7247 ctsio->kern_total_len = total_len;
7249 ctsio->residual = 0;
7250 ctsio->kern_data_len = alloc_len;
7251 ctsio->kern_total_len = alloc_len;
7253 ctsio->kern_data_resid = 0;
7254 ctsio->kern_rel_offset = 0;
7256 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7257 data->byte1 |= RST_ATS | RST_LURS | RST_TRS;
7259 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7260 ctsio->be_move_done = ctl_config_move_done;
7262 ctl_datamove((union ctl_io *)ctsio);
7267 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7269 struct ctl_lun *lun;
7270 struct scsi_report_timestamp *cdb;
7271 struct scsi_report_timestamp_data *data;
7275 int alloc_len, total_len;
7277 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7279 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7280 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7282 retval = CTL_RETVAL_COMPLETE;
7284 total_len = sizeof(struct scsi_report_timestamp_data);
7285 alloc_len = scsi_4btoul(cdb->length);
7287 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7289 ctsio->kern_sg_entries = 0;
7291 if (total_len < alloc_len) {
7292 ctsio->residual = alloc_len - total_len;
7293 ctsio->kern_data_len = total_len;
7294 ctsio->kern_total_len = total_len;
7296 ctsio->residual = 0;
7297 ctsio->kern_data_len = alloc_len;
7298 ctsio->kern_total_len = alloc_len;
7300 ctsio->kern_data_resid = 0;
7301 ctsio->kern_rel_offset = 0;
7303 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7304 scsi_ulto2b(sizeof(*data) - 2, data->length);
7305 data->origin = RTS_ORIG_OUTSIDE;
7307 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7308 scsi_ulto4b(timestamp >> 16, data->timestamp);
7309 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7311 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7312 ctsio->be_move_done = ctl_config_move_done;
7314 ctl_datamove((union ctl_io *)ctsio);
7319 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7321 struct scsi_per_res_in *cdb;
7322 int alloc_len, total_len = 0;
7323 /* struct scsi_per_res_in_rsrv in_data; */
7324 struct ctl_lun *lun;
7325 struct ctl_softc *softc;
7327 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7329 softc = control_softc;
7331 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7333 alloc_len = scsi_2btoul(cdb->length);
7335 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7338 mtx_lock(&lun->lun_lock);
7339 switch (cdb->action) {
7340 case SPRI_RK: /* read keys */
7341 total_len = sizeof(struct scsi_per_res_in_keys) +
7343 sizeof(struct scsi_per_res_key);
7345 case SPRI_RR: /* read reservation */
7346 if (lun->flags & CTL_LUN_PR_RESERVED)
7347 total_len = sizeof(struct scsi_per_res_in_rsrv);
7349 total_len = sizeof(struct scsi_per_res_in_header);
7351 case SPRI_RC: /* report capabilities */
7352 total_len = sizeof(struct scsi_per_res_cap);
7355 panic("Invalid PR type %x", cdb->action);
7357 mtx_unlock(&lun->lun_lock);
7359 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7361 if (total_len < alloc_len) {
7362 ctsio->residual = alloc_len - total_len;
7363 ctsio->kern_data_len = total_len;
7364 ctsio->kern_total_len = total_len;
7366 ctsio->residual = 0;
7367 ctsio->kern_data_len = alloc_len;
7368 ctsio->kern_total_len = alloc_len;
7371 ctsio->kern_data_resid = 0;
7372 ctsio->kern_rel_offset = 0;
7373 ctsio->kern_sg_entries = 0;
7375 mtx_lock(&lun->lun_lock);
7376 switch (cdb->action) {
7377 case SPRI_RK: { // read keys
7378 struct scsi_per_res_in_keys *res_keys;
7381 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7384 * We had to drop the lock to allocate our buffer, which
7385 * leaves time for someone to come in with another
7386 * persistent reservation. (That is unlikely, though,
7387 * since this should be the only persistent reservation
7388 * command active right now.)
7390 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7391 (lun->pr_key_count *
7392 sizeof(struct scsi_per_res_key)))){
7393 mtx_unlock(&lun->lun_lock);
7394 free(ctsio->kern_data_ptr, M_CTL);
7395 printf("%s: reservation length changed, retrying\n",
7400 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7402 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7403 lun->pr_key_count, res_keys->header.length);
7405 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7406 if (!lun->per_res[i].registered)
7410 * We used lun->pr_key_count to calculate the
7411 * size to allocate. If it turns out the number of
7412 * initiators with the registered flag set is
7413 * larger than that (i.e. they haven't been kept in
7414 * sync), we've got a problem.
7416 if (key_count >= lun->pr_key_count) {
7418 csevent_log(CSC_CTL | CSC_SHELF_SW |
7420 csevent_LogType_Fault,
7421 csevent_AlertLevel_Yellow,
7422 csevent_FRU_ShelfController,
7423 csevent_FRU_Firmware,
7424 csevent_FRU_Unknown,
7425 "registered keys %d >= key "
7426 "count %d", key_count,
7432 memcpy(res_keys->keys[key_count].key,
7433 lun->per_res[i].res_key.key,
7434 ctl_min(sizeof(res_keys->keys[key_count].key),
7435 sizeof(lun->per_res[i].res_key)));
7440 case SPRI_RR: { // read reservation
7441 struct scsi_per_res_in_rsrv *res;
7442 int tmp_len, header_only;
7444 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7446 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7448 if (lun->flags & CTL_LUN_PR_RESERVED)
7450 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7451 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7452 res->header.length);
7455 tmp_len = sizeof(struct scsi_per_res_in_header);
7456 scsi_ulto4b(0, res->header.length);
7461 * We had to drop the lock to allocate our buffer, which
7462 * leaves time for someone to come in with another
7463 * persistent reservation. (That is unlikely, though,
7464 * since this should be the only persistent reservation
7465 * command active right now.)
7467 if (tmp_len != total_len) {
7468 mtx_unlock(&lun->lun_lock);
7469 free(ctsio->kern_data_ptr, M_CTL);
7470 printf("%s: reservation status changed, retrying\n",
7476 * No reservation held, so we're done.
7478 if (header_only != 0)
7482 * If the registration is an All Registrants type, the key
7483 * is 0, since it doesn't really matter.
7485 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7486 memcpy(res->data.reservation,
7487 &lun->per_res[lun->pr_res_idx].res_key,
7488 sizeof(struct scsi_per_res_key));
7490 res->data.scopetype = lun->res_type;
7493 case SPRI_RC: //report capabilities
7495 struct scsi_per_res_cap *res_cap;
7498 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7499 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7500 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7501 type_mask = SPRI_TM_WR_EX_AR |
7507 scsi_ulto2b(type_mask, res_cap->type_mask);
7510 case SPRI_RS: //read full status
7513 * This is a bug, because we just checked for this above,
7514 * and should have returned an error.
7516 panic("Invalid PR type %x", cdb->action);
7517 break; /* NOTREACHED */
7519 mtx_unlock(&lun->lun_lock);
7521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7522 ctsio->be_move_done = ctl_config_move_done;
7524 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7525 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7526 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7527 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7528 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7530 ctl_datamove((union ctl_io *)ctsio);
7532 return (CTL_RETVAL_COMPLETE);
7536 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7540 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7541 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7542 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7543 struct scsi_per_res_out_parms* param)
7545 union ctl_ha_msg persis_io;
7551 mtx_lock(&lun->lun_lock);
7552 if (sa_res_key == 0) {
7553 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7554 /* validate scope and type */
7555 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7557 mtx_unlock(&lun->lun_lock);
7558 ctl_set_invalid_field(/*ctsio*/ ctsio,
7564 ctl_done((union ctl_io *)ctsio);
7568 if (type>8 || type==2 || type==4 || type==0) {
7569 mtx_unlock(&lun->lun_lock);
7570 ctl_set_invalid_field(/*ctsio*/ ctsio,
7576 ctl_done((union ctl_io *)ctsio);
7580 /* temporarily unregister this nexus */
7581 lun->per_res[residx].registered = 0;
7584 * Unregister everybody else and build UA for
7587 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7588 if (lun->per_res[i].registered == 0)
7592 && i <CTL_MAX_INITIATORS)
7593 lun->pending_sense[i].ua_pending |=
7595 else if (persis_offset
7596 && i >= persis_offset)
7597 lun->pending_sense[i-persis_offset
7600 lun->per_res[i].registered = 0;
7601 memset(&lun->per_res[i].res_key, 0,
7602 sizeof(struct scsi_per_res_key));
7604 lun->per_res[residx].registered = 1;
7605 lun->pr_key_count = 1;
7606 lun->res_type = type;
7607 if (lun->res_type != SPR_TYPE_WR_EX_AR
7608 && lun->res_type != SPR_TYPE_EX_AC_AR)
7609 lun->pr_res_idx = residx;
7611 /* send msg to other side */
7612 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7613 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7614 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7615 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7616 persis_io.pr.pr_info.res_type = type;
7617 memcpy(persis_io.pr.pr_info.sa_res_key,
7618 param->serv_act_res_key,
7619 sizeof(param->serv_act_res_key));
7620 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7621 &persis_io, sizeof(persis_io), 0)) >
7622 CTL_HA_STATUS_SUCCESS) {
7623 printf("CTL:Persis Out error returned "
7624 "from ctl_ha_msg_send %d\n",
7628 /* not all registrants */
7629 mtx_unlock(&lun->lun_lock);
7630 free(ctsio->kern_data_ptr, M_CTL);
7631 ctl_set_invalid_field(ctsio,
7637 ctl_done((union ctl_io *)ctsio);
7640 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7641 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7644 if (res_key == sa_res_key) {
7647 * The spec implies this is not good but doesn't
7648 * say what to do. There are two choices either
7649 * generate a res conflict or check condition
7650 * with illegal field in parameter data. Since
7651 * that is what is done when the sa_res_key is
7652 * zero I'll take that approach since this has
7653 * to do with the sa_res_key.
7655 mtx_unlock(&lun->lun_lock);
7656 free(ctsio->kern_data_ptr, M_CTL);
7657 ctl_set_invalid_field(ctsio,
7663 ctl_done((union ctl_io *)ctsio);
7667 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7668 if (lun->per_res[i].registered
7669 && memcmp(param->serv_act_res_key,
7670 lun->per_res[i].res_key.key,
7671 sizeof(struct scsi_per_res_key)) != 0)
7675 lun->per_res[i].registered = 0;
7676 memset(&lun->per_res[i].res_key, 0,
7677 sizeof(struct scsi_per_res_key));
7678 lun->pr_key_count--;
7681 && i < CTL_MAX_INITIATORS)
7682 lun->pending_sense[i].ua_pending |=
7684 else if (persis_offset
7685 && i >= persis_offset)
7686 lun->pending_sense[i-persis_offset].ua_pending|=
7690 mtx_unlock(&lun->lun_lock);
7691 free(ctsio->kern_data_ptr, M_CTL);
7692 ctl_set_reservation_conflict(ctsio);
7693 ctl_done((union ctl_io *)ctsio);
7694 return (CTL_RETVAL_COMPLETE);
7696 /* send msg to other side */
7697 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7698 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7699 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7700 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7701 persis_io.pr.pr_info.res_type = type;
7702 memcpy(persis_io.pr.pr_info.sa_res_key,
7703 param->serv_act_res_key,
7704 sizeof(param->serv_act_res_key));
7705 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7706 &persis_io, sizeof(persis_io), 0)) >
7707 CTL_HA_STATUS_SUCCESS) {
7708 printf("CTL:Persis Out error returned from "
7709 "ctl_ha_msg_send %d\n", isc_retval);
7712 /* Reserved but not all registrants */
7713 /* sa_res_key is res holder */
7714 if (memcmp(param->serv_act_res_key,
7715 lun->per_res[lun->pr_res_idx].res_key.key,
7716 sizeof(struct scsi_per_res_key)) == 0) {
7717 /* validate scope and type */
7718 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7720 mtx_unlock(&lun->lun_lock);
7721 ctl_set_invalid_field(/*ctsio*/ ctsio,
7727 ctl_done((union ctl_io *)ctsio);
7731 if (type>8 || type==2 || type==4 || type==0) {
7732 mtx_unlock(&lun->lun_lock);
7733 ctl_set_invalid_field(/*ctsio*/ ctsio,
7739 ctl_done((union ctl_io *)ctsio);
7745 * if sa_res_key != res_key remove all
7746 * registrants w/sa_res_key and generate UA
7747 * for these registrants(Registrations
7748 * Preempted) if it wasn't an exclusive
7749 * reservation generate UA(Reservations
7750 * Preempted) for all other registered nexuses
7751 * if the type has changed. Establish the new
7752 * reservation and holder. If res_key and
7753 * sa_res_key are the same do the above
7754 * except don't unregister the res holder.
7758 * Temporarily unregister so it won't get
7759 * removed or UA generated
7761 lun->per_res[residx].registered = 0;
7762 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7763 if (lun->per_res[i].registered == 0)
7766 if (memcmp(param->serv_act_res_key,
7767 lun->per_res[i].res_key.key,
7768 sizeof(struct scsi_per_res_key)) == 0) {
7769 lun->per_res[i].registered = 0;
7770 memset(&lun->per_res[i].res_key,
7772 sizeof(struct scsi_per_res_key));
7773 lun->pr_key_count--;
7776 && i < CTL_MAX_INITIATORS)
7777 lun->pending_sense[i
7780 else if (persis_offset
7781 && i >= persis_offset)
7783 i-persis_offset].ua_pending |=
7785 } else if (type != lun->res_type
7786 && (lun->res_type == SPR_TYPE_WR_EX_RO
7787 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7789 && i < CTL_MAX_INITIATORS)
7790 lun->pending_sense[i
7793 else if (persis_offset
7794 && i >= persis_offset)
7801 lun->per_res[residx].registered = 1;
7802 lun->res_type = type;
7803 if (lun->res_type != SPR_TYPE_WR_EX_AR
7804 && lun->res_type != SPR_TYPE_EX_AC_AR)
7805 lun->pr_res_idx = residx;
7808 CTL_PR_ALL_REGISTRANTS;
7810 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7811 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7812 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7813 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7814 persis_io.pr.pr_info.res_type = type;
7815 memcpy(persis_io.pr.pr_info.sa_res_key,
7816 param->serv_act_res_key,
7817 sizeof(param->serv_act_res_key));
7818 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7819 &persis_io, sizeof(persis_io), 0)) >
7820 CTL_HA_STATUS_SUCCESS) {
7821 printf("CTL:Persis Out error returned "
7822 "from ctl_ha_msg_send %d\n",
7827 * sa_res_key is not the res holder just
7828 * remove registrants
7832 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7833 if (memcmp(param->serv_act_res_key,
7834 lun->per_res[i].res_key.key,
7835 sizeof(struct scsi_per_res_key)) != 0)
7839 lun->per_res[i].registered = 0;
7840 memset(&lun->per_res[i].res_key, 0,
7841 sizeof(struct scsi_per_res_key));
7842 lun->pr_key_count--;
7845 && i < CTL_MAX_INITIATORS)
7846 lun->pending_sense[i].ua_pending |=
7848 else if (persis_offset
7849 && i >= persis_offset)
7851 i-persis_offset].ua_pending |=
7856 mtx_unlock(&lun->lun_lock);
7857 free(ctsio->kern_data_ptr, M_CTL);
7858 ctl_set_reservation_conflict(ctsio);
7859 ctl_done((union ctl_io *)ctsio);
7862 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7863 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7864 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7865 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7866 persis_io.pr.pr_info.res_type = type;
7867 memcpy(persis_io.pr.pr_info.sa_res_key,
7868 param->serv_act_res_key,
7869 sizeof(param->serv_act_res_key));
7870 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7871 &persis_io, sizeof(persis_io), 0)) >
7872 CTL_HA_STATUS_SUCCESS) {
7873 printf("CTL:Persis Out error returned "
7874 "from ctl_ha_msg_send %d\n",
7880 lun->PRGeneration++;
7881 mtx_unlock(&lun->lun_lock);
7887 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7891 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7892 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7893 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7894 msg->pr.pr_info.sa_res_key,
7895 sizeof(struct scsi_per_res_key)) != 0) {
7896 uint64_t sa_res_key;
7897 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7899 if (sa_res_key == 0) {
7900 /* temporarily unregister this nexus */
7901 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7904 * Unregister everybody else and build UA for
7907 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7908 if (lun->per_res[i].registered == 0)
7912 && i < CTL_MAX_INITIATORS)
7913 lun->pending_sense[i].ua_pending |=
7915 else if (persis_offset && i >= persis_offset)
7916 lun->pending_sense[i -
7917 persis_offset].ua_pending |=
7919 lun->per_res[i].registered = 0;
7920 memset(&lun->per_res[i].res_key, 0,
7921 sizeof(struct scsi_per_res_key));
7924 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7925 lun->pr_key_count = 1;
7926 lun->res_type = msg->pr.pr_info.res_type;
7927 if (lun->res_type != SPR_TYPE_WR_EX_AR
7928 && lun->res_type != SPR_TYPE_EX_AC_AR)
7929 lun->pr_res_idx = msg->pr.pr_info.residx;
7931 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7932 if (memcmp(msg->pr.pr_info.sa_res_key,
7933 lun->per_res[i].res_key.key,
7934 sizeof(struct scsi_per_res_key)) != 0)
7937 lun->per_res[i].registered = 0;
7938 memset(&lun->per_res[i].res_key, 0,
7939 sizeof(struct scsi_per_res_key));
7940 lun->pr_key_count--;
7943 && i < persis_offset)
7944 lun->pending_sense[i].ua_pending |=
7946 else if (persis_offset
7947 && i >= persis_offset)
7948 lun->pending_sense[i -
7949 persis_offset].ua_pending |=
7955 * Temporarily unregister so it won't get removed
7958 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7959 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7960 if (lun->per_res[i].registered == 0)
7963 if (memcmp(msg->pr.pr_info.sa_res_key,
7964 lun->per_res[i].res_key.key,
7965 sizeof(struct scsi_per_res_key)) == 0) {
7966 lun->per_res[i].registered = 0;
7967 memset(&lun->per_res[i].res_key, 0,
7968 sizeof(struct scsi_per_res_key));
7969 lun->pr_key_count--;
7971 && i < CTL_MAX_INITIATORS)
7972 lun->pending_sense[i].ua_pending |=
7974 else if (persis_offset
7975 && i >= persis_offset)
7976 lun->pending_sense[i -
7977 persis_offset].ua_pending |=
7979 } else if (msg->pr.pr_info.res_type != lun->res_type
7980 && (lun->res_type == SPR_TYPE_WR_EX_RO
7981 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7983 && i < persis_offset)
7984 lun->pending_sense[i
7987 else if (persis_offset
7988 && i >= persis_offset)
7989 lun->pending_sense[i -
7990 persis_offset].ua_pending |=
7994 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7995 lun->res_type = msg->pr.pr_info.res_type;
7996 if (lun->res_type != SPR_TYPE_WR_EX_AR
7997 && lun->res_type != SPR_TYPE_EX_AC_AR)
7998 lun->pr_res_idx = msg->pr.pr_info.residx;
8000 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8002 lun->PRGeneration++;
8008 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8012 u_int32_t param_len;
8013 struct scsi_per_res_out *cdb;
8014 struct ctl_lun *lun;
8015 struct scsi_per_res_out_parms* param;
8016 struct ctl_softc *softc;
8018 uint64_t res_key, sa_res_key;
8020 union ctl_ha_msg persis_io;
8023 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8025 retval = CTL_RETVAL_COMPLETE;
8027 softc = control_softc;
8029 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8030 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8033 * We only support whole-LUN scope. The scope & type are ignored for
8034 * register, register and ignore existing key and clear.
8035 * We sometimes ignore scope and type on preempts too!!
8036 * Verify reservation type here as well.
8038 type = cdb->scope_type & SPR_TYPE_MASK;
8039 if ((cdb->action == SPRO_RESERVE)
8040 || (cdb->action == SPRO_RELEASE)) {
8041 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8042 ctl_set_invalid_field(/*ctsio*/ ctsio,
8048 ctl_done((union ctl_io *)ctsio);
8049 return (CTL_RETVAL_COMPLETE);
8052 if (type>8 || type==2 || type==4 || type==0) {
8053 ctl_set_invalid_field(/*ctsio*/ ctsio,
8059 ctl_done((union ctl_io *)ctsio);
8060 return (CTL_RETVAL_COMPLETE);
8064 param_len = scsi_4btoul(cdb->length);
8066 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8067 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8068 ctsio->kern_data_len = param_len;
8069 ctsio->kern_total_len = param_len;
8070 ctsio->kern_data_resid = 0;
8071 ctsio->kern_rel_offset = 0;
8072 ctsio->kern_sg_entries = 0;
8073 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8074 ctsio->be_move_done = ctl_config_move_done;
8075 ctl_datamove((union ctl_io *)ctsio);
8077 return (CTL_RETVAL_COMPLETE);
8080 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8082 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8083 res_key = scsi_8btou64(param->res_key.key);
8084 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8087 * Validate the reservation key here except for SPRO_REG_IGNO
8088 * This must be done for all other service actions
8090 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8091 mtx_lock(&lun->lun_lock);
8092 if (lun->per_res[residx].registered) {
8093 if (memcmp(param->res_key.key,
8094 lun->per_res[residx].res_key.key,
8095 ctl_min(sizeof(param->res_key),
8096 sizeof(lun->per_res[residx].res_key))) != 0) {
8098 * The current key passed in doesn't match
8099 * the one the initiator previously
8102 mtx_unlock(&lun->lun_lock);
8103 free(ctsio->kern_data_ptr, M_CTL);
8104 ctl_set_reservation_conflict(ctsio);
8105 ctl_done((union ctl_io *)ctsio);
8106 return (CTL_RETVAL_COMPLETE);
8108 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8110 * We are not registered
8112 mtx_unlock(&lun->lun_lock);
8113 free(ctsio->kern_data_ptr, M_CTL);
8114 ctl_set_reservation_conflict(ctsio);
8115 ctl_done((union ctl_io *)ctsio);
8116 return (CTL_RETVAL_COMPLETE);
8117 } else if (res_key != 0) {
8119 * We are not registered and trying to register but
8120 * the register key isn't zero.
8122 mtx_unlock(&lun->lun_lock);
8123 free(ctsio->kern_data_ptr, M_CTL);
8124 ctl_set_reservation_conflict(ctsio);
8125 ctl_done((union ctl_io *)ctsio);
8126 return (CTL_RETVAL_COMPLETE);
8128 mtx_unlock(&lun->lun_lock);
8131 switch (cdb->action & SPRO_ACTION_MASK) {
8133 case SPRO_REG_IGNO: {
8136 printf("Registration received\n");
8140 * We don't support any of these options, as we report in
8141 * the read capabilities request (see
8142 * ctl_persistent_reserve_in(), above).
8144 if ((param->flags & SPR_SPEC_I_PT)
8145 || (param->flags & SPR_ALL_TG_PT)
8146 || (param->flags & SPR_APTPL)) {
8149 if (param->flags & SPR_APTPL)
8151 else if (param->flags & SPR_ALL_TG_PT)
8153 else /* SPR_SPEC_I_PT */
8156 free(ctsio->kern_data_ptr, M_CTL);
8157 ctl_set_invalid_field(ctsio,
8163 ctl_done((union ctl_io *)ctsio);
8164 return (CTL_RETVAL_COMPLETE);
8167 mtx_lock(&lun->lun_lock);
8170 * The initiator wants to clear the
8173 if (sa_res_key == 0) {
8175 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8176 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8177 && !lun->per_res[residx].registered)) {
8178 mtx_unlock(&lun->lun_lock);
8182 lun->per_res[residx].registered = 0;
8183 memset(&lun->per_res[residx].res_key,
8184 0, sizeof(lun->per_res[residx].res_key));
8185 lun->pr_key_count--;
8187 if (residx == lun->pr_res_idx) {
8188 lun->flags &= ~CTL_LUN_PR_RESERVED;
8189 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8191 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8192 || lun->res_type == SPR_TYPE_EX_AC_RO)
8193 && lun->pr_key_count) {
8195 * If the reservation is a registrants
8196 * only type we need to generate a UA
8197 * for other registered inits. The
8198 * sense code should be RESERVATIONS
8202 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8204 i+persis_offset].registered
8207 lun->pending_sense[i
8213 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8214 if (lun->pr_key_count==0) {
8215 lun->flags &= ~CTL_LUN_PR_RESERVED;
8217 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8220 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8221 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8222 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8223 persis_io.pr.pr_info.residx = residx;
8224 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8225 &persis_io, sizeof(persis_io), 0 )) >
8226 CTL_HA_STATUS_SUCCESS) {
8227 printf("CTL:Persis Out error returned from "
8228 "ctl_ha_msg_send %d\n", isc_retval);
8230 } else /* sa_res_key != 0 */ {
8233 * If we aren't registered currently then increment
8234 * the key count and set the registered flag.
8236 if (!lun->per_res[residx].registered) {
8237 lun->pr_key_count++;
8238 lun->per_res[residx].registered = 1;
8241 memcpy(&lun->per_res[residx].res_key,
8242 param->serv_act_res_key,
8243 ctl_min(sizeof(param->serv_act_res_key),
8244 sizeof(lun->per_res[residx].res_key)));
8246 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8247 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8248 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8249 persis_io.pr.pr_info.residx = residx;
8250 memcpy(persis_io.pr.pr_info.sa_res_key,
8251 param->serv_act_res_key,
8252 sizeof(param->serv_act_res_key));
8253 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8254 &persis_io, sizeof(persis_io), 0)) >
8255 CTL_HA_STATUS_SUCCESS) {
8256 printf("CTL:Persis Out error returned from "
8257 "ctl_ha_msg_send %d\n", isc_retval);
8260 lun->PRGeneration++;
8261 mtx_unlock(&lun->lun_lock);
8267 printf("Reserve executed type %d\n", type);
8269 mtx_lock(&lun->lun_lock);
8270 if (lun->flags & CTL_LUN_PR_RESERVED) {
8272 * if this isn't the reservation holder and it's
8273 * not a "all registrants" type or if the type is
8274 * different then we have a conflict
8276 if ((lun->pr_res_idx != residx
8277 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8278 || lun->res_type != type) {
8279 mtx_unlock(&lun->lun_lock);
8280 free(ctsio->kern_data_ptr, M_CTL);
8281 ctl_set_reservation_conflict(ctsio);
8282 ctl_done((union ctl_io *)ctsio);
8283 return (CTL_RETVAL_COMPLETE);
8285 mtx_unlock(&lun->lun_lock);
8286 } else /* create a reservation */ {
8288 * If it's not an "all registrants" type record
8289 * reservation holder
8291 if (type != SPR_TYPE_WR_EX_AR
8292 && type != SPR_TYPE_EX_AC_AR)
8293 lun->pr_res_idx = residx; /* Res holder */
8295 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8297 lun->flags |= CTL_LUN_PR_RESERVED;
8298 lun->res_type = type;
8300 mtx_unlock(&lun->lun_lock);
8302 /* send msg to other side */
8303 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8304 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8305 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8306 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8307 persis_io.pr.pr_info.res_type = type;
8308 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8309 &persis_io, sizeof(persis_io), 0)) >
8310 CTL_HA_STATUS_SUCCESS) {
8311 printf("CTL:Persis Out error returned from "
8312 "ctl_ha_msg_send %d\n", isc_retval);
8318 mtx_lock(&lun->lun_lock);
8319 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8320 /* No reservation exists return good status */
8321 mtx_unlock(&lun->lun_lock);
8325 * Is this nexus a reservation holder?
8327 if (lun->pr_res_idx != residx
8328 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8330 * not a res holder return good status but
8333 mtx_unlock(&lun->lun_lock);
8337 if (lun->res_type != type) {
8338 mtx_unlock(&lun->lun_lock);
8339 free(ctsio->kern_data_ptr, M_CTL);
8340 ctl_set_illegal_pr_release(ctsio);
8341 ctl_done((union ctl_io *)ctsio);
8342 return (CTL_RETVAL_COMPLETE);
8345 /* okay to release */
8346 lun->flags &= ~CTL_LUN_PR_RESERVED;
8347 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8351 * if this isn't an exclusive access
8352 * res generate UA for all other
8355 if (type != SPR_TYPE_EX_AC
8356 && type != SPR_TYPE_WR_EX) {
8358 * temporarily unregister so we don't generate UA
8360 lun->per_res[residx].registered = 0;
8362 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8363 if (lun->per_res[i+persis_offset].registered
8366 lun->pending_sense[i].ua_pending |=
8370 lun->per_res[residx].registered = 1;
8372 mtx_unlock(&lun->lun_lock);
8373 /* Send msg to other side */
8374 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8375 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8376 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8377 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8378 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8379 printf("CTL:Persis Out error returned from "
8380 "ctl_ha_msg_send %d\n", isc_retval);
8385 /* send msg to other side */
8387 mtx_lock(&lun->lun_lock);
8388 lun->flags &= ~CTL_LUN_PR_RESERVED;
8390 lun->pr_key_count = 0;
8391 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8394 memset(&lun->per_res[residx].res_key,
8395 0, sizeof(lun->per_res[residx].res_key));
8396 lun->per_res[residx].registered = 0;
8398 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8399 if (lun->per_res[i].registered) {
8400 if (!persis_offset && i < CTL_MAX_INITIATORS)
8401 lun->pending_sense[i].ua_pending |=
8403 else if (persis_offset && i >= persis_offset)
8404 lun->pending_sense[i-persis_offset
8405 ].ua_pending |= CTL_UA_RES_PREEMPT;
8407 memset(&lun->per_res[i].res_key,
8408 0, sizeof(struct scsi_per_res_key));
8409 lun->per_res[i].registered = 0;
8411 lun->PRGeneration++;
8412 mtx_unlock(&lun->lun_lock);
8413 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8414 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8415 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8416 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8417 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8418 printf("CTL:Persis Out error returned from "
8419 "ctl_ha_msg_send %d\n", isc_retval);
8423 case SPRO_PREEMPT: {
8426 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8427 residx, ctsio, cdb, param);
8429 return (CTL_RETVAL_COMPLETE);
8433 panic("Invalid PR type %x", cdb->action);
8437 free(ctsio->kern_data_ptr, M_CTL);
8438 ctl_set_success(ctsio);
8439 ctl_done((union ctl_io *)ctsio);
8445 * This routine is for handling a message from the other SC pertaining to
8446 * persistent reserve out. All the error checking will have been done
8447 * so only perorming the action need be done here to keep the two
8451 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8453 struct ctl_lun *lun;
8454 struct ctl_softc *softc;
8458 softc = control_softc;
8460 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8461 lun = softc->ctl_luns[targ_lun];
8462 mtx_lock(&lun->lun_lock);
8463 switch(msg->pr.pr_info.action) {
8464 case CTL_PR_REG_KEY:
8465 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8466 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8467 lun->pr_key_count++;
8469 lun->PRGeneration++;
8470 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8471 msg->pr.pr_info.sa_res_key,
8472 sizeof(struct scsi_per_res_key));
8475 case CTL_PR_UNREG_KEY:
8476 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8477 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8478 0, sizeof(struct scsi_per_res_key));
8479 lun->pr_key_count--;
8481 /* XXX Need to see if the reservation has been released */
8482 /* if so do we need to generate UA? */
8483 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8484 lun->flags &= ~CTL_LUN_PR_RESERVED;
8485 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8487 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8488 || lun->res_type == SPR_TYPE_EX_AC_RO)
8489 && lun->pr_key_count) {
8491 * If the reservation is a registrants
8492 * only type we need to generate a UA
8493 * for other registered inits. The
8494 * sense code should be RESERVATIONS
8498 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8500 persis_offset].registered == 0)
8503 lun->pending_sense[i
8509 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8510 if (lun->pr_key_count==0) {
8511 lun->flags &= ~CTL_LUN_PR_RESERVED;
8513 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8516 lun->PRGeneration++;
8519 case CTL_PR_RESERVE:
8520 lun->flags |= CTL_LUN_PR_RESERVED;
8521 lun->res_type = msg->pr.pr_info.res_type;
8522 lun->pr_res_idx = msg->pr.pr_info.residx;
8526 case CTL_PR_RELEASE:
8528 * if this isn't an exclusive access res generate UA for all
8529 * other registrants.
8531 if (lun->res_type != SPR_TYPE_EX_AC
8532 && lun->res_type != SPR_TYPE_WR_EX) {
8533 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8534 if (lun->per_res[i+persis_offset].registered)
8535 lun->pending_sense[i].ua_pending |=
8539 lun->flags &= ~CTL_LUN_PR_RESERVED;
8540 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8544 case CTL_PR_PREEMPT:
8545 ctl_pro_preempt_other(lun, msg);
8548 lun->flags &= ~CTL_LUN_PR_RESERVED;
8550 lun->pr_key_count = 0;
8551 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8553 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8554 if (lun->per_res[i].registered == 0)
8557 && i < CTL_MAX_INITIATORS)
8558 lun->pending_sense[i].ua_pending |=
8560 else if (persis_offset
8561 && i >= persis_offset)
8562 lun->pending_sense[i-persis_offset].ua_pending|=
8564 memset(&lun->per_res[i].res_key, 0,
8565 sizeof(struct scsi_per_res_key));
8566 lun->per_res[i].registered = 0;
8568 lun->PRGeneration++;
8572 mtx_unlock(&lun->lun_lock);
8576 ctl_read_write(struct ctl_scsiio *ctsio)
8578 struct ctl_lun *lun;
8579 struct ctl_lba_len_flags *lbalen;
8581 uint32_t num_blocks;
8586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8588 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8593 retval = CTL_RETVAL_COMPLETE;
8595 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8596 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8597 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8601 * XXX KDM need a lock here.
8603 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8604 if ((lun->res_type == SPR_TYPE_EX_AC
8605 && residx != lun->pr_res_idx)
8606 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8607 || lun->res_type == SPR_TYPE_EX_AC_AR)
8608 && !lun->per_res[residx].registered)) {
8609 ctl_set_reservation_conflict(ctsio);
8610 ctl_done((union ctl_io *)ctsio);
8611 return (CTL_RETVAL_COMPLETE);
8615 switch (ctsio->cdb[0]) {
8618 struct scsi_rw_6 *cdb;
8620 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8622 lba = scsi_3btoul(cdb->addr);
8623 /* only 5 bits are valid in the most significant address byte */
8625 num_blocks = cdb->length;
8627 * This is correct according to SBC-2.
8629 if (num_blocks == 0)
8635 struct scsi_rw_10 *cdb;
8637 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8639 if (cdb->byte2 & SRW10_FUA)
8641 if (cdb->byte2 & SRW10_DPO)
8644 lba = scsi_4btoul(cdb->addr);
8645 num_blocks = scsi_2btoul(cdb->length);
8648 case WRITE_VERIFY_10: {
8649 struct scsi_write_verify_10 *cdb;
8651 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8654 * XXX KDM we should do actual write verify support at some
8655 * point. This is obviously fake, we're just translating
8656 * things to a write. So we don't even bother checking the
8657 * BYTCHK field, since we don't do any verification. If
8658 * the user asks for it, we'll just pretend we did it.
8660 if (cdb->byte2 & SWV_DPO)
8663 lba = scsi_4btoul(cdb->addr);
8664 num_blocks = scsi_2btoul(cdb->length);
8669 struct scsi_rw_12 *cdb;
8671 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8673 if (cdb->byte2 & SRW12_FUA)
8675 if (cdb->byte2 & SRW12_DPO)
8677 lba = scsi_4btoul(cdb->addr);
8678 num_blocks = scsi_4btoul(cdb->length);
8681 case WRITE_VERIFY_12: {
8682 struct scsi_write_verify_12 *cdb;
8684 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8686 if (cdb->byte2 & SWV_DPO)
8689 lba = scsi_4btoul(cdb->addr);
8690 num_blocks = scsi_4btoul(cdb->length);
8696 struct scsi_rw_16 *cdb;
8698 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8700 if (cdb->byte2 & SRW12_FUA)
8702 if (cdb->byte2 & SRW12_DPO)
8705 lba = scsi_8btou64(cdb->addr);
8706 num_blocks = scsi_4btoul(cdb->length);
8709 case WRITE_VERIFY_16: {
8710 struct scsi_write_verify_16 *cdb;
8712 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8714 if (cdb->byte2 & SWV_DPO)
8717 lba = scsi_8btou64(cdb->addr);
8718 num_blocks = scsi_4btoul(cdb->length);
8723 * We got a command we don't support. This shouldn't
8724 * happen, commands should be filtered out above us.
8726 ctl_set_invalid_opcode(ctsio);
8727 ctl_done((union ctl_io *)ctsio);
8729 return (CTL_RETVAL_COMPLETE);
8730 break; /* NOTREACHED */
8734 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8735 * interesting for us, but if RAIDCore is in write-back mode,
8736 * getting it to do write-through for a particular transaction may
8741 * The first check is to make sure we're in bounds, the second
8742 * check is to catch wrap-around problems. If the lba + num blocks
8743 * is less than the lba, then we've wrapped around and the block
8744 * range is invalid anyway.
8746 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8747 || ((lba + num_blocks) < lba)) {
8748 ctl_set_lba_out_of_range(ctsio);
8749 ctl_done((union ctl_io *)ctsio);
8750 return (CTL_RETVAL_COMPLETE);
8754 * According to SBC-3, a transfer length of 0 is not an error.
8755 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8756 * translates to 256 blocks for those commands.
8758 if (num_blocks == 0) {
8759 ctl_set_success(ctsio);
8760 ctl_done((union ctl_io *)ctsio);
8761 return (CTL_RETVAL_COMPLETE);
8764 lbalen = (struct ctl_lba_len_flags *)
8765 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8767 lbalen->len = num_blocks;
8768 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
8770 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8771 ctsio->kern_rel_offset = 0;
8773 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8775 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8781 ctl_cnw_cont(union ctl_io *io)
8783 struct ctl_scsiio *ctsio;
8784 struct ctl_lun *lun;
8785 struct ctl_lba_len_flags *lbalen;
8788 ctsio = &io->scsiio;
8789 ctsio->io_hdr.status = CTL_STATUS_NONE;
8790 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8791 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8792 lbalen = (struct ctl_lba_len_flags *)
8793 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8794 lbalen->flags = CTL_LLF_WRITE;
8796 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8797 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8802 ctl_cnw(struct ctl_scsiio *ctsio)
8804 struct ctl_lun *lun;
8805 struct ctl_lba_len_flags *lbalen;
8807 uint32_t num_blocks;
8811 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8813 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8818 retval = CTL_RETVAL_COMPLETE;
8820 switch (ctsio->cdb[0]) {
8821 case COMPARE_AND_WRITE: {
8822 struct scsi_compare_and_write *cdb;
8824 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8826 if (cdb->byte2 & SRW10_FUA)
8828 if (cdb->byte2 & SRW10_DPO)
8830 lba = scsi_8btou64(cdb->addr);
8831 num_blocks = cdb->length;
8836 * We got a command we don't support. This shouldn't
8837 * happen, commands should be filtered out above us.
8839 ctl_set_invalid_opcode(ctsio);
8840 ctl_done((union ctl_io *)ctsio);
8842 return (CTL_RETVAL_COMPLETE);
8843 break; /* NOTREACHED */
8847 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8848 * interesting for us, but if RAIDCore is in write-back mode,
8849 * getting it to do write-through for a particular transaction may
8854 * The first check is to make sure we're in bounds, the second
8855 * check is to catch wrap-around problems. If the lba + num blocks
8856 * is less than the lba, then we've wrapped around and the block
8857 * range is invalid anyway.
8859 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8860 || ((lba + num_blocks) < lba)) {
8861 ctl_set_lba_out_of_range(ctsio);
8862 ctl_done((union ctl_io *)ctsio);
8863 return (CTL_RETVAL_COMPLETE);
8867 * According to SBC-3, a transfer length of 0 is not an error.
8869 if (num_blocks == 0) {
8870 ctl_set_success(ctsio);
8871 ctl_done((union ctl_io *)ctsio);
8872 return (CTL_RETVAL_COMPLETE);
8875 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8876 ctsio->kern_rel_offset = 0;
8879 * Set the IO_CONT flag, so that if this I/O gets passed to
8880 * ctl_data_submit_done(), it'll get passed back to
8881 * ctl_ctl_cnw_cont() for further processing.
8883 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8884 ctsio->io_cont = ctl_cnw_cont;
8886 lbalen = (struct ctl_lba_len_flags *)
8887 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8889 lbalen->len = num_blocks;
8890 lbalen->flags = CTL_LLF_COMPARE;
8892 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8893 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8898 ctl_verify(struct ctl_scsiio *ctsio)
8900 struct ctl_lun *lun;
8901 struct ctl_lba_len_flags *lbalen;
8903 uint32_t num_blocks;
8907 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8909 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8913 retval = CTL_RETVAL_COMPLETE;
8915 switch (ctsio->cdb[0]) {
8917 struct scsi_verify_10 *cdb;
8919 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8920 if (cdb->byte2 & SVFY_BYTCHK)
8922 if (cdb->byte2 & SVFY_DPO)
8924 lba = scsi_4btoul(cdb->addr);
8925 num_blocks = scsi_2btoul(cdb->length);
8929 struct scsi_verify_12 *cdb;
8931 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8932 if (cdb->byte2 & SVFY_BYTCHK)
8934 if (cdb->byte2 & SVFY_DPO)
8936 lba = scsi_4btoul(cdb->addr);
8937 num_blocks = scsi_4btoul(cdb->length);
8941 struct scsi_rw_16 *cdb;
8943 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8944 if (cdb->byte2 & SVFY_BYTCHK)
8946 if (cdb->byte2 & SVFY_DPO)
8948 lba = scsi_8btou64(cdb->addr);
8949 num_blocks = scsi_4btoul(cdb->length);
8954 * We got a command we don't support. This shouldn't
8955 * happen, commands should be filtered out above us.
8957 ctl_set_invalid_opcode(ctsio);
8958 ctl_done((union ctl_io *)ctsio);
8959 return (CTL_RETVAL_COMPLETE);
8963 * The first check is to make sure we're in bounds, the second
8964 * check is to catch wrap-around problems. If the lba + num blocks
8965 * is less than the lba, then we've wrapped around and the block
8966 * range is invalid anyway.
8968 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8969 || ((lba + num_blocks) < lba)) {
8970 ctl_set_lba_out_of_range(ctsio);
8971 ctl_done((union ctl_io *)ctsio);
8972 return (CTL_RETVAL_COMPLETE);
8976 * According to SBC-3, a transfer length of 0 is not an error.
8978 if (num_blocks == 0) {
8979 ctl_set_success(ctsio);
8980 ctl_done((union ctl_io *)ctsio);
8981 return (CTL_RETVAL_COMPLETE);
8984 lbalen = (struct ctl_lba_len_flags *)
8985 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8987 lbalen->len = num_blocks;
8989 lbalen->flags = CTL_LLF_COMPARE;
8990 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8992 lbalen->flags = CTL_LLF_VERIFY;
8993 ctsio->kern_total_len = 0;
8995 ctsio->kern_rel_offset = 0;
8997 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8998 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9003 ctl_report_luns(struct ctl_scsiio *ctsio)
9005 struct scsi_report_luns *cdb;
9006 struct scsi_report_luns_data *lun_data;
9007 struct ctl_lun *lun, *request_lun;
9008 int num_luns, retval;
9009 uint32_t alloc_len, lun_datalen;
9010 int num_filled, well_known;
9011 uint32_t initidx, targ_lun_id, lun_id;
9013 retval = CTL_RETVAL_COMPLETE;
9016 cdb = (struct scsi_report_luns *)ctsio->cdb;
9018 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9020 mtx_lock(&control_softc->ctl_lock);
9021 num_luns = control_softc->num_luns;
9022 mtx_unlock(&control_softc->ctl_lock);
9024 switch (cdb->select_report) {
9025 case RPL_REPORT_DEFAULT:
9026 case RPL_REPORT_ALL:
9028 case RPL_REPORT_WELLKNOWN:
9033 ctl_set_invalid_field(ctsio,
9039 ctl_done((union ctl_io *)ctsio);
9041 break; /* NOTREACHED */
9044 alloc_len = scsi_4btoul(cdb->length);
9046 * The initiator has to allocate at least 16 bytes for this request,
9047 * so he can at least get the header and the first LUN. Otherwise
9048 * we reject the request (per SPC-3 rev 14, section 6.21).
9050 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9051 sizeof(struct scsi_report_luns_lundata))) {
9052 ctl_set_invalid_field(ctsio,
9058 ctl_done((union ctl_io *)ctsio);
9062 request_lun = (struct ctl_lun *)
9063 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9065 lun_datalen = sizeof(*lun_data) +
9066 (num_luns * sizeof(struct scsi_report_luns_lundata));
9068 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9069 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9070 ctsio->kern_sg_entries = 0;
9072 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9074 mtx_lock(&control_softc->ctl_lock);
9075 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9076 lun_id = targ_lun_id;
9077 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
9078 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
9079 if (lun_id >= CTL_MAX_LUNS)
9081 lun = control_softc->ctl_luns[lun_id];
9085 if (targ_lun_id <= 0xff) {
9087 * Peripheral addressing method, bus number 0.
9089 lun_data->luns[num_filled].lundata[0] =
9090 RPL_LUNDATA_ATYP_PERIPH;
9091 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9093 } else if (targ_lun_id <= 0x3fff) {
9095 * Flat addressing method.
9097 lun_data->luns[num_filled].lundata[0] =
9098 RPL_LUNDATA_ATYP_FLAT |
9099 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9100 #ifdef OLDCTLHEADERS
9101 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9102 (targ_lun_id & SRLD_BUS_LUN_MASK);
9104 lun_data->luns[num_filled].lundata[1] =
9105 #ifdef OLDCTLHEADERS
9106 targ_lun_id >> SRLD_BUS_LUN_BITS;
9108 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9111 printf("ctl_report_luns: bogus LUN number %jd, "
9112 "skipping\n", (intmax_t)targ_lun_id);
9115 * According to SPC-3, rev 14 section 6.21:
9117 * "The execution of a REPORT LUNS command to any valid and
9118 * installed logical unit shall clear the REPORTED LUNS DATA
9119 * HAS CHANGED unit attention condition for all logical
9120 * units of that target with respect to the requesting
9121 * initiator. A valid and installed logical unit is one
9122 * having a PERIPHERAL QUALIFIER of 000b in the standard
9123 * INQUIRY data (see 6.4.2)."
9125 * If request_lun is NULL, the LUN this report luns command
9126 * was issued to is either disabled or doesn't exist. In that
9127 * case, we shouldn't clear any pending lun change unit
9130 if (request_lun != NULL) {
9131 mtx_lock(&lun->lun_lock);
9132 lun->pending_sense[initidx].ua_pending &=
9134 mtx_unlock(&lun->lun_lock);
9137 mtx_unlock(&control_softc->ctl_lock);
9140 * It's quite possible that we've returned fewer LUNs than we allocated
9141 * space for. Trim it.
9143 lun_datalen = sizeof(*lun_data) +
9144 (num_filled * sizeof(struct scsi_report_luns_lundata));
9146 if (lun_datalen < alloc_len) {
9147 ctsio->residual = alloc_len - lun_datalen;
9148 ctsio->kern_data_len = lun_datalen;
9149 ctsio->kern_total_len = lun_datalen;
9151 ctsio->residual = 0;
9152 ctsio->kern_data_len = alloc_len;
9153 ctsio->kern_total_len = alloc_len;
9155 ctsio->kern_data_resid = 0;
9156 ctsio->kern_rel_offset = 0;
9157 ctsio->kern_sg_entries = 0;
9160 * We set this to the actual data length, regardless of how much
9161 * space we actually have to return results. If the user looks at
9162 * this value, he'll know whether or not he allocated enough space
9163 * and reissue the command if necessary. We don't support well
9164 * known logical units, so if the user asks for that, return none.
9166 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9169 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9172 ctsio->scsi_status = SCSI_STATUS_OK;
9174 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9175 ctsio->be_move_done = ctl_config_move_done;
9176 ctl_datamove((union ctl_io *)ctsio);
9182 ctl_request_sense(struct ctl_scsiio *ctsio)
9184 struct scsi_request_sense *cdb;
9185 struct scsi_sense_data *sense_ptr;
9186 struct ctl_lun *lun;
9189 scsi_sense_data_type sense_format;
9191 cdb = (struct scsi_request_sense *)ctsio->cdb;
9193 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9195 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9198 * Determine which sense format the user wants.
9200 if (cdb->byte2 & SRS_DESC)
9201 sense_format = SSD_TYPE_DESC;
9203 sense_format = SSD_TYPE_FIXED;
9205 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9206 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9207 ctsio->kern_sg_entries = 0;
9210 * struct scsi_sense_data, which is currently set to 256 bytes, is
9211 * larger than the largest allowed value for the length field in the
9212 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9214 ctsio->residual = 0;
9215 ctsio->kern_data_len = cdb->length;
9216 ctsio->kern_total_len = cdb->length;
9218 ctsio->kern_data_resid = 0;
9219 ctsio->kern_rel_offset = 0;
9220 ctsio->kern_sg_entries = 0;
9223 * If we don't have a LUN, we don't have any pending sense.
9229 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9231 * Check for pending sense, and then for pending unit attentions.
9232 * Pending sense gets returned first, then pending unit attentions.
9234 mtx_lock(&lun->lun_lock);
9235 if (ctl_is_set(lun->have_ca, initidx)) {
9236 scsi_sense_data_type stored_format;
9239 * Check to see which sense format was used for the stored
9242 stored_format = scsi_sense_type(
9243 &lun->pending_sense[initidx].sense);
9246 * If the user requested a different sense format than the
9247 * one we stored, then we need to convert it to the other
9248 * format. If we're going from descriptor to fixed format
9249 * sense data, we may lose things in translation, depending
9250 * on what options were used.
9252 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9253 * for some reason we'll just copy it out as-is.
9255 if ((stored_format == SSD_TYPE_FIXED)
9256 && (sense_format == SSD_TYPE_DESC))
9257 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9258 &lun->pending_sense[initidx].sense,
9259 (struct scsi_sense_data_desc *)sense_ptr);
9260 else if ((stored_format == SSD_TYPE_DESC)
9261 && (sense_format == SSD_TYPE_FIXED))
9262 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9263 &lun->pending_sense[initidx].sense,
9264 (struct scsi_sense_data_fixed *)sense_ptr);
9266 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9267 ctl_min(sizeof(*sense_ptr),
9268 sizeof(lun->pending_sense[initidx].sense)));
9270 ctl_clear_mask(lun->have_ca, initidx);
9272 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9273 ctl_ua_type ua_type;
9275 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9276 sense_ptr, sense_format);
9277 if (ua_type != CTL_UA_NONE) {
9279 /* We're reporting this UA, so clear it */
9280 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9283 mtx_unlock(&lun->lun_lock);
9286 * We already have a pending error, return it.
9288 if (have_error != 0) {
9290 * We report the SCSI status as OK, since the status of the
9291 * request sense command itself is OK.
9293 ctsio->scsi_status = SCSI_STATUS_OK;
9296 * We report 0 for the sense length, because we aren't doing
9297 * autosense in this case. We're reporting sense as
9300 ctsio->sense_len = 0;
9301 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9302 ctsio->be_move_done = ctl_config_move_done;
9303 ctl_datamove((union ctl_io *)ctsio);
9305 return (CTL_RETVAL_COMPLETE);
9311 * No sense information to report, so we report that everything is
9314 ctl_set_sense_data(sense_ptr,
9317 /*current_error*/ 1,
9318 /*sense_key*/ SSD_KEY_NO_SENSE,
9323 ctsio->scsi_status = SCSI_STATUS_OK;
9326 * We report 0 for the sense length, because we aren't doing
9327 * autosense in this case. We're reporting sense as parameter data.
9329 ctsio->sense_len = 0;
9330 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9331 ctsio->be_move_done = ctl_config_move_done;
9332 ctl_datamove((union ctl_io *)ctsio);
9334 return (CTL_RETVAL_COMPLETE);
9338 ctl_tur(struct ctl_scsiio *ctsio)
9340 struct ctl_lun *lun;
9342 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9344 CTL_DEBUG_PRINT(("ctl_tur\n"));
9349 ctsio->scsi_status = SCSI_STATUS_OK;
9350 ctsio->io_hdr.status = CTL_SUCCESS;
9352 ctl_done((union ctl_io *)ctsio);
9354 return (CTL_RETVAL_COMPLETE);
9359 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9366 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9368 struct scsi_vpd_supported_pages *pages;
9370 struct ctl_lun *lun;
9372 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9374 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9375 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9376 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9377 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9378 ctsio->kern_sg_entries = 0;
9380 if (sup_page_size < alloc_len) {
9381 ctsio->residual = alloc_len - sup_page_size;
9382 ctsio->kern_data_len = sup_page_size;
9383 ctsio->kern_total_len = sup_page_size;
9385 ctsio->residual = 0;
9386 ctsio->kern_data_len = alloc_len;
9387 ctsio->kern_total_len = alloc_len;
9389 ctsio->kern_data_resid = 0;
9390 ctsio->kern_rel_offset = 0;
9391 ctsio->kern_sg_entries = 0;
9394 * The control device is always connected. The disk device, on the
9395 * other hand, may not be online all the time. Need to change this
9396 * to figure out whether the disk device is actually online or not.
9399 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9400 lun->be_lun->lun_type;
9402 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9404 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9405 /* Supported VPD pages */
9406 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9408 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9409 /* Device Identification */
9410 pages->page_list[2] = SVPD_DEVICE_ID;
9412 pages->page_list[3] = SVPD_BLOCK_LIMITS;
9413 /* Logical Block Provisioning */
9414 pages->page_list[4] = SVPD_LBP;
9416 ctsio->scsi_status = SCSI_STATUS_OK;
9418 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9419 ctsio->be_move_done = ctl_config_move_done;
9420 ctl_datamove((union ctl_io *)ctsio);
9422 return (CTL_RETVAL_COMPLETE);
9426 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9428 struct scsi_vpd_unit_serial_number *sn_ptr;
9429 struct ctl_lun *lun;
9431 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9433 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9434 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9435 ctsio->kern_sg_entries = 0;
9437 if (sizeof(*sn_ptr) < alloc_len) {
9438 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9439 ctsio->kern_data_len = sizeof(*sn_ptr);
9440 ctsio->kern_total_len = sizeof(*sn_ptr);
9442 ctsio->residual = 0;
9443 ctsio->kern_data_len = alloc_len;
9444 ctsio->kern_total_len = alloc_len;
9446 ctsio->kern_data_resid = 0;
9447 ctsio->kern_rel_offset = 0;
9448 ctsio->kern_sg_entries = 0;
9451 * The control device is always connected. The disk device, on the
9452 * other hand, may not be online all the time. Need to change this
9453 * to figure out whether the disk device is actually online or not.
9456 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9457 lun->be_lun->lun_type;
9459 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9461 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9462 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9464 * If we don't have a LUN, we just leave the serial number as
9467 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9469 strncpy((char *)sn_ptr->serial_num,
9470 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9472 ctsio->scsi_status = SCSI_STATUS_OK;
9474 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9475 ctsio->be_move_done = ctl_config_move_done;
9476 ctl_datamove((union ctl_io *)ctsio);
9478 return (CTL_RETVAL_COMPLETE);
9483 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9485 struct scsi_vpd_device_id *devid_ptr;
9486 struct scsi_vpd_id_descriptor *desc, *desc1;
9487 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9488 struct scsi_vpd_id_t10 *t10id;
9489 struct ctl_softc *ctl_softc;
9490 struct ctl_lun *lun;
9491 struct ctl_port *port;
9493 int data_len, devid_len;
9495 ctl_softc = control_softc;
9497 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9499 if (port->devid != NULL)
9500 return ((port->devid)(ctsio, alloc_len));
9502 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9505 devid_len = CTL_DEVID_MIN_LEN;
9507 devid_len = max(CTL_DEVID_MIN_LEN,
9508 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN));
9511 data_len = sizeof(struct scsi_vpd_device_id) +
9512 sizeof(struct scsi_vpd_id_descriptor) +
9513 sizeof(struct scsi_vpd_id_t10) + devid_len +
9514 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9515 sizeof(struct scsi_vpd_id_descriptor) +
9516 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9517 sizeof(struct scsi_vpd_id_descriptor) +
9518 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9520 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9521 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9522 ctsio->kern_sg_entries = 0;
9524 if (data_len < alloc_len) {
9525 ctsio->residual = alloc_len - data_len;
9526 ctsio->kern_data_len = data_len;
9527 ctsio->kern_total_len = data_len;
9529 ctsio->residual = 0;
9530 ctsio->kern_data_len = alloc_len;
9531 ctsio->kern_total_len = alloc_len;
9533 ctsio->kern_data_resid = 0;
9534 ctsio->kern_rel_offset = 0;
9535 ctsio->kern_sg_entries = 0;
9537 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9538 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9539 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9540 sizeof(struct scsi_vpd_id_t10) + devid_len);
9541 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9543 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9544 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9547 * The control device is always connected. The disk device, on the
9548 * other hand, may not be online all the time.
9551 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9552 lun->be_lun->lun_type;
9554 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9556 devid_ptr->page_code = SVPD_DEVICE_ID;
9558 scsi_ulto2b(data_len - 4, devid_ptr->length);
9561 * For Fibre channel,
9563 if (port->port_type == CTL_PORT_FC)
9565 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9566 SVPD_ID_CODESET_ASCII;
9567 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9568 SVPD_ID_CODESET_BINARY;
9572 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9573 SVPD_ID_CODESET_ASCII;
9574 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9575 SVPD_ID_CODESET_BINARY;
9577 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9580 * We're using a LUN association here. i.e., this device ID is a
9581 * per-LUN identifier.
9583 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9584 desc->length = sizeof(*t10id) + devid_len;
9585 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
9586 "vendor")) == NULL) {
9587 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9589 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
9590 strncpy(t10id->vendor, val,
9591 min(sizeof(t10id->vendor), strlen(val)));
9595 * desc1 is for the WWPN which is a port asscociation.
9597 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9598 desc1->length = CTL_WWPN_LEN;
9599 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9600 /* For testing just create the WWPN */
9602 ddb_GetWWNN((char *)desc1->identifier);
9604 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9605 /* This is so Copancontrol will return something sane */
9606 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9607 ctsio->io_hdr.nexus.targ_port!=8)
9608 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9610 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9613 be64enc(desc1->identifier, port->wwpn);
9616 * desc2 is for the Relative Target Port(type 4h) identifier
9618 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9619 | SVPD_ID_TYPE_RELTARG;
9622 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9623 /* This is so Copancontrol will return something sane */
9624 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9625 ctsio->io_hdr.nexus.targ_port!=8)
9626 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9628 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9632 * desc3 is for the Target Port Group(type 5h) identifier
9634 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9635 | SVPD_ID_TYPE_TPORTGRP;
9637 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9638 desc3->identifier[3] = 1;
9640 desc3->identifier[3] = 2;
9643 * If we've actually got a backend, copy the device id from the
9644 * per-LUN data. Otherwise, set it to all spaces.
9648 * Copy the backend's LUN ID.
9650 strncpy((char *)t10id->vendor_spec_id,
9651 (char *)lun->be_lun->device_id, devid_len);
9654 * No backend, set this to spaces.
9656 memset(t10id->vendor_spec_id, 0x20, devid_len);
9659 ctsio->scsi_status = SCSI_STATUS_OK;
9661 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9662 ctsio->be_move_done = ctl_config_move_done;
9663 ctl_datamove((union ctl_io *)ctsio);
9665 return (CTL_RETVAL_COMPLETE);
9669 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9671 struct scsi_vpd_block_limits *bl_ptr;
9672 struct ctl_lun *lun;
9675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9676 bs = lun->be_lun->blocksize;
9678 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9679 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9680 ctsio->kern_sg_entries = 0;
9682 if (sizeof(*bl_ptr) < alloc_len) {
9683 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9684 ctsio->kern_data_len = sizeof(*bl_ptr);
9685 ctsio->kern_total_len = sizeof(*bl_ptr);
9687 ctsio->residual = 0;
9688 ctsio->kern_data_len = alloc_len;
9689 ctsio->kern_total_len = alloc_len;
9691 ctsio->kern_data_resid = 0;
9692 ctsio->kern_rel_offset = 0;
9693 ctsio->kern_sg_entries = 0;
9696 * The control device is always connected. The disk device, on the
9697 * other hand, may not be online all the time. Need to change this
9698 * to figure out whether the disk device is actually online or not.
9701 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9702 lun->be_lun->lun_type;
9704 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9706 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9707 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
9708 bl_ptr->max_cmp_write_len = 0xff;
9709 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9710 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
9711 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9712 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9713 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9715 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9717 ctsio->scsi_status = SCSI_STATUS_OK;
9718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9719 ctsio->be_move_done = ctl_config_move_done;
9720 ctl_datamove((union ctl_io *)ctsio);
9722 return (CTL_RETVAL_COMPLETE);
9726 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9728 struct scsi_vpd_logical_block_prov *lbp_ptr;
9729 struct ctl_lun *lun;
9732 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9733 bs = lun->be_lun->blocksize;
9735 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9736 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9737 ctsio->kern_sg_entries = 0;
9739 if (sizeof(*lbp_ptr) < alloc_len) {
9740 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9741 ctsio->kern_data_len = sizeof(*lbp_ptr);
9742 ctsio->kern_total_len = sizeof(*lbp_ptr);
9744 ctsio->residual = 0;
9745 ctsio->kern_data_len = alloc_len;
9746 ctsio->kern_total_len = alloc_len;
9748 ctsio->kern_data_resid = 0;
9749 ctsio->kern_rel_offset = 0;
9750 ctsio->kern_sg_entries = 0;
9753 * The control device is always connected. The disk device, on the
9754 * other hand, may not be online all the time. Need to change this
9755 * to figure out whether the disk device is actually online or not.
9758 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9759 lun->be_lun->lun_type;
9761 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9763 lbp_ptr->page_code = SVPD_LBP;
9764 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
9765 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
9767 ctsio->scsi_status = SCSI_STATUS_OK;
9768 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9769 ctsio->be_move_done = ctl_config_move_done;
9770 ctl_datamove((union ctl_io *)ctsio);
9772 return (CTL_RETVAL_COMPLETE);
9776 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9778 struct scsi_inquiry *cdb;
9779 struct ctl_lun *lun;
9780 int alloc_len, retval;
9782 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9783 cdb = (struct scsi_inquiry *)ctsio->cdb;
9785 retval = CTL_RETVAL_COMPLETE;
9787 alloc_len = scsi_2btoul(cdb->length);
9789 switch (cdb->page_code) {
9790 case SVPD_SUPPORTED_PAGES:
9791 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9793 case SVPD_UNIT_SERIAL_NUMBER:
9794 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9796 case SVPD_DEVICE_ID:
9797 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9799 case SVPD_BLOCK_LIMITS:
9800 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9803 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9806 ctl_set_invalid_field(ctsio,
9812 ctl_done((union ctl_io *)ctsio);
9813 retval = CTL_RETVAL_COMPLETE;
9821 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9823 struct scsi_inquiry_data *inq_ptr;
9824 struct scsi_inquiry *cdb;
9825 struct ctl_softc *ctl_softc;
9826 struct ctl_lun *lun;
9831 ctl_softc = control_softc;
9834 * Figure out whether we're talking to a Fibre Channel port or not.
9835 * We treat the ioctl front end, and any SCSI adapters, as packetized
9838 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9844 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9845 cdb = (struct scsi_inquiry *)ctsio->cdb;
9846 alloc_len = scsi_2btoul(cdb->length);
9849 * We malloc the full inquiry data size here and fill it
9850 * in. If the user only asks for less, we'll give him
9853 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9854 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9855 ctsio->kern_sg_entries = 0;
9856 ctsio->kern_data_resid = 0;
9857 ctsio->kern_rel_offset = 0;
9859 if (sizeof(*inq_ptr) < alloc_len) {
9860 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9861 ctsio->kern_data_len = sizeof(*inq_ptr);
9862 ctsio->kern_total_len = sizeof(*inq_ptr);
9864 ctsio->residual = 0;
9865 ctsio->kern_data_len = alloc_len;
9866 ctsio->kern_total_len = alloc_len;
9870 * If we have a LUN configured, report it as connected. Otherwise,
9871 * report that it is offline or no device is supported, depending
9872 * on the value of inquiry_pq_no_lun.
9874 * According to the spec (SPC-4 r34), the peripheral qualifier
9875 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9877 * "A peripheral device having the specified peripheral device type
9878 * is not connected to this logical unit. However, the device
9879 * server is capable of supporting the specified peripheral device
9880 * type on this logical unit."
9882 * According to the same spec, the peripheral qualifier
9883 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9885 * "The device server is not capable of supporting a peripheral
9886 * device on this logical unit. For this peripheral qualifier the
9887 * peripheral device type shall be set to 1Fh. All other peripheral
9888 * device type values are reserved for this peripheral qualifier."
9890 * Given the text, it would seem that we probably want to report that
9891 * the LUN is offline here. There is no LUN connected, but we can
9892 * support a LUN at the given LUN number.
9894 * In the real world, though, it sounds like things are a little
9897 * - Linux, when presented with a LUN with the offline peripheral
9898 * qualifier, will create an sg driver instance for it. So when
9899 * you attach it to CTL, you wind up with a ton of sg driver
9900 * instances. (One for every LUN that Linux bothered to probe.)
9901 * Linux does this despite the fact that it issues a REPORT LUNs
9902 * to LUN 0 to get the inventory of supported LUNs.
9904 * - There is other anecdotal evidence (from Emulex folks) about
9905 * arrays that use the offline peripheral qualifier for LUNs that
9906 * are on the "passive" path in an active/passive array.
9908 * So the solution is provide a hopefully reasonable default
9909 * (return bad/no LUN) and allow the user to change the behavior
9910 * with a tunable/sysctl variable.
9913 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9914 lun->be_lun->lun_type;
9915 else if (ctl_softc->inquiry_pq_no_lun == 0)
9916 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9918 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9920 /* RMB in byte 2 is 0 */
9921 inq_ptr->version = SCSI_REV_SPC3;
9924 * According to SAM-3, even if a device only supports a single
9925 * level of LUN addressing, it should still set the HISUP bit:
9927 * 4.9.1 Logical unit numbers overview
9929 * All logical unit number formats described in this standard are
9930 * hierarchical in structure even when only a single level in that
9931 * hierarchy is used. The HISUP bit shall be set to one in the
9932 * standard INQUIRY data (see SPC-2) when any logical unit number
9933 * format described in this standard is used. Non-hierarchical
9934 * formats are outside the scope of this standard.
9936 * Therefore we set the HiSup bit here.
9938 * The reponse format is 2, per SPC-3.
9940 inq_ptr->response_format = SID_HiSup | 2;
9942 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9943 CTL_DEBUG_PRINT(("additional_length = %d\n",
9944 inq_ptr->additional_length));
9946 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9947 /* 16 bit addressing */
9949 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9950 /* XXX set the SID_MultiP bit here if we're actually going to
9951 respond on multiple ports */
9952 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9954 /* 16 bit data bus, synchronous transfers */
9955 /* XXX these flags don't apply for FC */
9957 inq_ptr->flags = SID_WBus16 | SID_Sync;
9959 * XXX KDM do we want to support tagged queueing on the control
9963 || (lun->be_lun->lun_type != T_PROCESSOR))
9964 inq_ptr->flags |= SID_CmdQue;
9966 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9967 * We have 8 bytes for the vendor name, and 16 bytes for the device
9968 * name and 4 bytes for the revision.
9970 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
9971 "vendor")) == NULL) {
9972 strcpy(inq_ptr->vendor, CTL_VENDOR);
9974 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
9975 strncpy(inq_ptr->vendor, val,
9976 min(sizeof(inq_ptr->vendor), strlen(val)));
9979 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9980 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
9981 switch (lun->be_lun->lun_type) {
9983 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9986 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9989 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9993 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
9994 strncpy(inq_ptr->product, val,
9995 min(sizeof(inq_ptr->product), strlen(val)));
9999 * XXX make this a macro somewhere so it automatically gets
10000 * incremented when we make changes.
10002 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10003 "revision")) == NULL) {
10004 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10006 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10007 strncpy(inq_ptr->revision, val,
10008 min(sizeof(inq_ptr->revision), strlen(val)));
10012 * For parallel SCSI, we support double transition and single
10013 * transition clocking. We also support QAS (Quick Arbitration
10014 * and Selection) and Information Unit transfers on both the
10015 * control and array devices.
10018 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10022 scsi_ulto2b(0x0060, inq_ptr->version1);
10023 /* SPC-3 (no version claimed) XXX should we claim a version? */
10024 scsi_ulto2b(0x0300, inq_ptr->version2);
10026 /* FCP-2 ANSI INCITS.350:2003 */
10027 scsi_ulto2b(0x0917, inq_ptr->version3);
10029 /* SPI-4 ANSI INCITS.362:200x */
10030 scsi_ulto2b(0x0B56, inq_ptr->version3);
10034 /* SBC-2 (no version claimed) XXX should we claim a version? */
10035 scsi_ulto2b(0x0320, inq_ptr->version4);
10037 switch (lun->be_lun->lun_type) {
10040 * SBC-2 (no version claimed) XXX should we claim a
10043 scsi_ulto2b(0x0320, inq_ptr->version4);
10051 ctsio->scsi_status = SCSI_STATUS_OK;
10052 if (ctsio->kern_data_len > 0) {
10053 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10054 ctsio->be_move_done = ctl_config_move_done;
10055 ctl_datamove((union ctl_io *)ctsio);
10057 ctsio->io_hdr.status = CTL_SUCCESS;
10058 ctl_done((union ctl_io *)ctsio);
10061 return (CTL_RETVAL_COMPLETE);
10065 ctl_inquiry(struct ctl_scsiio *ctsio)
10067 struct scsi_inquiry *cdb;
10070 cdb = (struct scsi_inquiry *)ctsio->cdb;
10074 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10077 * Right now, we don't support the CmdDt inquiry information.
10078 * This would be nice to support in the future. When we do
10079 * support it, we should change this test so that it checks to make
10080 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10083 if (((cdb->byte2 & SI_EVPD)
10084 && (cdb->byte2 & SI_CMDDT)))
10086 if (cdb->byte2 & SI_CMDDT) {
10088 * Point to the SI_CMDDT bit. We might change this
10089 * when we support SI_CMDDT, but since both bits would be
10090 * "wrong", this should probably just stay as-is then.
10092 ctl_set_invalid_field(ctsio,
10098 ctl_done((union ctl_io *)ctsio);
10099 return (CTL_RETVAL_COMPLETE);
10101 if (cdb->byte2 & SI_EVPD)
10102 retval = ctl_inquiry_evpd(ctsio);
10104 else if (cdb->byte2 & SI_CMDDT)
10105 retval = ctl_inquiry_cmddt(ctsio);
10108 retval = ctl_inquiry_std(ctsio);
10114 * For known CDB types, parse the LBA and length.
10117 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10119 if (io->io_hdr.io_type != CTL_IO_SCSI)
10122 switch (io->scsiio.cdb[0]) {
10123 case COMPARE_AND_WRITE: {
10124 struct scsi_compare_and_write *cdb;
10126 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10128 *lba = scsi_8btou64(cdb->addr);
10129 *len = cdb->length;
10134 struct scsi_rw_6 *cdb;
10136 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10138 *lba = scsi_3btoul(cdb->addr);
10139 /* only 5 bits are valid in the most significant address byte */
10141 *len = cdb->length;
10146 struct scsi_rw_10 *cdb;
10148 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10150 *lba = scsi_4btoul(cdb->addr);
10151 *len = scsi_2btoul(cdb->length);
10154 case WRITE_VERIFY_10: {
10155 struct scsi_write_verify_10 *cdb;
10157 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10159 *lba = scsi_4btoul(cdb->addr);
10160 *len = scsi_2btoul(cdb->length);
10165 struct scsi_rw_12 *cdb;
10167 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10169 *lba = scsi_4btoul(cdb->addr);
10170 *len = scsi_4btoul(cdb->length);
10173 case WRITE_VERIFY_12: {
10174 struct scsi_write_verify_12 *cdb;
10176 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10178 *lba = scsi_4btoul(cdb->addr);
10179 *len = scsi_4btoul(cdb->length);
10184 struct scsi_rw_16 *cdb;
10186 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10188 *lba = scsi_8btou64(cdb->addr);
10189 *len = scsi_4btoul(cdb->length);
10192 case WRITE_VERIFY_16: {
10193 struct scsi_write_verify_16 *cdb;
10195 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10198 *lba = scsi_8btou64(cdb->addr);
10199 *len = scsi_4btoul(cdb->length);
10202 case WRITE_SAME_10: {
10203 struct scsi_write_same_10 *cdb;
10205 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10207 *lba = scsi_4btoul(cdb->addr);
10208 *len = scsi_2btoul(cdb->length);
10211 case WRITE_SAME_16: {
10212 struct scsi_write_same_16 *cdb;
10214 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10216 *lba = scsi_8btou64(cdb->addr);
10217 *len = scsi_4btoul(cdb->length);
10221 struct scsi_verify_10 *cdb;
10223 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10225 *lba = scsi_4btoul(cdb->addr);
10226 *len = scsi_2btoul(cdb->length);
10230 struct scsi_verify_12 *cdb;
10232 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10234 *lba = scsi_4btoul(cdb->addr);
10235 *len = scsi_4btoul(cdb->length);
10239 struct scsi_verify_16 *cdb;
10241 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10243 *lba = scsi_8btou64(cdb->addr);
10244 *len = scsi_4btoul(cdb->length);
10249 break; /* NOTREACHED */
10256 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10258 uint64_t endlba1, endlba2;
10260 endlba1 = lba1 + len1 - 1;
10261 endlba2 = lba2 + len2 - 1;
10263 if ((endlba1 < lba2)
10264 || (endlba2 < lba1))
10265 return (CTL_ACTION_PASS);
10267 return (CTL_ACTION_BLOCK);
10271 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10273 uint64_t lba1, lba2;
10274 uint32_t len1, len2;
10277 retval = ctl_get_lba_len(io1, &lba1, &len1);
10279 return (CTL_ACTION_ERROR);
10281 retval = ctl_get_lba_len(io2, &lba2, &len2);
10283 return (CTL_ACTION_ERROR);
10285 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10289 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10291 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10292 ctl_serialize_action *serialize_row;
10295 * The initiator attempted multiple untagged commands at the same
10296 * time. Can't do that.
10298 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10299 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10300 && ((pending_io->io_hdr.nexus.targ_port ==
10301 ooa_io->io_hdr.nexus.targ_port)
10302 && (pending_io->io_hdr.nexus.initid.id ==
10303 ooa_io->io_hdr.nexus.initid.id))
10304 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10305 return (CTL_ACTION_OVERLAP);
10308 * The initiator attempted to send multiple tagged commands with
10309 * the same ID. (It's fine if different initiators have the same
10312 * Even if all of those conditions are true, we don't kill the I/O
10313 * if the command ahead of us has been aborted. We won't end up
10314 * sending it to the FETD, and it's perfectly legal to resend a
10315 * command with the same tag number as long as the previous
10316 * instance of this tag number has been aborted somehow.
10318 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10319 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10320 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
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_TAG);
10329 * If we get a head of queue tag, SAM-3 says that we should
10330 * immediately execute it.
10332 * What happens if this command would normally block for some other
10333 * reason? e.g. a request sense with a head of queue tag
10334 * immediately after a write. Normally that would block, but this
10335 * will result in its getting executed immediately...
10337 * We currently return "pass" instead of "skip", so we'll end up
10338 * going through the rest of the queue to check for overlapped tags.
10340 * XXX KDM check for other types of blockage first??
10342 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10343 return (CTL_ACTION_PASS);
10346 * Ordered tags have to block until all items ahead of them
10347 * have completed. If we get called with an ordered tag, we always
10348 * block, if something else is ahead of us in the queue.
10350 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10351 return (CTL_ACTION_BLOCK);
10354 * Simple tags get blocked until all head of queue and ordered tags
10355 * ahead of them have completed. I'm lumping untagged commands in
10356 * with simple tags here. XXX KDM is that the right thing to do?
10358 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10359 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10360 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10361 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10362 return (CTL_ACTION_BLOCK);
10364 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10365 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10367 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10369 switch (serialize_row[pending_entry->seridx]) {
10370 case CTL_SER_BLOCK:
10371 return (CTL_ACTION_BLOCK);
10372 break; /* NOTREACHED */
10373 case CTL_SER_EXTENT:
10374 return (ctl_extent_check(pending_io, ooa_io));
10375 break; /* NOTREACHED */
10377 return (CTL_ACTION_PASS);
10378 break; /* NOTREACHED */
10380 return (CTL_ACTION_SKIP);
10383 panic("invalid serialization value %d",
10384 serialize_row[pending_entry->seridx]);
10385 break; /* NOTREACHED */
10388 return (CTL_ACTION_ERROR);
10392 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10394 * - pending_io is generally either incoming, or on the blocked queue
10395 * - starting I/O is the I/O we want to start the check with.
10398 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10399 union ctl_io *starting_io)
10401 union ctl_io *ooa_io;
10404 mtx_assert(&lun->lun_lock, MA_OWNED);
10407 * Run back along the OOA queue, starting with the current
10408 * blocked I/O and going through every I/O before it on the
10409 * queue. If starting_io is NULL, we'll just end up returning
10412 for (ooa_io = starting_io; ooa_io != NULL;
10413 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10417 * This routine just checks to see whether
10418 * cur_blocked is blocked by ooa_io, which is ahead
10419 * of it in the queue. It doesn't queue/dequeue
10422 action = ctl_check_for_blockage(pending_io, ooa_io);
10424 case CTL_ACTION_BLOCK:
10425 case CTL_ACTION_OVERLAP:
10426 case CTL_ACTION_OVERLAP_TAG:
10427 case CTL_ACTION_SKIP:
10428 case CTL_ACTION_ERROR:
10430 break; /* NOTREACHED */
10431 case CTL_ACTION_PASS:
10434 panic("invalid action %d", action);
10435 break; /* NOTREACHED */
10439 return (CTL_ACTION_PASS);
10444 * - An I/O has just completed, and has been removed from the per-LUN OOA
10445 * queue, so some items on the blocked queue may now be unblocked.
10448 ctl_check_blocked(struct ctl_lun *lun)
10450 union ctl_io *cur_blocked, *next_blocked;
10452 mtx_assert(&lun->lun_lock, MA_OWNED);
10455 * Run forward from the head of the blocked queue, checking each
10456 * entry against the I/Os prior to it on the OOA queue to see if
10457 * there is still any blockage.
10459 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10460 * with our removing a variable on it while it is traversing the
10463 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10464 cur_blocked != NULL; cur_blocked = next_blocked) {
10465 union ctl_io *prev_ooa;
10468 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10471 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10472 ctl_ooaq, ooa_links);
10475 * If cur_blocked happens to be the first item in the OOA
10476 * queue now, prev_ooa will be NULL, and the action
10477 * returned will just be CTL_ACTION_PASS.
10479 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10482 case CTL_ACTION_BLOCK:
10483 /* Nothing to do here, still blocked */
10485 case CTL_ACTION_OVERLAP:
10486 case CTL_ACTION_OVERLAP_TAG:
10488 * This shouldn't happen! In theory we've already
10489 * checked this command for overlap...
10492 case CTL_ACTION_PASS:
10493 case CTL_ACTION_SKIP: {
10494 struct ctl_softc *softc;
10495 const struct ctl_cmd_entry *entry;
10500 * The skip case shouldn't happen, this transaction
10501 * should have never made it onto the blocked queue.
10504 * This I/O is no longer blocked, we can remove it
10505 * from the blocked queue. Since this is a TAILQ
10506 * (doubly linked list), we can do O(1) removals
10507 * from any place on the list.
10509 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10511 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10513 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10515 * Need to send IO back to original side to
10518 union ctl_ha_msg msg_info;
10520 msg_info.hdr.original_sc =
10521 cur_blocked->io_hdr.original_sc;
10522 msg_info.hdr.serializing_sc = cur_blocked;
10523 msg_info.hdr.msg_type = CTL_MSG_R2R;
10524 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10525 &msg_info, sizeof(msg_info), 0)) >
10526 CTL_HA_STATUS_SUCCESS) {
10527 printf("CTL:Check Blocked error from "
10528 "ctl_ha_msg_send %d\n",
10533 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10534 softc = control_softc;
10536 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10539 * Check this I/O for LUN state changes that may
10540 * have happened while this command was blocked.
10541 * The LUN state may have been changed by a command
10542 * ahead of us in the queue, so we need to re-check
10543 * for any states that can be caused by SCSI
10546 if (ctl_scsiio_lun_check(softc, lun, entry,
10547 &cur_blocked->scsiio) == 0) {
10548 cur_blocked->io_hdr.flags |=
10549 CTL_FLAG_IS_WAS_ON_RTR;
10550 ctl_enqueue_rtr(cur_blocked);
10552 ctl_done(cur_blocked);
10557 * This probably shouldn't happen -- we shouldn't
10558 * get CTL_ACTION_ERROR, or anything else.
10564 return (CTL_RETVAL_COMPLETE);
10568 * This routine (with one exception) checks LUN flags that can be set by
10569 * commands ahead of us in the OOA queue. These flags have to be checked
10570 * when a command initially comes in, and when we pull a command off the
10571 * blocked queue and are preparing to execute it. The reason we have to
10572 * check these flags for commands on the blocked queue is that the LUN
10573 * state may have been changed by a command ahead of us while we're on the
10576 * Ordering is somewhat important with these checks, so please pay
10577 * careful attention to the placement of any new checks.
10580 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10581 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10587 mtx_assert(&lun->lun_lock, MA_OWNED);
10590 * If this shelf is a secondary shelf controller, we have to reject
10591 * any media access commands.
10594 /* No longer needed for HA */
10595 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10596 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10597 ctl_set_lun_standby(ctsio);
10604 * Check for a reservation conflict. If this command isn't allowed
10605 * even on reserved LUNs, and if this initiator isn't the one who
10606 * reserved us, reject the command with a reservation conflict.
10608 if ((lun->flags & CTL_LUN_RESERVED)
10609 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10610 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10611 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10612 || (ctsio->io_hdr.nexus.targ_target.id !=
10613 lun->rsv_nexus.targ_target.id)) {
10614 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10615 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10621 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10622 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10625 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10627 * if we aren't registered or it's a res holder type
10628 * reservation and this isn't the res holder then set a
10630 * NOTE: Commands which might be allowed on write exclusive
10631 * type reservations are checked in the particular command
10632 * for a conflict. Read and SSU are the only ones.
10634 if (!lun->per_res[residx].registered
10635 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10636 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10637 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10644 if ((lun->flags & CTL_LUN_OFFLINE)
10645 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10646 ctl_set_lun_not_ready(ctsio);
10652 * If the LUN is stopped, see if this particular command is allowed
10653 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10655 if ((lun->flags & CTL_LUN_STOPPED)
10656 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10657 /* "Logical unit not ready, initializing cmd. required" */
10658 ctl_set_lun_stopped(ctsio);
10663 if ((lun->flags & CTL_LUN_INOPERABLE)
10664 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10665 /* "Medium format corrupted" */
10666 ctl_set_medium_format_corrupted(ctsio);
10677 ctl_failover_io(union ctl_io *io, int have_lock)
10679 ctl_set_busy(&io->scsiio);
10686 struct ctl_lun *lun;
10687 struct ctl_softc *ctl_softc;
10688 union ctl_io *next_io, *pending_io;
10693 ctl_softc = control_softc;
10695 mtx_lock(&ctl_softc->ctl_lock);
10697 * Remove any cmds from the other SC from the rtr queue. These
10698 * will obviously only be for LUNs for which we're the primary.
10699 * We can't send status or get/send data for these commands.
10700 * Since they haven't been executed yet, we can just remove them.
10701 * We'll either abort them or delete them below, depending on
10702 * which HA mode we're in.
10705 mtx_lock(&ctl_softc->queue_lock);
10706 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10707 io != NULL; io = next_io) {
10708 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10709 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10710 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10711 ctl_io_hdr, links);
10713 mtx_unlock(&ctl_softc->queue_lock);
10716 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10717 lun = ctl_softc->ctl_luns[lun_idx];
10722 * Processor LUNs are primary on both sides.
10723 * XXX will this always be true?
10725 if (lun->be_lun->lun_type == T_PROCESSOR)
10728 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10729 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10730 printf("FAILOVER: primary lun %d\n", lun_idx);
10732 * Remove all commands from the other SC. First from the
10733 * blocked queue then from the ooa queue. Once we have
10734 * removed them. Call ctl_check_blocked to see if there
10735 * is anything that can run.
10737 for (io = (union ctl_io *)TAILQ_FIRST(
10738 &lun->blocked_queue); io != NULL; io = next_io) {
10740 next_io = (union ctl_io *)TAILQ_NEXT(
10741 &io->io_hdr, blocked_links);
10743 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10744 TAILQ_REMOVE(&lun->blocked_queue,
10745 &io->io_hdr,blocked_links);
10746 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10747 TAILQ_REMOVE(&lun->ooa_queue,
10748 &io->io_hdr, ooa_links);
10754 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10755 io != NULL; io = next_io) {
10757 next_io = (union ctl_io *)TAILQ_NEXT(
10758 &io->io_hdr, ooa_links);
10760 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10762 TAILQ_REMOVE(&lun->ooa_queue,
10769 ctl_check_blocked(lun);
10770 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10771 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10773 printf("FAILOVER: primary lun %d\n", lun_idx);
10775 * Abort all commands from the other SC. We can't
10776 * send status back for them now. These should get
10777 * cleaned up when they are completed or come out
10778 * for a datamove operation.
10780 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10781 io != NULL; io = next_io) {
10782 next_io = (union ctl_io *)TAILQ_NEXT(
10783 &io->io_hdr, ooa_links);
10785 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10786 io->io_hdr.flags |= CTL_FLAG_ABORT;
10788 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10789 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10791 printf("FAILOVER: secondary lun %d\n", lun_idx);
10793 lun->flags |= CTL_LUN_PRIMARY_SC;
10796 * We send all I/O that was sent to this controller
10797 * and redirected to the other side back with
10798 * busy status, and have the initiator retry it.
10799 * Figuring out how much data has been transferred,
10800 * etc. and picking up where we left off would be
10803 * XXX KDM need to remove I/O from the blocked
10806 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10807 &lun->ooa_queue); pending_io != NULL;
10808 pending_io = next_io) {
10810 next_io = (union ctl_io *)TAILQ_NEXT(
10811 &pending_io->io_hdr, ooa_links);
10813 pending_io->io_hdr.flags &=
10814 ~CTL_FLAG_SENT_2OTHER_SC;
10816 if (pending_io->io_hdr.flags &
10817 CTL_FLAG_IO_ACTIVE) {
10818 pending_io->io_hdr.flags |=
10821 ctl_set_busy(&pending_io->scsiio);
10822 ctl_done(pending_io);
10827 * Build Unit Attention
10829 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10830 lun->pending_sense[i].ua_pending |=
10831 CTL_UA_ASYM_ACC_CHANGE;
10833 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10834 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10835 printf("FAILOVER: secondary lun %d\n", lun_idx);
10837 * if the first io on the OOA is not on the RtR queue
10840 lun->flags |= CTL_LUN_PRIMARY_SC;
10842 pending_io = (union ctl_io *)TAILQ_FIRST(
10844 if (pending_io==NULL) {
10845 printf("Nothing on OOA queue\n");
10849 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10850 if ((pending_io->io_hdr.flags &
10851 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10852 pending_io->io_hdr.flags |=
10853 CTL_FLAG_IS_WAS_ON_RTR;
10854 ctl_enqueue_rtr(pending_io);
10859 printf("Tag 0x%04x is running\n",
10860 pending_io->scsiio.tag_num);
10864 next_io = (union ctl_io *)TAILQ_NEXT(
10865 &pending_io->io_hdr, ooa_links);
10866 for (pending_io=next_io; pending_io != NULL;
10867 pending_io = next_io) {
10868 pending_io->io_hdr.flags &=
10869 ~CTL_FLAG_SENT_2OTHER_SC;
10870 next_io = (union ctl_io *)TAILQ_NEXT(
10871 &pending_io->io_hdr, ooa_links);
10872 if (pending_io->io_hdr.flags &
10873 CTL_FLAG_IS_WAS_ON_RTR) {
10875 printf("Tag 0x%04x is running\n",
10876 pending_io->scsiio.tag_num);
10881 switch (ctl_check_ooa(lun, pending_io,
10882 (union ctl_io *)TAILQ_PREV(
10883 &pending_io->io_hdr, ctl_ooaq,
10886 case CTL_ACTION_BLOCK:
10887 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10888 &pending_io->io_hdr,
10890 pending_io->io_hdr.flags |=
10893 case CTL_ACTION_PASS:
10894 case CTL_ACTION_SKIP:
10895 pending_io->io_hdr.flags |=
10896 CTL_FLAG_IS_WAS_ON_RTR;
10897 ctl_enqueue_rtr(pending_io);
10899 case CTL_ACTION_OVERLAP:
10900 ctl_set_overlapped_cmd(
10901 (struct ctl_scsiio *)pending_io);
10902 ctl_done(pending_io);
10904 case CTL_ACTION_OVERLAP_TAG:
10905 ctl_set_overlapped_tag(
10906 (struct ctl_scsiio *)pending_io,
10907 pending_io->scsiio.tag_num & 0xff);
10908 ctl_done(pending_io);
10910 case CTL_ACTION_ERROR:
10912 ctl_set_internal_failure(
10913 (struct ctl_scsiio *)pending_io,
10916 ctl_done(pending_io);
10922 * Build Unit Attention
10924 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10925 lun->pending_sense[i].ua_pending |=
10926 CTL_UA_ASYM_ACC_CHANGE;
10929 panic("Unhandled HA mode failover, LUN flags = %#x, "
10930 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10934 mtx_unlock(&ctl_softc->ctl_lock);
10938 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10940 struct ctl_lun *lun;
10941 const struct ctl_cmd_entry *entry;
10942 uint32_t initidx, targ_lun;
10949 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
10950 if ((targ_lun < CTL_MAX_LUNS)
10951 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10952 lun = ctl_softc->ctl_luns[targ_lun];
10954 * If the LUN is invalid, pretend that it doesn't exist.
10955 * It will go away as soon as all pending I/O has been
10958 if (lun->flags & CTL_LUN_DISABLED) {
10961 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10962 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10964 if (lun->be_lun->lun_type == T_PROCESSOR) {
10965 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10969 * Every I/O goes into the OOA queue for a
10970 * particular LUN, and stays there until completion.
10972 mtx_lock(&lun->lun_lock);
10973 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
10977 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10978 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10981 /* Get command entry and return error if it is unsuppotyed. */
10982 entry = ctl_validate_command(ctsio);
10983 if (entry == NULL) {
10985 mtx_unlock(&lun->lun_lock);
10989 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10990 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10993 * Check to see whether we can send this command to LUNs that don't
10994 * exist. This should pretty much only be the case for inquiry
10995 * and request sense. Further checks, below, really require having
10996 * a LUN, so we can't really check the command anymore. Just put
10997 * it on the rtr queue.
11000 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11001 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11002 ctl_enqueue_rtr((union ctl_io *)ctsio);
11006 ctl_set_unsupported_lun(ctsio);
11007 ctl_done((union ctl_io *)ctsio);
11008 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11012 * Make sure we support this particular command on this LUN.
11013 * e.g., we don't support writes to the control LUN.
11015 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11016 mtx_unlock(&lun->lun_lock);
11017 ctl_set_invalid_opcode(ctsio);
11018 ctl_done((union ctl_io *)ctsio);
11023 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11026 * If we've got a request sense, it'll clear the contingent
11027 * allegiance condition. Otherwise, if we have a CA condition for
11028 * this initiator, clear it, because it sent down a command other
11029 * than request sense.
11031 if ((ctsio->cdb[0] != REQUEST_SENSE)
11032 && (ctl_is_set(lun->have_ca, initidx)))
11033 ctl_clear_mask(lun->have_ca, initidx);
11036 * If the command has this flag set, it handles its own unit
11037 * attention reporting, we shouldn't do anything. Otherwise we
11038 * check for any pending unit attentions, and send them back to the
11039 * initiator. We only do this when a command initially comes in,
11040 * not when we pull it off the blocked queue.
11042 * According to SAM-3, section 5.3.2, the order that things get
11043 * presented back to the host is basically unit attentions caused
11044 * by some sort of reset event, busy status, reservation conflicts
11045 * or task set full, and finally any other status.
11047 * One issue here is that some of the unit attentions we report
11048 * don't fall into the "reset" category (e.g. "reported luns data
11049 * has changed"). So reporting it here, before the reservation
11050 * check, may be technically wrong. I guess the only thing to do
11051 * would be to check for and report the reset events here, and then
11052 * check for the other unit attention types after we check for a
11053 * reservation conflict.
11055 * XXX KDM need to fix this
11057 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11058 ctl_ua_type ua_type;
11060 ua_type = lun->pending_sense[initidx].ua_pending;
11061 if (ua_type != CTL_UA_NONE) {
11062 scsi_sense_data_type sense_format;
11065 sense_format = (lun->flags &
11066 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11069 sense_format = SSD_TYPE_FIXED;
11071 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11073 if (ua_type != CTL_UA_NONE) {
11074 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11075 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11077 ctsio->sense_len = SSD_FULL_SIZE;
11078 lun->pending_sense[initidx].ua_pending &=
11080 mtx_unlock(&lun->lun_lock);
11081 ctl_done((union ctl_io *)ctsio);
11088 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11089 mtx_unlock(&lun->lun_lock);
11090 ctl_done((union ctl_io *)ctsio);
11095 * XXX CHD this is where we want to send IO to other side if
11096 * this LUN is secondary on this SC. We will need to make a copy
11097 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11098 * the copy we send as FROM_OTHER.
11099 * We also need to stuff the address of the original IO so we can
11100 * find it easily. Something similar will need be done on the other
11101 * side so when we are done we can find the copy.
11103 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11104 union ctl_ha_msg msg_info;
11107 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11109 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11110 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11112 printf("1. ctsio %p\n", ctsio);
11114 msg_info.hdr.serializing_sc = NULL;
11115 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11116 msg_info.scsi.tag_num = ctsio->tag_num;
11117 msg_info.scsi.tag_type = ctsio->tag_type;
11118 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11120 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11122 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11123 (void *)&msg_info, sizeof(msg_info), 0)) >
11124 CTL_HA_STATUS_SUCCESS) {
11125 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11127 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11130 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11135 * XXX KDM this I/O is off the incoming queue, but hasn't
11136 * been inserted on any other queue. We may need to come
11137 * up with a holding queue while we wait for serialization
11138 * so that we have an idea of what we're waiting for from
11141 mtx_unlock(&lun->lun_lock);
11145 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11146 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11147 ctl_ooaq, ooa_links))) {
11148 case CTL_ACTION_BLOCK:
11149 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11150 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11152 mtx_unlock(&lun->lun_lock);
11154 case CTL_ACTION_PASS:
11155 case CTL_ACTION_SKIP:
11156 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11157 mtx_unlock(&lun->lun_lock);
11158 ctl_enqueue_rtr((union ctl_io *)ctsio);
11160 case CTL_ACTION_OVERLAP:
11161 mtx_unlock(&lun->lun_lock);
11162 ctl_set_overlapped_cmd(ctsio);
11163 ctl_done((union ctl_io *)ctsio);
11165 case CTL_ACTION_OVERLAP_TAG:
11166 mtx_unlock(&lun->lun_lock);
11167 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11168 ctl_done((union ctl_io *)ctsio);
11170 case CTL_ACTION_ERROR:
11172 mtx_unlock(&lun->lun_lock);
11173 ctl_set_internal_failure(ctsio,
11175 /*retry_count*/ 0);
11176 ctl_done((union ctl_io *)ctsio);
11182 const struct ctl_cmd_entry *
11183 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11185 const struct ctl_cmd_entry *entry;
11186 int service_action;
11188 entry = &ctl_cmd_table[ctsio->cdb[0]];
11189 if (entry->flags & CTL_CMD_FLAG_SA5) {
11190 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11191 entry = &((const struct ctl_cmd_entry *)
11192 entry->execute)[service_action];
11197 const struct ctl_cmd_entry *
11198 ctl_validate_command(struct ctl_scsiio *ctsio)
11200 const struct ctl_cmd_entry *entry;
11204 entry = ctl_get_cmd_entry(ctsio);
11205 if (entry->execute == NULL) {
11206 ctl_set_invalid_opcode(ctsio);
11207 ctl_done((union ctl_io *)ctsio);
11210 KASSERT(entry->length > 0,
11211 ("Not defined length for command 0x%02x/0x%02x",
11212 ctsio->cdb[0], ctsio->cdb[1]));
11213 for (i = 1; i < entry->length; i++) {
11214 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11217 ctl_set_invalid_field(ctsio,
11222 /*bit*/ fls(diff) - 1);
11223 ctl_done((union ctl_io *)ctsio);
11230 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11233 switch (lun_type) {
11235 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11236 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11240 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11241 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11251 ctl_scsiio(struct ctl_scsiio *ctsio)
11254 const struct ctl_cmd_entry *entry;
11256 retval = CTL_RETVAL_COMPLETE;
11258 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11260 entry = ctl_get_cmd_entry(ctsio);
11263 * If this I/O has been aborted, just send it straight to
11264 * ctl_done() without executing it.
11266 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11267 ctl_done((union ctl_io *)ctsio);
11272 * All the checks should have been handled by ctl_scsiio_precheck().
11273 * We should be clear now to just execute the I/O.
11275 retval = entry->execute(ctsio);
11282 * Since we only implement one target right now, a bus reset simply resets
11283 * our single target.
11286 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11288 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11292 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11293 ctl_ua_type ua_type)
11295 struct ctl_lun *lun;
11298 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11299 union ctl_ha_msg msg_info;
11301 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11302 msg_info.hdr.nexus = io->io_hdr.nexus;
11303 if (ua_type==CTL_UA_TARG_RESET)
11304 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11306 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11307 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11308 msg_info.hdr.original_sc = NULL;
11309 msg_info.hdr.serializing_sc = NULL;
11310 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11311 (void *)&msg_info, sizeof(msg_info), 0)) {
11316 mtx_lock(&ctl_softc->ctl_lock);
11317 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11318 retval += ctl_lun_reset(lun, io, ua_type);
11319 mtx_unlock(&ctl_softc->ctl_lock);
11325 * The LUN should always be set. The I/O is optional, and is used to
11326 * distinguish between I/Os sent by this initiator, and by other
11327 * initiators. We set unit attention for initiators other than this one.
11328 * SAM-3 is vague on this point. It does say that a unit attention should
11329 * be established for other initiators when a LUN is reset (see section
11330 * 5.7.3), but it doesn't specifically say that the unit attention should
11331 * be established for this particular initiator when a LUN is reset. Here
11332 * is the relevant text, from SAM-3 rev 8:
11334 * 5.7.2 When a SCSI initiator port aborts its own tasks
11336 * When a SCSI initiator port causes its own task(s) to be aborted, no
11337 * notification that the task(s) have been aborted shall be returned to
11338 * the SCSI initiator port other than the completion response for the
11339 * command or task management function action that caused the task(s) to
11340 * be aborted and notification(s) associated with related effects of the
11341 * action (e.g., a reset unit attention condition).
11343 * XXX KDM for now, we're setting unit attention for all initiators.
11346 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11350 uint32_t initindex;
11354 mtx_lock(&lun->lun_lock);
11356 * Run through the OOA queue and abort each I/O.
11359 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11361 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11362 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11363 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11367 * This version sets unit attention for every
11370 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11371 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11372 if (initindex == i)
11374 lun->pending_sense[i].ua_pending |= ua_type;
11379 * A reset (any kind, really) clears reservations established with
11380 * RESERVE/RELEASE. It does not clear reservations established
11381 * with PERSISTENT RESERVE OUT, but we don't support that at the
11382 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11383 * reservations made with the RESERVE/RELEASE commands, because
11384 * those commands are obsolete in SPC-3.
11386 lun->flags &= ~CTL_LUN_RESERVED;
11388 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11389 ctl_clear_mask(lun->have_ca, i);
11390 lun->pending_sense[i].ua_pending |= ua_type;
11392 mtx_unlock(&lun->lun_lock);
11398 ctl_abort_task(union ctl_io *io)
11401 struct ctl_lun *lun;
11402 struct ctl_softc *ctl_softc;
11405 char printbuf[128];
11410 ctl_softc = control_softc;
11416 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11417 mtx_lock(&ctl_softc->ctl_lock);
11418 if ((targ_lun < CTL_MAX_LUNS)
11419 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11420 lun = ctl_softc->ctl_luns[targ_lun];
11422 mtx_unlock(&ctl_softc->ctl_lock);
11427 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11428 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11431 mtx_lock(&lun->lun_lock);
11432 mtx_unlock(&ctl_softc->ctl_lock);
11434 * Run through the OOA queue and attempt to find the given I/O.
11435 * The target port, initiator ID, tag type and tag number have to
11436 * match the values that we got from the initiator. If we have an
11437 * untagged command to abort, simply abort the first untagged command
11438 * we come to. We only allow one untagged command at a time of course.
11441 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11443 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11444 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11446 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11448 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11449 lun->lun, xio->scsiio.tag_num,
11450 xio->scsiio.tag_type,
11451 (xio->io_hdr.blocked_links.tqe_prev
11452 == NULL) ? "" : " BLOCKED",
11453 (xio->io_hdr.flags &
11454 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11455 (xio->io_hdr.flags &
11456 CTL_FLAG_ABORT) ? " ABORT" : "",
11457 (xio->io_hdr.flags &
11458 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11459 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11461 printf("%s\n", sbuf_data(&sb));
11464 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11465 && (xio->io_hdr.nexus.initid.id ==
11466 io->io_hdr.nexus.initid.id)) {
11468 * If the abort says that the task is untagged, the
11469 * task in the queue must be untagged. Otherwise,
11470 * we just check to see whether the tag numbers
11471 * match. This is because the QLogic firmware
11472 * doesn't pass back the tag type in an abort
11476 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11477 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11478 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11481 * XXX KDM we've got problems with FC, because it
11482 * doesn't send down a tag type with aborts. So we
11483 * can only really go by the tag number...
11484 * This may cause problems with parallel SCSI.
11485 * Need to figure that out!!
11487 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11488 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11490 if ((io->io_hdr.flags &
11491 CTL_FLAG_FROM_OTHER_SC) == 0 &&
11492 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11493 union ctl_ha_msg msg_info;
11495 io->io_hdr.flags |=
11496 CTL_FLAG_SENT_2OTHER_SC;
11497 msg_info.hdr.nexus = io->io_hdr.nexus;
11498 msg_info.task.task_action =
11499 CTL_TASK_ABORT_TASK;
11500 msg_info.task.tag_num =
11501 io->taskio.tag_num;
11502 msg_info.task.tag_type =
11503 io->taskio.tag_type;
11504 msg_info.hdr.msg_type =
11505 CTL_MSG_MANAGE_TASKS;
11506 msg_info.hdr.original_sc = NULL;
11507 msg_info.hdr.serializing_sc = NULL;
11509 printf("Sent Abort to other side\n");
11511 if (CTL_HA_STATUS_SUCCESS !=
11512 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11514 sizeof(msg_info), 0)) {
11518 printf("ctl_abort_task: found I/O to abort\n");
11524 mtx_unlock(&lun->lun_lock);
11530 * This isn't really an error. It's entirely possible for
11531 * the abort and command completion to cross on the wire.
11532 * This is more of an informative/diagnostic error.
11535 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
11536 "%d:%d:%d:%d tag %d type %d\n",
11537 io->io_hdr.nexus.initid.id,
11538 io->io_hdr.nexus.targ_port,
11539 io->io_hdr.nexus.targ_target.id,
11540 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
11541 io->taskio.tag_type);
11549 ctl_run_task(union ctl_io *io)
11551 struct ctl_softc *ctl_softc;
11553 const char *task_desc;
11555 CTL_DEBUG_PRINT(("ctl_run_task\n"));
11557 ctl_softc = control_softc;
11560 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
11561 ("ctl_run_task: Unextected io_type %d\n",
11562 io->io_hdr.io_type));
11564 task_desc = ctl_scsi_task_string(&io->taskio);
11565 if (task_desc != NULL) {
11567 csevent_log(CSC_CTL | CSC_SHELF_SW |
11569 csevent_LogType_Trace,
11570 csevent_Severity_Information,
11571 csevent_AlertLevel_Green,
11572 csevent_FRU_Firmware,
11573 csevent_FRU_Unknown,
11574 "CTL: received task: %s",task_desc);
11578 csevent_log(CSC_CTL | CSC_SHELF_SW |
11580 csevent_LogType_Trace,
11581 csevent_Severity_Information,
11582 csevent_AlertLevel_Green,
11583 csevent_FRU_Firmware,
11584 csevent_FRU_Unknown,
11585 "CTL: received unknown task "
11587 io->taskio.task_action,
11588 io->taskio.task_action);
11591 switch (io->taskio.task_action) {
11592 case CTL_TASK_ABORT_TASK:
11593 retval = ctl_abort_task(io);
11595 case CTL_TASK_ABORT_TASK_SET:
11597 case CTL_TASK_CLEAR_ACA:
11599 case CTL_TASK_CLEAR_TASK_SET:
11601 case CTL_TASK_LUN_RESET: {
11602 struct ctl_lun *lun;
11606 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11607 mtx_lock(&ctl_softc->ctl_lock);
11608 if ((targ_lun < CTL_MAX_LUNS)
11609 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11610 lun = ctl_softc->ctl_luns[targ_lun];
11612 mtx_unlock(&ctl_softc->ctl_lock);
11617 if (!(io->io_hdr.flags &
11618 CTL_FLAG_FROM_OTHER_SC)) {
11619 union ctl_ha_msg msg_info;
11621 io->io_hdr.flags |=
11622 CTL_FLAG_SENT_2OTHER_SC;
11623 msg_info.hdr.msg_type =
11624 CTL_MSG_MANAGE_TASKS;
11625 msg_info.hdr.nexus = io->io_hdr.nexus;
11626 msg_info.task.task_action =
11627 CTL_TASK_LUN_RESET;
11628 msg_info.hdr.original_sc = NULL;
11629 msg_info.hdr.serializing_sc = NULL;
11630 if (CTL_HA_STATUS_SUCCESS !=
11631 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11633 sizeof(msg_info), 0)) {
11637 retval = ctl_lun_reset(lun, io,
11639 mtx_unlock(&ctl_softc->ctl_lock);
11642 case CTL_TASK_TARGET_RESET:
11643 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
11645 case CTL_TASK_BUS_RESET:
11646 retval = ctl_bus_reset(ctl_softc, io);
11648 case CTL_TASK_PORT_LOGIN:
11650 case CTL_TASK_PORT_LOGOUT:
11653 printf("ctl_run_task: got unknown task management event %d\n",
11654 io->taskio.task_action);
11658 io->io_hdr.status = CTL_SUCCESS;
11660 io->io_hdr.status = CTL_ERROR;
11663 * This will queue this I/O to the done queue, but the
11664 * work thread won't be able to process it until we
11665 * return and the lock is released.
11671 * For HA operation. Handle commands that come in from the other
11675 ctl_handle_isc(union ctl_io *io)
11678 struct ctl_lun *lun;
11679 struct ctl_softc *ctl_softc;
11682 ctl_softc = control_softc;
11684 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11685 lun = ctl_softc->ctl_luns[targ_lun];
11687 switch (io->io_hdr.msg_type) {
11688 case CTL_MSG_SERIALIZE:
11689 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
11691 case CTL_MSG_R2R: {
11692 const struct ctl_cmd_entry *entry;
11695 * This is only used in SER_ONLY mode.
11698 entry = ctl_get_cmd_entry(&io->scsiio);
11699 mtx_lock(&lun->lun_lock);
11700 if (ctl_scsiio_lun_check(ctl_softc, lun,
11701 entry, (struct ctl_scsiio *)io) != 0) {
11702 mtx_unlock(&lun->lun_lock);
11706 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11707 mtx_unlock(&lun->lun_lock);
11708 ctl_enqueue_rtr(io);
11711 case CTL_MSG_FINISH_IO:
11712 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11717 mtx_lock(&lun->lun_lock);
11718 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11720 ctl_check_blocked(lun);
11721 mtx_unlock(&lun->lun_lock);
11724 case CTL_MSG_PERS_ACTION:
11725 ctl_hndl_per_res_out_on_other_sc(
11726 (union ctl_ha_msg *)&io->presio.pr_msg);
11729 case CTL_MSG_BAD_JUJU:
11733 case CTL_MSG_DATAMOVE:
11734 /* Only used in XFER mode */
11736 ctl_datamove_remote(io);
11738 case CTL_MSG_DATAMOVE_DONE:
11739 /* Only used in XFER mode */
11741 io->scsiio.be_move_done(io);
11745 printf("%s: Invalid message type %d\n",
11746 __func__, io->io_hdr.msg_type);
11756 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11757 * there is no match.
11759 static ctl_lun_error_pattern
11760 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11762 const struct ctl_cmd_entry *entry;
11763 ctl_lun_error_pattern filtered_pattern, pattern;
11765 pattern = desc->error_pattern;
11768 * XXX KDM we need more data passed into this function to match a
11769 * custom pattern, and we actually need to implement custom pattern
11772 if (pattern & CTL_LUN_PAT_CMD)
11773 return (CTL_LUN_PAT_CMD);
11775 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11776 return (CTL_LUN_PAT_ANY);
11778 entry = ctl_get_cmd_entry(ctsio);
11780 filtered_pattern = entry->pattern & pattern;
11783 * If the user requested specific flags in the pattern (e.g.
11784 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11787 * If the user did not specify any flags, it doesn't matter whether
11788 * or not the command supports the flags.
11790 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11791 (pattern & ~CTL_LUN_PAT_MASK))
11792 return (CTL_LUN_PAT_NONE);
11795 * If the user asked for a range check, see if the requested LBA
11796 * range overlaps with this command's LBA range.
11798 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11804 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11806 return (CTL_LUN_PAT_NONE);
11808 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11809 desc->lba_range.len);
11811 * A "pass" means that the LBA ranges don't overlap, so
11812 * this doesn't match the user's range criteria.
11814 if (action == CTL_ACTION_PASS)
11815 return (CTL_LUN_PAT_NONE);
11818 return (filtered_pattern);
11822 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11824 struct ctl_error_desc *desc, *desc2;
11826 mtx_assert(&lun->lun_lock, MA_OWNED);
11828 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11829 ctl_lun_error_pattern pattern;
11831 * Check to see whether this particular command matches
11832 * the pattern in the descriptor.
11834 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11835 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11838 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11839 case CTL_LUN_INJ_ABORTED:
11840 ctl_set_aborted(&io->scsiio);
11842 case CTL_LUN_INJ_MEDIUM_ERR:
11843 ctl_set_medium_error(&io->scsiio);
11845 case CTL_LUN_INJ_UA:
11846 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11848 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11850 case CTL_LUN_INJ_CUSTOM:
11852 * We're assuming the user knows what he is doing.
11853 * Just copy the sense information without doing
11856 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11857 ctl_min(sizeof(desc->custom_sense),
11858 sizeof(io->scsiio.sense_data)));
11859 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11860 io->scsiio.sense_len = SSD_FULL_SIZE;
11861 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11863 case CTL_LUN_INJ_NONE:
11866 * If this is an error injection type we don't know
11867 * about, clear the continuous flag (if it is set)
11868 * so it will get deleted below.
11870 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11874 * By default, each error injection action is a one-shot
11876 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11879 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11885 #ifdef CTL_IO_DELAY
11887 ctl_datamove_timer_wakeup(void *arg)
11891 io = (union ctl_io *)arg;
11895 #endif /* CTL_IO_DELAY */
11898 ctl_datamove(union ctl_io *io)
11900 void (*fe_datamove)(union ctl_io *io);
11902 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11904 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11907 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11912 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11913 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11915 sbuf_cat(&sb, path_str);
11916 switch (io->io_hdr.io_type) {
11918 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11919 sbuf_printf(&sb, "\n");
11920 sbuf_cat(&sb, path_str);
11921 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11922 io->scsiio.tag_num, io->scsiio.tag_type);
11925 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11926 "Tag Type: %d\n", io->taskio.task_action,
11927 io->taskio.tag_num, io->taskio.tag_type);
11930 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11931 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11934 sbuf_cat(&sb, path_str);
11935 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11936 (intmax_t)time_uptime - io->io_hdr.start_time);
11938 printf("%s", sbuf_data(&sb));
11940 #endif /* CTL_TIME_IO */
11942 #ifdef CTL_IO_DELAY
11943 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11944 struct ctl_lun *lun;
11946 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11948 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11950 struct ctl_lun *lun;
11952 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11954 && (lun->delay_info.datamove_delay > 0)) {
11955 struct callout *callout;
11957 callout = (struct callout *)&io->io_hdr.timer_bytes;
11958 callout_init(callout, /*mpsafe*/ 1);
11959 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11960 callout_reset(callout,
11961 lun->delay_info.datamove_delay * hz,
11962 ctl_datamove_timer_wakeup, io);
11963 if (lun->delay_info.datamove_type ==
11964 CTL_DELAY_TYPE_ONESHOT)
11965 lun->delay_info.datamove_delay = 0;
11972 * This command has been aborted. Set the port status, so we fail
11975 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11976 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11977 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11978 io->io_hdr.nexus.targ_port,
11979 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11980 io->io_hdr.nexus.targ_lun);
11981 io->io_hdr.status = CTL_CMD_ABORTED;
11982 io->io_hdr.port_status = 31337;
11984 * Note that the backend, in this case, will get the
11985 * callback in its context. In other cases it may get
11986 * called in the frontend's interrupt thread context.
11988 io->scsiio.be_move_done(io);
11993 * If we're in XFER mode and this I/O is from the other shelf
11994 * controller, we need to send the DMA to the other side to
11995 * actually transfer the data to/from the host. In serialize only
11996 * mode the transfer happens below CTL and ctl_datamove() is only
11997 * called on the machine that originally received the I/O.
11999 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12000 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12001 union ctl_ha_msg msg;
12002 uint32_t sg_entries_sent;
12006 memset(&msg, 0, sizeof(msg));
12007 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12008 msg.hdr.original_sc = io->io_hdr.original_sc;
12009 msg.hdr.serializing_sc = io;
12010 msg.hdr.nexus = io->io_hdr.nexus;
12011 msg.dt.flags = io->io_hdr.flags;
12013 * We convert everything into a S/G list here. We can't
12014 * pass by reference, only by value between controllers.
12015 * So we can't pass a pointer to the S/G list, only as many
12016 * S/G entries as we can fit in here. If it's possible for
12017 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12018 * then we need to break this up into multiple transfers.
12020 if (io->scsiio.kern_sg_entries == 0) {
12021 msg.dt.kern_sg_entries = 1;
12023 * If this is in cached memory, flush the cache
12024 * before we send the DMA request to the other
12025 * controller. We want to do this in either the
12026 * read or the write case. The read case is
12027 * straightforward. In the write case, we want to
12028 * make sure nothing is in the local cache that
12029 * could overwrite the DMAed data.
12031 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12033 * XXX KDM use bus_dmamap_sync() here.
12038 * Convert to a physical address if this is a
12041 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12042 msg.dt.sg_list[0].addr =
12043 io->scsiio.kern_data_ptr;
12046 * XXX KDM use busdma here!
12049 msg.dt.sg_list[0].addr = (void *)
12050 vtophys(io->scsiio.kern_data_ptr);
12054 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12057 struct ctl_sg_entry *sgl;
12060 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12061 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12062 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12064 * XXX KDM use bus_dmamap_sync() here.
12069 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12070 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12071 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12072 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12073 msg.dt.sg_sequence = 0;
12076 * Loop until we've sent all of the S/G entries. On the
12077 * other end, we'll recompose these S/G entries into one
12078 * contiguous list before passing it to the
12080 for (sg_entries_sent = 0; sg_entries_sent <
12081 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12082 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12083 sizeof(msg.dt.sg_list[0])),
12084 msg.dt.kern_sg_entries - sg_entries_sent);
12086 if (do_sg_copy != 0) {
12087 struct ctl_sg_entry *sgl;
12090 sgl = (struct ctl_sg_entry *)
12091 io->scsiio.kern_data_ptr;
12093 * If this is in cached memory, flush the cache
12094 * before we send the DMA request to the other
12095 * controller. We want to do this in either
12096 * the * read or the write case. The read
12097 * case is straightforward. In the write
12098 * case, we want to make sure nothing is
12099 * in the local cache that could overwrite
12103 for (i = sg_entries_sent, j = 0;
12104 i < msg.dt.cur_sg_entries; i++, j++) {
12105 if ((io->io_hdr.flags &
12106 CTL_FLAG_NO_DATASYNC) == 0) {
12108 * XXX KDM use bus_dmamap_sync()
12111 if ((io->io_hdr.flags &
12112 CTL_FLAG_BUS_ADDR) == 0) {
12114 * XXX KDM use busdma.
12117 msg.dt.sg_list[j].addr =(void *)
12118 vtophys(sgl[i].addr);
12121 msg.dt.sg_list[j].addr =
12124 msg.dt.sg_list[j].len = sgl[i].len;
12128 sg_entries_sent += msg.dt.cur_sg_entries;
12129 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12130 msg.dt.sg_last = 1;
12132 msg.dt.sg_last = 0;
12135 * XXX KDM drop and reacquire the lock here?
12137 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12138 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12140 * XXX do something here.
12144 msg.dt.sent_sg_entries = sg_entries_sent;
12146 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12147 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12148 ctl_failover_io(io, /*have_lock*/ 0);
12153 * Lookup the fe_datamove() function for this particular
12157 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12164 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12166 union ctl_ha_msg msg;
12169 memset(&msg, 0, sizeof(msg));
12171 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12172 msg.hdr.original_sc = io;
12173 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12174 msg.hdr.nexus = io->io_hdr.nexus;
12175 msg.hdr.status = io->io_hdr.status;
12176 msg.scsi.tag_num = io->scsiio.tag_num;
12177 msg.scsi.tag_type = io->scsiio.tag_type;
12178 msg.scsi.scsi_status = io->scsiio.scsi_status;
12179 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12180 sizeof(io->scsiio.sense_data));
12181 msg.scsi.sense_len = io->scsiio.sense_len;
12182 msg.scsi.sense_residual = io->scsiio.sense_residual;
12183 msg.scsi.fetd_status = io->io_hdr.port_status;
12184 msg.scsi.residual = io->scsiio.residual;
12185 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12187 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12188 ctl_failover_io(io, /*have_lock*/ have_lock);
12192 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12193 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12194 /* XXX do something if this fails */
12200 * The DMA to the remote side is done, now we need to tell the other side
12201 * we're done so it can continue with its data movement.
12204 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12210 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12211 printf("%s: ISC DMA write failed with error %d", __func__,
12213 ctl_set_internal_failure(&io->scsiio,
12215 /*retry_count*/ rq->ret);
12218 ctl_dt_req_free(rq);
12221 * In this case, we had to malloc the memory locally. Free it.
12223 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12225 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12226 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12229 * The data is in local and remote memory, so now we need to send
12230 * status (good or back) back to the other side.
12232 ctl_send_datamove_done(io, /*have_lock*/ 0);
12236 * We've moved the data from the host/controller into local memory. Now we
12237 * need to push it over to the remote controller's memory.
12240 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12246 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12247 ctl_datamove_remote_write_cb);
12253 ctl_datamove_remote_write(union ctl_io *io)
12256 void (*fe_datamove)(union ctl_io *io);
12259 * - Get the data from the host/HBA into local memory.
12260 * - DMA memory from the local controller to the remote controller.
12261 * - Send status back to the remote controller.
12264 retval = ctl_datamove_remote_sgl_setup(io);
12268 /* Switch the pointer over so the FETD knows what to do */
12269 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12272 * Use a custom move done callback, since we need to send completion
12273 * back to the other controller, not to the backend on this side.
12275 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12277 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12286 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12295 * In this case, we had to malloc the memory locally. Free it.
12297 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12299 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12300 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12304 scsi_path_string(io, path_str, sizeof(path_str));
12305 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12306 sbuf_cat(&sb, path_str);
12307 scsi_command_string(&io->scsiio, NULL, &sb);
12308 sbuf_printf(&sb, "\n");
12309 sbuf_cat(&sb, path_str);
12310 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12311 io->scsiio.tag_num, io->scsiio.tag_type);
12312 sbuf_cat(&sb, path_str);
12313 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12314 io->io_hdr.flags, io->io_hdr.status);
12316 printk("%s", sbuf_data(&sb));
12321 * The read is done, now we need to send status (good or bad) back
12322 * to the other side.
12324 ctl_send_datamove_done(io, /*have_lock*/ 0);
12330 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12333 void (*fe_datamove)(union ctl_io *io);
12337 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12338 printf("%s: ISC DMA read failed with error %d", __func__,
12340 ctl_set_internal_failure(&io->scsiio,
12342 /*retry_count*/ rq->ret);
12345 ctl_dt_req_free(rq);
12347 /* Switch the pointer over so the FETD knows what to do */
12348 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12351 * Use a custom move done callback, since we need to send completion
12352 * back to the other controller, not to the backend on this side.
12354 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12356 /* XXX KDM add checks like the ones in ctl_datamove? */
12358 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12364 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12366 struct ctl_sg_entry *local_sglist, *remote_sglist;
12367 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12368 struct ctl_softc *softc;
12373 softc = control_softc;
12375 local_sglist = io->io_hdr.local_sglist;
12376 local_dma_sglist = io->io_hdr.local_dma_sglist;
12377 remote_sglist = io->io_hdr.remote_sglist;
12378 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12380 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12381 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12382 local_sglist[i].len = remote_sglist[i].len;
12385 * XXX Detect the situation where the RS-level I/O
12386 * redirector on the other side has already read the
12387 * data off of the AOR RS on this side, and
12388 * transferred it to remote (mirror) memory on the
12389 * other side. Since we already have the data in
12390 * memory here, we just need to use it.
12392 * XXX KDM this can probably be removed once we
12393 * get the cache device code in and take the
12394 * current AOR implementation out.
12397 if ((remote_sglist[i].addr >=
12398 (void *)vtophys(softc->mirr->addr))
12399 && (remote_sglist[i].addr <
12400 ((void *)vtophys(softc->mirr->addr) +
12401 CacheMirrorOffset))) {
12402 local_sglist[i].addr = remote_sglist[i].addr -
12404 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12406 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12408 local_sglist[i].addr = remote_sglist[i].addr +
12413 printf("%s: local %p, remote %p, len %d\n",
12414 __func__, local_sglist[i].addr,
12415 remote_sglist[i].addr, local_sglist[i].len);
12419 uint32_t len_to_go;
12422 * In this case, we don't have automatically allocated
12423 * memory for this I/O on this controller. This typically
12424 * happens with internal CTL I/O -- e.g. inquiry, mode
12425 * sense, etc. Anything coming from RAIDCore will have
12426 * a mirror area available.
12428 len_to_go = io->scsiio.kern_data_len;
12431 * Clear the no datasync flag, we have to use malloced
12434 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12437 * The difficult thing here is that the size of the various
12438 * S/G segments may be different than the size from the
12439 * remote controller. That'll make it harder when DMAing
12440 * the data back to the other side.
12442 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12443 sizeof(io->io_hdr.remote_sglist[0])) &&
12444 (len_to_go > 0); i++) {
12445 local_sglist[i].len = ctl_min(len_to_go, 131072);
12446 CTL_SIZE_8B(local_dma_sglist[i].len,
12447 local_sglist[i].len);
12448 local_sglist[i].addr =
12449 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12451 local_dma_sglist[i].addr = local_sglist[i].addr;
12453 if (local_sglist[i].addr == NULL) {
12456 printf("malloc failed for %zd bytes!",
12457 local_dma_sglist[i].len);
12458 for (j = 0; j < i; j++) {
12459 free(local_sglist[j].addr, M_CTL);
12461 ctl_set_internal_failure(&io->scsiio,
12463 /*retry_count*/ 4857);
12465 goto bailout_error;
12468 /* XXX KDM do we need a sync here? */
12470 len_to_go -= local_sglist[i].len;
12473 * Reset the number of S/G entries accordingly. The
12474 * original number of S/G entries is available in
12477 io->scsiio.kern_sg_entries = i;
12480 printf("%s: kern_sg_entries = %d\n", __func__,
12481 io->scsiio.kern_sg_entries);
12482 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12483 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
12484 local_sglist[i].addr, local_sglist[i].len,
12485 local_dma_sglist[i].len);
12494 ctl_send_datamove_done(io, /*have_lock*/ 0);
12500 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12501 ctl_ha_dt_cb callback)
12503 struct ctl_ha_dt_req *rq;
12504 struct ctl_sg_entry *remote_sglist, *local_sglist;
12505 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12506 uint32_t local_used, remote_used, total_used;
12512 rq = ctl_dt_req_alloc();
12515 * If we failed to allocate the request, and if the DMA didn't fail
12516 * anyway, set busy status. This is just a resource allocation
12520 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12521 ctl_set_busy(&io->scsiio);
12523 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12526 ctl_dt_req_free(rq);
12529 * The data move failed. We need to return status back
12530 * to the other controller. No point in trying to DMA
12531 * data to the remote controller.
12534 ctl_send_datamove_done(io, /*have_lock*/ 0);
12541 local_sglist = io->io_hdr.local_sglist;
12542 local_dma_sglist = io->io_hdr.local_dma_sglist;
12543 remote_sglist = io->io_hdr.remote_sglist;
12544 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12549 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12550 rq->ret = CTL_HA_STATUS_SUCCESS;
12557 * Pull/push the data over the wire from/to the other controller.
12558 * This takes into account the possibility that the local and
12559 * remote sglists may not be identical in terms of the size of
12560 * the elements and the number of elements.
12562 * One fundamental assumption here is that the length allocated for
12563 * both the local and remote sglists is identical. Otherwise, we've
12564 * essentially got a coding error of some sort.
12566 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12568 uint32_t cur_len, dma_length;
12571 rq->id = CTL_HA_DATA_CTL;
12572 rq->command = command;
12576 * Both pointers should be aligned. But it is possible
12577 * that the allocation length is not. They should both
12578 * also have enough slack left over at the end, though,
12579 * to round up to the next 8 byte boundary.
12581 cur_len = ctl_min(local_sglist[i].len - local_used,
12582 remote_sglist[j].len - remote_used);
12585 * In this case, we have a size issue and need to decrease
12586 * the size, except in the case where we actually have less
12587 * than 8 bytes left. In that case, we need to increase
12588 * the DMA length to get the last bit.
12590 if ((cur_len & 0x7) != 0) {
12591 if (cur_len > 0x7) {
12592 cur_len = cur_len - (cur_len & 0x7);
12593 dma_length = cur_len;
12595 CTL_SIZE_8B(dma_length, cur_len);
12599 dma_length = cur_len;
12602 * If we had to allocate memory for this I/O, instead of using
12603 * the non-cached mirror memory, we'll need to flush the cache
12604 * before trying to DMA to the other controller.
12606 * We could end up doing this multiple times for the same
12607 * segment if we have a larger local segment than remote
12608 * segment. That shouldn't be an issue.
12610 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12612 * XXX KDM use bus_dmamap_sync() here.
12616 rq->size = dma_length;
12618 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12619 tmp_ptr += local_used;
12621 /* Use physical addresses when talking to ISC hardware */
12622 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12623 /* XXX KDM use busdma */
12625 rq->local = vtophys(tmp_ptr);
12628 rq->local = tmp_ptr;
12630 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12631 tmp_ptr += remote_used;
12632 rq->remote = tmp_ptr;
12634 rq->callback = NULL;
12636 local_used += cur_len;
12637 if (local_used >= local_sglist[i].len) {
12642 remote_used += cur_len;
12643 if (remote_used >= remote_sglist[j].len) {
12647 total_used += cur_len;
12649 if (total_used >= io->scsiio.kern_data_len)
12650 rq->callback = callback;
12652 if ((rq->size & 0x7) != 0) {
12653 printf("%s: warning: size %d is not on 8b boundary\n",
12654 __func__, rq->size);
12656 if (((uintptr_t)rq->local & 0x7) != 0) {
12657 printf("%s: warning: local %p not on 8b boundary\n",
12658 __func__, rq->local);
12660 if (((uintptr_t)rq->remote & 0x7) != 0) {
12661 printf("%s: warning: remote %p not on 8b boundary\n",
12662 __func__, rq->local);
12665 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12666 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12667 rq->local, rq->remote, rq->size);
12670 isc_ret = ctl_dt_single(rq);
12671 if (isc_ret == CTL_HA_STATUS_WAIT)
12674 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12675 rq->ret = CTL_HA_STATUS_SUCCESS;
12689 ctl_datamove_remote_read(union ctl_io *io)
12695 * This will send an error to the other controller in the case of a
12698 retval = ctl_datamove_remote_sgl_setup(io);
12702 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12703 ctl_datamove_remote_read_cb);
12705 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12707 * Make sure we free memory if there was an error.. The
12708 * ctl_datamove_remote_xfer() function will send the
12709 * datamove done message, or call the callback with an
12710 * error if there is a problem.
12712 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12713 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12720 * Process a datamove request from the other controller. This is used for
12721 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12722 * first. Once that is complete, the data gets DMAed into the remote
12723 * controller's memory. For reads, we DMA from the remote controller's
12724 * memory into our memory first, and then move it out to the FETD.
12727 ctl_datamove_remote(union ctl_io *io)
12729 struct ctl_softc *softc;
12731 softc = control_softc;
12733 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12736 * Note that we look for an aborted I/O here, but don't do some of
12737 * the other checks that ctl_datamove() normally does. We don't
12738 * need to run the task queue, because this I/O is on the ISC
12739 * queue, which is executed by the work thread after the task queue.
12740 * We don't need to run the datamove delay code, since that should
12741 * have been done if need be on the other controller.
12743 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12745 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12746 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12747 io->io_hdr.nexus.targ_port,
12748 io->io_hdr.nexus.targ_target.id,
12749 io->io_hdr.nexus.targ_lun);
12750 io->io_hdr.status = CTL_CMD_ABORTED;
12751 io->io_hdr.port_status = 31338;
12753 ctl_send_datamove_done(io, /*have_lock*/ 0);
12758 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12759 ctl_datamove_remote_write(io);
12760 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12761 ctl_datamove_remote_read(io);
12763 union ctl_ha_msg msg;
12764 struct scsi_sense_data *sense;
12768 memset(&msg, 0, sizeof(msg));
12770 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12771 msg.hdr.status = CTL_SCSI_ERROR;
12772 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12774 retry_count = 4243;
12776 sense = &msg.scsi.sense_data;
12777 sks[0] = SSD_SCS_VALID;
12778 sks[1] = (retry_count >> 8) & 0xff;
12779 sks[2] = retry_count & 0xff;
12781 /* "Internal target failure" */
12782 scsi_set_sense_data(sense,
12783 /*sense_format*/ SSD_TYPE_NONE,
12784 /*current_error*/ 1,
12785 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12788 /*type*/ SSD_ELEM_SKS,
12789 /*size*/ sizeof(sks),
12793 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12794 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12795 ctl_failover_io(io, /*have_lock*/ 1);
12799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12800 CTL_HA_STATUS_SUCCESS) {
12801 /* XXX KDM what to do if this fails? */
12809 ctl_process_done(union ctl_io *io)
12811 struct ctl_lun *lun;
12812 struct ctl_softc *ctl_softc;
12813 void (*fe_done)(union ctl_io *io);
12814 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12816 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12819 control_softc->ctl_ports[targ_port]->fe_done;
12822 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12827 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12828 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12830 sbuf_cat(&sb, path_str);
12831 switch (io->io_hdr.io_type) {
12833 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12834 sbuf_printf(&sb, "\n");
12835 sbuf_cat(&sb, path_str);
12836 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12837 io->scsiio.tag_num, io->scsiio.tag_type);
12840 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12841 "Tag Type: %d\n", io->taskio.task_action,
12842 io->taskio.tag_num, io->taskio.tag_type);
12845 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12846 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12849 sbuf_cat(&sb, path_str);
12850 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12851 (intmax_t)time_uptime - io->io_hdr.start_time);
12853 printf("%s", sbuf_data(&sb));
12855 #endif /* CTL_TIME_IO */
12857 switch (io->io_hdr.io_type) {
12861 if (bootverbose || verbose > 0)
12862 ctl_io_error_print(io, NULL);
12863 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12867 return (CTL_RETVAL_COMPLETE);
12870 printf("ctl_process_done: invalid io type %d\n",
12871 io->io_hdr.io_type);
12872 panic("ctl_process_done: invalid io type %d\n",
12873 io->io_hdr.io_type);
12874 break; /* NOTREACHED */
12877 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12879 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12880 io->io_hdr.nexus.targ_mapped_lun));
12884 ctl_softc = lun->ctl_softc;
12886 mtx_lock(&lun->lun_lock);
12889 * Check to see if we have any errors to inject here. We only
12890 * inject errors for commands that don't already have errors set.
12892 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12893 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12894 ctl_inject_error(lun, io);
12897 * XXX KDM how do we treat commands that aren't completed
12900 * XXX KDM should we also track I/O latency?
12902 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12903 io->io_hdr.io_type == CTL_IO_SCSI) {
12905 struct bintime cur_bt;
12909 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12911 type = CTL_STATS_READ;
12912 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12914 type = CTL_STATS_WRITE;
12916 type = CTL_STATS_NO_IO;
12918 lun->stats.ports[targ_port].bytes[type] +=
12919 io->scsiio.kern_total_len;
12920 lun->stats.ports[targ_port].operations[type]++;
12922 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
12923 &io->io_hdr.dma_bt);
12924 lun->stats.ports[targ_port].num_dmas[type] +=
12925 io->io_hdr.num_dmas;
12926 getbintime(&cur_bt);
12927 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12928 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
12933 * Remove this from the OOA queue.
12935 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12938 * Run through the blocked queue on this LUN and see if anything
12939 * has become unblocked, now that this transaction is done.
12941 ctl_check_blocked(lun);
12944 * If the LUN has been invalidated, free it if there is nothing
12945 * left on its OOA queue.
12947 if ((lun->flags & CTL_LUN_INVALID)
12948 && TAILQ_EMPTY(&lun->ooa_queue)) {
12949 mtx_unlock(&lun->lun_lock);
12950 mtx_lock(&ctl_softc->ctl_lock);
12952 mtx_unlock(&ctl_softc->ctl_lock);
12954 mtx_unlock(&lun->lun_lock);
12957 * If this command has been aborted, make sure we set the status
12958 * properly. The FETD is responsible for freeing the I/O and doing
12959 * whatever it needs to do to clean up its state.
12961 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12962 io->io_hdr.status = CTL_CMD_ABORTED;
12965 * We print out status for every task management command. For SCSI
12966 * commands, we filter out any unit attention errors; they happen
12967 * on every boot, and would clutter up the log. Note: task
12968 * management commands aren't printed here, they are printed above,
12969 * since they should never even make it down here.
12971 switch (io->io_hdr.io_type) {
12972 case CTL_IO_SCSI: {
12973 int error_code, sense_key, asc, ascq;
12977 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12978 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12980 * Since this is just for printing, no need to
12981 * show errors here.
12983 scsi_extract_sense_len(&io->scsiio.sense_data,
12984 io->scsiio.sense_len,
12989 /*show_errors*/ 0);
12992 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12993 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12994 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12995 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12997 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12998 ctl_softc->skipped_prints++;
13000 uint32_t skipped_prints;
13002 skipped_prints = ctl_softc->skipped_prints;
13004 ctl_softc->skipped_prints = 0;
13005 ctl_softc->last_print_jiffies = time_uptime;
13007 if (skipped_prints > 0) {
13009 csevent_log(CSC_CTL | CSC_SHELF_SW |
13011 csevent_LogType_Trace,
13012 csevent_Severity_Information,
13013 csevent_AlertLevel_Green,
13014 csevent_FRU_Firmware,
13015 csevent_FRU_Unknown,
13016 "High CTL error volume, %d prints "
13017 "skipped", skipped_prints);
13020 if (bootverbose || verbose > 0)
13021 ctl_io_error_print(io, NULL);
13027 if (bootverbose || verbose > 0)
13028 ctl_io_error_print(io, NULL);
13035 * Tell the FETD or the other shelf controller we're done with this
13036 * command. Note that only SCSI commands get to this point. Task
13037 * management commands are completed above.
13039 * We only send status to the other controller if we're in XFER
13040 * mode. In SER_ONLY mode, the I/O is done on the controller that
13041 * received the I/O (from CTL's perspective), and so the status is
13044 * XXX KDM if we hold the lock here, we could cause a deadlock
13045 * if the frontend comes back in in this context to queue
13048 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13049 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13050 union ctl_ha_msg msg;
13052 memset(&msg, 0, sizeof(msg));
13053 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13054 msg.hdr.original_sc = io->io_hdr.original_sc;
13055 msg.hdr.nexus = io->io_hdr.nexus;
13056 msg.hdr.status = io->io_hdr.status;
13057 msg.scsi.scsi_status = io->scsiio.scsi_status;
13058 msg.scsi.tag_num = io->scsiio.tag_num;
13059 msg.scsi.tag_type = io->scsiio.tag_type;
13060 msg.scsi.sense_len = io->scsiio.sense_len;
13061 msg.scsi.sense_residual = io->scsiio.sense_residual;
13062 msg.scsi.residual = io->scsiio.residual;
13063 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13064 sizeof(io->scsiio.sense_data));
13066 * We copy this whether or not this is an I/O-related
13067 * command. Otherwise, we'd have to go and check to see
13068 * whether it's a read/write command, and it really isn't
13071 memcpy(&msg.scsi.lbalen,
13072 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13073 sizeof(msg.scsi.lbalen));
13075 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13076 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13077 /* XXX do something here */
13086 return (CTL_RETVAL_COMPLETE);
13090 * Front end should call this if it doesn't do autosense. When the request
13091 * sense comes back in from the initiator, we'll dequeue this and send it.
13094 ctl_queue_sense(union ctl_io *io)
13096 struct ctl_lun *lun;
13097 struct ctl_softc *ctl_softc;
13098 uint32_t initidx, targ_lun;
13100 ctl_softc = control_softc;
13102 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13105 * LUN lookup will likely move to the ctl_work_thread() once we
13106 * have our new queueing infrastructure (that doesn't put things on
13107 * a per-LUN queue initially). That is so that we can handle
13108 * things like an INQUIRY to a LUN that we don't have enabled. We
13109 * can't deal with that right now.
13111 mtx_lock(&ctl_softc->ctl_lock);
13114 * If we don't have a LUN for this, just toss the sense
13117 targ_lun = io->io_hdr.nexus.targ_lun;
13118 if (io->io_hdr.nexus.lun_map_fn != NULL)
13119 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
13120 if ((targ_lun < CTL_MAX_LUNS)
13121 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13122 lun = ctl_softc->ctl_luns[targ_lun];
13126 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13128 mtx_lock(&lun->lun_lock);
13130 * Already have CA set for this LUN...toss the sense information.
13132 if (ctl_is_set(lun->have_ca, initidx)) {
13133 mtx_unlock(&lun->lun_lock);
13137 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13138 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13139 sizeof(io->scsiio.sense_data)));
13140 ctl_set_mask(lun->have_ca, initidx);
13141 mtx_unlock(&lun->lun_lock);
13144 mtx_unlock(&ctl_softc->ctl_lock);
13148 return (CTL_RETVAL_COMPLETE);
13152 * Primary command inlet from frontend ports. All SCSI and task I/O
13153 * requests must go through this function.
13156 ctl_queue(union ctl_io *io)
13158 struct ctl_softc *ctl_softc;
13160 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13162 ctl_softc = control_softc;
13165 io->io_hdr.start_time = time_uptime;
13166 getbintime(&io->io_hdr.start_bt);
13167 #endif /* CTL_TIME_IO */
13169 /* Map FE-specific LUN ID into global one. */
13170 if (io->io_hdr.nexus.lun_map_fn != NULL)
13171 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn(
13172 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun);
13174 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun;
13176 switch (io->io_hdr.io_type) {
13179 ctl_enqueue_incoming(io);
13182 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13186 return (CTL_RETVAL_COMPLETE);
13189 #ifdef CTL_IO_DELAY
13191 ctl_done_timer_wakeup(void *arg)
13195 io = (union ctl_io *)arg;
13198 #endif /* CTL_IO_DELAY */
13201 ctl_done(union ctl_io *io)
13203 struct ctl_softc *ctl_softc;
13205 ctl_softc = control_softc;
13208 * Enable this to catch duplicate completion issues.
13211 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13212 printf("%s: type %d msg %d cdb %x iptl: "
13213 "%d:%d:%d:%d tag 0x%04x "
13214 "flag %#x status %x\n",
13216 io->io_hdr.io_type,
13217 io->io_hdr.msg_type,
13219 io->io_hdr.nexus.initid.id,
13220 io->io_hdr.nexus.targ_port,
13221 io->io_hdr.nexus.targ_target.id,
13222 io->io_hdr.nexus.targ_lun,
13223 (io->io_hdr.io_type ==
13225 io->taskio.tag_num :
13226 io->scsiio.tag_num,
13228 io->io_hdr.status);
13230 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13234 * This is an internal copy of an I/O, and should not go through
13235 * the normal done processing logic.
13237 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13241 * We need to send a msg to the serializing shelf to finish the IO
13242 * as well. We don't send a finish message to the other shelf if
13243 * this is a task management command. Task management commands
13244 * aren't serialized in the OOA queue, but rather just executed on
13245 * both shelf controllers for commands that originated on that
13248 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13249 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13250 union ctl_ha_msg msg_io;
13252 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13253 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13254 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13255 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13257 /* continue on to finish IO */
13259 #ifdef CTL_IO_DELAY
13260 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13261 struct ctl_lun *lun;
13263 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13265 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13267 struct ctl_lun *lun;
13269 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13272 && (lun->delay_info.done_delay > 0)) {
13273 struct callout *callout;
13275 callout = (struct callout *)&io->io_hdr.timer_bytes;
13276 callout_init(callout, /*mpsafe*/ 1);
13277 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13278 callout_reset(callout,
13279 lun->delay_info.done_delay * hz,
13280 ctl_done_timer_wakeup, io);
13281 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13282 lun->delay_info.done_delay = 0;
13286 #endif /* CTL_IO_DELAY */
13288 ctl_enqueue_done(io);
13292 ctl_isc(struct ctl_scsiio *ctsio)
13294 struct ctl_lun *lun;
13297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13299 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13301 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13303 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13310 ctl_work_thread(void *arg)
13312 struct ctl_thread *thr = (struct ctl_thread *)arg;
13313 struct ctl_softc *softc = thr->ctl_softc;
13317 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13323 * We handle the queues in this order:
13325 * - done queue (to free up resources, unblock other commands)
13329 * If those queues are empty, we break out of the loop and
13332 mtx_lock(&thr->queue_lock);
13333 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13335 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13336 mtx_unlock(&thr->queue_lock);
13337 ctl_handle_isc(io);
13340 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13342 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13343 /* clear any blocked commands, call fe_done */
13344 mtx_unlock(&thr->queue_lock);
13345 retval = ctl_process_done(io);
13348 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13350 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13351 mtx_unlock(&thr->queue_lock);
13352 if (io->io_hdr.io_type == CTL_IO_TASK)
13355 ctl_scsiio_precheck(softc, &io->scsiio);
13358 if (!ctl_pause_rtr) {
13359 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13361 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13362 mtx_unlock(&thr->queue_lock);
13363 retval = ctl_scsiio(&io->scsiio);
13364 if (retval != CTL_RETVAL_COMPLETE)
13365 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13370 /* Sleep until we have something to do. */
13371 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13376 ctl_lun_thread(void *arg)
13378 struct ctl_softc *softc = (struct ctl_softc *)arg;
13379 struct ctl_be_lun *be_lun;
13382 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13386 mtx_lock(&softc->ctl_lock);
13387 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13388 if (be_lun != NULL) {
13389 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13390 mtx_unlock(&softc->ctl_lock);
13391 ctl_create_lun(be_lun);
13395 /* Sleep until we have something to do. */
13396 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13397 PDROP | PRIBIO, "-", 0);
13402 ctl_enqueue_incoming(union ctl_io *io)
13404 struct ctl_softc *softc = control_softc;
13405 struct ctl_thread *thr;
13408 idx = (io->io_hdr.nexus.targ_port * 127 +
13409 io->io_hdr.nexus.initid.id) % worker_threads;
13410 thr = &softc->threads[idx];
13411 mtx_lock(&thr->queue_lock);
13412 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13413 mtx_unlock(&thr->queue_lock);
13418 ctl_enqueue_rtr(union ctl_io *io)
13420 struct ctl_softc *softc = control_softc;
13421 struct ctl_thread *thr;
13423 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13424 mtx_lock(&thr->queue_lock);
13425 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13426 mtx_unlock(&thr->queue_lock);
13431 ctl_enqueue_done(union ctl_io *io)
13433 struct ctl_softc *softc = control_softc;
13434 struct ctl_thread *thr;
13436 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13437 mtx_lock(&thr->queue_lock);
13438 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13439 mtx_unlock(&thr->queue_lock);
13444 ctl_enqueue_isc(union ctl_io *io)
13446 struct ctl_softc *softc = control_softc;
13447 struct ctl_thread *thr;
13449 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13450 mtx_lock(&thr->queue_lock);
13451 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13452 mtx_unlock(&thr->queue_lock);
13456 /* Initialization and failover */
13459 ctl_init_isc_msg(void)
13461 printf("CTL: Still calling this thing\n");
13466 * Initializes component into configuration defined by bootMode
13468 * returns hasc_Status:
13470 * ERROR - fatal error
13472 static ctl_ha_comp_status
13473 ctl_isc_init(struct ctl_ha_component *c)
13475 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13482 * Starts component in state requested. If component starts successfully,
13483 * it must set its own state to the requestrd state
13484 * When requested state is HASC_STATE_HA, the component may refine it
13485 * by adding _SLAVE or _MASTER flags.
13486 * Currently allowed state transitions are:
13487 * UNKNOWN->HA - initial startup
13488 * UNKNOWN->SINGLE - initial startup when no parter detected
13489 * HA->SINGLE - failover
13490 * returns ctl_ha_comp_status:
13491 * OK - component successfully started in requested state
13492 * FAILED - could not start the requested state, failover may
13494 * ERROR - fatal error detected, no future startup possible
13496 static ctl_ha_comp_status
13497 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13499 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13501 printf("%s: go\n", __func__);
13503 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13504 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13506 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13507 != CTL_HA_STATUS_SUCCESS) {
13508 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13509 ret = CTL_HA_COMP_STATUS_ERROR;
13511 } else if (CTL_HA_STATE_IS_HA(c->state)
13512 && CTL_HA_STATE_IS_SINGLE(state)){
13513 // HA->SINGLE transition
13517 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13519 ret = CTL_HA_COMP_STATUS_ERROR;
13521 if (CTL_HA_STATE_IS_SINGLE(state))
13530 * Quiesce component
13531 * The component must clear any error conditions (set status to OK) and
13532 * prepare itself to another Start call
13533 * returns ctl_ha_comp_status:
13537 static ctl_ha_comp_status
13538 ctl_isc_quiesce(struct ctl_ha_component *c)
13540 int ret = CTL_HA_COMP_STATUS_OK;
13547 struct ctl_ha_component ctl_ha_component_ctlisc =
13550 .state = CTL_HA_STATE_UNKNOWN,
13551 .init = ctl_isc_init,
13552 .start = ctl_isc_start,
13553 .quiesce = ctl_isc_quiesce