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},
287 /*busy_timeout_period*/{0, 0},
288 /*extended_selftest_completion_time*/{0, 0}
291 static struct scsi_control_page control_page_changeable = {
292 /*page_code*/SMS_CONTROL_MODE_PAGE,
293 /*page_length*/sizeof(struct scsi_control_page) - 2,
298 /*aen_holdoff_period*/{0, 0},
299 /*busy_timeout_period*/{0, 0},
300 /*extended_selftest_completion_time*/{0, 0}
305 * XXX KDM move these into the softc.
307 static int rcv_sync_msg;
308 static int persis_offset;
309 static uint8_t ctl_pause_rtr;
310 static int ctl_is_single = 1;
311 static int index_to_aps_page;
313 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
314 static int worker_threads = -1;
315 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
316 &worker_threads, 1, "Number of worker threads");
317 static int verbose = 0;
318 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
319 &verbose, 0, "Show SCSI errors returned to initiator");
322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
323 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
324 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
326 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 8
328 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
330 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
331 static int ctl_init(void);
332 void ctl_shutdown(void);
333 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
334 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
335 static void ctl_ioctl_online(void *arg);
336 static void ctl_ioctl_offline(void *arg);
337 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
338 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
339 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
340 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
341 static int ctl_ioctl_submit_wait(union ctl_io *io);
342 static void ctl_ioctl_datamove(union ctl_io *io);
343 static void ctl_ioctl_done(union ctl_io *io);
344 static void ctl_ioctl_hard_startstop_callback(void *arg,
345 struct cfi_metatask *metatask);
346 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
347 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
348 struct ctl_ooa *ooa_hdr,
349 struct ctl_ooa_entry *kern_entries);
350 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
352 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
353 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
355 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
356 uint32_t targ_target, uint32_t targ_lun,
358 static void ctl_kfree_io(union ctl_io *io);
360 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
361 struct ctl_be_lun *be_lun, struct ctl_id target_id);
362 static int ctl_free_lun(struct ctl_lun *lun);
363 static void ctl_create_lun(struct ctl_be_lun *be_lun);
365 static void ctl_failover_change_pages(struct ctl_softc *softc,
366 struct ctl_scsiio *ctsio, int master);
369 static int ctl_do_mode_select(union ctl_io *io);
370 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
371 uint64_t res_key, uint64_t sa_res_key,
372 uint8_t type, uint32_t residx,
373 struct ctl_scsiio *ctsio,
374 struct scsi_per_res_out *cdb,
375 struct scsi_per_res_out_parms* param);
376 static void ctl_pro_preempt_other(struct ctl_lun *lun,
377 union ctl_ha_msg *msg);
378 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
379 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
380 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
381 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
384 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
386 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
388 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
389 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
390 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
391 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
392 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
393 union ctl_io *ooa_io);
394 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
395 union ctl_io *starting_io);
396 static int ctl_check_blocked(struct ctl_lun *lun);
397 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
399 const struct ctl_cmd_entry *entry,
400 struct ctl_scsiio *ctsio);
401 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
402 static void ctl_failover(void);
403 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
404 struct ctl_scsiio *ctsio);
405 static int ctl_scsiio(struct ctl_scsiio *ctsio);
407 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
408 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
409 ctl_ua_type ua_type);
410 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
411 ctl_ua_type ua_type);
412 static int ctl_abort_task(union ctl_io *io);
413 static int ctl_abort_task_set(union ctl_io *io);
414 static int ctl_i_t_nexus_reset(union ctl_io *io);
415 static void ctl_run_task(union ctl_io *io);
417 static void ctl_datamove_timer_wakeup(void *arg);
418 static void ctl_done_timer_wakeup(void *arg);
419 #endif /* CTL_IO_DELAY */
421 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
422 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
423 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
424 static void ctl_datamove_remote_write(union ctl_io *io);
425 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
426 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
427 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
428 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
429 ctl_ha_dt_cb callback);
430 static void ctl_datamove_remote_read(union ctl_io *io);
431 static void ctl_datamove_remote(union ctl_io *io);
432 static int ctl_process_done(union ctl_io *io);
433 static void ctl_lun_thread(void *arg);
434 static void ctl_work_thread(void *arg);
435 static void ctl_enqueue_incoming(union ctl_io *io);
436 static void ctl_enqueue_rtr(union ctl_io *io);
437 static void ctl_enqueue_done(union ctl_io *io);
438 static void ctl_enqueue_isc(union ctl_io *io);
439 static const struct ctl_cmd_entry *
440 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
441 static const struct ctl_cmd_entry *
442 ctl_validate_command(struct ctl_scsiio *ctsio);
443 static int ctl_cmd_applicable(uint8_t lun_type,
444 const struct ctl_cmd_entry *entry);
447 * Load the serialization table. This isn't very pretty, but is probably
448 * the easiest way to do it.
450 #include "ctl_ser_table.c"
453 * We only need to define open, close and ioctl routines for this driver.
455 static struct cdevsw ctl_cdevsw = {
456 .d_version = D_VERSION,
459 .d_close = ctl_close,
460 .d_ioctl = ctl_ioctl,
465 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
466 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
468 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
470 static moduledata_t ctl_moduledata = {
472 ctl_module_event_handler,
476 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
477 MODULE_VERSION(ctl, 1);
479 static struct ctl_frontend ioctl_frontend =
485 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
486 union ctl_ha_msg *msg_info)
488 struct ctl_scsiio *ctsio;
490 if (msg_info->hdr.original_sc == NULL) {
491 printf("%s: original_sc == NULL!\n", __func__);
492 /* XXX KDM now what? */
496 ctsio = &msg_info->hdr.original_sc->scsiio;
497 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
498 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
499 ctsio->io_hdr.status = msg_info->hdr.status;
500 ctsio->scsi_status = msg_info->scsi.scsi_status;
501 ctsio->sense_len = msg_info->scsi.sense_len;
502 ctsio->sense_residual = msg_info->scsi.sense_residual;
503 ctsio->residual = msg_info->scsi.residual;
504 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
505 sizeof(ctsio->sense_data));
506 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
507 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
508 ctl_enqueue_isc((union ctl_io *)ctsio);
512 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
513 union ctl_ha_msg *msg_info)
515 struct ctl_scsiio *ctsio;
517 if (msg_info->hdr.serializing_sc == NULL) {
518 printf("%s: serializing_sc == NULL!\n", __func__);
519 /* XXX KDM now what? */
523 ctsio = &msg_info->hdr.serializing_sc->scsiio;
526 * Attempt to catch the situation where an I/O has
527 * been freed, and we're using it again.
529 if (ctsio->io_hdr.io_type == 0xff) {
530 union ctl_io *tmp_io;
531 tmp_io = (union ctl_io *)ctsio;
532 printf("%s: %p use after free!\n", __func__,
534 printf("%s: type %d msg %d cdb %x iptl: "
535 "%d:%d:%d:%d tag 0x%04x "
536 "flag %#x status %x\n",
538 tmp_io->io_hdr.io_type,
539 tmp_io->io_hdr.msg_type,
540 tmp_io->scsiio.cdb[0],
541 tmp_io->io_hdr.nexus.initid.id,
542 tmp_io->io_hdr.nexus.targ_port,
543 tmp_io->io_hdr.nexus.targ_target.id,
544 tmp_io->io_hdr.nexus.targ_lun,
545 (tmp_io->io_hdr.io_type ==
547 tmp_io->taskio.tag_num :
548 tmp_io->scsiio.tag_num,
549 tmp_io->io_hdr.flags,
550 tmp_io->io_hdr.status);
553 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
554 ctl_enqueue_isc((union ctl_io *)ctsio);
558 * ISC (Inter Shelf Communication) event handler. Events from the HA
559 * subsystem come in here.
562 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
564 struct ctl_softc *ctl_softc;
566 struct ctl_prio *presio;
567 ctl_ha_status isc_status;
569 ctl_softc = control_softc;
574 printf("CTL: Isc Msg event %d\n", event);
576 if (event == CTL_HA_EVT_MSG_RECV) {
577 union ctl_ha_msg msg_info;
579 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
580 sizeof(msg_info), /*wait*/ 0);
582 printf("CTL: msg_type %d\n", msg_info.msg_type);
584 if (isc_status != 0) {
585 printf("Error receiving message, status = %d\n",
590 switch (msg_info.hdr.msg_type) {
591 case CTL_MSG_SERIALIZE:
593 printf("Serialize\n");
595 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
597 printf("ctl_isc_event_handler: can't allocate "
600 /* Need to set busy and send msg back */
601 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
602 msg_info.hdr.status = CTL_SCSI_ERROR;
603 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
604 msg_info.scsi.sense_len = 0;
605 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
606 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
611 // populate ctsio from msg_info
612 io->io_hdr.io_type = CTL_IO_SCSI;
613 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
614 io->io_hdr.original_sc = msg_info.hdr.original_sc;
616 printf("pOrig %x\n", (int)msg_info.original_sc);
618 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
621 * If we're in serialization-only mode, we don't
622 * want to go through full done processing. Thus
625 * XXX KDM add another flag that is more specific.
627 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
628 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
629 io->io_hdr.nexus = msg_info.hdr.nexus;
631 printf("targ %d, port %d, iid %d, lun %d\n",
632 io->io_hdr.nexus.targ_target.id,
633 io->io_hdr.nexus.targ_port,
634 io->io_hdr.nexus.initid.id,
635 io->io_hdr.nexus.targ_lun);
637 io->scsiio.tag_num = msg_info.scsi.tag_num;
638 io->scsiio.tag_type = msg_info.scsi.tag_type;
639 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
641 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
642 const struct ctl_cmd_entry *entry;
644 entry = ctl_get_cmd_entry(&io->scsiio);
645 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
647 entry->flags & CTL_FLAG_DATA_MASK;
652 /* Performed on the Originating SC, XFER mode only */
653 case CTL_MSG_DATAMOVE: {
654 struct ctl_sg_entry *sgl;
657 io = msg_info.hdr.original_sc;
659 printf("%s: original_sc == NULL!\n", __func__);
660 /* XXX KDM do something here */
663 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
664 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
666 * Keep track of this, we need to send it back over
667 * when the datamove is complete.
669 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
671 if (msg_info.dt.sg_sequence == 0) {
673 * XXX KDM we use the preallocated S/G list
674 * here, but we'll need to change this to
675 * dynamic allocation if we need larger S/G
678 if (msg_info.dt.kern_sg_entries >
679 sizeof(io->io_hdr.remote_sglist) /
680 sizeof(io->io_hdr.remote_sglist[0])) {
681 printf("%s: number of S/G entries "
682 "needed %u > allocated num %zd\n",
684 msg_info.dt.kern_sg_entries,
685 sizeof(io->io_hdr.remote_sglist)/
686 sizeof(io->io_hdr.remote_sglist[0]));
689 * XXX KDM send a message back to
690 * the other side to shut down the
691 * DMA. The error will come back
692 * through via the normal channel.
696 sgl = io->io_hdr.remote_sglist;
698 sizeof(io->io_hdr.remote_sglist));
700 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
702 io->scsiio.kern_sg_entries =
703 msg_info.dt.kern_sg_entries;
704 io->scsiio.rem_sg_entries =
705 msg_info.dt.kern_sg_entries;
706 io->scsiio.kern_data_len =
707 msg_info.dt.kern_data_len;
708 io->scsiio.kern_total_len =
709 msg_info.dt.kern_total_len;
710 io->scsiio.kern_data_resid =
711 msg_info.dt.kern_data_resid;
712 io->scsiio.kern_rel_offset =
713 msg_info.dt.kern_rel_offset;
715 * Clear out per-DMA flags.
717 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
719 * Add per-DMA flags that are set for this
720 * particular DMA request.
722 io->io_hdr.flags |= msg_info.dt.flags &
725 sgl = (struct ctl_sg_entry *)
726 io->scsiio.kern_data_ptr;
728 for (i = msg_info.dt.sent_sg_entries, j = 0;
729 i < (msg_info.dt.sent_sg_entries +
730 msg_info.dt.cur_sg_entries); i++, j++) {
731 sgl[i].addr = msg_info.dt.sg_list[j].addr;
732 sgl[i].len = msg_info.dt.sg_list[j].len;
735 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
737 msg_info.dt.sg_list[j].addr,
738 msg_info.dt.sg_list[j].len,
739 sgl[i].addr, sgl[i].len, j, i);
743 memcpy(&sgl[msg_info.dt.sent_sg_entries],
745 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
749 * If this is the last piece of the I/O, we've got
750 * the full S/G list. Queue processing in the thread.
751 * Otherwise wait for the next piece.
753 if (msg_info.dt.sg_last != 0)
757 /* Performed on the Serializing (primary) SC, XFER mode only */
758 case CTL_MSG_DATAMOVE_DONE: {
759 if (msg_info.hdr.serializing_sc == NULL) {
760 printf("%s: serializing_sc == NULL!\n",
762 /* XXX KDM now what? */
766 * We grab the sense information here in case
767 * there was a failure, so we can return status
768 * back to the initiator.
770 io = msg_info.hdr.serializing_sc;
771 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
772 io->io_hdr.status = msg_info.hdr.status;
773 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
774 io->scsiio.sense_len = msg_info.scsi.sense_len;
775 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
776 io->io_hdr.port_status = msg_info.scsi.fetd_status;
777 io->scsiio.residual = msg_info.scsi.residual;
778 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
779 sizeof(io->scsiio.sense_data));
784 /* Preformed on Originating SC, SER_ONLY mode */
786 io = msg_info.hdr.original_sc;
788 printf("%s: Major Bummer\n", __func__);
792 printf("pOrig %x\n",(int) ctsio);
795 io->io_hdr.msg_type = CTL_MSG_R2R;
796 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
801 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
803 * Performed on the Originating (i.e. secondary) SC in XFER
806 case CTL_MSG_FINISH_IO:
807 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
808 ctl_isc_handler_finish_xfer(ctl_softc,
811 ctl_isc_handler_finish_ser_only(ctl_softc,
815 /* Preformed on Originating SC */
816 case CTL_MSG_BAD_JUJU:
817 io = msg_info.hdr.original_sc;
819 printf("%s: Bad JUJU!, original_sc is NULL!\n",
823 ctl_copy_sense_data(&msg_info, io);
825 * IO should have already been cleaned up on other
826 * SC so clear this flag so we won't send a message
827 * back to finish the IO there.
829 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
830 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
832 /* io = msg_info.hdr.serializing_sc; */
833 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
837 /* Handle resets sent from the other side */
838 case CTL_MSG_MANAGE_TASKS: {
839 struct ctl_taskio *taskio;
840 taskio = (struct ctl_taskio *)ctl_alloc_io(
841 (void *)ctl_softc->othersc_pool);
842 if (taskio == NULL) {
843 printf("ctl_isc_event_handler: can't allocate "
846 /* should I just call the proper reset func
850 ctl_zero_io((union ctl_io *)taskio);
851 taskio->io_hdr.io_type = CTL_IO_TASK;
852 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
853 taskio->io_hdr.nexus = msg_info.hdr.nexus;
854 taskio->task_action = msg_info.task.task_action;
855 taskio->tag_num = msg_info.task.tag_num;
856 taskio->tag_type = msg_info.task.tag_type;
858 taskio->io_hdr.start_time = time_uptime;
859 getbintime(&taskio->io_hdr.start_bt);
861 cs_prof_gettime(&taskio->io_hdr.start_ticks);
863 #endif /* CTL_TIME_IO */
864 ctl_run_task((union ctl_io *)taskio);
867 /* Persistent Reserve action which needs attention */
868 case CTL_MSG_PERS_ACTION:
869 presio = (struct ctl_prio *)ctl_alloc_io(
870 (void *)ctl_softc->othersc_pool);
871 if (presio == NULL) {
872 printf("ctl_isc_event_handler: can't allocate "
875 /* Need to set busy and send msg back */
878 ctl_zero_io((union ctl_io *)presio);
879 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
880 presio->pr_msg = msg_info.pr;
881 ctl_enqueue_isc((union ctl_io *)presio);
883 case CTL_MSG_SYNC_FE:
886 case CTL_MSG_APS_LOCK: {
887 // It's quicker to execute this then to
890 struct ctl_page_index *page_index;
891 struct copan_aps_subpage *current_sp;
894 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
895 lun = ctl_softc->ctl_luns[targ_lun];
896 mtx_lock(&lun->lun_lock);
897 page_index = &lun->mode_pages.index[index_to_aps_page];
898 current_sp = (struct copan_aps_subpage *)
899 (page_index->page_data +
900 (page_index->page_len * CTL_PAGE_CURRENT));
902 current_sp->lock_active = msg_info.aps.lock_flag;
903 mtx_unlock(&lun->lun_lock);
907 printf("How did I get here?\n");
909 } else if (event == CTL_HA_EVT_MSG_SENT) {
910 if (param != CTL_HA_STATUS_SUCCESS) {
911 printf("Bad status from ctl_ha_msg_send status %d\n",
915 } else if (event == CTL_HA_EVT_DISCONNECT) {
916 printf("CTL: Got a disconnect from Isc\n");
919 printf("ctl_isc_event_handler: Unknown event %d\n", event);
928 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
930 struct scsi_sense_data *sense;
932 sense = &dest->scsiio.sense_data;
933 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
934 dest->scsiio.scsi_status = src->scsi.scsi_status;
935 dest->scsiio.sense_len = src->scsi.sense_len;
936 dest->io_hdr.status = src->hdr.status;
942 struct ctl_softc *softc;
943 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
944 struct ctl_port *port;
946 int i, error, retval;
953 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
955 softc = control_softc;
957 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
960 softc->dev->si_drv1 = softc;
963 * By default, return a "bad LUN" peripheral qualifier for unknown
964 * LUNs. The user can override this default using the tunable or
965 * sysctl. See the comment in ctl_inquiry_std() for more details.
967 softc->inquiry_pq_no_lun = 1;
968 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
969 &softc->inquiry_pq_no_lun);
970 sysctl_ctx_init(&softc->sysctl_ctx);
971 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
972 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
973 CTLFLAG_RD, 0, "CAM Target Layer");
975 if (softc->sysctl_tree == NULL) {
976 printf("%s: unable to allocate sysctl tree\n", __func__);
977 destroy_dev(softc->dev);
978 free(control_softc, M_DEVBUF);
979 control_softc = NULL;
983 SYSCTL_ADD_INT(&softc->sysctl_ctx,
984 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
985 "inquiry_pq_no_lun", CTLFLAG_RW,
986 &softc->inquiry_pq_no_lun, 0,
987 "Report no lun possible for invalid LUNs");
989 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
990 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
991 softc->open_count = 0;
994 * Default to actually sending a SYNCHRONIZE CACHE command down to
997 softc->flags = CTL_FLAG_REAL_SYNC;
1000 * In Copan's HA scheme, the "master" and "slave" roles are
1001 * figured out through the slot the controller is in. Although it
1002 * is an active/active system, someone has to be in charge.
1005 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1009 softc->flags |= CTL_FLAG_MASTER_SHELF;
1012 persis_offset = CTL_MAX_INITIATORS;
1015 * XXX KDM need to figure out where we want to get our target ID
1016 * and WWID. Is it different on each port?
1018 softc->target.id = 0;
1019 softc->target.wwid[0] = 0x12345678;
1020 softc->target.wwid[1] = 0x87654321;
1021 STAILQ_INIT(&softc->lun_list);
1022 STAILQ_INIT(&softc->pending_lun_queue);
1023 STAILQ_INIT(&softc->fe_list);
1024 STAILQ_INIT(&softc->port_list);
1025 STAILQ_INIT(&softc->be_list);
1026 STAILQ_INIT(&softc->io_pools);
1027 ctl_tpc_init(softc);
1029 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1030 &internal_pool)!= 0){
1031 printf("ctl: can't allocate %d entry internal pool, "
1032 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1036 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1037 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1038 printf("ctl: can't allocate %d entry emergency pool, "
1039 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1040 ctl_pool_free(internal_pool);
1044 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1047 printf("ctl: can't allocate %d entry other SC pool, "
1048 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1049 ctl_pool_free(internal_pool);
1050 ctl_pool_free(emergency_pool);
1054 softc->internal_pool = internal_pool;
1055 softc->emergency_pool = emergency_pool;
1056 softc->othersc_pool = other_pool;
1058 if (worker_threads <= 0)
1059 worker_threads = max(1, mp_ncpus / 4);
1060 if (worker_threads > CTL_MAX_THREADS)
1061 worker_threads = CTL_MAX_THREADS;
1063 for (i = 0; i < worker_threads; i++) {
1064 struct ctl_thread *thr = &softc->threads[i];
1066 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1067 thr->ctl_softc = softc;
1068 STAILQ_INIT(&thr->incoming_queue);
1069 STAILQ_INIT(&thr->rtr_queue);
1070 STAILQ_INIT(&thr->done_queue);
1071 STAILQ_INIT(&thr->isc_queue);
1073 error = kproc_kthread_add(ctl_work_thread, thr,
1074 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1076 printf("error creating CTL work thread!\n");
1077 ctl_pool_free(internal_pool);
1078 ctl_pool_free(emergency_pool);
1079 ctl_pool_free(other_pool);
1083 error = kproc_kthread_add(ctl_lun_thread, softc,
1084 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1086 printf("error creating CTL lun thread!\n");
1087 ctl_pool_free(internal_pool);
1088 ctl_pool_free(emergency_pool);
1089 ctl_pool_free(other_pool);
1093 printf("ctl: CAM Target Layer loaded\n");
1096 * Initialize the ioctl front end.
1098 ctl_frontend_register(&ioctl_frontend);
1099 port = &softc->ioctl_info.port;
1100 port->frontend = &ioctl_frontend;
1101 sprintf(softc->ioctl_info.port_name, "ioctl");
1102 port->port_type = CTL_PORT_IOCTL;
1103 port->num_requested_ctl_io = 100;
1104 port->port_name = softc->ioctl_info.port_name;
1105 port->port_online = ctl_ioctl_online;
1106 port->port_offline = ctl_ioctl_offline;
1107 port->onoff_arg = &softc->ioctl_info;
1108 port->lun_enable = ctl_ioctl_lun_enable;
1109 port->lun_disable = ctl_ioctl_lun_disable;
1110 port->targ_lun_arg = &softc->ioctl_info;
1111 port->fe_datamove = ctl_ioctl_datamove;
1112 port->fe_done = ctl_ioctl_done;
1113 port->max_targets = 15;
1114 port->max_target_id = 15;
1116 if (ctl_port_register(&softc->ioctl_info.port,
1117 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1118 printf("ctl: ioctl front end registration failed, will "
1119 "continue anyway\n");
1123 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1124 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1125 sizeof(struct callout), CTL_TIMER_BYTES);
1128 #endif /* CTL_IO_DELAY */
1136 struct ctl_softc *softc;
1137 struct ctl_lun *lun, *next_lun;
1138 struct ctl_io_pool *pool;
1140 softc = (struct ctl_softc *)control_softc;
1142 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1143 printf("ctl: ioctl front end deregistration failed\n");
1145 mtx_lock(&softc->ctl_lock);
1150 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1151 next_lun = STAILQ_NEXT(lun, links);
1155 mtx_unlock(&softc->ctl_lock);
1157 ctl_frontend_deregister(&ioctl_frontend);
1160 * This will rip the rug out from under any FETDs or anyone else
1161 * that has a pool allocated. Since we increment our module
1162 * refcount any time someone outside the main CTL module allocates
1163 * a pool, we shouldn't have any problems here. The user won't be
1164 * able to unload the CTL module until client modules have
1165 * successfully unloaded.
1167 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1168 ctl_pool_free(pool);
1171 ctl_shutdown_thread(softc->work_thread);
1172 mtx_destroy(&softc->queue_lock);
1175 ctl_tpc_shutdown(softc);
1176 mtx_destroy(&softc->pool_lock);
1177 mtx_destroy(&softc->ctl_lock);
1179 destroy_dev(softc->dev);
1181 sysctl_ctx_free(&softc->sysctl_ctx);
1183 free(control_softc, M_DEVBUF);
1184 control_softc = NULL;
1187 printf("ctl: CAM Target Layer unloaded\n");
1191 ctl_module_event_handler(module_t mod, int what, void *arg)
1196 return (ctl_init());
1200 return (EOPNOTSUPP);
1205 * XXX KDM should we do some access checks here? Bump a reference count to
1206 * prevent a CTL module from being unloaded while someone has it open?
1209 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1215 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1221 ctl_port_enable(ctl_port_type port_type)
1223 struct ctl_softc *softc;
1224 struct ctl_port *port;
1226 if (ctl_is_single == 0) {
1227 union ctl_ha_msg msg_info;
1231 printf("%s: HA mode, synchronizing frontend enable\n",
1234 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1235 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1236 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1237 printf("Sync msg send error retval %d\n", isc_retval);
1239 if (!rcv_sync_msg) {
1240 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1241 sizeof(msg_info), 1);
1244 printf("CTL:Frontend Enable\n");
1246 printf("%s: single mode, skipping frontend synchronization\n",
1251 softc = control_softc;
1253 STAILQ_FOREACH(port, &softc->port_list, links) {
1254 if (port_type & port->port_type)
1257 printf("port %d\n", port->targ_port);
1259 ctl_port_online(port);
1267 ctl_port_disable(ctl_port_type port_type)
1269 struct ctl_softc *softc;
1270 struct ctl_port *port;
1272 softc = control_softc;
1274 STAILQ_FOREACH(port, &softc->port_list, links) {
1275 if (port_type & port->port_type)
1276 ctl_port_offline(port);
1283 * Returns 0 for success, 1 for failure.
1284 * Currently the only failure mode is if there aren't enough entries
1285 * allocated. So, in case of a failure, look at num_entries_dropped,
1286 * reallocate and try again.
1289 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1290 int *num_entries_filled, int *num_entries_dropped,
1291 ctl_port_type port_type, int no_virtual)
1293 struct ctl_softc *softc;
1294 struct ctl_port *port;
1295 int entries_dropped, entries_filled;
1299 softc = control_softc;
1303 entries_dropped = 0;
1306 mtx_lock(&softc->ctl_lock);
1307 STAILQ_FOREACH(port, &softc->port_list, links) {
1308 struct ctl_port_entry *entry;
1310 if ((port->port_type & port_type) == 0)
1313 if ((no_virtual != 0)
1314 && (port->virtual_port != 0))
1317 if (entries_filled >= num_entries_alloced) {
1321 entry = &entries[i];
1323 entry->port_type = port->port_type;
1324 strlcpy(entry->port_name, port->port_name,
1325 sizeof(entry->port_name));
1326 entry->physical_port = port->physical_port;
1327 entry->virtual_port = port->virtual_port;
1328 entry->wwnn = port->wwnn;
1329 entry->wwpn = port->wwpn;
1335 mtx_unlock(&softc->ctl_lock);
1337 if (entries_dropped > 0)
1340 *num_entries_dropped = entries_dropped;
1341 *num_entries_filled = entries_filled;
1347 ctl_ioctl_online(void *arg)
1349 struct ctl_ioctl_info *ioctl_info;
1351 ioctl_info = (struct ctl_ioctl_info *)arg;
1353 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1357 ctl_ioctl_offline(void *arg)
1359 struct ctl_ioctl_info *ioctl_info;
1361 ioctl_info = (struct ctl_ioctl_info *)arg;
1363 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1367 * Remove an initiator by port number and initiator ID.
1368 * Returns 0 for success, -1 for failure.
1371 ctl_remove_initiator(struct ctl_port *port, int iid)
1373 struct ctl_softc *softc = control_softc;
1375 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1377 if (iid > CTL_MAX_INIT_PER_PORT) {
1378 printf("%s: initiator ID %u > maximun %u!\n",
1379 __func__, iid, CTL_MAX_INIT_PER_PORT);
1383 mtx_lock(&softc->ctl_lock);
1384 port->wwpn_iid[iid].in_use--;
1385 port->wwpn_iid[iid].last_use = time_uptime;
1386 mtx_unlock(&softc->ctl_lock);
1392 * Add an initiator to the initiator map.
1393 * Returns iid for success, < 0 for failure.
1396 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1398 struct ctl_softc *softc = control_softc;
1402 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1404 if (iid >= CTL_MAX_INIT_PER_PORT) {
1405 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1406 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1411 mtx_lock(&softc->ctl_lock);
1413 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1414 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1415 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1419 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1420 strcmp(name, port->wwpn_iid[i].name) == 0) {
1428 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1429 if (port->wwpn_iid[i].in_use == 0 &&
1430 port->wwpn_iid[i].wwpn == 0 &&
1431 port->wwpn_iid[i].name == NULL) {
1440 best_time = INT32_MAX;
1441 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1442 if (port->wwpn_iid[i].in_use == 0) {
1443 if (port->wwpn_iid[i].last_use < best_time) {
1445 best_time = port->wwpn_iid[i].last_use;
1453 mtx_unlock(&softc->ctl_lock);
1458 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1460 * This is not an error yet.
1462 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1464 printf("%s: port %d iid %u WWPN %#jx arrived"
1465 " again\n", __func__, port->targ_port,
1466 iid, (uintmax_t)wwpn);
1470 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1471 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1473 printf("%s: port %d iid %u name '%s' arrived"
1474 " again\n", __func__, port->targ_port,
1481 * This is an error, but what do we do about it? The
1482 * driver is telling us we have a new WWPN for this
1483 * initiator ID, so we pretty much need to use it.
1485 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1486 " but WWPN %#jx '%s' is still at that address\n",
1487 __func__, port->targ_port, iid, wwpn, name,
1488 (uintmax_t)port->wwpn_iid[iid].wwpn,
1489 port->wwpn_iid[iid].name);
1492 * XXX KDM clear have_ca and ua_pending on each LUN for
1497 free(port->wwpn_iid[iid].name, M_CTL);
1498 port->wwpn_iid[iid].name = name;
1499 port->wwpn_iid[iid].wwpn = wwpn;
1500 port->wwpn_iid[iid].in_use++;
1501 mtx_unlock(&softc->ctl_lock);
1507 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1511 switch (port->port_type) {
1514 struct scsi_transportid_fcp *id =
1515 (struct scsi_transportid_fcp *)buf;
1516 if (port->wwpn_iid[iid].wwpn == 0)
1518 memset(id, 0, sizeof(*id));
1519 id->format_protocol = SCSI_PROTO_FC;
1520 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1521 return (sizeof(*id));
1523 case CTL_PORT_ISCSI:
1525 struct scsi_transportid_iscsi_port *id =
1526 (struct scsi_transportid_iscsi_port *)buf;
1527 if (port->wwpn_iid[iid].name == NULL)
1530 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1532 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1533 len = roundup2(min(len, 252), 4);
1534 scsi_ulto2b(len, id->additional_length);
1535 return (sizeof(*id) + len);
1539 struct scsi_transportid_sas *id =
1540 (struct scsi_transportid_sas *)buf;
1541 if (port->wwpn_iid[iid].wwpn == 0)
1543 memset(id, 0, sizeof(*id));
1544 id->format_protocol = SCSI_PROTO_SAS;
1545 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1546 return (sizeof(*id));
1550 struct scsi_transportid_spi *id =
1551 (struct scsi_transportid_spi *)buf;
1552 memset(id, 0, sizeof(*id));
1553 id->format_protocol = SCSI_PROTO_SPI;
1554 scsi_ulto2b(iid, id->scsi_addr);
1555 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1556 return (sizeof(*id));
1562 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1568 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1574 * Data movement routine for the CTL ioctl frontend port.
1577 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1579 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1580 struct ctl_sg_entry ext_entry, kern_entry;
1581 int ext_sglen, ext_sg_entries, kern_sg_entries;
1582 int ext_sg_start, ext_offset;
1583 int len_to_copy, len_copied;
1584 int kern_watermark, ext_watermark;
1585 int ext_sglist_malloced;
1588 ext_sglist_malloced = 0;
1592 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1595 * If this flag is set, fake the data transfer.
1597 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1598 ctsio->ext_data_filled = ctsio->ext_data_len;
1603 * To simplify things here, if we have a single buffer, stick it in
1604 * a S/G entry and just make it a single entry S/G list.
1606 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1609 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1611 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1613 ext_sglist_malloced = 1;
1614 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1616 ctl_set_internal_failure(ctsio,
1621 ext_sg_entries = ctsio->ext_sg_entries;
1623 for (i = 0; i < ext_sg_entries; i++) {
1624 if ((len_seen + ext_sglist[i].len) >=
1625 ctsio->ext_data_filled) {
1627 ext_offset = ctsio->ext_data_filled - len_seen;
1630 len_seen += ext_sglist[i].len;
1633 ext_sglist = &ext_entry;
1634 ext_sglist->addr = ctsio->ext_data_ptr;
1635 ext_sglist->len = ctsio->ext_data_len;
1638 ext_offset = ctsio->ext_data_filled;
1641 if (ctsio->kern_sg_entries > 0) {
1642 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1643 kern_sg_entries = ctsio->kern_sg_entries;
1645 kern_sglist = &kern_entry;
1646 kern_sglist->addr = ctsio->kern_data_ptr;
1647 kern_sglist->len = ctsio->kern_data_len;
1648 kern_sg_entries = 1;
1653 ext_watermark = ext_offset;
1655 for (i = ext_sg_start, j = 0;
1656 i < ext_sg_entries && j < kern_sg_entries;) {
1657 uint8_t *ext_ptr, *kern_ptr;
1659 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1660 kern_sglist[j].len - kern_watermark);
1662 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1663 ext_ptr = ext_ptr + ext_watermark;
1664 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1668 panic("need to implement bus address support");
1670 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1673 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1674 kern_ptr = kern_ptr + kern_watermark;
1676 kern_watermark += len_to_copy;
1677 ext_watermark += len_to_copy;
1679 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1681 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1682 "bytes to user\n", len_to_copy));
1683 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1684 "to %p\n", kern_ptr, ext_ptr));
1685 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1686 ctl_set_internal_failure(ctsio,
1692 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1693 "bytes from user\n", len_to_copy));
1694 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1695 "to %p\n", ext_ptr, kern_ptr));
1696 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1697 ctl_set_internal_failure(ctsio,
1704 len_copied += len_to_copy;
1706 if (ext_sglist[i].len == ext_watermark) {
1711 if (kern_sglist[j].len == kern_watermark) {
1717 ctsio->ext_data_filled += len_copied;
1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1720 "kern_sg_entries: %d\n", ext_sg_entries,
1722 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1723 "kern_data_len = %d\n", ctsio->ext_data_len,
1724 ctsio->kern_data_len));
1727 /* XXX KDM set residual?? */
1730 if (ext_sglist_malloced != 0)
1731 free(ext_sglist, M_CTL);
1733 return (CTL_RETVAL_COMPLETE);
1737 * Serialize a command that went down the "wrong" side, and so was sent to
1738 * this controller for execution. The logic is a little different than the
1739 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1740 * sent back to the other side, but in the success case, we execute the
1741 * command on this side (XFER mode) or tell the other side to execute it
1745 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1747 struct ctl_softc *ctl_softc;
1748 union ctl_ha_msg msg_info;
1749 struct ctl_lun *lun;
1753 ctl_softc = control_softc;
1755 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1756 lun = ctl_softc->ctl_luns[targ_lun];
1760 * Why isn't LUN defined? The other side wouldn't
1761 * send a cmd if the LUN is undefined.
1763 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1765 /* "Logical unit not supported" */
1766 ctl_set_sense_data(&msg_info.scsi.sense_data,
1768 /*sense_format*/SSD_TYPE_NONE,
1769 /*current_error*/ 1,
1770 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1775 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1776 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1777 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1778 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1779 msg_info.hdr.serializing_sc = NULL;
1780 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1781 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1782 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1788 mtx_lock(&lun->lun_lock);
1789 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1791 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1792 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1794 case CTL_ACTION_BLOCK:
1795 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1796 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1799 case CTL_ACTION_PASS:
1800 case CTL_ACTION_SKIP:
1801 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1802 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1803 ctl_enqueue_rtr((union ctl_io *)ctsio);
1806 /* send msg back to other side */
1807 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1808 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1809 msg_info.hdr.msg_type = CTL_MSG_R2R;
1811 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1813 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1814 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1818 case CTL_ACTION_OVERLAP:
1819 /* OVERLAPPED COMMANDS ATTEMPTED */
1820 ctl_set_sense_data(&msg_info.scsi.sense_data,
1822 /*sense_format*/SSD_TYPE_NONE,
1823 /*current_error*/ 1,
1824 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1829 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1830 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1831 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1832 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1833 msg_info.hdr.serializing_sc = NULL;
1834 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1836 printf("BAD JUJU:Major Bummer Overlap\n");
1838 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1840 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1841 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1844 case CTL_ACTION_OVERLAP_TAG:
1845 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1846 ctl_set_sense_data(&msg_info.scsi.sense_data,
1848 /*sense_format*/SSD_TYPE_NONE,
1849 /*current_error*/ 1,
1850 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1852 /*ascq*/ ctsio->tag_num & 0xff,
1855 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1856 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1857 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1858 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1859 msg_info.hdr.serializing_sc = NULL;
1860 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1862 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1864 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1866 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1867 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1870 case CTL_ACTION_ERROR:
1872 /* "Internal target failure" */
1873 ctl_set_sense_data(&msg_info.scsi.sense_data,
1875 /*sense_format*/SSD_TYPE_NONE,
1876 /*current_error*/ 1,
1877 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1882 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1883 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1884 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1885 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1886 msg_info.hdr.serializing_sc = NULL;
1887 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1889 printf("BAD JUJU:Major Bummer HW Error\n");
1891 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1893 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1894 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1898 mtx_unlock(&lun->lun_lock);
1903 ctl_ioctl_submit_wait(union ctl_io *io)
1905 struct ctl_fe_ioctl_params params;
1906 ctl_fe_ioctl_state last_state;
1911 bzero(¶ms, sizeof(params));
1913 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1914 cv_init(¶ms.sem, "ctlioccv");
1915 params.state = CTL_IOCTL_INPROG;
1916 last_state = params.state;
1918 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1920 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1922 /* This shouldn't happen */
1923 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1929 mtx_lock(¶ms.ioctl_mtx);
1931 * Check the state here, and don't sleep if the state has
1932 * already changed (i.e. wakeup has already occured, but we
1933 * weren't waiting yet).
1935 if (params.state == last_state) {
1936 /* XXX KDM cv_wait_sig instead? */
1937 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1939 last_state = params.state;
1941 switch (params.state) {
1942 case CTL_IOCTL_INPROG:
1943 /* Why did we wake up? */
1944 /* XXX KDM error here? */
1945 mtx_unlock(¶ms.ioctl_mtx);
1947 case CTL_IOCTL_DATAMOVE:
1948 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1951 * change last_state back to INPROG to avoid
1952 * deadlock on subsequent data moves.
1954 params.state = last_state = CTL_IOCTL_INPROG;
1956 mtx_unlock(¶ms.ioctl_mtx);
1957 ctl_ioctl_do_datamove(&io->scsiio);
1959 * Note that in some cases, most notably writes,
1960 * this will queue the I/O and call us back later.
1961 * In other cases, generally reads, this routine
1962 * will immediately call back and wake us up,
1963 * probably using our own context.
1965 io->scsiio.be_move_done(io);
1967 case CTL_IOCTL_DONE:
1968 mtx_unlock(¶ms.ioctl_mtx);
1969 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1973 mtx_unlock(¶ms.ioctl_mtx);
1974 /* XXX KDM error here? */
1977 } while (done == 0);
1979 mtx_destroy(¶ms.ioctl_mtx);
1980 cv_destroy(¶ms.sem);
1982 return (CTL_RETVAL_COMPLETE);
1986 ctl_ioctl_datamove(union ctl_io *io)
1988 struct ctl_fe_ioctl_params *params;
1990 params = (struct ctl_fe_ioctl_params *)
1991 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1993 mtx_lock(¶ms->ioctl_mtx);
1994 params->state = CTL_IOCTL_DATAMOVE;
1995 cv_broadcast(¶ms->sem);
1996 mtx_unlock(¶ms->ioctl_mtx);
2000 ctl_ioctl_done(union ctl_io *io)
2002 struct ctl_fe_ioctl_params *params;
2004 params = (struct ctl_fe_ioctl_params *)
2005 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2007 mtx_lock(¶ms->ioctl_mtx);
2008 params->state = CTL_IOCTL_DONE;
2009 cv_broadcast(¶ms->sem);
2010 mtx_unlock(¶ms->ioctl_mtx);
2014 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2016 struct ctl_fe_ioctl_startstop_info *sd_info;
2018 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2020 sd_info->hs_info.status = metatask->status;
2021 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2022 sd_info->hs_info.luns_complete =
2023 metatask->taskinfo.startstop.luns_complete;
2024 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2026 cv_broadcast(&sd_info->sem);
2030 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2032 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2034 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2036 mtx_lock(fe_bbr_info->lock);
2037 fe_bbr_info->bbr_info->status = metatask->status;
2038 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2039 fe_bbr_info->wakeup_done = 1;
2040 mtx_unlock(fe_bbr_info->lock);
2042 cv_broadcast(&fe_bbr_info->sem);
2046 * Returns 0 for success, errno for failure.
2049 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2050 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2057 mtx_lock(&lun->lun_lock);
2058 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2059 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2061 struct ctl_ooa_entry *entry;
2064 * If we've got more than we can fit, just count the
2065 * remaining entries.
2067 if (*cur_fill_num >= ooa_hdr->alloc_num)
2070 entry = &kern_entries[*cur_fill_num];
2072 entry->tag_num = io->scsiio.tag_num;
2073 entry->lun_num = lun->lun;
2075 entry->start_bt = io->io_hdr.start_bt;
2077 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2078 entry->cdb_len = io->scsiio.cdb_len;
2079 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2080 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2082 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2083 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2085 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2086 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2088 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2089 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2091 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2092 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2094 mtx_unlock(&lun->lun_lock);
2100 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2101 size_t error_str_len)
2105 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2107 if (copyin(user_addr, kptr, len) != 0) {
2108 snprintf(error_str, error_str_len, "Error copying %d bytes "
2109 "from user address %p to kernel address %p", len,
2119 ctl_free_args(int num_args, struct ctl_be_arg *args)
2126 for (i = 0; i < num_args; i++) {
2127 free(args[i].kname, M_CTL);
2128 free(args[i].kvalue, M_CTL);
2134 static struct ctl_be_arg *
2135 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2136 char *error_str, size_t error_str_len)
2138 struct ctl_be_arg *args;
2141 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2142 error_str, error_str_len);
2147 for (i = 0; i < num_args; i++) {
2148 args[i].kname = NULL;
2149 args[i].kvalue = NULL;
2152 for (i = 0; i < num_args; i++) {
2155 args[i].kname = ctl_copyin_alloc(args[i].name,
2156 args[i].namelen, error_str, error_str_len);
2157 if (args[i].kname == NULL)
2160 if (args[i].kname[args[i].namelen - 1] != '\0') {
2161 snprintf(error_str, error_str_len, "Argument %d "
2162 "name is not NUL-terminated", i);
2166 if (args[i].flags & CTL_BEARG_RD) {
2167 tmpptr = ctl_copyin_alloc(args[i].value,
2168 args[i].vallen, error_str, error_str_len);
2171 if ((args[i].flags & CTL_BEARG_ASCII)
2172 && (tmpptr[args[i].vallen - 1] != '\0')) {
2173 snprintf(error_str, error_str_len, "Argument "
2174 "%d value is not NUL-terminated", i);
2177 args[i].kvalue = tmpptr;
2179 args[i].kvalue = malloc(args[i].vallen,
2180 M_CTL, M_WAITOK | M_ZERO);
2187 ctl_free_args(num_args, args);
2193 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2197 for (i = 0; i < num_args; i++) {
2198 if (args[i].flags & CTL_BEARG_WR)
2199 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2204 * Escape characters that are illegal or not recommended in XML.
2207 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2213 for (; *str; str++) {
2216 retval = sbuf_printf(sb, "&");
2219 retval = sbuf_printf(sb, ">");
2222 retval = sbuf_printf(sb, "<");
2225 retval = sbuf_putc(sb, *str);
2238 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2241 struct ctl_softc *softc;
2244 softc = control_softc;
2254 * If we haven't been "enabled", don't allow any SCSI I/O
2257 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2262 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2264 printf("ctl_ioctl: can't allocate ctl_io!\n");
2270 * Need to save the pool reference so it doesn't get
2271 * spammed by the user's ctl_io.
2273 pool_tmp = io->io_hdr.pool;
2275 memcpy(io, (void *)addr, sizeof(*io));
2277 io->io_hdr.pool = pool_tmp;
2279 * No status yet, so make sure the status is set properly.
2281 io->io_hdr.status = CTL_STATUS_NONE;
2284 * The user sets the initiator ID, target and LUN IDs.
2286 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2287 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2288 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2289 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2290 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2292 retval = ctl_ioctl_submit_wait(io);
2299 memcpy((void *)addr, io, sizeof(*io));
2301 /* return this to our pool */
2306 case CTL_ENABLE_PORT:
2307 case CTL_DISABLE_PORT:
2308 case CTL_SET_PORT_WWNS: {
2309 struct ctl_port *port;
2310 struct ctl_port_entry *entry;
2312 entry = (struct ctl_port_entry *)addr;
2314 mtx_lock(&softc->ctl_lock);
2315 STAILQ_FOREACH(port, &softc->port_list, links) {
2321 if ((entry->port_type == CTL_PORT_NONE)
2322 && (entry->targ_port == port->targ_port)) {
2324 * If the user only wants to enable or
2325 * disable or set WWNs on a specific port,
2326 * do the operation and we're done.
2330 } else if (entry->port_type & port->port_type) {
2332 * Compare the user's type mask with the
2333 * particular frontend type to see if we
2340 * Make sure the user isn't trying to set
2341 * WWNs on multiple ports at the same time.
2343 if (cmd == CTL_SET_PORT_WWNS) {
2344 printf("%s: Can't set WWNs on "
2345 "multiple ports\n", __func__);
2352 * XXX KDM we have to drop the lock here,
2353 * because the online/offline operations
2354 * can potentially block. We need to
2355 * reference count the frontends so they
2358 mtx_unlock(&softc->ctl_lock);
2360 if (cmd == CTL_ENABLE_PORT) {
2361 struct ctl_lun *lun;
2363 STAILQ_FOREACH(lun, &softc->lun_list,
2365 port->lun_enable(port->targ_lun_arg,
2370 ctl_port_online(port);
2371 } else if (cmd == CTL_DISABLE_PORT) {
2372 struct ctl_lun *lun;
2374 ctl_port_offline(port);
2376 STAILQ_FOREACH(lun, &softc->lun_list,
2385 mtx_lock(&softc->ctl_lock);
2387 if (cmd == CTL_SET_PORT_WWNS)
2388 ctl_port_set_wwns(port,
2389 (entry->flags & CTL_PORT_WWNN_VALID) ?
2391 (entry->flags & CTL_PORT_WWPN_VALID) ?
2392 1 : 0, entry->wwpn);
2397 mtx_unlock(&softc->ctl_lock);
2400 case CTL_GET_PORT_LIST: {
2401 struct ctl_port *port;
2402 struct ctl_port_list *list;
2405 list = (struct ctl_port_list *)addr;
2407 if (list->alloc_len != (list->alloc_num *
2408 sizeof(struct ctl_port_entry))) {
2409 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2410 "alloc_num %u * sizeof(struct ctl_port_entry) "
2411 "%zu\n", __func__, list->alloc_len,
2412 list->alloc_num, sizeof(struct ctl_port_entry));
2418 list->dropped_num = 0;
2420 mtx_lock(&softc->ctl_lock);
2421 STAILQ_FOREACH(port, &softc->port_list, links) {
2422 struct ctl_port_entry entry, *list_entry;
2424 if (list->fill_num >= list->alloc_num) {
2425 list->dropped_num++;
2429 entry.port_type = port->port_type;
2430 strlcpy(entry.port_name, port->port_name,
2431 sizeof(entry.port_name));
2432 entry.targ_port = port->targ_port;
2433 entry.physical_port = port->physical_port;
2434 entry.virtual_port = port->virtual_port;
2435 entry.wwnn = port->wwnn;
2436 entry.wwpn = port->wwpn;
2437 if (port->status & CTL_PORT_STATUS_ONLINE)
2442 list_entry = &list->entries[i];
2444 retval = copyout(&entry, list_entry, sizeof(entry));
2446 printf("%s: CTL_GET_PORT_LIST: copyout "
2447 "returned %d\n", __func__, retval);
2452 list->fill_len += sizeof(entry);
2454 mtx_unlock(&softc->ctl_lock);
2457 * If this is non-zero, we had a copyout fault, so there's
2458 * probably no point in attempting to set the status inside
2464 if (list->dropped_num > 0)
2465 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2467 list->status = CTL_PORT_LIST_OK;
2470 case CTL_DUMP_OOA: {
2471 struct ctl_lun *lun;
2476 mtx_lock(&softc->ctl_lock);
2477 printf("Dumping OOA queues:\n");
2478 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2479 mtx_lock(&lun->lun_lock);
2480 for (io = (union ctl_io *)TAILQ_FIRST(
2481 &lun->ooa_queue); io != NULL;
2482 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2484 sbuf_new(&sb, printbuf, sizeof(printbuf),
2486 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2490 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2492 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2494 CTL_FLAG_ABORT) ? " ABORT" : "",
2496 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2497 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2499 printf("%s\n", sbuf_data(&sb));
2501 mtx_unlock(&lun->lun_lock);
2503 printf("OOA queues dump done\n");
2504 mtx_unlock(&softc->ctl_lock);
2508 struct ctl_lun *lun;
2509 struct ctl_ooa *ooa_hdr;
2510 struct ctl_ooa_entry *entries;
2511 uint32_t cur_fill_num;
2513 ooa_hdr = (struct ctl_ooa *)addr;
2515 if ((ooa_hdr->alloc_len == 0)
2516 || (ooa_hdr->alloc_num == 0)) {
2517 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2518 "must be non-zero\n", __func__,
2519 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2524 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2525 sizeof(struct ctl_ooa_entry))) {
2526 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2527 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2528 __func__, ooa_hdr->alloc_len,
2529 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2534 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2535 if (entries == NULL) {
2536 printf("%s: could not allocate %d bytes for OOA "
2537 "dump\n", __func__, ooa_hdr->alloc_len);
2542 mtx_lock(&softc->ctl_lock);
2543 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2544 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2545 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2546 mtx_unlock(&softc->ctl_lock);
2547 free(entries, M_CTL);
2548 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2549 __func__, (uintmax_t)ooa_hdr->lun_num);
2556 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2557 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2558 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2564 mtx_unlock(&softc->ctl_lock);
2565 free(entries, M_CTL);
2569 lun = softc->ctl_luns[ooa_hdr->lun_num];
2571 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2574 mtx_unlock(&softc->ctl_lock);
2576 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2577 ooa_hdr->fill_len = ooa_hdr->fill_num *
2578 sizeof(struct ctl_ooa_entry);
2579 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2581 printf("%s: error copying out %d bytes for OOA dump\n",
2582 __func__, ooa_hdr->fill_len);
2585 getbintime(&ooa_hdr->cur_bt);
2587 if (cur_fill_num > ooa_hdr->alloc_num) {
2588 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2589 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2591 ooa_hdr->dropped_num = 0;
2592 ooa_hdr->status = CTL_OOA_OK;
2595 free(entries, M_CTL);
2598 case CTL_CHECK_OOA: {
2600 struct ctl_lun *lun;
2601 struct ctl_ooa_info *ooa_info;
2604 ooa_info = (struct ctl_ooa_info *)addr;
2606 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2607 ooa_info->status = CTL_OOA_INVALID_LUN;
2610 mtx_lock(&softc->ctl_lock);
2611 lun = softc->ctl_luns[ooa_info->lun_id];
2613 mtx_unlock(&softc->ctl_lock);
2614 ooa_info->status = CTL_OOA_INVALID_LUN;
2617 mtx_lock(&lun->lun_lock);
2618 mtx_unlock(&softc->ctl_lock);
2619 ooa_info->num_entries = 0;
2620 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2621 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2622 &io->io_hdr, ooa_links)) {
2623 ooa_info->num_entries++;
2625 mtx_unlock(&lun->lun_lock);
2627 ooa_info->status = CTL_OOA_SUCCESS;
2631 case CTL_HARD_START:
2632 case CTL_HARD_STOP: {
2633 struct ctl_fe_ioctl_startstop_info ss_info;
2634 struct cfi_metatask *metatask;
2637 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2639 cv_init(&ss_info.sem, "hard start/stop cv" );
2641 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2642 if (metatask == NULL) {
2644 mtx_destroy(&hs_mtx);
2648 if (cmd == CTL_HARD_START)
2649 metatask->tasktype = CFI_TASK_STARTUP;
2651 metatask->tasktype = CFI_TASK_SHUTDOWN;
2653 metatask->callback = ctl_ioctl_hard_startstop_callback;
2654 metatask->callback_arg = &ss_info;
2656 cfi_action(metatask);
2658 /* Wait for the callback */
2660 cv_wait_sig(&ss_info.sem, &hs_mtx);
2661 mtx_unlock(&hs_mtx);
2664 * All information has been copied from the metatask by the
2665 * time cv_broadcast() is called, so we free the metatask here.
2667 cfi_free_metatask(metatask);
2669 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2671 mtx_destroy(&hs_mtx);
2675 struct ctl_bbrread_info *bbr_info;
2676 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2678 struct cfi_metatask *metatask;
2680 bbr_info = (struct ctl_bbrread_info *)addr;
2682 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2684 bzero(&bbr_mtx, sizeof(bbr_mtx));
2685 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2687 fe_bbr_info.bbr_info = bbr_info;
2688 fe_bbr_info.lock = &bbr_mtx;
2690 cv_init(&fe_bbr_info.sem, "BBR read cv");
2691 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2693 if (metatask == NULL) {
2694 mtx_destroy(&bbr_mtx);
2695 cv_destroy(&fe_bbr_info.sem);
2699 metatask->tasktype = CFI_TASK_BBRREAD;
2700 metatask->callback = ctl_ioctl_bbrread_callback;
2701 metatask->callback_arg = &fe_bbr_info;
2702 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2703 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2704 metatask->taskinfo.bbrread.len = bbr_info->len;
2706 cfi_action(metatask);
2709 while (fe_bbr_info.wakeup_done == 0)
2710 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2711 mtx_unlock(&bbr_mtx);
2713 bbr_info->status = metatask->status;
2714 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2715 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2716 memcpy(&bbr_info->sense_data,
2717 &metatask->taskinfo.bbrread.sense_data,
2718 ctl_min(sizeof(bbr_info->sense_data),
2719 sizeof(metatask->taskinfo.bbrread.sense_data)));
2721 cfi_free_metatask(metatask);
2723 mtx_destroy(&bbr_mtx);
2724 cv_destroy(&fe_bbr_info.sem);
2728 case CTL_DELAY_IO: {
2729 struct ctl_io_delay_info *delay_info;
2731 struct ctl_lun *lun;
2732 #endif /* CTL_IO_DELAY */
2734 delay_info = (struct ctl_io_delay_info *)addr;
2737 mtx_lock(&softc->ctl_lock);
2739 if ((delay_info->lun_id > CTL_MAX_LUNS)
2740 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2741 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2743 lun = softc->ctl_luns[delay_info->lun_id];
2744 mtx_lock(&lun->lun_lock);
2746 delay_info->status = CTL_DELAY_STATUS_OK;
2748 switch (delay_info->delay_type) {
2749 case CTL_DELAY_TYPE_CONT:
2751 case CTL_DELAY_TYPE_ONESHOT:
2754 delay_info->status =
2755 CTL_DELAY_STATUS_INVALID_TYPE;
2759 switch (delay_info->delay_loc) {
2760 case CTL_DELAY_LOC_DATAMOVE:
2761 lun->delay_info.datamove_type =
2762 delay_info->delay_type;
2763 lun->delay_info.datamove_delay =
2764 delay_info->delay_secs;
2766 case CTL_DELAY_LOC_DONE:
2767 lun->delay_info.done_type =
2768 delay_info->delay_type;
2769 lun->delay_info.done_delay =
2770 delay_info->delay_secs;
2773 delay_info->status =
2774 CTL_DELAY_STATUS_INVALID_LOC;
2777 mtx_unlock(&lun->lun_lock);
2780 mtx_unlock(&softc->ctl_lock);
2782 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2783 #endif /* CTL_IO_DELAY */
2786 case CTL_REALSYNC_SET: {
2789 syncstate = (int *)addr;
2791 mtx_lock(&softc->ctl_lock);
2792 switch (*syncstate) {
2794 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2797 softc->flags |= CTL_FLAG_REAL_SYNC;
2803 mtx_unlock(&softc->ctl_lock);
2806 case CTL_REALSYNC_GET: {
2809 syncstate = (int*)addr;
2811 mtx_lock(&softc->ctl_lock);
2812 if (softc->flags & CTL_FLAG_REAL_SYNC)
2816 mtx_unlock(&softc->ctl_lock);
2822 struct ctl_sync_info *sync_info;
2823 struct ctl_lun *lun;
2825 sync_info = (struct ctl_sync_info *)addr;
2827 mtx_lock(&softc->ctl_lock);
2828 lun = softc->ctl_luns[sync_info->lun_id];
2830 mtx_unlock(&softc->ctl_lock);
2831 sync_info->status = CTL_GS_SYNC_NO_LUN;
2834 * Get or set the sync interval. We're not bounds checking
2835 * in the set case, hopefully the user won't do something
2838 mtx_lock(&lun->lun_lock);
2839 mtx_unlock(&softc->ctl_lock);
2840 if (cmd == CTL_GETSYNC)
2841 sync_info->sync_interval = lun->sync_interval;
2843 lun->sync_interval = sync_info->sync_interval;
2844 mtx_unlock(&lun->lun_lock);
2846 sync_info->status = CTL_GS_SYNC_OK;
2850 case CTL_GETSTATS: {
2851 struct ctl_stats *stats;
2852 struct ctl_lun *lun;
2855 stats = (struct ctl_stats *)addr;
2857 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2859 stats->status = CTL_SS_NEED_MORE_SPACE;
2860 stats->num_luns = softc->num_luns;
2864 * XXX KDM no locking here. If the LUN list changes,
2865 * things can blow up.
2867 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2868 i++, lun = STAILQ_NEXT(lun, links)) {
2869 retval = copyout(&lun->stats, &stats->lun_stats[i],
2870 sizeof(lun->stats));
2874 stats->num_luns = softc->num_luns;
2875 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2877 stats->status = CTL_SS_OK;
2879 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2881 stats->flags = CTL_STATS_FLAG_NONE;
2883 getnanouptime(&stats->timestamp);
2886 case CTL_ERROR_INJECT: {
2887 struct ctl_error_desc *err_desc, *new_err_desc;
2888 struct ctl_lun *lun;
2890 err_desc = (struct ctl_error_desc *)addr;
2892 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2894 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2896 mtx_lock(&softc->ctl_lock);
2897 lun = softc->ctl_luns[err_desc->lun_id];
2899 mtx_unlock(&softc->ctl_lock);
2900 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2901 __func__, (uintmax_t)err_desc->lun_id);
2905 mtx_lock(&lun->lun_lock);
2906 mtx_unlock(&softc->ctl_lock);
2909 * We could do some checking here to verify the validity
2910 * of the request, but given the complexity of error
2911 * injection requests, the checking logic would be fairly
2914 * For now, if the request is invalid, it just won't get
2915 * executed and might get deleted.
2917 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2920 * XXX KDM check to make sure the serial number is unique,
2921 * in case we somehow manage to wrap. That shouldn't
2922 * happen for a very long time, but it's the right thing to
2925 new_err_desc->serial = lun->error_serial;
2926 err_desc->serial = lun->error_serial;
2927 lun->error_serial++;
2929 mtx_unlock(&lun->lun_lock);
2932 case CTL_ERROR_INJECT_DELETE: {
2933 struct ctl_error_desc *delete_desc, *desc, *desc2;
2934 struct ctl_lun *lun;
2937 delete_desc = (struct ctl_error_desc *)addr;
2940 mtx_lock(&softc->ctl_lock);
2941 lun = softc->ctl_luns[delete_desc->lun_id];
2943 mtx_unlock(&softc->ctl_lock);
2944 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2945 __func__, (uintmax_t)delete_desc->lun_id);
2949 mtx_lock(&lun->lun_lock);
2950 mtx_unlock(&softc->ctl_lock);
2951 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2952 if (desc->serial != delete_desc->serial)
2955 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2960 mtx_unlock(&lun->lun_lock);
2961 if (delete_done == 0) {
2962 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2963 "error serial %ju on LUN %u\n", __func__,
2964 delete_desc->serial, delete_desc->lun_id);
2970 case CTL_DUMP_STRUCTS: {
2972 struct ctl_port *port;
2973 struct ctl_frontend *fe;
2975 mtx_lock(&softc->ctl_lock);
2976 printf("CTL Persistent Reservation information start:\n");
2977 for (i = 0; i < CTL_MAX_LUNS; i++) {
2978 struct ctl_lun *lun;
2980 lun = softc->ctl_luns[i];
2983 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2986 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2987 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2988 idx = j * CTL_MAX_INIT_PER_PORT + k;
2989 if (lun->per_res[idx].registered == 0)
2991 printf(" LUN %d port %d iid %d key "
2993 (uintmax_t)scsi_8btou64(
2994 lun->per_res[idx].res_key.key));
2998 printf("CTL Persistent Reservation information end\n");
2999 printf("CTL Ports:\n");
3000 STAILQ_FOREACH(port, &softc->port_list, links) {
3001 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3002 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3003 port->frontend->name, port->port_type,
3004 port->physical_port, port->virtual_port,
3005 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3006 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3007 if (port->wwpn_iid[j].in_use == 0 &&
3008 port->wwpn_iid[j].wwpn == 0 &&
3009 port->wwpn_iid[j].name == NULL)
3012 printf(" iid %u use %d WWPN %#jx '%s'\n",
3013 j, port->wwpn_iid[j].in_use,
3014 (uintmax_t)port->wwpn_iid[j].wwpn,
3015 port->wwpn_iid[j].name);
3018 printf("CTL Port information end\n");
3019 mtx_unlock(&softc->ctl_lock);
3021 * XXX KDM calling this without a lock. We'd likely want
3022 * to drop the lock before calling the frontend's dump
3025 printf("CTL Frontends:\n");
3026 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3027 printf(" Frontend '%s'\n", fe->name);
3028 if (fe->fe_dump != NULL)
3031 printf("CTL Frontend information end\n");
3035 struct ctl_lun_req *lun_req;
3036 struct ctl_backend_driver *backend;
3038 lun_req = (struct ctl_lun_req *)addr;
3040 backend = ctl_backend_find(lun_req->backend);
3041 if (backend == NULL) {
3042 lun_req->status = CTL_LUN_ERROR;
3043 snprintf(lun_req->error_str,
3044 sizeof(lun_req->error_str),
3045 "Backend \"%s\" not found.",
3049 if (lun_req->num_be_args > 0) {
3050 lun_req->kern_be_args = ctl_copyin_args(
3051 lun_req->num_be_args,
3054 sizeof(lun_req->error_str));
3055 if (lun_req->kern_be_args == NULL) {
3056 lun_req->status = CTL_LUN_ERROR;
3061 retval = backend->ioctl(dev, cmd, addr, flag, td);
3063 if (lun_req->num_be_args > 0) {
3064 ctl_copyout_args(lun_req->num_be_args,
3065 lun_req->kern_be_args);
3066 ctl_free_args(lun_req->num_be_args,
3067 lun_req->kern_be_args);
3071 case CTL_LUN_LIST: {
3073 struct ctl_lun *lun;
3074 struct ctl_lun_list *list;
3075 struct ctl_option *opt;
3077 list = (struct ctl_lun_list *)addr;
3080 * Allocate a fixed length sbuf here, based on the length
3081 * of the user's buffer. We could allocate an auto-extending
3082 * buffer, and then tell the user how much larger our
3083 * amount of data is than his buffer, but that presents
3086 * 1. The sbuf(9) routines use a blocking malloc, and so
3087 * we can't hold a lock while calling them with an
3088 * auto-extending buffer.
3090 * 2. There is not currently a LUN reference counting
3091 * mechanism, outside of outstanding transactions on
3092 * the LUN's OOA queue. So a LUN could go away on us
3093 * while we're getting the LUN number, backend-specific
3094 * information, etc. Thus, given the way things
3095 * currently work, we need to hold the CTL lock while
3096 * grabbing LUN information.
3098 * So, from the user's standpoint, the best thing to do is
3099 * allocate what he thinks is a reasonable buffer length,
3100 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3101 * double the buffer length and try again. (And repeat
3102 * that until he succeeds.)
3104 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3106 list->status = CTL_LUN_LIST_ERROR;
3107 snprintf(list->error_str, sizeof(list->error_str),
3108 "Unable to allocate %d bytes for LUN list",
3113 sbuf_printf(sb, "<ctllunlist>\n");
3115 mtx_lock(&softc->ctl_lock);
3116 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3117 mtx_lock(&lun->lun_lock);
3118 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3119 (uintmax_t)lun->lun);
3122 * Bail out as soon as we see that we've overfilled
3128 retval = sbuf_printf(sb, "\t<backend_type>%s"
3129 "</backend_type>\n",
3130 (lun->backend == NULL) ? "none" :
3131 lun->backend->name);
3136 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3137 lun->be_lun->lun_type);
3142 if (lun->backend == NULL) {
3143 retval = sbuf_printf(sb, "</lun>\n");
3149 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3150 (lun->be_lun->maxlba > 0) ?
3151 lun->be_lun->maxlba + 1 : 0);
3156 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3157 lun->be_lun->blocksize);
3162 retval = sbuf_printf(sb, "\t<serial_number>");
3167 retval = ctl_sbuf_printf_esc(sb,
3168 lun->be_lun->serial_num);
3173 retval = sbuf_printf(sb, "</serial_number>\n");
3178 retval = sbuf_printf(sb, "\t<device_id>");
3183 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3188 retval = sbuf_printf(sb, "</device_id>\n");
3193 if (lun->backend->lun_info != NULL) {
3194 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3198 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3199 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3200 opt->name, opt->value, opt->name);
3205 retval = sbuf_printf(sb, "</lun>\n");
3209 mtx_unlock(&lun->lun_lock);
3212 mtx_unlock(&lun->lun_lock);
3213 mtx_unlock(&softc->ctl_lock);
3216 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3219 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3220 snprintf(list->error_str, sizeof(list->error_str),
3221 "Out of space, %d bytes is too small",
3228 retval = copyout(sbuf_data(sb), list->lun_xml,
3231 list->fill_len = sbuf_len(sb) + 1;
3232 list->status = CTL_LUN_LIST_OK;
3237 struct ctl_iscsi *ci;
3238 struct ctl_frontend *fe;
3240 ci = (struct ctl_iscsi *)addr;
3242 fe = ctl_frontend_find("iscsi");
3244 ci->status = CTL_ISCSI_ERROR;
3245 snprintf(ci->error_str, sizeof(ci->error_str),
3246 "Frontend \"iscsi\" not found.");
3250 retval = fe->ioctl(dev, cmd, addr, flag, td);
3253 case CTL_PORT_REQ: {
3254 struct ctl_req *req;
3255 struct ctl_frontend *fe;
3257 req = (struct ctl_req *)addr;
3259 fe = ctl_frontend_find(req->driver);
3261 req->status = CTL_LUN_ERROR;
3262 snprintf(req->error_str, sizeof(req->error_str),
3263 "Frontend \"%s\" not found.", req->driver);
3266 if (req->num_args > 0) {
3267 req->kern_args = ctl_copyin_args(req->num_args,
3268 req->args, req->error_str, sizeof(req->error_str));
3269 if (req->kern_args == NULL) {
3270 req->status = CTL_LUN_ERROR;
3275 retval = fe->ioctl(dev, cmd, addr, flag, td);
3277 if (req->num_args > 0) {
3278 ctl_copyout_args(req->num_args, req->kern_args);
3279 ctl_free_args(req->num_args, req->kern_args);
3283 case CTL_PORT_LIST: {
3285 struct ctl_port *port;
3286 struct ctl_lun_list *list;
3287 struct ctl_option *opt;
3289 list = (struct ctl_lun_list *)addr;
3291 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3293 list->status = CTL_LUN_LIST_ERROR;
3294 snprintf(list->error_str, sizeof(list->error_str),
3295 "Unable to allocate %d bytes for LUN list",
3300 sbuf_printf(sb, "<ctlportlist>\n");
3302 mtx_lock(&softc->ctl_lock);
3303 STAILQ_FOREACH(port, &softc->port_list, links) {
3304 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3305 (uintmax_t)port->targ_port);
3308 * Bail out as soon as we see that we've overfilled
3314 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3315 "</frontend_type>\n", port->frontend->name);
3319 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3324 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3325 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3329 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3334 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3335 port->physical_port);
3339 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3340 port->virtual_port);
3344 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3345 (uintmax_t)port->wwnn);
3349 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3350 (uintmax_t)port->wwpn);
3354 if (port->port_info != NULL) {
3355 retval = port->port_info(port->onoff_arg, sb);
3359 STAILQ_FOREACH(opt, &port->options, links) {
3360 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3361 opt->name, opt->value, opt->name);
3366 retval = sbuf_printf(sb, "</targ_port>\n");
3370 mtx_unlock(&softc->ctl_lock);
3373 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3376 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3377 snprintf(list->error_str, sizeof(list->error_str),
3378 "Out of space, %d bytes is too small",
3385 retval = copyout(sbuf_data(sb), list->lun_xml,
3388 list->fill_len = sbuf_len(sb) + 1;
3389 list->status = CTL_LUN_LIST_OK;
3394 /* XXX KDM should we fix this? */
3396 struct ctl_backend_driver *backend;
3403 * We encode the backend type as the ioctl type for backend
3404 * ioctls. So parse it out here, and then search for a
3405 * backend of this type.
3407 type = _IOC_TYPE(cmd);
3409 STAILQ_FOREACH(backend, &softc->be_list, links) {
3410 if (backend->type == type) {
3416 printf("ctl: unknown ioctl command %#lx or backend "
3421 retval = backend->ioctl(dev, cmd, addr, flag, td);
3431 ctl_get_initindex(struct ctl_nexus *nexus)
3433 if (nexus->targ_port < CTL_MAX_PORTS)
3434 return (nexus->initid.id +
3435 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3437 return (nexus->initid.id +
3438 ((nexus->targ_port - CTL_MAX_PORTS) *
3439 CTL_MAX_INIT_PER_PORT));
3443 ctl_get_resindex(struct ctl_nexus *nexus)
3445 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3449 ctl_port_idx(int port_num)
3451 if (port_num < CTL_MAX_PORTS)
3454 return(port_num - CTL_MAX_PORTS);
3458 ctl_map_lun(int port_num, uint32_t lun_id)
3460 struct ctl_port *port;
3462 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3464 return (UINT32_MAX);
3465 if (port->lun_map == NULL)
3467 return (port->lun_map(port->targ_lun_arg, lun_id));
3471 ctl_map_lun_back(int port_num, uint32_t lun_id)
3473 struct ctl_port *port;
3476 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3477 if (port->lun_map == NULL)
3479 for (i = 0; i < CTL_MAX_LUNS; i++) {
3480 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3483 return (UINT32_MAX);
3487 * Note: This only works for bitmask sizes that are at least 32 bits, and
3488 * that are a power of 2.
3491 ctl_ffz(uint32_t *mask, uint32_t size)
3493 uint32_t num_chunks, num_pieces;
3496 num_chunks = (size >> 5);
3497 if (num_chunks == 0)
3499 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3501 for (i = 0; i < num_chunks; i++) {
3502 for (j = 0; j < num_pieces; j++) {
3503 if ((mask[i] & (1 << j)) == 0)
3504 return ((i << 5) + j);
3512 ctl_set_mask(uint32_t *mask, uint32_t bit)
3514 uint32_t chunk, piece;
3517 piece = bit % (sizeof(uint32_t) * 8);
3519 if ((mask[chunk] & (1 << piece)) != 0)
3522 mask[chunk] |= (1 << piece);
3528 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3530 uint32_t chunk, piece;
3533 piece = bit % (sizeof(uint32_t) * 8);
3535 if ((mask[chunk] & (1 << piece)) == 0)
3538 mask[chunk] &= ~(1 << piece);
3544 ctl_is_set(uint32_t *mask, uint32_t bit)
3546 uint32_t chunk, piece;
3549 piece = bit % (sizeof(uint32_t) * 8);
3551 if ((mask[chunk] & (1 << piece)) == 0)
3559 * The bus, target and lun are optional, they can be filled in later.
3560 * can_wait is used to determine whether we can wait on the malloc or not.
3563 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3564 uint32_t targ_lun, int can_wait)
3569 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3571 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3574 io->io_hdr.io_type = io_type;
3575 io->io_hdr.targ_port = targ_port;
3577 * XXX KDM this needs to change/go away. We need to move
3578 * to a preallocated pool of ctl_scsiio structures.
3580 io->io_hdr.nexus.targ_target.id = targ_target;
3581 io->io_hdr.nexus.targ_lun = targ_lun;
3588 ctl_kfree_io(union ctl_io *io)
3595 * ctl_softc, pool_type, total_ctl_io are passed in.
3596 * npool is passed out.
3599 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3600 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3603 union ctl_io *cur_io, *next_io;
3604 struct ctl_io_pool *pool;
3609 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3616 pool->type = pool_type;
3617 pool->ctl_softc = ctl_softc;
3619 mtx_lock(&ctl_softc->pool_lock);
3620 pool->id = ctl_softc->cur_pool_id++;
3621 mtx_unlock(&ctl_softc->pool_lock);
3623 pool->flags = CTL_POOL_FLAG_NONE;
3624 pool->refcount = 1; /* Reference for validity. */
3625 STAILQ_INIT(&pool->free_queue);
3628 * XXX KDM other options here:
3629 * - allocate a page at a time
3630 * - allocate one big chunk of memory.
3631 * Page allocation might work well, but would take a little more
3634 for (i = 0; i < total_ctl_io; i++) {
3635 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3637 if (cur_io == NULL) {
3641 cur_io->io_hdr.pool = pool;
3642 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3643 pool->total_ctl_io++;
3644 pool->free_ctl_io++;
3648 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3649 cur_io != NULL; cur_io = next_io) {
3650 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3652 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3654 free(cur_io, M_CTLIO);
3660 mtx_lock(&ctl_softc->pool_lock);
3661 ctl_softc->num_pools++;
3662 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3664 * Increment our usage count if this is an external consumer, so we
3665 * can't get unloaded until the external consumer (most likely a
3666 * FETD) unloads and frees his pool.
3668 * XXX KDM will this increment the caller's module use count, or
3672 if ((pool_type != CTL_POOL_EMERGENCY)
3673 && (pool_type != CTL_POOL_INTERNAL)
3674 && (pool_type != CTL_POOL_4OTHERSC))
3678 mtx_unlock(&ctl_softc->pool_lock);
3688 ctl_pool_acquire(struct ctl_io_pool *pool)
3691 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3693 if (pool->flags & CTL_POOL_FLAG_INVALID)
3702 ctl_pool_release(struct ctl_io_pool *pool)
3704 struct ctl_softc *ctl_softc = pool->ctl_softc;
3707 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3709 if (--pool->refcount != 0)
3712 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3713 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3718 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3719 ctl_softc->num_pools--;
3722 * XXX KDM will this decrement the caller's usage count or mine?
3725 if ((pool->type != CTL_POOL_EMERGENCY)
3726 && (pool->type != CTL_POOL_INTERNAL)
3727 && (pool->type != CTL_POOL_4OTHERSC))
3735 ctl_pool_free(struct ctl_io_pool *pool)
3737 struct ctl_softc *ctl_softc;
3742 ctl_softc = pool->ctl_softc;
3743 mtx_lock(&ctl_softc->pool_lock);
3744 pool->flags |= CTL_POOL_FLAG_INVALID;
3745 ctl_pool_release(pool);
3746 mtx_unlock(&ctl_softc->pool_lock);
3750 * This routine does not block (except for spinlocks of course).
3751 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3755 ctl_alloc_io(void *pool_ref)
3758 struct ctl_softc *ctl_softc;
3759 struct ctl_io_pool *pool, *npool;
3760 struct ctl_io_pool *emergency_pool;
3762 pool = (struct ctl_io_pool *)pool_ref;
3765 printf("%s: pool is NULL\n", __func__);
3769 emergency_pool = NULL;
3771 ctl_softc = pool->ctl_softc;
3773 mtx_lock(&ctl_softc->pool_lock);
3775 * First, try to get the io structure from the user's pool.
3777 if (ctl_pool_acquire(pool) == 0) {
3778 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3780 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3781 pool->total_allocated++;
3782 pool->free_ctl_io--;
3783 mtx_unlock(&ctl_softc->pool_lock);
3786 ctl_pool_release(pool);
3789 * If he doesn't have any io structures left, search for an
3790 * emergency pool and grab one from there.
3792 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3793 if (npool->type != CTL_POOL_EMERGENCY)
3796 if (ctl_pool_acquire(npool) != 0)
3799 emergency_pool = npool;
3801 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3803 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3804 npool->total_allocated++;
3805 npool->free_ctl_io--;
3806 mtx_unlock(&ctl_softc->pool_lock);
3809 ctl_pool_release(npool);
3812 /* Drop the spinlock before we malloc */
3813 mtx_unlock(&ctl_softc->pool_lock);
3816 * The emergency pool (if it exists) didn't have one, so try an
3817 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3819 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3822 * If the emergency pool exists but is empty, add this
3823 * ctl_io to its list when it gets freed.
3825 if (emergency_pool != NULL) {
3826 mtx_lock(&ctl_softc->pool_lock);
3827 if (ctl_pool_acquire(emergency_pool) == 0) {
3828 io->io_hdr.pool = emergency_pool;
3829 emergency_pool->total_ctl_io++;
3831 * Need to bump this, otherwise
3832 * total_allocated and total_freed won't
3833 * match when we no longer have anything
3836 emergency_pool->total_allocated++;
3838 mtx_unlock(&ctl_softc->pool_lock);
3840 io->io_hdr.pool = NULL;
3847 ctl_free_io(union ctl_io *io)
3853 * If this ctl_io has a pool, return it to that pool.
3855 if (io->io_hdr.pool != NULL) {
3856 struct ctl_io_pool *pool;
3858 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3859 mtx_lock(&pool->ctl_softc->pool_lock);
3860 io->io_hdr.io_type = 0xff;
3861 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3862 pool->total_freed++;
3863 pool->free_ctl_io++;
3864 ctl_pool_release(pool);
3865 mtx_unlock(&pool->ctl_softc->pool_lock);
3868 * Otherwise, just free it. We probably malloced it and
3869 * the emergency pool wasn't available.
3877 ctl_zero_io(union ctl_io *io)
3885 * May need to preserve linked list pointers at some point too.
3887 pool_ref = io->io_hdr.pool;
3889 memset(io, 0, sizeof(*io));
3891 io->io_hdr.pool = pool_ref;
3895 * This routine is currently used for internal copies of ctl_ios that need
3896 * to persist for some reason after we've already returned status to the
3897 * FETD. (Thus the flag set.)
3900 * Note that this makes a blind copy of all fields in the ctl_io, except
3901 * for the pool reference. This includes any memory that has been
3902 * allocated! That memory will no longer be valid after done has been
3903 * called, so this would be VERY DANGEROUS for command that actually does
3904 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3905 * start and stop commands, which don't transfer any data, so this is not a
3906 * problem. If it is used for anything else, the caller would also need to
3907 * allocate data buffer space and this routine would need to be modified to
3908 * copy the data buffer(s) as well.
3911 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3920 * May need to preserve linked list pointers at some point too.
3922 pool_ref = dest->io_hdr.pool;
3924 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3926 dest->io_hdr.pool = pool_ref;
3928 * We need to know that this is an internal copy, and doesn't need
3929 * to get passed back to the FETD that allocated it.
3931 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3936 ctl_update_power_subpage(struct copan_power_subpage *page)
3938 int num_luns, num_partitions, config_type;
3939 struct ctl_softc *softc;
3940 cs_BOOL_t aor_present, shelf_50pct_power;
3941 cs_raidset_personality_t rs_type;
3942 int max_active_luns;
3944 softc = control_softc;
3946 /* subtract out the processor LUN */
3947 num_luns = softc->num_luns - 1;
3949 * Default to 7 LUNs active, which was the only number we allowed
3952 max_active_luns = 7;
3954 num_partitions = config_GetRsPartitionInfo();
3955 config_type = config_GetConfigType();
3956 shelf_50pct_power = config_GetShelfPowerMode();
3957 aor_present = config_IsAorRsPresent();
3959 rs_type = ddb_GetRsRaidType(1);
3960 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3961 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3962 EPRINT(0, "Unsupported RS type %d!", rs_type);
3966 page->total_luns = num_luns;
3968 switch (config_type) {
3971 * In a 40 drive configuration, it doesn't matter what DC
3972 * cards we have, whether we have AOR enabled or not,
3973 * partitioning or not, or what type of RAIDset we have.
3974 * In that scenario, we can power up every LUN we present
3977 max_active_luns = num_luns;
3981 if (shelf_50pct_power == CS_FALSE) {
3983 if (aor_present == CS_TRUE) {
3985 CS_RAIDSET_PERSONALITY_RAID5) {
3986 max_active_luns = 7;
3987 } else if (rs_type ==
3988 CS_RAIDSET_PERSONALITY_RAID1){
3989 max_active_luns = 14;
3991 /* XXX KDM now what?? */
3995 CS_RAIDSET_PERSONALITY_RAID5) {
3996 max_active_luns = 8;
3997 } else if (rs_type ==
3998 CS_RAIDSET_PERSONALITY_RAID1){
3999 max_active_luns = 16;
4001 /* XXX KDM now what?? */
4007 * With 50% power in a 64 drive configuration, we
4008 * can power all LUNs we present.
4010 max_active_luns = num_luns;
4014 if (shelf_50pct_power == CS_FALSE) {
4016 if (aor_present == CS_TRUE) {
4018 CS_RAIDSET_PERSONALITY_RAID5) {
4019 max_active_luns = 7;
4020 } else if (rs_type ==
4021 CS_RAIDSET_PERSONALITY_RAID1){
4022 max_active_luns = 14;
4024 /* XXX KDM now what?? */
4028 CS_RAIDSET_PERSONALITY_RAID5) {
4029 max_active_luns = 8;
4030 } else if (rs_type ==
4031 CS_RAIDSET_PERSONALITY_RAID1){
4032 max_active_luns = 16;
4034 /* XXX KDM now what?? */
4039 if (aor_present == CS_TRUE) {
4041 CS_RAIDSET_PERSONALITY_RAID5) {
4042 max_active_luns = 14;
4043 } else if (rs_type ==
4044 CS_RAIDSET_PERSONALITY_RAID1){
4046 * We're assuming here that disk
4047 * caching is enabled, and so we're
4048 * able to power up half of each
4049 * LUN, and cache all writes.
4051 max_active_luns = num_luns;
4053 /* XXX KDM now what?? */
4057 CS_RAIDSET_PERSONALITY_RAID5) {
4058 max_active_luns = 15;
4059 } else if (rs_type ==
4060 CS_RAIDSET_PERSONALITY_RAID1){
4061 max_active_luns = 30;
4063 /* XXX KDM now what?? */
4070 * In this case, we have an unknown configuration, so we
4071 * just use the default from above.
4076 page->max_active_luns = max_active_luns;
4078 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
4079 page->total_luns, page->max_active_luns);
4082 #endif /* NEEDTOPORT */
4085 * This routine could be used in the future to load default and/or saved
4086 * mode page parameters for a particuar lun.
4089 ctl_init_page_index(struct ctl_lun *lun)
4092 struct ctl_page_index *page_index;
4093 struct ctl_softc *softc;
4095 memcpy(&lun->mode_pages.index, page_index_template,
4096 sizeof(page_index_template));
4098 softc = lun->ctl_softc;
4100 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4102 page_index = &lun->mode_pages.index[i];
4104 * If this is a disk-only mode page, there's no point in
4105 * setting it up. For some pages, we have to have some
4106 * basic information about the disk in order to calculate the
4109 if ((lun->be_lun->lun_type != T_DIRECT)
4110 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4113 switch (page_index->page_code & SMPH_PC_MASK) {
4114 case SMS_FORMAT_DEVICE_PAGE: {
4115 struct scsi_format_page *format_page;
4117 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4118 panic("subpage is incorrect!");
4121 * Sectors per track are set above. Bytes per
4122 * sector need to be set here on a per-LUN basis.
4124 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4125 &format_page_default,
4126 sizeof(format_page_default));
4127 memcpy(&lun->mode_pages.format_page[
4128 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4129 sizeof(format_page_changeable));
4130 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4131 &format_page_default,
4132 sizeof(format_page_default));
4133 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4134 &format_page_default,
4135 sizeof(format_page_default));
4137 format_page = &lun->mode_pages.format_page[
4139 scsi_ulto2b(lun->be_lun->blocksize,
4140 format_page->bytes_per_sector);
4142 format_page = &lun->mode_pages.format_page[
4144 scsi_ulto2b(lun->be_lun->blocksize,
4145 format_page->bytes_per_sector);
4147 format_page = &lun->mode_pages.format_page[
4149 scsi_ulto2b(lun->be_lun->blocksize,
4150 format_page->bytes_per_sector);
4152 page_index->page_data =
4153 (uint8_t *)lun->mode_pages.format_page;
4156 case SMS_RIGID_DISK_PAGE: {
4157 struct scsi_rigid_disk_page *rigid_disk_page;
4158 uint32_t sectors_per_cylinder;
4162 #endif /* !__XSCALE__ */
4164 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4165 panic("invalid subpage value %d",
4166 page_index->subpage);
4169 * Rotation rate and sectors per track are set
4170 * above. We calculate the cylinders here based on
4171 * capacity. Due to the number of heads and
4172 * sectors per track we're using, smaller arrays
4173 * may turn out to have 0 cylinders. Linux and
4174 * FreeBSD don't pay attention to these mode pages
4175 * to figure out capacity, but Solaris does. It
4176 * seems to deal with 0 cylinders just fine, and
4177 * works out a fake geometry based on the capacity.
4179 memcpy(&lun->mode_pages.rigid_disk_page[
4180 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4181 sizeof(rigid_disk_page_default));
4182 memcpy(&lun->mode_pages.rigid_disk_page[
4183 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4184 sizeof(rigid_disk_page_changeable));
4185 memcpy(&lun->mode_pages.rigid_disk_page[
4186 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4187 sizeof(rigid_disk_page_default));
4188 memcpy(&lun->mode_pages.rigid_disk_page[
4189 CTL_PAGE_SAVED], &rigid_disk_page_default,
4190 sizeof(rigid_disk_page_default));
4192 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4196 * The divide method here will be more accurate,
4197 * probably, but results in floating point being
4198 * used in the kernel on i386 (__udivdi3()). On the
4199 * XScale, though, __udivdi3() is implemented in
4202 * The shift method for cylinder calculation is
4203 * accurate if sectors_per_cylinder is a power of
4204 * 2. Otherwise it might be slightly off -- you
4205 * might have a bit of a truncation problem.
4208 cylinders = (lun->be_lun->maxlba + 1) /
4209 sectors_per_cylinder;
4211 for (shift = 31; shift > 0; shift--) {
4212 if (sectors_per_cylinder & (1 << shift))
4215 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4219 * We've basically got 3 bytes, or 24 bits for the
4220 * cylinder size in the mode page. If we're over,
4221 * just round down to 2^24.
4223 if (cylinders > 0xffffff)
4224 cylinders = 0xffffff;
4226 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4228 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4230 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4232 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4234 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4236 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4238 page_index->page_data =
4239 (uint8_t *)lun->mode_pages.rigid_disk_page;
4242 case SMS_CACHING_PAGE: {
4244 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4245 panic("invalid subpage value %d",
4246 page_index->subpage);
4248 * Defaults should be okay here, no calculations
4251 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4252 &caching_page_default,
4253 sizeof(caching_page_default));
4254 memcpy(&lun->mode_pages.caching_page[
4255 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4256 sizeof(caching_page_changeable));
4257 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4258 &caching_page_default,
4259 sizeof(caching_page_default));
4260 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4261 &caching_page_default,
4262 sizeof(caching_page_default));
4263 page_index->page_data =
4264 (uint8_t *)lun->mode_pages.caching_page;
4267 case SMS_CONTROL_MODE_PAGE: {
4269 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4270 panic("invalid subpage value %d",
4271 page_index->subpage);
4274 * Defaults should be okay here, no calculations
4277 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4278 &control_page_default,
4279 sizeof(control_page_default));
4280 memcpy(&lun->mode_pages.control_page[
4281 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4282 sizeof(control_page_changeable));
4283 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4284 &control_page_default,
4285 sizeof(control_page_default));
4286 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4287 &control_page_default,
4288 sizeof(control_page_default));
4289 page_index->page_data =
4290 (uint8_t *)lun->mode_pages.control_page;
4294 case SMS_VENDOR_SPECIFIC_PAGE:{
4295 switch (page_index->subpage) {
4296 case PWR_SUBPAGE_CODE: {
4297 struct copan_power_subpage *current_page,
4300 memcpy(&lun->mode_pages.power_subpage[
4302 &power_page_default,
4303 sizeof(power_page_default));
4304 memcpy(&lun->mode_pages.power_subpage[
4305 CTL_PAGE_CHANGEABLE],
4306 &power_page_changeable,
4307 sizeof(power_page_changeable));
4308 memcpy(&lun->mode_pages.power_subpage[
4310 &power_page_default,
4311 sizeof(power_page_default));
4312 memcpy(&lun->mode_pages.power_subpage[
4314 &power_page_default,
4315 sizeof(power_page_default));
4316 page_index->page_data =
4317 (uint8_t *)lun->mode_pages.power_subpage;
4319 current_page = (struct copan_power_subpage *)
4320 (page_index->page_data +
4321 (page_index->page_len *
4323 saved_page = (struct copan_power_subpage *)
4324 (page_index->page_data +
4325 (page_index->page_len *
4329 case APS_SUBPAGE_CODE: {
4330 struct copan_aps_subpage *current_page,
4333 // This gets set multiple times but
4334 // it should always be the same. It's
4335 // only done during init so who cares.
4336 index_to_aps_page = i;
4338 memcpy(&lun->mode_pages.aps_subpage[
4341 sizeof(aps_page_default));
4342 memcpy(&lun->mode_pages.aps_subpage[
4343 CTL_PAGE_CHANGEABLE],
4344 &aps_page_changeable,
4345 sizeof(aps_page_changeable));
4346 memcpy(&lun->mode_pages.aps_subpage[
4349 sizeof(aps_page_default));
4350 memcpy(&lun->mode_pages.aps_subpage[
4353 sizeof(aps_page_default));
4354 page_index->page_data =
4355 (uint8_t *)lun->mode_pages.aps_subpage;
4357 current_page = (struct copan_aps_subpage *)
4358 (page_index->page_data +
4359 (page_index->page_len *
4361 saved_page = (struct copan_aps_subpage *)
4362 (page_index->page_data +
4363 (page_index->page_len *
4367 case DBGCNF_SUBPAGE_CODE: {
4368 struct copan_debugconf_subpage *current_page,
4371 memcpy(&lun->mode_pages.debugconf_subpage[
4373 &debugconf_page_default,
4374 sizeof(debugconf_page_default));
4375 memcpy(&lun->mode_pages.debugconf_subpage[
4376 CTL_PAGE_CHANGEABLE],
4377 &debugconf_page_changeable,
4378 sizeof(debugconf_page_changeable));
4379 memcpy(&lun->mode_pages.debugconf_subpage[
4381 &debugconf_page_default,
4382 sizeof(debugconf_page_default));
4383 memcpy(&lun->mode_pages.debugconf_subpage[
4385 &debugconf_page_default,
4386 sizeof(debugconf_page_default));
4387 page_index->page_data =
4388 (uint8_t *)lun->mode_pages.debugconf_subpage;
4390 current_page = (struct copan_debugconf_subpage *)
4391 (page_index->page_data +
4392 (page_index->page_len *
4394 saved_page = (struct copan_debugconf_subpage *)
4395 (page_index->page_data +
4396 (page_index->page_len *
4401 panic("invalid subpage value %d",
4402 page_index->subpage);
4408 panic("invalid page value %d",
4409 page_index->page_code & SMPH_PC_MASK);
4414 return (CTL_RETVAL_COMPLETE);
4421 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4422 * wants us to allocate the LUN and he can block.
4423 * - ctl_softc is always set
4424 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4426 * Returns 0 for success, non-zero (errno) for failure.
4429 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4430 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4432 struct ctl_lun *nlun, *lun;
4433 struct ctl_port *port;
4434 struct scsi_vpd_id_descriptor *desc;
4435 struct scsi_vpd_id_t10 *t10id;
4436 const char *eui, *naa, *scsiname, *vendor;
4437 int lun_number, i, lun_malloced;
4438 int devidlen, idlen1, idlen2 = 0, len;
4444 * We currently only support Direct Access or Processor LUN types.
4446 switch (be_lun->lun_type) {
4454 be_lun->lun_config_status(be_lun->be_lun,
4455 CTL_LUN_CONFIG_FAILURE);
4458 if (ctl_lun == NULL) {
4459 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4466 memset(lun, 0, sizeof(*lun));
4468 lun->flags = CTL_LUN_MALLOCED;
4470 /* Generate LUN ID. */
4471 devidlen = max(CTL_DEVID_MIN_LEN,
4472 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4473 idlen1 = sizeof(*t10id) + devidlen;
4474 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4475 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4476 if (scsiname != NULL) {
4477 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4478 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4480 eui = ctl_get_opt(&be_lun->options, "eui");
4482 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4484 naa = ctl_get_opt(&be_lun->options, "naa");
4486 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4488 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4489 M_CTL, M_WAITOK | M_ZERO);
4490 lun->lun_devid->len = len;
4491 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4492 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4493 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4494 desc->length = idlen1;
4495 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4496 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4497 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4498 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4500 strncpy(t10id->vendor, vendor,
4501 min(sizeof(t10id->vendor), strlen(vendor)));
4503 strncpy((char *)t10id->vendor_spec_id,
4504 (char *)be_lun->device_id, devidlen);
4505 if (scsiname != NULL) {
4506 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4508 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4509 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4510 SVPD_ID_TYPE_SCSI_NAME;
4511 desc->length = idlen2;
4512 strlcpy(desc->identifier, scsiname, idlen2);
4515 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4517 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4518 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4521 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4524 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4526 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4527 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4530 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4533 mtx_lock(&ctl_softc->ctl_lock);
4535 * See if the caller requested a particular LUN number. If so, see
4536 * if it is available. Otherwise, allocate the first available LUN.
4538 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4539 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4540 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4541 mtx_unlock(&ctl_softc->ctl_lock);
4542 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4543 printf("ctl: requested LUN ID %d is higher "
4544 "than CTL_MAX_LUNS - 1 (%d)\n",
4545 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4548 * XXX KDM return an error, or just assign
4549 * another LUN ID in this case??
4551 printf("ctl: requested LUN ID %d is already "
4552 "in use\n", be_lun->req_lun_id);
4554 if (lun->flags & CTL_LUN_MALLOCED)
4556 be_lun->lun_config_status(be_lun->be_lun,
4557 CTL_LUN_CONFIG_FAILURE);
4560 lun_number = be_lun->req_lun_id;
4562 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4563 if (lun_number == -1) {
4564 mtx_unlock(&ctl_softc->ctl_lock);
4565 printf("ctl: can't allocate LUN on target %ju, out of "
4566 "LUNs\n", (uintmax_t)target_id.id);
4567 if (lun->flags & CTL_LUN_MALLOCED)
4569 be_lun->lun_config_status(be_lun->be_lun,
4570 CTL_LUN_CONFIG_FAILURE);
4574 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4576 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4577 lun->target = target_id;
4578 lun->lun = lun_number;
4579 lun->be_lun = be_lun;
4581 * The processor LUN is always enabled. Disk LUNs come on line
4582 * disabled, and must be enabled by the backend.
4584 lun->flags |= CTL_LUN_DISABLED;
4585 lun->backend = be_lun->be;
4586 be_lun->ctl_lun = lun;
4587 be_lun->lun_id = lun_number;
4588 atomic_add_int(&be_lun->be->num_luns, 1);
4589 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4590 lun->flags |= CTL_LUN_STOPPED;
4592 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4593 lun->flags |= CTL_LUN_INOPERABLE;
4595 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4596 lun->flags |= CTL_LUN_PRIMARY_SC;
4598 lun->ctl_softc = ctl_softc;
4599 TAILQ_INIT(&lun->ooa_queue);
4600 TAILQ_INIT(&lun->blocked_queue);
4601 STAILQ_INIT(&lun->error_list);
4602 ctl_tpc_lun_init(lun);
4605 * Initialize the mode page index.
4607 ctl_init_page_index(lun);
4610 * Set the poweron UA for all initiators on this LUN only.
4612 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4613 lun->pending_ua[i] = CTL_UA_POWERON;
4616 * Now, before we insert this lun on the lun list, set the lun
4617 * inventory changed UA for all other luns.
4619 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4620 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4621 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4625 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4627 ctl_softc->ctl_luns[lun_number] = lun;
4629 ctl_softc->num_luns++;
4631 /* Setup statistics gathering */
4632 lun->stats.device_type = be_lun->lun_type;
4633 lun->stats.lun_number = lun_number;
4634 if (lun->stats.device_type == T_DIRECT)
4635 lun->stats.blocksize = be_lun->blocksize;
4637 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4638 for (i = 0;i < CTL_MAX_PORTS;i++)
4639 lun->stats.ports[i].targ_port = i;
4641 mtx_unlock(&ctl_softc->ctl_lock);
4643 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4646 * Run through each registered FETD and bring it online if it isn't
4647 * already. Enable the target ID if it hasn't been enabled, and
4648 * enable this particular LUN.
4650 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4653 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4655 printf("ctl_alloc_lun: FETD %s port %d returned error "
4656 "%d for lun_enable on target %ju lun %d\n",
4657 port->port_name, port->targ_port, retval,
4658 (uintmax_t)target_id.id, lun_number);
4660 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4668 * - LUN has already been marked invalid and any pending I/O has been taken
4672 ctl_free_lun(struct ctl_lun *lun)
4674 struct ctl_softc *softc;
4676 struct ctl_port *port;
4678 struct ctl_lun *nlun;
4681 softc = lun->ctl_softc;
4683 mtx_assert(&softc->ctl_lock, MA_OWNED);
4685 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4687 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4689 softc->ctl_luns[lun->lun] = NULL;
4691 if (!TAILQ_EMPTY(&lun->ooa_queue))
4692 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4697 * XXX KDM this scheme only works for a single target/multiple LUN
4698 * setup. It needs to be revamped for a multiple target scheme.
4700 * XXX KDM this results in port->lun_disable() getting called twice,
4701 * once when ctl_disable_lun() is called, and a second time here.
4702 * We really need to re-think the LUN disable semantics. There
4703 * should probably be several steps/levels to LUN removal:
4708 * Right now we only have a disable method when communicating to
4709 * the front end ports, at least for individual LUNs.
4712 STAILQ_FOREACH(port, &softc->port_list, links) {
4715 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4718 printf("ctl_free_lun: FETD %s port %d returned error "
4719 "%d for lun_disable on target %ju lun %jd\n",
4720 port->port_name, port->targ_port, retval,
4721 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4724 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4725 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4727 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4729 printf("ctl_free_lun: FETD %s port %d "
4730 "returned error %d for targ_disable on "
4731 "target %ju\n", port->port_name,
4732 port->targ_port, retval,
4733 (uintmax_t)lun->target.id);
4735 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4737 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4741 port->port_offline(port->onoff_arg);
4742 port->status &= ~CTL_PORT_STATUS_ONLINE;
4749 * Tell the backend to free resources, if this LUN has a backend.
4751 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4752 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4754 ctl_tpc_lun_shutdown(lun);
4755 mtx_destroy(&lun->lun_lock);
4756 free(lun->lun_devid, M_CTL);
4757 if (lun->flags & CTL_LUN_MALLOCED)
4760 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4761 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4762 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4770 ctl_create_lun(struct ctl_be_lun *be_lun)
4772 struct ctl_softc *ctl_softc;
4774 ctl_softc = control_softc;
4777 * ctl_alloc_lun() should handle all potential failure cases.
4779 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4783 ctl_add_lun(struct ctl_be_lun *be_lun)
4785 struct ctl_softc *ctl_softc = control_softc;
4787 mtx_lock(&ctl_softc->ctl_lock);
4788 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4789 mtx_unlock(&ctl_softc->ctl_lock);
4790 wakeup(&ctl_softc->pending_lun_queue);
4796 ctl_enable_lun(struct ctl_be_lun *be_lun)
4798 struct ctl_softc *ctl_softc;
4799 struct ctl_port *port, *nport;
4800 struct ctl_lun *lun;
4803 ctl_softc = control_softc;
4805 lun = (struct ctl_lun *)be_lun->ctl_lun;
4807 mtx_lock(&ctl_softc->ctl_lock);
4808 mtx_lock(&lun->lun_lock);
4809 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4811 * eh? Why did we get called if the LUN is already
4814 mtx_unlock(&lun->lun_lock);
4815 mtx_unlock(&ctl_softc->ctl_lock);
4818 lun->flags &= ~CTL_LUN_DISABLED;
4819 mtx_unlock(&lun->lun_lock);
4821 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4822 nport = STAILQ_NEXT(port, links);
4825 * Drop the lock while we call the FETD's enable routine.
4826 * This can lead to a callback into CTL (at least in the
4827 * case of the internal initiator frontend.
4829 mtx_unlock(&ctl_softc->ctl_lock);
4830 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4831 mtx_lock(&ctl_softc->ctl_lock);
4833 printf("%s: FETD %s port %d returned error "
4834 "%d for lun_enable on target %ju lun %jd\n",
4835 __func__, port->port_name, port->targ_port, retval,
4836 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4840 /* NOTE: TODO: why does lun enable affect port status? */
4841 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4846 mtx_unlock(&ctl_softc->ctl_lock);
4852 ctl_disable_lun(struct ctl_be_lun *be_lun)
4854 struct ctl_softc *ctl_softc;
4855 struct ctl_port *port;
4856 struct ctl_lun *lun;
4859 ctl_softc = control_softc;
4861 lun = (struct ctl_lun *)be_lun->ctl_lun;
4863 mtx_lock(&ctl_softc->ctl_lock);
4864 mtx_lock(&lun->lun_lock);
4865 if (lun->flags & CTL_LUN_DISABLED) {
4866 mtx_unlock(&lun->lun_lock);
4867 mtx_unlock(&ctl_softc->ctl_lock);
4870 lun->flags |= CTL_LUN_DISABLED;
4871 mtx_unlock(&lun->lun_lock);
4873 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4874 mtx_unlock(&ctl_softc->ctl_lock);
4876 * Drop the lock before we call the frontend's disable
4877 * routine, to avoid lock order reversals.
4879 * XXX KDM what happens if the frontend list changes while
4880 * we're traversing it? It's unlikely, but should be handled.
4882 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4884 mtx_lock(&ctl_softc->ctl_lock);
4886 printf("ctl_alloc_lun: FETD %s port %d returned error "
4887 "%d for lun_disable on target %ju lun %jd\n",
4888 port->port_name, port->targ_port, retval,
4889 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4893 mtx_unlock(&ctl_softc->ctl_lock);
4899 ctl_start_lun(struct ctl_be_lun *be_lun)
4901 struct ctl_softc *ctl_softc;
4902 struct ctl_lun *lun;
4904 ctl_softc = control_softc;
4906 lun = (struct ctl_lun *)be_lun->ctl_lun;
4908 mtx_lock(&lun->lun_lock);
4909 lun->flags &= ~CTL_LUN_STOPPED;
4910 mtx_unlock(&lun->lun_lock);
4916 ctl_stop_lun(struct ctl_be_lun *be_lun)
4918 struct ctl_softc *ctl_softc;
4919 struct ctl_lun *lun;
4921 ctl_softc = control_softc;
4923 lun = (struct ctl_lun *)be_lun->ctl_lun;
4925 mtx_lock(&lun->lun_lock);
4926 lun->flags |= CTL_LUN_STOPPED;
4927 mtx_unlock(&lun->lun_lock);
4933 ctl_lun_offline(struct ctl_be_lun *be_lun)
4935 struct ctl_softc *ctl_softc;
4936 struct ctl_lun *lun;
4938 ctl_softc = control_softc;
4940 lun = (struct ctl_lun *)be_lun->ctl_lun;
4942 mtx_lock(&lun->lun_lock);
4943 lun->flags |= CTL_LUN_OFFLINE;
4944 mtx_unlock(&lun->lun_lock);
4950 ctl_lun_online(struct ctl_be_lun *be_lun)
4952 struct ctl_softc *ctl_softc;
4953 struct ctl_lun *lun;
4955 ctl_softc = control_softc;
4957 lun = (struct ctl_lun *)be_lun->ctl_lun;
4959 mtx_lock(&lun->lun_lock);
4960 lun->flags &= ~CTL_LUN_OFFLINE;
4961 mtx_unlock(&lun->lun_lock);
4967 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4969 struct ctl_softc *ctl_softc;
4970 struct ctl_lun *lun;
4972 ctl_softc = control_softc;
4974 lun = (struct ctl_lun *)be_lun->ctl_lun;
4976 mtx_lock(&lun->lun_lock);
4979 * The LUN needs to be disabled before it can be marked invalid.
4981 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4982 mtx_unlock(&lun->lun_lock);
4986 * Mark the LUN invalid.
4988 lun->flags |= CTL_LUN_INVALID;
4991 * If there is nothing in the OOA queue, go ahead and free the LUN.
4992 * If we have something in the OOA queue, we'll free it when the
4993 * last I/O completes.
4995 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4996 mtx_unlock(&lun->lun_lock);
4997 mtx_lock(&ctl_softc->ctl_lock);
4999 mtx_unlock(&ctl_softc->ctl_lock);
5001 mtx_unlock(&lun->lun_lock);
5007 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5009 struct ctl_softc *ctl_softc;
5010 struct ctl_lun *lun;
5012 ctl_softc = control_softc;
5013 lun = (struct ctl_lun *)be_lun->ctl_lun;
5015 mtx_lock(&lun->lun_lock);
5016 lun->flags |= CTL_LUN_INOPERABLE;
5017 mtx_unlock(&lun->lun_lock);
5023 ctl_lun_operable(struct ctl_be_lun *be_lun)
5025 struct ctl_softc *ctl_softc;
5026 struct ctl_lun *lun;
5028 ctl_softc = control_softc;
5029 lun = (struct ctl_lun *)be_lun->ctl_lun;
5031 mtx_lock(&lun->lun_lock);
5032 lun->flags &= ~CTL_LUN_INOPERABLE;
5033 mtx_unlock(&lun->lun_lock);
5039 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5042 struct ctl_softc *softc;
5043 struct ctl_lun *lun;
5044 struct copan_aps_subpage *current_sp;
5045 struct ctl_page_index *page_index;
5048 softc = control_softc;
5050 mtx_lock(&softc->ctl_lock);
5052 lun = (struct ctl_lun *)be_lun->ctl_lun;
5053 mtx_lock(&lun->lun_lock);
5056 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5057 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5061 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5063 page_index = &lun->mode_pages.index[i];
5066 if (page_index == NULL) {
5067 mtx_unlock(&lun->lun_lock);
5068 mtx_unlock(&softc->ctl_lock);
5069 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5070 (uintmax_t)lun->lun);
5074 if ((softc->aps_locked_lun != 0)
5075 && (softc->aps_locked_lun != lun->lun)) {
5076 printf("%s: attempt to lock LUN %llu when %llu is already "
5078 mtx_unlock(&lun->lun_lock);
5079 mtx_unlock(&softc->ctl_lock);
5084 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5085 (page_index->page_len * CTL_PAGE_CURRENT));
5088 current_sp->lock_active = APS_LOCK_ACTIVE;
5089 softc->aps_locked_lun = lun->lun;
5091 current_sp->lock_active = 0;
5092 softc->aps_locked_lun = 0;
5097 * If we're in HA mode, try to send the lock message to the other
5100 if (ctl_is_single == 0) {
5102 union ctl_ha_msg lock_msg;
5104 lock_msg.hdr.nexus = *nexus;
5105 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5107 lock_msg.aps.lock_flag = 1;
5109 lock_msg.aps.lock_flag = 0;
5110 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5111 sizeof(lock_msg), 0);
5112 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5113 printf("%s: APS (lock=%d) error returned from "
5114 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5115 mtx_unlock(&lun->lun_lock);
5116 mtx_unlock(&softc->ctl_lock);
5121 mtx_unlock(&lun->lun_lock);
5122 mtx_unlock(&softc->ctl_lock);
5128 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5130 struct ctl_lun *lun;
5131 struct ctl_softc *softc;
5134 softc = control_softc;
5136 lun = (struct ctl_lun *)be_lun->ctl_lun;
5138 mtx_lock(&lun->lun_lock);
5140 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5141 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5143 mtx_unlock(&lun->lun_lock);
5147 * Backend "memory move is complete" callback for requests that never
5148 * make it down to say RAIDCore's configuration code.
5151 ctl_config_move_done(union ctl_io *io)
5155 retval = CTL_RETVAL_COMPLETE;
5158 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5160 * XXX KDM this shouldn't happen, but what if it does?
5162 if (io->io_hdr.io_type != CTL_IO_SCSI)
5163 panic("I/O type isn't CTL_IO_SCSI!");
5165 if ((io->io_hdr.port_status == 0)
5166 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5167 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5168 io->io_hdr.status = CTL_SUCCESS;
5169 else if ((io->io_hdr.port_status != 0)
5170 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5171 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5173 * For hardware error sense keys, the sense key
5174 * specific value is defined to be a retry count,
5175 * but we use it to pass back an internal FETD
5176 * error code. XXX KDM Hopefully the FETD is only
5177 * using 16 bits for an error code, since that's
5178 * all the space we have in the sks field.
5180 ctl_set_internal_failure(&io->scsiio,
5183 io->io_hdr.port_status);
5184 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5185 free(io->scsiio.kern_data_ptr, M_CTL);
5190 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5191 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5192 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5194 * XXX KDM just assuming a single pointer here, and not a
5195 * S/G list. If we start using S/G lists for config data,
5196 * we'll need to know how to clean them up here as well.
5198 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5199 free(io->scsiio.kern_data_ptr, M_CTL);
5200 /* Hopefully the user has already set the status... */
5204 * XXX KDM now we need to continue data movement. Some
5206 * - call ctl_scsiio() again? We don't do this for data
5207 * writes, because for those at least we know ahead of
5208 * time where the write will go and how long it is. For
5209 * config writes, though, that information is largely
5210 * contained within the write itself, thus we need to
5211 * parse out the data again.
5213 * - Call some other function once the data is in?
5217 * XXX KDM call ctl_scsiio() again for now, and check flag
5218 * bits to see whether we're allocated or not.
5220 retval = ctl_scsiio(&io->scsiio);
5227 * This gets called by a backend driver when it is done with a
5228 * data_submit method.
5231 ctl_data_submit_done(union ctl_io *io)
5234 * If the IO_CONT flag is set, we need to call the supplied
5235 * function to continue processing the I/O, instead of completing
5238 * If there is an error, though, we don't want to keep processing.
5239 * Instead, just send status back to the initiator.
5241 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5242 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5243 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5244 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5245 io->scsiio.io_cont(io);
5252 * This gets called by a backend driver when it is done with a
5253 * configuration write.
5256 ctl_config_write_done(union ctl_io *io)
5259 * If the IO_CONT flag is set, we need to call the supplied
5260 * function to continue processing the I/O, instead of completing
5263 * If there is an error, though, we don't want to keep processing.
5264 * Instead, just send status back to the initiator.
5266 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5267 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5268 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5269 io->scsiio.io_cont(io);
5273 * Since a configuration write can be done for commands that actually
5274 * have data allocated, like write buffer, and commands that have
5275 * no data, like start/stop unit, we need to check here.
5277 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5278 free(io->scsiio.kern_data_ptr, M_CTL);
5283 * SCSI release command.
5286 ctl_scsi_release(struct ctl_scsiio *ctsio)
5288 int length, longid, thirdparty_id, resv_id;
5289 struct ctl_softc *ctl_softc;
5290 struct ctl_lun *lun;
5295 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5298 ctl_softc = control_softc;
5300 switch (ctsio->cdb[0]) {
5302 struct scsi_release_10 *cdb;
5304 cdb = (struct scsi_release_10 *)ctsio->cdb;
5306 if (cdb->byte2 & SR10_LONGID)
5309 thirdparty_id = cdb->thirdparty_id;
5311 resv_id = cdb->resv_id;
5312 length = scsi_2btoul(cdb->length);
5319 * XXX KDM right now, we only support LUN reservation. We don't
5320 * support 3rd party reservations, or extent reservations, which
5321 * might actually need the parameter list. If we've gotten this
5322 * far, we've got a LUN reservation. Anything else got kicked out
5323 * above. So, according to SPC, ignore the length.
5327 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5329 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5330 ctsio->kern_data_len = length;
5331 ctsio->kern_total_len = length;
5332 ctsio->kern_data_resid = 0;
5333 ctsio->kern_rel_offset = 0;
5334 ctsio->kern_sg_entries = 0;
5335 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5336 ctsio->be_move_done = ctl_config_move_done;
5337 ctl_datamove((union ctl_io *)ctsio);
5339 return (CTL_RETVAL_COMPLETE);
5343 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5345 mtx_lock(&lun->lun_lock);
5348 * According to SPC, it is not an error for an intiator to attempt
5349 * to release a reservation on a LUN that isn't reserved, or that
5350 * is reserved by another initiator. The reservation can only be
5351 * released, though, by the initiator who made it or by one of
5352 * several reset type events.
5354 if (lun->flags & CTL_LUN_RESERVED) {
5355 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5356 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5357 && (ctsio->io_hdr.nexus.targ_target.id ==
5358 lun->rsv_nexus.targ_target.id)) {
5359 lun->flags &= ~CTL_LUN_RESERVED;
5363 mtx_unlock(&lun->lun_lock);
5365 ctsio->scsi_status = SCSI_STATUS_OK;
5366 ctsio->io_hdr.status = CTL_SUCCESS;
5368 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5369 free(ctsio->kern_data_ptr, M_CTL);
5370 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5373 ctl_done((union ctl_io *)ctsio);
5374 return (CTL_RETVAL_COMPLETE);
5378 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5380 int extent, thirdparty, longid;
5381 int resv_id, length;
5382 uint64_t thirdparty_id;
5383 struct ctl_softc *ctl_softc;
5384 struct ctl_lun *lun;
5393 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5395 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5396 ctl_softc = control_softc;
5398 switch (ctsio->cdb[0]) {
5400 struct scsi_reserve_10 *cdb;
5402 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5404 if (cdb->byte2 & SR10_LONGID)
5407 thirdparty_id = cdb->thirdparty_id;
5409 resv_id = cdb->resv_id;
5410 length = scsi_2btoul(cdb->length);
5416 * XXX KDM right now, we only support LUN reservation. We don't
5417 * support 3rd party reservations, or extent reservations, which
5418 * might actually need the parameter list. If we've gotten this
5419 * far, we've got a LUN reservation. Anything else got kicked out
5420 * above. So, according to SPC, ignore the length.
5424 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5426 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5427 ctsio->kern_data_len = length;
5428 ctsio->kern_total_len = length;
5429 ctsio->kern_data_resid = 0;
5430 ctsio->kern_rel_offset = 0;
5431 ctsio->kern_sg_entries = 0;
5432 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5433 ctsio->be_move_done = ctl_config_move_done;
5434 ctl_datamove((union ctl_io *)ctsio);
5436 return (CTL_RETVAL_COMPLETE);
5440 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5442 mtx_lock(&lun->lun_lock);
5443 if (lun->flags & CTL_LUN_RESERVED) {
5444 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5445 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5446 || (ctsio->io_hdr.nexus.targ_target.id !=
5447 lun->rsv_nexus.targ_target.id)) {
5448 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5449 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5454 lun->flags |= CTL_LUN_RESERVED;
5455 lun->rsv_nexus = ctsio->io_hdr.nexus;
5457 ctsio->scsi_status = SCSI_STATUS_OK;
5458 ctsio->io_hdr.status = CTL_SUCCESS;
5461 mtx_unlock(&lun->lun_lock);
5463 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5464 free(ctsio->kern_data_ptr, M_CTL);
5465 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5468 ctl_done((union ctl_io *)ctsio);
5469 return (CTL_RETVAL_COMPLETE);
5473 ctl_start_stop(struct ctl_scsiio *ctsio)
5475 struct scsi_start_stop_unit *cdb;
5476 struct ctl_lun *lun;
5477 struct ctl_softc *ctl_softc;
5480 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5482 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5483 ctl_softc = control_softc;
5486 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5490 * We don't support the immediate bit on a stop unit. In order to
5491 * do that, we would need to code up a way to know that a stop is
5492 * pending, and hold off any new commands until it completes, one
5493 * way or another. Then we could accept or reject those commands
5494 * depending on its status. We would almost need to do the reverse
5495 * of what we do below for an immediate start -- return the copy of
5496 * the ctl_io to the FETD with status to send to the host (and to
5497 * free the copy!) and then free the original I/O once the stop
5498 * actually completes. That way, the OOA queue mechanism can work
5499 * to block commands that shouldn't proceed. Another alternative
5500 * would be to put the copy in the queue in place of the original,
5501 * and return the original back to the caller. That could be
5504 if ((cdb->byte2 & SSS_IMMED)
5505 && ((cdb->how & SSS_START) == 0)) {
5506 ctl_set_invalid_field(ctsio,
5512 ctl_done((union ctl_io *)ctsio);
5513 return (CTL_RETVAL_COMPLETE);
5516 if ((lun->flags & CTL_LUN_PR_RESERVED)
5517 && ((cdb->how & SSS_START)==0)) {
5520 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5521 if (!lun->per_res[residx].registered
5522 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5524 ctl_set_reservation_conflict(ctsio);
5525 ctl_done((union ctl_io *)ctsio);
5526 return (CTL_RETVAL_COMPLETE);
5531 * If there is no backend on this device, we can't start or stop
5532 * it. In theory we shouldn't get any start/stop commands in the
5533 * first place at this level if the LUN doesn't have a backend.
5534 * That should get stopped by the command decode code.
5536 if (lun->backend == NULL) {
5537 ctl_set_invalid_opcode(ctsio);
5538 ctl_done((union ctl_io *)ctsio);
5539 return (CTL_RETVAL_COMPLETE);
5543 * XXX KDM Copan-specific offline behavior.
5544 * Figure out a reasonable way to port this?
5547 mtx_lock(&lun->lun_lock);
5549 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5550 && (lun->flags & CTL_LUN_OFFLINE)) {
5552 * If the LUN is offline, and the on/offline bit isn't set,
5553 * reject the start or stop. Otherwise, let it through.
5555 mtx_unlock(&lun->lun_lock);
5556 ctl_set_lun_not_ready(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5559 mtx_unlock(&lun->lun_lock);
5560 #endif /* NEEDTOPORT */
5562 * This could be a start or a stop when we're online,
5563 * or a stop/offline or start/online. A start or stop when
5564 * we're offline is covered in the case above.
5567 * In the non-immediate case, we send the request to
5568 * the backend and return status to the user when
5571 * In the immediate case, we allocate a new ctl_io
5572 * to hold a copy of the request, and send that to
5573 * the backend. We then set good status on the
5574 * user's request and return it immediately.
5576 if (cdb->byte2 & SSS_IMMED) {
5577 union ctl_io *new_io;
5579 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5580 if (new_io == NULL) {
5581 ctl_set_busy(ctsio);
5582 ctl_done((union ctl_io *)ctsio);
5584 ctl_copy_io((union ctl_io *)ctsio,
5586 retval = lun->backend->config_write(new_io);
5587 ctl_set_success(ctsio);
5588 ctl_done((union ctl_io *)ctsio);
5591 retval = lun->backend->config_write(
5592 (union ctl_io *)ctsio);
5601 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5602 * we don't really do anything with the LBA and length fields if the user
5603 * passes them in. Instead we'll just flush out the cache for the entire
5607 ctl_sync_cache(struct ctl_scsiio *ctsio)
5609 struct ctl_lun *lun;
5610 struct ctl_softc *ctl_softc;
5611 uint64_t starting_lba;
5612 uint32_t block_count;
5615 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5618 ctl_softc = control_softc;
5621 switch (ctsio->cdb[0]) {
5622 case SYNCHRONIZE_CACHE: {
5623 struct scsi_sync_cache *cdb;
5624 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5626 starting_lba = scsi_4btoul(cdb->begin_lba);
5627 block_count = scsi_2btoul(cdb->lb_count);
5630 case SYNCHRONIZE_CACHE_16: {
5631 struct scsi_sync_cache_16 *cdb;
5632 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5634 starting_lba = scsi_8btou64(cdb->begin_lba);
5635 block_count = scsi_4btoul(cdb->lb_count);
5639 ctl_set_invalid_opcode(ctsio);
5640 ctl_done((union ctl_io *)ctsio);
5642 break; /* NOTREACHED */
5646 * We check the LBA and length, but don't do anything with them.
5647 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5648 * get flushed. This check will just help satisfy anyone who wants
5649 * to see an error for an out of range LBA.
5651 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5652 ctl_set_lba_out_of_range(ctsio);
5653 ctl_done((union ctl_io *)ctsio);
5658 * If this LUN has no backend, we can't flush the cache anyway.
5660 if (lun->backend == NULL) {
5661 ctl_set_invalid_opcode(ctsio);
5662 ctl_done((union ctl_io *)ctsio);
5667 * Check to see whether we're configured to send the SYNCHRONIZE
5668 * CACHE command directly to the back end.
5670 mtx_lock(&lun->lun_lock);
5671 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5672 && (++(lun->sync_count) >= lun->sync_interval)) {
5673 lun->sync_count = 0;
5674 mtx_unlock(&lun->lun_lock);
5675 retval = lun->backend->config_write((union ctl_io *)ctsio);
5677 mtx_unlock(&lun->lun_lock);
5678 ctl_set_success(ctsio);
5679 ctl_done((union ctl_io *)ctsio);
5688 ctl_format(struct ctl_scsiio *ctsio)
5690 struct scsi_format *cdb;
5691 struct ctl_lun *lun;
5692 struct ctl_softc *ctl_softc;
5693 int length, defect_list_len;
5695 CTL_DEBUG_PRINT(("ctl_format\n"));
5697 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5698 ctl_softc = control_softc;
5700 cdb = (struct scsi_format *)ctsio->cdb;
5703 if (cdb->byte2 & SF_FMTDATA) {
5704 if (cdb->byte2 & SF_LONGLIST)
5705 length = sizeof(struct scsi_format_header_long);
5707 length = sizeof(struct scsi_format_header_short);
5710 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5712 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5713 ctsio->kern_data_len = length;
5714 ctsio->kern_total_len = length;
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 defect_list_len = 0;
5727 if (cdb->byte2 & SF_FMTDATA) {
5728 if (cdb->byte2 & SF_LONGLIST) {
5729 struct scsi_format_header_long *header;
5731 header = (struct scsi_format_header_long *)
5732 ctsio->kern_data_ptr;
5734 defect_list_len = scsi_4btoul(header->defect_list_len);
5735 if (defect_list_len != 0) {
5736 ctl_set_invalid_field(ctsio,
5745 struct scsi_format_header_short *header;
5747 header = (struct scsi_format_header_short *)
5748 ctsio->kern_data_ptr;
5750 defect_list_len = scsi_2btoul(header->defect_list_len);
5751 if (defect_list_len != 0) {
5752 ctl_set_invalid_field(ctsio,
5764 * The format command will clear out the "Medium format corrupted"
5765 * status if set by the configuration code. That status is really
5766 * just a way to notify the host that we have lost the media, and
5767 * get them to issue a command that will basically make them think
5768 * they're blowing away the media.
5770 mtx_lock(&lun->lun_lock);
5771 lun->flags &= ~CTL_LUN_INOPERABLE;
5772 mtx_unlock(&lun->lun_lock);
5774 ctsio->scsi_status = SCSI_STATUS_OK;
5775 ctsio->io_hdr.status = CTL_SUCCESS;
5778 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5779 free(ctsio->kern_data_ptr, M_CTL);
5780 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5783 ctl_done((union ctl_io *)ctsio);
5784 return (CTL_RETVAL_COMPLETE);
5788 ctl_read_buffer(struct ctl_scsiio *ctsio)
5790 struct scsi_read_buffer *cdb;
5791 struct ctl_lun *lun;
5792 int buffer_offset, len;
5793 static uint8_t descr[4];
5794 static uint8_t echo_descr[4] = { 0 };
5796 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5799 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5801 if (lun->flags & CTL_LUN_PR_RESERVED) {
5805 * XXX KDM need a lock here.
5807 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5808 if ((lun->res_type == SPR_TYPE_EX_AC
5809 && residx != lun->pr_res_idx)
5810 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5811 || lun->res_type == SPR_TYPE_EX_AC_AR)
5812 && !lun->per_res[residx].registered)) {
5813 ctl_set_reservation_conflict(ctsio);
5814 ctl_done((union ctl_io *)ctsio);
5815 return (CTL_RETVAL_COMPLETE);
5819 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5820 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5821 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5822 ctl_set_invalid_field(ctsio,
5828 ctl_done((union ctl_io *)ctsio);
5829 return (CTL_RETVAL_COMPLETE);
5832 len = scsi_3btoul(cdb->length);
5833 buffer_offset = scsi_3btoul(cdb->offset);
5835 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5836 ctl_set_invalid_field(ctsio,
5842 ctl_done((union ctl_io *)ctsio);
5843 return (CTL_RETVAL_COMPLETE);
5846 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5848 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5849 ctsio->kern_data_ptr = descr;
5850 len = min(len, sizeof(descr));
5851 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5852 ctsio->kern_data_ptr = echo_descr;
5853 len = min(len, sizeof(echo_descr));
5855 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5856 ctsio->kern_data_len = len;
5857 ctsio->kern_total_len = len;
5858 ctsio->kern_data_resid = 0;
5859 ctsio->kern_rel_offset = 0;
5860 ctsio->kern_sg_entries = 0;
5861 ctsio->be_move_done = ctl_config_move_done;
5862 ctl_datamove((union ctl_io *)ctsio);
5864 return (CTL_RETVAL_COMPLETE);
5868 ctl_write_buffer(struct ctl_scsiio *ctsio)
5870 struct scsi_write_buffer *cdb;
5871 struct ctl_lun *lun;
5872 int buffer_offset, len;
5874 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5876 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5877 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5879 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5880 ctl_set_invalid_field(ctsio,
5886 ctl_done((union ctl_io *)ctsio);
5887 return (CTL_RETVAL_COMPLETE);
5890 len = scsi_3btoul(cdb->length);
5891 buffer_offset = scsi_3btoul(cdb->offset);
5893 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5894 ctl_set_invalid_field(ctsio,
5900 ctl_done((union ctl_io *)ctsio);
5901 return (CTL_RETVAL_COMPLETE);
5905 * If we've got a kernel request that hasn't been malloced yet,
5906 * malloc it and tell the caller the data buffer is here.
5908 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5909 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5910 ctsio->kern_data_len = len;
5911 ctsio->kern_total_len = len;
5912 ctsio->kern_data_resid = 0;
5913 ctsio->kern_rel_offset = 0;
5914 ctsio->kern_sg_entries = 0;
5915 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5916 ctsio->be_move_done = ctl_config_move_done;
5917 ctl_datamove((union ctl_io *)ctsio);
5919 return (CTL_RETVAL_COMPLETE);
5922 ctl_done((union ctl_io *)ctsio);
5924 return (CTL_RETVAL_COMPLETE);
5928 ctl_write_same(struct ctl_scsiio *ctsio)
5930 struct ctl_lun *lun;
5931 struct ctl_lba_len_flags *lbalen;
5933 uint32_t num_blocks;
5937 retval = CTL_RETVAL_COMPLETE;
5939 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5941 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5943 switch (ctsio->cdb[0]) {
5944 case WRITE_SAME_10: {
5945 struct scsi_write_same_10 *cdb;
5947 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5949 lba = scsi_4btoul(cdb->addr);
5950 num_blocks = scsi_2btoul(cdb->length);
5954 case WRITE_SAME_16: {
5955 struct scsi_write_same_16 *cdb;
5957 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5959 lba = scsi_8btou64(cdb->addr);
5960 num_blocks = scsi_4btoul(cdb->length);
5966 * We got a command we don't support. This shouldn't
5967 * happen, commands should be filtered out above us.
5969 ctl_set_invalid_opcode(ctsio);
5970 ctl_done((union ctl_io *)ctsio);
5972 return (CTL_RETVAL_COMPLETE);
5973 break; /* NOTREACHED */
5977 * The first check is to make sure we're in bounds, the second
5978 * check is to catch wrap-around problems. If the lba + num blocks
5979 * is less than the lba, then we've wrapped around and the block
5980 * range is invalid anyway.
5982 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5983 || ((lba + num_blocks) < lba)) {
5984 ctl_set_lba_out_of_range(ctsio);
5985 ctl_done((union ctl_io *)ctsio);
5986 return (CTL_RETVAL_COMPLETE);
5989 /* Zero number of blocks means "to the last logical block" */
5990 if (num_blocks == 0) {
5991 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5992 ctl_set_invalid_field(ctsio,
5998 ctl_done((union ctl_io *)ctsio);
5999 return (CTL_RETVAL_COMPLETE);
6001 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6004 len = lun->be_lun->blocksize;
6007 * If we've got a kernel request that hasn't been malloced yet,
6008 * malloc it and tell the caller the data buffer is here.
6010 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6011 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6012 ctsio->kern_data_len = len;
6013 ctsio->kern_total_len = len;
6014 ctsio->kern_data_resid = 0;
6015 ctsio->kern_rel_offset = 0;
6016 ctsio->kern_sg_entries = 0;
6017 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6018 ctsio->be_move_done = ctl_config_move_done;
6019 ctl_datamove((union ctl_io *)ctsio);
6021 return (CTL_RETVAL_COMPLETE);
6024 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6026 lbalen->len = num_blocks;
6027 lbalen->flags = byte2;
6028 retval = lun->backend->config_write((union ctl_io *)ctsio);
6034 ctl_unmap(struct ctl_scsiio *ctsio)
6036 struct ctl_lun *lun;
6037 struct scsi_unmap *cdb;
6038 struct ctl_ptr_len_flags *ptrlen;
6039 struct scsi_unmap_header *hdr;
6040 struct scsi_unmap_desc *buf, *end;
6042 uint32_t num_blocks;
6046 retval = CTL_RETVAL_COMPLETE;
6048 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6051 cdb = (struct scsi_unmap *)ctsio->cdb;
6053 len = scsi_2btoul(cdb->length);
6057 * If we've got a kernel request that hasn't been malloced yet,
6058 * malloc it and tell the caller the data buffer is here.
6060 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6061 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6062 ctsio->kern_data_len = len;
6063 ctsio->kern_total_len = len;
6064 ctsio->kern_data_resid = 0;
6065 ctsio->kern_rel_offset = 0;
6066 ctsio->kern_sg_entries = 0;
6067 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6068 ctsio->be_move_done = ctl_config_move_done;
6069 ctl_datamove((union ctl_io *)ctsio);
6071 return (CTL_RETVAL_COMPLETE);
6074 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6075 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6076 if (len < sizeof (*hdr) ||
6077 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6078 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6079 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6080 ctl_set_invalid_field(ctsio,
6086 ctl_done((union ctl_io *)ctsio);
6087 return (CTL_RETVAL_COMPLETE);
6089 len = scsi_2btoul(hdr->desc_length);
6090 buf = (struct scsi_unmap_desc *)(hdr + 1);
6091 end = buf + len / sizeof(*buf);
6093 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6094 ptrlen->ptr = (void *)buf;
6096 ptrlen->flags = byte2;
6098 for (; buf < end; buf++) {
6099 lba = scsi_8btou64(buf->lba);
6100 num_blocks = scsi_4btoul(buf->length);
6101 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6102 || ((lba + num_blocks) < lba)) {
6103 ctl_set_lba_out_of_range(ctsio);
6104 ctl_done((union ctl_io *)ctsio);
6105 return (CTL_RETVAL_COMPLETE);
6109 retval = lun->backend->config_write((union ctl_io *)ctsio);
6115 * Note that this function currently doesn't actually do anything inside
6116 * CTL to enforce things if the DQue bit is turned on.
6118 * Also note that this function can't be used in the default case, because
6119 * the DQue bit isn't set in the changeable mask for the control mode page
6120 * anyway. This is just here as an example for how to implement a page
6121 * handler, and a placeholder in case we want to allow the user to turn
6122 * tagged queueing on and off.
6124 * The D_SENSE bit handling is functional, however, and will turn
6125 * descriptor sense on and off for a given LUN.
6128 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6129 struct ctl_page_index *page_index, uint8_t *page_ptr)
6131 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6132 struct ctl_lun *lun;
6133 struct ctl_softc *softc;
6137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6138 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6141 user_cp = (struct scsi_control_page *)page_ptr;
6142 current_cp = (struct scsi_control_page *)
6143 (page_index->page_data + (page_index->page_len *
6145 saved_cp = (struct scsi_control_page *)
6146 (page_index->page_data + (page_index->page_len *
6149 softc = control_softc;
6151 mtx_lock(&lun->lun_lock);
6152 if (((current_cp->rlec & SCP_DSENSE) == 0)
6153 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6155 * Descriptor sense is currently turned off and the user
6156 * wants to turn it on.
6158 current_cp->rlec |= SCP_DSENSE;
6159 saved_cp->rlec |= SCP_DSENSE;
6160 lun->flags |= CTL_LUN_SENSE_DESC;
6162 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6163 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6165 * Descriptor sense is currently turned on, and the user
6166 * wants to turn it off.
6168 current_cp->rlec &= ~SCP_DSENSE;
6169 saved_cp->rlec &= ~SCP_DSENSE;
6170 lun->flags &= ~CTL_LUN_SENSE_DESC;
6173 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6174 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6176 csevent_log(CSC_CTL | CSC_SHELF_SW |
6178 csevent_LogType_Trace,
6179 csevent_Severity_Information,
6180 csevent_AlertLevel_Green,
6181 csevent_FRU_Firmware,
6182 csevent_FRU_Unknown,
6183 "Received untagged to untagged transition");
6184 #endif /* NEEDTOPORT */
6187 csevent_log(CSC_CTL | CSC_SHELF_SW |
6189 csevent_LogType_ConfigChange,
6190 csevent_Severity_Information,
6191 csevent_AlertLevel_Green,
6192 csevent_FRU_Firmware,
6193 csevent_FRU_Unknown,
6194 "Received untagged to tagged "
6195 "queueing transition");
6196 #endif /* NEEDTOPORT */
6198 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6199 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6203 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6205 csevent_log(CSC_CTL | CSC_SHELF_SW |
6207 csevent_LogType_ConfigChange,
6208 csevent_Severity_Warning,
6209 csevent_AlertLevel_Yellow,
6210 csevent_FRU_Firmware,
6211 csevent_FRU_Unknown,
6212 "Received tagged queueing to untagged "
6214 #endif /* NEEDTOPORT */
6216 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6217 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6221 csevent_log(CSC_CTL | CSC_SHELF_SW |
6223 csevent_LogType_Trace,
6224 csevent_Severity_Information,
6225 csevent_AlertLevel_Green,
6226 csevent_FRU_Firmware,
6227 csevent_FRU_Unknown,
6228 "Received tagged queueing to tagged "
6229 "queueing transition");
6230 #endif /* NEEDTOPORT */
6236 * Let other initiators know that the mode
6237 * parameters for this LUN have changed.
6239 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6243 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6246 mtx_unlock(&lun->lun_lock);
6252 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6253 struct ctl_page_index *page_index, uint8_t *page_ptr)
6259 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6260 struct ctl_page_index *page_index, int pc)
6262 struct copan_power_subpage *page;
6264 page = (struct copan_power_subpage *)page_index->page_data +
6265 (page_index->page_len * pc);
6268 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6270 * We don't update the changable bits for this page.
6273 case SMS_PAGE_CTRL_CURRENT >> 6:
6274 case SMS_PAGE_CTRL_DEFAULT >> 6:
6275 case SMS_PAGE_CTRL_SAVED >> 6:
6277 ctl_update_power_subpage(page);
6282 EPRINT(0, "Invalid PC %d!!", pc);
6291 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6292 struct ctl_page_index *page_index, uint8_t *page_ptr)
6294 struct copan_aps_subpage *user_sp;
6295 struct copan_aps_subpage *current_sp;
6296 union ctl_modepage_info *modepage_info;
6297 struct ctl_softc *softc;
6298 struct ctl_lun *lun;
6301 retval = CTL_RETVAL_COMPLETE;
6302 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6303 (page_index->page_len * CTL_PAGE_CURRENT));
6304 softc = control_softc;
6305 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6307 user_sp = (struct copan_aps_subpage *)page_ptr;
6309 modepage_info = (union ctl_modepage_info *)
6310 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6312 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6313 modepage_info->header.subpage = page_index->subpage;
6314 modepage_info->aps.lock_active = user_sp->lock_active;
6316 mtx_lock(&softc->ctl_lock);
6319 * If there is a request to lock the LUN and another LUN is locked
6320 * this is an error. If the requested LUN is already locked ignore
6321 * the request. If no LUN is locked attempt to lock it.
6322 * if there is a request to unlock the LUN and the LUN is currently
6323 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6324 * if another LUN is locked or no LUN is locked.
6326 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6327 if (softc->aps_locked_lun == lun->lun) {
6329 * This LUN is already locked, so we're done.
6331 retval = CTL_RETVAL_COMPLETE;
6332 } else if (softc->aps_locked_lun == 0) {
6334 * No one has the lock, pass the request to the
6337 retval = lun->backend->config_write(
6338 (union ctl_io *)ctsio);
6341 * Someone else has the lock, throw out the request.
6343 ctl_set_already_locked(ctsio);
6344 free(ctsio->kern_data_ptr, M_CTL);
6345 ctl_done((union ctl_io *)ctsio);
6348 * Set the return value so that ctl_do_mode_select()
6349 * won't try to complete the command. We already
6350 * completed it here.
6352 retval = CTL_RETVAL_ERROR;
6354 } else if (softc->aps_locked_lun == lun->lun) {
6356 * This LUN is locked, so pass the unlock request to the
6359 retval = lun->backend->config_write((union ctl_io *)ctsio);
6361 mtx_unlock(&softc->ctl_lock);
6367 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6368 struct ctl_page_index *page_index,
6374 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6379 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6380 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6381 printf("page data:");
6383 printf(" %.2x",page_ptr[i]);
6389 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6390 struct ctl_page_index *page_index,
6393 struct copan_debugconf_subpage *page;
6395 page = (struct copan_debugconf_subpage *)page_index->page_data +
6396 (page_index->page_len * pc);
6399 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6400 case SMS_PAGE_CTRL_DEFAULT >> 6:
6401 case SMS_PAGE_CTRL_SAVED >> 6:
6403 * We don't update the changable or default bits for this page.
6406 case SMS_PAGE_CTRL_CURRENT >> 6:
6407 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6408 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6412 EPRINT(0, "Invalid PC %d!!", pc);
6413 #endif /* NEEDTOPORT */
6421 ctl_do_mode_select(union ctl_io *io)
6423 struct scsi_mode_page_header *page_header;
6424 struct ctl_page_index *page_index;
6425 struct ctl_scsiio *ctsio;
6426 int control_dev, page_len;
6427 int page_len_offset, page_len_size;
6428 union ctl_modepage_info *modepage_info;
6429 struct ctl_lun *lun;
6430 int *len_left, *len_used;
6433 ctsio = &io->scsiio;
6436 retval = CTL_RETVAL_COMPLETE;
6438 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6440 if (lun->be_lun->lun_type != T_DIRECT)
6445 modepage_info = (union ctl_modepage_info *)
6446 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6447 len_left = &modepage_info->header.len_left;
6448 len_used = &modepage_info->header.len_used;
6452 page_header = (struct scsi_mode_page_header *)
6453 (ctsio->kern_data_ptr + *len_used);
6455 if (*len_left == 0) {
6456 free(ctsio->kern_data_ptr, M_CTL);
6457 ctl_set_success(ctsio);
6458 ctl_done((union ctl_io *)ctsio);
6459 return (CTL_RETVAL_COMPLETE);
6460 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6462 free(ctsio->kern_data_ptr, M_CTL);
6463 ctl_set_param_len_error(ctsio);
6464 ctl_done((union ctl_io *)ctsio);
6465 return (CTL_RETVAL_COMPLETE);
6467 } else if ((page_header->page_code & SMPH_SPF)
6468 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6470 free(ctsio->kern_data_ptr, M_CTL);
6471 ctl_set_param_len_error(ctsio);
6472 ctl_done((union ctl_io *)ctsio);
6473 return (CTL_RETVAL_COMPLETE);
6478 * XXX KDM should we do something with the block descriptor?
6480 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6482 if ((control_dev != 0)
6483 && (lun->mode_pages.index[i].page_flags &
6484 CTL_PAGE_FLAG_DISK_ONLY))
6487 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6488 (page_header->page_code & SMPH_PC_MASK))
6492 * If neither page has a subpage code, then we've got a
6495 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6496 && ((page_header->page_code & SMPH_SPF) == 0)) {
6497 page_index = &lun->mode_pages.index[i];
6498 page_len = page_header->page_length;
6503 * If both pages have subpages, then the subpage numbers
6506 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6507 && (page_header->page_code & SMPH_SPF)) {
6508 struct scsi_mode_page_header_sp *sph;
6510 sph = (struct scsi_mode_page_header_sp *)page_header;
6512 if (lun->mode_pages.index[i].subpage ==
6514 page_index = &lun->mode_pages.index[i];
6515 page_len = scsi_2btoul(sph->page_length);
6522 * If we couldn't find the page, or if we don't have a mode select
6523 * handler for it, send back an error to the user.
6525 if ((page_index == NULL)
6526 || (page_index->select_handler == NULL)) {
6527 ctl_set_invalid_field(ctsio,
6530 /*field*/ *len_used,
6533 free(ctsio->kern_data_ptr, M_CTL);
6534 ctl_done((union ctl_io *)ctsio);
6535 return (CTL_RETVAL_COMPLETE);
6538 if (page_index->page_code & SMPH_SPF) {
6539 page_len_offset = 2;
6543 page_len_offset = 1;
6547 * If the length the initiator gives us isn't the one we specify in
6548 * the mode page header, or if they didn't specify enough data in
6549 * the CDB to avoid truncating this page, kick out the request.
6551 if ((page_len != (page_index->page_len - page_len_offset -
6553 || (*len_left < page_index->page_len)) {
6556 ctl_set_invalid_field(ctsio,
6559 /*field*/ *len_used + page_len_offset,
6562 free(ctsio->kern_data_ptr, M_CTL);
6563 ctl_done((union ctl_io *)ctsio);
6564 return (CTL_RETVAL_COMPLETE);
6568 * Run through the mode page, checking to make sure that the bits
6569 * the user changed are actually legal for him to change.
6571 for (i = 0; i < page_index->page_len; i++) {
6572 uint8_t *user_byte, *change_mask, *current_byte;
6576 user_byte = (uint8_t *)page_header + i;
6577 change_mask = page_index->page_data +
6578 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6579 current_byte = page_index->page_data +
6580 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6583 * Check to see whether the user set any bits in this byte
6584 * that he is not allowed to set.
6586 if ((*user_byte & ~(*change_mask)) ==
6587 (*current_byte & ~(*change_mask)))
6591 * Go through bit by bit to determine which one is illegal.
6594 for (j = 7; j >= 0; j--) {
6595 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6596 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6601 ctl_set_invalid_field(ctsio,
6604 /*field*/ *len_used + i,
6607 free(ctsio->kern_data_ptr, M_CTL);
6608 ctl_done((union ctl_io *)ctsio);
6609 return (CTL_RETVAL_COMPLETE);
6613 * Decrement these before we call the page handler, since we may
6614 * end up getting called back one way or another before the handler
6615 * returns to this context.
6617 *len_left -= page_index->page_len;
6618 *len_used += page_index->page_len;
6620 retval = page_index->select_handler(ctsio, page_index,
6621 (uint8_t *)page_header);
6624 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6625 * wait until this queued command completes to finish processing
6626 * the mode page. If it returns anything other than
6627 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6628 * already set the sense information, freed the data pointer, and
6629 * completed the io for us.
6631 if (retval != CTL_RETVAL_COMPLETE)
6632 goto bailout_no_done;
6635 * If the initiator sent us more than one page, parse the next one.
6640 ctl_set_success(ctsio);
6641 free(ctsio->kern_data_ptr, M_CTL);
6642 ctl_done((union ctl_io *)ctsio);
6646 return (CTL_RETVAL_COMPLETE);
6651 ctl_mode_select(struct ctl_scsiio *ctsio)
6653 int param_len, pf, sp;
6654 int header_size, bd_len;
6655 int len_left, len_used;
6656 struct ctl_page_index *page_index;
6657 struct ctl_lun *lun;
6658 int control_dev, page_len;
6659 union ctl_modepage_info *modepage_info;
6671 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6673 if (lun->be_lun->lun_type != T_DIRECT)
6678 switch (ctsio->cdb[0]) {
6679 case MODE_SELECT_6: {
6680 struct scsi_mode_select_6 *cdb;
6682 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6684 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6685 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6687 param_len = cdb->length;
6688 header_size = sizeof(struct scsi_mode_header_6);
6691 case MODE_SELECT_10: {
6692 struct scsi_mode_select_10 *cdb;
6694 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6696 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6697 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6699 param_len = scsi_2btoul(cdb->length);
6700 header_size = sizeof(struct scsi_mode_header_10);
6704 ctl_set_invalid_opcode(ctsio);
6705 ctl_done((union ctl_io *)ctsio);
6706 return (CTL_RETVAL_COMPLETE);
6707 break; /* NOTREACHED */
6712 * "A parameter list length of zero indicates that the Data-Out Buffer
6713 * shall be empty. This condition shall not be considered as an error."
6715 if (param_len == 0) {
6716 ctl_set_success(ctsio);
6717 ctl_done((union ctl_io *)ctsio);
6718 return (CTL_RETVAL_COMPLETE);
6722 * Since we'll hit this the first time through, prior to
6723 * allocation, we don't need to free a data buffer here.
6725 if (param_len < header_size) {
6726 ctl_set_param_len_error(ctsio);
6727 ctl_done((union ctl_io *)ctsio);
6728 return (CTL_RETVAL_COMPLETE);
6732 * Allocate the data buffer and grab the user's data. In theory,
6733 * we shouldn't have to sanity check the parameter list length here
6734 * because the maximum size is 64K. We should be able to malloc
6735 * that much without too many problems.
6737 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6738 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6739 ctsio->kern_data_len = param_len;
6740 ctsio->kern_total_len = param_len;
6741 ctsio->kern_data_resid = 0;
6742 ctsio->kern_rel_offset = 0;
6743 ctsio->kern_sg_entries = 0;
6744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6745 ctsio->be_move_done = ctl_config_move_done;
6746 ctl_datamove((union ctl_io *)ctsio);
6748 return (CTL_RETVAL_COMPLETE);
6751 switch (ctsio->cdb[0]) {
6752 case MODE_SELECT_6: {
6753 struct scsi_mode_header_6 *mh6;
6755 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6756 bd_len = mh6->blk_desc_len;
6759 case MODE_SELECT_10: {
6760 struct scsi_mode_header_10 *mh10;
6762 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6763 bd_len = scsi_2btoul(mh10->blk_desc_len);
6767 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6771 if (param_len < (header_size + bd_len)) {
6772 free(ctsio->kern_data_ptr, M_CTL);
6773 ctl_set_param_len_error(ctsio);
6774 ctl_done((union ctl_io *)ctsio);
6775 return (CTL_RETVAL_COMPLETE);
6779 * Set the IO_CONT flag, so that if this I/O gets passed to
6780 * ctl_config_write_done(), it'll get passed back to
6781 * ctl_do_mode_select() for further processing, or completion if
6784 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6785 ctsio->io_cont = ctl_do_mode_select;
6787 modepage_info = (union ctl_modepage_info *)
6788 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6790 memset(modepage_info, 0, sizeof(*modepage_info));
6792 len_left = param_len - header_size - bd_len;
6793 len_used = header_size + bd_len;
6795 modepage_info->header.len_left = len_left;
6796 modepage_info->header.len_used = len_used;
6798 return (ctl_do_mode_select((union ctl_io *)ctsio));
6802 ctl_mode_sense(struct ctl_scsiio *ctsio)
6804 struct ctl_lun *lun;
6805 int pc, page_code, dbd, llba, subpage;
6806 int alloc_len, page_len, header_len, total_len;
6807 struct scsi_mode_block_descr *block_desc;
6808 struct ctl_page_index *page_index;
6816 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6820 if (lun->be_lun->lun_type != T_DIRECT)
6825 if (lun->flags & CTL_LUN_PR_RESERVED) {
6829 * XXX KDM need a lock here.
6831 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6832 if ((lun->res_type == SPR_TYPE_EX_AC
6833 && residx != lun->pr_res_idx)
6834 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6835 || lun->res_type == SPR_TYPE_EX_AC_AR)
6836 && !lun->per_res[residx].registered)) {
6837 ctl_set_reservation_conflict(ctsio);
6838 ctl_done((union ctl_io *)ctsio);
6839 return (CTL_RETVAL_COMPLETE);
6843 switch (ctsio->cdb[0]) {
6844 case MODE_SENSE_6: {
6845 struct scsi_mode_sense_6 *cdb;
6847 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6849 header_len = sizeof(struct scsi_mode_hdr_6);
6850 if (cdb->byte2 & SMS_DBD)
6853 header_len += sizeof(struct scsi_mode_block_descr);
6855 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6856 page_code = cdb->page & SMS_PAGE_CODE;
6857 subpage = cdb->subpage;
6858 alloc_len = cdb->length;
6861 case MODE_SENSE_10: {
6862 struct scsi_mode_sense_10 *cdb;
6864 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6866 header_len = sizeof(struct scsi_mode_hdr_10);
6868 if (cdb->byte2 & SMS_DBD)
6871 header_len += sizeof(struct scsi_mode_block_descr);
6872 if (cdb->byte2 & SMS10_LLBAA)
6874 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6875 page_code = cdb->page & SMS_PAGE_CODE;
6876 subpage = cdb->subpage;
6877 alloc_len = scsi_2btoul(cdb->length);
6881 ctl_set_invalid_opcode(ctsio);
6882 ctl_done((union ctl_io *)ctsio);
6883 return (CTL_RETVAL_COMPLETE);
6884 break; /* NOTREACHED */
6888 * We have to make a first pass through to calculate the size of
6889 * the pages that match the user's query. Then we allocate enough
6890 * memory to hold it, and actually copy the data into the buffer.
6892 switch (page_code) {
6893 case SMS_ALL_PAGES_PAGE: {
6899 * At the moment, values other than 0 and 0xff here are
6900 * reserved according to SPC-3.
6902 if ((subpage != SMS_SUBPAGE_PAGE_0)
6903 && (subpage != SMS_SUBPAGE_ALL)) {
6904 ctl_set_invalid_field(ctsio,
6910 ctl_done((union ctl_io *)ctsio);
6911 return (CTL_RETVAL_COMPLETE);
6914 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6915 if ((control_dev != 0)
6916 && (lun->mode_pages.index[i].page_flags &
6917 CTL_PAGE_FLAG_DISK_ONLY))
6921 * We don't use this subpage if the user didn't
6922 * request all subpages.
6924 if ((lun->mode_pages.index[i].subpage != 0)
6925 && (subpage == SMS_SUBPAGE_PAGE_0))
6929 printf("found page %#x len %d\n",
6930 lun->mode_pages.index[i].page_code &
6932 lun->mode_pages.index[i].page_len);
6934 page_len += lun->mode_pages.index[i].page_len;
6943 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6944 /* Look for the right page code */
6945 if ((lun->mode_pages.index[i].page_code &
6946 SMPH_PC_MASK) != page_code)
6949 /* Look for the right subpage or the subpage wildcard*/
6950 if ((lun->mode_pages.index[i].subpage != subpage)
6951 && (subpage != SMS_SUBPAGE_ALL))
6954 /* Make sure the page is supported for this dev type */
6955 if ((control_dev != 0)
6956 && (lun->mode_pages.index[i].page_flags &
6957 CTL_PAGE_FLAG_DISK_ONLY))
6961 printf("found page %#x len %d\n",
6962 lun->mode_pages.index[i].page_code &
6964 lun->mode_pages.index[i].page_len);
6967 page_len += lun->mode_pages.index[i].page_len;
6970 if (page_len == 0) {
6971 ctl_set_invalid_field(ctsio,
6977 ctl_done((union ctl_io *)ctsio);
6978 return (CTL_RETVAL_COMPLETE);
6984 total_len = header_len + page_len;
6986 printf("header_len = %d, page_len = %d, total_len = %d\n",
6987 header_len, page_len, total_len);
6990 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6991 ctsio->kern_sg_entries = 0;
6992 ctsio->kern_data_resid = 0;
6993 ctsio->kern_rel_offset = 0;
6994 if (total_len < alloc_len) {
6995 ctsio->residual = alloc_len - total_len;
6996 ctsio->kern_data_len = total_len;
6997 ctsio->kern_total_len = total_len;
6999 ctsio->residual = 0;
7000 ctsio->kern_data_len = alloc_len;
7001 ctsio->kern_total_len = alloc_len;
7004 switch (ctsio->cdb[0]) {
7005 case MODE_SENSE_6: {
7006 struct scsi_mode_hdr_6 *header;
7008 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
7010 header->datalen = ctl_min(total_len - 1, 254);
7013 header->block_descr_len = 0;
7015 header->block_descr_len =
7016 sizeof(struct scsi_mode_block_descr);
7017 block_desc = (struct scsi_mode_block_descr *)&header[1];
7020 case MODE_SENSE_10: {
7021 struct scsi_mode_hdr_10 *header;
7024 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7026 datalen = ctl_min(total_len - 2, 65533);
7027 scsi_ulto2b(datalen, header->datalen);
7029 scsi_ulto2b(0, header->block_descr_len);
7031 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7032 header->block_descr_len);
7033 block_desc = (struct scsi_mode_block_descr *)&header[1];
7037 panic("invalid CDB type %#x", ctsio->cdb[0]);
7038 break; /* NOTREACHED */
7042 * If we've got a disk, use its blocksize in the block
7043 * descriptor. Otherwise, just set it to 0.
7046 if (control_dev != 0)
7047 scsi_ulto3b(lun->be_lun->blocksize,
7048 block_desc->block_len);
7050 scsi_ulto3b(0, block_desc->block_len);
7053 switch (page_code) {
7054 case SMS_ALL_PAGES_PAGE: {
7057 data_used = header_len;
7058 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7059 struct ctl_page_index *page_index;
7061 page_index = &lun->mode_pages.index[i];
7063 if ((control_dev != 0)
7064 && (page_index->page_flags &
7065 CTL_PAGE_FLAG_DISK_ONLY))
7069 * We don't use this subpage if the user didn't
7070 * request all subpages. We already checked (above)
7071 * to make sure the user only specified a subpage
7072 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7074 if ((page_index->subpage != 0)
7075 && (subpage == SMS_SUBPAGE_PAGE_0))
7079 * Call the handler, if it exists, to update the
7080 * page to the latest values.
7082 if (page_index->sense_handler != NULL)
7083 page_index->sense_handler(ctsio, page_index,pc);
7085 memcpy(ctsio->kern_data_ptr + data_used,
7086 page_index->page_data +
7087 (page_index->page_len * pc),
7088 page_index->page_len);
7089 data_used += page_index->page_len;
7096 data_used = header_len;
7098 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7099 struct ctl_page_index *page_index;
7101 page_index = &lun->mode_pages.index[i];
7103 /* Look for the right page code */
7104 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7107 /* Look for the right subpage or the subpage wildcard*/
7108 if ((page_index->subpage != subpage)
7109 && (subpage != SMS_SUBPAGE_ALL))
7112 /* Make sure the page is supported for this dev type */
7113 if ((control_dev != 0)
7114 && (page_index->page_flags &
7115 CTL_PAGE_FLAG_DISK_ONLY))
7119 * Call the handler, if it exists, to update the
7120 * page to the latest values.
7122 if (page_index->sense_handler != NULL)
7123 page_index->sense_handler(ctsio, page_index,pc);
7125 memcpy(ctsio->kern_data_ptr + data_used,
7126 page_index->page_data +
7127 (page_index->page_len * pc),
7128 page_index->page_len);
7129 data_used += page_index->page_len;
7135 ctsio->scsi_status = SCSI_STATUS_OK;
7137 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7138 ctsio->be_move_done = ctl_config_move_done;
7139 ctl_datamove((union ctl_io *)ctsio);
7141 return (CTL_RETVAL_COMPLETE);
7145 ctl_read_capacity(struct ctl_scsiio *ctsio)
7147 struct scsi_read_capacity *cdb;
7148 struct scsi_read_capacity_data *data;
7149 struct ctl_lun *lun;
7152 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7154 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7156 lba = scsi_4btoul(cdb->addr);
7157 if (((cdb->pmi & SRC_PMI) == 0)
7159 ctl_set_invalid_field(/*ctsio*/ ctsio,
7165 ctl_done((union ctl_io *)ctsio);
7166 return (CTL_RETVAL_COMPLETE);
7169 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7171 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7172 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7173 ctsio->residual = 0;
7174 ctsio->kern_data_len = sizeof(*data);
7175 ctsio->kern_total_len = sizeof(*data);
7176 ctsio->kern_data_resid = 0;
7177 ctsio->kern_rel_offset = 0;
7178 ctsio->kern_sg_entries = 0;
7181 * If the maximum LBA is greater than 0xfffffffe, the user must
7182 * issue a SERVICE ACTION IN (16) command, with the read capacity
7183 * serivce action set.
7185 if (lun->be_lun->maxlba > 0xfffffffe)
7186 scsi_ulto4b(0xffffffff, data->addr);
7188 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7191 * XXX KDM this may not be 512 bytes...
7193 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7195 ctsio->scsi_status = SCSI_STATUS_OK;
7197 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7198 ctsio->be_move_done = ctl_config_move_done;
7199 ctl_datamove((union ctl_io *)ctsio);
7201 return (CTL_RETVAL_COMPLETE);
7205 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7207 struct scsi_read_capacity_16 *cdb;
7208 struct scsi_read_capacity_data_long *data;
7209 struct ctl_lun *lun;
7213 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7215 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7217 alloc_len = scsi_4btoul(cdb->alloc_len);
7218 lba = scsi_8btou64(cdb->addr);
7220 if ((cdb->reladr & SRC16_PMI)
7222 ctl_set_invalid_field(/*ctsio*/ ctsio,
7228 ctl_done((union ctl_io *)ctsio);
7229 return (CTL_RETVAL_COMPLETE);
7232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7234 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7235 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7237 if (sizeof(*data) < alloc_len) {
7238 ctsio->residual = alloc_len - sizeof(*data);
7239 ctsio->kern_data_len = sizeof(*data);
7240 ctsio->kern_total_len = sizeof(*data);
7242 ctsio->residual = 0;
7243 ctsio->kern_data_len = alloc_len;
7244 ctsio->kern_total_len = alloc_len;
7246 ctsio->kern_data_resid = 0;
7247 ctsio->kern_rel_offset = 0;
7248 ctsio->kern_sg_entries = 0;
7250 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7251 /* XXX KDM this may not be 512 bytes... */
7252 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7253 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7254 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7255 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7256 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7258 ctsio->scsi_status = SCSI_STATUS_OK;
7260 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7261 ctsio->be_move_done = ctl_config_move_done;
7262 ctl_datamove((union ctl_io *)ctsio);
7264 return (CTL_RETVAL_COMPLETE);
7268 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7270 struct scsi_maintenance_in *cdb;
7272 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7273 int num_target_port_groups, num_target_ports, single;
7274 struct ctl_lun *lun;
7275 struct ctl_softc *softc;
7276 struct ctl_port *port;
7277 struct scsi_target_group_data *rtg_ptr;
7278 struct scsi_target_group_data_extended *rtg_ext_ptr;
7279 struct scsi_target_port_group_descriptor *tpg_desc;
7281 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7283 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7284 softc = control_softc;
7285 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7287 retval = CTL_RETVAL_COMPLETE;
7289 switch (cdb->byte2 & STG_PDF_MASK) {
7290 case STG_PDF_LENGTH:
7293 case STG_PDF_EXTENDED:
7297 ctl_set_invalid_field(/*ctsio*/ ctsio,
7303 ctl_done((union ctl_io *)ctsio);
7307 single = ctl_is_single;
7309 num_target_port_groups = 1;
7311 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7312 num_target_ports = 0;
7313 mtx_lock(&softc->ctl_lock);
7314 STAILQ_FOREACH(port, &softc->port_list, links) {
7315 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7317 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7321 mtx_unlock(&softc->ctl_lock);
7324 total_len = sizeof(struct scsi_target_group_data_extended);
7326 total_len = sizeof(struct scsi_target_group_data);
7327 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7328 num_target_port_groups +
7329 sizeof(struct scsi_target_port_descriptor) *
7330 num_target_ports * num_target_port_groups;
7332 alloc_len = scsi_4btoul(cdb->length);
7334 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7336 ctsio->kern_sg_entries = 0;
7338 if (total_len < alloc_len) {
7339 ctsio->residual = alloc_len - total_len;
7340 ctsio->kern_data_len = total_len;
7341 ctsio->kern_total_len = total_len;
7343 ctsio->residual = 0;
7344 ctsio->kern_data_len = alloc_len;
7345 ctsio->kern_total_len = alloc_len;
7347 ctsio->kern_data_resid = 0;
7348 ctsio->kern_rel_offset = 0;
7351 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7352 ctsio->kern_data_ptr;
7353 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7354 rtg_ext_ptr->format_type = 0x10;
7355 rtg_ext_ptr->implicit_transition_time = 0;
7356 tpg_desc = &rtg_ext_ptr->groups[0];
7358 rtg_ptr = (struct scsi_target_group_data *)
7359 ctsio->kern_data_ptr;
7360 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7361 tpg_desc = &rtg_ptr->groups[0];
7364 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7365 mtx_lock(&softc->ctl_lock);
7366 for (g = 0; g < num_target_port_groups; g++) {
7368 tpg_desc->pref_state = TPG_PRIMARY |
7369 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7371 tpg_desc->pref_state =
7372 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7373 tpg_desc->support = TPG_AO_SUP;
7375 tpg_desc->support |= TPG_AN_SUP;
7376 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7377 tpg_desc->status = TPG_IMPLICIT;
7379 STAILQ_FOREACH(port, &softc->port_list, links) {
7380 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7382 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7385 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7386 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7387 relative_target_port_identifier);
7390 tpg_desc->target_port_count = pc;
7391 tpg_desc = (struct scsi_target_port_group_descriptor *)
7392 &tpg_desc->descriptors[pc];
7394 mtx_unlock(&softc->ctl_lock);
7396 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7397 ctsio->be_move_done = ctl_config_move_done;
7399 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7400 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7401 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7402 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7403 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7405 ctl_datamove((union ctl_io *)ctsio);
7410 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7412 struct ctl_lun *lun;
7413 struct scsi_report_supported_opcodes *cdb;
7414 const struct ctl_cmd_entry *entry, *sentry;
7415 struct scsi_report_supported_opcodes_all *all;
7416 struct scsi_report_supported_opcodes_descr *descr;
7417 struct scsi_report_supported_opcodes_one *one;
7419 int alloc_len, total_len;
7420 int opcode, service_action, i, j, num;
7422 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7424 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7425 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7427 retval = CTL_RETVAL_COMPLETE;
7429 opcode = cdb->requested_opcode;
7430 service_action = scsi_2btoul(cdb->requested_service_action);
7431 switch (cdb->options & RSO_OPTIONS_MASK) {
7432 case RSO_OPTIONS_ALL:
7434 for (i = 0; i < 256; i++) {
7435 entry = &ctl_cmd_table[i];
7436 if (entry->flags & CTL_CMD_FLAG_SA5) {
7437 for (j = 0; j < 32; j++) {
7438 sentry = &((const struct ctl_cmd_entry *)
7440 if (ctl_cmd_applicable(
7441 lun->be_lun->lun_type, sentry))
7445 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7450 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7451 num * sizeof(struct scsi_report_supported_opcodes_descr);
7453 case RSO_OPTIONS_OC:
7454 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7455 ctl_set_invalid_field(/*ctsio*/ ctsio,
7461 ctl_done((union ctl_io *)ctsio);
7462 return (CTL_RETVAL_COMPLETE);
7464 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7466 case RSO_OPTIONS_OC_SA:
7467 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7468 service_action >= 32) {
7469 ctl_set_invalid_field(/*ctsio*/ ctsio,
7475 ctl_done((union ctl_io *)ctsio);
7476 return (CTL_RETVAL_COMPLETE);
7478 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7481 ctl_set_invalid_field(/*ctsio*/ ctsio,
7487 ctl_done((union ctl_io *)ctsio);
7488 return (CTL_RETVAL_COMPLETE);
7491 alloc_len = scsi_4btoul(cdb->length);
7493 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7495 ctsio->kern_sg_entries = 0;
7497 if (total_len < alloc_len) {
7498 ctsio->residual = alloc_len - total_len;
7499 ctsio->kern_data_len = total_len;
7500 ctsio->kern_total_len = total_len;
7502 ctsio->residual = 0;
7503 ctsio->kern_data_len = alloc_len;
7504 ctsio->kern_total_len = alloc_len;
7506 ctsio->kern_data_resid = 0;
7507 ctsio->kern_rel_offset = 0;
7509 switch (cdb->options & RSO_OPTIONS_MASK) {
7510 case RSO_OPTIONS_ALL:
7511 all = (struct scsi_report_supported_opcodes_all *)
7512 ctsio->kern_data_ptr;
7514 for (i = 0; i < 256; i++) {
7515 entry = &ctl_cmd_table[i];
7516 if (entry->flags & CTL_CMD_FLAG_SA5) {
7517 for (j = 0; j < 32; j++) {
7518 sentry = &((const struct ctl_cmd_entry *)
7520 if (!ctl_cmd_applicable(
7521 lun->be_lun->lun_type, sentry))
7523 descr = &all->descr[num++];
7525 scsi_ulto2b(j, descr->service_action);
7526 descr->flags = RSO_SERVACTV;
7527 scsi_ulto2b(sentry->length,
7531 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7534 descr = &all->descr[num++];
7536 scsi_ulto2b(0, descr->service_action);
7538 scsi_ulto2b(entry->length, descr->cdb_length);
7542 num * sizeof(struct scsi_report_supported_opcodes_descr),
7545 case RSO_OPTIONS_OC:
7546 one = (struct scsi_report_supported_opcodes_one *)
7547 ctsio->kern_data_ptr;
7548 entry = &ctl_cmd_table[opcode];
7550 case RSO_OPTIONS_OC_SA:
7551 one = (struct scsi_report_supported_opcodes_one *)
7552 ctsio->kern_data_ptr;
7553 entry = &ctl_cmd_table[opcode];
7554 entry = &((const struct ctl_cmd_entry *)
7555 entry->execute)[service_action];
7557 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7559 scsi_ulto2b(entry->length, one->cdb_length);
7560 one->cdb_usage[0] = opcode;
7561 memcpy(&one->cdb_usage[1], entry->usage,
7568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7569 ctsio->be_move_done = ctl_config_move_done;
7571 ctl_datamove((union ctl_io *)ctsio);
7576 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7578 struct ctl_lun *lun;
7579 struct scsi_report_supported_tmf *cdb;
7580 struct scsi_report_supported_tmf_data *data;
7582 int alloc_len, total_len;
7584 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7586 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7589 retval = CTL_RETVAL_COMPLETE;
7591 total_len = sizeof(struct scsi_report_supported_tmf_data);
7592 alloc_len = scsi_4btoul(cdb->length);
7594 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7596 ctsio->kern_sg_entries = 0;
7598 if (total_len < alloc_len) {
7599 ctsio->residual = alloc_len - total_len;
7600 ctsio->kern_data_len = total_len;
7601 ctsio->kern_total_len = total_len;
7603 ctsio->residual = 0;
7604 ctsio->kern_data_len = alloc_len;
7605 ctsio->kern_total_len = alloc_len;
7607 ctsio->kern_data_resid = 0;
7608 ctsio->kern_rel_offset = 0;
7610 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7611 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7612 data->byte2 |= RST_ITNRS;
7614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7615 ctsio->be_move_done = ctl_config_move_done;
7617 ctl_datamove((union ctl_io *)ctsio);
7622 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7624 struct ctl_lun *lun;
7625 struct scsi_report_timestamp *cdb;
7626 struct scsi_report_timestamp_data *data;
7630 int alloc_len, total_len;
7632 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7634 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7637 retval = CTL_RETVAL_COMPLETE;
7639 total_len = sizeof(struct scsi_report_timestamp_data);
7640 alloc_len = scsi_4btoul(cdb->length);
7642 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7644 ctsio->kern_sg_entries = 0;
7646 if (total_len < alloc_len) {
7647 ctsio->residual = alloc_len - total_len;
7648 ctsio->kern_data_len = total_len;
7649 ctsio->kern_total_len = total_len;
7651 ctsio->residual = 0;
7652 ctsio->kern_data_len = alloc_len;
7653 ctsio->kern_total_len = alloc_len;
7655 ctsio->kern_data_resid = 0;
7656 ctsio->kern_rel_offset = 0;
7658 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7659 scsi_ulto2b(sizeof(*data) - 2, data->length);
7660 data->origin = RTS_ORIG_OUTSIDE;
7662 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7663 scsi_ulto4b(timestamp >> 16, data->timestamp);
7664 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7667 ctsio->be_move_done = ctl_config_move_done;
7669 ctl_datamove((union ctl_io *)ctsio);
7674 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7676 struct scsi_per_res_in *cdb;
7677 int alloc_len, total_len = 0;
7678 /* struct scsi_per_res_in_rsrv in_data; */
7679 struct ctl_lun *lun;
7680 struct ctl_softc *softc;
7682 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7684 softc = control_softc;
7686 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7688 alloc_len = scsi_2btoul(cdb->length);
7690 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7693 mtx_lock(&lun->lun_lock);
7694 switch (cdb->action) {
7695 case SPRI_RK: /* read keys */
7696 total_len = sizeof(struct scsi_per_res_in_keys) +
7698 sizeof(struct scsi_per_res_key);
7700 case SPRI_RR: /* read reservation */
7701 if (lun->flags & CTL_LUN_PR_RESERVED)
7702 total_len = sizeof(struct scsi_per_res_in_rsrv);
7704 total_len = sizeof(struct scsi_per_res_in_header);
7706 case SPRI_RC: /* report capabilities */
7707 total_len = sizeof(struct scsi_per_res_cap);
7709 case SPRI_RS: /* read full status */
7710 total_len = sizeof(struct scsi_per_res_in_header) +
7711 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7715 panic("Invalid PR type %x", cdb->action);
7717 mtx_unlock(&lun->lun_lock);
7719 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7721 if (total_len < alloc_len) {
7722 ctsio->residual = alloc_len - total_len;
7723 ctsio->kern_data_len = total_len;
7724 ctsio->kern_total_len = total_len;
7726 ctsio->residual = 0;
7727 ctsio->kern_data_len = alloc_len;
7728 ctsio->kern_total_len = alloc_len;
7731 ctsio->kern_data_resid = 0;
7732 ctsio->kern_rel_offset = 0;
7733 ctsio->kern_sg_entries = 0;
7735 mtx_lock(&lun->lun_lock);
7736 switch (cdb->action) {
7737 case SPRI_RK: { // read keys
7738 struct scsi_per_res_in_keys *res_keys;
7741 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7744 * We had to drop the lock to allocate our buffer, which
7745 * leaves time for someone to come in with another
7746 * persistent reservation. (That is unlikely, though,
7747 * since this should be the only persistent reservation
7748 * command active right now.)
7750 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7751 (lun->pr_key_count *
7752 sizeof(struct scsi_per_res_key)))){
7753 mtx_unlock(&lun->lun_lock);
7754 free(ctsio->kern_data_ptr, M_CTL);
7755 printf("%s: reservation length changed, retrying\n",
7760 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7762 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7763 lun->pr_key_count, res_keys->header.length);
7765 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7766 if (!lun->per_res[i].registered)
7770 * We used lun->pr_key_count to calculate the
7771 * size to allocate. If it turns out the number of
7772 * initiators with the registered flag set is
7773 * larger than that (i.e. they haven't been kept in
7774 * sync), we've got a problem.
7776 if (key_count >= lun->pr_key_count) {
7778 csevent_log(CSC_CTL | CSC_SHELF_SW |
7780 csevent_LogType_Fault,
7781 csevent_AlertLevel_Yellow,
7782 csevent_FRU_ShelfController,
7783 csevent_FRU_Firmware,
7784 csevent_FRU_Unknown,
7785 "registered keys %d >= key "
7786 "count %d", key_count,
7792 memcpy(res_keys->keys[key_count].key,
7793 lun->per_res[i].res_key.key,
7794 ctl_min(sizeof(res_keys->keys[key_count].key),
7795 sizeof(lun->per_res[i].res_key)));
7800 case SPRI_RR: { // read reservation
7801 struct scsi_per_res_in_rsrv *res;
7802 int tmp_len, header_only;
7804 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7806 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7808 if (lun->flags & CTL_LUN_PR_RESERVED)
7810 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7811 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7812 res->header.length);
7815 tmp_len = sizeof(struct scsi_per_res_in_header);
7816 scsi_ulto4b(0, res->header.length);
7821 * We had to drop the lock to allocate our buffer, which
7822 * leaves time for someone to come in with another
7823 * persistent reservation. (That is unlikely, though,
7824 * since this should be the only persistent reservation
7825 * command active right now.)
7827 if (tmp_len != total_len) {
7828 mtx_unlock(&lun->lun_lock);
7829 free(ctsio->kern_data_ptr, M_CTL);
7830 printf("%s: reservation status changed, retrying\n",
7836 * No reservation held, so we're done.
7838 if (header_only != 0)
7842 * If the registration is an All Registrants type, the key
7843 * is 0, since it doesn't really matter.
7845 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7846 memcpy(res->data.reservation,
7847 &lun->per_res[lun->pr_res_idx].res_key,
7848 sizeof(struct scsi_per_res_key));
7850 res->data.scopetype = lun->res_type;
7853 case SPRI_RC: //report capabilities
7855 struct scsi_per_res_cap *res_cap;
7858 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7859 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7860 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7861 type_mask = SPRI_TM_WR_EX_AR |
7867 scsi_ulto2b(type_mask, res_cap->type_mask);
7870 case SPRI_RS: { // read full status
7871 struct scsi_per_res_in_full *res_status;
7872 struct scsi_per_res_in_full_desc *res_desc;
7873 struct ctl_port *port;
7876 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7879 * We had to drop the lock to allocate our buffer, which
7880 * leaves time for someone to come in with another
7881 * persistent reservation. (That is unlikely, though,
7882 * since this should be the only persistent reservation
7883 * command active right now.)
7885 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7886 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7887 lun->pr_key_count)){
7888 mtx_unlock(&lun->lun_lock);
7889 free(ctsio->kern_data_ptr, M_CTL);
7890 printf("%s: reservation length changed, retrying\n",
7895 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7897 res_desc = &res_status->desc[0];
7898 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7899 if (!lun->per_res[i].registered)
7902 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key,
7903 sizeof(res_desc->res_key));
7904 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7905 (lun->pr_res_idx == i ||
7906 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7907 res_desc->flags = SPRI_FULL_R_HOLDER;
7908 res_desc->scopetype = lun->res_type;
7910 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7911 res_desc->rel_trgt_port_id);
7913 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7915 len = ctl_create_iid(port,
7916 i % CTL_MAX_INIT_PER_PORT,
7917 res_desc->transport_id);
7918 scsi_ulto4b(len, res_desc->additional_length);
7919 res_desc = (struct scsi_per_res_in_full_desc *)
7920 &res_desc->transport_id[len];
7922 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7923 res_status->header.length);
7928 * This is a bug, because we just checked for this above,
7929 * and should have returned an error.
7931 panic("Invalid PR type %x", cdb->action);
7932 break; /* NOTREACHED */
7934 mtx_unlock(&lun->lun_lock);
7936 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7937 ctsio->be_move_done = ctl_config_move_done;
7939 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7940 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7941 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7942 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7943 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7945 ctl_datamove((union ctl_io *)ctsio);
7947 return (CTL_RETVAL_COMPLETE);
7951 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7955 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7956 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7957 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7958 struct scsi_per_res_out_parms* param)
7960 union ctl_ha_msg persis_io;
7966 mtx_lock(&lun->lun_lock);
7967 if (sa_res_key == 0) {
7968 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7969 /* validate scope and type */
7970 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7972 mtx_unlock(&lun->lun_lock);
7973 ctl_set_invalid_field(/*ctsio*/ ctsio,
7979 ctl_done((union ctl_io *)ctsio);
7983 if (type>8 || type==2 || type==4 || type==0) {
7984 mtx_unlock(&lun->lun_lock);
7985 ctl_set_invalid_field(/*ctsio*/ ctsio,
7991 ctl_done((union ctl_io *)ctsio);
7995 /* temporarily unregister this nexus */
7996 lun->per_res[residx].registered = 0;
7999 * Unregister everybody else and build UA for
8002 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8003 if (lun->per_res[i].registered == 0)
8007 && i <CTL_MAX_INITIATORS)
8008 lun->pending_ua[i] |=
8010 else if (persis_offset
8011 && i >= persis_offset)
8012 lun->pending_ua[i-persis_offset] |=
8014 lun->per_res[i].registered = 0;
8015 memset(&lun->per_res[i].res_key, 0,
8016 sizeof(struct scsi_per_res_key));
8018 lun->per_res[residx].registered = 1;
8019 lun->pr_key_count = 1;
8020 lun->res_type = type;
8021 if (lun->res_type != SPR_TYPE_WR_EX_AR
8022 && lun->res_type != SPR_TYPE_EX_AC_AR)
8023 lun->pr_res_idx = residx;
8025 /* send msg to other side */
8026 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8027 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8028 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8029 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8030 persis_io.pr.pr_info.res_type = type;
8031 memcpy(persis_io.pr.pr_info.sa_res_key,
8032 param->serv_act_res_key,
8033 sizeof(param->serv_act_res_key));
8034 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8035 &persis_io, sizeof(persis_io), 0)) >
8036 CTL_HA_STATUS_SUCCESS) {
8037 printf("CTL:Persis Out error returned "
8038 "from ctl_ha_msg_send %d\n",
8042 /* not all registrants */
8043 mtx_unlock(&lun->lun_lock);
8044 free(ctsio->kern_data_ptr, M_CTL);
8045 ctl_set_invalid_field(ctsio,
8051 ctl_done((union ctl_io *)ctsio);
8054 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8055 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8058 if (res_key == sa_res_key) {
8061 * The spec implies this is not good but doesn't
8062 * say what to do. There are two choices either
8063 * generate a res conflict or check condition
8064 * with illegal field in parameter data. Since
8065 * that is what is done when the sa_res_key is
8066 * zero I'll take that approach since this has
8067 * to do with the sa_res_key.
8069 mtx_unlock(&lun->lun_lock);
8070 free(ctsio->kern_data_ptr, M_CTL);
8071 ctl_set_invalid_field(ctsio,
8077 ctl_done((union ctl_io *)ctsio);
8081 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8082 if (lun->per_res[i].registered
8083 && memcmp(param->serv_act_res_key,
8084 lun->per_res[i].res_key.key,
8085 sizeof(struct scsi_per_res_key)) != 0)
8089 lun->per_res[i].registered = 0;
8090 memset(&lun->per_res[i].res_key, 0,
8091 sizeof(struct scsi_per_res_key));
8092 lun->pr_key_count--;
8094 if (!persis_offset && i < CTL_MAX_INITIATORS)
8095 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8096 else if (persis_offset && i >= persis_offset)
8097 lun->pending_ua[i-persis_offset] |=
8101 mtx_unlock(&lun->lun_lock);
8102 free(ctsio->kern_data_ptr, M_CTL);
8103 ctl_set_reservation_conflict(ctsio);
8104 ctl_done((union ctl_io *)ctsio);
8105 return (CTL_RETVAL_COMPLETE);
8107 /* send msg to other side */
8108 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8109 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8110 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8111 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8112 persis_io.pr.pr_info.res_type = type;
8113 memcpy(persis_io.pr.pr_info.sa_res_key,
8114 param->serv_act_res_key,
8115 sizeof(param->serv_act_res_key));
8116 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8117 &persis_io, sizeof(persis_io), 0)) >
8118 CTL_HA_STATUS_SUCCESS) {
8119 printf("CTL:Persis Out error returned from "
8120 "ctl_ha_msg_send %d\n", isc_retval);
8123 /* Reserved but not all registrants */
8124 /* sa_res_key is res holder */
8125 if (memcmp(param->serv_act_res_key,
8126 lun->per_res[lun->pr_res_idx].res_key.key,
8127 sizeof(struct scsi_per_res_key)) == 0) {
8128 /* validate scope and type */
8129 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8131 mtx_unlock(&lun->lun_lock);
8132 ctl_set_invalid_field(/*ctsio*/ ctsio,
8138 ctl_done((union ctl_io *)ctsio);
8142 if (type>8 || type==2 || type==4 || type==0) {
8143 mtx_unlock(&lun->lun_lock);
8144 ctl_set_invalid_field(/*ctsio*/ ctsio,
8150 ctl_done((union ctl_io *)ctsio);
8156 * if sa_res_key != res_key remove all
8157 * registrants w/sa_res_key and generate UA
8158 * for these registrants(Registrations
8159 * Preempted) if it wasn't an exclusive
8160 * reservation generate UA(Reservations
8161 * Preempted) for all other registered nexuses
8162 * if the type has changed. Establish the new
8163 * reservation and holder. If res_key and
8164 * sa_res_key are the same do the above
8165 * except don't unregister the res holder.
8169 * Temporarily unregister so it won't get
8170 * removed or UA generated
8172 lun->per_res[residx].registered = 0;
8173 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8174 if (lun->per_res[i].registered == 0)
8177 if (memcmp(param->serv_act_res_key,
8178 lun->per_res[i].res_key.key,
8179 sizeof(struct scsi_per_res_key)) == 0) {
8180 lun->per_res[i].registered = 0;
8181 memset(&lun->per_res[i].res_key,
8183 sizeof(struct scsi_per_res_key));
8184 lun->pr_key_count--;
8187 && i < CTL_MAX_INITIATORS)
8188 lun->pending_ua[i] |=
8190 else if (persis_offset
8191 && i >= persis_offset)
8192 lun->pending_ua[i-persis_offset] |=
8194 } else if (type != lun->res_type
8195 && (lun->res_type == SPR_TYPE_WR_EX_RO
8196 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8198 && i < CTL_MAX_INITIATORS)
8199 lun->pending_ua[i] |=
8201 else if (persis_offset
8202 && i >= persis_offset)
8208 lun->per_res[residx].registered = 1;
8209 lun->res_type = type;
8210 if (lun->res_type != SPR_TYPE_WR_EX_AR
8211 && lun->res_type != SPR_TYPE_EX_AC_AR)
8212 lun->pr_res_idx = residx;
8214 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8216 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8217 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8218 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8219 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8220 persis_io.pr.pr_info.res_type = type;
8221 memcpy(persis_io.pr.pr_info.sa_res_key,
8222 param->serv_act_res_key,
8223 sizeof(param->serv_act_res_key));
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 "
8228 "from ctl_ha_msg_send %d\n",
8233 * sa_res_key is not the res holder just
8234 * remove registrants
8238 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8239 if (memcmp(param->serv_act_res_key,
8240 lun->per_res[i].res_key.key,
8241 sizeof(struct scsi_per_res_key)) != 0)
8245 lun->per_res[i].registered = 0;
8246 memset(&lun->per_res[i].res_key, 0,
8247 sizeof(struct scsi_per_res_key));
8248 lun->pr_key_count--;
8251 && i < CTL_MAX_INITIATORS)
8252 lun->pending_ua[i] |=
8254 else if (persis_offset
8255 && i >= persis_offset)
8256 lun->pending_ua[i-persis_offset] |=
8261 mtx_unlock(&lun->lun_lock);
8262 free(ctsio->kern_data_ptr, M_CTL);
8263 ctl_set_reservation_conflict(ctsio);
8264 ctl_done((union ctl_io *)ctsio);
8267 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8268 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8269 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8270 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8271 persis_io.pr.pr_info.res_type = type;
8272 memcpy(persis_io.pr.pr_info.sa_res_key,
8273 param->serv_act_res_key,
8274 sizeof(param->serv_act_res_key));
8275 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8276 &persis_io, sizeof(persis_io), 0)) >
8277 CTL_HA_STATUS_SUCCESS) {
8278 printf("CTL:Persis Out error returned "
8279 "from ctl_ha_msg_send %d\n",
8285 lun->PRGeneration++;
8286 mtx_unlock(&lun->lun_lock);
8292 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8296 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8297 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8298 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8299 msg->pr.pr_info.sa_res_key,
8300 sizeof(struct scsi_per_res_key)) != 0) {
8301 uint64_t sa_res_key;
8302 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8304 if (sa_res_key == 0) {
8305 /* temporarily unregister this nexus */
8306 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8309 * Unregister everybody else and build UA for
8312 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8313 if (lun->per_res[i].registered == 0)
8317 && i < CTL_MAX_INITIATORS)
8318 lun->pending_ua[i] |=
8320 else if (persis_offset && i >= persis_offset)
8321 lun->pending_ua[i - persis_offset] |=
8323 lun->per_res[i].registered = 0;
8324 memset(&lun->per_res[i].res_key, 0,
8325 sizeof(struct scsi_per_res_key));
8328 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8329 lun->pr_key_count = 1;
8330 lun->res_type = msg->pr.pr_info.res_type;
8331 if (lun->res_type != SPR_TYPE_WR_EX_AR
8332 && lun->res_type != SPR_TYPE_EX_AC_AR)
8333 lun->pr_res_idx = msg->pr.pr_info.residx;
8335 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8336 if (memcmp(msg->pr.pr_info.sa_res_key,
8337 lun->per_res[i].res_key.key,
8338 sizeof(struct scsi_per_res_key)) != 0)
8341 lun->per_res[i].registered = 0;
8342 memset(&lun->per_res[i].res_key, 0,
8343 sizeof(struct scsi_per_res_key));
8344 lun->pr_key_count--;
8347 && i < persis_offset)
8348 lun->pending_ua[i] |=
8350 else if (persis_offset
8351 && i >= persis_offset)
8352 lun->pending_ua[i - persis_offset] |=
8358 * Temporarily unregister so it won't get removed
8361 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8362 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8363 if (lun->per_res[i].registered == 0)
8366 if (memcmp(msg->pr.pr_info.sa_res_key,
8367 lun->per_res[i].res_key.key,
8368 sizeof(struct scsi_per_res_key)) == 0) {
8369 lun->per_res[i].registered = 0;
8370 memset(&lun->per_res[i].res_key, 0,
8371 sizeof(struct scsi_per_res_key));
8372 lun->pr_key_count--;
8374 && i < CTL_MAX_INITIATORS)
8375 lun->pending_ua[i] |=
8377 else if (persis_offset
8378 && i >= persis_offset)
8379 lun->pending_ua[i - persis_offset] |=
8381 } else if (msg->pr.pr_info.res_type != lun->res_type
8382 && (lun->res_type == SPR_TYPE_WR_EX_RO
8383 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8385 && i < persis_offset)
8386 lun->pending_ua[i] |=
8388 else if (persis_offset
8389 && i >= persis_offset)
8390 lun->pending_ua[i - persis_offset] |=
8394 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8395 lun->res_type = msg->pr.pr_info.res_type;
8396 if (lun->res_type != SPR_TYPE_WR_EX_AR
8397 && lun->res_type != SPR_TYPE_EX_AC_AR)
8398 lun->pr_res_idx = msg->pr.pr_info.residx;
8400 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8402 lun->PRGeneration++;
8408 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8412 u_int32_t param_len;
8413 struct scsi_per_res_out *cdb;
8414 struct ctl_lun *lun;
8415 struct scsi_per_res_out_parms* param;
8416 struct ctl_softc *softc;
8418 uint64_t res_key, sa_res_key;
8420 union ctl_ha_msg persis_io;
8423 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8425 retval = CTL_RETVAL_COMPLETE;
8427 softc = control_softc;
8429 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8430 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8433 * We only support whole-LUN scope. The scope & type are ignored for
8434 * register, register and ignore existing key and clear.
8435 * We sometimes ignore scope and type on preempts too!!
8436 * Verify reservation type here as well.
8438 type = cdb->scope_type & SPR_TYPE_MASK;
8439 if ((cdb->action == SPRO_RESERVE)
8440 || (cdb->action == SPRO_RELEASE)) {
8441 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8442 ctl_set_invalid_field(/*ctsio*/ ctsio,
8448 ctl_done((union ctl_io *)ctsio);
8449 return (CTL_RETVAL_COMPLETE);
8452 if (type>8 || type==2 || type==4 || type==0) {
8453 ctl_set_invalid_field(/*ctsio*/ ctsio,
8459 ctl_done((union ctl_io *)ctsio);
8460 return (CTL_RETVAL_COMPLETE);
8464 param_len = scsi_4btoul(cdb->length);
8466 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8467 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8468 ctsio->kern_data_len = param_len;
8469 ctsio->kern_total_len = param_len;
8470 ctsio->kern_data_resid = 0;
8471 ctsio->kern_rel_offset = 0;
8472 ctsio->kern_sg_entries = 0;
8473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8474 ctsio->be_move_done = ctl_config_move_done;
8475 ctl_datamove((union ctl_io *)ctsio);
8477 return (CTL_RETVAL_COMPLETE);
8480 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8482 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8483 res_key = scsi_8btou64(param->res_key.key);
8484 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8487 * Validate the reservation key here except for SPRO_REG_IGNO
8488 * This must be done for all other service actions
8490 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8491 mtx_lock(&lun->lun_lock);
8492 if (lun->per_res[residx].registered) {
8493 if (memcmp(param->res_key.key,
8494 lun->per_res[residx].res_key.key,
8495 ctl_min(sizeof(param->res_key),
8496 sizeof(lun->per_res[residx].res_key))) != 0) {
8498 * The current key passed in doesn't match
8499 * the one the initiator previously
8502 mtx_unlock(&lun->lun_lock);
8503 free(ctsio->kern_data_ptr, M_CTL);
8504 ctl_set_reservation_conflict(ctsio);
8505 ctl_done((union ctl_io *)ctsio);
8506 return (CTL_RETVAL_COMPLETE);
8508 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8510 * We are not registered
8512 mtx_unlock(&lun->lun_lock);
8513 free(ctsio->kern_data_ptr, M_CTL);
8514 ctl_set_reservation_conflict(ctsio);
8515 ctl_done((union ctl_io *)ctsio);
8516 return (CTL_RETVAL_COMPLETE);
8517 } else if (res_key != 0) {
8519 * We are not registered and trying to register but
8520 * the register key isn't zero.
8522 mtx_unlock(&lun->lun_lock);
8523 free(ctsio->kern_data_ptr, M_CTL);
8524 ctl_set_reservation_conflict(ctsio);
8525 ctl_done((union ctl_io *)ctsio);
8526 return (CTL_RETVAL_COMPLETE);
8528 mtx_unlock(&lun->lun_lock);
8531 switch (cdb->action & SPRO_ACTION_MASK) {
8533 case SPRO_REG_IGNO: {
8536 printf("Registration received\n");
8540 * We don't support any of these options, as we report in
8541 * the read capabilities request (see
8542 * ctl_persistent_reserve_in(), above).
8544 if ((param->flags & SPR_SPEC_I_PT)
8545 || (param->flags & SPR_ALL_TG_PT)
8546 || (param->flags & SPR_APTPL)) {
8549 if (param->flags & SPR_APTPL)
8551 else if (param->flags & SPR_ALL_TG_PT)
8553 else /* SPR_SPEC_I_PT */
8556 free(ctsio->kern_data_ptr, M_CTL);
8557 ctl_set_invalid_field(ctsio,
8563 ctl_done((union ctl_io *)ctsio);
8564 return (CTL_RETVAL_COMPLETE);
8567 mtx_lock(&lun->lun_lock);
8570 * The initiator wants to clear the
8573 if (sa_res_key == 0) {
8575 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8576 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8577 && !lun->per_res[residx].registered)) {
8578 mtx_unlock(&lun->lun_lock);
8582 lun->per_res[residx].registered = 0;
8583 memset(&lun->per_res[residx].res_key,
8584 0, sizeof(lun->per_res[residx].res_key));
8585 lun->pr_key_count--;
8587 if (residx == lun->pr_res_idx) {
8588 lun->flags &= ~CTL_LUN_PR_RESERVED;
8589 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8591 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8592 || lun->res_type == SPR_TYPE_EX_AC_RO)
8593 && lun->pr_key_count) {
8595 * If the reservation is a registrants
8596 * only type we need to generate a UA
8597 * for other registered inits. The
8598 * sense code should be RESERVATIONS
8602 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8604 i+persis_offset].registered
8607 lun->pending_ua[i] |=
8612 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8613 if (lun->pr_key_count==0) {
8614 lun->flags &= ~CTL_LUN_PR_RESERVED;
8616 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8619 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8620 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8621 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8622 persis_io.pr.pr_info.residx = residx;
8623 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8624 &persis_io, sizeof(persis_io), 0 )) >
8625 CTL_HA_STATUS_SUCCESS) {
8626 printf("CTL:Persis Out error returned from "
8627 "ctl_ha_msg_send %d\n", isc_retval);
8629 } else /* sa_res_key != 0 */ {
8632 * If we aren't registered currently then increment
8633 * the key count and set the registered flag.
8635 if (!lun->per_res[residx].registered) {
8636 lun->pr_key_count++;
8637 lun->per_res[residx].registered = 1;
8640 memcpy(&lun->per_res[residx].res_key,
8641 param->serv_act_res_key,
8642 ctl_min(sizeof(param->serv_act_res_key),
8643 sizeof(lun->per_res[residx].res_key)));
8645 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8646 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8647 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8648 persis_io.pr.pr_info.residx = residx;
8649 memcpy(persis_io.pr.pr_info.sa_res_key,
8650 param->serv_act_res_key,
8651 sizeof(param->serv_act_res_key));
8652 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8653 &persis_io, sizeof(persis_io), 0)) >
8654 CTL_HA_STATUS_SUCCESS) {
8655 printf("CTL:Persis Out error returned from "
8656 "ctl_ha_msg_send %d\n", isc_retval);
8659 lun->PRGeneration++;
8660 mtx_unlock(&lun->lun_lock);
8666 printf("Reserve executed type %d\n", type);
8668 mtx_lock(&lun->lun_lock);
8669 if (lun->flags & CTL_LUN_PR_RESERVED) {
8671 * if this isn't the reservation holder and it's
8672 * not a "all registrants" type or if the type is
8673 * different then we have a conflict
8675 if ((lun->pr_res_idx != residx
8676 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8677 || lun->res_type != type) {
8678 mtx_unlock(&lun->lun_lock);
8679 free(ctsio->kern_data_ptr, M_CTL);
8680 ctl_set_reservation_conflict(ctsio);
8681 ctl_done((union ctl_io *)ctsio);
8682 return (CTL_RETVAL_COMPLETE);
8684 mtx_unlock(&lun->lun_lock);
8685 } else /* create a reservation */ {
8687 * If it's not an "all registrants" type record
8688 * reservation holder
8690 if (type != SPR_TYPE_WR_EX_AR
8691 && type != SPR_TYPE_EX_AC_AR)
8692 lun->pr_res_idx = residx; /* Res holder */
8694 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8696 lun->flags |= CTL_LUN_PR_RESERVED;
8697 lun->res_type = type;
8699 mtx_unlock(&lun->lun_lock);
8701 /* send msg to other side */
8702 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8703 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8704 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8705 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8706 persis_io.pr.pr_info.res_type = type;
8707 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8708 &persis_io, sizeof(persis_io), 0)) >
8709 CTL_HA_STATUS_SUCCESS) {
8710 printf("CTL:Persis Out error returned from "
8711 "ctl_ha_msg_send %d\n", isc_retval);
8717 mtx_lock(&lun->lun_lock);
8718 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8719 /* No reservation exists return good status */
8720 mtx_unlock(&lun->lun_lock);
8724 * Is this nexus a reservation holder?
8726 if (lun->pr_res_idx != residx
8727 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8729 * not a res holder return good status but
8732 mtx_unlock(&lun->lun_lock);
8736 if (lun->res_type != type) {
8737 mtx_unlock(&lun->lun_lock);
8738 free(ctsio->kern_data_ptr, M_CTL);
8739 ctl_set_illegal_pr_release(ctsio);
8740 ctl_done((union ctl_io *)ctsio);
8741 return (CTL_RETVAL_COMPLETE);
8744 /* okay to release */
8745 lun->flags &= ~CTL_LUN_PR_RESERVED;
8746 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8750 * if this isn't an exclusive access
8751 * res generate UA for all other
8754 if (type != SPR_TYPE_EX_AC
8755 && type != SPR_TYPE_WR_EX) {
8757 * temporarily unregister so we don't generate UA
8759 lun->per_res[residx].registered = 0;
8761 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8762 if (lun->per_res[i+persis_offset].registered
8765 lun->pending_ua[i] |=
8769 lun->per_res[residx].registered = 1;
8771 mtx_unlock(&lun->lun_lock);
8772 /* Send msg to other side */
8773 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8774 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8775 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8776 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8777 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8778 printf("CTL:Persis Out error returned from "
8779 "ctl_ha_msg_send %d\n", isc_retval);
8784 /* send msg to other side */
8786 mtx_lock(&lun->lun_lock);
8787 lun->flags &= ~CTL_LUN_PR_RESERVED;
8789 lun->pr_key_count = 0;
8790 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8793 memset(&lun->per_res[residx].res_key,
8794 0, sizeof(lun->per_res[residx].res_key));
8795 lun->per_res[residx].registered = 0;
8797 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8798 if (lun->per_res[i].registered) {
8799 if (!persis_offset && i < CTL_MAX_INITIATORS)
8800 lun->pending_ua[i] |=
8802 else if (persis_offset && i >= persis_offset)
8803 lun->pending_ua[i-persis_offset] |=
8806 memset(&lun->per_res[i].res_key,
8807 0, sizeof(struct scsi_per_res_key));
8808 lun->per_res[i].registered = 0;
8810 lun->PRGeneration++;
8811 mtx_unlock(&lun->lun_lock);
8812 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8813 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8814 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8815 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8816 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8817 printf("CTL:Persis Out error returned from "
8818 "ctl_ha_msg_send %d\n", isc_retval);
8822 case SPRO_PREEMPT: {
8825 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8826 residx, ctsio, cdb, param);
8828 return (CTL_RETVAL_COMPLETE);
8832 panic("Invalid PR type %x", cdb->action);
8836 free(ctsio->kern_data_ptr, M_CTL);
8837 ctl_set_success(ctsio);
8838 ctl_done((union ctl_io *)ctsio);
8844 * This routine is for handling a message from the other SC pertaining to
8845 * persistent reserve out. All the error checking will have been done
8846 * so only perorming the action need be done here to keep the two
8850 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8852 struct ctl_lun *lun;
8853 struct ctl_softc *softc;
8857 softc = control_softc;
8859 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8860 lun = softc->ctl_luns[targ_lun];
8861 mtx_lock(&lun->lun_lock);
8862 switch(msg->pr.pr_info.action) {
8863 case CTL_PR_REG_KEY:
8864 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8865 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8866 lun->pr_key_count++;
8868 lun->PRGeneration++;
8869 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8870 msg->pr.pr_info.sa_res_key,
8871 sizeof(struct scsi_per_res_key));
8874 case CTL_PR_UNREG_KEY:
8875 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8876 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8877 0, sizeof(struct scsi_per_res_key));
8878 lun->pr_key_count--;
8880 /* XXX Need to see if the reservation has been released */
8881 /* if so do we need to generate UA? */
8882 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8883 lun->flags &= ~CTL_LUN_PR_RESERVED;
8884 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8886 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8887 || lun->res_type == SPR_TYPE_EX_AC_RO)
8888 && lun->pr_key_count) {
8890 * If the reservation is a registrants
8891 * only type we need to generate a UA
8892 * for other registered inits. The
8893 * sense code should be RESERVATIONS
8897 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8899 persis_offset].registered == 0)
8902 lun->pending_ua[i] |=
8907 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8908 if (lun->pr_key_count==0) {
8909 lun->flags &= ~CTL_LUN_PR_RESERVED;
8911 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8914 lun->PRGeneration++;
8917 case CTL_PR_RESERVE:
8918 lun->flags |= CTL_LUN_PR_RESERVED;
8919 lun->res_type = msg->pr.pr_info.res_type;
8920 lun->pr_res_idx = msg->pr.pr_info.residx;
8924 case CTL_PR_RELEASE:
8926 * if this isn't an exclusive access res generate UA for all
8927 * other registrants.
8929 if (lun->res_type != SPR_TYPE_EX_AC
8930 && lun->res_type != SPR_TYPE_WR_EX) {
8931 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8932 if (lun->per_res[i+persis_offset].registered)
8933 lun->pending_ua[i] |=
8937 lun->flags &= ~CTL_LUN_PR_RESERVED;
8938 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8942 case CTL_PR_PREEMPT:
8943 ctl_pro_preempt_other(lun, msg);
8946 lun->flags &= ~CTL_LUN_PR_RESERVED;
8948 lun->pr_key_count = 0;
8949 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8951 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8952 if (lun->per_res[i].registered == 0)
8955 && i < CTL_MAX_INITIATORS)
8956 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8957 else if (persis_offset
8958 && i >= persis_offset)
8959 lun->pending_ua[i-persis_offset] |=
8961 memset(&lun->per_res[i].res_key, 0,
8962 sizeof(struct scsi_per_res_key));
8963 lun->per_res[i].registered = 0;
8965 lun->PRGeneration++;
8969 mtx_unlock(&lun->lun_lock);
8973 ctl_read_write(struct ctl_scsiio *ctsio)
8975 struct ctl_lun *lun;
8976 struct ctl_lba_len_flags *lbalen;
8978 uint32_t num_blocks;
8983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8985 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8990 retval = CTL_RETVAL_COMPLETE;
8992 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8993 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8994 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8998 * XXX KDM need a lock here.
9000 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
9001 if ((lun->res_type == SPR_TYPE_EX_AC
9002 && residx != lun->pr_res_idx)
9003 || ((lun->res_type == SPR_TYPE_EX_AC_RO
9004 || lun->res_type == SPR_TYPE_EX_AC_AR)
9005 && !lun->per_res[residx].registered)) {
9006 ctl_set_reservation_conflict(ctsio);
9007 ctl_done((union ctl_io *)ctsio);
9008 return (CTL_RETVAL_COMPLETE);
9012 switch (ctsio->cdb[0]) {
9015 struct scsi_rw_6 *cdb;
9017 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9019 lba = scsi_3btoul(cdb->addr);
9020 /* only 5 bits are valid in the most significant address byte */
9022 num_blocks = cdb->length;
9024 * This is correct according to SBC-2.
9026 if (num_blocks == 0)
9032 struct scsi_rw_10 *cdb;
9034 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9036 if (cdb->byte2 & SRW10_FUA)
9038 if (cdb->byte2 & SRW10_DPO)
9041 lba = scsi_4btoul(cdb->addr);
9042 num_blocks = scsi_2btoul(cdb->length);
9045 case WRITE_VERIFY_10: {
9046 struct scsi_write_verify_10 *cdb;
9048 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9051 * XXX KDM we should do actual write verify support at some
9052 * point. This is obviously fake, we're just translating
9053 * things to a write. So we don't even bother checking the
9054 * BYTCHK field, since we don't do any verification. If
9055 * the user asks for it, we'll just pretend we did it.
9057 if (cdb->byte2 & SWV_DPO)
9060 lba = scsi_4btoul(cdb->addr);
9061 num_blocks = scsi_2btoul(cdb->length);
9066 struct scsi_rw_12 *cdb;
9068 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9070 if (cdb->byte2 & SRW12_FUA)
9072 if (cdb->byte2 & SRW12_DPO)
9074 lba = scsi_4btoul(cdb->addr);
9075 num_blocks = scsi_4btoul(cdb->length);
9078 case WRITE_VERIFY_12: {
9079 struct scsi_write_verify_12 *cdb;
9081 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9083 if (cdb->byte2 & SWV_DPO)
9086 lba = scsi_4btoul(cdb->addr);
9087 num_blocks = scsi_4btoul(cdb->length);
9093 struct scsi_rw_16 *cdb;
9095 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9097 if (cdb->byte2 & SRW12_FUA)
9099 if (cdb->byte2 & SRW12_DPO)
9102 lba = scsi_8btou64(cdb->addr);
9103 num_blocks = scsi_4btoul(cdb->length);
9106 case WRITE_VERIFY_16: {
9107 struct scsi_write_verify_16 *cdb;
9109 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9111 if (cdb->byte2 & SWV_DPO)
9114 lba = scsi_8btou64(cdb->addr);
9115 num_blocks = scsi_4btoul(cdb->length);
9120 * We got a command we don't support. This shouldn't
9121 * happen, commands should be filtered out above us.
9123 ctl_set_invalid_opcode(ctsio);
9124 ctl_done((union ctl_io *)ctsio);
9126 return (CTL_RETVAL_COMPLETE);
9127 break; /* NOTREACHED */
9131 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9132 * interesting for us, but if RAIDCore is in write-back mode,
9133 * getting it to do write-through for a particular transaction may
9138 * The first check is to make sure we're in bounds, the second
9139 * check is to catch wrap-around problems. If the lba + num blocks
9140 * is less than the lba, then we've wrapped around and the block
9141 * range is invalid anyway.
9143 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9144 || ((lba + num_blocks) < lba)) {
9145 ctl_set_lba_out_of_range(ctsio);
9146 ctl_done((union ctl_io *)ctsio);
9147 return (CTL_RETVAL_COMPLETE);
9151 * According to SBC-3, a transfer length of 0 is not an error.
9152 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9153 * translates to 256 blocks for those commands.
9155 if (num_blocks == 0) {
9156 ctl_set_success(ctsio);
9157 ctl_done((union ctl_io *)ctsio);
9158 return (CTL_RETVAL_COMPLETE);
9161 lbalen = (struct ctl_lba_len_flags *)
9162 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9164 lbalen->len = num_blocks;
9165 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
9167 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9168 ctsio->kern_rel_offset = 0;
9170 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9172 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9178 ctl_cnw_cont(union ctl_io *io)
9180 struct ctl_scsiio *ctsio;
9181 struct ctl_lun *lun;
9182 struct ctl_lba_len_flags *lbalen;
9185 ctsio = &io->scsiio;
9186 ctsio->io_hdr.status = CTL_STATUS_NONE;
9187 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9188 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9189 lbalen = (struct ctl_lba_len_flags *)
9190 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9191 lbalen->flags = CTL_LLF_WRITE;
9193 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9194 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9199 ctl_cnw(struct ctl_scsiio *ctsio)
9201 struct ctl_lun *lun;
9202 struct ctl_lba_len_flags *lbalen;
9204 uint32_t num_blocks;
9208 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9210 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9215 retval = CTL_RETVAL_COMPLETE;
9217 switch (ctsio->cdb[0]) {
9218 case COMPARE_AND_WRITE: {
9219 struct scsi_compare_and_write *cdb;
9221 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9223 if (cdb->byte2 & SRW10_FUA)
9225 if (cdb->byte2 & SRW10_DPO)
9227 lba = scsi_8btou64(cdb->addr);
9228 num_blocks = cdb->length;
9233 * We got a command we don't support. This shouldn't
9234 * happen, commands should be filtered out above us.
9236 ctl_set_invalid_opcode(ctsio);
9237 ctl_done((union ctl_io *)ctsio);
9239 return (CTL_RETVAL_COMPLETE);
9240 break; /* NOTREACHED */
9244 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9245 * interesting for us, but if RAIDCore is in write-back mode,
9246 * getting it to do write-through for a particular transaction may
9251 * The first check is to make sure we're in bounds, the second
9252 * check is to catch wrap-around problems. If the lba + num blocks
9253 * is less than the lba, then we've wrapped around and the block
9254 * range is invalid anyway.
9256 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9257 || ((lba + num_blocks) < lba)) {
9258 ctl_set_lba_out_of_range(ctsio);
9259 ctl_done((union ctl_io *)ctsio);
9260 return (CTL_RETVAL_COMPLETE);
9264 * According to SBC-3, a transfer length of 0 is not an error.
9266 if (num_blocks == 0) {
9267 ctl_set_success(ctsio);
9268 ctl_done((union ctl_io *)ctsio);
9269 return (CTL_RETVAL_COMPLETE);
9272 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9273 ctsio->kern_rel_offset = 0;
9276 * Set the IO_CONT flag, so that if this I/O gets passed to
9277 * ctl_data_submit_done(), it'll get passed back to
9278 * ctl_ctl_cnw_cont() for further processing.
9280 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9281 ctsio->io_cont = ctl_cnw_cont;
9283 lbalen = (struct ctl_lba_len_flags *)
9284 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9286 lbalen->len = num_blocks;
9287 lbalen->flags = CTL_LLF_COMPARE;
9289 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9290 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9295 ctl_verify(struct ctl_scsiio *ctsio)
9297 struct ctl_lun *lun;
9298 struct ctl_lba_len_flags *lbalen;
9300 uint32_t num_blocks;
9304 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9306 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9310 retval = CTL_RETVAL_COMPLETE;
9312 switch (ctsio->cdb[0]) {
9314 struct scsi_verify_10 *cdb;
9316 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9317 if (cdb->byte2 & SVFY_BYTCHK)
9319 if (cdb->byte2 & SVFY_DPO)
9321 lba = scsi_4btoul(cdb->addr);
9322 num_blocks = scsi_2btoul(cdb->length);
9326 struct scsi_verify_12 *cdb;
9328 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9329 if (cdb->byte2 & SVFY_BYTCHK)
9331 if (cdb->byte2 & SVFY_DPO)
9333 lba = scsi_4btoul(cdb->addr);
9334 num_blocks = scsi_4btoul(cdb->length);
9338 struct scsi_rw_16 *cdb;
9340 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9341 if (cdb->byte2 & SVFY_BYTCHK)
9343 if (cdb->byte2 & SVFY_DPO)
9345 lba = scsi_8btou64(cdb->addr);
9346 num_blocks = scsi_4btoul(cdb->length);
9351 * We got a command we don't support. This shouldn't
9352 * happen, commands should be filtered out above us.
9354 ctl_set_invalid_opcode(ctsio);
9355 ctl_done((union ctl_io *)ctsio);
9356 return (CTL_RETVAL_COMPLETE);
9360 * The first check is to make sure we're in bounds, the second
9361 * check is to catch wrap-around problems. If the lba + num blocks
9362 * is less than the lba, then we've wrapped around and the block
9363 * range is invalid anyway.
9365 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9366 || ((lba + num_blocks) < lba)) {
9367 ctl_set_lba_out_of_range(ctsio);
9368 ctl_done((union ctl_io *)ctsio);
9369 return (CTL_RETVAL_COMPLETE);
9373 * According to SBC-3, a transfer length of 0 is not an error.
9375 if (num_blocks == 0) {
9376 ctl_set_success(ctsio);
9377 ctl_done((union ctl_io *)ctsio);
9378 return (CTL_RETVAL_COMPLETE);
9381 lbalen = (struct ctl_lba_len_flags *)
9382 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9384 lbalen->len = num_blocks;
9386 lbalen->flags = CTL_LLF_COMPARE;
9387 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9389 lbalen->flags = CTL_LLF_VERIFY;
9390 ctsio->kern_total_len = 0;
9392 ctsio->kern_rel_offset = 0;
9394 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9395 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9400 ctl_report_luns(struct ctl_scsiio *ctsio)
9402 struct scsi_report_luns *cdb;
9403 struct scsi_report_luns_data *lun_data;
9404 struct ctl_lun *lun, *request_lun;
9405 int num_luns, retval;
9406 uint32_t alloc_len, lun_datalen;
9407 int num_filled, well_known;
9408 uint32_t initidx, targ_lun_id, lun_id;
9410 retval = CTL_RETVAL_COMPLETE;
9413 cdb = (struct scsi_report_luns *)ctsio->cdb;
9415 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9417 mtx_lock(&control_softc->ctl_lock);
9418 num_luns = control_softc->num_luns;
9419 mtx_unlock(&control_softc->ctl_lock);
9421 switch (cdb->select_report) {
9422 case RPL_REPORT_DEFAULT:
9423 case RPL_REPORT_ALL:
9425 case RPL_REPORT_WELLKNOWN:
9430 ctl_set_invalid_field(ctsio,
9436 ctl_done((union ctl_io *)ctsio);
9438 break; /* NOTREACHED */
9441 alloc_len = scsi_4btoul(cdb->length);
9443 * The initiator has to allocate at least 16 bytes for this request,
9444 * so he can at least get the header and the first LUN. Otherwise
9445 * we reject the request (per SPC-3 rev 14, section 6.21).
9447 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9448 sizeof(struct scsi_report_luns_lundata))) {
9449 ctl_set_invalid_field(ctsio,
9455 ctl_done((union ctl_io *)ctsio);
9459 request_lun = (struct ctl_lun *)
9460 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9462 lun_datalen = sizeof(*lun_data) +
9463 (num_luns * sizeof(struct scsi_report_luns_lundata));
9465 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9466 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9467 ctsio->kern_sg_entries = 0;
9469 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9471 mtx_lock(&control_softc->ctl_lock);
9472 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9473 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9474 if (lun_id >= CTL_MAX_LUNS)
9476 lun = control_softc->ctl_luns[lun_id];
9480 if (targ_lun_id <= 0xff) {
9482 * Peripheral addressing method, bus number 0.
9484 lun_data->luns[num_filled].lundata[0] =
9485 RPL_LUNDATA_ATYP_PERIPH;
9486 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9488 } else if (targ_lun_id <= 0x3fff) {
9490 * Flat addressing method.
9492 lun_data->luns[num_filled].lundata[0] =
9493 RPL_LUNDATA_ATYP_FLAT |
9494 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9495 #ifdef OLDCTLHEADERS
9496 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9497 (targ_lun_id & SRLD_BUS_LUN_MASK);
9499 lun_data->luns[num_filled].lundata[1] =
9500 #ifdef OLDCTLHEADERS
9501 targ_lun_id >> SRLD_BUS_LUN_BITS;
9503 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9506 printf("ctl_report_luns: bogus LUN number %jd, "
9507 "skipping\n", (intmax_t)targ_lun_id);
9510 * According to SPC-3, rev 14 section 6.21:
9512 * "The execution of a REPORT LUNS command to any valid and
9513 * installed logical unit shall clear the REPORTED LUNS DATA
9514 * HAS CHANGED unit attention condition for all logical
9515 * units of that target with respect to the requesting
9516 * initiator. A valid and installed logical unit is one
9517 * having a PERIPHERAL QUALIFIER of 000b in the standard
9518 * INQUIRY data (see 6.4.2)."
9520 * If request_lun is NULL, the LUN this report luns command
9521 * was issued to is either disabled or doesn't exist. In that
9522 * case, we shouldn't clear any pending lun change unit
9525 if (request_lun != NULL) {
9526 mtx_lock(&lun->lun_lock);
9527 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9528 mtx_unlock(&lun->lun_lock);
9531 mtx_unlock(&control_softc->ctl_lock);
9534 * It's quite possible that we've returned fewer LUNs than we allocated
9535 * space for. Trim it.
9537 lun_datalen = sizeof(*lun_data) +
9538 (num_filled * sizeof(struct scsi_report_luns_lundata));
9540 if (lun_datalen < alloc_len) {
9541 ctsio->residual = alloc_len - lun_datalen;
9542 ctsio->kern_data_len = lun_datalen;
9543 ctsio->kern_total_len = lun_datalen;
9545 ctsio->residual = 0;
9546 ctsio->kern_data_len = alloc_len;
9547 ctsio->kern_total_len = alloc_len;
9549 ctsio->kern_data_resid = 0;
9550 ctsio->kern_rel_offset = 0;
9551 ctsio->kern_sg_entries = 0;
9554 * We set this to the actual data length, regardless of how much
9555 * space we actually have to return results. If the user looks at
9556 * this value, he'll know whether or not he allocated enough space
9557 * and reissue the command if necessary. We don't support well
9558 * known logical units, so if the user asks for that, return none.
9560 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9563 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9566 ctsio->scsi_status = SCSI_STATUS_OK;
9568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9569 ctsio->be_move_done = ctl_config_move_done;
9570 ctl_datamove((union ctl_io *)ctsio);
9576 ctl_request_sense(struct ctl_scsiio *ctsio)
9578 struct scsi_request_sense *cdb;
9579 struct scsi_sense_data *sense_ptr;
9580 struct ctl_lun *lun;
9583 scsi_sense_data_type sense_format;
9585 cdb = (struct scsi_request_sense *)ctsio->cdb;
9587 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9589 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9592 * Determine which sense format the user wants.
9594 if (cdb->byte2 & SRS_DESC)
9595 sense_format = SSD_TYPE_DESC;
9597 sense_format = SSD_TYPE_FIXED;
9599 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9600 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9601 ctsio->kern_sg_entries = 0;
9604 * struct scsi_sense_data, which is currently set to 256 bytes, is
9605 * larger than the largest allowed value for the length field in the
9606 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9608 ctsio->residual = 0;
9609 ctsio->kern_data_len = cdb->length;
9610 ctsio->kern_total_len = cdb->length;
9612 ctsio->kern_data_resid = 0;
9613 ctsio->kern_rel_offset = 0;
9614 ctsio->kern_sg_entries = 0;
9617 * If we don't have a LUN, we don't have any pending sense.
9623 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9625 * Check for pending sense, and then for pending unit attentions.
9626 * Pending sense gets returned first, then pending unit attentions.
9628 mtx_lock(&lun->lun_lock);
9630 if (ctl_is_set(lun->have_ca, initidx)) {
9631 scsi_sense_data_type stored_format;
9634 * Check to see which sense format was used for the stored
9637 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9640 * If the user requested a different sense format than the
9641 * one we stored, then we need to convert it to the other
9642 * format. If we're going from descriptor to fixed format
9643 * sense data, we may lose things in translation, depending
9644 * on what options were used.
9646 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9647 * for some reason we'll just copy it out as-is.
9649 if ((stored_format == SSD_TYPE_FIXED)
9650 && (sense_format == SSD_TYPE_DESC))
9651 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9652 &lun->pending_sense[initidx],
9653 (struct scsi_sense_data_desc *)sense_ptr);
9654 else if ((stored_format == SSD_TYPE_DESC)
9655 && (sense_format == SSD_TYPE_FIXED))
9656 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9657 &lun->pending_sense[initidx],
9658 (struct scsi_sense_data_fixed *)sense_ptr);
9660 memcpy(sense_ptr, &lun->pending_sense[initidx],
9661 ctl_min(sizeof(*sense_ptr),
9662 sizeof(lun->pending_sense[initidx])));
9664 ctl_clear_mask(lun->have_ca, initidx);
9668 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9669 ctl_ua_type ua_type;
9671 ua_type = ctl_build_ua(lun->pending_ua[initidx],
9672 sense_ptr, sense_format);
9673 if (ua_type != CTL_UA_NONE) {
9675 /* We're reporting this UA, so clear it */
9676 lun->pending_ua[initidx] &= ~ua_type;
9679 mtx_unlock(&lun->lun_lock);
9682 * We already have a pending error, return it.
9684 if (have_error != 0) {
9686 * We report the SCSI status as OK, since the status of the
9687 * request sense command itself is OK.
9689 ctsio->scsi_status = SCSI_STATUS_OK;
9692 * We report 0 for the sense length, because we aren't doing
9693 * autosense in this case. We're reporting sense as
9696 ctsio->sense_len = 0;
9697 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9698 ctsio->be_move_done = ctl_config_move_done;
9699 ctl_datamove((union ctl_io *)ctsio);
9701 return (CTL_RETVAL_COMPLETE);
9707 * No sense information to report, so we report that everything is
9710 ctl_set_sense_data(sense_ptr,
9713 /*current_error*/ 1,
9714 /*sense_key*/ SSD_KEY_NO_SENSE,
9719 ctsio->scsi_status = SCSI_STATUS_OK;
9722 * We report 0 for the sense length, because we aren't doing
9723 * autosense in this case. We're reporting sense as parameter data.
9725 ctsio->sense_len = 0;
9726 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9727 ctsio->be_move_done = ctl_config_move_done;
9728 ctl_datamove((union ctl_io *)ctsio);
9730 return (CTL_RETVAL_COMPLETE);
9734 ctl_tur(struct ctl_scsiio *ctsio)
9736 struct ctl_lun *lun;
9738 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9740 CTL_DEBUG_PRINT(("ctl_tur\n"));
9745 ctsio->scsi_status = SCSI_STATUS_OK;
9746 ctsio->io_hdr.status = CTL_SUCCESS;
9748 ctl_done((union ctl_io *)ctsio);
9750 return (CTL_RETVAL_COMPLETE);
9755 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9762 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9764 struct scsi_vpd_supported_pages *pages;
9766 struct ctl_lun *lun;
9768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9770 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9771 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9772 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9773 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9774 ctsio->kern_sg_entries = 0;
9776 if (sup_page_size < alloc_len) {
9777 ctsio->residual = alloc_len - sup_page_size;
9778 ctsio->kern_data_len = sup_page_size;
9779 ctsio->kern_total_len = sup_page_size;
9781 ctsio->residual = 0;
9782 ctsio->kern_data_len = alloc_len;
9783 ctsio->kern_total_len = alloc_len;
9785 ctsio->kern_data_resid = 0;
9786 ctsio->kern_rel_offset = 0;
9787 ctsio->kern_sg_entries = 0;
9790 * The control device is always connected. The disk device, on the
9791 * other hand, may not be online all the time. Need to change this
9792 * to figure out whether the disk device is actually online or not.
9795 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9796 lun->be_lun->lun_type;
9798 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9800 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9801 /* Supported VPD pages */
9802 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9804 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9805 /* Device Identification */
9806 pages->page_list[2] = SVPD_DEVICE_ID;
9808 pages->page_list[3] = SVPD_SCSI_PORTS;
9809 /* Third-party Copy */
9810 pages->page_list[4] = SVPD_SCSI_TPC;
9812 pages->page_list[5] = SVPD_BLOCK_LIMITS;
9813 /* Block Device Characteristics */
9814 pages->page_list[6] = SVPD_BDC;
9815 /* Logical Block Provisioning */
9816 pages->page_list[7] = SVPD_LBP;
9818 ctsio->scsi_status = SCSI_STATUS_OK;
9820 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9821 ctsio->be_move_done = ctl_config_move_done;
9822 ctl_datamove((union ctl_io *)ctsio);
9824 return (CTL_RETVAL_COMPLETE);
9828 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9830 struct scsi_vpd_unit_serial_number *sn_ptr;
9831 struct ctl_lun *lun;
9833 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9835 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9836 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9837 ctsio->kern_sg_entries = 0;
9839 if (sizeof(*sn_ptr) < alloc_len) {
9840 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9841 ctsio->kern_data_len = sizeof(*sn_ptr);
9842 ctsio->kern_total_len = sizeof(*sn_ptr);
9844 ctsio->residual = 0;
9845 ctsio->kern_data_len = alloc_len;
9846 ctsio->kern_total_len = alloc_len;
9848 ctsio->kern_data_resid = 0;
9849 ctsio->kern_rel_offset = 0;
9850 ctsio->kern_sg_entries = 0;
9853 * The control device is always connected. The disk device, on the
9854 * other hand, may not be online all the time. Need to change this
9855 * to figure out whether the disk device is actually online or not.
9858 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9859 lun->be_lun->lun_type;
9861 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9863 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9864 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9866 * If we don't have a LUN, we just leave the serial number as
9869 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9871 strncpy((char *)sn_ptr->serial_num,
9872 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9874 ctsio->scsi_status = SCSI_STATUS_OK;
9876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9877 ctsio->be_move_done = ctl_config_move_done;
9878 ctl_datamove((union ctl_io *)ctsio);
9880 return (CTL_RETVAL_COMPLETE);
9885 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9887 struct scsi_vpd_device_id *devid_ptr;
9888 struct scsi_vpd_id_descriptor *desc;
9889 struct ctl_softc *ctl_softc;
9890 struct ctl_lun *lun;
9891 struct ctl_port *port;
9895 ctl_softc = control_softc;
9897 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9900 data_len = sizeof(struct scsi_vpd_device_id) +
9901 sizeof(struct scsi_vpd_id_descriptor) +
9902 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9903 sizeof(struct scsi_vpd_id_descriptor) +
9904 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9905 if (lun && lun->lun_devid)
9906 data_len += lun->lun_devid->len;
9907 if (port->port_devid)
9908 data_len += port->port_devid->len;
9909 if (port->target_devid)
9910 data_len += port->target_devid->len;
9912 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9913 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9914 ctsio->kern_sg_entries = 0;
9916 if (data_len < alloc_len) {
9917 ctsio->residual = alloc_len - data_len;
9918 ctsio->kern_data_len = data_len;
9919 ctsio->kern_total_len = data_len;
9921 ctsio->residual = 0;
9922 ctsio->kern_data_len = alloc_len;
9923 ctsio->kern_total_len = alloc_len;
9925 ctsio->kern_data_resid = 0;
9926 ctsio->kern_rel_offset = 0;
9927 ctsio->kern_sg_entries = 0;
9930 * The control device is always connected. The disk device, on the
9931 * other hand, may not be online all the time.
9934 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9935 lun->be_lun->lun_type;
9937 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9938 devid_ptr->page_code = SVPD_DEVICE_ID;
9939 scsi_ulto2b(data_len - 4, devid_ptr->length);
9941 if (port->port_type == CTL_PORT_FC)
9942 proto = SCSI_PROTO_FC << 4;
9943 else if (port->port_type == CTL_PORT_ISCSI)
9944 proto = SCSI_PROTO_ISCSI << 4;
9946 proto = SCSI_PROTO_SPI << 4;
9947 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9950 * We're using a LUN association here. i.e., this device ID is a
9951 * per-LUN identifier.
9953 if (lun && lun->lun_devid) {
9954 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9955 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9956 lun->lun_devid->len);
9960 * This is for the WWPN which is a port association.
9962 if (port->port_devid) {
9963 memcpy(desc, port->port_devid->data, port->port_devid->len);
9964 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9965 port->port_devid->len);
9969 * This is for the Relative Target Port(type 4h) identifier
9971 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9972 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9973 SVPD_ID_TYPE_RELTARG;
9975 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9976 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9977 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9980 * This is for the Target Port Group(type 5h) identifier
9982 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9983 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9984 SVPD_ID_TYPE_TPORTGRP;
9986 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9987 &desc->identifier[2]);
9988 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9989 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9992 * This is for the Target identifier
9994 if (port->target_devid) {
9995 memcpy(desc, port->target_devid->data, port->target_devid->len);
9998 ctsio->scsi_status = SCSI_STATUS_OK;
9999 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10000 ctsio->be_move_done = ctl_config_move_done;
10001 ctl_datamove((union ctl_io *)ctsio);
10003 return (CTL_RETVAL_COMPLETE);
10007 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10009 struct ctl_softc *softc = control_softc;
10010 struct scsi_vpd_scsi_ports *sp;
10011 struct scsi_vpd_port_designation *pd;
10012 struct scsi_vpd_port_designation_cont *pdc;
10013 struct ctl_lun *lun;
10014 struct ctl_port *port;
10015 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10016 int num_target_port_groups, single;
10018 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10020 single = ctl_is_single;
10022 num_target_port_groups = 1;
10024 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10025 num_target_ports = 0;
10028 mtx_lock(&softc->ctl_lock);
10029 STAILQ_FOREACH(port, &softc->port_list, links) {
10030 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10033 ctl_map_lun_back(port->targ_port, lun->lun) >=
10036 num_target_ports++;
10037 if (port->init_devid)
10038 iid_len += port->init_devid->len;
10039 if (port->port_devid)
10040 id_len += port->port_devid->len;
10042 mtx_unlock(&softc->ctl_lock);
10044 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10045 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10046 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10047 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10048 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10049 ctsio->kern_sg_entries = 0;
10051 if (data_len < alloc_len) {
10052 ctsio->residual = alloc_len - data_len;
10053 ctsio->kern_data_len = data_len;
10054 ctsio->kern_total_len = data_len;
10056 ctsio->residual = 0;
10057 ctsio->kern_data_len = alloc_len;
10058 ctsio->kern_total_len = alloc_len;
10060 ctsio->kern_data_resid = 0;
10061 ctsio->kern_rel_offset = 0;
10062 ctsio->kern_sg_entries = 0;
10065 * The control device is always connected. The disk device, on the
10066 * other hand, may not be online all the time. Need to change this
10067 * to figure out whether the disk device is actually online or not.
10070 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10071 lun->be_lun->lun_type;
10073 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10075 sp->page_code = SVPD_SCSI_PORTS;
10076 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10078 pd = &sp->design[0];
10080 mtx_lock(&softc->ctl_lock);
10081 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10085 for (g = 0; g < num_target_port_groups; g++) {
10086 STAILQ_FOREACH(port, &softc->port_list, links) {
10087 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10090 ctl_map_lun_back(port->targ_port, lun->lun) >=
10093 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10094 scsi_ulto2b(p, pd->relative_port_id);
10095 if (port->init_devid && g == pg) {
10096 iid_len = port->init_devid->len;
10097 memcpy(pd->initiator_transportid,
10098 port->init_devid->data, port->init_devid->len);
10101 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10102 pdc = (struct scsi_vpd_port_designation_cont *)
10103 (&pd->initiator_transportid[iid_len]);
10104 if (port->port_devid && g == pg) {
10105 id_len = port->port_devid->len;
10106 memcpy(pdc->target_port_descriptors,
10107 port->port_devid->data, port->port_devid->len);
10110 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10111 pd = (struct scsi_vpd_port_designation *)
10112 ((uint8_t *)pdc->target_port_descriptors + id_len);
10115 mtx_unlock(&softc->ctl_lock);
10117 ctsio->scsi_status = SCSI_STATUS_OK;
10118 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10119 ctsio->be_move_done = ctl_config_move_done;
10120 ctl_datamove((union ctl_io *)ctsio);
10122 return (CTL_RETVAL_COMPLETE);
10126 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10128 struct scsi_vpd_block_limits *bl_ptr;
10129 struct ctl_lun *lun;
10132 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10134 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10135 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10136 ctsio->kern_sg_entries = 0;
10138 if (sizeof(*bl_ptr) < alloc_len) {
10139 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10140 ctsio->kern_data_len = sizeof(*bl_ptr);
10141 ctsio->kern_total_len = sizeof(*bl_ptr);
10143 ctsio->residual = 0;
10144 ctsio->kern_data_len = alloc_len;
10145 ctsio->kern_total_len = alloc_len;
10147 ctsio->kern_data_resid = 0;
10148 ctsio->kern_rel_offset = 0;
10149 ctsio->kern_sg_entries = 0;
10152 * The control device is always connected. The disk device, on the
10153 * other hand, may not be online all the time. Need to change this
10154 * to figure out whether the disk device is actually online or not.
10157 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10158 lun->be_lun->lun_type;
10160 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10162 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10163 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
10164 bl_ptr->max_cmp_write_len = 0xff;
10165 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10167 bs = lun->be_lun->blocksize;
10168 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10169 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10170 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10171 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10172 if (lun->be_lun->pblockexp != 0) {
10173 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10174 bl_ptr->opt_unmap_grain);
10175 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10176 bl_ptr->unmap_grain_align);
10180 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10182 ctsio->scsi_status = SCSI_STATUS_OK;
10183 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10184 ctsio->be_move_done = ctl_config_move_done;
10185 ctl_datamove((union ctl_io *)ctsio);
10187 return (CTL_RETVAL_COMPLETE);
10191 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10193 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10194 struct ctl_lun *lun;
10196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10198 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10199 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10200 ctsio->kern_sg_entries = 0;
10202 if (sizeof(*bdc_ptr) < alloc_len) {
10203 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10204 ctsio->kern_data_len = sizeof(*bdc_ptr);
10205 ctsio->kern_total_len = sizeof(*bdc_ptr);
10207 ctsio->residual = 0;
10208 ctsio->kern_data_len = alloc_len;
10209 ctsio->kern_total_len = alloc_len;
10211 ctsio->kern_data_resid = 0;
10212 ctsio->kern_rel_offset = 0;
10213 ctsio->kern_sg_entries = 0;
10216 * The control device is always connected. The disk device, on the
10217 * other hand, may not be online all the time. Need to change this
10218 * to figure out whether the disk device is actually online or not.
10221 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10222 lun->be_lun->lun_type;
10224 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10225 bdc_ptr->page_code = SVPD_BDC;
10226 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10227 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10228 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10230 ctsio->scsi_status = SCSI_STATUS_OK;
10231 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10232 ctsio->be_move_done = ctl_config_move_done;
10233 ctl_datamove((union ctl_io *)ctsio);
10235 return (CTL_RETVAL_COMPLETE);
10239 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10241 struct scsi_vpd_logical_block_prov *lbp_ptr;
10242 struct ctl_lun *lun;
10244 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10246 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10247 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10248 ctsio->kern_sg_entries = 0;
10250 if (sizeof(*lbp_ptr) < alloc_len) {
10251 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10252 ctsio->kern_data_len = sizeof(*lbp_ptr);
10253 ctsio->kern_total_len = sizeof(*lbp_ptr);
10255 ctsio->residual = 0;
10256 ctsio->kern_data_len = alloc_len;
10257 ctsio->kern_total_len = alloc_len;
10259 ctsio->kern_data_resid = 0;
10260 ctsio->kern_rel_offset = 0;
10261 ctsio->kern_sg_entries = 0;
10264 * The control device is always connected. The disk device, on the
10265 * other hand, may not be online all the time. Need to change this
10266 * to figure out whether the disk device is actually online or not.
10269 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10270 lun->be_lun->lun_type;
10272 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10274 lbp_ptr->page_code = SVPD_LBP;
10275 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10276 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10277 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10278 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10279 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10282 ctsio->scsi_status = SCSI_STATUS_OK;
10283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10284 ctsio->be_move_done = ctl_config_move_done;
10285 ctl_datamove((union ctl_io *)ctsio);
10287 return (CTL_RETVAL_COMPLETE);
10291 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10293 struct scsi_inquiry *cdb;
10294 struct ctl_lun *lun;
10295 int alloc_len, retval;
10297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10298 cdb = (struct scsi_inquiry *)ctsio->cdb;
10300 retval = CTL_RETVAL_COMPLETE;
10302 alloc_len = scsi_2btoul(cdb->length);
10304 switch (cdb->page_code) {
10305 case SVPD_SUPPORTED_PAGES:
10306 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10308 case SVPD_UNIT_SERIAL_NUMBER:
10309 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10311 case SVPD_DEVICE_ID:
10312 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10314 case SVPD_SCSI_PORTS:
10315 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10317 case SVPD_SCSI_TPC:
10318 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10320 case SVPD_BLOCK_LIMITS:
10321 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10324 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10327 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10330 ctl_set_invalid_field(ctsio,
10336 ctl_done((union ctl_io *)ctsio);
10337 retval = CTL_RETVAL_COMPLETE;
10345 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10347 struct scsi_inquiry_data *inq_ptr;
10348 struct scsi_inquiry *cdb;
10349 struct ctl_softc *ctl_softc;
10350 struct ctl_lun *lun;
10352 uint32_t alloc_len;
10353 ctl_port_type port_type;
10355 ctl_softc = control_softc;
10358 * Figure out whether we're talking to a Fibre Channel port or not.
10359 * We treat the ioctl front end, and any SCSI adapters, as packetized
10362 port_type = ctl_softc->ctl_ports[
10363 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10364 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10365 port_type = CTL_PORT_SCSI;
10367 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10368 cdb = (struct scsi_inquiry *)ctsio->cdb;
10369 alloc_len = scsi_2btoul(cdb->length);
10372 * We malloc the full inquiry data size here and fill it
10373 * in. If the user only asks for less, we'll give him
10376 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10377 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10378 ctsio->kern_sg_entries = 0;
10379 ctsio->kern_data_resid = 0;
10380 ctsio->kern_rel_offset = 0;
10382 if (sizeof(*inq_ptr) < alloc_len) {
10383 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10384 ctsio->kern_data_len = sizeof(*inq_ptr);
10385 ctsio->kern_total_len = sizeof(*inq_ptr);
10387 ctsio->residual = 0;
10388 ctsio->kern_data_len = alloc_len;
10389 ctsio->kern_total_len = alloc_len;
10393 * If we have a LUN configured, report it as connected. Otherwise,
10394 * report that it is offline or no device is supported, depending
10395 * on the value of inquiry_pq_no_lun.
10397 * According to the spec (SPC-4 r34), the peripheral qualifier
10398 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10400 * "A peripheral device having the specified peripheral device type
10401 * is not connected to this logical unit. However, the device
10402 * server is capable of supporting the specified peripheral device
10403 * type on this logical unit."
10405 * According to the same spec, the peripheral qualifier
10406 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10408 * "The device server is not capable of supporting a peripheral
10409 * device on this logical unit. For this peripheral qualifier the
10410 * peripheral device type shall be set to 1Fh. All other peripheral
10411 * device type values are reserved for this peripheral qualifier."
10413 * Given the text, it would seem that we probably want to report that
10414 * the LUN is offline here. There is no LUN connected, but we can
10415 * support a LUN at the given LUN number.
10417 * In the real world, though, it sounds like things are a little
10420 * - Linux, when presented with a LUN with the offline peripheral
10421 * qualifier, will create an sg driver instance for it. So when
10422 * you attach it to CTL, you wind up with a ton of sg driver
10423 * instances. (One for every LUN that Linux bothered to probe.)
10424 * Linux does this despite the fact that it issues a REPORT LUNs
10425 * to LUN 0 to get the inventory of supported LUNs.
10427 * - There is other anecdotal evidence (from Emulex folks) about
10428 * arrays that use the offline peripheral qualifier for LUNs that
10429 * are on the "passive" path in an active/passive array.
10431 * So the solution is provide a hopefully reasonable default
10432 * (return bad/no LUN) and allow the user to change the behavior
10433 * with a tunable/sysctl variable.
10436 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10437 lun->be_lun->lun_type;
10438 else if (ctl_softc->inquiry_pq_no_lun == 0)
10439 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10441 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10443 /* RMB in byte 2 is 0 */
10444 inq_ptr->version = SCSI_REV_SPC4;
10447 * According to SAM-3, even if a device only supports a single
10448 * level of LUN addressing, it should still set the HISUP bit:
10450 * 4.9.1 Logical unit numbers overview
10452 * All logical unit number formats described in this standard are
10453 * hierarchical in structure even when only a single level in that
10454 * hierarchy is used. The HISUP bit shall be set to one in the
10455 * standard INQUIRY data (see SPC-2) when any logical unit number
10456 * format described in this standard is used. Non-hierarchical
10457 * formats are outside the scope of this standard.
10459 * Therefore we set the HiSup bit here.
10461 * The reponse format is 2, per SPC-3.
10463 inq_ptr->response_format = SID_HiSup | 2;
10465 inq_ptr->additional_length =
10466 offsetof(struct scsi_inquiry_data, vendor_specific1) -
10467 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10468 CTL_DEBUG_PRINT(("additional_length = %d\n",
10469 inq_ptr->additional_length));
10471 inq_ptr->spc3_flags = SPC3_SID_3PC;
10472 if (!ctl_is_single)
10473 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT;
10474 /* 16 bit addressing */
10475 if (port_type == CTL_PORT_SCSI)
10476 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10477 /* XXX set the SID_MultiP bit here if we're actually going to
10478 respond on multiple ports */
10479 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10481 /* 16 bit data bus, synchronous transfers */
10482 if (port_type == CTL_PORT_SCSI)
10483 inq_ptr->flags = SID_WBus16 | SID_Sync;
10485 * XXX KDM do we want to support tagged queueing on the control
10489 || (lun->be_lun->lun_type != T_PROCESSOR))
10490 inq_ptr->flags |= SID_CmdQue;
10492 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10493 * We have 8 bytes for the vendor name, and 16 bytes for the device
10494 * name and 4 bytes for the revision.
10496 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10497 "vendor")) == NULL) {
10498 strcpy(inq_ptr->vendor, CTL_VENDOR);
10500 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10501 strncpy(inq_ptr->vendor, val,
10502 min(sizeof(inq_ptr->vendor), strlen(val)));
10505 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10506 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10507 switch (lun->be_lun->lun_type) {
10509 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10512 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10515 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10519 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10520 strncpy(inq_ptr->product, val,
10521 min(sizeof(inq_ptr->product), strlen(val)));
10525 * XXX make this a macro somewhere so it automatically gets
10526 * incremented when we make changes.
10528 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10529 "revision")) == NULL) {
10530 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10532 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10533 strncpy(inq_ptr->revision, val,
10534 min(sizeof(inq_ptr->revision), strlen(val)));
10538 * For parallel SCSI, we support double transition and single
10539 * transition clocking. We also support QAS (Quick Arbitration
10540 * and Selection) and Information Unit transfers on both the
10541 * control and array devices.
10543 if (port_type == CTL_PORT_SCSI)
10544 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10547 /* SAM-5 (no version claimed) */
10548 scsi_ulto2b(0x00A0, inq_ptr->version1);
10549 /* SPC-4 (no version claimed) */
10550 scsi_ulto2b(0x0460, inq_ptr->version2);
10551 if (port_type == CTL_PORT_FC) {
10552 /* FCP-2 ANSI INCITS.350:2003 */
10553 scsi_ulto2b(0x0917, inq_ptr->version3);
10554 } else if (port_type == CTL_PORT_SCSI) {
10555 /* SPI-4 ANSI INCITS.362:200x */
10556 scsi_ulto2b(0x0B56, inq_ptr->version3);
10557 } else if (port_type == CTL_PORT_ISCSI) {
10558 /* iSCSI (no version claimed) */
10559 scsi_ulto2b(0x0960, inq_ptr->version3);
10560 } else if (port_type == CTL_PORT_SAS) {
10561 /* SAS (no version claimed) */
10562 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10566 /* SBC-3 (no version claimed) */
10567 scsi_ulto2b(0x04C0, inq_ptr->version4);
10569 switch (lun->be_lun->lun_type) {
10571 /* SBC-3 (no version claimed) */
10572 scsi_ulto2b(0x04C0, inq_ptr->version4);
10580 ctsio->scsi_status = SCSI_STATUS_OK;
10581 if (ctsio->kern_data_len > 0) {
10582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10583 ctsio->be_move_done = ctl_config_move_done;
10584 ctl_datamove((union ctl_io *)ctsio);
10586 ctsio->io_hdr.status = CTL_SUCCESS;
10587 ctl_done((union ctl_io *)ctsio);
10590 return (CTL_RETVAL_COMPLETE);
10594 ctl_inquiry(struct ctl_scsiio *ctsio)
10596 struct scsi_inquiry *cdb;
10599 cdb = (struct scsi_inquiry *)ctsio->cdb;
10603 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10606 * Right now, we don't support the CmdDt inquiry information.
10607 * This would be nice to support in the future. When we do
10608 * support it, we should change this test so that it checks to make
10609 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10612 if (((cdb->byte2 & SI_EVPD)
10613 && (cdb->byte2 & SI_CMDDT)))
10615 if (cdb->byte2 & SI_CMDDT) {
10617 * Point to the SI_CMDDT bit. We might change this
10618 * when we support SI_CMDDT, but since both bits would be
10619 * "wrong", this should probably just stay as-is then.
10621 ctl_set_invalid_field(ctsio,
10627 ctl_done((union ctl_io *)ctsio);
10628 return (CTL_RETVAL_COMPLETE);
10630 if (cdb->byte2 & SI_EVPD)
10631 retval = ctl_inquiry_evpd(ctsio);
10633 else if (cdb->byte2 & SI_CMDDT)
10634 retval = ctl_inquiry_cmddt(ctsio);
10637 retval = ctl_inquiry_std(ctsio);
10643 * For known CDB types, parse the LBA and length.
10646 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10648 if (io->io_hdr.io_type != CTL_IO_SCSI)
10651 switch (io->scsiio.cdb[0]) {
10652 case COMPARE_AND_WRITE: {
10653 struct scsi_compare_and_write *cdb;
10655 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10657 *lba = scsi_8btou64(cdb->addr);
10658 *len = cdb->length;
10663 struct scsi_rw_6 *cdb;
10665 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10667 *lba = scsi_3btoul(cdb->addr);
10668 /* only 5 bits are valid in the most significant address byte */
10670 *len = cdb->length;
10675 struct scsi_rw_10 *cdb;
10677 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10679 *lba = scsi_4btoul(cdb->addr);
10680 *len = scsi_2btoul(cdb->length);
10683 case WRITE_VERIFY_10: {
10684 struct scsi_write_verify_10 *cdb;
10686 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10688 *lba = scsi_4btoul(cdb->addr);
10689 *len = scsi_2btoul(cdb->length);
10694 struct scsi_rw_12 *cdb;
10696 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10698 *lba = scsi_4btoul(cdb->addr);
10699 *len = scsi_4btoul(cdb->length);
10702 case WRITE_VERIFY_12: {
10703 struct scsi_write_verify_12 *cdb;
10705 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10707 *lba = scsi_4btoul(cdb->addr);
10708 *len = scsi_4btoul(cdb->length);
10713 struct scsi_rw_16 *cdb;
10715 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10717 *lba = scsi_8btou64(cdb->addr);
10718 *len = scsi_4btoul(cdb->length);
10721 case WRITE_VERIFY_16: {
10722 struct scsi_write_verify_16 *cdb;
10724 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10727 *lba = scsi_8btou64(cdb->addr);
10728 *len = scsi_4btoul(cdb->length);
10731 case WRITE_SAME_10: {
10732 struct scsi_write_same_10 *cdb;
10734 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10736 *lba = scsi_4btoul(cdb->addr);
10737 *len = scsi_2btoul(cdb->length);
10740 case WRITE_SAME_16: {
10741 struct scsi_write_same_16 *cdb;
10743 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10745 *lba = scsi_8btou64(cdb->addr);
10746 *len = scsi_4btoul(cdb->length);
10750 struct scsi_verify_10 *cdb;
10752 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10754 *lba = scsi_4btoul(cdb->addr);
10755 *len = scsi_2btoul(cdb->length);
10759 struct scsi_verify_12 *cdb;
10761 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10763 *lba = scsi_4btoul(cdb->addr);
10764 *len = scsi_4btoul(cdb->length);
10768 struct scsi_verify_16 *cdb;
10770 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10772 *lba = scsi_8btou64(cdb->addr);
10773 *len = scsi_4btoul(cdb->length);
10778 break; /* NOTREACHED */
10785 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10787 uint64_t endlba1, endlba2;
10789 endlba1 = lba1 + len1 - 1;
10790 endlba2 = lba2 + len2 - 1;
10792 if ((endlba1 < lba2)
10793 || (endlba2 < lba1))
10794 return (CTL_ACTION_PASS);
10796 return (CTL_ACTION_BLOCK);
10800 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10802 uint64_t lba1, lba2;
10803 uint32_t len1, len2;
10806 retval = ctl_get_lba_len(io1, &lba1, &len1);
10808 return (CTL_ACTION_ERROR);
10810 retval = ctl_get_lba_len(io2, &lba2, &len2);
10812 return (CTL_ACTION_ERROR);
10814 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10818 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10820 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10821 ctl_serialize_action *serialize_row;
10824 * The initiator attempted multiple untagged commands at the same
10825 * time. Can't do that.
10827 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10828 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10829 && ((pending_io->io_hdr.nexus.targ_port ==
10830 ooa_io->io_hdr.nexus.targ_port)
10831 && (pending_io->io_hdr.nexus.initid.id ==
10832 ooa_io->io_hdr.nexus.initid.id))
10833 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10834 return (CTL_ACTION_OVERLAP);
10837 * The initiator attempted to send multiple tagged commands with
10838 * the same ID. (It's fine if different initiators have the same
10841 * Even if all of those conditions are true, we don't kill the I/O
10842 * if the command ahead of us has been aborted. We won't end up
10843 * sending it to the FETD, and it's perfectly legal to resend a
10844 * command with the same tag number as long as the previous
10845 * instance of this tag number has been aborted somehow.
10847 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10848 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10849 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10850 && ((pending_io->io_hdr.nexus.targ_port ==
10851 ooa_io->io_hdr.nexus.targ_port)
10852 && (pending_io->io_hdr.nexus.initid.id ==
10853 ooa_io->io_hdr.nexus.initid.id))
10854 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10855 return (CTL_ACTION_OVERLAP_TAG);
10858 * If we get a head of queue tag, SAM-3 says that we should
10859 * immediately execute it.
10861 * What happens if this command would normally block for some other
10862 * reason? e.g. a request sense with a head of queue tag
10863 * immediately after a write. Normally that would block, but this
10864 * will result in its getting executed immediately...
10866 * We currently return "pass" instead of "skip", so we'll end up
10867 * going through the rest of the queue to check for overlapped tags.
10869 * XXX KDM check for other types of blockage first??
10871 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10872 return (CTL_ACTION_PASS);
10875 * Ordered tags have to block until all items ahead of them
10876 * have completed. If we get called with an ordered tag, we always
10877 * block, if something else is ahead of us in the queue.
10879 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10880 return (CTL_ACTION_BLOCK);
10883 * Simple tags get blocked until all head of queue and ordered tags
10884 * ahead of them have completed. I'm lumping untagged commands in
10885 * with simple tags here. XXX KDM is that the right thing to do?
10887 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10888 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10889 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10890 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10891 return (CTL_ACTION_BLOCK);
10893 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10894 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10896 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10898 switch (serialize_row[pending_entry->seridx]) {
10899 case CTL_SER_BLOCK:
10900 return (CTL_ACTION_BLOCK);
10901 break; /* NOTREACHED */
10902 case CTL_SER_EXTENT:
10903 return (ctl_extent_check(pending_io, ooa_io));
10904 break; /* NOTREACHED */
10906 return (CTL_ACTION_PASS);
10907 break; /* NOTREACHED */
10909 return (CTL_ACTION_SKIP);
10912 panic("invalid serialization value %d",
10913 serialize_row[pending_entry->seridx]);
10914 break; /* NOTREACHED */
10917 return (CTL_ACTION_ERROR);
10921 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10923 * - pending_io is generally either incoming, or on the blocked queue
10924 * - starting I/O is the I/O we want to start the check with.
10927 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10928 union ctl_io *starting_io)
10930 union ctl_io *ooa_io;
10933 mtx_assert(&lun->lun_lock, MA_OWNED);
10936 * Run back along the OOA queue, starting with the current
10937 * blocked I/O and going through every I/O before it on the
10938 * queue. If starting_io is NULL, we'll just end up returning
10941 for (ooa_io = starting_io; ooa_io != NULL;
10942 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10946 * This routine just checks to see whether
10947 * cur_blocked is blocked by ooa_io, which is ahead
10948 * of it in the queue. It doesn't queue/dequeue
10951 action = ctl_check_for_blockage(pending_io, ooa_io);
10953 case CTL_ACTION_BLOCK:
10954 case CTL_ACTION_OVERLAP:
10955 case CTL_ACTION_OVERLAP_TAG:
10956 case CTL_ACTION_SKIP:
10957 case CTL_ACTION_ERROR:
10959 break; /* NOTREACHED */
10960 case CTL_ACTION_PASS:
10963 panic("invalid action %d", action);
10964 break; /* NOTREACHED */
10968 return (CTL_ACTION_PASS);
10973 * - An I/O has just completed, and has been removed from the per-LUN OOA
10974 * queue, so some items on the blocked queue may now be unblocked.
10977 ctl_check_blocked(struct ctl_lun *lun)
10979 union ctl_io *cur_blocked, *next_blocked;
10981 mtx_assert(&lun->lun_lock, MA_OWNED);
10984 * Run forward from the head of the blocked queue, checking each
10985 * entry against the I/Os prior to it on the OOA queue to see if
10986 * there is still any blockage.
10988 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10989 * with our removing a variable on it while it is traversing the
10992 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10993 cur_blocked != NULL; cur_blocked = next_blocked) {
10994 union ctl_io *prev_ooa;
10997 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11000 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11001 ctl_ooaq, ooa_links);
11004 * If cur_blocked happens to be the first item in the OOA
11005 * queue now, prev_ooa will be NULL, and the action
11006 * returned will just be CTL_ACTION_PASS.
11008 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11011 case CTL_ACTION_BLOCK:
11012 /* Nothing to do here, still blocked */
11014 case CTL_ACTION_OVERLAP:
11015 case CTL_ACTION_OVERLAP_TAG:
11017 * This shouldn't happen! In theory we've already
11018 * checked this command for overlap...
11021 case CTL_ACTION_PASS:
11022 case CTL_ACTION_SKIP: {
11023 struct ctl_softc *softc;
11024 const struct ctl_cmd_entry *entry;
11029 * The skip case shouldn't happen, this transaction
11030 * should have never made it onto the blocked queue.
11033 * This I/O is no longer blocked, we can remove it
11034 * from the blocked queue. Since this is a TAILQ
11035 * (doubly linked list), we can do O(1) removals
11036 * from any place on the list.
11038 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11040 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11042 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11044 * Need to send IO back to original side to
11047 union ctl_ha_msg msg_info;
11049 msg_info.hdr.original_sc =
11050 cur_blocked->io_hdr.original_sc;
11051 msg_info.hdr.serializing_sc = cur_blocked;
11052 msg_info.hdr.msg_type = CTL_MSG_R2R;
11053 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11054 &msg_info, sizeof(msg_info), 0)) >
11055 CTL_HA_STATUS_SUCCESS) {
11056 printf("CTL:Check Blocked error from "
11057 "ctl_ha_msg_send %d\n",
11062 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
11063 softc = control_softc;
11065 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11068 * Check this I/O for LUN state changes that may
11069 * have happened while this command was blocked.
11070 * The LUN state may have been changed by a command
11071 * ahead of us in the queue, so we need to re-check
11072 * for any states that can be caused by SCSI
11075 if (ctl_scsiio_lun_check(softc, lun, entry,
11076 &cur_blocked->scsiio) == 0) {
11077 cur_blocked->io_hdr.flags |=
11078 CTL_FLAG_IS_WAS_ON_RTR;
11079 ctl_enqueue_rtr(cur_blocked);
11081 ctl_done(cur_blocked);
11086 * This probably shouldn't happen -- we shouldn't
11087 * get CTL_ACTION_ERROR, or anything else.
11093 return (CTL_RETVAL_COMPLETE);
11097 * This routine (with one exception) checks LUN flags that can be set by
11098 * commands ahead of us in the OOA queue. These flags have to be checked
11099 * when a command initially comes in, and when we pull a command off the
11100 * blocked queue and are preparing to execute it. The reason we have to
11101 * check these flags for commands on the blocked queue is that the LUN
11102 * state may have been changed by a command ahead of us while we're on the
11105 * Ordering is somewhat important with these checks, so please pay
11106 * careful attention to the placement of any new checks.
11109 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11110 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11116 mtx_assert(&lun->lun_lock, MA_OWNED);
11119 * If this shelf is a secondary shelf controller, we have to reject
11120 * any media access commands.
11123 /* No longer needed for HA */
11124 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11125 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11126 ctl_set_lun_standby(ctsio);
11133 * Check for a reservation conflict. If this command isn't allowed
11134 * even on reserved LUNs, and if this initiator isn't the one who
11135 * reserved us, reject the command with a reservation conflict.
11137 if ((lun->flags & CTL_LUN_RESERVED)
11138 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11139 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
11140 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
11141 || (ctsio->io_hdr.nexus.targ_target.id !=
11142 lun->rsv_nexus.targ_target.id)) {
11143 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11144 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11150 if ( (lun->flags & CTL_LUN_PR_RESERVED)
11151 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11154 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11156 * if we aren't registered or it's a res holder type
11157 * reservation and this isn't the res holder then set a
11159 * NOTE: Commands which might be allowed on write exclusive
11160 * type reservations are checked in the particular command
11161 * for a conflict. Read and SSU are the only ones.
11163 if (!lun->per_res[residx].registered
11164 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11165 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11166 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11173 if ((lun->flags & CTL_LUN_OFFLINE)
11174 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11175 ctl_set_lun_not_ready(ctsio);
11181 * If the LUN is stopped, see if this particular command is allowed
11182 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11184 if ((lun->flags & CTL_LUN_STOPPED)
11185 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11186 /* "Logical unit not ready, initializing cmd. required" */
11187 ctl_set_lun_stopped(ctsio);
11192 if ((lun->flags & CTL_LUN_INOPERABLE)
11193 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11194 /* "Medium format corrupted" */
11195 ctl_set_medium_format_corrupted(ctsio);
11206 ctl_failover_io(union ctl_io *io, int have_lock)
11208 ctl_set_busy(&io->scsiio);
11215 struct ctl_lun *lun;
11216 struct ctl_softc *ctl_softc;
11217 union ctl_io *next_io, *pending_io;
11222 ctl_softc = control_softc;
11224 mtx_lock(&ctl_softc->ctl_lock);
11226 * Remove any cmds from the other SC from the rtr queue. These
11227 * will obviously only be for LUNs for which we're the primary.
11228 * We can't send status or get/send data for these commands.
11229 * Since they haven't been executed yet, we can just remove them.
11230 * We'll either abort them or delete them below, depending on
11231 * which HA mode we're in.
11234 mtx_lock(&ctl_softc->queue_lock);
11235 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11236 io != NULL; io = next_io) {
11237 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11238 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11239 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11240 ctl_io_hdr, links);
11242 mtx_unlock(&ctl_softc->queue_lock);
11245 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11246 lun = ctl_softc->ctl_luns[lun_idx];
11251 * Processor LUNs are primary on both sides.
11252 * XXX will this always be true?
11254 if (lun->be_lun->lun_type == T_PROCESSOR)
11257 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11258 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11259 printf("FAILOVER: primary lun %d\n", lun_idx);
11261 * Remove all commands from the other SC. First from the
11262 * blocked queue then from the ooa queue. Once we have
11263 * removed them. Call ctl_check_blocked to see if there
11264 * is anything that can run.
11266 for (io = (union ctl_io *)TAILQ_FIRST(
11267 &lun->blocked_queue); io != NULL; io = next_io) {
11269 next_io = (union ctl_io *)TAILQ_NEXT(
11270 &io->io_hdr, blocked_links);
11272 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11273 TAILQ_REMOVE(&lun->blocked_queue,
11274 &io->io_hdr,blocked_links);
11275 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11276 TAILQ_REMOVE(&lun->ooa_queue,
11277 &io->io_hdr, ooa_links);
11283 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11284 io != NULL; io = next_io) {
11286 next_io = (union ctl_io *)TAILQ_NEXT(
11287 &io->io_hdr, ooa_links);
11289 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11291 TAILQ_REMOVE(&lun->ooa_queue,
11298 ctl_check_blocked(lun);
11299 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11300 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11302 printf("FAILOVER: primary lun %d\n", lun_idx);
11304 * Abort all commands from the other SC. We can't
11305 * send status back for them now. These should get
11306 * cleaned up when they are completed or come out
11307 * for a datamove operation.
11309 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11310 io != NULL; io = next_io) {
11311 next_io = (union ctl_io *)TAILQ_NEXT(
11312 &io->io_hdr, ooa_links);
11314 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11315 io->io_hdr.flags |= CTL_FLAG_ABORT;
11317 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11318 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11320 printf("FAILOVER: secondary lun %d\n", lun_idx);
11322 lun->flags |= CTL_LUN_PRIMARY_SC;
11325 * We send all I/O that was sent to this controller
11326 * and redirected to the other side back with
11327 * busy status, and have the initiator retry it.
11328 * Figuring out how much data has been transferred,
11329 * etc. and picking up where we left off would be
11332 * XXX KDM need to remove I/O from the blocked
11335 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11336 &lun->ooa_queue); pending_io != NULL;
11337 pending_io = next_io) {
11339 next_io = (union ctl_io *)TAILQ_NEXT(
11340 &pending_io->io_hdr, ooa_links);
11342 pending_io->io_hdr.flags &=
11343 ~CTL_FLAG_SENT_2OTHER_SC;
11345 if (pending_io->io_hdr.flags &
11346 CTL_FLAG_IO_ACTIVE) {
11347 pending_io->io_hdr.flags |=
11350 ctl_set_busy(&pending_io->scsiio);
11351 ctl_done(pending_io);
11356 * Build Unit Attention
11358 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11359 lun->pending_ua[i] |=
11360 CTL_UA_ASYM_ACC_CHANGE;
11362 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11363 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11364 printf("FAILOVER: secondary lun %d\n", lun_idx);
11366 * if the first io on the OOA is not on the RtR queue
11369 lun->flags |= CTL_LUN_PRIMARY_SC;
11371 pending_io = (union ctl_io *)TAILQ_FIRST(
11373 if (pending_io==NULL) {
11374 printf("Nothing on OOA queue\n");
11378 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11379 if ((pending_io->io_hdr.flags &
11380 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11381 pending_io->io_hdr.flags |=
11382 CTL_FLAG_IS_WAS_ON_RTR;
11383 ctl_enqueue_rtr(pending_io);
11388 printf("Tag 0x%04x is running\n",
11389 pending_io->scsiio.tag_num);
11393 next_io = (union ctl_io *)TAILQ_NEXT(
11394 &pending_io->io_hdr, ooa_links);
11395 for (pending_io=next_io; pending_io != NULL;
11396 pending_io = next_io) {
11397 pending_io->io_hdr.flags &=
11398 ~CTL_FLAG_SENT_2OTHER_SC;
11399 next_io = (union ctl_io *)TAILQ_NEXT(
11400 &pending_io->io_hdr, ooa_links);
11401 if (pending_io->io_hdr.flags &
11402 CTL_FLAG_IS_WAS_ON_RTR) {
11404 printf("Tag 0x%04x is running\n",
11405 pending_io->scsiio.tag_num);
11410 switch (ctl_check_ooa(lun, pending_io,
11411 (union ctl_io *)TAILQ_PREV(
11412 &pending_io->io_hdr, ctl_ooaq,
11415 case CTL_ACTION_BLOCK:
11416 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11417 &pending_io->io_hdr,
11419 pending_io->io_hdr.flags |=
11422 case CTL_ACTION_PASS:
11423 case CTL_ACTION_SKIP:
11424 pending_io->io_hdr.flags |=
11425 CTL_FLAG_IS_WAS_ON_RTR;
11426 ctl_enqueue_rtr(pending_io);
11428 case CTL_ACTION_OVERLAP:
11429 ctl_set_overlapped_cmd(
11430 (struct ctl_scsiio *)pending_io);
11431 ctl_done(pending_io);
11433 case CTL_ACTION_OVERLAP_TAG:
11434 ctl_set_overlapped_tag(
11435 (struct ctl_scsiio *)pending_io,
11436 pending_io->scsiio.tag_num & 0xff);
11437 ctl_done(pending_io);
11439 case CTL_ACTION_ERROR:
11441 ctl_set_internal_failure(
11442 (struct ctl_scsiio *)pending_io,
11445 ctl_done(pending_io);
11451 * Build Unit Attention
11453 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11454 lun->pending_ua[i] |=
11455 CTL_UA_ASYM_ACC_CHANGE;
11458 panic("Unhandled HA mode failover, LUN flags = %#x, "
11459 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11463 mtx_unlock(&ctl_softc->ctl_lock);
11467 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11469 struct ctl_lun *lun;
11470 const struct ctl_cmd_entry *entry;
11471 uint32_t initidx, targ_lun;
11478 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11479 if ((targ_lun < CTL_MAX_LUNS)
11480 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11481 lun = ctl_softc->ctl_luns[targ_lun];
11483 * If the LUN is invalid, pretend that it doesn't exist.
11484 * It will go away as soon as all pending I/O has been
11487 if (lun->flags & CTL_LUN_DISABLED) {
11490 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11491 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11493 if (lun->be_lun->lun_type == T_PROCESSOR) {
11494 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11498 * Every I/O goes into the OOA queue for a
11499 * particular LUN, and stays there until completion.
11501 mtx_lock(&lun->lun_lock);
11502 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11506 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11507 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11510 /* Get command entry and return error if it is unsuppotyed. */
11511 entry = ctl_validate_command(ctsio);
11512 if (entry == NULL) {
11514 mtx_unlock(&lun->lun_lock);
11518 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11519 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11522 * Check to see whether we can send this command to LUNs that don't
11523 * exist. This should pretty much only be the case for inquiry
11524 * and request sense. Further checks, below, really require having
11525 * a LUN, so we can't really check the command anymore. Just put
11526 * it on the rtr queue.
11529 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11530 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11531 ctl_enqueue_rtr((union ctl_io *)ctsio);
11535 ctl_set_unsupported_lun(ctsio);
11536 ctl_done((union ctl_io *)ctsio);
11537 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11541 * Make sure we support this particular command on this LUN.
11542 * e.g., we don't support writes to the control LUN.
11544 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11545 mtx_unlock(&lun->lun_lock);
11546 ctl_set_invalid_opcode(ctsio);
11547 ctl_done((union ctl_io *)ctsio);
11552 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11556 * If we've got a request sense, it'll clear the contingent
11557 * allegiance condition. Otherwise, if we have a CA condition for
11558 * this initiator, clear it, because it sent down a command other
11559 * than request sense.
11561 if ((ctsio->cdb[0] != REQUEST_SENSE)
11562 && (ctl_is_set(lun->have_ca, initidx)))
11563 ctl_clear_mask(lun->have_ca, initidx);
11567 * If the command has this flag set, it handles its own unit
11568 * attention reporting, we shouldn't do anything. Otherwise we
11569 * check for any pending unit attentions, and send them back to the
11570 * initiator. We only do this when a command initially comes in,
11571 * not when we pull it off the blocked queue.
11573 * According to SAM-3, section 5.3.2, the order that things get
11574 * presented back to the host is basically unit attentions caused
11575 * by some sort of reset event, busy status, reservation conflicts
11576 * or task set full, and finally any other status.
11578 * One issue here is that some of the unit attentions we report
11579 * don't fall into the "reset" category (e.g. "reported luns data
11580 * has changed"). So reporting it here, before the reservation
11581 * check, may be technically wrong. I guess the only thing to do
11582 * would be to check for and report the reset events here, and then
11583 * check for the other unit attention types after we check for a
11584 * reservation conflict.
11586 * XXX KDM need to fix this
11588 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11589 ctl_ua_type ua_type;
11591 ua_type = lun->pending_ua[initidx];
11592 if (ua_type != CTL_UA_NONE) {
11593 scsi_sense_data_type sense_format;
11596 sense_format = (lun->flags &
11597 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11600 sense_format = SSD_TYPE_FIXED;
11602 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11604 if (ua_type != CTL_UA_NONE) {
11605 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11606 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11608 ctsio->sense_len = SSD_FULL_SIZE;
11609 lun->pending_ua[initidx] &= ~ua_type;
11610 mtx_unlock(&lun->lun_lock);
11611 ctl_done((union ctl_io *)ctsio);
11618 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11619 mtx_unlock(&lun->lun_lock);
11620 ctl_done((union ctl_io *)ctsio);
11625 * XXX CHD this is where we want to send IO to other side if
11626 * this LUN is secondary on this SC. We will need to make a copy
11627 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11628 * the copy we send as FROM_OTHER.
11629 * We also need to stuff the address of the original IO so we can
11630 * find it easily. Something similar will need be done on the other
11631 * side so when we are done we can find the copy.
11633 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11634 union ctl_ha_msg msg_info;
11637 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11639 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11640 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11642 printf("1. ctsio %p\n", ctsio);
11644 msg_info.hdr.serializing_sc = NULL;
11645 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11646 msg_info.scsi.tag_num = ctsio->tag_num;
11647 msg_info.scsi.tag_type = ctsio->tag_type;
11648 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11650 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11652 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11653 (void *)&msg_info, sizeof(msg_info), 0)) >
11654 CTL_HA_STATUS_SUCCESS) {
11655 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11657 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11660 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11665 * XXX KDM this I/O is off the incoming queue, but hasn't
11666 * been inserted on any other queue. We may need to come
11667 * up with a holding queue while we wait for serialization
11668 * so that we have an idea of what we're waiting for from
11671 mtx_unlock(&lun->lun_lock);
11675 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11676 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11677 ctl_ooaq, ooa_links))) {
11678 case CTL_ACTION_BLOCK:
11679 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11680 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11682 mtx_unlock(&lun->lun_lock);
11684 case CTL_ACTION_PASS:
11685 case CTL_ACTION_SKIP:
11686 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11687 mtx_unlock(&lun->lun_lock);
11688 ctl_enqueue_rtr((union ctl_io *)ctsio);
11690 case CTL_ACTION_OVERLAP:
11691 mtx_unlock(&lun->lun_lock);
11692 ctl_set_overlapped_cmd(ctsio);
11693 ctl_done((union ctl_io *)ctsio);
11695 case CTL_ACTION_OVERLAP_TAG:
11696 mtx_unlock(&lun->lun_lock);
11697 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11698 ctl_done((union ctl_io *)ctsio);
11700 case CTL_ACTION_ERROR:
11702 mtx_unlock(&lun->lun_lock);
11703 ctl_set_internal_failure(ctsio,
11705 /*retry_count*/ 0);
11706 ctl_done((union ctl_io *)ctsio);
11712 const struct ctl_cmd_entry *
11713 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11715 const struct ctl_cmd_entry *entry;
11716 int service_action;
11718 entry = &ctl_cmd_table[ctsio->cdb[0]];
11719 if (entry->flags & CTL_CMD_FLAG_SA5) {
11720 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11721 entry = &((const struct ctl_cmd_entry *)
11722 entry->execute)[service_action];
11727 const struct ctl_cmd_entry *
11728 ctl_validate_command(struct ctl_scsiio *ctsio)
11730 const struct ctl_cmd_entry *entry;
11734 entry = ctl_get_cmd_entry(ctsio);
11735 if (entry->execute == NULL) {
11736 ctl_set_invalid_opcode(ctsio);
11737 ctl_done((union ctl_io *)ctsio);
11740 KASSERT(entry->length > 0,
11741 ("Not defined length for command 0x%02x/0x%02x",
11742 ctsio->cdb[0], ctsio->cdb[1]));
11743 for (i = 1; i < entry->length; i++) {
11744 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11747 ctl_set_invalid_field(ctsio,
11752 /*bit*/ fls(diff) - 1);
11753 ctl_done((union ctl_io *)ctsio);
11760 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11763 switch (lun_type) {
11765 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11766 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11770 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11771 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11781 ctl_scsiio(struct ctl_scsiio *ctsio)
11784 const struct ctl_cmd_entry *entry;
11786 retval = CTL_RETVAL_COMPLETE;
11788 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11790 entry = ctl_get_cmd_entry(ctsio);
11793 * If this I/O has been aborted, just send it straight to
11794 * ctl_done() without executing it.
11796 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11797 ctl_done((union ctl_io *)ctsio);
11802 * All the checks should have been handled by ctl_scsiio_precheck().
11803 * We should be clear now to just execute the I/O.
11805 retval = entry->execute(ctsio);
11812 * Since we only implement one target right now, a bus reset simply resets
11813 * our single target.
11816 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11818 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11822 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11823 ctl_ua_type ua_type)
11825 struct ctl_lun *lun;
11828 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11829 union ctl_ha_msg msg_info;
11831 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11832 msg_info.hdr.nexus = io->io_hdr.nexus;
11833 if (ua_type==CTL_UA_TARG_RESET)
11834 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11836 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11837 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11838 msg_info.hdr.original_sc = NULL;
11839 msg_info.hdr.serializing_sc = NULL;
11840 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11841 (void *)&msg_info, sizeof(msg_info), 0)) {
11846 mtx_lock(&ctl_softc->ctl_lock);
11847 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11848 retval += ctl_lun_reset(lun, io, ua_type);
11849 mtx_unlock(&ctl_softc->ctl_lock);
11855 * The LUN should always be set. The I/O is optional, and is used to
11856 * distinguish between I/Os sent by this initiator, and by other
11857 * initiators. We set unit attention for initiators other than this one.
11858 * SAM-3 is vague on this point. It does say that a unit attention should
11859 * be established for other initiators when a LUN is reset (see section
11860 * 5.7.3), but it doesn't specifically say that the unit attention should
11861 * be established for this particular initiator when a LUN is reset. Here
11862 * is the relevant text, from SAM-3 rev 8:
11864 * 5.7.2 When a SCSI initiator port aborts its own tasks
11866 * When a SCSI initiator port causes its own task(s) to be aborted, no
11867 * notification that the task(s) have been aborted shall be returned to
11868 * the SCSI initiator port other than the completion response for the
11869 * command or task management function action that caused the task(s) to
11870 * be aborted and notification(s) associated with related effects of the
11871 * action (e.g., a reset unit attention condition).
11873 * XXX KDM for now, we're setting unit attention for all initiators.
11876 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11880 uint32_t initindex;
11884 mtx_lock(&lun->lun_lock);
11886 * Run through the OOA queue and abort each I/O.
11889 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11891 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11892 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11893 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11897 * This version sets unit attention for every
11900 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11901 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11902 if (initindex == i)
11904 lun->pending_ua[i] |= ua_type;
11909 * A reset (any kind, really) clears reservations established with
11910 * RESERVE/RELEASE. It does not clear reservations established
11911 * with PERSISTENT RESERVE OUT, but we don't support that at the
11912 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11913 * reservations made with the RESERVE/RELEASE commands, because
11914 * those commands are obsolete in SPC-3.
11916 lun->flags &= ~CTL_LUN_RESERVED;
11918 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11920 ctl_clear_mask(lun->have_ca, i);
11922 lun->pending_ua[i] |= ua_type;
11924 mtx_unlock(&lun->lun_lock);
11930 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11936 mtx_assert(&lun->lun_lock, MA_OWNED);
11939 * Run through the OOA queue and attempt to find the given I/O.
11940 * The target port, initiator ID, tag type and tag number have to
11941 * match the values that we got from the initiator. If we have an
11942 * untagged command to abort, simply abort the first untagged command
11943 * we come to. We only allow one untagged command at a time of course.
11945 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11946 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11948 if ((targ_port == UINT32_MAX ||
11949 targ_port == xio->io_hdr.nexus.targ_port) &&
11950 (init_id == UINT32_MAX ||
11951 init_id == xio->io_hdr.nexus.initid.id)) {
11952 if (targ_port != xio->io_hdr.nexus.targ_port ||
11953 init_id != xio->io_hdr.nexus.initid.id)
11954 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11955 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11957 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11958 union ctl_ha_msg msg_info;
11960 msg_info.hdr.nexus = xio->io_hdr.nexus;
11961 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11962 msg_info.task.tag_num = xio->scsiio.tag_num;
11963 msg_info.task.tag_type = xio->scsiio.tag_type;
11964 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11965 msg_info.hdr.original_sc = NULL;
11966 msg_info.hdr.serializing_sc = NULL;
11967 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11968 (void *)&msg_info, sizeof(msg_info), 0);
11976 ctl_abort_task_set(union ctl_io *io)
11978 struct ctl_softc *softc = control_softc;
11979 struct ctl_lun *lun;
11985 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11986 mtx_lock(&softc->ctl_lock);
11987 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11988 lun = softc->ctl_luns[targ_lun];
11990 mtx_unlock(&softc->ctl_lock);
11994 mtx_lock(&lun->lun_lock);
11995 mtx_unlock(&softc->ctl_lock);
11996 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11997 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11998 io->io_hdr.nexus.initid.id,
11999 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12000 } else { /* CTL_TASK_CLEAR_TASK_SET */
12001 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12002 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12004 mtx_unlock(&lun->lun_lock);
12009 ctl_i_t_nexus_reset(union ctl_io *io)
12011 struct ctl_softc *softc = control_softc;
12012 struct ctl_lun *lun;
12013 uint32_t initindex;
12015 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12016 mtx_lock(&softc->ctl_lock);
12017 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12018 mtx_lock(&lun->lun_lock);
12019 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12020 io->io_hdr.nexus.initid.id,
12021 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12023 ctl_clear_mask(lun->have_ca, initindex);
12025 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12026 mtx_unlock(&lun->lun_lock);
12028 mtx_unlock(&softc->ctl_lock);
12033 ctl_abort_task(union ctl_io *io)
12036 struct ctl_lun *lun;
12037 struct ctl_softc *ctl_softc;
12040 char printbuf[128];
12045 ctl_softc = control_softc;
12051 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12052 mtx_lock(&ctl_softc->ctl_lock);
12053 if ((targ_lun < CTL_MAX_LUNS)
12054 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12055 lun = ctl_softc->ctl_luns[targ_lun];
12057 mtx_unlock(&ctl_softc->ctl_lock);
12062 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12063 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12066 mtx_lock(&lun->lun_lock);
12067 mtx_unlock(&ctl_softc->ctl_lock);
12069 * Run through the OOA queue and attempt to find the given I/O.
12070 * The target port, initiator ID, tag type and tag number have to
12071 * match the values that we got from the initiator. If we have an
12072 * untagged command to abort, simply abort the first untagged command
12073 * we come to. We only allow one untagged command at a time of course.
12076 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12078 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12079 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12081 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12083 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12084 lun->lun, xio->scsiio.tag_num,
12085 xio->scsiio.tag_type,
12086 (xio->io_hdr.blocked_links.tqe_prev
12087 == NULL) ? "" : " BLOCKED",
12088 (xio->io_hdr.flags &
12089 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12090 (xio->io_hdr.flags &
12091 CTL_FLAG_ABORT) ? " ABORT" : "",
12092 (xio->io_hdr.flags &
12093 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12094 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12096 printf("%s\n", sbuf_data(&sb));
12099 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12100 && (xio->io_hdr.nexus.initid.id ==
12101 io->io_hdr.nexus.initid.id)) {
12103 * If the abort says that the task is untagged, the
12104 * task in the queue must be untagged. Otherwise,
12105 * we just check to see whether the tag numbers
12106 * match. This is because the QLogic firmware
12107 * doesn't pass back the tag type in an abort
12111 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12112 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12113 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12116 * XXX KDM we've got problems with FC, because it
12117 * doesn't send down a tag type with aborts. So we
12118 * can only really go by the tag number...
12119 * This may cause problems with parallel SCSI.
12120 * Need to figure that out!!
12122 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12123 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12125 if ((io->io_hdr.flags &
12126 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12127 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12128 union ctl_ha_msg msg_info;
12130 io->io_hdr.flags |=
12131 CTL_FLAG_SENT_2OTHER_SC;
12132 msg_info.hdr.nexus = io->io_hdr.nexus;
12133 msg_info.task.task_action =
12134 CTL_TASK_ABORT_TASK;
12135 msg_info.task.tag_num =
12136 io->taskio.tag_num;
12137 msg_info.task.tag_type =
12138 io->taskio.tag_type;
12139 msg_info.hdr.msg_type =
12140 CTL_MSG_MANAGE_TASKS;
12141 msg_info.hdr.original_sc = NULL;
12142 msg_info.hdr.serializing_sc = NULL;
12144 printf("Sent Abort to other side\n");
12146 if (CTL_HA_STATUS_SUCCESS !=
12147 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12149 sizeof(msg_info), 0)) {
12153 printf("ctl_abort_task: found I/O to abort\n");
12159 mtx_unlock(&lun->lun_lock);
12163 * This isn't really an error. It's entirely possible for
12164 * the abort and command completion to cross on the wire.
12165 * This is more of an informative/diagnostic error.
12168 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12169 "%d:%d:%d:%d tag %d type %d\n",
12170 io->io_hdr.nexus.initid.id,
12171 io->io_hdr.nexus.targ_port,
12172 io->io_hdr.nexus.targ_target.id,
12173 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12174 io->taskio.tag_type);
12181 ctl_run_task(union ctl_io *io)
12183 struct ctl_softc *ctl_softc = control_softc;
12185 const char *task_desc;
12187 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12189 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12190 ("ctl_run_task: Unextected io_type %d\n",
12191 io->io_hdr.io_type));
12193 task_desc = ctl_scsi_task_string(&io->taskio);
12194 if (task_desc != NULL) {
12196 csevent_log(CSC_CTL | CSC_SHELF_SW |
12198 csevent_LogType_Trace,
12199 csevent_Severity_Information,
12200 csevent_AlertLevel_Green,
12201 csevent_FRU_Firmware,
12202 csevent_FRU_Unknown,
12203 "CTL: received task: %s",task_desc);
12207 csevent_log(CSC_CTL | CSC_SHELF_SW |
12209 csevent_LogType_Trace,
12210 csevent_Severity_Information,
12211 csevent_AlertLevel_Green,
12212 csevent_FRU_Firmware,
12213 csevent_FRU_Unknown,
12214 "CTL: received unknown task "
12216 io->taskio.task_action,
12217 io->taskio.task_action);
12220 switch (io->taskio.task_action) {
12221 case CTL_TASK_ABORT_TASK:
12222 retval = ctl_abort_task(io);
12224 case CTL_TASK_ABORT_TASK_SET:
12225 case CTL_TASK_CLEAR_TASK_SET:
12226 retval = ctl_abort_task_set(io);
12228 case CTL_TASK_CLEAR_ACA:
12230 case CTL_TASK_I_T_NEXUS_RESET:
12231 retval = ctl_i_t_nexus_reset(io);
12233 case CTL_TASK_LUN_RESET: {
12234 struct ctl_lun *lun;
12237 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12238 mtx_lock(&ctl_softc->ctl_lock);
12239 if ((targ_lun < CTL_MAX_LUNS)
12240 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12241 lun = ctl_softc->ctl_luns[targ_lun];
12243 mtx_unlock(&ctl_softc->ctl_lock);
12248 if (!(io->io_hdr.flags &
12249 CTL_FLAG_FROM_OTHER_SC)) {
12250 union ctl_ha_msg msg_info;
12252 io->io_hdr.flags |=
12253 CTL_FLAG_SENT_2OTHER_SC;
12254 msg_info.hdr.msg_type =
12255 CTL_MSG_MANAGE_TASKS;
12256 msg_info.hdr.nexus = io->io_hdr.nexus;
12257 msg_info.task.task_action =
12258 CTL_TASK_LUN_RESET;
12259 msg_info.hdr.original_sc = NULL;
12260 msg_info.hdr.serializing_sc = NULL;
12261 if (CTL_HA_STATUS_SUCCESS !=
12262 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12264 sizeof(msg_info), 0)) {
12268 retval = ctl_lun_reset(lun, io,
12270 mtx_unlock(&ctl_softc->ctl_lock);
12273 case CTL_TASK_TARGET_RESET:
12274 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12276 case CTL_TASK_BUS_RESET:
12277 retval = ctl_bus_reset(ctl_softc, io);
12279 case CTL_TASK_PORT_LOGIN:
12281 case CTL_TASK_PORT_LOGOUT:
12284 printf("ctl_run_task: got unknown task management event %d\n",
12285 io->taskio.task_action);
12289 io->io_hdr.status = CTL_SUCCESS;
12291 io->io_hdr.status = CTL_ERROR;
12296 * For HA operation. Handle commands that come in from the other
12300 ctl_handle_isc(union ctl_io *io)
12303 struct ctl_lun *lun;
12304 struct ctl_softc *ctl_softc;
12307 ctl_softc = control_softc;
12309 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12310 lun = ctl_softc->ctl_luns[targ_lun];
12312 switch (io->io_hdr.msg_type) {
12313 case CTL_MSG_SERIALIZE:
12314 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12316 case CTL_MSG_R2R: {
12317 const struct ctl_cmd_entry *entry;
12320 * This is only used in SER_ONLY mode.
12323 entry = ctl_get_cmd_entry(&io->scsiio);
12324 mtx_lock(&lun->lun_lock);
12325 if (ctl_scsiio_lun_check(ctl_softc, lun,
12326 entry, (struct ctl_scsiio *)io) != 0) {
12327 mtx_unlock(&lun->lun_lock);
12331 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12332 mtx_unlock(&lun->lun_lock);
12333 ctl_enqueue_rtr(io);
12336 case CTL_MSG_FINISH_IO:
12337 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12342 mtx_lock(&lun->lun_lock);
12343 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12345 ctl_check_blocked(lun);
12346 mtx_unlock(&lun->lun_lock);
12349 case CTL_MSG_PERS_ACTION:
12350 ctl_hndl_per_res_out_on_other_sc(
12351 (union ctl_ha_msg *)&io->presio.pr_msg);
12354 case CTL_MSG_BAD_JUJU:
12358 case CTL_MSG_DATAMOVE:
12359 /* Only used in XFER mode */
12361 ctl_datamove_remote(io);
12363 case CTL_MSG_DATAMOVE_DONE:
12364 /* Only used in XFER mode */
12366 io->scsiio.be_move_done(io);
12370 printf("%s: Invalid message type %d\n",
12371 __func__, io->io_hdr.msg_type);
12381 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12382 * there is no match.
12384 static ctl_lun_error_pattern
12385 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12387 const struct ctl_cmd_entry *entry;
12388 ctl_lun_error_pattern filtered_pattern, pattern;
12390 pattern = desc->error_pattern;
12393 * XXX KDM we need more data passed into this function to match a
12394 * custom pattern, and we actually need to implement custom pattern
12397 if (pattern & CTL_LUN_PAT_CMD)
12398 return (CTL_LUN_PAT_CMD);
12400 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12401 return (CTL_LUN_PAT_ANY);
12403 entry = ctl_get_cmd_entry(ctsio);
12405 filtered_pattern = entry->pattern & pattern;
12408 * If the user requested specific flags in the pattern (e.g.
12409 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12412 * If the user did not specify any flags, it doesn't matter whether
12413 * or not the command supports the flags.
12415 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12416 (pattern & ~CTL_LUN_PAT_MASK))
12417 return (CTL_LUN_PAT_NONE);
12420 * If the user asked for a range check, see if the requested LBA
12421 * range overlaps with this command's LBA range.
12423 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12429 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12431 return (CTL_LUN_PAT_NONE);
12433 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12434 desc->lba_range.len);
12436 * A "pass" means that the LBA ranges don't overlap, so
12437 * this doesn't match the user's range criteria.
12439 if (action == CTL_ACTION_PASS)
12440 return (CTL_LUN_PAT_NONE);
12443 return (filtered_pattern);
12447 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12449 struct ctl_error_desc *desc, *desc2;
12451 mtx_assert(&lun->lun_lock, MA_OWNED);
12453 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12454 ctl_lun_error_pattern pattern;
12456 * Check to see whether this particular command matches
12457 * the pattern in the descriptor.
12459 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12460 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12463 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12464 case CTL_LUN_INJ_ABORTED:
12465 ctl_set_aborted(&io->scsiio);
12467 case CTL_LUN_INJ_MEDIUM_ERR:
12468 ctl_set_medium_error(&io->scsiio);
12470 case CTL_LUN_INJ_UA:
12471 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12473 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12475 case CTL_LUN_INJ_CUSTOM:
12477 * We're assuming the user knows what he is doing.
12478 * Just copy the sense information without doing
12481 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12482 ctl_min(sizeof(desc->custom_sense),
12483 sizeof(io->scsiio.sense_data)));
12484 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12485 io->scsiio.sense_len = SSD_FULL_SIZE;
12486 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12488 case CTL_LUN_INJ_NONE:
12491 * If this is an error injection type we don't know
12492 * about, clear the continuous flag (if it is set)
12493 * so it will get deleted below.
12495 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12499 * By default, each error injection action is a one-shot
12501 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12504 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12510 #ifdef CTL_IO_DELAY
12512 ctl_datamove_timer_wakeup(void *arg)
12516 io = (union ctl_io *)arg;
12520 #endif /* CTL_IO_DELAY */
12523 ctl_datamove(union ctl_io *io)
12525 void (*fe_datamove)(union ctl_io *io);
12527 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12529 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12532 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12537 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12538 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12540 sbuf_cat(&sb, path_str);
12541 switch (io->io_hdr.io_type) {
12543 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12544 sbuf_printf(&sb, "\n");
12545 sbuf_cat(&sb, path_str);
12546 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12547 io->scsiio.tag_num, io->scsiio.tag_type);
12550 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12551 "Tag Type: %d\n", io->taskio.task_action,
12552 io->taskio.tag_num, io->taskio.tag_type);
12555 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12556 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12559 sbuf_cat(&sb, path_str);
12560 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12561 (intmax_t)time_uptime - io->io_hdr.start_time);
12563 printf("%s", sbuf_data(&sb));
12565 #endif /* CTL_TIME_IO */
12567 #ifdef CTL_IO_DELAY
12568 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12569 struct ctl_lun *lun;
12571 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12573 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12575 struct ctl_lun *lun;
12577 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12579 && (lun->delay_info.datamove_delay > 0)) {
12580 struct callout *callout;
12582 callout = (struct callout *)&io->io_hdr.timer_bytes;
12583 callout_init(callout, /*mpsafe*/ 1);
12584 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12585 callout_reset(callout,
12586 lun->delay_info.datamove_delay * hz,
12587 ctl_datamove_timer_wakeup, io);
12588 if (lun->delay_info.datamove_type ==
12589 CTL_DELAY_TYPE_ONESHOT)
12590 lun->delay_info.datamove_delay = 0;
12597 * This command has been aborted. Set the port status, so we fail
12600 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12601 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12602 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12603 io->io_hdr.nexus.targ_port,
12604 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12605 io->io_hdr.nexus.targ_lun);
12606 io->io_hdr.port_status = 31337;
12608 * Note that the backend, in this case, will get the
12609 * callback in its context. In other cases it may get
12610 * called in the frontend's interrupt thread context.
12612 io->scsiio.be_move_done(io);
12617 * If we're in XFER mode and this I/O is from the other shelf
12618 * controller, we need to send the DMA to the other side to
12619 * actually transfer the data to/from the host. In serialize only
12620 * mode the transfer happens below CTL and ctl_datamove() is only
12621 * called on the machine that originally received the I/O.
12623 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12624 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12625 union ctl_ha_msg msg;
12626 uint32_t sg_entries_sent;
12630 memset(&msg, 0, sizeof(msg));
12631 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12632 msg.hdr.original_sc = io->io_hdr.original_sc;
12633 msg.hdr.serializing_sc = io;
12634 msg.hdr.nexus = io->io_hdr.nexus;
12635 msg.dt.flags = io->io_hdr.flags;
12637 * We convert everything into a S/G list here. We can't
12638 * pass by reference, only by value between controllers.
12639 * So we can't pass a pointer to the S/G list, only as many
12640 * S/G entries as we can fit in here. If it's possible for
12641 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12642 * then we need to break this up into multiple transfers.
12644 if (io->scsiio.kern_sg_entries == 0) {
12645 msg.dt.kern_sg_entries = 1;
12647 * If this is in cached memory, flush the cache
12648 * before we send the DMA request to the other
12649 * controller. We want to do this in either the
12650 * read or the write case. The read case is
12651 * straightforward. In the write case, we want to
12652 * make sure nothing is in the local cache that
12653 * could overwrite the DMAed data.
12655 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12657 * XXX KDM use bus_dmamap_sync() here.
12662 * Convert to a physical address if this is a
12665 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12666 msg.dt.sg_list[0].addr =
12667 io->scsiio.kern_data_ptr;
12670 * XXX KDM use busdma here!
12673 msg.dt.sg_list[0].addr = (void *)
12674 vtophys(io->scsiio.kern_data_ptr);
12678 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12681 struct ctl_sg_entry *sgl;
12684 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12685 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12686 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12688 * XXX KDM use bus_dmamap_sync() here.
12693 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12694 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12695 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12696 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12697 msg.dt.sg_sequence = 0;
12700 * Loop until we've sent all of the S/G entries. On the
12701 * other end, we'll recompose these S/G entries into one
12702 * contiguous list before passing it to the
12704 for (sg_entries_sent = 0; sg_entries_sent <
12705 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12706 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12707 sizeof(msg.dt.sg_list[0])),
12708 msg.dt.kern_sg_entries - sg_entries_sent);
12710 if (do_sg_copy != 0) {
12711 struct ctl_sg_entry *sgl;
12714 sgl = (struct ctl_sg_entry *)
12715 io->scsiio.kern_data_ptr;
12717 * If this is in cached memory, flush the cache
12718 * before we send the DMA request to the other
12719 * controller. We want to do this in either
12720 * the * read or the write case. The read
12721 * case is straightforward. In the write
12722 * case, we want to make sure nothing is
12723 * in the local cache that could overwrite
12727 for (i = sg_entries_sent, j = 0;
12728 i < msg.dt.cur_sg_entries; i++, j++) {
12729 if ((io->io_hdr.flags &
12730 CTL_FLAG_NO_DATASYNC) == 0) {
12732 * XXX KDM use bus_dmamap_sync()
12735 if ((io->io_hdr.flags &
12736 CTL_FLAG_BUS_ADDR) == 0) {
12738 * XXX KDM use busdma.
12741 msg.dt.sg_list[j].addr =(void *)
12742 vtophys(sgl[i].addr);
12745 msg.dt.sg_list[j].addr =
12748 msg.dt.sg_list[j].len = sgl[i].len;
12752 sg_entries_sent += msg.dt.cur_sg_entries;
12753 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12754 msg.dt.sg_last = 1;
12756 msg.dt.sg_last = 0;
12759 * XXX KDM drop and reacquire the lock here?
12761 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12762 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12764 * XXX do something here.
12768 msg.dt.sent_sg_entries = sg_entries_sent;
12770 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12771 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12772 ctl_failover_io(io, /*have_lock*/ 0);
12777 * Lookup the fe_datamove() function for this particular
12781 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12788 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12790 union ctl_ha_msg msg;
12793 memset(&msg, 0, sizeof(msg));
12795 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12796 msg.hdr.original_sc = io;
12797 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12798 msg.hdr.nexus = io->io_hdr.nexus;
12799 msg.hdr.status = io->io_hdr.status;
12800 msg.scsi.tag_num = io->scsiio.tag_num;
12801 msg.scsi.tag_type = io->scsiio.tag_type;
12802 msg.scsi.scsi_status = io->scsiio.scsi_status;
12803 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12804 sizeof(io->scsiio.sense_data));
12805 msg.scsi.sense_len = io->scsiio.sense_len;
12806 msg.scsi.sense_residual = io->scsiio.sense_residual;
12807 msg.scsi.fetd_status = io->io_hdr.port_status;
12808 msg.scsi.residual = io->scsiio.residual;
12809 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12811 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12812 ctl_failover_io(io, /*have_lock*/ have_lock);
12816 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12817 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12818 /* XXX do something if this fails */
12824 * The DMA to the remote side is done, now we need to tell the other side
12825 * we're done so it can continue with its data movement.
12828 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12834 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12835 printf("%s: ISC DMA write failed with error %d", __func__,
12837 ctl_set_internal_failure(&io->scsiio,
12839 /*retry_count*/ rq->ret);
12842 ctl_dt_req_free(rq);
12845 * In this case, we had to malloc the memory locally. Free it.
12847 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12849 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12850 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12853 * The data is in local and remote memory, so now we need to send
12854 * status (good or back) back to the other side.
12856 ctl_send_datamove_done(io, /*have_lock*/ 0);
12860 * We've moved the data from the host/controller into local memory. Now we
12861 * need to push it over to the remote controller's memory.
12864 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12870 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12871 ctl_datamove_remote_write_cb);
12877 ctl_datamove_remote_write(union ctl_io *io)
12880 void (*fe_datamove)(union ctl_io *io);
12883 * - Get the data from the host/HBA into local memory.
12884 * - DMA memory from the local controller to the remote controller.
12885 * - Send status back to the remote controller.
12888 retval = ctl_datamove_remote_sgl_setup(io);
12892 /* Switch the pointer over so the FETD knows what to do */
12893 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12896 * Use a custom move done callback, since we need to send completion
12897 * back to the other controller, not to the backend on this side.
12899 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12901 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12910 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12919 * In this case, we had to malloc the memory locally. Free it.
12921 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12923 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12924 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12928 scsi_path_string(io, path_str, sizeof(path_str));
12929 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12930 sbuf_cat(&sb, path_str);
12931 scsi_command_string(&io->scsiio, NULL, &sb);
12932 sbuf_printf(&sb, "\n");
12933 sbuf_cat(&sb, path_str);
12934 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12935 io->scsiio.tag_num, io->scsiio.tag_type);
12936 sbuf_cat(&sb, path_str);
12937 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12938 io->io_hdr.flags, io->io_hdr.status);
12940 printk("%s", sbuf_data(&sb));
12945 * The read is done, now we need to send status (good or bad) back
12946 * to the other side.
12948 ctl_send_datamove_done(io, /*have_lock*/ 0);
12954 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12957 void (*fe_datamove)(union ctl_io *io);
12961 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12962 printf("%s: ISC DMA read failed with error %d", __func__,
12964 ctl_set_internal_failure(&io->scsiio,
12966 /*retry_count*/ rq->ret);
12969 ctl_dt_req_free(rq);
12971 /* Switch the pointer over so the FETD knows what to do */
12972 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12975 * Use a custom move done callback, since we need to send completion
12976 * back to the other controller, not to the backend on this side.
12978 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12980 /* XXX KDM add checks like the ones in ctl_datamove? */
12982 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12988 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12990 struct ctl_sg_entry *local_sglist, *remote_sglist;
12991 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12992 struct ctl_softc *softc;
12997 softc = control_softc;
12999 local_sglist = io->io_hdr.local_sglist;
13000 local_dma_sglist = io->io_hdr.local_dma_sglist;
13001 remote_sglist = io->io_hdr.remote_sglist;
13002 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13004 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13005 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13006 local_sglist[i].len = remote_sglist[i].len;
13009 * XXX Detect the situation where the RS-level I/O
13010 * redirector on the other side has already read the
13011 * data off of the AOR RS on this side, and
13012 * transferred it to remote (mirror) memory on the
13013 * other side. Since we already have the data in
13014 * memory here, we just need to use it.
13016 * XXX KDM this can probably be removed once we
13017 * get the cache device code in and take the
13018 * current AOR implementation out.
13021 if ((remote_sglist[i].addr >=
13022 (void *)vtophys(softc->mirr->addr))
13023 && (remote_sglist[i].addr <
13024 ((void *)vtophys(softc->mirr->addr) +
13025 CacheMirrorOffset))) {
13026 local_sglist[i].addr = remote_sglist[i].addr -
13028 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13030 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13032 local_sglist[i].addr = remote_sglist[i].addr +
13037 printf("%s: local %p, remote %p, len %d\n",
13038 __func__, local_sglist[i].addr,
13039 remote_sglist[i].addr, local_sglist[i].len);
13043 uint32_t len_to_go;
13046 * In this case, we don't have automatically allocated
13047 * memory for this I/O on this controller. This typically
13048 * happens with internal CTL I/O -- e.g. inquiry, mode
13049 * sense, etc. Anything coming from RAIDCore will have
13050 * a mirror area available.
13052 len_to_go = io->scsiio.kern_data_len;
13055 * Clear the no datasync flag, we have to use malloced
13058 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13061 * The difficult thing here is that the size of the various
13062 * S/G segments may be different than the size from the
13063 * remote controller. That'll make it harder when DMAing
13064 * the data back to the other side.
13066 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13067 sizeof(io->io_hdr.remote_sglist[0])) &&
13068 (len_to_go > 0); i++) {
13069 local_sglist[i].len = ctl_min(len_to_go, 131072);
13070 CTL_SIZE_8B(local_dma_sglist[i].len,
13071 local_sglist[i].len);
13072 local_sglist[i].addr =
13073 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13075 local_dma_sglist[i].addr = local_sglist[i].addr;
13077 if (local_sglist[i].addr == NULL) {
13080 printf("malloc failed for %zd bytes!",
13081 local_dma_sglist[i].len);
13082 for (j = 0; j < i; j++) {
13083 free(local_sglist[j].addr, M_CTL);
13085 ctl_set_internal_failure(&io->scsiio,
13087 /*retry_count*/ 4857);
13089 goto bailout_error;
13092 /* XXX KDM do we need a sync here? */
13094 len_to_go -= local_sglist[i].len;
13097 * Reset the number of S/G entries accordingly. The
13098 * original number of S/G entries is available in
13101 io->scsiio.kern_sg_entries = i;
13104 printf("%s: kern_sg_entries = %d\n", __func__,
13105 io->scsiio.kern_sg_entries);
13106 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13107 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13108 local_sglist[i].addr, local_sglist[i].len,
13109 local_dma_sglist[i].len);
13118 ctl_send_datamove_done(io, /*have_lock*/ 0);
13124 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13125 ctl_ha_dt_cb callback)
13127 struct ctl_ha_dt_req *rq;
13128 struct ctl_sg_entry *remote_sglist, *local_sglist;
13129 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13130 uint32_t local_used, remote_used, total_used;
13136 rq = ctl_dt_req_alloc();
13139 * If we failed to allocate the request, and if the DMA didn't fail
13140 * anyway, set busy status. This is just a resource allocation
13144 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13145 ctl_set_busy(&io->scsiio);
13147 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13150 ctl_dt_req_free(rq);
13153 * The data move failed. We need to return status back
13154 * to the other controller. No point in trying to DMA
13155 * data to the remote controller.
13158 ctl_send_datamove_done(io, /*have_lock*/ 0);
13165 local_sglist = io->io_hdr.local_sglist;
13166 local_dma_sglist = io->io_hdr.local_dma_sglist;
13167 remote_sglist = io->io_hdr.remote_sglist;
13168 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13173 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13174 rq->ret = CTL_HA_STATUS_SUCCESS;
13181 * Pull/push the data over the wire from/to the other controller.
13182 * This takes into account the possibility that the local and
13183 * remote sglists may not be identical in terms of the size of
13184 * the elements and the number of elements.
13186 * One fundamental assumption here is that the length allocated for
13187 * both the local and remote sglists is identical. Otherwise, we've
13188 * essentially got a coding error of some sort.
13190 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13192 uint32_t cur_len, dma_length;
13195 rq->id = CTL_HA_DATA_CTL;
13196 rq->command = command;
13200 * Both pointers should be aligned. But it is possible
13201 * that the allocation length is not. They should both
13202 * also have enough slack left over at the end, though,
13203 * to round up to the next 8 byte boundary.
13205 cur_len = ctl_min(local_sglist[i].len - local_used,
13206 remote_sglist[j].len - remote_used);
13209 * In this case, we have a size issue and need to decrease
13210 * the size, except in the case where we actually have less
13211 * than 8 bytes left. In that case, we need to increase
13212 * the DMA length to get the last bit.
13214 if ((cur_len & 0x7) != 0) {
13215 if (cur_len > 0x7) {
13216 cur_len = cur_len - (cur_len & 0x7);
13217 dma_length = cur_len;
13219 CTL_SIZE_8B(dma_length, cur_len);
13223 dma_length = cur_len;
13226 * If we had to allocate memory for this I/O, instead of using
13227 * the non-cached mirror memory, we'll need to flush the cache
13228 * before trying to DMA to the other controller.
13230 * We could end up doing this multiple times for the same
13231 * segment if we have a larger local segment than remote
13232 * segment. That shouldn't be an issue.
13234 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13236 * XXX KDM use bus_dmamap_sync() here.
13240 rq->size = dma_length;
13242 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13243 tmp_ptr += local_used;
13245 /* Use physical addresses when talking to ISC hardware */
13246 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13247 /* XXX KDM use busdma */
13249 rq->local = vtophys(tmp_ptr);
13252 rq->local = tmp_ptr;
13254 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13255 tmp_ptr += remote_used;
13256 rq->remote = tmp_ptr;
13258 rq->callback = NULL;
13260 local_used += cur_len;
13261 if (local_used >= local_sglist[i].len) {
13266 remote_used += cur_len;
13267 if (remote_used >= remote_sglist[j].len) {
13271 total_used += cur_len;
13273 if (total_used >= io->scsiio.kern_data_len)
13274 rq->callback = callback;
13276 if ((rq->size & 0x7) != 0) {
13277 printf("%s: warning: size %d is not on 8b boundary\n",
13278 __func__, rq->size);
13280 if (((uintptr_t)rq->local & 0x7) != 0) {
13281 printf("%s: warning: local %p not on 8b boundary\n",
13282 __func__, rq->local);
13284 if (((uintptr_t)rq->remote & 0x7) != 0) {
13285 printf("%s: warning: remote %p not on 8b boundary\n",
13286 __func__, rq->local);
13289 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13290 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13291 rq->local, rq->remote, rq->size);
13294 isc_ret = ctl_dt_single(rq);
13295 if (isc_ret == CTL_HA_STATUS_WAIT)
13298 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13299 rq->ret = CTL_HA_STATUS_SUCCESS;
13313 ctl_datamove_remote_read(union ctl_io *io)
13319 * This will send an error to the other controller in the case of a
13322 retval = ctl_datamove_remote_sgl_setup(io);
13326 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13327 ctl_datamove_remote_read_cb);
13329 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13331 * Make sure we free memory if there was an error.. The
13332 * ctl_datamove_remote_xfer() function will send the
13333 * datamove done message, or call the callback with an
13334 * error if there is a problem.
13336 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13337 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13344 * Process a datamove request from the other controller. This is used for
13345 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13346 * first. Once that is complete, the data gets DMAed into the remote
13347 * controller's memory. For reads, we DMA from the remote controller's
13348 * memory into our memory first, and then move it out to the FETD.
13351 ctl_datamove_remote(union ctl_io *io)
13353 struct ctl_softc *softc;
13355 softc = control_softc;
13357 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13360 * Note that we look for an aborted I/O here, but don't do some of
13361 * the other checks that ctl_datamove() normally does.
13362 * We don't need to run the datamove delay code, since that should
13363 * have been done if need be on the other controller.
13365 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13366 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13367 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13368 io->io_hdr.nexus.targ_port,
13369 io->io_hdr.nexus.targ_target.id,
13370 io->io_hdr.nexus.targ_lun);
13371 io->io_hdr.port_status = 31338;
13372 ctl_send_datamove_done(io, /*have_lock*/ 0);
13376 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13377 ctl_datamove_remote_write(io);
13378 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13379 ctl_datamove_remote_read(io);
13381 union ctl_ha_msg msg;
13382 struct scsi_sense_data *sense;
13386 memset(&msg, 0, sizeof(msg));
13388 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13389 msg.hdr.status = CTL_SCSI_ERROR;
13390 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13392 retry_count = 4243;
13394 sense = &msg.scsi.sense_data;
13395 sks[0] = SSD_SCS_VALID;
13396 sks[1] = (retry_count >> 8) & 0xff;
13397 sks[2] = retry_count & 0xff;
13399 /* "Internal target failure" */
13400 scsi_set_sense_data(sense,
13401 /*sense_format*/ SSD_TYPE_NONE,
13402 /*current_error*/ 1,
13403 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13406 /*type*/ SSD_ELEM_SKS,
13407 /*size*/ sizeof(sks),
13411 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13412 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13413 ctl_failover_io(io, /*have_lock*/ 1);
13417 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13418 CTL_HA_STATUS_SUCCESS) {
13419 /* XXX KDM what to do if this fails? */
13427 ctl_process_done(union ctl_io *io)
13429 struct ctl_lun *lun;
13430 struct ctl_softc *ctl_softc;
13431 void (*fe_done)(union ctl_io *io);
13432 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13434 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13437 control_softc->ctl_ports[targ_port]->fe_done;
13440 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13445 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13446 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13448 sbuf_cat(&sb, path_str);
13449 switch (io->io_hdr.io_type) {
13451 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13452 sbuf_printf(&sb, "\n");
13453 sbuf_cat(&sb, path_str);
13454 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13455 io->scsiio.tag_num, io->scsiio.tag_type);
13458 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13459 "Tag Type: %d\n", io->taskio.task_action,
13460 io->taskio.tag_num, io->taskio.tag_type);
13463 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13464 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13467 sbuf_cat(&sb, path_str);
13468 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13469 (intmax_t)time_uptime - io->io_hdr.start_time);
13471 printf("%s", sbuf_data(&sb));
13473 #endif /* CTL_TIME_IO */
13475 switch (io->io_hdr.io_type) {
13479 if (bootverbose || verbose > 0)
13480 ctl_io_error_print(io, NULL);
13481 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13485 return (CTL_RETVAL_COMPLETE);
13488 printf("ctl_process_done: invalid io type %d\n",
13489 io->io_hdr.io_type);
13490 panic("ctl_process_done: invalid io type %d\n",
13491 io->io_hdr.io_type);
13492 break; /* NOTREACHED */
13495 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13497 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13498 io->io_hdr.nexus.targ_mapped_lun));
13502 ctl_softc = lun->ctl_softc;
13504 mtx_lock(&lun->lun_lock);
13507 * Check to see if we have any errors to inject here. We only
13508 * inject errors for commands that don't already have errors set.
13510 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13511 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13512 ctl_inject_error(lun, io);
13515 * XXX KDM how do we treat commands that aren't completed
13518 * XXX KDM should we also track I/O latency?
13520 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13521 io->io_hdr.io_type == CTL_IO_SCSI) {
13523 struct bintime cur_bt;
13527 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13529 type = CTL_STATS_READ;
13530 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13532 type = CTL_STATS_WRITE;
13534 type = CTL_STATS_NO_IO;
13536 lun->stats.ports[targ_port].bytes[type] +=
13537 io->scsiio.kern_total_len;
13538 lun->stats.ports[targ_port].operations[type]++;
13540 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13541 &io->io_hdr.dma_bt);
13542 lun->stats.ports[targ_port].num_dmas[type] +=
13543 io->io_hdr.num_dmas;
13544 getbintime(&cur_bt);
13545 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13546 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13551 * Remove this from the OOA queue.
13553 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13556 * Run through the blocked queue on this LUN and see if anything
13557 * has become unblocked, now that this transaction is done.
13559 ctl_check_blocked(lun);
13562 * If the LUN has been invalidated, free it if there is nothing
13563 * left on its OOA queue.
13565 if ((lun->flags & CTL_LUN_INVALID)
13566 && TAILQ_EMPTY(&lun->ooa_queue)) {
13567 mtx_unlock(&lun->lun_lock);
13568 mtx_lock(&ctl_softc->ctl_lock);
13570 mtx_unlock(&ctl_softc->ctl_lock);
13572 mtx_unlock(&lun->lun_lock);
13575 * If this command has been aborted, make sure we set the status
13576 * properly. The FETD is responsible for freeing the I/O and doing
13577 * whatever it needs to do to clean up its state.
13579 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13580 ctl_set_task_aborted(&io->scsiio);
13583 * We print out status for every task management command. For SCSI
13584 * commands, we filter out any unit attention errors; they happen
13585 * on every boot, and would clutter up the log. Note: task
13586 * management commands aren't printed here, they are printed above,
13587 * since they should never even make it down here.
13589 switch (io->io_hdr.io_type) {
13590 case CTL_IO_SCSI: {
13591 int error_code, sense_key, asc, ascq;
13595 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13596 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13598 * Since this is just for printing, no need to
13599 * show errors here.
13601 scsi_extract_sense_len(&io->scsiio.sense_data,
13602 io->scsiio.sense_len,
13607 /*show_errors*/ 0);
13610 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13611 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13612 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13613 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13615 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13616 ctl_softc->skipped_prints++;
13618 uint32_t skipped_prints;
13620 skipped_prints = ctl_softc->skipped_prints;
13622 ctl_softc->skipped_prints = 0;
13623 ctl_softc->last_print_jiffies = time_uptime;
13625 if (skipped_prints > 0) {
13627 csevent_log(CSC_CTL | CSC_SHELF_SW |
13629 csevent_LogType_Trace,
13630 csevent_Severity_Information,
13631 csevent_AlertLevel_Green,
13632 csevent_FRU_Firmware,
13633 csevent_FRU_Unknown,
13634 "High CTL error volume, %d prints "
13635 "skipped", skipped_prints);
13638 if (bootverbose || verbose > 0)
13639 ctl_io_error_print(io, NULL);
13645 if (bootverbose || verbose > 0)
13646 ctl_io_error_print(io, NULL);
13653 * Tell the FETD or the other shelf controller we're done with this
13654 * command. Note that only SCSI commands get to this point. Task
13655 * management commands are completed above.
13657 * We only send status to the other controller if we're in XFER
13658 * mode. In SER_ONLY mode, the I/O is done on the controller that
13659 * received the I/O (from CTL's perspective), and so the status is
13662 * XXX KDM if we hold the lock here, we could cause a deadlock
13663 * if the frontend comes back in in this context to queue
13666 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13667 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13668 union ctl_ha_msg msg;
13670 memset(&msg, 0, sizeof(msg));
13671 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13672 msg.hdr.original_sc = io->io_hdr.original_sc;
13673 msg.hdr.nexus = io->io_hdr.nexus;
13674 msg.hdr.status = io->io_hdr.status;
13675 msg.scsi.scsi_status = io->scsiio.scsi_status;
13676 msg.scsi.tag_num = io->scsiio.tag_num;
13677 msg.scsi.tag_type = io->scsiio.tag_type;
13678 msg.scsi.sense_len = io->scsiio.sense_len;
13679 msg.scsi.sense_residual = io->scsiio.sense_residual;
13680 msg.scsi.residual = io->scsiio.residual;
13681 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13682 sizeof(io->scsiio.sense_data));
13684 * We copy this whether or not this is an I/O-related
13685 * command. Otherwise, we'd have to go and check to see
13686 * whether it's a read/write command, and it really isn't
13689 memcpy(&msg.scsi.lbalen,
13690 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13691 sizeof(msg.scsi.lbalen));
13693 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13694 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13695 /* XXX do something here */
13704 return (CTL_RETVAL_COMPLETE);
13709 * Front end should call this if it doesn't do autosense. When the request
13710 * sense comes back in from the initiator, we'll dequeue this and send it.
13713 ctl_queue_sense(union ctl_io *io)
13715 struct ctl_lun *lun;
13716 struct ctl_softc *ctl_softc;
13717 uint32_t initidx, targ_lun;
13719 ctl_softc = control_softc;
13721 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13724 * LUN lookup will likely move to the ctl_work_thread() once we
13725 * have our new queueing infrastructure (that doesn't put things on
13726 * a per-LUN queue initially). That is so that we can handle
13727 * things like an INQUIRY to a LUN that we don't have enabled. We
13728 * can't deal with that right now.
13730 mtx_lock(&ctl_softc->ctl_lock);
13733 * If we don't have a LUN for this, just toss the sense
13736 targ_lun = io->io_hdr.nexus.targ_lun;
13737 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13738 if ((targ_lun < CTL_MAX_LUNS)
13739 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13740 lun = ctl_softc->ctl_luns[targ_lun];
13744 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13746 mtx_lock(&lun->lun_lock);
13748 * Already have CA set for this LUN...toss the sense information.
13750 if (ctl_is_set(lun->have_ca, initidx)) {
13751 mtx_unlock(&lun->lun_lock);
13755 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13756 ctl_min(sizeof(lun->pending_sense[initidx]),
13757 sizeof(io->scsiio.sense_data)));
13758 ctl_set_mask(lun->have_ca, initidx);
13759 mtx_unlock(&lun->lun_lock);
13762 mtx_unlock(&ctl_softc->ctl_lock);
13766 return (CTL_RETVAL_COMPLETE);
13771 * Primary command inlet from frontend ports. All SCSI and task I/O
13772 * requests must go through this function.
13775 ctl_queue(union ctl_io *io)
13777 struct ctl_softc *ctl_softc;
13779 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13781 ctl_softc = control_softc;
13784 io->io_hdr.start_time = time_uptime;
13785 getbintime(&io->io_hdr.start_bt);
13786 #endif /* CTL_TIME_IO */
13788 /* Map FE-specific LUN ID into global one. */
13789 io->io_hdr.nexus.targ_mapped_lun =
13790 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13792 switch (io->io_hdr.io_type) {
13795 ctl_enqueue_incoming(io);
13798 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13802 return (CTL_RETVAL_COMPLETE);
13805 #ifdef CTL_IO_DELAY
13807 ctl_done_timer_wakeup(void *arg)
13811 io = (union ctl_io *)arg;
13814 #endif /* CTL_IO_DELAY */
13817 ctl_done(union ctl_io *io)
13819 struct ctl_softc *ctl_softc;
13821 ctl_softc = control_softc;
13824 * Enable this to catch duplicate completion issues.
13827 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13828 printf("%s: type %d msg %d cdb %x iptl: "
13829 "%d:%d:%d:%d tag 0x%04x "
13830 "flag %#x status %x\n",
13832 io->io_hdr.io_type,
13833 io->io_hdr.msg_type,
13835 io->io_hdr.nexus.initid.id,
13836 io->io_hdr.nexus.targ_port,
13837 io->io_hdr.nexus.targ_target.id,
13838 io->io_hdr.nexus.targ_lun,
13839 (io->io_hdr.io_type ==
13841 io->taskio.tag_num :
13842 io->scsiio.tag_num,
13844 io->io_hdr.status);
13846 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13850 * This is an internal copy of an I/O, and should not go through
13851 * the normal done processing logic.
13853 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13857 * We need to send a msg to the serializing shelf to finish the IO
13858 * as well. We don't send a finish message to the other shelf if
13859 * this is a task management command. Task management commands
13860 * aren't serialized in the OOA queue, but rather just executed on
13861 * both shelf controllers for commands that originated on that
13864 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13865 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13866 union ctl_ha_msg msg_io;
13868 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13869 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13870 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13871 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13873 /* continue on to finish IO */
13875 #ifdef CTL_IO_DELAY
13876 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13877 struct ctl_lun *lun;
13879 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13881 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13883 struct ctl_lun *lun;
13885 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13888 && (lun->delay_info.done_delay > 0)) {
13889 struct callout *callout;
13891 callout = (struct callout *)&io->io_hdr.timer_bytes;
13892 callout_init(callout, /*mpsafe*/ 1);
13893 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13894 callout_reset(callout,
13895 lun->delay_info.done_delay * hz,
13896 ctl_done_timer_wakeup, io);
13897 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13898 lun->delay_info.done_delay = 0;
13902 #endif /* CTL_IO_DELAY */
13904 ctl_enqueue_done(io);
13908 ctl_isc(struct ctl_scsiio *ctsio)
13910 struct ctl_lun *lun;
13913 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13915 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13917 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13919 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13926 ctl_work_thread(void *arg)
13928 struct ctl_thread *thr = (struct ctl_thread *)arg;
13929 struct ctl_softc *softc = thr->ctl_softc;
13933 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13939 * We handle the queues in this order:
13941 * - done queue (to free up resources, unblock other commands)
13945 * If those queues are empty, we break out of the loop and
13948 mtx_lock(&thr->queue_lock);
13949 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13951 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13952 mtx_unlock(&thr->queue_lock);
13953 ctl_handle_isc(io);
13956 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13958 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13959 /* clear any blocked commands, call fe_done */
13960 mtx_unlock(&thr->queue_lock);
13961 retval = ctl_process_done(io);
13964 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13966 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13967 mtx_unlock(&thr->queue_lock);
13968 if (io->io_hdr.io_type == CTL_IO_TASK)
13971 ctl_scsiio_precheck(softc, &io->scsiio);
13974 if (!ctl_pause_rtr) {
13975 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13977 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13978 mtx_unlock(&thr->queue_lock);
13979 retval = ctl_scsiio(&io->scsiio);
13980 if (retval != CTL_RETVAL_COMPLETE)
13981 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13986 /* Sleep until we have something to do. */
13987 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13992 ctl_lun_thread(void *arg)
13994 struct ctl_softc *softc = (struct ctl_softc *)arg;
13995 struct ctl_be_lun *be_lun;
13998 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14002 mtx_lock(&softc->ctl_lock);
14003 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14004 if (be_lun != NULL) {
14005 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14006 mtx_unlock(&softc->ctl_lock);
14007 ctl_create_lun(be_lun);
14011 /* Sleep until we have something to do. */
14012 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14013 PDROP | PRIBIO, "-", 0);
14018 ctl_enqueue_incoming(union ctl_io *io)
14020 struct ctl_softc *softc = control_softc;
14021 struct ctl_thread *thr;
14024 idx = (io->io_hdr.nexus.targ_port * 127 +
14025 io->io_hdr.nexus.initid.id) % worker_threads;
14026 thr = &softc->threads[idx];
14027 mtx_lock(&thr->queue_lock);
14028 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14029 mtx_unlock(&thr->queue_lock);
14034 ctl_enqueue_rtr(union ctl_io *io)
14036 struct ctl_softc *softc = control_softc;
14037 struct ctl_thread *thr;
14039 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14040 mtx_lock(&thr->queue_lock);
14041 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14042 mtx_unlock(&thr->queue_lock);
14047 ctl_enqueue_done(union ctl_io *io)
14049 struct ctl_softc *softc = control_softc;
14050 struct ctl_thread *thr;
14052 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14053 mtx_lock(&thr->queue_lock);
14054 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14055 mtx_unlock(&thr->queue_lock);
14060 ctl_enqueue_isc(union ctl_io *io)
14062 struct ctl_softc *softc = control_softc;
14063 struct ctl_thread *thr;
14065 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14066 mtx_lock(&thr->queue_lock);
14067 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14068 mtx_unlock(&thr->queue_lock);
14072 /* Initialization and failover */
14075 ctl_init_isc_msg(void)
14077 printf("CTL: Still calling this thing\n");
14082 * Initializes component into configuration defined by bootMode
14084 * returns hasc_Status:
14086 * ERROR - fatal error
14088 static ctl_ha_comp_status
14089 ctl_isc_init(struct ctl_ha_component *c)
14091 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14098 * Starts component in state requested. If component starts successfully,
14099 * it must set its own state to the requestrd state
14100 * When requested state is HASC_STATE_HA, the component may refine it
14101 * by adding _SLAVE or _MASTER flags.
14102 * Currently allowed state transitions are:
14103 * UNKNOWN->HA - initial startup
14104 * UNKNOWN->SINGLE - initial startup when no parter detected
14105 * HA->SINGLE - failover
14106 * returns ctl_ha_comp_status:
14107 * OK - component successfully started in requested state
14108 * FAILED - could not start the requested state, failover may
14110 * ERROR - fatal error detected, no future startup possible
14112 static ctl_ha_comp_status
14113 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14115 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14117 printf("%s: go\n", __func__);
14119 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14120 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14122 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14123 != CTL_HA_STATUS_SUCCESS) {
14124 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14125 ret = CTL_HA_COMP_STATUS_ERROR;
14127 } else if (CTL_HA_STATE_IS_HA(c->state)
14128 && CTL_HA_STATE_IS_SINGLE(state)){
14129 // HA->SINGLE transition
14133 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14135 ret = CTL_HA_COMP_STATUS_ERROR;
14137 if (CTL_HA_STATE_IS_SINGLE(state))
14146 * Quiesce component
14147 * The component must clear any error conditions (set status to OK) and
14148 * prepare itself to another Start call
14149 * returns ctl_ha_comp_status:
14153 static ctl_ha_comp_status
14154 ctl_isc_quiesce(struct ctl_ha_component *c)
14156 int ret = CTL_HA_COMP_STATUS_OK;
14163 struct ctl_ha_component ctl_ha_component_ctlisc =
14166 .state = CTL_HA_STATE_UNKNOWN,
14167 .init = ctl_isc_init,
14168 .start = ctl_isc_start,
14169 .quiesce = ctl_isc_quiesce