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>
63 #include <sys/endian.h>
64 #include <sys/sysctl.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_da.h>
69 #include <cam/ctl/ctl_io.h>
70 #include <cam/ctl/ctl.h>
71 #include <cam/ctl/ctl_frontend.h>
72 #include <cam/ctl/ctl_frontend_internal.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_backend.h>
75 #include <cam/ctl/ctl_ioctl.h>
76 #include <cam/ctl/ctl_ha.h>
77 #include <cam/ctl/ctl_private.h>
78 #include <cam/ctl/ctl_debug.h>
79 #include <cam/ctl/ctl_scsi_all.h>
80 #include <cam/ctl/ctl_error.h>
82 struct ctl_softc *control_softc = NULL;
85 * The default is to run with CTL_DONE_THREAD turned on. Completed
86 * transactions are queued for processing by the CTL work thread. When
87 * CTL_DONE_THREAD is not defined, completed transactions are processed in
88 * the caller's context.
90 #define CTL_DONE_THREAD
93 * Use the serial number and device ID provided by the backend, rather than
96 #define CTL_USE_BACKEND_SN
99 * Size and alignment macros needed for Copan-specific HA hardware. These
100 * can go away when the HA code is re-written, and uses busdma for any
103 #define CTL_ALIGN_8B(target, source, type) \
104 if (((uint32_t)source & 0x7) != 0) \
105 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
107 target = (type)source;
109 #define CTL_SIZE_8B(target, size) \
110 if ((size & 0x7) != 0) \
111 target = size + (0x8 - (size & 0x7)); \
115 #define CTL_ALIGN_8B_MARGIN 16
118 * Template mode pages.
122 * Note that these are default values only. The actual values will be
123 * filled in when the user does a mode sense.
125 static struct copan_power_subpage power_page_default = {
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},
130 /*page_version*/ PWR_VERSION,
132 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
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_power_subpage power_page_changeable = {
139 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
140 /*subpage*/ PWR_SUBPAGE_CODE,
141 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
142 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
145 /* max_active_luns*/ 0,
146 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151 static struct copan_aps_subpage aps_page_default = {
152 APS_PAGE_CODE | SMPH_SPF, //page_code
153 APS_SUBPAGE_CODE, //subpage
154 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
155 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
156 APS_VERSION, //page_version
158 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0} //reserved
163 static struct copan_aps_subpage aps_page_changeable = {
164 APS_PAGE_CODE | SMPH_SPF, //page_code
165 APS_SUBPAGE_CODE, //subpage
166 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
167 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
170 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0} //reserved
175 static struct copan_debugconf_subpage debugconf_page_default = {
176 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
177 DBGCNF_SUBPAGE_CODE, /* subpage */
178 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
179 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
180 DBGCNF_VERSION, /* page_version */
181 {CTL_TIME_IO_DEFAULT_SECS>>8,
182 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
185 static struct copan_debugconf_subpage debugconf_page_changeable = {
186 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
187 DBGCNF_SUBPAGE_CODE, /* subpage */
188 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
189 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
190 0, /* page_version */
191 {0xff,0xff}, /* ctl_time_io_secs */
194 static struct scsi_format_page format_page_default = {
195 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
196 /*page_length*/sizeof(struct scsi_format_page) - 2,
197 /*tracks_per_zone*/ {0, 0},
198 /*alt_sectors_per_zone*/ {0, 0},
199 /*alt_tracks_per_zone*/ {0, 0},
200 /*alt_tracks_per_lun*/ {0, 0},
201 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
202 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
203 /*bytes_per_sector*/ {0, 0},
204 /*interleave*/ {0, 0},
205 /*track_skew*/ {0, 0},
206 /*cylinder_skew*/ {0, 0},
208 /*reserved*/ {0, 0, 0}
211 static struct scsi_format_page format_page_changeable = {
212 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
213 /*page_length*/sizeof(struct scsi_format_page) - 2,
214 /*tracks_per_zone*/ {0, 0},
215 /*alt_sectors_per_zone*/ {0, 0},
216 /*alt_tracks_per_zone*/ {0, 0},
217 /*alt_tracks_per_lun*/ {0, 0},
218 /*sectors_per_track*/ {0, 0},
219 /*bytes_per_sector*/ {0, 0},
220 /*interleave*/ {0, 0},
221 /*track_skew*/ {0, 0},
222 /*cylinder_skew*/ {0, 0},
224 /*reserved*/ {0, 0, 0}
227 static struct scsi_rigid_disk_page rigid_disk_page_default = {
228 /*page_code*/SMS_RIGID_DISK_PAGE,
229 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
230 /*cylinders*/ {0, 0, 0},
231 /*heads*/ CTL_DEFAULT_HEADS,
232 /*start_write_precomp*/ {0, 0, 0},
233 /*start_reduced_current*/ {0, 0, 0},
234 /*step_rate*/ {0, 0},
235 /*landing_zone_cylinder*/ {0, 0, 0},
236 /*rpl*/ SRDP_RPL_DISABLED,
237 /*rotational_offset*/ 0,
239 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
240 CTL_DEFAULT_ROTATION_RATE & 0xff},
244 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
245 /*page_code*/SMS_RIGID_DISK_PAGE,
246 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
247 /*cylinders*/ {0, 0, 0},
249 /*start_write_precomp*/ {0, 0, 0},
250 /*start_reduced_current*/ {0, 0, 0},
251 /*step_rate*/ {0, 0},
252 /*landing_zone_cylinder*/ {0, 0, 0},
254 /*rotational_offset*/ 0,
256 /*rotation_rate*/ {0, 0},
260 static struct scsi_caching_page caching_page_default = {
261 /*page_code*/SMS_CACHING_PAGE,
262 /*page_length*/sizeof(struct scsi_caching_page) - 2,
263 /*flags1*/ SCP_DISC | SCP_WCE,
265 /*disable_pf_transfer_len*/ {0xff, 0xff},
266 /*min_prefetch*/ {0, 0},
267 /*max_prefetch*/ {0xff, 0xff},
268 /*max_pf_ceiling*/ {0xff, 0xff},
270 /*cache_segments*/ 0,
271 /*cache_seg_size*/ {0, 0},
273 /*non_cache_seg_size*/ {0, 0, 0}
276 static struct scsi_caching_page caching_page_changeable = {
277 /*page_code*/SMS_CACHING_PAGE,
278 /*page_length*/sizeof(struct scsi_caching_page) - 2,
281 /*disable_pf_transfer_len*/ {0, 0},
282 /*min_prefetch*/ {0, 0},
283 /*max_prefetch*/ {0, 0},
284 /*max_pf_ceiling*/ {0, 0},
286 /*cache_segments*/ 0,
287 /*cache_seg_size*/ {0, 0},
289 /*non_cache_seg_size*/ {0, 0, 0}
292 static struct scsi_control_page control_page_default = {
293 /*page_code*/SMS_CONTROL_MODE_PAGE,
294 /*page_length*/sizeof(struct scsi_control_page) - 2,
299 /*aen_holdoff_period*/{0, 0}
302 static struct scsi_control_page control_page_changeable = {
303 /*page_code*/SMS_CONTROL_MODE_PAGE,
304 /*page_length*/sizeof(struct scsi_control_page) - 2,
309 /*aen_holdoff_period*/{0, 0}
314 * XXX KDM move these into the softc.
316 static int rcv_sync_msg;
317 static int persis_offset;
318 static uint8_t ctl_pause_rtr;
319 static int ctl_is_single = 1;
320 static int index_to_aps_page;
323 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
324 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, disable, CTLFLAG_RDTUN, &ctl_disable, 0,
326 TUNABLE_INT("kern.cam.ctl.disable", &ctl_disable);
327 static int verbose = 0;
328 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
329 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
330 &verbose, 0, "Show SCSI errors returned to initiator");
333 * Serial number (0x80), device id (0x83), and supported pages (0x00)
335 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 3
337 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
339 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
340 static int ctl_init(void);
341 void ctl_shutdown(void);
342 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
343 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
344 static void ctl_ioctl_online(void *arg);
345 static void ctl_ioctl_offline(void *arg);
346 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
347 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
348 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
349 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
350 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
351 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
352 static int ctl_ioctl_submit_wait(union ctl_io *io);
353 static void ctl_ioctl_datamove(union ctl_io *io);
354 static void ctl_ioctl_done(union ctl_io *io);
355 static void ctl_ioctl_hard_startstop_callback(void *arg,
356 struct cfi_metatask *metatask);
357 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
358 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
359 struct ctl_ooa *ooa_hdr,
360 struct ctl_ooa_entry *kern_entries);
361 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
363 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
364 uint32_t ctl_port_idx(int port_num);
366 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
367 uint32_t targ_target, uint32_t targ_lun,
369 static void ctl_kfree_io(union ctl_io *io);
371 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
372 struct ctl_be_lun *be_lun, struct ctl_id target_id);
373 static int ctl_free_lun(struct ctl_lun *lun);
374 static void ctl_create_lun(struct ctl_be_lun *be_lun);
376 static void ctl_failover_change_pages(struct ctl_softc *softc,
377 struct ctl_scsiio *ctsio, int master);
380 static int ctl_do_mode_select(union ctl_io *io);
381 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
382 uint64_t res_key, uint64_t sa_res_key,
383 uint8_t type, uint32_t residx,
384 struct ctl_scsiio *ctsio,
385 struct scsi_per_res_out *cdb,
386 struct scsi_per_res_out_parms* param);
387 static void ctl_pro_preempt_other(struct ctl_lun *lun,
388 union ctl_ha_msg *msg);
389 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
390 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
391 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
392 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
393 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
394 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
395 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
396 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
397 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
398 union ctl_io *ooa_io);
399 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
400 union ctl_io *starting_io);
401 static int ctl_check_blocked(struct ctl_lun *lun);
402 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
404 struct ctl_cmd_entry *entry,
405 struct ctl_scsiio *ctsio);
406 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
407 static void ctl_failover(void);
408 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
409 struct ctl_scsiio *ctsio);
410 static int ctl_scsiio(struct ctl_scsiio *ctsio);
412 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
413 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
414 ctl_ua_type ua_type);
415 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
416 ctl_ua_type ua_type);
417 static int ctl_abort_task(union ctl_io *io);
418 static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
420 static void ctl_datamove_timer_wakeup(void *arg);
421 static void ctl_done_timer_wakeup(void *arg);
422 #endif /* CTL_IO_DELAY */
424 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
425 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
426 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
427 static void ctl_datamove_remote_write(union ctl_io *io);
428 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
429 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
430 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
431 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
432 ctl_ha_dt_cb callback);
433 static void ctl_datamove_remote_read(union ctl_io *io);
434 static void ctl_datamove_remote(union ctl_io *io);
435 static int ctl_process_done(union ctl_io *io, int have_lock);
436 static void ctl_work_thread(void *arg);
439 * Load the serialization table. This isn't very pretty, but is probably
440 * the easiest way to do it.
442 #include "ctl_ser_table.c"
445 * We only need to define open, close and ioctl routines for this driver.
447 static struct cdevsw ctl_cdevsw = {
448 .d_version = D_VERSION,
451 .d_close = ctl_close,
452 .d_ioctl = ctl_ioctl,
457 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
459 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
461 static moduledata_t ctl_moduledata = {
463 ctl_module_event_handler,
467 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
468 MODULE_VERSION(ctl, 1);
471 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
472 union ctl_ha_msg *msg_info)
474 struct ctl_scsiio *ctsio;
476 if (msg_info->hdr.original_sc == NULL) {
477 printf("%s: original_sc == NULL!\n", __func__);
478 /* XXX KDM now what? */
482 ctsio = &msg_info->hdr.original_sc->scsiio;
483 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
484 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
485 ctsio->io_hdr.status = msg_info->hdr.status;
486 ctsio->scsi_status = msg_info->scsi.scsi_status;
487 ctsio->sense_len = msg_info->scsi.sense_len;
488 ctsio->sense_residual = msg_info->scsi.sense_residual;
489 ctsio->residual = msg_info->scsi.residual;
490 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
491 sizeof(ctsio->sense_data));
492 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
493 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
494 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
499 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
500 union ctl_ha_msg *msg_info)
502 struct ctl_scsiio *ctsio;
504 if (msg_info->hdr.serializing_sc == NULL) {
505 printf("%s: serializing_sc == NULL!\n", __func__);
506 /* XXX KDM now what? */
510 ctsio = &msg_info->hdr.serializing_sc->scsiio;
513 * Attempt to catch the situation where an I/O has
514 * been freed, and we're using it again.
516 if (ctsio->io_hdr.io_type == 0xff) {
517 union ctl_io *tmp_io;
518 tmp_io = (union ctl_io *)ctsio;
519 printf("%s: %p use after free!\n", __func__,
521 printf("%s: type %d msg %d cdb %x iptl: "
522 "%d:%d:%d:%d tag 0x%04x "
523 "flag %#x status %x\n",
525 tmp_io->io_hdr.io_type,
526 tmp_io->io_hdr.msg_type,
527 tmp_io->scsiio.cdb[0],
528 tmp_io->io_hdr.nexus.initid.id,
529 tmp_io->io_hdr.nexus.targ_port,
530 tmp_io->io_hdr.nexus.targ_target.id,
531 tmp_io->io_hdr.nexus.targ_lun,
532 (tmp_io->io_hdr.io_type ==
534 tmp_io->taskio.tag_num :
535 tmp_io->scsiio.tag_num,
536 tmp_io->io_hdr.flags,
537 tmp_io->io_hdr.status);
540 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
541 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
546 * ISC (Inter Shelf Communication) event handler. Events from the HA
547 * subsystem come in here.
550 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
552 struct ctl_softc *ctl_softc;
554 struct ctl_prio *presio;
555 ctl_ha_status isc_status;
557 ctl_softc = control_softc;
562 printf("CTL: Isc Msg event %d\n", event);
564 if (event == CTL_HA_EVT_MSG_RECV) {
565 union ctl_ha_msg msg_info;
567 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
568 sizeof(msg_info), /*wait*/ 0);
570 printf("CTL: msg_type %d\n", msg_info.msg_type);
572 if (isc_status != 0) {
573 printf("Error receiving message, status = %d\n",
577 mtx_lock(&ctl_softc->ctl_lock);
579 switch (msg_info.hdr.msg_type) {
580 case CTL_MSG_SERIALIZE:
582 printf("Serialize\n");
584 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
586 printf("ctl_isc_event_handler: can't allocate "
589 /* Need to set busy and send msg back */
590 mtx_unlock(&ctl_softc->ctl_lock);
591 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
592 msg_info.hdr.status = CTL_SCSI_ERROR;
593 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
594 msg_info.scsi.sense_len = 0;
595 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
596 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
601 // populate ctsio from msg_info
602 io->io_hdr.io_type = CTL_IO_SCSI;
603 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
604 io->io_hdr.original_sc = msg_info.hdr.original_sc;
606 printf("pOrig %x\n", (int)msg_info.original_sc);
608 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
611 * If we're in serialization-only mode, we don't
612 * want to go through full done processing. Thus
615 * XXX KDM add another flag that is more specific.
617 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
618 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
619 io->io_hdr.nexus = msg_info.hdr.nexus;
621 printf("targ %d, port %d, iid %d, lun %d\n",
622 io->io_hdr.nexus.targ_target.id,
623 io->io_hdr.nexus.targ_port,
624 io->io_hdr.nexus.initid.id,
625 io->io_hdr.nexus.targ_lun);
627 io->scsiio.tag_num = msg_info.scsi.tag_num;
628 io->scsiio.tag_type = msg_info.scsi.tag_type;
629 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
631 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
632 struct ctl_cmd_entry *entry;
635 opcode = io->scsiio.cdb[0];
636 entry = &ctl_cmd_table[opcode];
637 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
639 entry->flags & CTL_FLAG_DATA_MASK;
641 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
646 /* Performed on the Originating SC, XFER mode only */
647 case CTL_MSG_DATAMOVE: {
648 struct ctl_sg_entry *sgl;
651 io = msg_info.hdr.original_sc;
653 printf("%s: original_sc == NULL!\n", __func__);
654 /* XXX KDM do something here */
657 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
658 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
660 * Keep track of this, we need to send it back over
661 * when the datamove is complete.
663 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
665 if (msg_info.dt.sg_sequence == 0) {
667 * XXX KDM we use the preallocated S/G list
668 * here, but we'll need to change this to
669 * dynamic allocation if we need larger S/G
672 if (msg_info.dt.kern_sg_entries >
673 sizeof(io->io_hdr.remote_sglist) /
674 sizeof(io->io_hdr.remote_sglist[0])) {
675 printf("%s: number of S/G entries "
676 "needed %u > allocated num %zd\n",
678 msg_info.dt.kern_sg_entries,
679 sizeof(io->io_hdr.remote_sglist)/
680 sizeof(io->io_hdr.remote_sglist[0]));
683 * XXX KDM send a message back to
684 * the other side to shut down the
685 * DMA. The error will come back
686 * through via the normal channel.
690 sgl = io->io_hdr.remote_sglist;
692 sizeof(io->io_hdr.remote_sglist));
694 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
696 io->scsiio.kern_sg_entries =
697 msg_info.dt.kern_sg_entries;
698 io->scsiio.rem_sg_entries =
699 msg_info.dt.kern_sg_entries;
700 io->scsiio.kern_data_len =
701 msg_info.dt.kern_data_len;
702 io->scsiio.kern_total_len =
703 msg_info.dt.kern_total_len;
704 io->scsiio.kern_data_resid =
705 msg_info.dt.kern_data_resid;
706 io->scsiio.kern_rel_offset =
707 msg_info.dt.kern_rel_offset;
709 * Clear out per-DMA flags.
711 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
713 * Add per-DMA flags that are set for this
714 * particular DMA request.
716 io->io_hdr.flags |= msg_info.dt.flags &
719 sgl = (struct ctl_sg_entry *)
720 io->scsiio.kern_data_ptr;
722 for (i = msg_info.dt.sent_sg_entries, j = 0;
723 i < (msg_info.dt.sent_sg_entries +
724 msg_info.dt.cur_sg_entries); i++, j++) {
725 sgl[i].addr = msg_info.dt.sg_list[j].addr;
726 sgl[i].len = msg_info.dt.sg_list[j].len;
729 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
731 msg_info.dt.sg_list[j].addr,
732 msg_info.dt.sg_list[j].len,
733 sgl[i].addr, sgl[i].len, j, i);
737 memcpy(&sgl[msg_info.dt.sent_sg_entries],
739 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
743 * If this is the last piece of the I/O, we've got
744 * the full S/G list. Queue processing in the thread.
745 * Otherwise wait for the next piece.
747 if (msg_info.dt.sg_last != 0) {
748 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
754 /* Performed on the Serializing (primary) SC, XFER mode only */
755 case CTL_MSG_DATAMOVE_DONE: {
756 if (msg_info.hdr.serializing_sc == NULL) {
757 printf("%s: serializing_sc == NULL!\n",
759 /* XXX KDM now what? */
763 * We grab the sense information here in case
764 * there was a failure, so we can return status
765 * back to the initiator.
767 io = msg_info.hdr.serializing_sc;
768 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
769 io->io_hdr.status = msg_info.hdr.status;
770 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
771 io->scsiio.sense_len = msg_info.scsi.sense_len;
772 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
773 io->io_hdr.port_status = msg_info.scsi.fetd_status;
774 io->scsiio.residual = msg_info.scsi.residual;
775 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
776 sizeof(io->scsiio.sense_data));
778 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
784 /* Preformed on Originating SC, SER_ONLY mode */
786 io = msg_info.hdr.original_sc;
788 printf("%s: Major Bummer\n", __func__);
789 mtx_unlock(&ctl_softc->ctl_lock);
793 printf("pOrig %x\n",(int) ctsio);
796 io->io_hdr.msg_type = CTL_MSG_R2R;
797 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
798 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
804 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
806 * Performed on the Originating (i.e. secondary) SC in XFER
809 case CTL_MSG_FINISH_IO:
810 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
811 ctl_isc_handler_finish_xfer(ctl_softc,
814 ctl_isc_handler_finish_ser_only(ctl_softc,
818 /* Preformed on Originating SC */
819 case CTL_MSG_BAD_JUJU:
820 io = msg_info.hdr.original_sc;
822 printf("%s: Bad JUJU!, original_sc is NULL!\n",
826 ctl_copy_sense_data(&msg_info, io);
828 * IO should have already been cleaned up on other
829 * SC so clear this flag so we won't send a message
830 * back to finish the IO there.
832 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
833 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
835 /* io = msg_info.hdr.serializing_sc; */
836 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
837 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
842 /* Handle resets sent from the other side */
843 case CTL_MSG_MANAGE_TASKS: {
844 struct ctl_taskio *taskio;
845 taskio = (struct ctl_taskio *)ctl_alloc_io(
846 (void *)ctl_softc->othersc_pool);
847 if (taskio == NULL) {
848 printf("ctl_isc_event_handler: can't allocate "
851 /* should I just call the proper reset func
853 mtx_unlock(&ctl_softc->ctl_lock);
856 ctl_zero_io((union ctl_io *)taskio);
857 taskio->io_hdr.io_type = CTL_IO_TASK;
858 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
859 taskio->io_hdr.nexus = msg_info.hdr.nexus;
860 taskio->task_action = msg_info.task.task_action;
861 taskio->tag_num = msg_info.task.tag_num;
862 taskio->tag_type = msg_info.task.tag_type;
864 taskio->io_hdr.start_time = time_uptime;
865 getbintime(&taskio->io_hdr.start_bt);
867 cs_prof_gettime(&taskio->io_hdr.start_ticks);
869 #endif /* CTL_TIME_IO */
870 STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
871 &taskio->io_hdr, links);
872 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
876 /* Persistent Reserve action which needs attention */
877 case CTL_MSG_PERS_ACTION:
878 presio = (struct ctl_prio *)ctl_alloc_io(
879 (void *)ctl_softc->othersc_pool);
880 if (presio == NULL) {
881 printf("ctl_isc_event_handler: can't allocate "
884 /* Need to set busy and send msg back */
885 mtx_unlock(&ctl_softc->ctl_lock);
888 ctl_zero_io((union ctl_io *)presio);
889 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
890 presio->pr_msg = msg_info.pr;
891 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
892 &presio->io_hdr, links);
895 case CTL_MSG_SYNC_FE:
898 case CTL_MSG_APS_LOCK: {
899 // It's quicker to execute this then to
902 struct ctl_page_index *page_index;
903 struct copan_aps_subpage *current_sp;
905 lun = ctl_softc->ctl_luns[msg_info.hdr.nexus.targ_lun];
906 page_index = &lun->mode_pages.index[index_to_aps_page];
907 current_sp = (struct copan_aps_subpage *)
908 (page_index->page_data +
909 (page_index->page_len * CTL_PAGE_CURRENT));
911 current_sp->lock_active = msg_info.aps.lock_flag;
915 printf("How did I get here?\n");
917 mtx_unlock(&ctl_softc->ctl_lock);
918 } else if (event == CTL_HA_EVT_MSG_SENT) {
919 if (param != CTL_HA_STATUS_SUCCESS) {
920 printf("Bad status from ctl_ha_msg_send status %d\n",
924 } else if (event == CTL_HA_EVT_DISCONNECT) {
925 printf("CTL: Got a disconnect from Isc\n");
928 printf("ctl_isc_event_handler: Unknown event %d\n", event);
937 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
939 struct scsi_sense_data *sense;
941 sense = &dest->scsiio.sense_data;
942 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
943 dest->scsiio.scsi_status = src->scsi.scsi_status;
944 dest->scsiio.sense_len = src->scsi.sense_len;
945 dest->io_hdr.status = src->hdr.status;
951 struct ctl_softc *softc;
952 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
953 struct ctl_frontend *fe;
966 /* If we're disabled, don't initialize. */
967 if (ctl_disable != 0)
970 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
972 softc = control_softc;
974 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
977 softc->dev->si_drv1 = softc;
980 * By default, return a "bad LUN" peripheral qualifier for unknown
981 * LUNs. The user can override this default using the tunable or
982 * sysctl. See the comment in ctl_inquiry_std() for more details.
984 softc->inquiry_pq_no_lun = 1;
985 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
986 &softc->inquiry_pq_no_lun);
987 sysctl_ctx_init(&softc->sysctl_ctx);
988 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
989 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
990 CTLFLAG_RD, 0, "CAM Target Layer");
992 if (softc->sysctl_tree == NULL) {
993 printf("%s: unable to allocate sysctl tree\n", __func__);
994 destroy_dev(softc->dev);
995 free(control_softc, M_DEVBUF);
996 control_softc = NULL;
1000 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1001 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1002 "inquiry_pq_no_lun", CTLFLAG_RW,
1003 &softc->inquiry_pq_no_lun, 0,
1004 "Report no lun possible for invalid LUNs");
1006 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1007 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
1008 softc->open_count = 0;
1011 * Default to actually sending a SYNCHRONIZE CACHE command down to
1014 softc->flags = CTL_FLAG_REAL_SYNC;
1017 * In Copan's HA scheme, the "master" and "slave" roles are
1018 * figured out through the slot the controller is in. Although it
1019 * is an active/active system, someone has to be in charge.
1022 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1026 softc->flags |= CTL_FLAG_MASTER_SHELF;
1029 persis_offset = CTL_MAX_INITIATORS;
1032 * XXX KDM need to figure out where we want to get our target ID
1033 * and WWID. Is it different on each port?
1035 softc->target.id = 0;
1036 softc->target.wwid[0] = 0x12345678;
1037 softc->target.wwid[1] = 0x87654321;
1038 STAILQ_INIT(&softc->lun_list);
1039 STAILQ_INIT(&softc->pending_lun_queue);
1040 STAILQ_INIT(&softc->task_queue);
1041 STAILQ_INIT(&softc->incoming_queue);
1042 STAILQ_INIT(&softc->rtr_queue);
1043 STAILQ_INIT(&softc->done_queue);
1044 STAILQ_INIT(&softc->isc_queue);
1045 STAILQ_INIT(&softc->fe_list);
1046 STAILQ_INIT(&softc->be_list);
1047 STAILQ_INIT(&softc->io_pools);
1052 * We don't bother calling these with ctl_lock held here, because,
1053 * in theory, no one else can try to do anything while we're in our
1054 * module init routine.
1056 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1057 &internal_pool)!= 0){
1058 printf("ctl: can't allocate %d entry internal pool, "
1059 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1063 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1064 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1065 printf("ctl: can't allocate %d entry emergency pool, "
1066 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1067 ctl_pool_free(internal_pool);
1071 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1074 printf("ctl: can't allocate %d entry other SC pool, "
1075 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1076 ctl_pool_free(internal_pool);
1077 ctl_pool_free(emergency_pool);
1081 softc->internal_pool = internal_pool;
1082 softc->emergency_pool = emergency_pool;
1083 softc->othersc_pool = other_pool;
1086 * We used to allocate a processor LUN here. The new scheme is to
1087 * just let the user allocate LUNs as he sees fit.
1090 mtx_lock(&softc->ctl_lock);
1091 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1092 mtx_unlock(&softc->ctl_lock);
1095 error = kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
1098 printf("error creating CTL work thread!\n");
1099 mtx_lock(&softc->ctl_lock);
1101 mtx_unlock(&softc->ctl_lock);
1102 ctl_pool_free(internal_pool);
1103 ctl_pool_free(emergency_pool);
1104 ctl_pool_free(other_pool);
1107 printf("ctl: CAM Target Layer loaded\n");
1110 * Initialize the initiator and portname mappings
1112 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1115 * Initialize the ioctl front end.
1117 fe = &softc->ioctl_info.fe;
1118 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1119 fe->port_type = CTL_PORT_IOCTL;
1120 fe->num_requested_ctl_io = 100;
1121 fe->port_name = softc->ioctl_info.port_name;
1122 fe->port_online = ctl_ioctl_online;
1123 fe->port_offline = ctl_ioctl_offline;
1124 fe->onoff_arg = &softc->ioctl_info;
1125 fe->targ_enable = ctl_ioctl_targ_enable;
1126 fe->targ_disable = ctl_ioctl_targ_disable;
1127 fe->lun_enable = ctl_ioctl_lun_enable;
1128 fe->lun_disable = ctl_ioctl_lun_disable;
1129 fe->targ_lun_arg = &softc->ioctl_info;
1130 fe->fe_datamove = ctl_ioctl_datamove;
1131 fe->fe_done = ctl_ioctl_done;
1132 fe->max_targets = 15;
1133 fe->max_target_id = 15;
1135 if (ctl_frontend_register(&softc->ioctl_info.fe,
1136 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1137 printf("ctl: ioctl front end registration failed, will "
1138 "continue anyway\n");
1142 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1143 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1144 sizeof(struct callout), CTL_TIMER_BYTES);
1147 #endif /* CTL_IO_DELAY */
1155 struct ctl_softc *softc;
1156 struct ctl_lun *lun, *next_lun;
1157 struct ctl_io_pool *pool;
1159 softc = (struct ctl_softc *)control_softc;
1161 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1162 printf("ctl: ioctl front end deregistration failed\n");
1164 mtx_lock(&softc->ctl_lock);
1169 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1170 next_lun = STAILQ_NEXT(lun, links);
1174 mtx_unlock(&softc->ctl_lock);
1177 * This will rip the rug out from under any FETDs or anyone else
1178 * that has a pool allocated. Since we increment our module
1179 * refcount any time someone outside the main CTL module allocates
1180 * a pool, we shouldn't have any problems here. The user won't be
1181 * able to unload the CTL module until client modules have
1182 * successfully unloaded.
1184 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1185 ctl_pool_free(pool);
1188 ctl_shutdown_thread(softc->work_thread);
1191 mtx_destroy(&softc->pool_lock);
1192 mtx_destroy(&softc->ctl_lock);
1194 destroy_dev(softc->dev);
1196 sysctl_ctx_free(&softc->sysctl_ctx);
1198 free(control_softc, M_DEVBUF);
1199 control_softc = NULL;
1201 printf("ctl: CAM Target Layer unloaded\n");
1205 ctl_module_event_handler(module_t mod, int what, void *arg)
1210 return (ctl_init());
1214 return (EOPNOTSUPP);
1219 * XXX KDM should we do some access checks here? Bump a reference count to
1220 * prevent a CTL module from being unloaded while someone has it open?
1223 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1229 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1235 ctl_port_enable(ctl_port_type port_type)
1237 struct ctl_softc *softc;
1238 struct ctl_frontend *fe;
1240 if (ctl_is_single == 0) {
1241 union ctl_ha_msg msg_info;
1245 printf("%s: HA mode, synchronizing frontend enable\n",
1248 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1249 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1250 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1251 printf("Sync msg send error retval %d\n", isc_retval);
1253 if (!rcv_sync_msg) {
1254 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1255 sizeof(msg_info), 1);
1258 printf("CTL:Frontend Enable\n");
1260 printf("%s: single mode, skipping frontend synchronization\n",
1265 softc = control_softc;
1267 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1268 if (port_type & fe->port_type)
1271 printf("port %d\n", fe->targ_port);
1273 ctl_frontend_online(fe);
1281 ctl_port_disable(ctl_port_type port_type)
1283 struct ctl_softc *softc;
1284 struct ctl_frontend *fe;
1286 softc = control_softc;
1288 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1289 if (port_type & fe->port_type)
1290 ctl_frontend_offline(fe);
1297 * Returns 0 for success, 1 for failure.
1298 * Currently the only failure mode is if there aren't enough entries
1299 * allocated. So, in case of a failure, look at num_entries_dropped,
1300 * reallocate and try again.
1303 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1304 int *num_entries_filled, int *num_entries_dropped,
1305 ctl_port_type port_type, int no_virtual)
1307 struct ctl_softc *softc;
1308 struct ctl_frontend *fe;
1309 int entries_dropped, entries_filled;
1313 softc = control_softc;
1317 entries_dropped = 0;
1320 mtx_lock(&softc->ctl_lock);
1321 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1322 struct ctl_port_entry *entry;
1324 if ((fe->port_type & port_type) == 0)
1327 if ((no_virtual != 0)
1328 && (fe->virtual_port != 0))
1331 if (entries_filled >= num_entries_alloced) {
1335 entry = &entries[i];
1337 entry->port_type = fe->port_type;
1338 strlcpy(entry->port_name, fe->port_name,
1339 sizeof(entry->port_name));
1340 entry->physical_port = fe->physical_port;
1341 entry->virtual_port = fe->virtual_port;
1342 entry->wwnn = fe->wwnn;
1343 entry->wwpn = fe->wwpn;
1349 mtx_unlock(&softc->ctl_lock);
1351 if (entries_dropped > 0)
1354 *num_entries_dropped = entries_dropped;
1355 *num_entries_filled = entries_filled;
1361 ctl_ioctl_online(void *arg)
1363 struct ctl_ioctl_info *ioctl_info;
1365 ioctl_info = (struct ctl_ioctl_info *)arg;
1367 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1371 ctl_ioctl_offline(void *arg)
1373 struct ctl_ioctl_info *ioctl_info;
1375 ioctl_info = (struct ctl_ioctl_info *)arg;
1377 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1381 * Remove an initiator by port number and initiator ID.
1382 * Returns 0 for success, 1 for failure.
1385 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1387 struct ctl_softc *softc;
1389 softc = control_softc;
1391 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1394 || (targ_port > CTL_MAX_PORTS)) {
1395 printf("%s: invalid port number %d\n", __func__, targ_port);
1398 if (iid > CTL_MAX_INIT_PER_PORT) {
1399 printf("%s: initiator ID %u > maximun %u!\n",
1400 __func__, iid, CTL_MAX_INIT_PER_PORT);
1404 mtx_lock(&softc->ctl_lock);
1406 softc->wwpn_iid[targ_port][iid].in_use = 0;
1408 mtx_unlock(&softc->ctl_lock);
1414 * Add an initiator to the initiator map.
1415 * Returns 0 for success, 1 for failure.
1418 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1420 struct ctl_softc *softc;
1423 softc = control_softc;
1425 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1430 || (targ_port > CTL_MAX_PORTS)) {
1431 printf("%s: invalid port number %d\n", __func__, targ_port);
1434 if (iid > CTL_MAX_INIT_PER_PORT) {
1435 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1436 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1440 mtx_lock(&softc->ctl_lock);
1442 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1444 * We don't treat this as an error.
1446 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1447 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1448 __func__, targ_port, iid, (uintmax_t)wwpn);
1453 * This is an error, but what do we do about it? The
1454 * driver is telling us we have a new WWPN for this
1455 * initiator ID, so we pretty much need to use it.
1457 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1458 "still at that address\n", __func__, targ_port, iid,
1460 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1463 * XXX KDM clear have_ca and ua_pending on each LUN for
1467 softc->wwpn_iid[targ_port][iid].in_use = 1;
1468 softc->wwpn_iid[targ_port][iid].iid = iid;
1469 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1470 softc->wwpn_iid[targ_port][iid].port = targ_port;
1474 mtx_unlock(&softc->ctl_lock);
1480 * XXX KDM should we pretend to do something in the target/lun
1481 * enable/disable functions?
1484 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1490 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1496 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1502 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1508 * Data movement routine for the CTL ioctl frontend port.
1511 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1513 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1514 struct ctl_sg_entry ext_entry, kern_entry;
1515 int ext_sglen, ext_sg_entries, kern_sg_entries;
1516 int ext_sg_start, ext_offset;
1517 int len_to_copy, len_copied;
1518 int kern_watermark, ext_watermark;
1519 int ext_sglist_malloced;
1522 ext_sglist_malloced = 0;
1526 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1529 * If this flag is set, fake the data transfer.
1531 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1532 ctsio->ext_data_filled = ctsio->ext_data_len;
1537 * To simplify things here, if we have a single buffer, stick it in
1538 * a S/G entry and just make it a single entry S/G list.
1540 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1543 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1545 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1547 ext_sglist_malloced = 1;
1548 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1550 ctl_set_internal_failure(ctsio,
1555 ext_sg_entries = ctsio->ext_sg_entries;
1557 for (i = 0; i < ext_sg_entries; i++) {
1558 if ((len_seen + ext_sglist[i].len) >=
1559 ctsio->ext_data_filled) {
1561 ext_offset = ctsio->ext_data_filled - len_seen;
1564 len_seen += ext_sglist[i].len;
1567 ext_sglist = &ext_entry;
1568 ext_sglist->addr = ctsio->ext_data_ptr;
1569 ext_sglist->len = ctsio->ext_data_len;
1572 ext_offset = ctsio->ext_data_filled;
1575 if (ctsio->kern_sg_entries > 0) {
1576 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1577 kern_sg_entries = ctsio->kern_sg_entries;
1579 kern_sglist = &kern_entry;
1580 kern_sglist->addr = ctsio->kern_data_ptr;
1581 kern_sglist->len = ctsio->kern_data_len;
1582 kern_sg_entries = 1;
1587 ext_watermark = ext_offset;
1589 for (i = ext_sg_start, j = 0;
1590 i < ext_sg_entries && j < kern_sg_entries;) {
1591 uint8_t *ext_ptr, *kern_ptr;
1593 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1594 kern_sglist[j].len - kern_watermark);
1596 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1597 ext_ptr = ext_ptr + ext_watermark;
1598 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1602 panic("need to implement bus address support");
1604 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1607 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1608 kern_ptr = kern_ptr + kern_watermark;
1610 kern_watermark += len_to_copy;
1611 ext_watermark += len_to_copy;
1613 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1615 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1616 "bytes to user\n", len_to_copy));
1617 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1618 "to %p\n", kern_ptr, ext_ptr));
1619 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1620 ctl_set_internal_failure(ctsio,
1626 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1627 "bytes from user\n", len_to_copy));
1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1629 "to %p\n", ext_ptr, kern_ptr));
1630 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1631 ctl_set_internal_failure(ctsio,
1638 len_copied += len_to_copy;
1640 if (ext_sglist[i].len == ext_watermark) {
1645 if (kern_sglist[j].len == kern_watermark) {
1651 ctsio->ext_data_filled += len_copied;
1653 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1654 "kern_sg_entries: %d\n", ext_sg_entries,
1656 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1657 "kern_data_len = %d\n", ctsio->ext_data_len,
1658 ctsio->kern_data_len));
1661 /* XXX KDM set residual?? */
1664 if (ext_sglist_malloced != 0)
1665 free(ext_sglist, M_CTL);
1667 return (CTL_RETVAL_COMPLETE);
1671 * Serialize a command that went down the "wrong" side, and so was sent to
1672 * this controller for execution. The logic is a little different than the
1673 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1674 * sent back to the other side, but in the success case, we execute the
1675 * command on this side (XFER mode) or tell the other side to execute it
1679 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1681 struct ctl_softc *ctl_softc;
1682 union ctl_ha_msg msg_info;
1683 struct ctl_lun *lun;
1686 ctl_softc = control_softc;
1688 mtx_lock(&ctl_softc->ctl_lock);
1690 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
1694 * Why isn't LUN defined? The other side wouldn't
1695 * send a cmd if the LUN is undefined.
1697 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1699 /* "Logical unit not supported" */
1700 ctl_set_sense_data(&msg_info.scsi.sense_data,
1702 /*sense_format*/SSD_TYPE_NONE,
1703 /*current_error*/ 1,
1704 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1709 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1710 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1711 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1712 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1713 msg_info.hdr.serializing_sc = NULL;
1714 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1715 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1716 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1719 mtx_unlock(&ctl_softc->ctl_lock);
1724 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1726 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1727 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1729 case CTL_ACTION_BLOCK:
1730 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1731 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1734 case CTL_ACTION_PASS:
1735 case CTL_ACTION_SKIP:
1736 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1737 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1738 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1739 &ctsio->io_hdr, links);
1742 /* send msg back to other side */
1743 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1744 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1745 msg_info.hdr.msg_type = CTL_MSG_R2R;
1747 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1749 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1750 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1754 case CTL_ACTION_OVERLAP:
1755 /* OVERLAPPED COMMANDS ATTEMPTED */
1756 ctl_set_sense_data(&msg_info.scsi.sense_data,
1758 /*sense_format*/SSD_TYPE_NONE,
1759 /*current_error*/ 1,
1760 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1765 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1766 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1767 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1768 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1769 msg_info.hdr.serializing_sc = NULL;
1770 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1772 printf("BAD JUJU:Major Bummer Overlap\n");
1774 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1776 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1777 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1780 case CTL_ACTION_OVERLAP_TAG:
1781 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1782 ctl_set_sense_data(&msg_info.scsi.sense_data,
1784 /*sense_format*/SSD_TYPE_NONE,
1785 /*current_error*/ 1,
1786 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1788 /*ascq*/ ctsio->tag_num & 0xff,
1791 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1792 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1793 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1794 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1795 msg_info.hdr.serializing_sc = NULL;
1796 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1798 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1800 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1802 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1803 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1806 case CTL_ACTION_ERROR:
1808 /* "Internal target failure" */
1809 ctl_set_sense_data(&msg_info.scsi.sense_data,
1811 /*sense_format*/SSD_TYPE_NONE,
1812 /*current_error*/ 1,
1813 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1818 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1819 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1820 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1821 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1822 msg_info.hdr.serializing_sc = NULL;
1823 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1825 printf("BAD JUJU:Major Bummer HW Error\n");
1827 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1829 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1830 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1835 mtx_unlock(&ctl_softc->ctl_lock);
1840 ctl_ioctl_submit_wait(union ctl_io *io)
1842 struct ctl_fe_ioctl_params params;
1843 ctl_fe_ioctl_state last_state;
1848 bzero(¶ms, sizeof(params));
1850 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1851 cv_init(¶ms.sem, "ctlioccv");
1852 params.state = CTL_IOCTL_INPROG;
1853 last_state = params.state;
1855 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1857 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1859 /* This shouldn't happen */
1860 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1866 mtx_lock(¶ms.ioctl_mtx);
1868 * Check the state here, and don't sleep if the state has
1869 * already changed (i.e. wakeup has already occured, but we
1870 * weren't waiting yet).
1872 if (params.state == last_state) {
1873 /* XXX KDM cv_wait_sig instead? */
1874 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1876 last_state = params.state;
1878 switch (params.state) {
1879 case CTL_IOCTL_INPROG:
1880 /* Why did we wake up? */
1881 /* XXX KDM error here? */
1882 mtx_unlock(¶ms.ioctl_mtx);
1884 case CTL_IOCTL_DATAMOVE:
1885 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1888 * change last_state back to INPROG to avoid
1889 * deadlock on subsequent data moves.
1891 params.state = last_state = CTL_IOCTL_INPROG;
1893 mtx_unlock(¶ms.ioctl_mtx);
1894 ctl_ioctl_do_datamove(&io->scsiio);
1896 * Note that in some cases, most notably writes,
1897 * this will queue the I/O and call us back later.
1898 * In other cases, generally reads, this routine
1899 * will immediately call back and wake us up,
1900 * probably using our own context.
1902 io->scsiio.be_move_done(io);
1904 case CTL_IOCTL_DONE:
1905 mtx_unlock(¶ms.ioctl_mtx);
1906 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1910 mtx_unlock(¶ms.ioctl_mtx);
1911 /* XXX KDM error here? */
1914 } while (done == 0);
1916 mtx_destroy(¶ms.ioctl_mtx);
1917 cv_destroy(¶ms.sem);
1919 return (CTL_RETVAL_COMPLETE);
1923 ctl_ioctl_datamove(union ctl_io *io)
1925 struct ctl_fe_ioctl_params *params;
1927 params = (struct ctl_fe_ioctl_params *)
1928 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1930 mtx_lock(¶ms->ioctl_mtx);
1931 params->state = CTL_IOCTL_DATAMOVE;
1932 cv_broadcast(¶ms->sem);
1933 mtx_unlock(¶ms->ioctl_mtx);
1937 ctl_ioctl_done(union ctl_io *io)
1939 struct ctl_fe_ioctl_params *params;
1941 params = (struct ctl_fe_ioctl_params *)
1942 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1944 mtx_lock(¶ms->ioctl_mtx);
1945 params->state = CTL_IOCTL_DONE;
1946 cv_broadcast(¶ms->sem);
1947 mtx_unlock(¶ms->ioctl_mtx);
1951 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1953 struct ctl_fe_ioctl_startstop_info *sd_info;
1955 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1957 sd_info->hs_info.status = metatask->status;
1958 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1959 sd_info->hs_info.luns_complete =
1960 metatask->taskinfo.startstop.luns_complete;
1961 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1963 cv_broadcast(&sd_info->sem);
1967 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1969 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1971 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1973 mtx_lock(fe_bbr_info->lock);
1974 fe_bbr_info->bbr_info->status = metatask->status;
1975 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1976 fe_bbr_info->wakeup_done = 1;
1977 mtx_unlock(fe_bbr_info->lock);
1979 cv_broadcast(&fe_bbr_info->sem);
1983 * Returns 0 for success, errno for failure.
1986 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1987 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
1994 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
1996 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1997 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1999 struct ctl_ooa_entry *entry;
2002 * If we've got more than we can fit, just count the
2003 * remaining entries.
2005 if (*cur_fill_num >= ooa_hdr->alloc_num)
2008 entry = &kern_entries[*cur_fill_num];
2010 entry->tag_num = io->scsiio.tag_num;
2011 entry->lun_num = lun->lun;
2013 entry->start_bt = io->io_hdr.start_bt;
2015 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2016 entry->cdb_len = io->scsiio.cdb_len;
2017 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2018 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2020 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2021 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2023 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2024 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2026 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2027 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2029 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2030 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2037 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2038 size_t error_str_len)
2042 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2044 if (copyin(user_addr, kptr, len) != 0) {
2045 snprintf(error_str, error_str_len, "Error copying %d bytes "
2046 "from user address %p to kernel address %p", len,
2056 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2060 if (be_args == NULL)
2063 for (i = 0; i < num_be_args; i++) {
2064 free(be_args[i].kname, M_CTL);
2065 free(be_args[i].kvalue, M_CTL);
2068 free(be_args, M_CTL);
2071 static struct ctl_be_arg *
2072 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2073 char *error_str, size_t error_str_len)
2075 struct ctl_be_arg *args;
2078 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2079 error_str, error_str_len);
2084 for (i = 0; i < num_be_args; i++) {
2085 args[i].kname = NULL;
2086 args[i].kvalue = NULL;
2089 for (i = 0; i < num_be_args; i++) {
2092 args[i].kname = ctl_copyin_alloc(args[i].name,
2093 args[i].namelen, error_str, error_str_len);
2094 if (args[i].kname == NULL)
2097 if (args[i].kname[args[i].namelen - 1] != '\0') {
2098 snprintf(error_str, error_str_len, "Argument %d "
2099 "name is not NUL-terminated", i);
2103 args[i].kvalue = NULL;
2105 tmpptr = ctl_copyin_alloc(args[i].value,
2106 args[i].vallen, error_str, error_str_len);
2110 args[i].kvalue = tmpptr;
2112 if ((args[i].flags & CTL_BEARG_ASCII)
2113 && (tmpptr[args[i].vallen - 1] != '\0')) {
2114 snprintf(error_str, error_str_len, "Argument %d "
2115 "value is not NUL-terminated", i);
2123 ctl_free_args(num_be_args, args);
2129 * Escape characters that are illegal or not recommended in XML.
2132 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2138 for (; *str; str++) {
2141 retval = sbuf_printf(sb, "&");
2144 retval = sbuf_printf(sb, ">");
2147 retval = sbuf_printf(sb, "<");
2150 retval = sbuf_putc(sb, *str);
2163 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2166 struct ctl_softc *softc;
2169 softc = control_softc;
2179 * If we haven't been "enabled", don't allow any SCSI I/O
2182 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2187 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2189 printf("ctl_ioctl: can't allocate ctl_io!\n");
2195 * Need to save the pool reference so it doesn't get
2196 * spammed by the user's ctl_io.
2198 pool_tmp = io->io_hdr.pool;
2200 memcpy(io, (void *)addr, sizeof(*io));
2202 io->io_hdr.pool = pool_tmp;
2204 * No status yet, so make sure the status is set properly.
2206 io->io_hdr.status = CTL_STATUS_NONE;
2209 * The user sets the initiator ID, target and LUN IDs.
2211 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2212 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2213 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2214 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2215 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2217 retval = ctl_ioctl_submit_wait(io);
2224 memcpy((void *)addr, io, sizeof(*io));
2226 /* return this to our pool */
2231 case CTL_ENABLE_PORT:
2232 case CTL_DISABLE_PORT:
2233 case CTL_SET_PORT_WWNS: {
2234 struct ctl_frontend *fe;
2235 struct ctl_port_entry *entry;
2237 entry = (struct ctl_port_entry *)addr;
2239 mtx_lock(&softc->ctl_lock);
2240 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2246 if ((entry->port_type == CTL_PORT_NONE)
2247 && (entry->targ_port == fe->targ_port)) {
2249 * If the user only wants to enable or
2250 * disable or set WWNs on a specific port,
2251 * do the operation and we're done.
2255 } else if (entry->port_type & fe->port_type) {
2257 * Compare the user's type mask with the
2258 * particular frontend type to see if we
2265 * Make sure the user isn't trying to set
2266 * WWNs on multiple ports at the same time.
2268 if (cmd == CTL_SET_PORT_WWNS) {
2269 printf("%s: Can't set WWNs on "
2270 "multiple ports\n", __func__);
2277 * XXX KDM we have to drop the lock here,
2278 * because the online/offline operations
2279 * can potentially block. We need to
2280 * reference count the frontends so they
2283 mtx_unlock(&softc->ctl_lock);
2285 if (cmd == CTL_ENABLE_PORT) {
2286 struct ctl_lun *lun;
2288 STAILQ_FOREACH(lun, &softc->lun_list,
2290 fe->lun_enable(fe->targ_lun_arg,
2295 ctl_frontend_online(fe);
2296 } else if (cmd == CTL_DISABLE_PORT) {
2297 struct ctl_lun *lun;
2299 ctl_frontend_offline(fe);
2301 STAILQ_FOREACH(lun, &softc->lun_list,
2310 mtx_lock(&softc->ctl_lock);
2312 if (cmd == CTL_SET_PORT_WWNS)
2313 ctl_frontend_set_wwns(fe,
2314 (entry->flags & CTL_PORT_WWNN_VALID) ?
2316 (entry->flags & CTL_PORT_WWPN_VALID) ?
2317 1 : 0, entry->wwpn);
2322 mtx_unlock(&softc->ctl_lock);
2325 case CTL_GET_PORT_LIST: {
2326 struct ctl_frontend *fe;
2327 struct ctl_port_list *list;
2330 list = (struct ctl_port_list *)addr;
2332 if (list->alloc_len != (list->alloc_num *
2333 sizeof(struct ctl_port_entry))) {
2334 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2335 "alloc_num %u * sizeof(struct ctl_port_entry) "
2336 "%zu\n", __func__, list->alloc_len,
2337 list->alloc_num, sizeof(struct ctl_port_entry));
2343 list->dropped_num = 0;
2345 mtx_lock(&softc->ctl_lock);
2346 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2347 struct ctl_port_entry entry, *list_entry;
2349 if (list->fill_num >= list->alloc_num) {
2350 list->dropped_num++;
2354 entry.port_type = fe->port_type;
2355 strlcpy(entry.port_name, fe->port_name,
2356 sizeof(entry.port_name));
2357 entry.targ_port = fe->targ_port;
2358 entry.physical_port = fe->physical_port;
2359 entry.virtual_port = fe->virtual_port;
2360 entry.wwnn = fe->wwnn;
2361 entry.wwpn = fe->wwpn;
2362 if (fe->status & CTL_PORT_STATUS_ONLINE)
2367 list_entry = &list->entries[i];
2369 retval = copyout(&entry, list_entry, sizeof(entry));
2371 printf("%s: CTL_GET_PORT_LIST: copyout "
2372 "returned %d\n", __func__, retval);
2377 list->fill_len += sizeof(entry);
2379 mtx_unlock(&softc->ctl_lock);
2382 * If this is non-zero, we had a copyout fault, so there's
2383 * probably no point in attempting to set the status inside
2389 if (list->dropped_num > 0)
2390 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2392 list->status = CTL_PORT_LIST_OK;
2395 case CTL_DUMP_OOA: {
2396 struct ctl_lun *lun;
2401 mtx_lock(&softc->ctl_lock);
2402 printf("Dumping OOA queues:\n");
2403 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2404 for (io = (union ctl_io *)TAILQ_FIRST(
2405 &lun->ooa_queue); io != NULL;
2406 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2408 sbuf_new(&sb, printbuf, sizeof(printbuf),
2410 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2414 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2416 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2418 CTL_FLAG_ABORT) ? " ABORT" : "",
2420 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2421 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2423 printf("%s\n", sbuf_data(&sb));
2426 printf("OOA queues dump done\n");
2427 mtx_unlock(&softc->ctl_lock);
2431 struct ctl_lun *lun;
2432 struct ctl_ooa *ooa_hdr;
2433 struct ctl_ooa_entry *entries;
2434 uint32_t cur_fill_num;
2436 ooa_hdr = (struct ctl_ooa *)addr;
2438 if ((ooa_hdr->alloc_len == 0)
2439 || (ooa_hdr->alloc_num == 0)) {
2440 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2441 "must be non-zero\n", __func__,
2442 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2447 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2448 sizeof(struct ctl_ooa_entry))) {
2449 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2450 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2451 __func__, ooa_hdr->alloc_len,
2452 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2457 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2458 if (entries == NULL) {
2459 printf("%s: could not allocate %d bytes for OOA "
2460 "dump\n", __func__, ooa_hdr->alloc_len);
2465 mtx_lock(&softc->ctl_lock);
2466 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2467 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2468 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2469 mtx_unlock(&softc->ctl_lock);
2470 free(entries, M_CTL);
2471 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2472 __func__, (uintmax_t)ooa_hdr->lun_num);
2479 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2480 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2481 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2487 mtx_unlock(&softc->ctl_lock);
2488 free(entries, M_CTL);
2492 lun = softc->ctl_luns[ooa_hdr->lun_num];
2494 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2497 mtx_unlock(&softc->ctl_lock);
2499 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2500 ooa_hdr->fill_len = ooa_hdr->fill_num *
2501 sizeof(struct ctl_ooa_entry);
2502 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2504 printf("%s: error copying out %d bytes for OOA dump\n",
2505 __func__, ooa_hdr->fill_len);
2508 getbintime(&ooa_hdr->cur_bt);
2510 if (cur_fill_num > ooa_hdr->alloc_num) {
2511 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2512 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2514 ooa_hdr->dropped_num = 0;
2515 ooa_hdr->status = CTL_OOA_OK;
2518 free(entries, M_CTL);
2521 case CTL_CHECK_OOA: {
2523 struct ctl_lun *lun;
2524 struct ctl_ooa_info *ooa_info;
2527 ooa_info = (struct ctl_ooa_info *)addr;
2529 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2530 ooa_info->status = CTL_OOA_INVALID_LUN;
2533 mtx_lock(&softc->ctl_lock);
2534 lun = softc->ctl_luns[ooa_info->lun_id];
2536 mtx_unlock(&softc->ctl_lock);
2537 ooa_info->status = CTL_OOA_INVALID_LUN;
2541 ooa_info->num_entries = 0;
2542 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2543 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2544 &io->io_hdr, ooa_links)) {
2545 ooa_info->num_entries++;
2548 mtx_unlock(&softc->ctl_lock);
2549 ooa_info->status = CTL_OOA_SUCCESS;
2553 case CTL_HARD_START:
2554 case CTL_HARD_STOP: {
2555 struct ctl_fe_ioctl_startstop_info ss_info;
2556 struct cfi_metatask *metatask;
2559 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2561 cv_init(&ss_info.sem, "hard start/stop cv" );
2563 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2564 if (metatask == NULL) {
2566 mtx_destroy(&hs_mtx);
2570 if (cmd == CTL_HARD_START)
2571 metatask->tasktype = CFI_TASK_STARTUP;
2573 metatask->tasktype = CFI_TASK_SHUTDOWN;
2575 metatask->callback = ctl_ioctl_hard_startstop_callback;
2576 metatask->callback_arg = &ss_info;
2578 cfi_action(metatask);
2580 /* Wait for the callback */
2582 cv_wait_sig(&ss_info.sem, &hs_mtx);
2583 mtx_unlock(&hs_mtx);
2586 * All information has been copied from the metatask by the
2587 * time cv_broadcast() is called, so we free the metatask here.
2589 cfi_free_metatask(metatask);
2591 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2593 mtx_destroy(&hs_mtx);
2597 struct ctl_bbrread_info *bbr_info;
2598 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2600 struct cfi_metatask *metatask;
2602 bbr_info = (struct ctl_bbrread_info *)addr;
2604 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2606 bzero(&bbr_mtx, sizeof(bbr_mtx));
2607 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2609 fe_bbr_info.bbr_info = bbr_info;
2610 fe_bbr_info.lock = &bbr_mtx;
2612 cv_init(&fe_bbr_info.sem, "BBR read cv");
2613 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2615 if (metatask == NULL) {
2616 mtx_destroy(&bbr_mtx);
2617 cv_destroy(&fe_bbr_info.sem);
2621 metatask->tasktype = CFI_TASK_BBRREAD;
2622 metatask->callback = ctl_ioctl_bbrread_callback;
2623 metatask->callback_arg = &fe_bbr_info;
2624 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2625 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2626 metatask->taskinfo.bbrread.len = bbr_info->len;
2628 cfi_action(metatask);
2631 while (fe_bbr_info.wakeup_done == 0)
2632 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2633 mtx_unlock(&bbr_mtx);
2635 bbr_info->status = metatask->status;
2636 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2637 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2638 memcpy(&bbr_info->sense_data,
2639 &metatask->taskinfo.bbrread.sense_data,
2640 ctl_min(sizeof(bbr_info->sense_data),
2641 sizeof(metatask->taskinfo.bbrread.sense_data)));
2643 cfi_free_metatask(metatask);
2645 mtx_destroy(&bbr_mtx);
2646 cv_destroy(&fe_bbr_info.sem);
2650 case CTL_DELAY_IO: {
2651 struct ctl_io_delay_info *delay_info;
2653 struct ctl_lun *lun;
2654 #endif /* CTL_IO_DELAY */
2656 delay_info = (struct ctl_io_delay_info *)addr;
2659 mtx_lock(&softc->ctl_lock);
2661 if ((delay_info->lun_id > CTL_MAX_LUNS)
2662 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2663 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2665 lun = softc->ctl_luns[delay_info->lun_id];
2667 delay_info->status = CTL_DELAY_STATUS_OK;
2669 switch (delay_info->delay_type) {
2670 case CTL_DELAY_TYPE_CONT:
2672 case CTL_DELAY_TYPE_ONESHOT:
2675 delay_info->status =
2676 CTL_DELAY_STATUS_INVALID_TYPE;
2680 switch (delay_info->delay_loc) {
2681 case CTL_DELAY_LOC_DATAMOVE:
2682 lun->delay_info.datamove_type =
2683 delay_info->delay_type;
2684 lun->delay_info.datamove_delay =
2685 delay_info->delay_secs;
2687 case CTL_DELAY_LOC_DONE:
2688 lun->delay_info.done_type =
2689 delay_info->delay_type;
2690 lun->delay_info.done_delay =
2691 delay_info->delay_secs;
2694 delay_info->status =
2695 CTL_DELAY_STATUS_INVALID_LOC;
2700 mtx_unlock(&softc->ctl_lock);
2702 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2703 #endif /* CTL_IO_DELAY */
2706 case CTL_REALSYNC_SET: {
2709 syncstate = (int *)addr;
2711 mtx_lock(&softc->ctl_lock);
2712 switch (*syncstate) {
2714 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2717 softc->flags |= CTL_FLAG_REAL_SYNC;
2723 mtx_unlock(&softc->ctl_lock);
2726 case CTL_REALSYNC_GET: {
2729 syncstate = (int*)addr;
2731 mtx_lock(&softc->ctl_lock);
2732 if (softc->flags & CTL_FLAG_REAL_SYNC)
2736 mtx_unlock(&softc->ctl_lock);
2742 struct ctl_sync_info *sync_info;
2743 struct ctl_lun *lun;
2745 sync_info = (struct ctl_sync_info *)addr;
2747 mtx_lock(&softc->ctl_lock);
2748 lun = softc->ctl_luns[sync_info->lun_id];
2750 mtx_unlock(&softc->ctl_lock);
2751 sync_info->status = CTL_GS_SYNC_NO_LUN;
2754 * Get or set the sync interval. We're not bounds checking
2755 * in the set case, hopefully the user won't do something
2758 if (cmd == CTL_GETSYNC)
2759 sync_info->sync_interval = lun->sync_interval;
2761 lun->sync_interval = sync_info->sync_interval;
2763 mtx_unlock(&softc->ctl_lock);
2765 sync_info->status = CTL_GS_SYNC_OK;
2769 case CTL_GETSTATS: {
2770 struct ctl_stats *stats;
2771 struct ctl_lun *lun;
2774 stats = (struct ctl_stats *)addr;
2776 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2778 stats->status = CTL_SS_NEED_MORE_SPACE;
2779 stats->num_luns = softc->num_luns;
2783 * XXX KDM no locking here. If the LUN list changes,
2784 * things can blow up.
2786 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2787 i++, lun = STAILQ_NEXT(lun, links)) {
2788 retval = copyout(&lun->stats, &stats->lun_stats[i],
2789 sizeof(lun->stats));
2793 stats->num_luns = softc->num_luns;
2794 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2796 stats->status = CTL_SS_OK;
2798 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2800 stats->flags = CTL_STATS_FLAG_NONE;
2802 getnanouptime(&stats->timestamp);
2805 case CTL_ERROR_INJECT: {
2806 struct ctl_error_desc *err_desc, *new_err_desc;
2807 struct ctl_lun *lun;
2809 err_desc = (struct ctl_error_desc *)addr;
2811 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2813 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2815 mtx_lock(&softc->ctl_lock);
2816 lun = softc->ctl_luns[err_desc->lun_id];
2818 mtx_unlock(&softc->ctl_lock);
2819 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2820 __func__, (uintmax_t)err_desc->lun_id);
2826 * We could do some checking here to verify the validity
2827 * of the request, but given the complexity of error
2828 * injection requests, the checking logic would be fairly
2831 * For now, if the request is invalid, it just won't get
2832 * executed and might get deleted.
2834 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2837 * XXX KDM check to make sure the serial number is unique,
2838 * in case we somehow manage to wrap. That shouldn't
2839 * happen for a very long time, but it's the right thing to
2842 new_err_desc->serial = lun->error_serial;
2843 err_desc->serial = lun->error_serial;
2844 lun->error_serial++;
2846 mtx_unlock(&softc->ctl_lock);
2849 case CTL_ERROR_INJECT_DELETE: {
2850 struct ctl_error_desc *delete_desc, *desc, *desc2;
2851 struct ctl_lun *lun;
2854 delete_desc = (struct ctl_error_desc *)addr;
2857 mtx_lock(&softc->ctl_lock);
2858 lun = softc->ctl_luns[delete_desc->lun_id];
2860 mtx_unlock(&softc->ctl_lock);
2861 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2862 __func__, (uintmax_t)delete_desc->lun_id);
2866 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2867 if (desc->serial != delete_desc->serial)
2870 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2875 mtx_unlock(&softc->ctl_lock);
2876 if (delete_done == 0) {
2877 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2878 "error serial %ju on LUN %u\n", __func__,
2879 delete_desc->serial, delete_desc->lun_id);
2885 case CTL_DUMP_STRUCTS: {
2887 struct ctl_frontend *fe;
2889 printf("CTL IID to WWPN map start:\n");
2890 for (i = 0; i < CTL_MAX_PORTS; i++) {
2891 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2892 if (softc->wwpn_iid[i][j].in_use == 0)
2895 printf("port %d iid %u WWPN %#jx\n",
2896 softc->wwpn_iid[i][j].port,
2897 softc->wwpn_iid[i][j].iid,
2898 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2901 printf("CTL IID to WWPN map end\n");
2902 printf("CTL Persistent Reservation information start:\n");
2903 for (i = 0; i < CTL_MAX_LUNS; i++) {
2904 struct ctl_lun *lun;
2906 lun = softc->ctl_luns[i];
2909 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2912 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2913 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2914 if (lun->per_res[j+k].registered == 0)
2916 printf("LUN %d port %d iid %d key "
2918 (uintmax_t)scsi_8btou64(
2919 lun->per_res[j+k].res_key.key));
2923 printf("CTL Persistent Reservation information end\n");
2924 printf("CTL Frontends:\n");
2926 * XXX KDM calling this without a lock. We'd likely want
2927 * to drop the lock before calling the frontend's dump
2930 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2931 printf("Frontend %s Type %u pport %d vport %d WWNN "
2932 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2933 fe->physical_port, fe->virtual_port,
2934 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2937 * Frontends are not required to support the dump
2940 if (fe->fe_dump == NULL)
2945 printf("CTL Frontend information end\n");
2949 struct ctl_lun_req *lun_req;
2950 struct ctl_backend_driver *backend;
2952 lun_req = (struct ctl_lun_req *)addr;
2954 backend = ctl_backend_find(lun_req->backend);
2955 if (backend == NULL) {
2956 lun_req->status = CTL_LUN_ERROR;
2957 snprintf(lun_req->error_str,
2958 sizeof(lun_req->error_str),
2959 "Backend \"%s\" not found.",
2963 if (lun_req->num_be_args > 0) {
2964 lun_req->kern_be_args = ctl_copyin_args(
2965 lun_req->num_be_args,
2968 sizeof(lun_req->error_str));
2969 if (lun_req->kern_be_args == NULL) {
2970 lun_req->status = CTL_LUN_ERROR;
2975 retval = backend->ioctl(dev, cmd, addr, flag, td);
2977 if (lun_req->num_be_args > 0) {
2978 ctl_free_args(lun_req->num_be_args,
2979 lun_req->kern_be_args);
2983 case CTL_LUN_LIST: {
2985 struct ctl_lun *lun;
2986 struct ctl_lun_list *list;
2988 list = (struct ctl_lun_list *)addr;
2991 * Allocate a fixed length sbuf here, based on the length
2992 * of the user's buffer. We could allocate an auto-extending
2993 * buffer, and then tell the user how much larger our
2994 * amount of data is than his buffer, but that presents
2997 * 1. The sbuf(9) routines use a blocking malloc, and so
2998 * we can't hold a lock while calling them with an
2999 * auto-extending buffer.
3001 * 2. There is not currently a LUN reference counting
3002 * mechanism, outside of outstanding transactions on
3003 * the LUN's OOA queue. So a LUN could go away on us
3004 * while we're getting the LUN number, backend-specific
3005 * information, etc. Thus, given the way things
3006 * currently work, we need to hold the CTL lock while
3007 * grabbing LUN information.
3009 * So, from the user's standpoint, the best thing to do is
3010 * allocate what he thinks is a reasonable buffer length,
3011 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3012 * double the buffer length and try again. (And repeat
3013 * that until he succeeds.)
3015 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3017 list->status = CTL_LUN_LIST_ERROR;
3018 snprintf(list->error_str, sizeof(list->error_str),
3019 "Unable to allocate %d bytes for LUN list",
3024 sbuf_printf(sb, "<ctllunlist>\n");
3026 mtx_lock(&softc->ctl_lock);
3028 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3029 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3030 (uintmax_t)lun->lun);
3033 * Bail out as soon as we see that we've overfilled
3039 retval = sbuf_printf(sb, "<backend_type>%s"
3040 "</backend_type>\n",
3041 (lun->backend == NULL) ? "none" :
3042 lun->backend->name);
3047 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3048 lun->be_lun->lun_type);
3053 if (lun->backend == NULL) {
3054 retval = sbuf_printf(sb, "</lun>\n");
3060 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3061 (lun->be_lun->maxlba > 0) ?
3062 lun->be_lun->maxlba + 1 : 0);
3067 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3068 lun->be_lun->blocksize);
3073 retval = sbuf_printf(sb, "<serial_number>");
3078 retval = ctl_sbuf_printf_esc(sb,
3079 lun->be_lun->serial_num);
3084 retval = sbuf_printf(sb, "</serial_number>\n");
3089 retval = sbuf_printf(sb, "<device_id>");
3094 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3099 retval = sbuf_printf(sb, "</device_id>\n");
3104 if (lun->backend->lun_info == NULL) {
3105 retval = sbuf_printf(sb, "</lun>\n");
3111 retval =lun->backend->lun_info(lun->be_lun->be_lun, sb);
3116 retval = sbuf_printf(sb, "</lun>\n");
3121 mtx_unlock(&softc->ctl_lock);
3124 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3127 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3128 snprintf(list->error_str, sizeof(list->error_str),
3129 "Out of space, %d bytes is too small",
3136 retval = copyout(sbuf_data(sb), list->lun_xml,
3139 list->fill_len = sbuf_len(sb) + 1;
3140 list->status = CTL_LUN_LIST_OK;
3145 /* XXX KDM should we fix this? */
3147 struct ctl_backend_driver *backend;
3154 * We encode the backend type as the ioctl type for backend
3155 * ioctls. So parse it out here, and then search for a
3156 * backend of this type.
3158 type = _IOC_TYPE(cmd);
3160 STAILQ_FOREACH(backend, &softc->be_list, links) {
3161 if (backend->type == type) {
3167 printf("ctl: unknown ioctl command %#lx or backend "
3172 retval = backend->ioctl(dev, cmd, addr, flag, td);
3182 ctl_get_initindex(struct ctl_nexus *nexus)
3184 if (nexus->targ_port < CTL_MAX_PORTS)
3185 return (nexus->initid.id +
3186 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3188 return (nexus->initid.id +
3189 ((nexus->targ_port - CTL_MAX_PORTS) *
3190 CTL_MAX_INIT_PER_PORT));
3194 ctl_get_resindex(struct ctl_nexus *nexus)
3196 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3200 ctl_port_idx(int port_num)
3202 if (port_num < CTL_MAX_PORTS)
3205 return(port_num - CTL_MAX_PORTS);
3209 * Note: This only works for bitmask sizes that are at least 32 bits, and
3210 * that are a power of 2.
3213 ctl_ffz(uint32_t *mask, uint32_t size)
3215 uint32_t num_chunks, num_pieces;
3218 num_chunks = (size >> 5);
3219 if (num_chunks == 0)
3221 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3223 for (i = 0; i < num_chunks; i++) {
3224 for (j = 0; j < num_pieces; j++) {
3225 if ((mask[i] & (1 << j)) == 0)
3226 return ((i << 5) + j);
3234 ctl_set_mask(uint32_t *mask, uint32_t bit)
3236 uint32_t chunk, piece;
3239 piece = bit % (sizeof(uint32_t) * 8);
3241 if ((mask[chunk] & (1 << piece)) != 0)
3244 mask[chunk] |= (1 << piece);
3250 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3252 uint32_t chunk, piece;
3255 piece = bit % (sizeof(uint32_t) * 8);
3257 if ((mask[chunk] & (1 << piece)) == 0)
3260 mask[chunk] &= ~(1 << piece);
3266 ctl_is_set(uint32_t *mask, uint32_t bit)
3268 uint32_t chunk, piece;
3271 piece = bit % (sizeof(uint32_t) * 8);
3273 if ((mask[chunk] & (1 << piece)) == 0)
3281 * The bus, target and lun are optional, they can be filled in later.
3282 * can_wait is used to determine whether we can wait on the malloc or not.
3285 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3286 uint32_t targ_lun, int can_wait)
3291 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3293 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3296 io->io_hdr.io_type = io_type;
3297 io->io_hdr.targ_port = targ_port;
3299 * XXX KDM this needs to change/go away. We need to move
3300 * to a preallocated pool of ctl_scsiio structures.
3302 io->io_hdr.nexus.targ_target.id = targ_target;
3303 io->io_hdr.nexus.targ_lun = targ_lun;
3310 ctl_kfree_io(union ctl_io *io)
3317 * ctl_softc, pool_type, total_ctl_io are passed in.
3318 * npool is passed out.
3321 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3322 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3325 union ctl_io *cur_io, *next_io;
3326 struct ctl_io_pool *pool;
3331 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3338 pool->type = pool_type;
3339 pool->ctl_softc = ctl_softc;
3341 mtx_lock(&ctl_softc->pool_lock);
3342 pool->id = ctl_softc->cur_pool_id++;
3343 mtx_unlock(&ctl_softc->pool_lock);
3345 pool->flags = CTL_POOL_FLAG_NONE;
3346 pool->refcount = 1; /* Reference for validity. */
3347 STAILQ_INIT(&pool->free_queue);
3350 * XXX KDM other options here:
3351 * - allocate a page at a time
3352 * - allocate one big chunk of memory.
3353 * Page allocation might work well, but would take a little more
3356 for (i = 0; i < total_ctl_io; i++) {
3357 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3359 if (cur_io == NULL) {
3363 cur_io->io_hdr.pool = pool;
3364 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3365 pool->total_ctl_io++;
3366 pool->free_ctl_io++;
3370 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3371 cur_io != NULL; cur_io = next_io) {
3372 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3374 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3376 free(cur_io, M_CTL);
3382 mtx_lock(&ctl_softc->pool_lock);
3383 ctl_softc->num_pools++;
3384 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3386 * Increment our usage count if this is an external consumer, so we
3387 * can't get unloaded until the external consumer (most likely a
3388 * FETD) unloads and frees his pool.
3390 * XXX KDM will this increment the caller's module use count, or
3394 if ((pool_type != CTL_POOL_EMERGENCY)
3395 && (pool_type != CTL_POOL_INTERNAL)
3396 && (pool_type != CTL_POOL_IOCTL)
3397 && (pool_type != CTL_POOL_4OTHERSC))
3401 mtx_unlock(&ctl_softc->pool_lock);
3411 ctl_pool_acquire(struct ctl_io_pool *pool)
3414 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3416 if (pool->flags & CTL_POOL_FLAG_INVALID)
3425 ctl_pool_release(struct ctl_io_pool *pool)
3427 struct ctl_softc *ctl_softc = pool->ctl_softc;
3430 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3432 if (--pool->refcount != 0)
3435 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3436 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3441 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3442 ctl_softc->num_pools--;
3445 * XXX KDM will this decrement the caller's usage count or mine?
3448 if ((pool->type != CTL_POOL_EMERGENCY)
3449 && (pool->type != CTL_POOL_INTERNAL)
3450 && (pool->type != CTL_POOL_IOCTL))
3458 ctl_pool_free(struct ctl_io_pool *pool)
3460 struct ctl_softc *ctl_softc;
3465 ctl_softc = pool->ctl_softc;
3466 mtx_lock(&ctl_softc->pool_lock);
3467 pool->flags |= CTL_POOL_FLAG_INVALID;
3468 ctl_pool_release(pool);
3469 mtx_unlock(&ctl_softc->pool_lock);
3473 * This routine does not block (except for spinlocks of course).
3474 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3478 ctl_alloc_io(void *pool_ref)
3481 struct ctl_softc *ctl_softc;
3482 struct ctl_io_pool *pool, *npool;
3483 struct ctl_io_pool *emergency_pool;
3485 pool = (struct ctl_io_pool *)pool_ref;
3488 printf("%s: pool is NULL\n", __func__);
3492 emergency_pool = NULL;
3494 ctl_softc = pool->ctl_softc;
3496 mtx_lock(&ctl_softc->pool_lock);
3498 * First, try to get the io structure from the user's pool.
3500 if (ctl_pool_acquire(pool) == 0) {
3501 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3503 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3504 pool->total_allocated++;
3505 pool->free_ctl_io--;
3506 mtx_unlock(&ctl_softc->pool_lock);
3509 ctl_pool_release(pool);
3512 * If he doesn't have any io structures left, search for an
3513 * emergency pool and grab one from there.
3515 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3516 if (npool->type != CTL_POOL_EMERGENCY)
3519 if (ctl_pool_acquire(npool) != 0)
3522 emergency_pool = npool;
3524 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3526 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3527 npool->total_allocated++;
3528 npool->free_ctl_io--;
3529 mtx_unlock(&ctl_softc->pool_lock);
3532 ctl_pool_release(npool);
3535 /* Drop the spinlock before we malloc */
3536 mtx_unlock(&ctl_softc->pool_lock);
3539 * The emergency pool (if it exists) didn't have one, so try an
3540 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3542 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3545 * If the emergency pool exists but is empty, add this
3546 * ctl_io to its list when it gets freed.
3548 if (emergency_pool != NULL) {
3549 mtx_lock(&ctl_softc->pool_lock);
3550 if (ctl_pool_acquire(emergency_pool) == 0) {
3551 io->io_hdr.pool = emergency_pool;
3552 emergency_pool->total_ctl_io++;
3554 * Need to bump this, otherwise
3555 * total_allocated and total_freed won't
3556 * match when we no longer have anything
3559 emergency_pool->total_allocated++;
3561 mtx_unlock(&ctl_softc->pool_lock);
3563 io->io_hdr.pool = NULL;
3570 ctl_free_io(union ctl_io *io)
3576 * If this ctl_io has a pool, return it to that pool.
3578 if (io->io_hdr.pool != NULL) {
3579 struct ctl_io_pool *pool;
3581 struct ctl_softc *ctl_softc;
3582 union ctl_io *tmp_io;
3583 unsigned long xflags;
3586 ctl_softc = control_softc;
3589 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3591 mtx_lock(&pool->ctl_softc->pool_lock);
3595 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3596 &ctl_softc->task_queue); tmp_io != NULL; i++,
3597 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3600 printf("%s: %p is still on the task queue!\n",
3602 printf("%s: (%d): type %d "
3603 "msg %d cdb %x iptl: "
3604 "%d:%d:%d:%d tag 0x%04x "
3607 tmp_io->io_hdr.io_type,
3608 tmp_io->io_hdr.msg_type,
3609 tmp_io->scsiio.cdb[0],
3610 tmp_io->io_hdr.nexus.initid.id,
3611 tmp_io->io_hdr.nexus.targ_port,
3612 tmp_io->io_hdr.nexus.targ_target.id,
3613 tmp_io->io_hdr.nexus.targ_lun,
3614 (tmp_io->io_hdr.io_type ==
3616 tmp_io->taskio.tag_num :
3617 tmp_io->scsiio.tag_num,
3619 panic("I/O still on the task queue!");
3623 io->io_hdr.io_type = 0xff;
3624 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3625 pool->total_freed++;
3626 pool->free_ctl_io++;
3627 ctl_pool_release(pool);
3628 mtx_unlock(&pool->ctl_softc->pool_lock);
3631 * Otherwise, just free it. We probably malloced it and
3632 * the emergency pool wasn't available.
3640 ctl_zero_io(union ctl_io *io)
3648 * May need to preserve linked list pointers at some point too.
3650 pool_ref = io->io_hdr.pool;
3652 memset(io, 0, sizeof(*io));
3654 io->io_hdr.pool = pool_ref;
3658 * This routine is currently used for internal copies of ctl_ios that need
3659 * to persist for some reason after we've already returned status to the
3660 * FETD. (Thus the flag set.)
3663 * Note that this makes a blind copy of all fields in the ctl_io, except
3664 * for the pool reference. This includes any memory that has been
3665 * allocated! That memory will no longer be valid after done has been
3666 * called, so this would be VERY DANGEROUS for command that actually does
3667 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3668 * start and stop commands, which don't transfer any data, so this is not a
3669 * problem. If it is used for anything else, the caller would also need to
3670 * allocate data buffer space and this routine would need to be modified to
3671 * copy the data buffer(s) as well.
3674 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3683 * May need to preserve linked list pointers at some point too.
3685 pool_ref = dest->io_hdr.pool;
3687 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3689 dest->io_hdr.pool = pool_ref;
3691 * We need to know that this is an internal copy, and doesn't need
3692 * to get passed back to the FETD that allocated it.
3694 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3699 ctl_update_power_subpage(struct copan_power_subpage *page)
3701 int num_luns, num_partitions, config_type;
3702 struct ctl_softc *softc;
3703 cs_BOOL_t aor_present, shelf_50pct_power;
3704 cs_raidset_personality_t rs_type;
3705 int max_active_luns;
3707 softc = control_softc;
3709 /* subtract out the processor LUN */
3710 num_luns = softc->num_luns - 1;
3712 * Default to 7 LUNs active, which was the only number we allowed
3715 max_active_luns = 7;
3717 num_partitions = config_GetRsPartitionInfo();
3718 config_type = config_GetConfigType();
3719 shelf_50pct_power = config_GetShelfPowerMode();
3720 aor_present = config_IsAorRsPresent();
3722 rs_type = ddb_GetRsRaidType(1);
3723 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3724 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3725 EPRINT(0, "Unsupported RS type %d!", rs_type);
3729 page->total_luns = num_luns;
3731 switch (config_type) {
3734 * In a 40 drive configuration, it doesn't matter what DC
3735 * cards we have, whether we have AOR enabled or not,
3736 * partitioning or not, or what type of RAIDset we have.
3737 * In that scenario, we can power up every LUN we present
3740 max_active_luns = num_luns;
3744 if (shelf_50pct_power == CS_FALSE) {
3746 if (aor_present == CS_TRUE) {
3748 CS_RAIDSET_PERSONALITY_RAID5) {
3749 max_active_luns = 7;
3750 } else if (rs_type ==
3751 CS_RAIDSET_PERSONALITY_RAID1){
3752 max_active_luns = 14;
3754 /* XXX KDM now what?? */
3758 CS_RAIDSET_PERSONALITY_RAID5) {
3759 max_active_luns = 8;
3760 } else if (rs_type ==
3761 CS_RAIDSET_PERSONALITY_RAID1){
3762 max_active_luns = 16;
3764 /* XXX KDM now what?? */
3770 * With 50% power in a 64 drive configuration, we
3771 * can power all LUNs we present.
3773 max_active_luns = num_luns;
3777 if (shelf_50pct_power == CS_FALSE) {
3779 if (aor_present == CS_TRUE) {
3781 CS_RAIDSET_PERSONALITY_RAID5) {
3782 max_active_luns = 7;
3783 } else if (rs_type ==
3784 CS_RAIDSET_PERSONALITY_RAID1){
3785 max_active_luns = 14;
3787 /* XXX KDM now what?? */
3791 CS_RAIDSET_PERSONALITY_RAID5) {
3792 max_active_luns = 8;
3793 } else if (rs_type ==
3794 CS_RAIDSET_PERSONALITY_RAID1){
3795 max_active_luns = 16;
3797 /* XXX KDM now what?? */
3802 if (aor_present == CS_TRUE) {
3804 CS_RAIDSET_PERSONALITY_RAID5) {
3805 max_active_luns = 14;
3806 } else if (rs_type ==
3807 CS_RAIDSET_PERSONALITY_RAID1){
3809 * We're assuming here that disk
3810 * caching is enabled, and so we're
3811 * able to power up half of each
3812 * LUN, and cache all writes.
3814 max_active_luns = num_luns;
3816 /* XXX KDM now what?? */
3820 CS_RAIDSET_PERSONALITY_RAID5) {
3821 max_active_luns = 15;
3822 } else if (rs_type ==
3823 CS_RAIDSET_PERSONALITY_RAID1){
3824 max_active_luns = 30;
3826 /* XXX KDM now what?? */
3833 * In this case, we have an unknown configuration, so we
3834 * just use the default from above.
3839 page->max_active_luns = max_active_luns;
3841 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3842 page->total_luns, page->max_active_luns);
3845 #endif /* NEEDTOPORT */
3848 * This routine could be used in the future to load default and/or saved
3849 * mode page parameters for a particuar lun.
3852 ctl_init_page_index(struct ctl_lun *lun)
3855 struct ctl_page_index *page_index;
3856 struct ctl_softc *softc;
3858 memcpy(&lun->mode_pages.index, page_index_template,
3859 sizeof(page_index_template));
3861 softc = lun->ctl_softc;
3863 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3865 page_index = &lun->mode_pages.index[i];
3867 * If this is a disk-only mode page, there's no point in
3868 * setting it up. For some pages, we have to have some
3869 * basic information about the disk in order to calculate the
3872 if ((lun->be_lun->lun_type != T_DIRECT)
3873 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3876 switch (page_index->page_code & SMPH_PC_MASK) {
3877 case SMS_FORMAT_DEVICE_PAGE: {
3878 struct scsi_format_page *format_page;
3880 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3881 panic("subpage is incorrect!");
3884 * Sectors per track are set above. Bytes per
3885 * sector need to be set here on a per-LUN basis.
3887 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3888 &format_page_default,
3889 sizeof(format_page_default));
3890 memcpy(&lun->mode_pages.format_page[
3891 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3892 sizeof(format_page_changeable));
3893 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3894 &format_page_default,
3895 sizeof(format_page_default));
3896 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3897 &format_page_default,
3898 sizeof(format_page_default));
3900 format_page = &lun->mode_pages.format_page[
3902 scsi_ulto2b(lun->be_lun->blocksize,
3903 format_page->bytes_per_sector);
3905 format_page = &lun->mode_pages.format_page[
3907 scsi_ulto2b(lun->be_lun->blocksize,
3908 format_page->bytes_per_sector);
3910 format_page = &lun->mode_pages.format_page[
3912 scsi_ulto2b(lun->be_lun->blocksize,
3913 format_page->bytes_per_sector);
3915 page_index->page_data =
3916 (uint8_t *)lun->mode_pages.format_page;
3919 case SMS_RIGID_DISK_PAGE: {
3920 struct scsi_rigid_disk_page *rigid_disk_page;
3921 uint32_t sectors_per_cylinder;
3925 #endif /* !__XSCALE__ */
3927 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3928 panic("invalid subpage value %d",
3929 page_index->subpage);
3932 * Rotation rate and sectors per track are set
3933 * above. We calculate the cylinders here based on
3934 * capacity. Due to the number of heads and
3935 * sectors per track we're using, smaller arrays
3936 * may turn out to have 0 cylinders. Linux and
3937 * FreeBSD don't pay attention to these mode pages
3938 * to figure out capacity, but Solaris does. It
3939 * seems to deal with 0 cylinders just fine, and
3940 * works out a fake geometry based on the capacity.
3942 memcpy(&lun->mode_pages.rigid_disk_page[
3943 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3944 sizeof(rigid_disk_page_default));
3945 memcpy(&lun->mode_pages.rigid_disk_page[
3946 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3947 sizeof(rigid_disk_page_changeable));
3948 memcpy(&lun->mode_pages.rigid_disk_page[
3949 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3950 sizeof(rigid_disk_page_default));
3951 memcpy(&lun->mode_pages.rigid_disk_page[
3952 CTL_PAGE_SAVED], &rigid_disk_page_default,
3953 sizeof(rigid_disk_page_default));
3955 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3959 * The divide method here will be more accurate,
3960 * probably, but results in floating point being
3961 * used in the kernel on i386 (__udivdi3()). On the
3962 * XScale, though, __udivdi3() is implemented in
3965 * The shift method for cylinder calculation is
3966 * accurate if sectors_per_cylinder is a power of
3967 * 2. Otherwise it might be slightly off -- you
3968 * might have a bit of a truncation problem.
3971 cylinders = (lun->be_lun->maxlba + 1) /
3972 sectors_per_cylinder;
3974 for (shift = 31; shift > 0; shift--) {
3975 if (sectors_per_cylinder & (1 << shift))
3978 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3982 * We've basically got 3 bytes, or 24 bits for the
3983 * cylinder size in the mode page. If we're over,
3984 * just round down to 2^24.
3986 if (cylinders > 0xffffff)
3987 cylinders = 0xffffff;
3989 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3991 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3993 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3995 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3997 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3999 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4001 page_index->page_data =
4002 (uint8_t *)lun->mode_pages.rigid_disk_page;
4005 case SMS_CACHING_PAGE: {
4007 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4008 panic("invalid subpage value %d",
4009 page_index->subpage);
4011 * Defaults should be okay here, no calculations
4014 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4015 &caching_page_default,
4016 sizeof(caching_page_default));
4017 memcpy(&lun->mode_pages.caching_page[
4018 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4019 sizeof(caching_page_changeable));
4020 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4021 &caching_page_default,
4022 sizeof(caching_page_default));
4023 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4024 &caching_page_default,
4025 sizeof(caching_page_default));
4026 page_index->page_data =
4027 (uint8_t *)lun->mode_pages.caching_page;
4030 case SMS_CONTROL_MODE_PAGE: {
4032 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4033 panic("invalid subpage value %d",
4034 page_index->subpage);
4037 * Defaults should be okay here, no calculations
4040 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4041 &control_page_default,
4042 sizeof(control_page_default));
4043 memcpy(&lun->mode_pages.control_page[
4044 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4045 sizeof(control_page_changeable));
4046 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4047 &control_page_default,
4048 sizeof(control_page_default));
4049 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4050 &control_page_default,
4051 sizeof(control_page_default));
4052 page_index->page_data =
4053 (uint8_t *)lun->mode_pages.control_page;
4057 case SMS_VENDOR_SPECIFIC_PAGE:{
4058 switch (page_index->subpage) {
4059 case PWR_SUBPAGE_CODE: {
4060 struct copan_power_subpage *current_page,
4063 memcpy(&lun->mode_pages.power_subpage[
4065 &power_page_default,
4066 sizeof(power_page_default));
4067 memcpy(&lun->mode_pages.power_subpage[
4068 CTL_PAGE_CHANGEABLE],
4069 &power_page_changeable,
4070 sizeof(power_page_changeable));
4071 memcpy(&lun->mode_pages.power_subpage[
4073 &power_page_default,
4074 sizeof(power_page_default));
4075 memcpy(&lun->mode_pages.power_subpage[
4077 &power_page_default,
4078 sizeof(power_page_default));
4079 page_index->page_data =
4080 (uint8_t *)lun->mode_pages.power_subpage;
4082 current_page = (struct copan_power_subpage *)
4083 (page_index->page_data +
4084 (page_index->page_len *
4086 saved_page = (struct copan_power_subpage *)
4087 (page_index->page_data +
4088 (page_index->page_len *
4092 case APS_SUBPAGE_CODE: {
4093 struct copan_aps_subpage *current_page,
4096 // This gets set multiple times but
4097 // it should always be the same. It's
4098 // only done during init so who cares.
4099 index_to_aps_page = i;
4101 memcpy(&lun->mode_pages.aps_subpage[
4104 sizeof(aps_page_default));
4105 memcpy(&lun->mode_pages.aps_subpage[
4106 CTL_PAGE_CHANGEABLE],
4107 &aps_page_changeable,
4108 sizeof(aps_page_changeable));
4109 memcpy(&lun->mode_pages.aps_subpage[
4112 sizeof(aps_page_default));
4113 memcpy(&lun->mode_pages.aps_subpage[
4116 sizeof(aps_page_default));
4117 page_index->page_data =
4118 (uint8_t *)lun->mode_pages.aps_subpage;
4120 current_page = (struct copan_aps_subpage *)
4121 (page_index->page_data +
4122 (page_index->page_len *
4124 saved_page = (struct copan_aps_subpage *)
4125 (page_index->page_data +
4126 (page_index->page_len *
4130 case DBGCNF_SUBPAGE_CODE: {
4131 struct copan_debugconf_subpage *current_page,
4134 memcpy(&lun->mode_pages.debugconf_subpage[
4136 &debugconf_page_default,
4137 sizeof(debugconf_page_default));
4138 memcpy(&lun->mode_pages.debugconf_subpage[
4139 CTL_PAGE_CHANGEABLE],
4140 &debugconf_page_changeable,
4141 sizeof(debugconf_page_changeable));
4142 memcpy(&lun->mode_pages.debugconf_subpage[
4144 &debugconf_page_default,
4145 sizeof(debugconf_page_default));
4146 memcpy(&lun->mode_pages.debugconf_subpage[
4148 &debugconf_page_default,
4149 sizeof(debugconf_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.debugconf_subpage;
4153 current_page = (struct copan_debugconf_subpage *)
4154 (page_index->page_data +
4155 (page_index->page_len *
4157 saved_page = (struct copan_debugconf_subpage *)
4158 (page_index->page_data +
4159 (page_index->page_len *
4164 panic("invalid subpage value %d",
4165 page_index->subpage);
4171 panic("invalid page value %d",
4172 page_index->page_code & SMPH_PC_MASK);
4177 return (CTL_RETVAL_COMPLETE);
4184 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4185 * wants us to allocate the LUN and he can block.
4186 * - ctl_softc is always set
4187 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4189 * Returns 0 for success, non-zero (errno) for failure.
4192 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4193 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4195 struct ctl_lun *nlun, *lun;
4196 struct ctl_frontend *fe;
4197 int lun_number, i, lun_malloced;
4203 * We currently only support Direct Access or Processor LUN types.
4205 switch (be_lun->lun_type) {
4213 be_lun->lun_config_status(be_lun->be_lun,
4214 CTL_LUN_CONFIG_FAILURE);
4217 if (ctl_lun == NULL) {
4218 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4225 memset(lun, 0, sizeof(*lun));
4227 lun->flags = CTL_LUN_MALLOCED;
4229 mtx_lock(&ctl_softc->ctl_lock);
4231 * See if the caller requested a particular LUN number. If so, see
4232 * if it is available. Otherwise, allocate the first available LUN.
4234 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4235 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4236 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4237 mtx_unlock(&ctl_softc->ctl_lock);
4238 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4239 printf("ctl: requested LUN ID %d is higher "
4240 "than CTL_MAX_LUNS - 1 (%d)\n",
4241 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4244 * XXX KDM return an error, or just assign
4245 * another LUN ID in this case??
4247 printf("ctl: requested LUN ID %d is already "
4248 "in use\n", be_lun->req_lun_id);
4250 if (lun->flags & CTL_LUN_MALLOCED)
4252 be_lun->lun_config_status(be_lun->be_lun,
4253 CTL_LUN_CONFIG_FAILURE);
4256 lun_number = be_lun->req_lun_id;
4258 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4259 if (lun_number == -1) {
4260 mtx_unlock(&ctl_softc->ctl_lock);
4261 printf("ctl: can't allocate LUN on target %ju, out of "
4262 "LUNs\n", (uintmax_t)target_id.id);
4263 if (lun->flags & CTL_LUN_MALLOCED)
4265 be_lun->lun_config_status(be_lun->be_lun,
4266 CTL_LUN_CONFIG_FAILURE);
4270 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4272 lun->target = target_id;
4273 lun->lun = lun_number;
4274 lun->be_lun = be_lun;
4276 * The processor LUN is always enabled. Disk LUNs come on line
4277 * disabled, and must be enabled by the backend.
4279 lun->flags |= CTL_LUN_DISABLED;
4280 lun->backend = be_lun->be;
4281 be_lun->ctl_lun = lun;
4282 be_lun->lun_id = lun_number;
4283 atomic_add_int(&be_lun->be->num_luns, 1);
4284 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4285 lun->flags |= CTL_LUN_STOPPED;
4287 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4288 lun->flags |= CTL_LUN_INOPERABLE;
4290 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4291 lun->flags |= CTL_LUN_PRIMARY_SC;
4293 lun->ctl_softc = ctl_softc;
4294 TAILQ_INIT(&lun->ooa_queue);
4295 TAILQ_INIT(&lun->blocked_queue);
4296 STAILQ_INIT(&lun->error_list);
4299 * Initialize the mode page index.
4301 ctl_init_page_index(lun);
4304 * Set the poweron UA for all initiators on this LUN only.
4306 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4307 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4310 * Now, before we insert this lun on the lun list, set the lun
4311 * inventory changed UA for all other luns.
4313 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4314 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4315 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4319 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4321 ctl_softc->ctl_luns[lun_number] = lun;
4323 ctl_softc->num_luns++;
4325 /* Setup statistics gathering */
4326 lun->stats.device_type = be_lun->lun_type;
4327 lun->stats.lun_number = lun_number;
4328 if (lun->stats.device_type == T_DIRECT)
4329 lun->stats.blocksize = be_lun->blocksize;
4331 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4332 for (i = 0;i < CTL_MAX_PORTS;i++)
4333 lun->stats.ports[i].targ_port = i;
4335 mtx_unlock(&ctl_softc->ctl_lock);
4337 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4340 * Run through each registered FETD and bring it online if it isn't
4341 * already. Enable the target ID if it hasn't been enabled, and
4342 * enable this particular LUN.
4344 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4348 * XXX KDM this only works for ONE TARGET ID. We'll need
4349 * to do things differently if we go to a multiple target
4352 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4354 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4356 printf("ctl_alloc_lun: FETD %s port %d "
4357 "returned error %d for targ_enable on "
4358 "target %ju\n", fe->port_name,
4359 fe->targ_port, retval,
4360 (uintmax_t)target_id.id);
4362 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4365 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4367 printf("ctl_alloc_lun: FETD %s port %d returned error "
4368 "%d for lun_enable on target %ju lun %d\n",
4369 fe->port_name, fe->targ_port, retval,
4370 (uintmax_t)target_id.id, lun_number);
4372 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4380 * - LUN has already been marked invalid and any pending I/O has been taken
4384 ctl_free_lun(struct ctl_lun *lun)
4386 struct ctl_softc *softc;
4388 struct ctl_frontend *fe;
4390 struct ctl_lun *nlun;
4391 union ctl_io *io, *next_io;
4394 softc = lun->ctl_softc;
4396 mtx_assert(&softc->ctl_lock, MA_OWNED);
4398 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4400 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4402 softc->ctl_luns[lun->lun] = NULL;
4404 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4405 printf("ctl_free_lun: aieee!! freeing a LUN with "
4406 "outstanding I/O!!\n");
4410 * If we have anything pending on the RtR queue, remove it.
4412 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4414 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4415 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4416 && (io->io_hdr.nexus.targ_lun == lun->lun))
4417 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4422 * Then remove everything from the blocked queue.
4424 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4426 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4427 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4428 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4432 * Now clear out the OOA queue, and free all the I/O.
4433 * XXX KDM should we notify the FETD here? We probably need to
4434 * quiesce the LUN before deleting it.
4436 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4438 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4439 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4446 * XXX KDM this scheme only works for a single target/multiple LUN
4447 * setup. It needs to be revamped for a multiple target scheme.
4449 * XXX KDM this results in fe->lun_disable() getting called twice,
4450 * once when ctl_disable_lun() is called, and a second time here.
4451 * We really need to re-think the LUN disable semantics. There
4452 * should probably be several steps/levels to LUN removal:
4457 * Right now we only have a disable method when communicating to
4458 * the front end ports, at least for individual LUNs.
4461 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4464 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4467 printf("ctl_free_lun: FETD %s port %d returned error "
4468 "%d for lun_disable on target %ju lun %jd\n",
4469 fe->port_name, fe->targ_port, retval,
4470 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4473 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4474 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4476 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4478 printf("ctl_free_lun: FETD %s port %d "
4479 "returned error %d for targ_disable on "
4480 "target %ju\n", fe->port_name,
4481 fe->targ_port, retval,
4482 (uintmax_t)lun->target.id);
4484 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4486 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4490 fe->port_offline(fe->onoff_arg);
4491 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4498 * Tell the backend to free resources, if this LUN has a backend.
4500 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4501 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4503 if (lun->flags & CTL_LUN_MALLOCED)
4506 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4507 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4508 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4516 ctl_create_lun(struct ctl_be_lun *be_lun)
4518 struct ctl_softc *ctl_softc;
4520 ctl_softc = control_softc;
4523 * ctl_alloc_lun() should handle all potential failure cases.
4525 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4529 ctl_add_lun(struct ctl_be_lun *be_lun)
4531 struct ctl_softc *ctl_softc;
4533 ctl_softc = control_softc;
4535 mtx_lock(&ctl_softc->ctl_lock);
4536 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4537 mtx_unlock(&ctl_softc->ctl_lock);
4539 ctl_wakeup_thread();
4545 ctl_enable_lun(struct ctl_be_lun *be_lun)
4547 struct ctl_softc *ctl_softc;
4548 struct ctl_frontend *fe, *nfe;
4549 struct ctl_lun *lun;
4552 ctl_softc = control_softc;
4554 lun = (struct ctl_lun *)be_lun->ctl_lun;
4556 mtx_lock(&ctl_softc->ctl_lock);
4557 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4559 * eh? Why did we get called if the LUN is already
4562 mtx_unlock(&ctl_softc->ctl_lock);
4565 lun->flags &= ~CTL_LUN_DISABLED;
4567 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4568 nfe = STAILQ_NEXT(fe, links);
4571 * Drop the lock while we call the FETD's enable routine.
4572 * This can lead to a callback into CTL (at least in the
4573 * case of the internal initiator frontend.
4575 mtx_unlock(&ctl_softc->ctl_lock);
4576 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4577 mtx_lock(&ctl_softc->ctl_lock);
4579 printf("%s: FETD %s port %d returned error "
4580 "%d for lun_enable on target %ju lun %jd\n",
4581 __func__, fe->port_name, fe->targ_port, retval,
4582 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4586 /* NOTE: TODO: why does lun enable affect port status? */
4587 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4592 mtx_unlock(&ctl_softc->ctl_lock);
4598 ctl_disable_lun(struct ctl_be_lun *be_lun)
4600 struct ctl_softc *ctl_softc;
4601 struct ctl_frontend *fe;
4602 struct ctl_lun *lun;
4605 ctl_softc = control_softc;
4607 lun = (struct ctl_lun *)be_lun->ctl_lun;
4609 mtx_lock(&ctl_softc->ctl_lock);
4611 if (lun->flags & CTL_LUN_DISABLED) {
4612 mtx_unlock(&ctl_softc->ctl_lock);
4615 lun->flags |= CTL_LUN_DISABLED;
4617 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4618 mtx_unlock(&ctl_softc->ctl_lock);
4620 * Drop the lock before we call the frontend's disable
4621 * routine, to avoid lock order reversals.
4623 * XXX KDM what happens if the frontend list changes while
4624 * we're traversing it? It's unlikely, but should be handled.
4626 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4628 mtx_lock(&ctl_softc->ctl_lock);
4630 printf("ctl_alloc_lun: FETD %s port %d returned error "
4631 "%d for lun_disable on target %ju lun %jd\n",
4632 fe->port_name, fe->targ_port, retval,
4633 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4637 mtx_unlock(&ctl_softc->ctl_lock);
4643 ctl_start_lun(struct ctl_be_lun *be_lun)
4645 struct ctl_softc *ctl_softc;
4646 struct ctl_lun *lun;
4648 ctl_softc = control_softc;
4650 lun = (struct ctl_lun *)be_lun->ctl_lun;
4652 mtx_lock(&ctl_softc->ctl_lock);
4653 lun->flags &= ~CTL_LUN_STOPPED;
4654 mtx_unlock(&ctl_softc->ctl_lock);
4660 ctl_stop_lun(struct ctl_be_lun *be_lun)
4662 struct ctl_softc *ctl_softc;
4663 struct ctl_lun *lun;
4665 ctl_softc = control_softc;
4667 lun = (struct ctl_lun *)be_lun->ctl_lun;
4669 mtx_lock(&ctl_softc->ctl_lock);
4670 lun->flags |= CTL_LUN_STOPPED;
4671 mtx_unlock(&ctl_softc->ctl_lock);
4677 ctl_lun_offline(struct ctl_be_lun *be_lun)
4679 struct ctl_softc *ctl_softc;
4680 struct ctl_lun *lun;
4682 ctl_softc = control_softc;
4684 lun = (struct ctl_lun *)be_lun->ctl_lun;
4686 mtx_lock(&ctl_softc->ctl_lock);
4687 lun->flags |= CTL_LUN_OFFLINE;
4688 mtx_unlock(&ctl_softc->ctl_lock);
4694 ctl_lun_online(struct ctl_be_lun *be_lun)
4696 struct ctl_softc *ctl_softc;
4697 struct ctl_lun *lun;
4699 ctl_softc = control_softc;
4701 lun = (struct ctl_lun *)be_lun->ctl_lun;
4703 mtx_lock(&ctl_softc->ctl_lock);
4704 lun->flags &= ~CTL_LUN_OFFLINE;
4705 mtx_unlock(&ctl_softc->ctl_lock);
4711 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4713 struct ctl_softc *ctl_softc;
4714 struct ctl_lun *lun;
4716 ctl_softc = control_softc;
4718 lun = (struct ctl_lun *)be_lun->ctl_lun;
4720 mtx_lock(&ctl_softc->ctl_lock);
4723 * The LUN needs to be disabled before it can be marked invalid.
4725 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4726 mtx_unlock(&ctl_softc->ctl_lock);
4730 * Mark the LUN invalid.
4732 lun->flags |= CTL_LUN_INVALID;
4735 * If there is nothing in the OOA queue, go ahead and free the LUN.
4736 * If we have something in the OOA queue, we'll free it when the
4737 * last I/O completes.
4739 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4741 mtx_unlock(&ctl_softc->ctl_lock);
4747 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4749 struct ctl_softc *ctl_softc;
4750 struct ctl_lun *lun;
4752 ctl_softc = control_softc;
4753 lun = (struct ctl_lun *)be_lun->ctl_lun;
4755 mtx_lock(&ctl_softc->ctl_lock);
4756 lun->flags |= CTL_LUN_INOPERABLE;
4757 mtx_unlock(&ctl_softc->ctl_lock);
4763 ctl_lun_operable(struct ctl_be_lun *be_lun)
4765 struct ctl_softc *ctl_softc;
4766 struct ctl_lun *lun;
4768 ctl_softc = control_softc;
4769 lun = (struct ctl_lun *)be_lun->ctl_lun;
4771 mtx_lock(&ctl_softc->ctl_lock);
4772 lun->flags &= ~CTL_LUN_INOPERABLE;
4773 mtx_unlock(&ctl_softc->ctl_lock);
4779 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4782 struct ctl_softc *softc;
4783 struct ctl_lun *lun;
4784 struct copan_aps_subpage *current_sp;
4785 struct ctl_page_index *page_index;
4788 softc = control_softc;
4790 mtx_lock(&softc->ctl_lock);
4792 lun = (struct ctl_lun *)be_lun->ctl_lun;
4795 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4796 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4800 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4802 page_index = &lun->mode_pages.index[i];
4805 if (page_index == NULL) {
4806 mtx_unlock(&softc->ctl_lock);
4807 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4808 (uintmax_t)lun->lun);
4812 if ((softc->aps_locked_lun != 0)
4813 && (softc->aps_locked_lun != lun->lun)) {
4814 printf("%s: attempt to lock LUN %llu when %llu is already "
4816 mtx_unlock(&softc->ctl_lock);
4821 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4822 (page_index->page_len * CTL_PAGE_CURRENT));
4825 current_sp->lock_active = APS_LOCK_ACTIVE;
4826 softc->aps_locked_lun = lun->lun;
4828 current_sp->lock_active = 0;
4829 softc->aps_locked_lun = 0;
4834 * If we're in HA mode, try to send the lock message to the other
4837 if (ctl_is_single == 0) {
4839 union ctl_ha_msg lock_msg;
4841 lock_msg.hdr.nexus = *nexus;
4842 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4844 lock_msg.aps.lock_flag = 1;
4846 lock_msg.aps.lock_flag = 0;
4847 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4848 sizeof(lock_msg), 0);
4849 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4850 printf("%s: APS (lock=%d) error returned from "
4851 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4852 mtx_unlock(&softc->ctl_lock);
4857 mtx_unlock(&softc->ctl_lock);
4863 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4865 struct ctl_lun *lun;
4866 struct ctl_softc *softc;
4869 softc = control_softc;
4871 mtx_lock(&softc->ctl_lock);
4873 lun = (struct ctl_lun *)be_lun->ctl_lun;
4875 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4876 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4878 mtx_unlock(&softc->ctl_lock);
4882 * Backend "memory move is complete" callback for requests that never
4883 * make it down to say RAIDCore's configuration code.
4886 ctl_config_move_done(union ctl_io *io)
4890 retval = CTL_RETVAL_COMPLETE;
4893 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4895 * XXX KDM this shouldn't happen, but what if it does?
4897 if (io->io_hdr.io_type != CTL_IO_SCSI)
4898 panic("I/O type isn't CTL_IO_SCSI!");
4900 if ((io->io_hdr.port_status == 0)
4901 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4902 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4903 io->io_hdr.status = CTL_SUCCESS;
4904 else if ((io->io_hdr.port_status != 0)
4905 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4906 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4908 * For hardware error sense keys, the sense key
4909 * specific value is defined to be a retry count,
4910 * but we use it to pass back an internal FETD
4911 * error code. XXX KDM Hopefully the FETD is only
4912 * using 16 bits for an error code, since that's
4913 * all the space we have in the sks field.
4915 ctl_set_internal_failure(&io->scsiio,
4918 io->io_hdr.port_status);
4919 free(io->scsiio.kern_data_ptr, M_CTL);
4924 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4925 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4926 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4928 * XXX KDM just assuming a single pointer here, and not a
4929 * S/G list. If we start using S/G lists for config data,
4930 * we'll need to know how to clean them up here as well.
4932 free(io->scsiio.kern_data_ptr, M_CTL);
4933 /* Hopefully the user has already set the status... */
4937 * XXX KDM now we need to continue data movement. Some
4939 * - call ctl_scsiio() again? We don't do this for data
4940 * writes, because for those at least we know ahead of
4941 * time where the write will go and how long it is. For
4942 * config writes, though, that information is largely
4943 * contained within the write itself, thus we need to
4944 * parse out the data again.
4946 * - Call some other function once the data is in?
4950 * XXX KDM call ctl_scsiio() again for now, and check flag
4951 * bits to see whether we're allocated or not.
4953 retval = ctl_scsiio(&io->scsiio);
4960 * This gets called by a backend driver when it is done with a
4961 * configuration write.
4964 ctl_config_write_done(union ctl_io *io)
4967 * If the IO_CONT flag is set, we need to call the supplied
4968 * function to continue processing the I/O, instead of completing
4971 * If there is an error, though, we don't want to keep processing.
4972 * Instead, just send status back to the initiator.
4974 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
4975 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
4976 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
4977 io->scsiio.io_cont(io);
4981 * Since a configuration write can be done for commands that actually
4982 * have data allocated, like write buffer, and commands that have
4983 * no data, like start/stop unit, we need to check here.
4985 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4986 free(io->scsiio.kern_data_ptr, M_CTL);
4991 * SCSI release command.
4994 ctl_scsi_release(struct ctl_scsiio *ctsio)
4996 int length, longid, thirdparty_id, resv_id;
4997 struct ctl_softc *ctl_softc;
4998 struct ctl_lun *lun;
5003 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5005 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5006 ctl_softc = control_softc;
5008 switch (ctsio->cdb[0]) {
5010 struct scsi_release *cdb;
5012 cdb = (struct scsi_release *)ctsio->cdb;
5013 if ((cdb->byte2 & 0x1f) != 0) {
5014 ctl_set_invalid_field(ctsio,
5020 ctl_done((union ctl_io *)ctsio);
5021 return (CTL_RETVAL_COMPLETE);
5026 struct scsi_release_10 *cdb;
5028 cdb = (struct scsi_release_10 *)ctsio->cdb;
5030 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5031 ctl_set_invalid_field(ctsio,
5037 ctl_done((union ctl_io *)ctsio);
5038 return (CTL_RETVAL_COMPLETE);
5042 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5043 ctl_set_invalid_field(ctsio,
5049 ctl_done((union ctl_io *)ctsio);
5050 return (CTL_RETVAL_COMPLETE);
5053 if (cdb->byte2 & SR10_LONGID)
5056 thirdparty_id = cdb->thirdparty_id;
5058 resv_id = cdb->resv_id;
5059 length = scsi_2btoul(cdb->length);
5066 * XXX KDM right now, we only support LUN reservation. We don't
5067 * support 3rd party reservations, or extent reservations, which
5068 * might actually need the parameter list. If we've gotten this
5069 * far, we've got a LUN reservation. Anything else got kicked out
5070 * above. So, according to SPC, ignore the length.
5074 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5076 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5077 ctsio->kern_data_len = length;
5078 ctsio->kern_total_len = length;
5079 ctsio->kern_data_resid = 0;
5080 ctsio->kern_rel_offset = 0;
5081 ctsio->kern_sg_entries = 0;
5082 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5083 ctsio->be_move_done = ctl_config_move_done;
5084 ctl_datamove((union ctl_io *)ctsio);
5086 return (CTL_RETVAL_COMPLETE);
5090 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5092 mtx_lock(&ctl_softc->ctl_lock);
5095 * According to SPC, it is not an error for an intiator to attempt
5096 * to release a reservation on a LUN that isn't reserved, or that
5097 * is reserved by another initiator. The reservation can only be
5098 * released, though, by the initiator who made it or by one of
5099 * several reset type events.
5101 if (lun->flags & CTL_LUN_RESERVED) {
5102 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5103 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5104 && (ctsio->io_hdr.nexus.targ_target.id ==
5105 lun->rsv_nexus.targ_target.id)) {
5106 lun->flags &= ~CTL_LUN_RESERVED;
5110 ctsio->scsi_status = SCSI_STATUS_OK;
5111 ctsio->io_hdr.status = CTL_SUCCESS;
5113 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5114 free(ctsio->kern_data_ptr, M_CTL);
5115 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5118 mtx_unlock(&ctl_softc->ctl_lock);
5120 ctl_done((union ctl_io *)ctsio);
5121 return (CTL_RETVAL_COMPLETE);
5125 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5127 int extent, thirdparty, longid;
5128 int resv_id, length;
5129 uint64_t thirdparty_id;
5130 struct ctl_softc *ctl_softc;
5131 struct ctl_lun *lun;
5140 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5142 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5143 ctl_softc = control_softc;
5145 switch (ctsio->cdb[0]) {
5147 struct scsi_reserve *cdb;
5149 cdb = (struct scsi_reserve *)ctsio->cdb;
5150 if ((cdb->byte2 & 0x1f) != 0) {
5151 ctl_set_invalid_field(ctsio,
5157 ctl_done((union ctl_io *)ctsio);
5158 return (CTL_RETVAL_COMPLETE);
5160 resv_id = cdb->resv_id;
5161 length = scsi_2btoul(cdb->length);
5165 struct scsi_reserve_10 *cdb;
5167 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5169 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5170 ctl_set_invalid_field(ctsio,
5176 ctl_done((union ctl_io *)ctsio);
5177 return (CTL_RETVAL_COMPLETE);
5179 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5180 ctl_set_invalid_field(ctsio,
5186 ctl_done((union ctl_io *)ctsio);
5187 return (CTL_RETVAL_COMPLETE);
5189 if (cdb->byte2 & SR10_LONGID)
5192 thirdparty_id = cdb->thirdparty_id;
5194 resv_id = cdb->resv_id;
5195 length = scsi_2btoul(cdb->length);
5201 * XXX KDM right now, we only support LUN reservation. We don't
5202 * support 3rd party reservations, or extent reservations, which
5203 * might actually need the parameter list. If we've gotten this
5204 * far, we've got a LUN reservation. Anything else got kicked out
5205 * above. So, according to SPC, ignore the length.
5209 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5211 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5212 ctsio->kern_data_len = length;
5213 ctsio->kern_total_len = length;
5214 ctsio->kern_data_resid = 0;
5215 ctsio->kern_rel_offset = 0;
5216 ctsio->kern_sg_entries = 0;
5217 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5218 ctsio->be_move_done = ctl_config_move_done;
5219 ctl_datamove((union ctl_io *)ctsio);
5221 return (CTL_RETVAL_COMPLETE);
5225 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5227 mtx_lock(&ctl_softc->ctl_lock);
5228 if (lun->flags & CTL_LUN_RESERVED) {
5229 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5230 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5231 || (ctsio->io_hdr.nexus.targ_target.id !=
5232 lun->rsv_nexus.targ_target.id)) {
5233 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5234 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5239 lun->flags |= CTL_LUN_RESERVED;
5240 lun->rsv_nexus = ctsio->io_hdr.nexus;
5242 ctsio->scsi_status = SCSI_STATUS_OK;
5243 ctsio->io_hdr.status = CTL_SUCCESS;
5246 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5247 free(ctsio->kern_data_ptr, M_CTL);
5248 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5251 mtx_unlock(&ctl_softc->ctl_lock);
5253 ctl_done((union ctl_io *)ctsio);
5254 return (CTL_RETVAL_COMPLETE);
5258 ctl_start_stop(struct ctl_scsiio *ctsio)
5260 struct scsi_start_stop_unit *cdb;
5261 struct ctl_lun *lun;
5262 struct ctl_softc *ctl_softc;
5265 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5267 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5268 ctl_softc = control_softc;
5271 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5275 * We don't support the immediate bit on a stop unit. In order to
5276 * do that, we would need to code up a way to know that a stop is
5277 * pending, and hold off any new commands until it completes, one
5278 * way or another. Then we could accept or reject those commands
5279 * depending on its status. We would almost need to do the reverse
5280 * of what we do below for an immediate start -- return the copy of
5281 * the ctl_io to the FETD with status to send to the host (and to
5282 * free the copy!) and then free the original I/O once the stop
5283 * actually completes. That way, the OOA queue mechanism can work
5284 * to block commands that shouldn't proceed. Another alternative
5285 * would be to put the copy in the queue in place of the original,
5286 * and return the original back to the caller. That could be
5289 if ((cdb->byte2 & SSS_IMMED)
5290 && ((cdb->how & SSS_START) == 0)) {
5291 ctl_set_invalid_field(ctsio,
5297 ctl_done((union ctl_io *)ctsio);
5298 return (CTL_RETVAL_COMPLETE);
5302 * We don't support the power conditions field. We need to check
5303 * this prior to checking the load/eject and start/stop bits.
5305 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5306 ctl_set_invalid_field(ctsio,
5312 ctl_done((union ctl_io *)ctsio);
5313 return (CTL_RETVAL_COMPLETE);
5317 * Media isn't removable, so we can't load or eject it.
5319 if ((cdb->how & SSS_LOEJ) != 0) {
5320 ctl_set_invalid_field(ctsio,
5326 ctl_done((union ctl_io *)ctsio);
5327 return (CTL_RETVAL_COMPLETE);
5330 if ((lun->flags & CTL_LUN_PR_RESERVED)
5331 && ((cdb->how & SSS_START)==0)) {
5334 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5335 if (!lun->per_res[residx].registered
5336 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5338 ctl_set_reservation_conflict(ctsio);
5339 ctl_done((union ctl_io *)ctsio);
5340 return (CTL_RETVAL_COMPLETE);
5345 * If there is no backend on this device, we can't start or stop
5346 * it. In theory we shouldn't get any start/stop commands in the
5347 * first place at this level if the LUN doesn't have a backend.
5348 * That should get stopped by the command decode code.
5350 if (lun->backend == NULL) {
5351 ctl_set_invalid_opcode(ctsio);
5352 ctl_done((union ctl_io *)ctsio);
5353 return (CTL_RETVAL_COMPLETE);
5357 * XXX KDM Copan-specific offline behavior.
5358 * Figure out a reasonable way to port this?
5361 mtx_lock(&ctl_softc->ctl_lock);
5363 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5364 && (lun->flags & CTL_LUN_OFFLINE)) {
5366 * If the LUN is offline, and the on/offline bit isn't set,
5367 * reject the start or stop. Otherwise, let it through.
5369 mtx_unlock(&ctl_softc->ctl_lock);
5370 ctl_set_lun_not_ready(ctsio);
5371 ctl_done((union ctl_io *)ctsio);
5373 mtx_unlock(&ctl_softc->ctl_lock);
5374 #endif /* NEEDTOPORT */
5376 * This could be a start or a stop when we're online,
5377 * or a stop/offline or start/online. A start or stop when
5378 * we're offline is covered in the case above.
5381 * In the non-immediate case, we send the request to
5382 * the backend and return status to the user when
5385 * In the immediate case, we allocate a new ctl_io
5386 * to hold a copy of the request, and send that to
5387 * the backend. We then set good status on the
5388 * user's request and return it immediately.
5390 if (cdb->byte2 & SSS_IMMED) {
5391 union ctl_io *new_io;
5393 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5394 if (new_io == NULL) {
5395 ctl_set_busy(ctsio);
5396 ctl_done((union ctl_io *)ctsio);
5398 ctl_copy_io((union ctl_io *)ctsio,
5400 retval = lun->backend->config_write(new_io);
5401 ctl_set_success(ctsio);
5402 ctl_done((union ctl_io *)ctsio);
5405 retval = lun->backend->config_write(
5406 (union ctl_io *)ctsio);
5415 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5416 * we don't really do anything with the LBA and length fields if the user
5417 * passes them in. Instead we'll just flush out the cache for the entire
5421 ctl_sync_cache(struct ctl_scsiio *ctsio)
5423 struct ctl_lun *lun;
5424 struct ctl_softc *ctl_softc;
5425 uint64_t starting_lba;
5426 uint32_t block_count;
5430 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5432 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5433 ctl_softc = control_softc;
5438 switch (ctsio->cdb[0]) {
5439 case SYNCHRONIZE_CACHE: {
5440 struct scsi_sync_cache *cdb;
5441 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5443 if (cdb->byte2 & SSC_RELADR)
5446 if (cdb->byte2 & SSC_IMMED)
5449 starting_lba = scsi_4btoul(cdb->begin_lba);
5450 block_count = scsi_2btoul(cdb->lb_count);
5453 case SYNCHRONIZE_CACHE_16: {
5454 struct scsi_sync_cache_16 *cdb;
5455 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5457 if (cdb->byte2 & SSC_RELADR)
5460 if (cdb->byte2 & SSC_IMMED)
5463 starting_lba = scsi_8btou64(cdb->begin_lba);
5464 block_count = scsi_4btoul(cdb->lb_count);
5468 ctl_set_invalid_opcode(ctsio);
5469 ctl_done((union ctl_io *)ctsio);
5471 break; /* NOTREACHED */
5476 * We don't support the immediate bit. Since it's in the
5477 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5478 * commands, we can just return the same error in either
5481 ctl_set_invalid_field(ctsio,
5487 ctl_done((union ctl_io *)ctsio);
5493 * We don't support the reladr bit either. It can only be
5494 * used with linked commands, and we don't support linked
5495 * commands. Since the bit is in the same place for the
5496 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5497 * just return the same error in either case.
5499 ctl_set_invalid_field(ctsio,
5505 ctl_done((union ctl_io *)ctsio);
5510 * We check the LBA and length, but don't do anything with them.
5511 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5512 * get flushed. This check will just help satisfy anyone who wants
5513 * to see an error for an out of range LBA.
5515 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5516 ctl_set_lba_out_of_range(ctsio);
5517 ctl_done((union ctl_io *)ctsio);
5522 * If this LUN has no backend, we can't flush the cache anyway.
5524 if (lun->backend == NULL) {
5525 ctl_set_invalid_opcode(ctsio);
5526 ctl_done((union ctl_io *)ctsio);
5531 * Check to see whether we're configured to send the SYNCHRONIZE
5532 * CACHE command directly to the back end.
5534 mtx_lock(&ctl_softc->ctl_lock);
5535 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5536 && (++(lun->sync_count) >= lun->sync_interval)) {
5537 lun->sync_count = 0;
5538 mtx_unlock(&ctl_softc->ctl_lock);
5539 retval = lun->backend->config_write((union ctl_io *)ctsio);
5541 mtx_unlock(&ctl_softc->ctl_lock);
5542 ctl_set_success(ctsio);
5543 ctl_done((union ctl_io *)ctsio);
5552 ctl_format(struct ctl_scsiio *ctsio)
5554 struct scsi_format *cdb;
5555 struct ctl_lun *lun;
5556 struct ctl_softc *ctl_softc;
5557 int length, defect_list_len;
5559 CTL_DEBUG_PRINT(("ctl_format\n"));
5561 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5562 ctl_softc = control_softc;
5564 cdb = (struct scsi_format *)ctsio->cdb;
5567 if (cdb->byte2 & SF_FMTDATA) {
5568 if (cdb->byte2 & SF_LONGLIST)
5569 length = sizeof(struct scsi_format_header_long);
5571 length = sizeof(struct scsi_format_header_short);
5574 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5576 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5577 ctsio->kern_data_len = length;
5578 ctsio->kern_total_len = length;
5579 ctsio->kern_data_resid = 0;
5580 ctsio->kern_rel_offset = 0;
5581 ctsio->kern_sg_entries = 0;
5582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5583 ctsio->be_move_done = ctl_config_move_done;
5584 ctl_datamove((union ctl_io *)ctsio);
5586 return (CTL_RETVAL_COMPLETE);
5589 defect_list_len = 0;
5591 if (cdb->byte2 & SF_FMTDATA) {
5592 if (cdb->byte2 & SF_LONGLIST) {
5593 struct scsi_format_header_long *header;
5595 header = (struct scsi_format_header_long *)
5596 ctsio->kern_data_ptr;
5598 defect_list_len = scsi_4btoul(header->defect_list_len);
5599 if (defect_list_len != 0) {
5600 ctl_set_invalid_field(ctsio,
5609 struct scsi_format_header_short *header;
5611 header = (struct scsi_format_header_short *)
5612 ctsio->kern_data_ptr;
5614 defect_list_len = scsi_2btoul(header->defect_list_len);
5615 if (defect_list_len != 0) {
5616 ctl_set_invalid_field(ctsio,
5628 * The format command will clear out the "Medium format corrupted"
5629 * status if set by the configuration code. That status is really
5630 * just a way to notify the host that we have lost the media, and
5631 * get them to issue a command that will basically make them think
5632 * they're blowing away the media.
5634 mtx_lock(&ctl_softc->ctl_lock);
5635 lun->flags &= ~CTL_LUN_INOPERABLE;
5636 mtx_unlock(&ctl_softc->ctl_lock);
5638 ctsio->scsi_status = SCSI_STATUS_OK;
5639 ctsio->io_hdr.status = CTL_SUCCESS;
5642 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5643 free(ctsio->kern_data_ptr, M_CTL);
5644 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5647 ctl_done((union ctl_io *)ctsio);
5648 return (CTL_RETVAL_COMPLETE);
5652 ctl_write_buffer(struct ctl_scsiio *ctsio)
5654 struct scsi_write_buffer *cdb;
5655 struct copan_page_header *header;
5656 struct ctl_lun *lun;
5657 struct ctl_softc *ctl_softc;
5658 int buffer_offset, len;
5663 retval = CTL_RETVAL_COMPLETE;
5665 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5667 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5668 ctl_softc = control_softc;
5669 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5671 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5672 ctl_set_invalid_field(ctsio,
5678 ctl_done((union ctl_io *)ctsio);
5679 return (CTL_RETVAL_COMPLETE);
5681 if (cdb->buffer_id != 0) {
5682 ctl_set_invalid_field(ctsio,
5688 ctl_done((union ctl_io *)ctsio);
5689 return (CTL_RETVAL_COMPLETE);
5692 len = scsi_3btoul(cdb->length);
5693 buffer_offset = scsi_3btoul(cdb->offset);
5695 if (len > sizeof(lun->write_buffer)) {
5696 ctl_set_invalid_field(ctsio,
5702 ctl_done((union ctl_io *)ctsio);
5703 return (CTL_RETVAL_COMPLETE);
5706 if (buffer_offset != 0) {
5707 ctl_set_invalid_field(ctsio,
5713 ctl_done((union ctl_io *)ctsio);
5714 return (CTL_RETVAL_COMPLETE);
5718 * If we've got a kernel request that hasn't been malloced yet,
5719 * malloc it and tell the caller the data buffer is here.
5721 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5722 ctsio->kern_data_ptr = lun->write_buffer;
5723 ctsio->kern_data_len = len;
5724 ctsio->kern_total_len = len;
5725 ctsio->kern_data_resid = 0;
5726 ctsio->kern_rel_offset = 0;
5727 ctsio->kern_sg_entries = 0;
5728 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5729 ctsio->be_move_done = ctl_config_move_done;
5730 ctl_datamove((union ctl_io *)ctsio);
5732 return (CTL_RETVAL_COMPLETE);
5735 ctl_done((union ctl_io *)ctsio);
5737 return (CTL_RETVAL_COMPLETE);
5741 * Note that this function currently doesn't actually do anything inside
5742 * CTL to enforce things if the DQue bit is turned on.
5744 * Also note that this function can't be used in the default case, because
5745 * the DQue bit isn't set in the changeable mask for the control mode page
5746 * anyway. This is just here as an example for how to implement a page
5747 * handler, and a placeholder in case we want to allow the user to turn
5748 * tagged queueing on and off.
5750 * The D_SENSE bit handling is functional, however, and will turn
5751 * descriptor sense on and off for a given LUN.
5754 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5755 struct ctl_page_index *page_index, uint8_t *page_ptr)
5757 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5758 struct ctl_lun *lun;
5759 struct ctl_softc *softc;
5763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5764 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5767 user_cp = (struct scsi_control_page *)page_ptr;
5768 current_cp = (struct scsi_control_page *)
5769 (page_index->page_data + (page_index->page_len *
5771 saved_cp = (struct scsi_control_page *)
5772 (page_index->page_data + (page_index->page_len *
5775 softc = control_softc;
5777 mtx_lock(&softc->ctl_lock);
5778 if (((current_cp->rlec & SCP_DSENSE) == 0)
5779 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5781 * Descriptor sense is currently turned off and the user
5782 * wants to turn it on.
5784 current_cp->rlec |= SCP_DSENSE;
5785 saved_cp->rlec |= SCP_DSENSE;
5786 lun->flags |= CTL_LUN_SENSE_DESC;
5788 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5789 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5791 * Descriptor sense is currently turned on, and the user
5792 * wants to turn it off.
5794 current_cp->rlec &= ~SCP_DSENSE;
5795 saved_cp->rlec &= ~SCP_DSENSE;
5796 lun->flags &= ~CTL_LUN_SENSE_DESC;
5799 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5800 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5802 csevent_log(CSC_CTL | CSC_SHELF_SW |
5804 csevent_LogType_Trace,
5805 csevent_Severity_Information,
5806 csevent_AlertLevel_Green,
5807 csevent_FRU_Firmware,
5808 csevent_FRU_Unknown,
5809 "Received untagged to untagged transition");
5810 #endif /* NEEDTOPORT */
5813 csevent_log(CSC_CTL | CSC_SHELF_SW |
5815 csevent_LogType_ConfigChange,
5816 csevent_Severity_Information,
5817 csevent_AlertLevel_Green,
5818 csevent_FRU_Firmware,
5819 csevent_FRU_Unknown,
5820 "Received untagged to tagged "
5821 "queueing transition");
5822 #endif /* NEEDTOPORT */
5824 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5825 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5829 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5831 csevent_log(CSC_CTL | CSC_SHELF_SW |
5833 csevent_LogType_ConfigChange,
5834 csevent_Severity_Warning,
5835 csevent_AlertLevel_Yellow,
5836 csevent_FRU_Firmware,
5837 csevent_FRU_Unknown,
5838 "Received tagged queueing to untagged "
5840 #endif /* NEEDTOPORT */
5842 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5843 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5847 csevent_log(CSC_CTL | CSC_SHELF_SW |
5849 csevent_LogType_Trace,
5850 csevent_Severity_Information,
5851 csevent_AlertLevel_Green,
5852 csevent_FRU_Firmware,
5853 csevent_FRU_Unknown,
5854 "Received tagged queueing to tagged "
5855 "queueing transition");
5856 #endif /* NEEDTOPORT */
5862 * Let other initiators know that the mode
5863 * parameters for this LUN have changed.
5865 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5869 lun->pending_sense[i].ua_pending |=
5873 mtx_unlock(&softc->ctl_lock);
5879 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5880 struct ctl_page_index *page_index, uint8_t *page_ptr)
5886 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5887 struct ctl_page_index *page_index, int pc)
5889 struct copan_power_subpage *page;
5891 page = (struct copan_power_subpage *)page_index->page_data +
5892 (page_index->page_len * pc);
5895 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5897 * We don't update the changable bits for this page.
5900 case SMS_PAGE_CTRL_CURRENT >> 6:
5901 case SMS_PAGE_CTRL_DEFAULT >> 6:
5902 case SMS_PAGE_CTRL_SAVED >> 6:
5904 ctl_update_power_subpage(page);
5909 EPRINT(0, "Invalid PC %d!!", pc);
5918 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5919 struct ctl_page_index *page_index, uint8_t *page_ptr)
5921 struct copan_aps_subpage *user_sp;
5922 struct copan_aps_subpage *current_sp;
5923 union ctl_modepage_info *modepage_info;
5924 struct ctl_softc *softc;
5925 struct ctl_lun *lun;
5928 retval = CTL_RETVAL_COMPLETE;
5929 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5930 (page_index->page_len * CTL_PAGE_CURRENT));
5931 softc = control_softc;
5932 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5934 user_sp = (struct copan_aps_subpage *)page_ptr;
5936 modepage_info = (union ctl_modepage_info *)
5937 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5939 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5940 modepage_info->header.subpage = page_index->subpage;
5941 modepage_info->aps.lock_active = user_sp->lock_active;
5943 mtx_lock(&softc->ctl_lock);
5946 * If there is a request to lock the LUN and another LUN is locked
5947 * this is an error. If the requested LUN is already locked ignore
5948 * the request. If no LUN is locked attempt to lock it.
5949 * if there is a request to unlock the LUN and the LUN is currently
5950 * locked attempt to unlock it. Otherwise ignore the request. i.e.
5951 * if another LUN is locked or no LUN is locked.
5953 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
5954 if (softc->aps_locked_lun == lun->lun) {
5956 * This LUN is already locked, so we're done.
5958 retval = CTL_RETVAL_COMPLETE;
5959 } else if (softc->aps_locked_lun == 0) {
5961 * No one has the lock, pass the request to the
5964 retval = lun->backend->config_write(
5965 (union ctl_io *)ctsio);
5968 * Someone else has the lock, throw out the request.
5970 ctl_set_already_locked(ctsio);
5971 free(ctsio->kern_data_ptr, M_CTL);
5972 ctl_done((union ctl_io *)ctsio);
5975 * Set the return value so that ctl_do_mode_select()
5976 * won't try to complete the command. We already
5977 * completed it here.
5979 retval = CTL_RETVAL_ERROR;
5981 } else if (softc->aps_locked_lun == lun->lun) {
5983 * This LUN is locked, so pass the unlock request to the
5986 retval = lun->backend->config_write((union ctl_io *)ctsio);
5988 mtx_unlock(&softc->ctl_lock);
5994 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5995 struct ctl_page_index *page_index,
6001 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6006 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6007 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6008 printf("page data:");
6010 printf(" %.2x",page_ptr[i]);
6016 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6017 struct ctl_page_index *page_index,
6020 struct copan_debugconf_subpage *page;
6022 page = (struct copan_debugconf_subpage *)page_index->page_data +
6023 (page_index->page_len * pc);
6026 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6027 case SMS_PAGE_CTRL_DEFAULT >> 6:
6028 case SMS_PAGE_CTRL_SAVED >> 6:
6030 * We don't update the changable or default bits for this page.
6033 case SMS_PAGE_CTRL_CURRENT >> 6:
6034 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6035 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6039 EPRINT(0, "Invalid PC %d!!", pc);
6040 #endif /* NEEDTOPORT */
6048 ctl_do_mode_select(union ctl_io *io)
6050 struct scsi_mode_page_header *page_header;
6051 struct ctl_page_index *page_index;
6052 struct ctl_scsiio *ctsio;
6053 int control_dev, page_len;
6054 int page_len_offset, page_len_size;
6055 union ctl_modepage_info *modepage_info;
6056 struct ctl_lun *lun;
6057 int *len_left, *len_used;
6060 ctsio = &io->scsiio;
6063 retval = CTL_RETVAL_COMPLETE;
6065 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6067 if (lun->be_lun->lun_type != T_DIRECT)
6072 modepage_info = (union ctl_modepage_info *)
6073 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6074 len_left = &modepage_info->header.len_left;
6075 len_used = &modepage_info->header.len_used;
6079 page_header = (struct scsi_mode_page_header *)
6080 (ctsio->kern_data_ptr + *len_used);
6082 if (*len_left == 0) {
6083 free(ctsio->kern_data_ptr, M_CTL);
6084 ctl_set_success(ctsio);
6085 ctl_done((union ctl_io *)ctsio);
6086 return (CTL_RETVAL_COMPLETE);
6087 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6089 free(ctsio->kern_data_ptr, M_CTL);
6090 ctl_set_param_len_error(ctsio);
6091 ctl_done((union ctl_io *)ctsio);
6092 return (CTL_RETVAL_COMPLETE);
6094 } else if ((page_header->page_code & SMPH_SPF)
6095 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6097 free(ctsio->kern_data_ptr, M_CTL);
6098 ctl_set_param_len_error(ctsio);
6099 ctl_done((union ctl_io *)ctsio);
6100 return (CTL_RETVAL_COMPLETE);
6105 * XXX KDM should we do something with the block descriptor?
6107 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6109 if ((control_dev != 0)
6110 && (lun->mode_pages.index[i].page_flags &
6111 CTL_PAGE_FLAG_DISK_ONLY))
6114 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6115 (page_header->page_code & SMPH_PC_MASK))
6119 * If neither page has a subpage code, then we've got a
6122 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6123 && ((page_header->page_code & SMPH_SPF) == 0)) {
6124 page_index = &lun->mode_pages.index[i];
6125 page_len = page_header->page_length;
6130 * If both pages have subpages, then the subpage numbers
6133 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6134 && (page_header->page_code & SMPH_SPF)) {
6135 struct scsi_mode_page_header_sp *sph;
6137 sph = (struct scsi_mode_page_header_sp *)page_header;
6139 if (lun->mode_pages.index[i].subpage ==
6141 page_index = &lun->mode_pages.index[i];
6142 page_len = scsi_2btoul(sph->page_length);
6149 * If we couldn't find the page, or if we don't have a mode select
6150 * handler for it, send back an error to the user.
6152 if ((page_index == NULL)
6153 || (page_index->select_handler == NULL)) {
6154 ctl_set_invalid_field(ctsio,
6157 /*field*/ *len_used,
6160 free(ctsio->kern_data_ptr, M_CTL);
6161 ctl_done((union ctl_io *)ctsio);
6162 return (CTL_RETVAL_COMPLETE);
6165 if (page_index->page_code & SMPH_SPF) {
6166 page_len_offset = 2;
6170 page_len_offset = 1;
6174 * If the length the initiator gives us isn't the one we specify in
6175 * the mode page header, or if they didn't specify enough data in
6176 * the CDB to avoid truncating this page, kick out the request.
6178 if ((page_len != (page_index->page_len - page_len_offset -
6180 || (*len_left < page_index->page_len)) {
6183 ctl_set_invalid_field(ctsio,
6186 /*field*/ *len_used + page_len_offset,
6189 free(ctsio->kern_data_ptr, M_CTL);
6190 ctl_done((union ctl_io *)ctsio);
6191 return (CTL_RETVAL_COMPLETE);
6195 * Run through the mode page, checking to make sure that the bits
6196 * the user changed are actually legal for him to change.
6198 for (i = 0; i < page_index->page_len; i++) {
6199 uint8_t *user_byte, *change_mask, *current_byte;
6203 user_byte = (uint8_t *)page_header + i;
6204 change_mask = page_index->page_data +
6205 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6206 current_byte = page_index->page_data +
6207 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6210 * Check to see whether the user set any bits in this byte
6211 * that he is not allowed to set.
6213 if ((*user_byte & ~(*change_mask)) ==
6214 (*current_byte & ~(*change_mask)))
6218 * Go through bit by bit to determine which one is illegal.
6221 for (j = 7; j >= 0; j--) {
6222 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6223 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6228 ctl_set_invalid_field(ctsio,
6231 /*field*/ *len_used + i,
6234 free(ctsio->kern_data_ptr, M_CTL);
6235 ctl_done((union ctl_io *)ctsio);
6236 return (CTL_RETVAL_COMPLETE);
6240 * Decrement these before we call the page handler, since we may
6241 * end up getting called back one way or another before the handler
6242 * returns to this context.
6244 *len_left -= page_index->page_len;
6245 *len_used += page_index->page_len;
6247 retval = page_index->select_handler(ctsio, page_index,
6248 (uint8_t *)page_header);
6251 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6252 * wait until this queued command completes to finish processing
6253 * the mode page. If it returns anything other than
6254 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6255 * already set the sense information, freed the data pointer, and
6256 * completed the io for us.
6258 if (retval != CTL_RETVAL_COMPLETE)
6259 goto bailout_no_done;
6262 * If the initiator sent us more than one page, parse the next one.
6267 ctl_set_success(ctsio);
6268 free(ctsio->kern_data_ptr, M_CTL);
6269 ctl_done((union ctl_io *)ctsio);
6273 return (CTL_RETVAL_COMPLETE);
6278 ctl_mode_select(struct ctl_scsiio *ctsio)
6280 int param_len, pf, sp;
6281 int header_size, bd_len;
6282 int len_left, len_used;
6283 struct ctl_page_index *page_index;
6284 struct ctl_lun *lun;
6285 int control_dev, page_len;
6286 union ctl_modepage_info *modepage_info;
6298 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6300 if (lun->be_lun->lun_type != T_DIRECT)
6305 switch (ctsio->cdb[0]) {
6306 case MODE_SELECT_6: {
6307 struct scsi_mode_select_6 *cdb;
6309 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6311 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6312 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6314 param_len = cdb->length;
6315 header_size = sizeof(struct scsi_mode_header_6);
6318 case MODE_SELECT_10: {
6319 struct scsi_mode_select_10 *cdb;
6321 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6323 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6324 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6326 param_len = scsi_2btoul(cdb->length);
6327 header_size = sizeof(struct scsi_mode_header_10);
6331 ctl_set_invalid_opcode(ctsio);
6332 ctl_done((union ctl_io *)ctsio);
6333 return (CTL_RETVAL_COMPLETE);
6334 break; /* NOTREACHED */
6339 * "A parameter list length of zero indicates that the Data-Out Buffer
6340 * shall be empty. This condition shall not be considered as an error."
6342 if (param_len == 0) {
6343 ctl_set_success(ctsio);
6344 ctl_done((union ctl_io *)ctsio);
6345 return (CTL_RETVAL_COMPLETE);
6349 * Since we'll hit this the first time through, prior to
6350 * allocation, we don't need to free a data buffer here.
6352 if (param_len < header_size) {
6353 ctl_set_param_len_error(ctsio);
6354 ctl_done((union ctl_io *)ctsio);
6355 return (CTL_RETVAL_COMPLETE);
6359 * Allocate the data buffer and grab the user's data. In theory,
6360 * we shouldn't have to sanity check the parameter list length here
6361 * because the maximum size is 64K. We should be able to malloc
6362 * that much without too many problems.
6364 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6365 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6366 ctsio->kern_data_len = param_len;
6367 ctsio->kern_total_len = param_len;
6368 ctsio->kern_data_resid = 0;
6369 ctsio->kern_rel_offset = 0;
6370 ctsio->kern_sg_entries = 0;
6371 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6372 ctsio->be_move_done = ctl_config_move_done;
6373 ctl_datamove((union ctl_io *)ctsio);
6375 return (CTL_RETVAL_COMPLETE);
6378 switch (ctsio->cdb[0]) {
6379 case MODE_SELECT_6: {
6380 struct scsi_mode_header_6 *mh6;
6382 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6383 bd_len = mh6->blk_desc_len;
6386 case MODE_SELECT_10: {
6387 struct scsi_mode_header_10 *mh10;
6389 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6390 bd_len = scsi_2btoul(mh10->blk_desc_len);
6394 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6398 if (param_len < (header_size + bd_len)) {
6399 free(ctsio->kern_data_ptr, M_CTL);
6400 ctl_set_param_len_error(ctsio);
6401 ctl_done((union ctl_io *)ctsio);
6402 return (CTL_RETVAL_COMPLETE);
6406 * Set the IO_CONT flag, so that if this I/O gets passed to
6407 * ctl_config_write_done(), it'll get passed back to
6408 * ctl_do_mode_select() for further processing, or completion if
6411 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6412 ctsio->io_cont = ctl_do_mode_select;
6414 modepage_info = (union ctl_modepage_info *)
6415 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6417 memset(modepage_info, 0, sizeof(*modepage_info));
6419 len_left = param_len - header_size - bd_len;
6420 len_used = header_size + bd_len;
6422 modepage_info->header.len_left = len_left;
6423 modepage_info->header.len_used = len_used;
6425 return (ctl_do_mode_select((union ctl_io *)ctsio));
6429 ctl_mode_sense(struct ctl_scsiio *ctsio)
6431 struct ctl_lun *lun;
6432 int pc, page_code, dbd, llba, subpage;
6433 int alloc_len, page_len, header_len, total_len;
6434 struct scsi_mode_block_descr *block_desc;
6435 struct ctl_page_index *page_index;
6443 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6445 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6447 if (lun->be_lun->lun_type != T_DIRECT)
6452 switch (ctsio->cdb[0]) {
6453 case MODE_SENSE_6: {
6454 struct scsi_mode_sense_6 *cdb;
6456 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6458 header_len = sizeof(struct scsi_mode_hdr_6);
6459 if (cdb->byte2 & SMS_DBD)
6462 header_len += sizeof(struct scsi_mode_block_descr);
6464 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6465 page_code = cdb->page & SMS_PAGE_CODE;
6466 subpage = cdb->subpage;
6467 alloc_len = cdb->length;
6470 case MODE_SENSE_10: {
6471 struct scsi_mode_sense_10 *cdb;
6473 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6475 header_len = sizeof(struct scsi_mode_hdr_10);
6477 if (cdb->byte2 & SMS_DBD)
6480 header_len += sizeof(struct scsi_mode_block_descr);
6481 if (cdb->byte2 & SMS10_LLBAA)
6483 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6484 page_code = cdb->page & SMS_PAGE_CODE;
6485 subpage = cdb->subpage;
6486 alloc_len = scsi_2btoul(cdb->length);
6490 ctl_set_invalid_opcode(ctsio);
6491 ctl_done((union ctl_io *)ctsio);
6492 return (CTL_RETVAL_COMPLETE);
6493 break; /* NOTREACHED */
6497 * We have to make a first pass through to calculate the size of
6498 * the pages that match the user's query. Then we allocate enough
6499 * memory to hold it, and actually copy the data into the buffer.
6501 switch (page_code) {
6502 case SMS_ALL_PAGES_PAGE: {
6508 * At the moment, values other than 0 and 0xff here are
6509 * reserved according to SPC-3.
6511 if ((subpage != SMS_SUBPAGE_PAGE_0)
6512 && (subpage != SMS_SUBPAGE_ALL)) {
6513 ctl_set_invalid_field(ctsio,
6519 ctl_done((union ctl_io *)ctsio);
6520 return (CTL_RETVAL_COMPLETE);
6523 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6524 if ((control_dev != 0)
6525 && (lun->mode_pages.index[i].page_flags &
6526 CTL_PAGE_FLAG_DISK_ONLY))
6530 * We don't use this subpage if the user didn't
6531 * request all subpages.
6533 if ((lun->mode_pages.index[i].subpage != 0)
6534 && (subpage == SMS_SUBPAGE_PAGE_0))
6538 printf("found page %#x len %d\n",
6539 lun->mode_pages.index[i].page_code &
6541 lun->mode_pages.index[i].page_len);
6543 page_len += lun->mode_pages.index[i].page_len;
6552 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6553 /* Look for the right page code */
6554 if ((lun->mode_pages.index[i].page_code &
6555 SMPH_PC_MASK) != page_code)
6558 /* Look for the right subpage or the subpage wildcard*/
6559 if ((lun->mode_pages.index[i].subpage != subpage)
6560 && (subpage != SMS_SUBPAGE_ALL))
6563 /* Make sure the page is supported for this dev type */
6564 if ((control_dev != 0)
6565 && (lun->mode_pages.index[i].page_flags &
6566 CTL_PAGE_FLAG_DISK_ONLY))
6570 printf("found page %#x len %d\n",
6571 lun->mode_pages.index[i].page_code &
6573 lun->mode_pages.index[i].page_len);
6576 page_len += lun->mode_pages.index[i].page_len;
6579 if (page_len == 0) {
6580 ctl_set_invalid_field(ctsio,
6586 ctl_done((union ctl_io *)ctsio);
6587 return (CTL_RETVAL_COMPLETE);
6593 total_len = header_len + page_len;
6595 printf("header_len = %d, page_len = %d, total_len = %d\n",
6596 header_len, page_len, total_len);
6599 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6600 ctsio->kern_sg_entries = 0;
6601 ctsio->kern_data_resid = 0;
6602 ctsio->kern_rel_offset = 0;
6603 if (total_len < alloc_len) {
6604 ctsio->residual = alloc_len - total_len;
6605 ctsio->kern_data_len = total_len;
6606 ctsio->kern_total_len = total_len;
6608 ctsio->residual = 0;
6609 ctsio->kern_data_len = alloc_len;
6610 ctsio->kern_total_len = alloc_len;
6613 switch (ctsio->cdb[0]) {
6614 case MODE_SENSE_6: {
6615 struct scsi_mode_hdr_6 *header;
6617 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6619 header->datalen = ctl_min(total_len - 1, 254);
6622 header->block_descr_len = 0;
6624 header->block_descr_len =
6625 sizeof(struct scsi_mode_block_descr);
6626 block_desc = (struct scsi_mode_block_descr *)&header[1];
6629 case MODE_SENSE_10: {
6630 struct scsi_mode_hdr_10 *header;
6633 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6635 datalen = ctl_min(total_len - 2, 65533);
6636 scsi_ulto2b(datalen, header->datalen);
6638 scsi_ulto2b(0, header->block_descr_len);
6640 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6641 header->block_descr_len);
6642 block_desc = (struct scsi_mode_block_descr *)&header[1];
6646 panic("invalid CDB type %#x", ctsio->cdb[0]);
6647 break; /* NOTREACHED */
6651 * If we've got a disk, use its blocksize in the block
6652 * descriptor. Otherwise, just set it to 0.
6655 if (control_dev != 0)
6656 scsi_ulto3b(lun->be_lun->blocksize,
6657 block_desc->block_len);
6659 scsi_ulto3b(0, block_desc->block_len);
6662 switch (page_code) {
6663 case SMS_ALL_PAGES_PAGE: {
6666 data_used = header_len;
6667 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6668 struct ctl_page_index *page_index;
6670 page_index = &lun->mode_pages.index[i];
6672 if ((control_dev != 0)
6673 && (page_index->page_flags &
6674 CTL_PAGE_FLAG_DISK_ONLY))
6678 * We don't use this subpage if the user didn't
6679 * request all subpages. We already checked (above)
6680 * to make sure the user only specified a subpage
6681 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6683 if ((page_index->subpage != 0)
6684 && (subpage == SMS_SUBPAGE_PAGE_0))
6688 * Call the handler, if it exists, to update the
6689 * page to the latest values.
6691 if (page_index->sense_handler != NULL)
6692 page_index->sense_handler(ctsio, page_index,pc);
6694 memcpy(ctsio->kern_data_ptr + data_used,
6695 page_index->page_data +
6696 (page_index->page_len * pc),
6697 page_index->page_len);
6698 data_used += page_index->page_len;
6705 data_used = header_len;
6707 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6708 struct ctl_page_index *page_index;
6710 page_index = &lun->mode_pages.index[i];
6712 /* Look for the right page code */
6713 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6716 /* Look for the right subpage or the subpage wildcard*/
6717 if ((page_index->subpage != subpage)
6718 && (subpage != SMS_SUBPAGE_ALL))
6721 /* Make sure the page is supported for this dev type */
6722 if ((control_dev != 0)
6723 && (page_index->page_flags &
6724 CTL_PAGE_FLAG_DISK_ONLY))
6728 * Call the handler, if it exists, to update the
6729 * page to the latest values.
6731 if (page_index->sense_handler != NULL)
6732 page_index->sense_handler(ctsio, page_index,pc);
6734 memcpy(ctsio->kern_data_ptr + data_used,
6735 page_index->page_data +
6736 (page_index->page_len * pc),
6737 page_index->page_len);
6738 data_used += page_index->page_len;
6744 ctsio->scsi_status = SCSI_STATUS_OK;
6746 ctsio->be_move_done = ctl_config_move_done;
6747 ctl_datamove((union ctl_io *)ctsio);
6749 return (CTL_RETVAL_COMPLETE);
6753 ctl_read_capacity(struct ctl_scsiio *ctsio)
6755 struct scsi_read_capacity *cdb;
6756 struct scsi_read_capacity_data *data;
6757 struct ctl_lun *lun;
6760 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6762 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6764 lba = scsi_4btoul(cdb->addr);
6765 if (((cdb->pmi & SRC_PMI) == 0)
6767 ctl_set_invalid_field(/*ctsio*/ ctsio,
6773 ctl_done((union ctl_io *)ctsio);
6774 return (CTL_RETVAL_COMPLETE);
6777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6779 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6780 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6781 ctsio->residual = 0;
6782 ctsio->kern_data_len = sizeof(*data);
6783 ctsio->kern_total_len = sizeof(*data);
6784 ctsio->kern_data_resid = 0;
6785 ctsio->kern_rel_offset = 0;
6786 ctsio->kern_sg_entries = 0;
6789 * If the maximum LBA is greater than 0xfffffffe, the user must
6790 * issue a SERVICE ACTION IN (16) command, with the read capacity
6791 * serivce action set.
6793 if (lun->be_lun->maxlba > 0xfffffffe)
6794 scsi_ulto4b(0xffffffff, data->addr);
6796 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6799 * XXX KDM this may not be 512 bytes...
6801 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6803 ctsio->scsi_status = SCSI_STATUS_OK;
6805 ctsio->be_move_done = ctl_config_move_done;
6806 ctl_datamove((union ctl_io *)ctsio);
6808 return (CTL_RETVAL_COMPLETE);
6812 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6814 struct scsi_read_capacity_16 *cdb;
6815 struct scsi_read_capacity_data_long *data;
6816 struct ctl_lun *lun;
6820 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6822 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6824 alloc_len = scsi_4btoul(cdb->alloc_len);
6825 lba = scsi_8btou64(cdb->addr);
6827 if ((cdb->reladr & SRC16_PMI)
6829 ctl_set_invalid_field(/*ctsio*/ ctsio,
6835 ctl_done((union ctl_io *)ctsio);
6836 return (CTL_RETVAL_COMPLETE);
6839 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6841 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6842 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6844 if (sizeof(*data) < alloc_len) {
6845 ctsio->residual = alloc_len - sizeof(*data);
6846 ctsio->kern_data_len = sizeof(*data);
6847 ctsio->kern_total_len = sizeof(*data);
6849 ctsio->residual = 0;
6850 ctsio->kern_data_len = alloc_len;
6851 ctsio->kern_total_len = alloc_len;
6853 ctsio->kern_data_resid = 0;
6854 ctsio->kern_rel_offset = 0;
6855 ctsio->kern_sg_entries = 0;
6857 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6858 /* XXX KDM this may not be 512 bytes... */
6859 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6860 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6861 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6863 ctsio->scsi_status = SCSI_STATUS_OK;
6865 ctsio->be_move_done = ctl_config_move_done;
6866 ctl_datamove((union ctl_io *)ctsio);
6868 return (CTL_RETVAL_COMPLETE);
6872 ctl_service_action_in(struct ctl_scsiio *ctsio)
6874 struct scsi_service_action_in *cdb;
6877 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
6879 cdb = (struct scsi_service_action_in *)ctsio->cdb;
6881 retval = CTL_RETVAL_COMPLETE;
6883 switch (cdb->service_action) {
6884 case SRC16_SERVICE_ACTION:
6885 retval = ctl_read_capacity_16(ctsio);
6888 ctl_set_invalid_field(/*ctsio*/ ctsio,
6894 ctl_done((union ctl_io *)ctsio);
6902 ctl_maintenance_in(struct ctl_scsiio *ctsio)
6904 struct scsi_maintenance_in *cdb;
6906 int alloc_len, total_len = 0;
6907 int num_target_port_groups, single;
6908 struct ctl_lun *lun;
6909 struct ctl_softc *softc;
6910 struct scsi_target_group_data *rtg_ptr;
6911 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6912 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6913 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6915 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
6917 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6918 softc = control_softc;
6919 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6921 retval = CTL_RETVAL_COMPLETE;
6923 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
6924 ctl_set_invalid_field(/*ctsio*/ ctsio,
6930 ctl_done((union ctl_io *)ctsio);
6934 mtx_lock(&softc->ctl_lock);
6935 single = ctl_is_single;
6936 mtx_unlock(&softc->ctl_lock);
6939 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6941 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
6943 total_len = sizeof(struct scsi_target_group_data) +
6944 sizeof(struct scsi_target_port_group_descriptor) *
6945 num_target_port_groups +
6946 sizeof(struct scsi_target_port_descriptor) *
6947 NUM_PORTS_PER_GRP * num_target_port_groups;
6949 alloc_len = scsi_4btoul(cdb->length);
6951 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6953 ctsio->kern_sg_entries = 0;
6955 if (total_len < alloc_len) {
6956 ctsio->residual = alloc_len - total_len;
6957 ctsio->kern_data_len = total_len;
6958 ctsio->kern_total_len = total_len;
6960 ctsio->residual = 0;
6961 ctsio->kern_data_len = alloc_len;
6962 ctsio->kern_total_len = alloc_len;
6964 ctsio->kern_data_resid = 0;
6965 ctsio->kern_rel_offset = 0;
6967 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
6969 tpg_desc_ptr1 = &rtg_ptr->groups[0];
6970 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
6971 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
6972 &tp_desc_ptr1_1->desc_list[0];
6975 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
6976 &tp_desc_ptr1_2->desc_list[0];
6977 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
6978 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
6979 &tp_desc_ptr2_1->desc_list[0];
6981 tpg_desc_ptr2 = NULL;
6982 tp_desc_ptr2_1 = NULL;
6983 tp_desc_ptr2_2 = NULL;
6986 scsi_ulto4b(total_len - 4, rtg_ptr->length);
6988 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
6989 if (lun->flags & CTL_LUN_PRIMARY_SC) {
6990 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
6991 tpg_desc_ptr2->pref_state =
6992 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6994 tpg_desc_ptr1->pref_state =
6995 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6996 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
6999 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7000 tpg_desc_ptr1->pref_state =
7001 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7002 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7004 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7005 tpg_desc_ptr2->pref_state =
7006 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7010 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7012 tpg_desc_ptr1->support = 0;
7013 tpg_desc_ptr1->target_port_group[1] = 1;
7014 tpg_desc_ptr1->status = TPG_IMPLICIT;
7015 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7018 tpg_desc_ptr2->support = 0;
7019 tpg_desc_ptr2->target_port_group[1] = 2;
7020 tpg_desc_ptr2->status = TPG_IMPLICIT;
7021 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7023 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7024 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7026 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7027 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7029 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7030 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7031 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7033 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7034 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7038 ctsio->be_move_done = ctl_config_move_done;
7040 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7041 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7042 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7043 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7044 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7046 ctl_datamove((union ctl_io *)ctsio);
7051 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7053 struct scsi_per_res_in *cdb;
7054 int alloc_len, total_len = 0;
7055 /* struct scsi_per_res_in_rsrv in_data; */
7056 struct ctl_lun *lun;
7057 struct ctl_softc *softc;
7059 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7061 softc = control_softc;
7063 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7065 alloc_len = scsi_2btoul(cdb->length);
7067 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7070 mtx_lock(&softc->ctl_lock);
7071 switch (cdb->action) {
7072 case SPRI_RK: /* read keys */
7073 total_len = sizeof(struct scsi_per_res_in_keys) +
7075 sizeof(struct scsi_per_res_key);
7077 case SPRI_RR: /* read reservation */
7078 if (lun->flags & CTL_LUN_PR_RESERVED)
7079 total_len = sizeof(struct scsi_per_res_in_rsrv);
7081 total_len = sizeof(struct scsi_per_res_in_header);
7083 case SPRI_RC: /* report capabilities */
7084 total_len = sizeof(struct scsi_per_res_cap);
7086 case SPRI_RS: /* read full status */
7088 mtx_unlock(&softc->ctl_lock);
7089 ctl_set_invalid_field(ctsio,
7095 ctl_done((union ctl_io *)ctsio);
7096 return (CTL_RETVAL_COMPLETE);
7097 break; /* NOTREACHED */
7099 mtx_unlock(&softc->ctl_lock);
7101 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7103 if (total_len < alloc_len) {
7104 ctsio->residual = alloc_len - total_len;
7105 ctsio->kern_data_len = total_len;
7106 ctsio->kern_total_len = total_len;
7108 ctsio->residual = 0;
7109 ctsio->kern_data_len = alloc_len;
7110 ctsio->kern_total_len = alloc_len;
7113 ctsio->kern_data_resid = 0;
7114 ctsio->kern_rel_offset = 0;
7115 ctsio->kern_sg_entries = 0;
7117 mtx_lock(&softc->ctl_lock);
7118 switch (cdb->action) {
7119 case SPRI_RK: { // read keys
7120 struct scsi_per_res_in_keys *res_keys;
7123 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7126 * We had to drop the lock to allocate our buffer, which
7127 * leaves time for someone to come in with another
7128 * persistent reservation. (That is unlikely, though,
7129 * since this should be the only persistent reservation
7130 * command active right now.)
7132 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7133 (lun->pr_key_count *
7134 sizeof(struct scsi_per_res_key)))){
7135 mtx_unlock(&softc->ctl_lock);
7136 free(ctsio->kern_data_ptr, M_CTL);
7137 printf("%s: reservation length changed, retrying\n",
7142 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7144 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7145 lun->pr_key_count, res_keys->header.length);
7147 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7148 if (!lun->per_res[i].registered)
7152 * We used lun->pr_key_count to calculate the
7153 * size to allocate. If it turns out the number of
7154 * initiators with the registered flag set is
7155 * larger than that (i.e. they haven't been kept in
7156 * sync), we've got a problem.
7158 if (key_count >= lun->pr_key_count) {
7160 csevent_log(CSC_CTL | CSC_SHELF_SW |
7162 csevent_LogType_Fault,
7163 csevent_AlertLevel_Yellow,
7164 csevent_FRU_ShelfController,
7165 csevent_FRU_Firmware,
7166 csevent_FRU_Unknown,
7167 "registered keys %d >= key "
7168 "count %d", key_count,
7174 memcpy(res_keys->keys[key_count].key,
7175 lun->per_res[i].res_key.key,
7176 ctl_min(sizeof(res_keys->keys[key_count].key),
7177 sizeof(lun->per_res[i].res_key)));
7182 case SPRI_RR: { // read reservation
7183 struct scsi_per_res_in_rsrv *res;
7184 int tmp_len, header_only;
7186 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7188 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7190 if (lun->flags & CTL_LUN_PR_RESERVED)
7192 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7193 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7194 res->header.length);
7197 tmp_len = sizeof(struct scsi_per_res_in_header);
7198 scsi_ulto4b(0, res->header.length);
7203 * We had to drop the lock to allocate our buffer, which
7204 * leaves time for someone to come in with another
7205 * persistent reservation. (That is unlikely, though,
7206 * since this should be the only persistent reservation
7207 * command active right now.)
7209 if (tmp_len != total_len) {
7210 mtx_unlock(&softc->ctl_lock);
7211 free(ctsio->kern_data_ptr, M_CTL);
7212 printf("%s: reservation status changed, retrying\n",
7218 * No reservation held, so we're done.
7220 if (header_only != 0)
7224 * If the registration is an All Registrants type, the key
7225 * is 0, since it doesn't really matter.
7227 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7228 memcpy(res->data.reservation,
7229 &lun->per_res[lun->pr_res_idx].res_key,
7230 sizeof(struct scsi_per_res_key));
7232 res->data.scopetype = lun->res_type;
7235 case SPRI_RC: //report capabilities
7237 struct scsi_per_res_cap *res_cap;
7240 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7241 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7242 res_cap->flags2 |= SPRI_TMV;
7243 type_mask = SPRI_TM_WR_EX_AR |
7249 scsi_ulto2b(type_mask, res_cap->type_mask);
7252 case SPRI_RS: //read full status
7255 * This is a bug, because we just checked for this above,
7256 * and should have returned an error.
7258 panic("Invalid PR type %x", cdb->action);
7259 break; /* NOTREACHED */
7261 mtx_unlock(&softc->ctl_lock);
7263 ctsio->be_move_done = ctl_config_move_done;
7265 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7266 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7267 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7268 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7269 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7271 ctl_datamove((union ctl_io *)ctsio);
7273 return (CTL_RETVAL_COMPLETE);
7277 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7281 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7282 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7283 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7284 struct scsi_per_res_out_parms* param)
7286 union ctl_ha_msg persis_io;
7292 if (sa_res_key == 0) {
7293 mtx_lock(&softc->ctl_lock);
7294 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7295 /* validate scope and type */
7296 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7298 mtx_unlock(&softc->ctl_lock);
7299 ctl_set_invalid_field(/*ctsio*/ ctsio,
7305 ctl_done((union ctl_io *)ctsio);
7309 if (type>8 || type==2 || type==4 || type==0) {
7310 mtx_unlock(&softc->ctl_lock);
7311 ctl_set_invalid_field(/*ctsio*/ ctsio,
7317 ctl_done((union ctl_io *)ctsio);
7321 /* temporarily unregister this nexus */
7322 lun->per_res[residx].registered = 0;
7325 * Unregister everybody else and build UA for
7328 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7329 if (lun->per_res[i].registered == 0)
7333 && i <CTL_MAX_INITIATORS)
7334 lun->pending_sense[i].ua_pending |=
7336 else if (persis_offset
7337 && i >= persis_offset)
7338 lun->pending_sense[i-persis_offset
7341 lun->per_res[i].registered = 0;
7342 memset(&lun->per_res[i].res_key, 0,
7343 sizeof(struct scsi_per_res_key));
7345 lun->per_res[residx].registered = 1;
7346 lun->pr_key_count = 1;
7347 lun->res_type = type;
7348 if (lun->res_type != SPR_TYPE_WR_EX_AR
7349 && lun->res_type != SPR_TYPE_EX_AC_AR)
7350 lun->pr_res_idx = residx;
7352 mtx_unlock(&softc->ctl_lock);
7353 /* send msg to other side */
7354 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7355 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7356 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7357 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7358 persis_io.pr.pr_info.res_type = type;
7359 memcpy(persis_io.pr.pr_info.sa_res_key,
7360 param->serv_act_res_key,
7361 sizeof(param->serv_act_res_key));
7362 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7363 &persis_io, sizeof(persis_io), 0)) >
7364 CTL_HA_STATUS_SUCCESS) {
7365 printf("CTL:Persis Out error returned "
7366 "from ctl_ha_msg_send %d\n",
7370 /* not all registrants */
7371 mtx_unlock(&softc->ctl_lock);
7372 free(ctsio->kern_data_ptr, M_CTL);
7373 ctl_set_invalid_field(ctsio,
7379 ctl_done((union ctl_io *)ctsio);
7382 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7383 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7386 mtx_lock(&softc->ctl_lock);
7387 if (res_key == sa_res_key) {
7390 * The spec implies this is not good but doesn't
7391 * say what to do. There are two choices either
7392 * generate a res conflict or check condition
7393 * with illegal field in parameter data. Since
7394 * that is what is done when the sa_res_key is
7395 * zero I'll take that approach since this has
7396 * to do with the sa_res_key.
7398 mtx_unlock(&softc->ctl_lock);
7399 free(ctsio->kern_data_ptr, M_CTL);
7400 ctl_set_invalid_field(ctsio,
7406 ctl_done((union ctl_io *)ctsio);
7410 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7411 if (lun->per_res[i].registered
7412 && memcmp(param->serv_act_res_key,
7413 lun->per_res[i].res_key.key,
7414 sizeof(struct scsi_per_res_key)) != 0)
7418 lun->per_res[i].registered = 0;
7419 memset(&lun->per_res[i].res_key, 0,
7420 sizeof(struct scsi_per_res_key));
7421 lun->pr_key_count--;
7424 && i < CTL_MAX_INITIATORS)
7425 lun->pending_sense[i].ua_pending |=
7427 else if (persis_offset
7428 && i >= persis_offset)
7429 lun->pending_sense[i-persis_offset].ua_pending|=
7432 mtx_unlock(&softc->ctl_lock);
7434 free(ctsio->kern_data_ptr, M_CTL);
7435 ctl_set_reservation_conflict(ctsio);
7436 ctl_done((union ctl_io *)ctsio);
7437 return (CTL_RETVAL_COMPLETE);
7439 /* send msg to other side */
7440 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7441 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7442 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7443 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7444 persis_io.pr.pr_info.res_type = type;
7445 memcpy(persis_io.pr.pr_info.sa_res_key,
7446 param->serv_act_res_key,
7447 sizeof(param->serv_act_res_key));
7448 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7449 &persis_io, sizeof(persis_io), 0)) >
7450 CTL_HA_STATUS_SUCCESS) {
7451 printf("CTL:Persis Out error returned from "
7452 "ctl_ha_msg_send %d\n", isc_retval);
7455 /* Reserved but not all registrants */
7456 /* sa_res_key is res holder */
7457 if (memcmp(param->serv_act_res_key,
7458 lun->per_res[lun->pr_res_idx].res_key.key,
7459 sizeof(struct scsi_per_res_key)) == 0) {
7460 /* validate scope and type */
7461 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7463 ctl_set_invalid_field(/*ctsio*/ ctsio,
7469 ctl_done((union ctl_io *)ctsio);
7473 if (type>8 || type==2 || type==4 || type==0) {
7474 ctl_set_invalid_field(/*ctsio*/ ctsio,
7480 ctl_done((union ctl_io *)ctsio);
7486 * if sa_res_key != res_key remove all
7487 * registrants w/sa_res_key and generate UA
7488 * for these registrants(Registrations
7489 * Preempted) if it wasn't an exclusive
7490 * reservation generate UA(Reservations
7491 * Preempted) for all other registered nexuses
7492 * if the type has changed. Establish the new
7493 * reservation and holder. If res_key and
7494 * sa_res_key are the same do the above
7495 * except don't unregister the res holder.
7499 * Temporarily unregister so it won't get
7500 * removed or UA generated
7502 lun->per_res[residx].registered = 0;
7503 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7504 if (lun->per_res[i].registered == 0)
7507 if (memcmp(param->serv_act_res_key,
7508 lun->per_res[i].res_key.key,
7509 sizeof(struct scsi_per_res_key)) == 0) {
7510 lun->per_res[i].registered = 0;
7511 memset(&lun->per_res[i].res_key,
7513 sizeof(struct scsi_per_res_key));
7514 lun->pr_key_count--;
7517 && i < CTL_MAX_INITIATORS)
7518 lun->pending_sense[i
7521 else if (persis_offset
7522 && i >= persis_offset)
7524 i-persis_offset].ua_pending |=
7526 } else if (type != lun->res_type
7527 && (lun->res_type == SPR_TYPE_WR_EX_RO
7528 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7530 && i < CTL_MAX_INITIATORS)
7531 lun->pending_sense[i
7534 else if (persis_offset
7535 && i >= persis_offset)
7542 lun->per_res[residx].registered = 1;
7543 lun->res_type = type;
7544 if (lun->res_type != SPR_TYPE_WR_EX_AR
7545 && lun->res_type != SPR_TYPE_EX_AC_AR)
7546 lun->pr_res_idx = residx;
7549 CTL_PR_ALL_REGISTRANTS;
7551 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7552 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7553 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7554 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7555 persis_io.pr.pr_info.res_type = type;
7556 memcpy(persis_io.pr.pr_info.sa_res_key,
7557 param->serv_act_res_key,
7558 sizeof(param->serv_act_res_key));
7559 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7560 &persis_io, sizeof(persis_io), 0)) >
7561 CTL_HA_STATUS_SUCCESS) {
7562 printf("CTL:Persis Out error returned "
7563 "from ctl_ha_msg_send %d\n",
7568 * sa_res_key is not the res holder just
7569 * remove registrants
7572 mtx_lock(&softc->ctl_lock);
7574 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7575 if (memcmp(param->serv_act_res_key,
7576 lun->per_res[i].res_key.key,
7577 sizeof(struct scsi_per_res_key)) != 0)
7581 lun->per_res[i].registered = 0;
7582 memset(&lun->per_res[i].res_key, 0,
7583 sizeof(struct scsi_per_res_key));
7584 lun->pr_key_count--;
7587 && i < CTL_MAX_INITIATORS)
7588 lun->pending_sense[i].ua_pending |=
7590 else if (persis_offset
7591 && i >= persis_offset)
7593 i-persis_offset].ua_pending |=
7598 mtx_unlock(&softc->ctl_lock);
7599 free(ctsio->kern_data_ptr, M_CTL);
7600 ctl_set_reservation_conflict(ctsio);
7601 ctl_done((union ctl_io *)ctsio);
7604 mtx_unlock(&softc->ctl_lock);
7605 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7606 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7607 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7608 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7609 persis_io.pr.pr_info.res_type = type;
7610 memcpy(persis_io.pr.pr_info.sa_res_key,
7611 param->serv_act_res_key,
7612 sizeof(param->serv_act_res_key));
7613 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7614 &persis_io, sizeof(persis_io), 0)) >
7615 CTL_HA_STATUS_SUCCESS) {
7616 printf("CTL:Persis Out error returned "
7617 "from ctl_ha_msg_send %d\n",
7623 lun->PRGeneration++;
7629 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7633 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7634 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7635 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7636 msg->pr.pr_info.sa_res_key,
7637 sizeof(struct scsi_per_res_key)) != 0) {
7638 uint64_t sa_res_key;
7639 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7641 if (sa_res_key == 0) {
7642 /* temporarily unregister this nexus */
7643 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7646 * Unregister everybody else and build UA for
7649 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7650 if (lun->per_res[i].registered == 0)
7654 && i < CTL_MAX_INITIATORS)
7655 lun->pending_sense[i].ua_pending |=
7657 else if (persis_offset && i >= persis_offset)
7658 lun->pending_sense[i -
7659 persis_offset].ua_pending |=
7661 lun->per_res[i].registered = 0;
7662 memset(&lun->per_res[i].res_key, 0,
7663 sizeof(struct scsi_per_res_key));
7666 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7667 lun->pr_key_count = 1;
7668 lun->res_type = msg->pr.pr_info.res_type;
7669 if (lun->res_type != SPR_TYPE_WR_EX_AR
7670 && lun->res_type != SPR_TYPE_EX_AC_AR)
7671 lun->pr_res_idx = msg->pr.pr_info.residx;
7673 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7674 if (memcmp(msg->pr.pr_info.sa_res_key,
7675 lun->per_res[i].res_key.key,
7676 sizeof(struct scsi_per_res_key)) != 0)
7679 lun->per_res[i].registered = 0;
7680 memset(&lun->per_res[i].res_key, 0,
7681 sizeof(struct scsi_per_res_key));
7682 lun->pr_key_count--;
7685 && i < persis_offset)
7686 lun->pending_sense[i].ua_pending |=
7688 else if (persis_offset
7689 && i >= persis_offset)
7690 lun->pending_sense[i -
7691 persis_offset].ua_pending |=
7697 * Temporarily unregister so it won't get removed
7700 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7701 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7702 if (lun->per_res[i].registered == 0)
7705 if (memcmp(msg->pr.pr_info.sa_res_key,
7706 lun->per_res[i].res_key.key,
7707 sizeof(struct scsi_per_res_key)) == 0) {
7708 lun->per_res[i].registered = 0;
7709 memset(&lun->per_res[i].res_key, 0,
7710 sizeof(struct scsi_per_res_key));
7711 lun->pr_key_count--;
7713 && i < CTL_MAX_INITIATORS)
7714 lun->pending_sense[i].ua_pending |=
7716 else if (persis_offset
7717 && i >= persis_offset)
7718 lun->pending_sense[i -
7719 persis_offset].ua_pending |=
7721 } else if (msg->pr.pr_info.res_type != lun->res_type
7722 && (lun->res_type == SPR_TYPE_WR_EX_RO
7723 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7725 && i < persis_offset)
7726 lun->pending_sense[i
7729 else if (persis_offset
7730 && i >= persis_offset)
7731 lun->pending_sense[i -
7732 persis_offset].ua_pending |=
7736 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7737 lun->res_type = msg->pr.pr_info.res_type;
7738 if (lun->res_type != SPR_TYPE_WR_EX_AR
7739 && lun->res_type != SPR_TYPE_EX_AC_AR)
7740 lun->pr_res_idx = msg->pr.pr_info.residx;
7742 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7744 lun->PRGeneration++;
7750 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7754 u_int32_t param_len;
7755 struct scsi_per_res_out *cdb;
7756 struct ctl_lun *lun;
7757 struct scsi_per_res_out_parms* param;
7758 struct ctl_softc *softc;
7760 uint64_t res_key, sa_res_key;
7762 union ctl_ha_msg persis_io;
7765 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7767 retval = CTL_RETVAL_COMPLETE;
7769 softc = control_softc;
7771 cdb = (struct scsi_per_res_out *)ctsio->cdb;
7772 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7775 * We only support whole-LUN scope. The scope & type are ignored for
7776 * register, register and ignore existing key and clear.
7777 * We sometimes ignore scope and type on preempts too!!
7778 * Verify reservation type here as well.
7780 type = cdb->scope_type & SPR_TYPE_MASK;
7781 if ((cdb->action == SPRO_RESERVE)
7782 || (cdb->action == SPRO_RELEASE)) {
7783 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
7784 ctl_set_invalid_field(/*ctsio*/ ctsio,
7790 ctl_done((union ctl_io *)ctsio);
7791 return (CTL_RETVAL_COMPLETE);
7794 if (type>8 || type==2 || type==4 || type==0) {
7795 ctl_set_invalid_field(/*ctsio*/ ctsio,
7801 ctl_done((union ctl_io *)ctsio);
7802 return (CTL_RETVAL_COMPLETE);
7806 switch (cdb->action & SPRO_ACTION_MASK) {
7817 ctl_set_invalid_field(/*ctsio*/ ctsio,
7823 ctl_done((union ctl_io *)ctsio);
7824 return (CTL_RETVAL_COMPLETE);
7825 break; /* NOTREACHED */
7828 param_len = scsi_4btoul(cdb->length);
7830 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
7831 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
7832 ctsio->kern_data_len = param_len;
7833 ctsio->kern_total_len = param_len;
7834 ctsio->kern_data_resid = 0;
7835 ctsio->kern_rel_offset = 0;
7836 ctsio->kern_sg_entries = 0;
7837 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7838 ctsio->be_move_done = ctl_config_move_done;
7839 ctl_datamove((union ctl_io *)ctsio);
7841 return (CTL_RETVAL_COMPLETE);
7844 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
7846 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7847 res_key = scsi_8btou64(param->res_key.key);
7848 sa_res_key = scsi_8btou64(param->serv_act_res_key);
7851 * Validate the reservation key here except for SPRO_REG_IGNO
7852 * This must be done for all other service actions
7854 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
7855 mtx_lock(&softc->ctl_lock);
7856 if (lun->per_res[residx].registered) {
7857 if (memcmp(param->res_key.key,
7858 lun->per_res[residx].res_key.key,
7859 ctl_min(sizeof(param->res_key),
7860 sizeof(lun->per_res[residx].res_key))) != 0) {
7862 * The current key passed in doesn't match
7863 * the one the initiator previously
7866 mtx_unlock(&softc->ctl_lock);
7867 free(ctsio->kern_data_ptr, M_CTL);
7868 ctl_set_reservation_conflict(ctsio);
7869 ctl_done((union ctl_io *)ctsio);
7870 return (CTL_RETVAL_COMPLETE);
7872 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
7874 * We are not registered
7876 mtx_unlock(&softc->ctl_lock);
7877 free(ctsio->kern_data_ptr, M_CTL);
7878 ctl_set_reservation_conflict(ctsio);
7879 ctl_done((union ctl_io *)ctsio);
7880 return (CTL_RETVAL_COMPLETE);
7881 } else if (res_key != 0) {
7883 * We are not registered and trying to register but
7884 * the register key isn't zero.
7886 mtx_unlock(&softc->ctl_lock);
7887 free(ctsio->kern_data_ptr, M_CTL);
7888 ctl_set_reservation_conflict(ctsio);
7889 ctl_done((union ctl_io *)ctsio);
7890 return (CTL_RETVAL_COMPLETE);
7892 mtx_unlock(&softc->ctl_lock);
7895 switch (cdb->action & SPRO_ACTION_MASK) {
7897 case SPRO_REG_IGNO: {
7900 printf("Registration received\n");
7904 * We don't support any of these options, as we report in
7905 * the read capabilities request (see
7906 * ctl_persistent_reserve_in(), above).
7908 if ((param->flags & SPR_SPEC_I_PT)
7909 || (param->flags & SPR_ALL_TG_PT)
7910 || (param->flags & SPR_APTPL)) {
7913 if (param->flags & SPR_APTPL)
7915 else if (param->flags & SPR_ALL_TG_PT)
7917 else /* SPR_SPEC_I_PT */
7920 free(ctsio->kern_data_ptr, M_CTL);
7921 ctl_set_invalid_field(ctsio,
7927 ctl_done((union ctl_io *)ctsio);
7928 return (CTL_RETVAL_COMPLETE);
7931 mtx_lock(&softc->ctl_lock);
7934 * The initiator wants to clear the
7937 if (sa_res_key == 0) {
7939 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
7940 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
7941 && !lun->per_res[residx].registered)) {
7942 mtx_unlock(&softc->ctl_lock);
7946 lun->per_res[residx].registered = 0;
7947 memset(&lun->per_res[residx].res_key,
7948 0, sizeof(lun->per_res[residx].res_key));
7949 lun->pr_key_count--;
7951 if (residx == lun->pr_res_idx) {
7952 lun->flags &= ~CTL_LUN_PR_RESERVED;
7953 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7955 if ((lun->res_type == SPR_TYPE_WR_EX_RO
7956 || lun->res_type == SPR_TYPE_EX_AC_RO)
7957 && lun->pr_key_count) {
7959 * If the reservation is a registrants
7960 * only type we need to generate a UA
7961 * for other registered inits. The
7962 * sense code should be RESERVATIONS
7966 for (i = 0; i < CTL_MAX_INITIATORS;i++){
7968 i+persis_offset].registered
7971 lun->pending_sense[i
7977 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7978 if (lun->pr_key_count==0) {
7979 lun->flags &= ~CTL_LUN_PR_RESERVED;
7981 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7984 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7985 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7986 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
7987 persis_io.pr.pr_info.residx = residx;
7988 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7989 &persis_io, sizeof(persis_io), 0 )) >
7990 CTL_HA_STATUS_SUCCESS) {
7991 printf("CTL:Persis Out error returned from "
7992 "ctl_ha_msg_send %d\n", isc_retval);
7994 mtx_unlock(&softc->ctl_lock);
7995 } else /* sa_res_key != 0 */ {
7998 * If we aren't registered currently then increment
7999 * the key count and set the registered flag.
8001 if (!lun->per_res[residx].registered) {
8002 lun->pr_key_count++;
8003 lun->per_res[residx].registered = 1;
8006 memcpy(&lun->per_res[residx].res_key,
8007 param->serv_act_res_key,
8008 ctl_min(sizeof(param->serv_act_res_key),
8009 sizeof(lun->per_res[residx].res_key)));
8011 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8012 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8013 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8014 persis_io.pr.pr_info.residx = residx;
8015 memcpy(persis_io.pr.pr_info.sa_res_key,
8016 param->serv_act_res_key,
8017 sizeof(param->serv_act_res_key));
8018 mtx_unlock(&softc->ctl_lock);
8019 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8020 &persis_io, sizeof(persis_io), 0)) >
8021 CTL_HA_STATUS_SUCCESS) {
8022 printf("CTL:Persis Out error returned from "
8023 "ctl_ha_msg_send %d\n", isc_retval);
8026 lun->PRGeneration++;
8032 printf("Reserve executed type %d\n", type);
8034 mtx_lock(&softc->ctl_lock);
8035 if (lun->flags & CTL_LUN_PR_RESERVED) {
8037 * if this isn't the reservation holder and it's
8038 * not a "all registrants" type or if the type is
8039 * different then we have a conflict
8041 if ((lun->pr_res_idx != residx
8042 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8043 || lun->res_type != type) {
8044 mtx_unlock(&softc->ctl_lock);
8045 free(ctsio->kern_data_ptr, M_CTL);
8046 ctl_set_reservation_conflict(ctsio);
8047 ctl_done((union ctl_io *)ctsio);
8048 return (CTL_RETVAL_COMPLETE);
8050 mtx_unlock(&softc->ctl_lock);
8051 } else /* create a reservation */ {
8053 * If it's not an "all registrants" type record
8054 * reservation holder
8056 if (type != SPR_TYPE_WR_EX_AR
8057 && type != SPR_TYPE_EX_AC_AR)
8058 lun->pr_res_idx = residx; /* Res holder */
8060 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8062 lun->flags |= CTL_LUN_PR_RESERVED;
8063 lun->res_type = type;
8065 mtx_unlock(&softc->ctl_lock);
8067 /* send msg to other side */
8068 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8069 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8070 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8071 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8072 persis_io.pr.pr_info.res_type = type;
8073 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8074 &persis_io, sizeof(persis_io), 0)) >
8075 CTL_HA_STATUS_SUCCESS) {
8076 printf("CTL:Persis Out error returned from "
8077 "ctl_ha_msg_send %d\n", isc_retval);
8083 mtx_lock(&softc->ctl_lock);
8084 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8085 /* No reservation exists return good status */
8086 mtx_unlock(&softc->ctl_lock);
8090 * Is this nexus a reservation holder?
8092 if (lun->pr_res_idx != residx
8093 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8095 * not a res holder return good status but
8098 mtx_unlock(&softc->ctl_lock);
8102 if (lun->res_type != type) {
8103 mtx_unlock(&softc->ctl_lock);
8104 free(ctsio->kern_data_ptr, M_CTL);
8105 ctl_set_illegal_pr_release(ctsio);
8106 ctl_done((union ctl_io *)ctsio);
8107 return (CTL_RETVAL_COMPLETE);
8110 /* okay to release */
8111 lun->flags &= ~CTL_LUN_PR_RESERVED;
8112 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8116 * if this isn't an exclusive access
8117 * res generate UA for all other
8120 if (type != SPR_TYPE_EX_AC
8121 && type != SPR_TYPE_WR_EX) {
8123 * temporarily unregister so we don't generate UA
8125 lun->per_res[residx].registered = 0;
8127 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8128 if (lun->per_res[i+persis_offset].registered
8131 lun->pending_sense[i].ua_pending |=
8135 lun->per_res[residx].registered = 1;
8137 mtx_unlock(&softc->ctl_lock);
8138 /* Send msg to other side */
8139 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8140 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8141 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8142 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8143 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8144 printf("CTL:Persis Out error returned from "
8145 "ctl_ha_msg_send %d\n", isc_retval);
8150 /* send msg to other side */
8152 mtx_lock(&softc->ctl_lock);
8153 lun->flags &= ~CTL_LUN_PR_RESERVED;
8155 lun->pr_key_count = 0;
8156 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8159 memset(&lun->per_res[residx].res_key,
8160 0, sizeof(lun->per_res[residx].res_key));
8161 lun->per_res[residx].registered = 0;
8163 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8164 if (lun->per_res[i].registered) {
8165 if (!persis_offset && i < CTL_MAX_INITIATORS)
8166 lun->pending_sense[i].ua_pending |=
8168 else if (persis_offset && i >= persis_offset)
8169 lun->pending_sense[i-persis_offset
8170 ].ua_pending |= CTL_UA_RES_PREEMPT;
8172 memset(&lun->per_res[i].res_key,
8173 0, sizeof(struct scsi_per_res_key));
8174 lun->per_res[i].registered = 0;
8176 lun->PRGeneration++;
8177 mtx_unlock(&softc->ctl_lock);
8178 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8179 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8180 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8181 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8182 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8183 printf("CTL:Persis Out error returned from "
8184 "ctl_ha_msg_send %d\n", isc_retval);
8188 case SPRO_PREEMPT: {
8191 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8192 residx, ctsio, cdb, param);
8194 return (CTL_RETVAL_COMPLETE);
8200 free(ctsio->kern_data_ptr, M_CTL);
8201 ctl_set_invalid_field(/*ctsio*/ ctsio,
8207 ctl_done((union ctl_io *)ctsio);
8208 return (CTL_RETVAL_COMPLETE);
8209 break; /* NOTREACHED */
8213 free(ctsio->kern_data_ptr, M_CTL);
8214 ctl_set_success(ctsio);
8215 ctl_done((union ctl_io *)ctsio);
8221 * This routine is for handling a message from the other SC pertaining to
8222 * persistent reserve out. All the error checking will have been done
8223 * so only perorming the action need be done here to keep the two
8227 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8229 struct ctl_lun *lun;
8230 struct ctl_softc *softc;
8233 softc = control_softc;
8235 mtx_lock(&softc->ctl_lock);
8237 lun = softc->ctl_luns[msg->hdr.nexus.targ_lun];
8238 switch(msg->pr.pr_info.action) {
8239 case CTL_PR_REG_KEY:
8240 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8241 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8242 lun->pr_key_count++;
8244 lun->PRGeneration++;
8245 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8246 msg->pr.pr_info.sa_res_key,
8247 sizeof(struct scsi_per_res_key));
8250 case CTL_PR_UNREG_KEY:
8251 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8252 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8253 0, sizeof(struct scsi_per_res_key));
8254 lun->pr_key_count--;
8256 /* XXX Need to see if the reservation has been released */
8257 /* if so do we need to generate UA? */
8258 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8259 lun->flags &= ~CTL_LUN_PR_RESERVED;
8260 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8262 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8263 || lun->res_type == SPR_TYPE_EX_AC_RO)
8264 && lun->pr_key_count) {
8266 * If the reservation is a registrants
8267 * only type we need to generate a UA
8268 * for other registered inits. The
8269 * sense code should be RESERVATIONS
8273 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8275 persis_offset].registered == 0)
8278 lun->pending_sense[i
8284 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8285 if (lun->pr_key_count==0) {
8286 lun->flags &= ~CTL_LUN_PR_RESERVED;
8288 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8291 lun->PRGeneration++;
8294 case CTL_PR_RESERVE:
8295 lun->flags |= CTL_LUN_PR_RESERVED;
8296 lun->res_type = msg->pr.pr_info.res_type;
8297 lun->pr_res_idx = msg->pr.pr_info.residx;
8301 case CTL_PR_RELEASE:
8303 * if this isn't an exclusive access res generate UA for all
8304 * other registrants.
8306 if (lun->res_type != SPR_TYPE_EX_AC
8307 && lun->res_type != SPR_TYPE_WR_EX) {
8308 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8309 if (lun->per_res[i+persis_offset].registered)
8310 lun->pending_sense[i].ua_pending |=
8314 lun->flags &= ~CTL_LUN_PR_RESERVED;
8315 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8319 case CTL_PR_PREEMPT:
8320 ctl_pro_preempt_other(lun, msg);
8323 lun->flags &= ~CTL_LUN_PR_RESERVED;
8325 lun->pr_key_count = 0;
8326 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8328 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8329 if (lun->per_res[i].registered == 0)
8332 && i < CTL_MAX_INITIATORS)
8333 lun->pending_sense[i].ua_pending |=
8335 else if (persis_offset
8336 && i >= persis_offset)
8337 lun->pending_sense[i-persis_offset].ua_pending|=
8339 memset(&lun->per_res[i].res_key, 0,
8340 sizeof(struct scsi_per_res_key));
8341 lun->per_res[i].registered = 0;
8343 lun->PRGeneration++;
8347 mtx_unlock(&softc->ctl_lock);
8351 ctl_read_write(struct ctl_scsiio *ctsio)
8353 struct ctl_lun *lun;
8354 struct ctl_lba_len lbalen;
8356 uint32_t num_blocks;
8357 int reladdr, fua, dpo, ebp;
8361 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8363 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8370 retval = CTL_RETVAL_COMPLETE;
8372 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8373 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8374 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8378 * XXX KDM need a lock here.
8380 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8381 if ((lun->res_type == SPR_TYPE_EX_AC
8382 && residx != lun->pr_res_idx)
8383 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8384 || lun->res_type == SPR_TYPE_EX_AC_AR)
8385 && !lun->per_res[residx].registered)) {
8386 ctl_set_reservation_conflict(ctsio);
8387 ctl_done((union ctl_io *)ctsio);
8388 return (CTL_RETVAL_COMPLETE);
8392 switch (ctsio->cdb[0]) {
8395 struct scsi_rw_6 *cdb;
8397 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8399 lba = scsi_3btoul(cdb->addr);
8400 /* only 5 bits are valid in the most significant address byte */
8402 num_blocks = cdb->length;
8404 * This is correct according to SBC-2.
8406 if (num_blocks == 0)
8412 struct scsi_rw_10 *cdb;
8414 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8416 if (cdb->byte2 & SRW10_RELADDR)
8418 if (cdb->byte2 & SRW10_FUA)
8420 if (cdb->byte2 & SRW10_DPO)
8423 if ((cdb->opcode == WRITE_10)
8424 && (cdb->byte2 & SRW10_EBP))
8427 lba = scsi_4btoul(cdb->addr);
8428 num_blocks = scsi_2btoul(cdb->length);
8431 case WRITE_VERIFY_10: {
8432 struct scsi_write_verify_10 *cdb;
8434 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8437 * XXX KDM we should do actual write verify support at some
8438 * point. This is obviously fake, we're just translating
8439 * things to a write. So we don't even bother checking the
8440 * BYTCHK field, since we don't do any verification. If
8441 * the user asks for it, we'll just pretend we did it.
8443 if (cdb->byte2 & SWV_DPO)
8446 lba = scsi_4btoul(cdb->addr);
8447 num_blocks = scsi_2btoul(cdb->length);
8452 struct scsi_rw_12 *cdb;
8454 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8456 if (cdb->byte2 & SRW12_RELADDR)
8458 if (cdb->byte2 & SRW12_FUA)
8460 if (cdb->byte2 & SRW12_DPO)
8462 lba = scsi_4btoul(cdb->addr);
8463 num_blocks = scsi_4btoul(cdb->length);
8466 case WRITE_VERIFY_12: {
8467 struct scsi_write_verify_12 *cdb;
8469 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8471 if (cdb->byte2 & SWV_DPO)
8474 lba = scsi_4btoul(cdb->addr);
8475 num_blocks = scsi_4btoul(cdb->length);
8481 struct scsi_rw_16 *cdb;
8483 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8485 if (cdb->byte2 & SRW12_RELADDR)
8487 if (cdb->byte2 & SRW12_FUA)
8489 if (cdb->byte2 & SRW12_DPO)
8492 lba = scsi_8btou64(cdb->addr);
8493 num_blocks = scsi_4btoul(cdb->length);
8496 case WRITE_VERIFY_16: {
8497 struct scsi_write_verify_16 *cdb;
8499 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8501 if (cdb->byte2 & SWV_DPO)
8504 lba = scsi_8btou64(cdb->addr);
8505 num_blocks = scsi_4btoul(cdb->length);
8510 * We got a command we don't support. This shouldn't
8511 * happen, commands should be filtered out above us.
8513 ctl_set_invalid_opcode(ctsio);
8514 ctl_done((union ctl_io *)ctsio);
8516 return (CTL_RETVAL_COMPLETE);
8517 break; /* NOTREACHED */
8521 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8522 * interesting for us, but if RAIDCore is in write-back mode,
8523 * getting it to do write-through for a particular transaction may
8527 * We don't support relative addressing. That also requires
8528 * supporting linked commands, which we don't do.
8531 ctl_set_invalid_field(ctsio,
8537 ctl_done((union ctl_io *)ctsio);
8538 return (CTL_RETVAL_COMPLETE);
8542 * The first check is to make sure we're in bounds, the second
8543 * check is to catch wrap-around problems. If the lba + num blocks
8544 * is less than the lba, then we've wrapped around and the block
8545 * range is invalid anyway.
8547 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8548 || ((lba + num_blocks) < lba)) {
8549 ctl_set_lba_out_of_range(ctsio);
8550 ctl_done((union ctl_io *)ctsio);
8551 return (CTL_RETVAL_COMPLETE);
8555 * According to SBC-3, a transfer length of 0 is not an error.
8556 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8557 * translates to 256 blocks for those commands.
8559 if (num_blocks == 0) {
8560 ctl_set_success(ctsio);
8561 ctl_done((union ctl_io *)ctsio);
8562 return (CTL_RETVAL_COMPLETE);
8566 lbalen.len = num_blocks;
8567 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8570 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8572 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8578 ctl_report_luns(struct ctl_scsiio *ctsio)
8580 struct scsi_report_luns *cdb;
8581 struct scsi_report_luns_data *lun_data;
8582 struct ctl_lun *lun, *request_lun;
8583 int num_luns, retval;
8584 uint32_t alloc_len, lun_datalen;
8585 int num_filled, well_known;
8588 retval = CTL_RETVAL_COMPLETE;
8591 cdb = (struct scsi_report_luns *)ctsio->cdb;
8593 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8595 mtx_lock(&control_softc->ctl_lock);
8596 num_luns = control_softc->num_luns;
8597 mtx_unlock(&control_softc->ctl_lock);
8599 switch (cdb->select_report) {
8600 case RPL_REPORT_DEFAULT:
8601 case RPL_REPORT_ALL:
8603 case RPL_REPORT_WELLKNOWN:
8608 ctl_set_invalid_field(ctsio,
8614 ctl_done((union ctl_io *)ctsio);
8616 break; /* NOTREACHED */
8619 alloc_len = scsi_4btoul(cdb->length);
8621 * The initiator has to allocate at least 16 bytes for this request,
8622 * so he can at least get the header and the first LUN. Otherwise
8623 * we reject the request (per SPC-3 rev 14, section 6.21).
8625 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8626 sizeof(struct scsi_report_luns_lundata))) {
8627 ctl_set_invalid_field(ctsio,
8633 ctl_done((union ctl_io *)ctsio);
8637 request_lun = (struct ctl_lun *)
8638 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8640 lun_datalen = sizeof(*lun_data) +
8641 (num_luns * sizeof(struct scsi_report_luns_lundata));
8643 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8644 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8645 ctsio->kern_sg_entries = 0;
8647 if (lun_datalen < alloc_len) {
8648 ctsio->residual = alloc_len - lun_datalen;
8649 ctsio->kern_data_len = lun_datalen;
8650 ctsio->kern_total_len = lun_datalen;
8652 ctsio->residual = 0;
8653 ctsio->kern_data_len = alloc_len;
8654 ctsio->kern_total_len = alloc_len;
8656 ctsio->kern_data_resid = 0;
8657 ctsio->kern_rel_offset = 0;
8658 ctsio->kern_sg_entries = 0;
8660 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8663 * We set this to the actual data length, regardless of how much
8664 * space we actually have to return results. If the user looks at
8665 * this value, he'll know whether or not he allocated enough space
8666 * and reissue the command if necessary. We don't support well
8667 * known logical units, so if the user asks for that, return none.
8669 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8671 mtx_lock(&control_softc->ctl_lock);
8672 for (num_filled = 0, lun = STAILQ_FIRST(&control_softc->lun_list);
8673 (lun != NULL) && (num_filled < num_luns);
8674 lun = STAILQ_NEXT(lun, links)) {
8676 if (lun->lun <= 0xff) {
8678 * Peripheral addressing method, bus number 0.
8680 lun_data->luns[num_filled].lundata[0] =
8681 RPL_LUNDATA_ATYP_PERIPH;
8682 lun_data->luns[num_filled].lundata[1] = lun->lun;
8684 } else if (lun->lun <= 0x3fff) {
8686 * Flat addressing method.
8688 lun_data->luns[num_filled].lundata[0] =
8689 RPL_LUNDATA_ATYP_FLAT |
8690 (lun->lun & RPL_LUNDATA_FLAT_LUN_MASK);
8691 #ifdef OLDCTLHEADERS
8692 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8693 (lun->lun & SRLD_BUS_LUN_MASK);
8695 lun_data->luns[num_filled].lundata[1] =
8696 #ifdef OLDCTLHEADERS
8697 lun->lun >> SRLD_BUS_LUN_BITS;
8699 lun->lun >> RPL_LUNDATA_FLAT_LUN_BITS;
8702 printf("ctl_report_luns: bogus LUN number %jd, "
8703 "skipping\n", (intmax_t)lun->lun);
8706 * According to SPC-3, rev 14 section 6.21:
8708 * "The execution of a REPORT LUNS command to any valid and
8709 * installed logical unit shall clear the REPORTED LUNS DATA
8710 * HAS CHANGED unit attention condition for all logical
8711 * units of that target with respect to the requesting
8712 * initiator. A valid and installed logical unit is one
8713 * having a PERIPHERAL QUALIFIER of 000b in the standard
8714 * INQUIRY data (see 6.4.2)."
8716 * If request_lun is NULL, the LUN this report luns command
8717 * was issued to is either disabled or doesn't exist. In that
8718 * case, we shouldn't clear any pending lun change unit
8721 if (request_lun != NULL)
8722 lun->pending_sense[initidx].ua_pending &=
8725 mtx_unlock(&control_softc->ctl_lock);
8728 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8731 ctsio->scsi_status = SCSI_STATUS_OK;
8733 ctsio->be_move_done = ctl_config_move_done;
8734 ctl_datamove((union ctl_io *)ctsio);
8740 ctl_request_sense(struct ctl_scsiio *ctsio)
8742 struct scsi_request_sense *cdb;
8743 struct scsi_sense_data *sense_ptr;
8744 struct ctl_lun *lun;
8747 scsi_sense_data_type sense_format;
8749 cdb = (struct scsi_request_sense *)ctsio->cdb;
8751 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8753 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
8756 * Determine which sense format the user wants.
8758 if (cdb->byte2 & SRS_DESC)
8759 sense_format = SSD_TYPE_DESC;
8761 sense_format = SSD_TYPE_FIXED;
8763 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
8764 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
8765 ctsio->kern_sg_entries = 0;
8768 * struct scsi_sense_data, which is currently set to 256 bytes, is
8769 * larger than the largest allowed value for the length field in the
8770 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
8772 ctsio->residual = 0;
8773 ctsio->kern_data_len = cdb->length;
8774 ctsio->kern_total_len = cdb->length;
8776 ctsio->kern_data_resid = 0;
8777 ctsio->kern_rel_offset = 0;
8778 ctsio->kern_sg_entries = 0;
8781 * If we don't have a LUN, we don't have any pending sense.
8787 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8789 * Check for pending sense, and then for pending unit attentions.
8790 * Pending sense gets returned first, then pending unit attentions.
8792 mtx_lock(&lun->ctl_softc->ctl_lock);
8793 if (ctl_is_set(lun->have_ca, initidx)) {
8794 scsi_sense_data_type stored_format;
8797 * Check to see which sense format was used for the stored
8800 stored_format = scsi_sense_type(
8801 &lun->pending_sense[initidx].sense);
8804 * If the user requested a different sense format than the
8805 * one we stored, then we need to convert it to the other
8806 * format. If we're going from descriptor to fixed format
8807 * sense data, we may lose things in translation, depending
8808 * on what options were used.
8810 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
8811 * for some reason we'll just copy it out as-is.
8813 if ((stored_format == SSD_TYPE_FIXED)
8814 && (sense_format == SSD_TYPE_DESC))
8815 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
8816 &lun->pending_sense[initidx].sense,
8817 (struct scsi_sense_data_desc *)sense_ptr);
8818 else if ((stored_format == SSD_TYPE_DESC)
8819 && (sense_format == SSD_TYPE_FIXED))
8820 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
8821 &lun->pending_sense[initidx].sense,
8822 (struct scsi_sense_data_fixed *)sense_ptr);
8824 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
8825 ctl_min(sizeof(*sense_ptr),
8826 sizeof(lun->pending_sense[initidx].sense)));
8828 ctl_clear_mask(lun->have_ca, initidx);
8830 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
8831 ctl_ua_type ua_type;
8833 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
8834 sense_ptr, sense_format);
8835 if (ua_type != CTL_UA_NONE) {
8837 /* We're reporting this UA, so clear it */
8838 lun->pending_sense[initidx].ua_pending &= ~ua_type;
8841 mtx_unlock(&lun->ctl_softc->ctl_lock);
8844 * We already have a pending error, return it.
8846 if (have_error != 0) {
8848 * We report the SCSI status as OK, since the status of the
8849 * request sense command itself is OK.
8851 ctsio->scsi_status = SCSI_STATUS_OK;
8854 * We report 0 for the sense length, because we aren't doing
8855 * autosense in this case. We're reporting sense as
8858 ctsio->sense_len = 0;
8860 ctsio->be_move_done = ctl_config_move_done;
8861 ctl_datamove((union ctl_io *)ctsio);
8863 return (CTL_RETVAL_COMPLETE);
8869 * No sense information to report, so we report that everything is
8872 ctl_set_sense_data(sense_ptr,
8875 /*current_error*/ 1,
8876 /*sense_key*/ SSD_KEY_NO_SENSE,
8881 ctsio->scsi_status = SCSI_STATUS_OK;
8884 * We report 0 for the sense length, because we aren't doing
8885 * autosense in this case. We're reporting sense as parameter data.
8887 ctsio->sense_len = 0;
8888 ctsio->be_move_done = ctl_config_move_done;
8889 ctl_datamove((union ctl_io *)ctsio);
8891 return (CTL_RETVAL_COMPLETE);
8895 ctl_tur(struct ctl_scsiio *ctsio)
8897 struct ctl_lun *lun;
8899 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8901 CTL_DEBUG_PRINT(("ctl_tur\n"));
8906 ctsio->scsi_status = SCSI_STATUS_OK;
8907 ctsio->io_hdr.status = CTL_SUCCESS;
8909 ctl_done((union ctl_io *)ctsio);
8911 return (CTL_RETVAL_COMPLETE);
8916 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
8923 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
8925 struct scsi_vpd_supported_pages *pages;
8927 struct ctl_lun *lun;
8929 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8931 sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
8932 SCSI_EVPD_NUM_SUPPORTED_PAGES;
8933 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
8934 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
8935 ctsio->kern_sg_entries = 0;
8937 if (sup_page_size < alloc_len) {
8938 ctsio->residual = alloc_len - sup_page_size;
8939 ctsio->kern_data_len = sup_page_size;
8940 ctsio->kern_total_len = sup_page_size;
8942 ctsio->residual = 0;
8943 ctsio->kern_data_len = alloc_len;
8944 ctsio->kern_total_len = alloc_len;
8946 ctsio->kern_data_resid = 0;
8947 ctsio->kern_rel_offset = 0;
8948 ctsio->kern_sg_entries = 0;
8951 * The control device is always connected. The disk device, on the
8952 * other hand, may not be online all the time. Need to change this
8953 * to figure out whether the disk device is actually online or not.
8956 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
8957 lun->be_lun->lun_type;
8959 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
8961 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
8962 /* Supported VPD pages */
8963 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
8965 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
8966 /* Device Identification */
8967 pages->page_list[2] = SVPD_DEVICE_ID;
8969 ctsio->scsi_status = SCSI_STATUS_OK;
8971 ctsio->be_move_done = ctl_config_move_done;
8972 ctl_datamove((union ctl_io *)ctsio);
8974 return (CTL_RETVAL_COMPLETE);
8978 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
8980 struct scsi_vpd_unit_serial_number *sn_ptr;
8981 struct ctl_lun *lun;
8982 #ifndef CTL_USE_BACKEND_SN
8986 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8988 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
8989 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
8990 ctsio->kern_sg_entries = 0;
8992 if (sizeof(*sn_ptr) < alloc_len) {
8993 ctsio->residual = alloc_len - sizeof(*sn_ptr);
8994 ctsio->kern_data_len = sizeof(*sn_ptr);
8995 ctsio->kern_total_len = sizeof(*sn_ptr);
8997 ctsio->residual = 0;
8998 ctsio->kern_data_len = alloc_len;
8999 ctsio->kern_total_len = alloc_len;
9001 ctsio->kern_data_resid = 0;
9002 ctsio->kern_rel_offset = 0;
9003 ctsio->kern_sg_entries = 0;
9006 * The control device is always connected. The disk device, on the
9007 * other hand, may not be online all the time. Need to change this
9008 * to figure out whether the disk device is actually online or not.
9011 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9012 lun->be_lun->lun_type;
9014 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9016 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9017 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9018 #ifdef CTL_USE_BACKEND_SN
9020 * If we don't have a LUN, we just leave the serial number as
9023 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9025 strncpy((char *)sn_ptr->serial_num,
9026 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9030 * Note that we're using a non-unique serial number here,
9032 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9033 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9034 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9035 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9037 ctsio->scsi_status = SCSI_STATUS_OK;
9039 ctsio->be_move_done = ctl_config_move_done;
9040 ctl_datamove((union ctl_io *)ctsio);
9042 return (CTL_RETVAL_COMPLETE);
9047 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9049 struct scsi_vpd_device_id *devid_ptr;
9050 struct scsi_vpd_id_descriptor *desc, *desc1;
9051 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9052 struct scsi_vpd_id_t10 *t10id;
9053 struct ctl_softc *ctl_softc;
9054 struct ctl_lun *lun;
9055 struct ctl_frontend *fe;
9056 #ifndef CTL_USE_BACKEND_SN
9058 #endif /* CTL_USE_BACKEND_SN */
9061 ctl_softc = control_softc;
9062 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9064 devid_len = sizeof(struct scsi_vpd_device_id) +
9065 sizeof(struct scsi_vpd_id_descriptor) +
9066 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9067 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9068 sizeof(struct scsi_vpd_id_descriptor) +
9069 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9070 sizeof(struct scsi_vpd_id_descriptor) +
9071 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9073 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9074 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9075 ctsio->kern_sg_entries = 0;
9077 if (devid_len < alloc_len) {
9078 ctsio->residual = alloc_len - devid_len;
9079 ctsio->kern_data_len = devid_len;
9080 ctsio->kern_total_len = devid_len;
9082 ctsio->residual = 0;
9083 ctsio->kern_data_len = alloc_len;
9084 ctsio->kern_total_len = alloc_len;
9086 ctsio->kern_data_resid = 0;
9087 ctsio->kern_rel_offset = 0;
9088 ctsio->kern_sg_entries = 0;
9090 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9091 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9092 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9093 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9094 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9096 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9097 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9100 * The control device is always connected. The disk device, on the
9101 * other hand, may not be online all the time.
9104 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9105 lun->be_lun->lun_type;
9107 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9109 devid_ptr->page_code = SVPD_DEVICE_ID;
9111 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9113 mtx_lock(&ctl_softc->ctl_lock);
9115 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9118 * For Fibre channel,
9120 if (fe->port_type == CTL_PORT_FC)
9122 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9123 SVPD_ID_CODESET_ASCII;
9124 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9125 SVPD_ID_CODESET_BINARY;
9129 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9130 SVPD_ID_CODESET_ASCII;
9131 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9132 SVPD_ID_CODESET_BINARY;
9134 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9135 mtx_unlock(&ctl_softc->ctl_lock);
9138 * We're using a LUN association here. i.e., this device ID is a
9139 * per-LUN identifier.
9141 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9142 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9143 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9146 * desc1 is for the WWPN which is a port asscociation.
9148 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9149 desc1->length = CTL_WWPN_LEN;
9150 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9151 /* For testing just create the WWPN */
9153 ddb_GetWWNN((char *)desc1->identifier);
9155 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9156 /* This is so Copancontrol will return something sane */
9157 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9158 ctsio->io_hdr.nexus.targ_port!=8)
9159 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9161 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9164 be64enc(desc1->identifier, fe->wwpn);
9167 * desc2 is for the Relative Target Port(type 4h) identifier
9169 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9170 | SVPD_ID_TYPE_RELTARG;
9173 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9174 /* This is so Copancontrol will return something sane */
9175 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9176 ctsio->io_hdr.nexus.targ_port!=8)
9177 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9179 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9183 * desc3 is for the Target Port Group(type 5h) identifier
9185 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9186 | SVPD_ID_TYPE_TPORTGRP;
9188 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9189 desc3->identifier[3] = 1;
9191 desc3->identifier[3] = 2;
9193 #ifdef CTL_USE_BACKEND_SN
9195 * If we've actually got a backend, copy the device id from the
9196 * per-LUN data. Otherwise, set it to all spaces.
9200 * Copy the backend's LUN ID.
9202 strncpy((char *)t10id->vendor_spec_id,
9203 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9206 * No backend, set this to spaces.
9208 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9211 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9212 (lun != NULL) ? (int)lun->lun : 0);
9213 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9217 ctsio->scsi_status = SCSI_STATUS_OK;
9219 ctsio->be_move_done = ctl_config_move_done;
9220 ctl_datamove((union ctl_io *)ctsio);
9222 return (CTL_RETVAL_COMPLETE);
9226 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9228 struct scsi_inquiry *cdb;
9229 int alloc_len, retval;
9231 cdb = (struct scsi_inquiry *)ctsio->cdb;
9233 retval = CTL_RETVAL_COMPLETE;
9235 alloc_len = scsi_2btoul(cdb->length);
9237 switch (cdb->page_code) {
9238 case SVPD_SUPPORTED_PAGES:
9239 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9241 case SVPD_UNIT_SERIAL_NUMBER:
9242 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9244 case SVPD_DEVICE_ID:
9245 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9248 ctl_set_invalid_field(ctsio,
9254 ctl_done((union ctl_io *)ctsio);
9255 retval = CTL_RETVAL_COMPLETE;
9263 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9265 struct scsi_inquiry_data *inq_ptr;
9266 struct scsi_inquiry *cdb;
9267 struct ctl_softc *ctl_softc;
9268 struct ctl_lun *lun;
9272 ctl_softc = control_softc;
9275 * Figure out whether we're talking to a Fibre Channel port or not.
9276 * We treat the ioctl front end, and any SCSI adapters, as packetized
9279 mtx_lock(&ctl_softc->ctl_lock);
9280 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9285 mtx_unlock(&ctl_softc->ctl_lock);
9287 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9288 cdb = (struct scsi_inquiry *)ctsio->cdb;
9289 alloc_len = scsi_2btoul(cdb->length);
9292 * We malloc the full inquiry data size here and fill it
9293 * in. If the user only asks for less, we'll give him
9296 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9297 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9298 ctsio->kern_sg_entries = 0;
9299 ctsio->kern_data_resid = 0;
9300 ctsio->kern_rel_offset = 0;
9302 if (sizeof(*inq_ptr) < alloc_len) {
9303 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9304 ctsio->kern_data_len = sizeof(*inq_ptr);
9305 ctsio->kern_total_len = sizeof(*inq_ptr);
9307 ctsio->residual = 0;
9308 ctsio->kern_data_len = alloc_len;
9309 ctsio->kern_total_len = alloc_len;
9313 * If we have a LUN configured, report it as connected. Otherwise,
9314 * report that it is offline or no device is supported, depending
9315 * on the value of inquiry_pq_no_lun.
9317 * According to the spec (SPC-4 r34), the peripheral qualifier
9318 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9320 * "A peripheral device having the specified peripheral device type
9321 * is not connected to this logical unit. However, the device
9322 * server is capable of supporting the specified peripheral device
9323 * type on this logical unit."
9325 * According to the same spec, the peripheral qualifier
9326 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9328 * "The device server is not capable of supporting a peripheral
9329 * device on this logical unit. For this peripheral qualifier the
9330 * peripheral device type shall be set to 1Fh. All other peripheral
9331 * device type values are reserved for this peripheral qualifier."
9333 * Given the text, it would seem that we probably want to report that
9334 * the LUN is offline here. There is no LUN connected, but we can
9335 * support a LUN at the given LUN number.
9337 * In the real world, though, it sounds like things are a little
9340 * - Linux, when presented with a LUN with the offline peripheral
9341 * qualifier, will create an sg driver instance for it. So when
9342 * you attach it to CTL, you wind up with a ton of sg driver
9343 * instances. (One for every LUN that Linux bothered to probe.)
9344 * Linux does this despite the fact that it issues a REPORT LUNs
9345 * to LUN 0 to get the inventory of supported LUNs.
9347 * - There is other anecdotal evidence (from Emulex folks) about
9348 * arrays that use the offline peripheral qualifier for LUNs that
9349 * are on the "passive" path in an active/passive array.
9351 * So the solution is provide a hopefully reasonable default
9352 * (return bad/no LUN) and allow the user to change the behavior
9353 * with a tunable/sysctl variable.
9356 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9357 lun->be_lun->lun_type;
9358 else if (ctl_softc->inquiry_pq_no_lun == 0)
9359 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9361 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9363 /* RMB in byte 2 is 0 */
9364 inq_ptr->version = SCSI_REV_SPC3;
9367 * According to SAM-3, even if a device only supports a single
9368 * level of LUN addressing, it should still set the HISUP bit:
9370 * 4.9.1 Logical unit numbers overview
9372 * All logical unit number formats described in this standard are
9373 * hierarchical in structure even when only a single level in that
9374 * hierarchy is used. The HISUP bit shall be set to one in the
9375 * standard INQUIRY data (see SPC-2) when any logical unit number
9376 * format described in this standard is used. Non-hierarchical
9377 * formats are outside the scope of this standard.
9379 * Therefore we set the HiSup bit here.
9381 * The reponse format is 2, per SPC-3.
9383 inq_ptr->response_format = SID_HiSup | 2;
9385 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9386 CTL_DEBUG_PRINT(("additional_length = %d\n",
9387 inq_ptr->additional_length));
9389 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9390 /* 16 bit addressing */
9392 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9393 /* XXX set the SID_MultiP bit here if we're actually going to
9394 respond on multiple ports */
9395 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9397 /* 16 bit data bus, synchronous transfers */
9398 /* XXX these flags don't apply for FC */
9400 inq_ptr->flags = SID_WBus16 | SID_Sync;
9402 * XXX KDM do we want to support tagged queueing on the control
9406 || (lun->be_lun->lun_type != T_PROCESSOR))
9407 inq_ptr->flags |= SID_CmdQue;
9409 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9410 * We have 8 bytes for the vendor name, and 16 bytes for the device
9411 * name and 4 bytes for the revision.
9413 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9415 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9417 switch (lun->be_lun->lun_type) {
9419 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9422 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9425 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9431 * XXX make this a macro somewhere so it automatically gets
9432 * incremented when we make changes.
9434 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9437 * For parallel SCSI, we support double transition and single
9438 * transition clocking. We also support QAS (Quick Arbitration
9439 * and Selection) and Information Unit transfers on both the
9440 * control and array devices.
9443 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9447 scsi_ulto2b(0x0060, inq_ptr->version1);
9448 /* SPC-3 (no version claimed) XXX should we claim a version? */
9449 scsi_ulto2b(0x0300, inq_ptr->version2);
9451 /* FCP-2 ANSI INCITS.350:2003 */
9452 scsi_ulto2b(0x0917, inq_ptr->version3);
9454 /* SPI-4 ANSI INCITS.362:200x */
9455 scsi_ulto2b(0x0B56, inq_ptr->version3);
9459 /* SBC-2 (no version claimed) XXX should we claim a version? */
9460 scsi_ulto2b(0x0320, inq_ptr->version4);
9462 switch (lun->be_lun->lun_type) {
9465 * SBC-2 (no version claimed) XXX should we claim a
9468 scsi_ulto2b(0x0320, inq_ptr->version4);
9476 ctsio->scsi_status = SCSI_STATUS_OK;
9477 if (ctsio->kern_data_len > 0) {
9478 ctsio->be_move_done = ctl_config_move_done;
9479 ctl_datamove((union ctl_io *)ctsio);
9481 ctsio->io_hdr.status = CTL_SUCCESS;
9482 ctl_done((union ctl_io *)ctsio);
9485 return (CTL_RETVAL_COMPLETE);
9489 ctl_inquiry(struct ctl_scsiio *ctsio)
9491 struct scsi_inquiry *cdb;
9494 cdb = (struct scsi_inquiry *)ctsio->cdb;
9498 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9501 * Right now, we don't support the CmdDt inquiry information.
9502 * This would be nice to support in the future. When we do
9503 * support it, we should change this test so that it checks to make
9504 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9507 if (((cdb->byte2 & SI_EVPD)
9508 && (cdb->byte2 & SI_CMDDT)))
9510 if (cdb->byte2 & SI_CMDDT) {
9512 * Point to the SI_CMDDT bit. We might change this
9513 * when we support SI_CMDDT, but since both bits would be
9514 * "wrong", this should probably just stay as-is then.
9516 ctl_set_invalid_field(ctsio,
9522 ctl_done((union ctl_io *)ctsio);
9523 return (CTL_RETVAL_COMPLETE);
9525 if (cdb->byte2 & SI_EVPD)
9526 retval = ctl_inquiry_evpd(ctsio);
9528 else if (cdb->byte2 & SI_CMDDT)
9529 retval = ctl_inquiry_cmddt(ctsio);
9532 retval = ctl_inquiry_std(ctsio);
9538 * For known CDB types, parse the LBA and length.
9541 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9543 if (io->io_hdr.io_type != CTL_IO_SCSI)
9546 switch (io->scsiio.cdb[0]) {
9549 struct scsi_rw_6 *cdb;
9551 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9553 *lba = scsi_3btoul(cdb->addr);
9554 /* only 5 bits are valid in the most significant address byte */
9561 struct scsi_rw_10 *cdb;
9563 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9565 *lba = scsi_4btoul(cdb->addr);
9566 *len = scsi_2btoul(cdb->length);
9569 case WRITE_VERIFY_10: {
9570 struct scsi_write_verify_10 *cdb;
9572 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9574 *lba = scsi_4btoul(cdb->addr);
9575 *len = scsi_2btoul(cdb->length);
9580 struct scsi_rw_12 *cdb;
9582 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9584 *lba = scsi_4btoul(cdb->addr);
9585 *len = scsi_4btoul(cdb->length);
9588 case WRITE_VERIFY_12: {
9589 struct scsi_write_verify_12 *cdb;
9591 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9593 *lba = scsi_4btoul(cdb->addr);
9594 *len = scsi_4btoul(cdb->length);
9599 struct scsi_rw_16 *cdb;
9601 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9603 *lba = scsi_8btou64(cdb->addr);
9604 *len = scsi_4btoul(cdb->length);
9607 case WRITE_VERIFY_16: {
9608 struct scsi_write_verify_16 *cdb;
9610 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9613 *lba = scsi_8btou64(cdb->addr);
9614 *len = scsi_4btoul(cdb->length);
9619 break; /* NOTREACHED */
9626 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
9628 uint64_t endlba1, endlba2;
9630 endlba1 = lba1 + len1 - 1;
9631 endlba2 = lba2 + len2 - 1;
9633 if ((endlba1 < lba2)
9634 || (endlba2 < lba1))
9635 return (CTL_ACTION_PASS);
9637 return (CTL_ACTION_BLOCK);
9641 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
9643 uint64_t lba1, lba2;
9644 uint32_t len1, len2;
9647 retval = ctl_get_lba_len(io1, &lba1, &len1);
9649 return (CTL_ACTION_ERROR);
9651 retval = ctl_get_lba_len(io2, &lba2, &len2);
9653 return (CTL_ACTION_ERROR);
9655 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
9659 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
9661 struct ctl_cmd_entry *pending_entry, *ooa_entry;
9662 ctl_serialize_action *serialize_row;
9665 * The initiator attempted multiple untagged commands at the same
9666 * time. Can't do that.
9668 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9669 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9670 && ((pending_io->io_hdr.nexus.targ_port ==
9671 ooa_io->io_hdr.nexus.targ_port)
9672 && (pending_io->io_hdr.nexus.initid.id ==
9673 ooa_io->io_hdr.nexus.initid.id))
9674 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9675 return (CTL_ACTION_OVERLAP);
9678 * The initiator attempted to send multiple tagged commands with
9679 * the same ID. (It's fine if different initiators have the same
9682 * Even if all of those conditions are true, we don't kill the I/O
9683 * if the command ahead of us has been aborted. We won't end up
9684 * sending it to the FETD, and it's perfectly legal to resend a
9685 * command with the same tag number as long as the previous
9686 * instance of this tag number has been aborted somehow.
9688 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9689 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9690 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
9691 && ((pending_io->io_hdr.nexus.targ_port ==
9692 ooa_io->io_hdr.nexus.targ_port)
9693 && (pending_io->io_hdr.nexus.initid.id ==
9694 ooa_io->io_hdr.nexus.initid.id))
9695 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9696 return (CTL_ACTION_OVERLAP_TAG);
9699 * If we get a head of queue tag, SAM-3 says that we should
9700 * immediately execute it.
9702 * What happens if this command would normally block for some other
9703 * reason? e.g. a request sense with a head of queue tag
9704 * immediately after a write. Normally that would block, but this
9705 * will result in its getting executed immediately...
9707 * We currently return "pass" instead of "skip", so we'll end up
9708 * going through the rest of the queue to check for overlapped tags.
9710 * XXX KDM check for other types of blockage first??
9712 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9713 return (CTL_ACTION_PASS);
9716 * Ordered tags have to block until all items ahead of them
9717 * have completed. If we get called with an ordered tag, we always
9718 * block, if something else is ahead of us in the queue.
9720 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
9721 return (CTL_ACTION_BLOCK);
9724 * Simple tags get blocked until all head of queue and ordered tags
9725 * ahead of them have completed. I'm lumping untagged commands in
9726 * with simple tags here. XXX KDM is that the right thing to do?
9728 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9729 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
9730 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9731 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
9732 return (CTL_ACTION_BLOCK);
9734 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
9735 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
9737 serialize_row = ctl_serialize_table[ooa_entry->seridx];
9739 switch (serialize_row[pending_entry->seridx]) {
9741 return (CTL_ACTION_BLOCK);
9742 break; /* NOTREACHED */
9743 case CTL_SER_EXTENT:
9744 return (ctl_extent_check(pending_io, ooa_io));
9745 break; /* NOTREACHED */
9747 return (CTL_ACTION_PASS);
9748 break; /* NOTREACHED */
9750 return (CTL_ACTION_SKIP);
9753 panic("invalid serialization value %d",
9754 serialize_row[pending_entry->seridx]);
9755 break; /* NOTREACHED */
9758 return (CTL_ACTION_ERROR);
9762 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
9764 * - pending_io is generally either incoming, or on the blocked queue
9765 * - starting I/O is the I/O we want to start the check with.
9768 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
9769 union ctl_io *starting_io)
9771 union ctl_io *ooa_io;
9774 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9777 * Run back along the OOA queue, starting with the current
9778 * blocked I/O and going through every I/O before it on the
9779 * queue. If starting_io is NULL, we'll just end up returning
9782 for (ooa_io = starting_io; ooa_io != NULL;
9783 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
9787 * This routine just checks to see whether
9788 * cur_blocked is blocked by ooa_io, which is ahead
9789 * of it in the queue. It doesn't queue/dequeue
9792 action = ctl_check_for_blockage(pending_io, ooa_io);
9794 case CTL_ACTION_BLOCK:
9795 case CTL_ACTION_OVERLAP:
9796 case CTL_ACTION_OVERLAP_TAG:
9797 case CTL_ACTION_SKIP:
9798 case CTL_ACTION_ERROR:
9800 break; /* NOTREACHED */
9801 case CTL_ACTION_PASS:
9804 panic("invalid action %d", action);
9805 break; /* NOTREACHED */
9809 return (CTL_ACTION_PASS);
9814 * - An I/O has just completed, and has been removed from the per-LUN OOA
9815 * queue, so some items on the blocked queue may now be unblocked.
9818 ctl_check_blocked(struct ctl_lun *lun)
9820 union ctl_io *cur_blocked, *next_blocked;
9822 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9825 * Run forward from the head of the blocked queue, checking each
9826 * entry against the I/Os prior to it on the OOA queue to see if
9827 * there is still any blockage.
9829 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
9830 * with our removing a variable on it while it is traversing the
9833 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
9834 cur_blocked != NULL; cur_blocked = next_blocked) {
9835 union ctl_io *prev_ooa;
9838 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
9841 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
9842 ctl_ooaq, ooa_links);
9845 * If cur_blocked happens to be the first item in the OOA
9846 * queue now, prev_ooa will be NULL, and the action
9847 * returned will just be CTL_ACTION_PASS.
9849 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
9852 case CTL_ACTION_BLOCK:
9853 /* Nothing to do here, still blocked */
9855 case CTL_ACTION_OVERLAP:
9856 case CTL_ACTION_OVERLAP_TAG:
9858 * This shouldn't happen! In theory we've already
9859 * checked this command for overlap...
9862 case CTL_ACTION_PASS:
9863 case CTL_ACTION_SKIP: {
9864 struct ctl_softc *softc;
9865 struct ctl_cmd_entry *entry;
9871 * The skip case shouldn't happen, this transaction
9872 * should have never made it onto the blocked queue.
9875 * This I/O is no longer blocked, we can remove it
9876 * from the blocked queue. Since this is a TAILQ
9877 * (doubly linked list), we can do O(1) removals
9878 * from any place on the list.
9880 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
9882 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
9884 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
9886 * Need to send IO back to original side to
9889 union ctl_ha_msg msg_info;
9891 msg_info.hdr.original_sc =
9892 cur_blocked->io_hdr.original_sc;
9893 msg_info.hdr.serializing_sc = cur_blocked;
9894 msg_info.hdr.msg_type = CTL_MSG_R2R;
9895 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
9896 &msg_info, sizeof(msg_info), 0)) >
9897 CTL_HA_STATUS_SUCCESS) {
9898 printf("CTL:Check Blocked error from "
9899 "ctl_ha_msg_send %d\n",
9904 opcode = cur_blocked->scsiio.cdb[0];
9905 entry = &ctl_cmd_table[opcode];
9906 softc = control_softc;
9908 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
9911 * Check this I/O for LUN state changes that may
9912 * have happened while this command was blocked.
9913 * The LUN state may have been changed by a command
9914 * ahead of us in the queue, so we need to re-check
9915 * for any states that can be caused by SCSI
9918 if (ctl_scsiio_lun_check(softc, lun, entry,
9919 &cur_blocked->scsiio) == 0) {
9920 cur_blocked->io_hdr.flags |=
9921 CTL_FLAG_IS_WAS_ON_RTR;
9922 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
9923 &cur_blocked->io_hdr, links);
9925 * In the non CTL_DONE_THREAD case, we need
9926 * to wake up the work thread here. When
9927 * we're processing completed requests from
9928 * the work thread context, we'll pop back
9929 * around and end up pulling things off the
9930 * RtR queue. When we aren't processing
9931 * things from the work thread context,
9932 * though, we won't ever check the RtR queue.
9933 * So we need to wake up the thread to clear
9934 * things off the queue. Otherwise this
9935 * transaction will just sit on the RtR queue
9936 * until a new I/O comes in. (Which may or
9937 * may not happen...)
9939 #ifndef CTL_DONE_THREAD
9940 ctl_wakeup_thread();
9943 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
9948 * This probably shouldn't happen -- we shouldn't
9949 * get CTL_ACTION_ERROR, or anything else.
9955 return (CTL_RETVAL_COMPLETE);
9959 * This routine (with one exception) checks LUN flags that can be set by
9960 * commands ahead of us in the OOA queue. These flags have to be checked
9961 * when a command initially comes in, and when we pull a command off the
9962 * blocked queue and are preparing to execute it. The reason we have to
9963 * check these flags for commands on the blocked queue is that the LUN
9964 * state may have been changed by a command ahead of us while we're on the
9967 * Ordering is somewhat important with these checks, so please pay
9968 * careful attention to the placement of any new checks.
9971 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
9972 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
9979 * If this shelf is a secondary shelf controller, we have to reject
9980 * any media access commands.
9983 /* No longer needed for HA */
9984 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
9985 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
9986 ctl_set_lun_standby(ctsio);
9993 * Check for a reservation conflict. If this command isn't allowed
9994 * even on reserved LUNs, and if this initiator isn't the one who
9995 * reserved us, reject the command with a reservation conflict.
9997 if ((lun->flags & CTL_LUN_RESERVED)
9998 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
9999 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10000 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10001 || (ctsio->io_hdr.nexus.targ_target.id !=
10002 lun->rsv_nexus.targ_target.id)) {
10003 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10004 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10010 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10011 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10014 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10016 * if we aren't registered or it's a res holder type
10017 * reservation and this isn't the res holder then set a
10019 * NOTE: Commands which might be allowed on write exclusive
10020 * type reservations are checked in the particular command
10021 * for a conflict. Read and SSU are the only ones.
10023 if (!lun->per_res[residx].registered
10024 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10025 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10026 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10033 if ((lun->flags & CTL_LUN_OFFLINE)
10034 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10035 ctl_set_lun_not_ready(ctsio);
10041 * If the LUN is stopped, see if this particular command is allowed
10042 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10044 if ((lun->flags & CTL_LUN_STOPPED)
10045 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10046 /* "Logical unit not ready, initializing cmd. required" */
10047 ctl_set_lun_stopped(ctsio);
10052 if ((lun->flags & CTL_LUN_INOPERABLE)
10053 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10054 /* "Medium format corrupted" */
10055 ctl_set_medium_format_corrupted(ctsio);
10066 ctl_failover_io(union ctl_io *io, int have_lock)
10068 ctl_set_busy(&io->scsiio);
10069 ctl_done_lock(io, have_lock);
10075 struct ctl_lun *lun;
10076 struct ctl_softc *ctl_softc;
10077 union ctl_io *next_io, *pending_io;
10082 ctl_softc = control_softc;
10084 mtx_lock(&ctl_softc->ctl_lock);
10086 * Remove any cmds from the other SC from the rtr queue. These
10087 * will obviously only be for LUNs for which we're the primary.
10088 * We can't send status or get/send data for these commands.
10089 * Since they haven't been executed yet, we can just remove them.
10090 * We'll either abort them or delete them below, depending on
10091 * which HA mode we're in.
10093 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10094 io != NULL; io = next_io) {
10095 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10096 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10097 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10098 ctl_io_hdr, links);
10101 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10102 lun = ctl_softc->ctl_luns[lun_idx];
10107 * Processor LUNs are primary on both sides.
10108 * XXX will this always be true?
10110 if (lun->be_lun->lun_type == T_PROCESSOR)
10113 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10114 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10115 printf("FAILOVER: primary lun %d\n", lun_idx);
10117 * Remove all commands from the other SC. First from the
10118 * blocked queue then from the ooa queue. Once we have
10119 * removed them. Call ctl_check_blocked to see if there
10120 * is anything that can run.
10122 for (io = (union ctl_io *)TAILQ_FIRST(
10123 &lun->blocked_queue); io != NULL; io = next_io) {
10125 next_io = (union ctl_io *)TAILQ_NEXT(
10126 &io->io_hdr, blocked_links);
10128 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10129 TAILQ_REMOVE(&lun->blocked_queue,
10130 &io->io_hdr,blocked_links);
10131 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10132 TAILQ_REMOVE(&lun->ooa_queue,
10133 &io->io_hdr, ooa_links);
10139 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10140 io != NULL; io = next_io) {
10142 next_io = (union ctl_io *)TAILQ_NEXT(
10143 &io->io_hdr, ooa_links);
10145 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10147 TAILQ_REMOVE(&lun->ooa_queue,
10154 ctl_check_blocked(lun);
10155 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10156 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10158 printf("FAILOVER: primary lun %d\n", lun_idx);
10160 * Abort all commands from the other SC. We can't
10161 * send status back for them now. These should get
10162 * cleaned up when they are completed or come out
10163 * for a datamove operation.
10165 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10166 io != NULL; io = next_io) {
10167 next_io = (union ctl_io *)TAILQ_NEXT(
10168 &io->io_hdr, ooa_links);
10170 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10171 io->io_hdr.flags |= CTL_FLAG_ABORT;
10173 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10174 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10176 printf("FAILOVER: secondary lun %d\n", lun_idx);
10178 lun->flags |= CTL_LUN_PRIMARY_SC;
10181 * We send all I/O that was sent to this controller
10182 * and redirected to the other side back with
10183 * busy status, and have the initiator retry it.
10184 * Figuring out how much data has been transferred,
10185 * etc. and picking up where we left off would be
10188 * XXX KDM need to remove I/O from the blocked
10191 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10192 &lun->ooa_queue); pending_io != NULL;
10193 pending_io = next_io) {
10195 next_io = (union ctl_io *)TAILQ_NEXT(
10196 &pending_io->io_hdr, ooa_links);
10198 pending_io->io_hdr.flags &=
10199 ~CTL_FLAG_SENT_2OTHER_SC;
10201 if (pending_io->io_hdr.flags &
10202 CTL_FLAG_IO_ACTIVE) {
10203 pending_io->io_hdr.flags |=
10206 ctl_set_busy(&pending_io->scsiio);
10207 ctl_done_lock(pending_io,
10213 * Build Unit Attention
10215 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10216 lun->pending_sense[i].ua_pending |=
10217 CTL_UA_ASYM_ACC_CHANGE;
10219 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10220 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10221 printf("FAILOVER: secondary lun %d\n", lun_idx);
10223 * if the first io on the OOA is not on the RtR queue
10226 lun->flags |= CTL_LUN_PRIMARY_SC;
10228 pending_io = (union ctl_io *)TAILQ_FIRST(
10230 if (pending_io==NULL) {
10231 printf("Nothing on OOA queue\n");
10235 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10236 if ((pending_io->io_hdr.flags &
10237 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10238 pending_io->io_hdr.flags |=
10239 CTL_FLAG_IS_WAS_ON_RTR;
10240 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10241 &pending_io->io_hdr, links);
10246 printf("Tag 0x%04x is running\n",
10247 pending_io->scsiio.tag_num);
10251 next_io = (union ctl_io *)TAILQ_NEXT(
10252 &pending_io->io_hdr, ooa_links);
10253 for (pending_io=next_io; pending_io != NULL;
10254 pending_io = next_io) {
10255 pending_io->io_hdr.flags &=
10256 ~CTL_FLAG_SENT_2OTHER_SC;
10257 next_io = (union ctl_io *)TAILQ_NEXT(
10258 &pending_io->io_hdr, ooa_links);
10259 if (pending_io->io_hdr.flags &
10260 CTL_FLAG_IS_WAS_ON_RTR) {
10262 printf("Tag 0x%04x is running\n",
10263 pending_io->scsiio.tag_num);
10268 switch (ctl_check_ooa(lun, pending_io,
10269 (union ctl_io *)TAILQ_PREV(
10270 &pending_io->io_hdr, ctl_ooaq,
10273 case CTL_ACTION_BLOCK:
10274 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10275 &pending_io->io_hdr,
10277 pending_io->io_hdr.flags |=
10280 case CTL_ACTION_PASS:
10281 case CTL_ACTION_SKIP:
10282 pending_io->io_hdr.flags |=
10283 CTL_FLAG_IS_WAS_ON_RTR;
10284 STAILQ_INSERT_TAIL(
10285 &ctl_softc->rtr_queue,
10286 &pending_io->io_hdr, links);
10288 case CTL_ACTION_OVERLAP:
10289 ctl_set_overlapped_cmd(
10290 (struct ctl_scsiio *)pending_io);
10291 ctl_done_lock(pending_io,
10294 case CTL_ACTION_OVERLAP_TAG:
10295 ctl_set_overlapped_tag(
10296 (struct ctl_scsiio *)pending_io,
10297 pending_io->scsiio.tag_num & 0xff);
10298 ctl_done_lock(pending_io,
10301 case CTL_ACTION_ERROR:
10303 ctl_set_internal_failure(
10304 (struct ctl_scsiio *)pending_io,
10307 ctl_done_lock(pending_io,
10314 * Build Unit Attention
10316 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10317 lun->pending_sense[i].ua_pending |=
10318 CTL_UA_ASYM_ACC_CHANGE;
10321 panic("Unhandled HA mode failover, LUN flags = %#x, "
10322 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10326 mtx_unlock(&ctl_softc->ctl_lock);
10330 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10332 struct ctl_lun *lun;
10333 struct ctl_cmd_entry *entry;
10342 opcode = ctsio->cdb[0];
10344 mtx_lock(&ctl_softc->ctl_lock);
10346 if ((ctsio->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10347 && (ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun] != NULL)) {
10348 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
10350 * If the LUN is invalid, pretend that it doesn't exist.
10351 * It will go away as soon as all pending I/O has been
10354 if (lun->flags & CTL_LUN_DISABLED) {
10357 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10358 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10360 if (lun->be_lun->lun_type == T_PROCESSOR) {
10361 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10365 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10366 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10369 entry = &ctl_cmd_table[opcode];
10371 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10372 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10375 * Check to see whether we can send this command to LUNs that don't
10376 * exist. This should pretty much only be the case for inquiry
10377 * and request sense. Further checks, below, really require having
10378 * a LUN, so we can't really check the command anymore. Just put
10379 * it on the rtr queue.
10382 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10385 ctl_set_unsupported_lun(ctsio);
10386 mtx_unlock(&ctl_softc->ctl_lock);
10387 ctl_done((union ctl_io *)ctsio);
10391 * Every I/O goes into the OOA queue for a particular LUN, and
10392 * stays there until completion.
10394 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10397 * Make sure we support this particular command on this LUN.
10398 * e.g., we don't support writes to the control LUN.
10400 switch (lun->be_lun->lun_type) {
10402 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10403 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10405 ctl_set_invalid_opcode(ctsio);
10406 mtx_unlock(&ctl_softc->ctl_lock);
10407 ctl_done((union ctl_io *)ctsio);
10412 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10413 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10415 ctl_set_invalid_opcode(ctsio);
10416 mtx_unlock(&ctl_softc->ctl_lock);
10417 ctl_done((union ctl_io *)ctsio);
10422 printf("Unsupported CTL LUN type %d\n",
10423 lun->be_lun->lun_type);
10424 panic("Unsupported CTL LUN type %d\n",
10425 lun->be_lun->lun_type);
10426 break; /* NOTREACHED */
10430 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10433 * If we've got a request sense, it'll clear the contingent
10434 * allegiance condition. Otherwise, if we have a CA condition for
10435 * this initiator, clear it, because it sent down a command other
10436 * than request sense.
10438 if ((opcode != REQUEST_SENSE)
10439 && (ctl_is_set(lun->have_ca, initidx)))
10440 ctl_clear_mask(lun->have_ca, initidx);
10443 * If the command has this flag set, it handles its own unit
10444 * attention reporting, we shouldn't do anything. Otherwise we
10445 * check for any pending unit attentions, and send them back to the
10446 * initiator. We only do this when a command initially comes in,
10447 * not when we pull it off the blocked queue.
10449 * According to SAM-3, section 5.3.2, the order that things get
10450 * presented back to the host is basically unit attentions caused
10451 * by some sort of reset event, busy status, reservation conflicts
10452 * or task set full, and finally any other status.
10454 * One issue here is that some of the unit attentions we report
10455 * don't fall into the "reset" category (e.g. "reported luns data
10456 * has changed"). So reporting it here, before the reservation
10457 * check, may be technically wrong. I guess the only thing to do
10458 * would be to check for and report the reset events here, and then
10459 * check for the other unit attention types after we check for a
10460 * reservation conflict.
10462 * XXX KDM need to fix this
10464 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10465 ctl_ua_type ua_type;
10467 ua_type = lun->pending_sense[initidx].ua_pending;
10468 if (ua_type != CTL_UA_NONE) {
10469 scsi_sense_data_type sense_format;
10472 sense_format = (lun->flags &
10473 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10476 sense_format = SSD_TYPE_FIXED;
10478 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10480 if (ua_type != CTL_UA_NONE) {
10481 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10482 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10484 ctsio->sense_len = SSD_FULL_SIZE;
10485 lun->pending_sense[initidx].ua_pending &=
10487 mtx_unlock(&ctl_softc->ctl_lock);
10488 ctl_done((union ctl_io *)ctsio);
10495 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10496 mtx_unlock(&ctl_softc->ctl_lock);
10497 ctl_done((union ctl_io *)ctsio);
10502 * XXX CHD this is where we want to send IO to other side if
10503 * this LUN is secondary on this SC. We will need to make a copy
10504 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10505 * the copy we send as FROM_OTHER.
10506 * We also need to stuff the address of the original IO so we can
10507 * find it easily. Something similar will need be done on the other
10508 * side so when we are done we can find the copy.
10510 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10511 union ctl_ha_msg msg_info;
10514 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10516 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10517 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10519 printf("1. ctsio %p\n", ctsio);
10521 msg_info.hdr.serializing_sc = NULL;
10522 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10523 msg_info.scsi.tag_num = ctsio->tag_num;
10524 msg_info.scsi.tag_type = ctsio->tag_type;
10525 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10527 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10529 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10530 (void *)&msg_info, sizeof(msg_info), 0)) >
10531 CTL_HA_STATUS_SUCCESS) {
10532 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10534 printf("CTL:opcode is %x\n",opcode);
10537 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10542 * XXX KDM this I/O is off the incoming queue, but hasn't
10543 * been inserted on any other queue. We may need to come
10544 * up with a holding queue while we wait for serialization
10545 * so that we have an idea of what we're waiting for from
10548 goto bailout_unlock;
10551 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10552 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10553 ctl_ooaq, ooa_links))) {
10554 case CTL_ACTION_BLOCK:
10555 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10556 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10558 goto bailout_unlock;
10559 break; /* NOTREACHED */
10560 case CTL_ACTION_PASS:
10561 case CTL_ACTION_SKIP:
10563 break; /* NOTREACHED */
10564 case CTL_ACTION_OVERLAP:
10565 ctl_set_overlapped_cmd(ctsio);
10566 mtx_unlock(&ctl_softc->ctl_lock);
10567 ctl_done((union ctl_io *)ctsio);
10569 break; /* NOTREACHED */
10570 case CTL_ACTION_OVERLAP_TAG:
10571 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10572 mtx_unlock(&ctl_softc->ctl_lock);
10573 ctl_done((union ctl_io *)ctsio);
10575 break; /* NOTREACHED */
10576 case CTL_ACTION_ERROR:
10578 ctl_set_internal_failure(ctsio,
10580 /*retry_count*/ 0);
10581 mtx_unlock(&ctl_softc->ctl_lock);
10582 ctl_done((union ctl_io *)ctsio);
10584 break; /* NOTREACHED */
10587 goto bailout_unlock;
10590 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10591 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
10594 mtx_unlock(&ctl_softc->ctl_lock);
10601 ctl_scsiio(struct ctl_scsiio *ctsio)
10604 struct ctl_cmd_entry *entry;
10606 retval = CTL_RETVAL_COMPLETE;
10608 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
10610 entry = &ctl_cmd_table[ctsio->cdb[0]];
10613 * If this I/O has been aborted, just send it straight to
10614 * ctl_done() without executing it.
10616 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
10617 ctl_done((union ctl_io *)ctsio);
10622 * All the checks should have been handled by ctl_scsiio_precheck().
10623 * We should be clear now to just execute the I/O.
10625 retval = entry->execute(ctsio);
10632 * Since we only implement one target right now, a bus reset simply resets
10633 * our single target.
10636 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
10638 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
10642 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
10643 ctl_ua_type ua_type)
10645 struct ctl_lun *lun;
10648 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
10649 union ctl_ha_msg msg_info;
10651 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10652 msg_info.hdr.nexus = io->io_hdr.nexus;
10653 if (ua_type==CTL_UA_TARG_RESET)
10654 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
10656 msg_info.task.task_action = CTL_TASK_BUS_RESET;
10657 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
10658 msg_info.hdr.original_sc = NULL;
10659 msg_info.hdr.serializing_sc = NULL;
10660 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10661 (void *)&msg_info, sizeof(msg_info), 0)) {
10666 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
10667 retval += ctl_lun_reset(lun, io, ua_type);
10673 * The LUN should always be set. The I/O is optional, and is used to
10674 * distinguish between I/Os sent by this initiator, and by other
10675 * initiators. We set unit attention for initiators other than this one.
10676 * SAM-3 is vague on this point. It does say that a unit attention should
10677 * be established for other initiators when a LUN is reset (see section
10678 * 5.7.3), but it doesn't specifically say that the unit attention should
10679 * be established for this particular initiator when a LUN is reset. Here
10680 * is the relevant text, from SAM-3 rev 8:
10682 * 5.7.2 When a SCSI initiator port aborts its own tasks
10684 * When a SCSI initiator port causes its own task(s) to be aborted, no
10685 * notification that the task(s) have been aborted shall be returned to
10686 * the SCSI initiator port other than the completion response for the
10687 * command or task management function action that caused the task(s) to
10688 * be aborted and notification(s) associated with related effects of the
10689 * action (e.g., a reset unit attention condition).
10691 * XXX KDM for now, we're setting unit attention for all initiators.
10694 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
10698 uint32_t initindex;
10703 * Run through the OOA queue and abort each I/O.
10706 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10708 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10709 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10710 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10714 * This version sets unit attention for every
10717 initindex = ctl_get_initindex(&io->io_hdr.nexus);
10718 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10719 if (initindex == i)
10721 lun->pending_sense[i].ua_pending |= ua_type;
10726 * A reset (any kind, really) clears reservations established with
10727 * RESERVE/RELEASE. It does not clear reservations established
10728 * with PERSISTENT RESERVE OUT, but we don't support that at the
10729 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
10730 * reservations made with the RESERVE/RELEASE commands, because
10731 * those commands are obsolete in SPC-3.
10733 lun->flags &= ~CTL_LUN_RESERVED;
10735 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10736 ctl_clear_mask(lun->have_ca, i);
10737 lun->pending_sense[i].ua_pending |= ua_type;
10744 ctl_abort_task(union ctl_io *io)
10747 struct ctl_lun *lun;
10748 struct ctl_softc *ctl_softc;
10751 char printbuf[128];
10755 ctl_softc = control_softc;
10761 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10762 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
10763 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
10768 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
10769 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
10773 * Run through the OOA queue and attempt to find the given I/O.
10774 * The target port, initiator ID, tag type and tag number have to
10775 * match the values that we got from the initiator. If we have an
10776 * untagged command to abort, simply abort the first untagged command
10777 * we come to. We only allow one untagged command at a time of course.
10780 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10782 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10783 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10785 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
10787 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
10788 lun->lun, xio->scsiio.tag_num,
10789 xio->scsiio.tag_type,
10790 (xio->io_hdr.blocked_links.tqe_prev
10791 == NULL) ? "" : " BLOCKED",
10792 (xio->io_hdr.flags &
10793 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
10794 (xio->io_hdr.flags &
10795 CTL_FLAG_ABORT) ? " ABORT" : "",
10796 (xio->io_hdr.flags &
10797 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
10798 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
10800 printf("%s\n", sbuf_data(&sb));
10803 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
10804 && (xio->io_hdr.nexus.initid.id ==
10805 io->io_hdr.nexus.initid.id)) {
10807 * If the abort says that the task is untagged, the
10808 * task in the queue must be untagged. Otherwise,
10809 * we just check to see whether the tag numbers
10810 * match. This is because the QLogic firmware
10811 * doesn't pass back the tag type in an abort
10815 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
10816 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
10817 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
10820 * XXX KDM we've got problems with FC, because it
10821 * doesn't send down a tag type with aborts. So we
10822 * can only really go by the tag number...
10823 * This may cause problems with parallel SCSI.
10824 * Need to figure that out!!
10826 if (xio->scsiio.tag_num == io->taskio.tag_num) {
10827 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10829 if ((io->io_hdr.flags &
10830 CTL_FLAG_FROM_OTHER_SC) == 0 &&
10831 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
10832 union ctl_ha_msg msg_info;
10834 io->io_hdr.flags |=
10835 CTL_FLAG_SENT_2OTHER_SC;
10836 msg_info.hdr.nexus = io->io_hdr.nexus;
10837 msg_info.task.task_action =
10838 CTL_TASK_ABORT_TASK;
10839 msg_info.task.tag_num =
10840 io->taskio.tag_num;
10841 msg_info.task.tag_type =
10842 io->taskio.tag_type;
10843 msg_info.hdr.msg_type =
10844 CTL_MSG_MANAGE_TASKS;
10845 msg_info.hdr.original_sc = NULL;
10846 msg_info.hdr.serializing_sc = NULL;
10848 printf("Sent Abort to other side\n");
10850 if (CTL_HA_STATUS_SUCCESS !=
10851 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10853 sizeof(msg_info), 0)) {
10857 printf("ctl_abort_task: found I/O to abort\n");
10868 * This isn't really an error. It's entirely possible for
10869 * the abort and command completion to cross on the wire.
10870 * This is more of an informative/diagnostic error.
10873 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
10874 "%d:%d:%d:%d tag %d type %d\n",
10875 io->io_hdr.nexus.initid.id,
10876 io->io_hdr.nexus.targ_port,
10877 io->io_hdr.nexus.targ_target.id,
10878 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
10879 io->taskio.tag_type);
10887 * This routine cannot block! It must be callable from an interrupt
10888 * handler as well as from the work thread.
10891 ctl_run_task_queue(struct ctl_softc *ctl_softc)
10893 union ctl_io *io, *next_io;
10895 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
10897 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
10899 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
10900 io != NULL; io = next_io) {
10902 const char *task_desc;
10904 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10908 switch (io->io_hdr.io_type) {
10909 case CTL_IO_TASK: {
10910 task_desc = ctl_scsi_task_string(&io->taskio);
10911 if (task_desc != NULL) {
10913 csevent_log(CSC_CTL | CSC_SHELF_SW |
10915 csevent_LogType_Trace,
10916 csevent_Severity_Information,
10917 csevent_AlertLevel_Green,
10918 csevent_FRU_Firmware,
10919 csevent_FRU_Unknown,
10920 "CTL: received task: %s",task_desc);
10924 csevent_log(CSC_CTL | CSC_SHELF_SW |
10926 csevent_LogType_Trace,
10927 csevent_Severity_Information,
10928 csevent_AlertLevel_Green,
10929 csevent_FRU_Firmware,
10930 csevent_FRU_Unknown,
10931 "CTL: received unknown task "
10933 io->taskio.task_action,
10934 io->taskio.task_action);
10937 switch (io->taskio.task_action) {
10938 case CTL_TASK_ABORT_TASK:
10939 retval = ctl_abort_task(io);
10941 case CTL_TASK_ABORT_TASK_SET:
10943 case CTL_TASK_CLEAR_ACA:
10945 case CTL_TASK_CLEAR_TASK_SET:
10947 case CTL_TASK_LUN_RESET: {
10948 struct ctl_lun *lun;
10952 targ_lun = io->io_hdr.nexus.targ_lun;
10954 if ((targ_lun < CTL_MAX_LUNS)
10955 && (ctl_softc->ctl_luns[targ_lun] != NULL))
10956 lun = ctl_softc->ctl_luns[targ_lun];
10962 if (!(io->io_hdr.flags &
10963 CTL_FLAG_FROM_OTHER_SC)) {
10964 union ctl_ha_msg msg_info;
10966 io->io_hdr.flags |=
10967 CTL_FLAG_SENT_2OTHER_SC;
10968 msg_info.hdr.msg_type =
10969 CTL_MSG_MANAGE_TASKS;
10970 msg_info.hdr.nexus = io->io_hdr.nexus;
10971 msg_info.task.task_action =
10972 CTL_TASK_LUN_RESET;
10973 msg_info.hdr.original_sc = NULL;
10974 msg_info.hdr.serializing_sc = NULL;
10975 if (CTL_HA_STATUS_SUCCESS !=
10976 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10978 sizeof(msg_info), 0)) {
10982 retval = ctl_lun_reset(lun, io,
10986 case CTL_TASK_TARGET_RESET:
10987 retval = ctl_target_reset(ctl_softc, io,
10988 CTL_UA_TARG_RESET);
10990 case CTL_TASK_BUS_RESET:
10991 retval = ctl_bus_reset(ctl_softc, io);
10993 case CTL_TASK_PORT_LOGIN:
10995 case CTL_TASK_PORT_LOGOUT:
10998 printf("ctl_run_task_queue: got unknown task "
10999 "management event %d\n",
11000 io->taskio.task_action);
11004 io->io_hdr.status = CTL_SUCCESS;
11006 io->io_hdr.status = CTL_ERROR;
11008 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11009 ctl_io_hdr, links);
11011 * This will queue this I/O to the done queue, but the
11012 * work thread won't be able to process it until we
11013 * return and the lock is released.
11015 ctl_done_lock(io, /*have_lock*/ 1);
11020 printf("%s: invalid I/O type %d msg %d cdb %x"
11021 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11022 __func__, io->io_hdr.io_type,
11023 io->io_hdr.msg_type, io->scsiio.cdb[0],
11024 (uintmax_t)io->io_hdr.nexus.initid.id,
11025 io->io_hdr.nexus.targ_port,
11026 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11027 io->io_hdr.nexus.targ_lun,
11028 (io->io_hdr.io_type == CTL_IO_TASK) ?
11029 io->taskio.tag_num : io->scsiio.tag_num);
11030 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11031 ctl_io_hdr, links);
11038 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11042 * For HA operation. Handle commands that come in from the other
11046 ctl_handle_isc(union ctl_io *io)
11049 struct ctl_lun *lun;
11050 struct ctl_softc *ctl_softc;
11052 ctl_softc = control_softc;
11054 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
11056 switch (io->io_hdr.msg_type) {
11057 case CTL_MSG_SERIALIZE:
11058 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11061 case CTL_MSG_R2R: {
11063 struct ctl_cmd_entry *entry;
11066 * This is only used in SER_ONLY mode.
11069 opcode = io->scsiio.cdb[0];
11070 entry = &ctl_cmd_table[opcode];
11071 mtx_lock(&ctl_softc->ctl_lock);
11072 if (ctl_scsiio_lun_check(ctl_softc, lun,
11073 entry, (struct ctl_scsiio *)io) != 0) {
11074 ctl_done_lock(io, /*have_lock*/ 1);
11075 mtx_unlock(&ctl_softc->ctl_lock);
11078 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11079 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11080 &io->io_hdr, links);
11081 mtx_unlock(&ctl_softc->ctl_lock);
11084 case CTL_MSG_FINISH_IO:
11085 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11087 ctl_done_lock(io, /*have_lock*/ 0);
11090 mtx_lock(&ctl_softc->ctl_lock);
11091 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11093 STAILQ_REMOVE(&ctl_softc->task_queue,
11094 &io->io_hdr, ctl_io_hdr, links);
11095 ctl_check_blocked(lun);
11096 mtx_unlock(&ctl_softc->ctl_lock);
11099 case CTL_MSG_PERS_ACTION:
11100 ctl_hndl_per_res_out_on_other_sc(
11101 (union ctl_ha_msg *)&io->presio.pr_msg);
11104 case CTL_MSG_BAD_JUJU:
11106 ctl_done_lock(io, /*have_lock*/ 0);
11108 case CTL_MSG_DATAMOVE:
11109 /* Only used in XFER mode */
11111 ctl_datamove_remote(io);
11113 case CTL_MSG_DATAMOVE_DONE:
11114 /* Only used in XFER mode */
11116 io->scsiio.be_move_done(io);
11120 printf("%s: Invalid message type %d\n",
11121 __func__, io->io_hdr.msg_type);
11131 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11132 * there is no match.
11134 static ctl_lun_error_pattern
11135 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11137 struct ctl_cmd_entry *entry;
11138 ctl_lun_error_pattern filtered_pattern, pattern;
11141 pattern = desc->error_pattern;
11144 * XXX KDM we need more data passed into this function to match a
11145 * custom pattern, and we actually need to implement custom pattern
11148 if (pattern & CTL_LUN_PAT_CMD)
11149 return (CTL_LUN_PAT_CMD);
11151 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11152 return (CTL_LUN_PAT_ANY);
11154 opcode = ctsio->cdb[0];
11155 entry = &ctl_cmd_table[opcode];
11157 filtered_pattern = entry->pattern & pattern;
11160 * If the user requested specific flags in the pattern (e.g.
11161 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11164 * If the user did not specify any flags, it doesn't matter whether
11165 * or not the command supports the flags.
11167 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11168 (pattern & ~CTL_LUN_PAT_MASK))
11169 return (CTL_LUN_PAT_NONE);
11172 * If the user asked for a range check, see if the requested LBA
11173 * range overlaps with this command's LBA range.
11175 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11181 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11183 return (CTL_LUN_PAT_NONE);
11185 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11186 desc->lba_range.len);
11188 * A "pass" means that the LBA ranges don't overlap, so
11189 * this doesn't match the user's range criteria.
11191 if (action == CTL_ACTION_PASS)
11192 return (CTL_LUN_PAT_NONE);
11195 return (filtered_pattern);
11199 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11201 struct ctl_error_desc *desc, *desc2;
11203 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11205 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11206 ctl_lun_error_pattern pattern;
11208 * Check to see whether this particular command matches
11209 * the pattern in the descriptor.
11211 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11212 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11215 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11216 case CTL_LUN_INJ_ABORTED:
11217 ctl_set_aborted(&io->scsiio);
11219 case CTL_LUN_INJ_MEDIUM_ERR:
11220 ctl_set_medium_error(&io->scsiio);
11222 case CTL_LUN_INJ_UA:
11223 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11225 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11227 case CTL_LUN_INJ_CUSTOM:
11229 * We're assuming the user knows what he is doing.
11230 * Just copy the sense information without doing
11233 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11234 ctl_min(sizeof(desc->custom_sense),
11235 sizeof(io->scsiio.sense_data)));
11236 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11237 io->scsiio.sense_len = SSD_FULL_SIZE;
11238 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11240 case CTL_LUN_INJ_NONE:
11243 * If this is an error injection type we don't know
11244 * about, clear the continuous flag (if it is set)
11245 * so it will get deleted below.
11247 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11251 * By default, each error injection action is a one-shot
11253 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11256 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11262 #ifdef CTL_IO_DELAY
11264 ctl_datamove_timer_wakeup(void *arg)
11268 io = (union ctl_io *)arg;
11272 #endif /* CTL_IO_DELAY */
11275 ctl_datamove(union ctl_io *io)
11277 void (*fe_datamove)(union ctl_io *io);
11279 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11281 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11284 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11289 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11290 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11292 sbuf_cat(&sb, path_str);
11293 switch (io->io_hdr.io_type) {
11295 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11296 sbuf_printf(&sb, "\n");
11297 sbuf_cat(&sb, path_str);
11298 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11299 io->scsiio.tag_num, io->scsiio.tag_type);
11302 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11303 "Tag Type: %d\n", io->taskio.task_action,
11304 io->taskio.tag_num, io->taskio.tag_type);
11307 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11308 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11311 sbuf_cat(&sb, path_str);
11312 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11313 (intmax_t)time_uptime - io->io_hdr.start_time);
11315 printf("%s", sbuf_data(&sb));
11317 #endif /* CTL_TIME_IO */
11319 mtx_lock(&control_softc->ctl_lock);
11320 #ifdef CTL_IO_DELAY
11321 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11322 struct ctl_lun *lun;
11324 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11326 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11328 struct ctl_lun *lun;
11330 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11332 && (lun->delay_info.datamove_delay > 0)) {
11333 struct callout *callout;
11335 callout = (struct callout *)&io->io_hdr.timer_bytes;
11336 callout_init(callout, /*mpsafe*/ 1);
11337 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11338 callout_reset(callout,
11339 lun->delay_info.datamove_delay * hz,
11340 ctl_datamove_timer_wakeup, io);
11341 if (lun->delay_info.datamove_type ==
11342 CTL_DELAY_TYPE_ONESHOT)
11343 lun->delay_info.datamove_delay = 0;
11344 mtx_unlock(&control_softc->ctl_lock);
11350 * If we have any pending task management commands, process them
11351 * first. This is necessary to eliminate a race condition with the
11354 * - FETD submits a task management command, like an abort.
11355 * - Back end calls fe_datamove() to move the data for the aborted
11356 * command. The FETD can't really accept it, but if it did, it
11357 * would end up transmitting data for a command that the initiator
11358 * told us to abort.
11360 * We close the race by processing all pending task management
11361 * commands here (we can't block!), and then check this I/O to see
11362 * if it has been aborted. If so, return it to the back end with
11363 * bad status, so the back end can say return an error to the back end
11364 * and then when the back end returns an error, we can return the
11365 * aborted command to the FETD, so it can clean up its resources.
11367 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11368 ctl_run_task_queue(control_softc);
11371 * This command has been aborted. Set the port status, so we fail
11374 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11375 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11376 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11377 io->io_hdr.nexus.targ_port,
11378 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11379 io->io_hdr.nexus.targ_lun);
11380 io->io_hdr.status = CTL_CMD_ABORTED;
11381 io->io_hdr.port_status = 31337;
11382 mtx_unlock(&control_softc->ctl_lock);
11384 * Note that the backend, in this case, will get the
11385 * callback in its context. In other cases it may get
11386 * called in the frontend's interrupt thread context.
11388 io->scsiio.be_move_done(io);
11393 * If we're in XFER mode and this I/O is from the other shelf
11394 * controller, we need to send the DMA to the other side to
11395 * actually transfer the data to/from the host. In serialize only
11396 * mode the transfer happens below CTL and ctl_datamove() is only
11397 * called on the machine that originally received the I/O.
11399 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11400 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11401 union ctl_ha_msg msg;
11402 uint32_t sg_entries_sent;
11406 memset(&msg, 0, sizeof(msg));
11407 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11408 msg.hdr.original_sc = io->io_hdr.original_sc;
11409 msg.hdr.serializing_sc = io;
11410 msg.hdr.nexus = io->io_hdr.nexus;
11411 msg.dt.flags = io->io_hdr.flags;
11413 * We convert everything into a S/G list here. We can't
11414 * pass by reference, only by value between controllers.
11415 * So we can't pass a pointer to the S/G list, only as many
11416 * S/G entries as we can fit in here. If it's possible for
11417 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11418 * then we need to break this up into multiple transfers.
11420 if (io->scsiio.kern_sg_entries == 0) {
11421 msg.dt.kern_sg_entries = 1;
11423 * If this is in cached memory, flush the cache
11424 * before we send the DMA request to the other
11425 * controller. We want to do this in either the
11426 * read or the write case. The read case is
11427 * straightforward. In the write case, we want to
11428 * make sure nothing is in the local cache that
11429 * could overwrite the DMAed data.
11431 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11433 * XXX KDM use bus_dmamap_sync() here.
11438 * Convert to a physical address if this is a
11441 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11442 msg.dt.sg_list[0].addr =
11443 io->scsiio.kern_data_ptr;
11446 * XXX KDM use busdma here!
11449 msg.dt.sg_list[0].addr = (void *)
11450 vtophys(io->scsiio.kern_data_ptr);
11454 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11457 struct ctl_sg_entry *sgl;
11460 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11461 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11462 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11464 * XXX KDM use bus_dmamap_sync() here.
11469 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11470 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11471 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11472 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11473 msg.dt.sg_sequence = 0;
11476 * Loop until we've sent all of the S/G entries. On the
11477 * other end, we'll recompose these S/G entries into one
11478 * contiguous list before passing it to the
11480 for (sg_entries_sent = 0; sg_entries_sent <
11481 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11482 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11483 sizeof(msg.dt.sg_list[0])),
11484 msg.dt.kern_sg_entries - sg_entries_sent);
11486 if (do_sg_copy != 0) {
11487 struct ctl_sg_entry *sgl;
11490 sgl = (struct ctl_sg_entry *)
11491 io->scsiio.kern_data_ptr;
11493 * If this is in cached memory, flush the cache
11494 * before we send the DMA request to the other
11495 * controller. We want to do this in either
11496 * the * read or the write case. The read
11497 * case is straightforward. In the write
11498 * case, we want to make sure nothing is
11499 * in the local cache that could overwrite
11503 for (i = sg_entries_sent, j = 0;
11504 i < msg.dt.cur_sg_entries; i++, j++) {
11505 if ((io->io_hdr.flags &
11506 CTL_FLAG_NO_DATASYNC) == 0) {
11508 * XXX KDM use bus_dmamap_sync()
11511 if ((io->io_hdr.flags &
11512 CTL_FLAG_BUS_ADDR) == 0) {
11514 * XXX KDM use busdma.
11517 msg.dt.sg_list[j].addr =(void *)
11518 vtophys(sgl[i].addr);
11521 msg.dt.sg_list[j].addr =
11524 msg.dt.sg_list[j].len = sgl[i].len;
11528 sg_entries_sent += msg.dt.cur_sg_entries;
11529 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11530 msg.dt.sg_last = 1;
11532 msg.dt.sg_last = 0;
11535 * XXX KDM drop and reacquire the lock here?
11537 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11538 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11540 * XXX do something here.
11544 msg.dt.sent_sg_entries = sg_entries_sent;
11546 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11547 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11548 ctl_failover_io(io, /*have_lock*/ 1);
11553 * Lookup the fe_datamove() function for this particular
11557 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11558 mtx_unlock(&control_softc->ctl_lock);
11565 ctl_send_datamove_done(union ctl_io *io, int have_lock)
11567 union ctl_ha_msg msg;
11570 memset(&msg, 0, sizeof(msg));
11572 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11573 msg.hdr.original_sc = io;
11574 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11575 msg.hdr.nexus = io->io_hdr.nexus;
11576 msg.hdr.status = io->io_hdr.status;
11577 msg.scsi.tag_num = io->scsiio.tag_num;
11578 msg.scsi.tag_type = io->scsiio.tag_type;
11579 msg.scsi.scsi_status = io->scsiio.scsi_status;
11580 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11581 sizeof(io->scsiio.sense_data));
11582 msg.scsi.sense_len = io->scsiio.sense_len;
11583 msg.scsi.sense_residual = io->scsiio.sense_residual;
11584 msg.scsi.fetd_status = io->io_hdr.port_status;
11585 msg.scsi.residual = io->scsiio.residual;
11586 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11588 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
11589 ctl_failover_io(io, /*have_lock*/ have_lock);
11593 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
11594 if (isc_status > CTL_HA_STATUS_SUCCESS) {
11595 /* XXX do something if this fails */
11601 * The DMA to the remote side is done, now we need to tell the other side
11602 * we're done so it can continue with its data movement.
11605 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
11611 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11612 printf("%s: ISC DMA write failed with error %d", __func__,
11614 ctl_set_internal_failure(&io->scsiio,
11616 /*retry_count*/ rq->ret);
11619 ctl_dt_req_free(rq);
11622 * In this case, we had to malloc the memory locally. Free it.
11624 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11626 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11627 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11630 * The data is in local and remote memory, so now we need to send
11631 * status (good or back) back to the other side.
11633 ctl_send_datamove_done(io, /*have_lock*/ 0);
11637 * We've moved the data from the host/controller into local memory. Now we
11638 * need to push it over to the remote controller's memory.
11641 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
11647 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
11648 ctl_datamove_remote_write_cb);
11654 ctl_datamove_remote_write(union ctl_io *io)
11657 void (*fe_datamove)(union ctl_io *io);
11660 * - Get the data from the host/HBA into local memory.
11661 * - DMA memory from the local controller to the remote controller.
11662 * - Send status back to the remote controller.
11665 retval = ctl_datamove_remote_sgl_setup(io);
11669 /* Switch the pointer over so the FETD knows what to do */
11670 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11673 * Use a custom move done callback, since we need to send completion
11674 * back to the other controller, not to the backend on this side.
11676 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
11678 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11687 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
11696 * In this case, we had to malloc the memory locally. Free it.
11698 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11700 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11701 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11705 scsi_path_string(io, path_str, sizeof(path_str));
11706 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11707 sbuf_cat(&sb, path_str);
11708 scsi_command_string(&io->scsiio, NULL, &sb);
11709 sbuf_printf(&sb, "\n");
11710 sbuf_cat(&sb, path_str);
11711 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11712 io->scsiio.tag_num, io->scsiio.tag_type);
11713 sbuf_cat(&sb, path_str);
11714 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
11715 io->io_hdr.flags, io->io_hdr.status);
11717 printk("%s", sbuf_data(&sb));
11722 * The read is done, now we need to send status (good or bad) back
11723 * to the other side.
11725 ctl_send_datamove_done(io, /*have_lock*/ 0);
11731 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
11734 void (*fe_datamove)(union ctl_io *io);
11738 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11739 printf("%s: ISC DMA read failed with error %d", __func__,
11741 ctl_set_internal_failure(&io->scsiio,
11743 /*retry_count*/ rq->ret);
11746 ctl_dt_req_free(rq);
11748 /* Switch the pointer over so the FETD knows what to do */
11749 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11752 * Use a custom move done callback, since we need to send completion
11753 * back to the other controller, not to the backend on this side.
11755 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
11757 /* XXX KDM add checks like the ones in ctl_datamove? */
11759 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11765 ctl_datamove_remote_sgl_setup(union ctl_io *io)
11767 struct ctl_sg_entry *local_sglist, *remote_sglist;
11768 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
11769 struct ctl_softc *softc;
11774 softc = control_softc;
11776 local_sglist = io->io_hdr.local_sglist;
11777 local_dma_sglist = io->io_hdr.local_dma_sglist;
11778 remote_sglist = io->io_hdr.remote_sglist;
11779 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11781 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
11782 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
11783 local_sglist[i].len = remote_sglist[i].len;
11786 * XXX Detect the situation where the RS-level I/O
11787 * redirector on the other side has already read the
11788 * data off of the AOR RS on this side, and
11789 * transferred it to remote (mirror) memory on the
11790 * other side. Since we already have the data in
11791 * memory here, we just need to use it.
11793 * XXX KDM this can probably be removed once we
11794 * get the cache device code in and take the
11795 * current AOR implementation out.
11798 if ((remote_sglist[i].addr >=
11799 (void *)vtophys(softc->mirr->addr))
11800 && (remote_sglist[i].addr <
11801 ((void *)vtophys(softc->mirr->addr) +
11802 CacheMirrorOffset))) {
11803 local_sglist[i].addr = remote_sglist[i].addr -
11805 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
11807 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
11809 local_sglist[i].addr = remote_sglist[i].addr +
11814 printf("%s: local %p, remote %p, len %d\n",
11815 __func__, local_sglist[i].addr,
11816 remote_sglist[i].addr, local_sglist[i].len);
11820 uint32_t len_to_go;
11823 * In this case, we don't have automatically allocated
11824 * memory for this I/O on this controller. This typically
11825 * happens with internal CTL I/O -- e.g. inquiry, mode
11826 * sense, etc. Anything coming from RAIDCore will have
11827 * a mirror area available.
11829 len_to_go = io->scsiio.kern_data_len;
11832 * Clear the no datasync flag, we have to use malloced
11835 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
11838 * The difficult thing here is that the size of the various
11839 * S/G segments may be different than the size from the
11840 * remote controller. That'll make it harder when DMAing
11841 * the data back to the other side.
11843 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
11844 sizeof(io->io_hdr.remote_sglist[0])) &&
11845 (len_to_go > 0); i++) {
11846 local_sglist[i].len = ctl_min(len_to_go, 131072);
11847 CTL_SIZE_8B(local_dma_sglist[i].len,
11848 local_sglist[i].len);
11849 local_sglist[i].addr =
11850 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
11852 local_dma_sglist[i].addr = local_sglist[i].addr;
11854 if (local_sglist[i].addr == NULL) {
11857 printf("malloc failed for %zd bytes!",
11858 local_dma_sglist[i].len);
11859 for (j = 0; j < i; j++) {
11860 free(local_sglist[j].addr, M_CTL);
11862 ctl_set_internal_failure(&io->scsiio,
11864 /*retry_count*/ 4857);
11866 goto bailout_error;
11869 /* XXX KDM do we need a sync here? */
11871 len_to_go -= local_sglist[i].len;
11874 * Reset the number of S/G entries accordingly. The
11875 * original number of S/G entries is available in
11878 io->scsiio.kern_sg_entries = i;
11881 printf("%s: kern_sg_entries = %d\n", __func__,
11882 io->scsiio.kern_sg_entries);
11883 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11884 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
11885 local_sglist[i].addr, local_sglist[i].len,
11886 local_dma_sglist[i].len);
11895 ctl_send_datamove_done(io, /*have_lock*/ 0);
11901 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
11902 ctl_ha_dt_cb callback)
11904 struct ctl_ha_dt_req *rq;
11905 struct ctl_sg_entry *remote_sglist, *local_sglist;
11906 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
11907 uint32_t local_used, remote_used, total_used;
11913 rq = ctl_dt_req_alloc();
11916 * If we failed to allocate the request, and if the DMA didn't fail
11917 * anyway, set busy status. This is just a resource allocation
11921 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
11922 ctl_set_busy(&io->scsiio);
11924 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
11927 ctl_dt_req_free(rq);
11930 * The data move failed. We need to return status back
11931 * to the other controller. No point in trying to DMA
11932 * data to the remote controller.
11935 ctl_send_datamove_done(io, /*have_lock*/ 0);
11942 local_sglist = io->io_hdr.local_sglist;
11943 local_dma_sglist = io->io_hdr.local_dma_sglist;
11944 remote_sglist = io->io_hdr.remote_sglist;
11945 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11950 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
11951 rq->ret = CTL_HA_STATUS_SUCCESS;
11958 * Pull/push the data over the wire from/to the other controller.
11959 * This takes into account the possibility that the local and
11960 * remote sglists may not be identical in terms of the size of
11961 * the elements and the number of elements.
11963 * One fundamental assumption here is that the length allocated for
11964 * both the local and remote sglists is identical. Otherwise, we've
11965 * essentially got a coding error of some sort.
11967 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
11969 uint32_t cur_len, dma_length;
11972 rq->id = CTL_HA_DATA_CTL;
11973 rq->command = command;
11977 * Both pointers should be aligned. But it is possible
11978 * that the allocation length is not. They should both
11979 * also have enough slack left over at the end, though,
11980 * to round up to the next 8 byte boundary.
11982 cur_len = ctl_min(local_sglist[i].len - local_used,
11983 remote_sglist[j].len - remote_used);
11986 * In this case, we have a size issue and need to decrease
11987 * the size, except in the case where we actually have less
11988 * than 8 bytes left. In that case, we need to increase
11989 * the DMA length to get the last bit.
11991 if ((cur_len & 0x7) != 0) {
11992 if (cur_len > 0x7) {
11993 cur_len = cur_len - (cur_len & 0x7);
11994 dma_length = cur_len;
11996 CTL_SIZE_8B(dma_length, cur_len);
12000 dma_length = cur_len;
12003 * If we had to allocate memory for this I/O, instead of using
12004 * the non-cached mirror memory, we'll need to flush the cache
12005 * before trying to DMA to the other controller.
12007 * We could end up doing this multiple times for the same
12008 * segment if we have a larger local segment than remote
12009 * segment. That shouldn't be an issue.
12011 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12013 * XXX KDM use bus_dmamap_sync() here.
12017 rq->size = dma_length;
12019 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12020 tmp_ptr += local_used;
12022 /* Use physical addresses when talking to ISC hardware */
12023 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12024 /* XXX KDM use busdma */
12026 rq->local = vtophys(tmp_ptr);
12029 rq->local = tmp_ptr;
12031 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12032 tmp_ptr += remote_used;
12033 rq->remote = tmp_ptr;
12035 rq->callback = NULL;
12037 local_used += cur_len;
12038 if (local_used >= local_sglist[i].len) {
12043 remote_used += cur_len;
12044 if (remote_used >= remote_sglist[j].len) {
12048 total_used += cur_len;
12050 if (total_used >= io->scsiio.kern_data_len)
12051 rq->callback = callback;
12053 if ((rq->size & 0x7) != 0) {
12054 printf("%s: warning: size %d is not on 8b boundary\n",
12055 __func__, rq->size);
12057 if (((uintptr_t)rq->local & 0x7) != 0) {
12058 printf("%s: warning: local %p not on 8b boundary\n",
12059 __func__, rq->local);
12061 if (((uintptr_t)rq->remote & 0x7) != 0) {
12062 printf("%s: warning: remote %p not on 8b boundary\n",
12063 __func__, rq->local);
12066 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12067 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12068 rq->local, rq->remote, rq->size);
12071 isc_ret = ctl_dt_single(rq);
12072 if (isc_ret == CTL_HA_STATUS_WAIT)
12075 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12076 rq->ret = CTL_HA_STATUS_SUCCESS;
12090 ctl_datamove_remote_read(union ctl_io *io)
12096 * This will send an error to the other controller in the case of a
12099 retval = ctl_datamove_remote_sgl_setup(io);
12103 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12104 ctl_datamove_remote_read_cb);
12106 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12108 * Make sure we free memory if there was an error.. The
12109 * ctl_datamove_remote_xfer() function will send the
12110 * datamove done message, or call the callback with an
12111 * error if there is a problem.
12113 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12114 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12121 * Process a datamove request from the other controller. This is used for
12122 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12123 * first. Once that is complete, the data gets DMAed into the remote
12124 * controller's memory. For reads, we DMA from the remote controller's
12125 * memory into our memory first, and then move it out to the FETD.
12128 ctl_datamove_remote(union ctl_io *io)
12130 struct ctl_softc *softc;
12132 softc = control_softc;
12134 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12137 * Note that we look for an aborted I/O here, but don't do some of
12138 * the other checks that ctl_datamove() normally does. We don't
12139 * need to run the task queue, because this I/O is on the ISC
12140 * queue, which is executed by the work thread after the task queue.
12141 * We don't need to run the datamove delay code, since that should
12142 * have been done if need be on the other controller.
12144 mtx_lock(&softc->ctl_lock);
12146 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12148 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12149 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12150 io->io_hdr.nexus.targ_port,
12151 io->io_hdr.nexus.targ_target.id,
12152 io->io_hdr.nexus.targ_lun);
12153 io->io_hdr.status = CTL_CMD_ABORTED;
12154 io->io_hdr.port_status = 31338;
12156 mtx_unlock(&softc->ctl_lock);
12158 ctl_send_datamove_done(io, /*have_lock*/ 0);
12163 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12164 mtx_unlock(&softc->ctl_lock);
12165 ctl_datamove_remote_write(io);
12166 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12167 mtx_unlock(&softc->ctl_lock);
12168 ctl_datamove_remote_read(io);
12170 union ctl_ha_msg msg;
12171 struct scsi_sense_data *sense;
12175 memset(&msg, 0, sizeof(msg));
12177 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12178 msg.hdr.status = CTL_SCSI_ERROR;
12179 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12181 retry_count = 4243;
12183 sense = &msg.scsi.sense_data;
12184 sks[0] = SSD_SCS_VALID;
12185 sks[1] = (retry_count >> 8) & 0xff;
12186 sks[2] = retry_count & 0xff;
12188 /* "Internal target failure" */
12189 scsi_set_sense_data(sense,
12190 /*sense_format*/ SSD_TYPE_NONE,
12191 /*current_error*/ 1,
12192 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12195 /*type*/ SSD_ELEM_SKS,
12196 /*size*/ sizeof(sks),
12200 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12201 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12202 ctl_failover_io(io, /*have_lock*/ 1);
12203 mtx_unlock(&softc->ctl_lock);
12207 mtx_unlock(&softc->ctl_lock);
12209 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12210 CTL_HA_STATUS_SUCCESS) {
12211 /* XXX KDM what to do if this fails? */
12219 ctl_process_done(union ctl_io *io, int have_lock)
12221 struct ctl_lun *lun;
12222 struct ctl_softc *ctl_softc;
12223 void (*fe_done)(union ctl_io *io);
12224 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12226 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12229 control_softc->ctl_ports[targ_port]->fe_done;
12232 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12237 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12238 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12240 sbuf_cat(&sb, path_str);
12241 switch (io->io_hdr.io_type) {
12243 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12244 sbuf_printf(&sb, "\n");
12245 sbuf_cat(&sb, path_str);
12246 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12247 io->scsiio.tag_num, io->scsiio.tag_type);
12250 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12251 "Tag Type: %d\n", io->taskio.task_action,
12252 io->taskio.tag_num, io->taskio.tag_type);
12255 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12256 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12259 sbuf_cat(&sb, path_str);
12260 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12261 (intmax_t)time_uptime - io->io_hdr.start_time);
12263 printf("%s", sbuf_data(&sb));
12265 #endif /* CTL_TIME_IO */
12267 switch (io->io_hdr.io_type) {
12271 if (bootverbose || verbose > 0)
12272 ctl_io_error_print(io, NULL);
12273 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12277 return (CTL_RETVAL_COMPLETE);
12280 printf("ctl_process_done: invalid io type %d\n",
12281 io->io_hdr.io_type);
12282 panic("ctl_process_done: invalid io type %d\n",
12283 io->io_hdr.io_type);
12284 break; /* NOTREACHED */
12287 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12289 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12290 io->io_hdr.nexus.targ_lun));
12294 ctl_softc = lun->ctl_softc;
12297 * Remove this from the OOA queue.
12299 if (have_lock == 0)
12300 mtx_lock(&ctl_softc->ctl_lock);
12303 * Check to see if we have any errors to inject here. We only
12304 * inject errors for commands that don't already have errors set.
12306 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12307 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12308 ctl_inject_error(lun, io);
12311 * XXX KDM how do we treat commands that aren't completed
12314 * XXX KDM should we also track I/O latency?
12316 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12317 uint32_t blocksize;
12319 struct bintime cur_bt;
12322 if ((lun->be_lun != NULL)
12323 && (lun->be_lun->blocksize != 0))
12324 blocksize = lun->be_lun->blocksize;
12328 switch (io->io_hdr.io_type) {
12329 case CTL_IO_SCSI: {
12331 struct ctl_lba_len lbalen;
12334 switch (io->scsiio.cdb[0]) {
12345 case WRITE_VERIFY_10:
12346 case WRITE_VERIFY_12:
12347 case WRITE_VERIFY_16:
12348 memcpy(&lbalen, io->io_hdr.ctl_private[
12349 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12352 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12353 lbalen.len * blocksize;
12354 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12358 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12359 &io->io_hdr.dma_bt);
12360 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12361 io->io_hdr.num_dmas;
12362 getbintime(&cur_bt);
12363 bintime_sub(&cur_bt,
12364 &io->io_hdr.start_bt);
12367 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12371 cs_prof_gettime(&cur_ticks);
12372 lun->stats.time[CTL_STATS_READ] +=
12374 io->io_hdr.start_ticks;
12377 lun->stats.time[CTL_STATS_READ] +=
12378 jiffies - io->io_hdr.start_time;
12380 #endif /* CTL_TIME_IO */
12382 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12383 lbalen.len * blocksize;
12384 lun->stats.ports[targ_port].operations[
12385 CTL_STATS_WRITE]++;
12389 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12390 &io->io_hdr.dma_bt);
12391 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12392 io->io_hdr.num_dmas;
12393 getbintime(&cur_bt);
12394 bintime_sub(&cur_bt,
12395 &io->io_hdr.start_bt);
12398 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12401 cs_prof_gettime(&cur_ticks);
12402 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12404 io->io_hdr.start_ticks;
12405 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12406 jiffies - io->io_hdr.start_time;
12408 #endif /* CTL_TIME_IO */
12412 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12416 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12417 &io->io_hdr.dma_bt);
12418 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12419 io->io_hdr.num_dmas;
12420 getbintime(&cur_bt);
12421 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12423 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12427 cs_prof_gettime(&cur_ticks);
12428 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12430 io->io_hdr.start_ticks;
12431 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12432 jiffies - io->io_hdr.start_time;
12434 #endif /* CTL_TIME_IO */
12444 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12447 * Run through the blocked queue on this LUN and see if anything
12448 * has become unblocked, now that this transaction is done.
12450 ctl_check_blocked(lun);
12453 * If the LUN has been invalidated, free it if there is nothing
12454 * left on its OOA queue.
12456 if ((lun->flags & CTL_LUN_INVALID)
12457 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12461 * If this command has been aborted, make sure we set the status
12462 * properly. The FETD is responsible for freeing the I/O and doing
12463 * whatever it needs to do to clean up its state.
12465 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12466 io->io_hdr.status = CTL_CMD_ABORTED;
12469 * We print out status for every task management command. For SCSI
12470 * commands, we filter out any unit attention errors; they happen
12471 * on every boot, and would clutter up the log. Note: task
12472 * management commands aren't printed here, they are printed above,
12473 * since they should never even make it down here.
12475 switch (io->io_hdr.io_type) {
12476 case CTL_IO_SCSI: {
12477 int error_code, sense_key, asc, ascq;
12481 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12482 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12484 * Since this is just for printing, no need to
12485 * show errors here.
12487 scsi_extract_sense_len(&io->scsiio.sense_data,
12488 io->scsiio.sense_len,
12493 /*show_errors*/ 0);
12496 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12497 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12498 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12499 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12501 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12502 ctl_softc->skipped_prints++;
12503 if (have_lock == 0)
12504 mtx_unlock(&ctl_softc->ctl_lock);
12506 uint32_t skipped_prints;
12508 skipped_prints = ctl_softc->skipped_prints;
12510 ctl_softc->skipped_prints = 0;
12511 ctl_softc->last_print_jiffies = time_uptime;
12513 if (have_lock == 0)
12514 mtx_unlock(&ctl_softc->ctl_lock);
12515 if (skipped_prints > 0) {
12517 csevent_log(CSC_CTL | CSC_SHELF_SW |
12519 csevent_LogType_Trace,
12520 csevent_Severity_Information,
12521 csevent_AlertLevel_Green,
12522 csevent_FRU_Firmware,
12523 csevent_FRU_Unknown,
12524 "High CTL error volume, %d prints "
12525 "skipped", skipped_prints);
12528 if (bootverbose || verbose > 0)
12529 ctl_io_error_print(io, NULL);
12532 if (have_lock == 0)
12533 mtx_unlock(&ctl_softc->ctl_lock);
12538 if (have_lock == 0)
12539 mtx_unlock(&ctl_softc->ctl_lock);
12540 if (bootverbose || verbose > 0)
12541 ctl_io_error_print(io, NULL);
12544 if (have_lock == 0)
12545 mtx_unlock(&ctl_softc->ctl_lock);
12550 * Tell the FETD or the other shelf controller we're done with this
12551 * command. Note that only SCSI commands get to this point. Task
12552 * management commands are completed above.
12554 * We only send status to the other controller if we're in XFER
12555 * mode. In SER_ONLY mode, the I/O is done on the controller that
12556 * received the I/O (from CTL's perspective), and so the status is
12559 * XXX KDM if we hold the lock here, we could cause a deadlock
12560 * if the frontend comes back in in this context to queue
12563 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12564 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12565 union ctl_ha_msg msg;
12567 memset(&msg, 0, sizeof(msg));
12568 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12569 msg.hdr.original_sc = io->io_hdr.original_sc;
12570 msg.hdr.nexus = io->io_hdr.nexus;
12571 msg.hdr.status = io->io_hdr.status;
12572 msg.scsi.scsi_status = io->scsiio.scsi_status;
12573 msg.scsi.tag_num = io->scsiio.tag_num;
12574 msg.scsi.tag_type = io->scsiio.tag_type;
12575 msg.scsi.sense_len = io->scsiio.sense_len;
12576 msg.scsi.sense_residual = io->scsiio.sense_residual;
12577 msg.scsi.residual = io->scsiio.residual;
12578 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12579 sizeof(io->scsiio.sense_data));
12581 * We copy this whether or not this is an I/O-related
12582 * command. Otherwise, we'd have to go and check to see
12583 * whether it's a read/write command, and it really isn't
12586 memcpy(&msg.scsi.lbalen,
12587 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12588 sizeof(msg.scsi.lbalen));
12590 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12591 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12592 /* XXX do something here */
12601 return (CTL_RETVAL_COMPLETE);
12605 * Front end should call this if it doesn't do autosense. When the request
12606 * sense comes back in from the initiator, we'll dequeue this and send it.
12609 ctl_queue_sense(union ctl_io *io)
12611 struct ctl_lun *lun;
12612 struct ctl_softc *ctl_softc;
12615 ctl_softc = control_softc;
12617 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12620 * LUN lookup will likely move to the ctl_work_thread() once we
12621 * have our new queueing infrastructure (that doesn't put things on
12622 * a per-LUN queue initially). That is so that we can handle
12623 * things like an INQUIRY to a LUN that we don't have enabled. We
12624 * can't deal with that right now.
12626 mtx_lock(&ctl_softc->ctl_lock);
12629 * If we don't have a LUN for this, just toss the sense
12632 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
12633 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
12634 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
12638 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12641 * Already have CA set for this LUN...toss the sense information.
12643 if (ctl_is_set(lun->have_ca, initidx))
12646 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
12647 ctl_min(sizeof(lun->pending_sense[initidx].sense),
12648 sizeof(io->scsiio.sense_data)));
12649 ctl_set_mask(lun->have_ca, initidx);
12652 mtx_unlock(&ctl_softc->ctl_lock);
12656 return (CTL_RETVAL_COMPLETE);
12660 * Primary command inlet from frontend ports. All SCSI and task I/O
12661 * requests must go through this function.
12664 ctl_queue(union ctl_io *io)
12666 struct ctl_softc *ctl_softc;
12668 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12670 ctl_softc = control_softc;
12673 io->io_hdr.start_time = time_uptime;
12674 getbintime(&io->io_hdr.start_bt);
12675 #endif /* CTL_TIME_IO */
12677 mtx_lock(&ctl_softc->ctl_lock);
12679 switch (io->io_hdr.io_type) {
12681 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
12685 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
12687 * Set the task pending flag. This is necessary to close a
12688 * race condition with the FETD:
12690 * - FETD submits a task management command, like an abort.
12691 * - Back end calls fe_datamove() to move the data for the
12692 * aborted command. The FETD can't really accept it, but
12693 * if it did, it would end up transmitting data for a
12694 * command that the initiator told us to abort.
12696 * We close the race condition by setting the flag here,
12697 * and checking it in ctl_datamove(), before calling the
12698 * FETD's fe_datamove routine. If we've got a task
12699 * pending, we run the task queue and then check to see
12700 * whether our particular I/O has been aborted.
12702 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
12705 mtx_unlock(&ctl_softc->ctl_lock);
12706 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12708 break; /* NOTREACHED */
12710 mtx_unlock(&ctl_softc->ctl_lock);
12712 ctl_wakeup_thread();
12714 return (CTL_RETVAL_COMPLETE);
12717 #ifdef CTL_IO_DELAY
12719 ctl_done_timer_wakeup(void *arg)
12723 io = (union ctl_io *)arg;
12724 ctl_done_lock(io, /*have_lock*/ 0);
12726 #endif /* CTL_IO_DELAY */
12729 ctl_done_lock(union ctl_io *io, int have_lock)
12731 struct ctl_softc *ctl_softc;
12732 #ifndef CTL_DONE_THREAD
12734 #endif /* !CTL_DONE_THREAD */
12736 ctl_softc = control_softc;
12738 if (have_lock == 0)
12739 mtx_lock(&ctl_softc->ctl_lock);
12742 * Enable this to catch duplicate completion issues.
12745 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12746 printf("%s: type %d msg %d cdb %x iptl: "
12747 "%d:%d:%d:%d tag 0x%04x "
12748 "flag %#x status %x\n",
12750 io->io_hdr.io_type,
12751 io->io_hdr.msg_type,
12753 io->io_hdr.nexus.initid.id,
12754 io->io_hdr.nexus.targ_port,
12755 io->io_hdr.nexus.targ_target.id,
12756 io->io_hdr.nexus.targ_lun,
12757 (io->io_hdr.io_type ==
12759 io->taskio.tag_num :
12760 io->scsiio.tag_num,
12762 io->io_hdr.status);
12764 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12768 * This is an internal copy of an I/O, and should not go through
12769 * the normal done processing logic.
12771 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
12772 if (have_lock == 0)
12773 mtx_unlock(&ctl_softc->ctl_lock);
12778 * We need to send a msg to the serializing shelf to finish the IO
12779 * as well. We don't send a finish message to the other shelf if
12780 * this is a task management command. Task management commands
12781 * aren't serialized in the OOA queue, but rather just executed on
12782 * both shelf controllers for commands that originated on that
12785 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
12786 && (io->io_hdr.io_type != CTL_IO_TASK)) {
12787 union ctl_ha_msg msg_io;
12789 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
12790 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
12791 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
12792 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
12794 /* continue on to finish IO */
12796 #ifdef CTL_IO_DELAY
12797 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12798 struct ctl_lun *lun;
12800 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12802 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12804 struct ctl_lun *lun;
12806 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12809 && (lun->delay_info.done_delay > 0)) {
12810 struct callout *callout;
12812 callout = (struct callout *)&io->io_hdr.timer_bytes;
12813 callout_init(callout, /*mpsafe*/ 1);
12814 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12815 callout_reset(callout,
12816 lun->delay_info.done_delay * hz,
12817 ctl_done_timer_wakeup, io);
12818 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
12819 lun->delay_info.done_delay = 0;
12820 if (have_lock == 0)
12821 mtx_unlock(&ctl_softc->ctl_lock);
12825 #endif /* CTL_IO_DELAY */
12827 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
12829 #ifdef CTL_DONE_THREAD
12830 if (have_lock == 0)
12831 mtx_unlock(&ctl_softc->ctl_lock);
12833 ctl_wakeup_thread();
12834 #else /* CTL_DONE_THREAD */
12835 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
12837 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
12839 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
12841 ctl_process_done(xio, /*have_lock*/ 1);
12843 if (have_lock == 0)
12844 mtx_unlock(&ctl_softc->ctl_lock);
12845 #endif /* CTL_DONE_THREAD */
12849 ctl_done(union ctl_io *io)
12851 ctl_done_lock(io, /*have_lock*/ 0);
12855 ctl_isc(struct ctl_scsiio *ctsio)
12857 struct ctl_lun *lun;
12860 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12862 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
12864 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
12866 retval = lun->backend->data_submit((union ctl_io *)ctsio);
12873 ctl_work_thread(void *arg)
12875 struct ctl_softc *softc;
12877 struct ctl_be_lun *be_lun;
12880 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
12882 softc = (struct ctl_softc *)arg;
12886 mtx_lock(&softc->ctl_lock);
12891 * We handle the queues in this order:
12892 * - task management
12894 * - done queue (to free up resources, unblock other commands)
12898 * If those queues are empty, we break out of the loop and
12901 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
12903 ctl_run_task_queue(softc);
12906 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
12908 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
12909 ctl_handle_isc(io);
12912 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
12914 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
12915 /* clear any blocked commands, call fe_done */
12916 mtx_unlock(&softc->ctl_lock);
12919 * Call this without a lock for now. This will
12920 * depend on whether there is any way the FETD can
12921 * sleep or deadlock if called with the CTL lock
12924 retval = ctl_process_done(io, /*have_lock*/ 0);
12925 mtx_lock(&softc->ctl_lock);
12928 if (!ctl_pause_rtr) {
12929 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
12931 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
12932 mtx_unlock(&softc->ctl_lock);
12936 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
12938 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
12939 mtx_unlock(&softc->ctl_lock);
12940 ctl_scsiio_precheck(softc, &io->scsiio);
12941 mtx_lock(&softc->ctl_lock);
12945 * We might want to move this to a separate thread, so that
12946 * configuration requests (in this case LUN creations)
12947 * won't impact the I/O path.
12949 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
12950 if (be_lun != NULL) {
12951 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
12952 mtx_unlock(&softc->ctl_lock);
12953 ctl_create_lun(be_lun);
12954 mtx_lock(&softc->ctl_lock);
12958 /* XXX KDM use the PDROP flag?? */
12959 /* Sleep until we have something to do. */
12960 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
12962 /* Back to the top of the loop to see what woke us up. */
12966 retval = ctl_scsiio(&io->scsiio);
12968 case CTL_RETVAL_COMPLETE:
12972 * Probably need to make sure this doesn't happen.
12976 mtx_lock(&softc->ctl_lock);
12981 ctl_wakeup_thread()
12983 struct ctl_softc *softc;
12985 softc = control_softc;
12990 /* Initialization and failover */
12993 ctl_init_isc_msg(void)
12995 printf("CTL: Still calling this thing\n");
13000 * Initializes component into configuration defined by bootMode
13002 * returns hasc_Status:
13004 * ERROR - fatal error
13006 static ctl_ha_comp_status
13007 ctl_isc_init(struct ctl_ha_component *c)
13009 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13016 * Starts component in state requested. If component starts successfully,
13017 * it must set its own state to the requestrd state
13018 * When requested state is HASC_STATE_HA, the component may refine it
13019 * by adding _SLAVE or _MASTER flags.
13020 * Currently allowed state transitions are:
13021 * UNKNOWN->HA - initial startup
13022 * UNKNOWN->SINGLE - initial startup when no parter detected
13023 * HA->SINGLE - failover
13024 * returns ctl_ha_comp_status:
13025 * OK - component successfully started in requested state
13026 * FAILED - could not start the requested state, failover may
13028 * ERROR - fatal error detected, no future startup possible
13030 static ctl_ha_comp_status
13031 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13033 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13035 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13036 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13038 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13039 != CTL_HA_STATUS_SUCCESS) {
13040 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13041 ret = CTL_HA_COMP_STATUS_ERROR;
13043 } else if (CTL_HA_STATE_IS_HA(c->state)
13044 && CTL_HA_STATE_IS_SINGLE(state)){
13045 // HA->SINGLE transition
13049 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13051 ret = CTL_HA_COMP_STATUS_ERROR;
13053 if (CTL_HA_STATE_IS_SINGLE(state))
13062 * Quiesce component
13063 * The component must clear any error conditions (set status to OK) and
13064 * prepare itself to another Start call
13065 * returns ctl_ha_comp_status:
13069 static ctl_ha_comp_status
13070 ctl_isc_quiesce(struct ctl_ha_component *c)
13072 int ret = CTL_HA_COMP_STATUS_OK;
13079 struct ctl_ha_component ctl_ha_component_ctlisc =
13082 .state = CTL_HA_STATE_UNKNOWN,
13083 .init = ctl_isc_init,
13084 .start = ctl_isc_start,
13085 .quiesce = ctl_isc_quiesce