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;
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);
329 * Serial number (0x80), device id (0x83), and supported pages (0x00)
331 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 3
333 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
335 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
336 static int ctl_init(void);
337 void ctl_shutdown(void);
338 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
339 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
340 static void ctl_ioctl_online(void *arg);
341 static void ctl_ioctl_offline(void *arg);
342 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
343 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
344 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
345 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
346 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
347 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
348 static int ctl_ioctl_submit_wait(union ctl_io *io);
349 static void ctl_ioctl_datamove(union ctl_io *io);
350 static void ctl_ioctl_done(union ctl_io *io);
351 static void ctl_ioctl_hard_startstop_callback(void *arg,
352 struct cfi_metatask *metatask);
353 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
354 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
355 struct ctl_ooa *ooa_hdr);
356 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
358 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
359 uint32_t ctl_port_idx(int port_num);
361 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
362 uint32_t targ_target, uint32_t targ_lun,
364 static void ctl_kfree_io(union ctl_io *io);
366 static void ctl_free_io_internal(union ctl_io *io, int have_lock);
367 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
368 struct ctl_be_lun *be_lun, struct ctl_id target_id);
369 static int ctl_free_lun(struct ctl_lun *lun);
370 static void ctl_create_lun(struct ctl_be_lun *be_lun);
372 static void ctl_failover_change_pages(struct ctl_softc *softc,
373 struct ctl_scsiio *ctsio, int master);
376 static int ctl_do_mode_select(union ctl_io *io);
377 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
378 uint64_t res_key, uint64_t sa_res_key,
379 uint8_t type, uint32_t residx,
380 struct ctl_scsiio *ctsio,
381 struct scsi_per_res_out *cdb,
382 struct scsi_per_res_out_parms* param);
383 static void ctl_pro_preempt_other(struct ctl_lun *lun,
384 union ctl_ha_msg *msg);
385 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
386 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
388 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
389 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
390 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
391 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
392 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
393 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
394 union ctl_io *ooa_io);
395 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
396 union ctl_io *starting_io);
397 static int ctl_check_blocked(struct ctl_lun *lun);
398 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
400 struct ctl_cmd_entry *entry,
401 struct ctl_scsiio *ctsio);
402 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
403 static void ctl_failover(void);
404 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
405 struct ctl_scsiio *ctsio);
406 static int ctl_scsiio(struct ctl_scsiio *ctsio);
408 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
409 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
410 ctl_ua_type ua_type);
411 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
412 ctl_ua_type ua_type);
413 static int ctl_abort_task(union ctl_io *io);
414 static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
416 static void ctl_datamove_timer_wakeup(void *arg);
417 static void ctl_done_timer_wakeup(void *arg);
418 #endif /* CTL_IO_DELAY */
420 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
421 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
422 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
423 static void ctl_datamove_remote_write(union ctl_io *io);
424 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
425 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
426 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
427 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
428 ctl_ha_dt_cb callback);
429 static void ctl_datamove_remote_read(union ctl_io *io);
430 static void ctl_datamove_remote(union ctl_io *io);
431 static int ctl_process_done(union ctl_io *io, int have_lock);
432 static void ctl_work_thread(void *arg);
435 * Load the serialization table. This isn't very pretty, but is probably
436 * the easiest way to do it.
438 #include "ctl_ser_table.c"
441 * We only need to define open, close and ioctl routines for this driver.
443 static struct cdevsw ctl_cdevsw = {
444 .d_version = D_VERSION,
447 .d_close = ctl_close,
448 .d_ioctl = ctl_ioctl,
453 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
455 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
457 static moduledata_t ctl_moduledata = {
459 ctl_module_event_handler,
463 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
464 MODULE_VERSION(ctl, 1);
467 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
468 union ctl_ha_msg *msg_info)
470 struct ctl_scsiio *ctsio;
472 if (msg_info->hdr.original_sc == NULL) {
473 printf("%s: original_sc == NULL!\n", __func__);
474 /* XXX KDM now what? */
478 ctsio = &msg_info->hdr.original_sc->scsiio;
479 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
480 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
481 ctsio->io_hdr.status = msg_info->hdr.status;
482 ctsio->scsi_status = msg_info->scsi.scsi_status;
483 ctsio->sense_len = msg_info->scsi.sense_len;
484 ctsio->sense_residual = msg_info->scsi.sense_residual;
485 ctsio->residual = msg_info->scsi.residual;
486 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
487 sizeof(ctsio->sense_data));
488 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
489 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
490 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
495 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
496 union ctl_ha_msg *msg_info)
498 struct ctl_scsiio *ctsio;
500 if (msg_info->hdr.serializing_sc == NULL) {
501 printf("%s: serializing_sc == NULL!\n", __func__);
502 /* XXX KDM now what? */
506 ctsio = &msg_info->hdr.serializing_sc->scsiio;
509 * Attempt to catch the situation where an I/O has
510 * been freed, and we're using it again.
512 if (ctsio->io_hdr.io_type == 0xff) {
513 union ctl_io *tmp_io;
514 tmp_io = (union ctl_io *)ctsio;
515 printf("%s: %p use after free!\n", __func__,
517 printf("%s: type %d msg %d cdb %x iptl: "
518 "%d:%d:%d:%d tag 0x%04x "
519 "flag %#x status %x\n",
521 tmp_io->io_hdr.io_type,
522 tmp_io->io_hdr.msg_type,
523 tmp_io->scsiio.cdb[0],
524 tmp_io->io_hdr.nexus.initid.id,
525 tmp_io->io_hdr.nexus.targ_port,
526 tmp_io->io_hdr.nexus.targ_target.id,
527 tmp_io->io_hdr.nexus.targ_lun,
528 (tmp_io->io_hdr.io_type ==
530 tmp_io->taskio.tag_num :
531 tmp_io->scsiio.tag_num,
532 tmp_io->io_hdr.flags,
533 tmp_io->io_hdr.status);
536 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
537 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
542 * ISC (Inter Shelf Communication) event handler. Events from the HA
543 * subsystem come in here.
546 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
548 struct ctl_softc *ctl_softc;
550 struct ctl_prio *presio;
551 ctl_ha_status isc_status;
553 ctl_softc = control_softc;
558 printf("CTL: Isc Msg event %d\n", event);
560 if (event == CTL_HA_EVT_MSG_RECV) {
561 union ctl_ha_msg msg_info;
563 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
564 sizeof(msg_info), /*wait*/ 0);
566 printf("CTL: msg_type %d\n", msg_info.msg_type);
568 if (isc_status != 0) {
569 printf("Error receiving message, status = %d\n",
573 mtx_lock(&ctl_softc->ctl_lock);
575 switch (msg_info.hdr.msg_type) {
576 case CTL_MSG_SERIALIZE:
578 printf("Serialize\n");
580 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
582 printf("ctl_isc_event_handler: can't allocate "
585 /* Need to set busy and send msg back */
586 mtx_unlock(&ctl_softc->ctl_lock);
587 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
588 msg_info.hdr.status = CTL_SCSI_ERROR;
589 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
590 msg_info.scsi.sense_len = 0;
591 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
592 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
597 // populate ctsio from msg_info
598 io->io_hdr.io_type = CTL_IO_SCSI;
599 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
600 io->io_hdr.original_sc = msg_info.hdr.original_sc;
602 printf("pOrig %x\n", (int)msg_info.original_sc);
604 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
607 * If we're in serialization-only mode, we don't
608 * want to go through full done processing. Thus
611 * XXX KDM add another flag that is more specific.
613 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
614 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
615 io->io_hdr.nexus = msg_info.hdr.nexus;
617 printf("targ %d, port %d, iid %d, lun %d\n",
618 io->io_hdr.nexus.targ_target.id,
619 io->io_hdr.nexus.targ_port,
620 io->io_hdr.nexus.initid.id,
621 io->io_hdr.nexus.targ_lun);
623 io->scsiio.tag_num = msg_info.scsi.tag_num;
624 io->scsiio.tag_type = msg_info.scsi.tag_type;
625 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
627 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
628 struct ctl_cmd_entry *entry;
631 opcode = io->scsiio.cdb[0];
632 entry = &ctl_cmd_table[opcode];
633 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
635 entry->flags & CTL_FLAG_DATA_MASK;
637 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
642 /* Performed on the Originating SC, XFER mode only */
643 case CTL_MSG_DATAMOVE: {
644 struct ctl_sg_entry *sgl;
647 io = msg_info.hdr.original_sc;
649 printf("%s: original_sc == NULL!\n", __func__);
650 /* XXX KDM do something here */
653 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
654 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
656 * Keep track of this, we need to send it back over
657 * when the datamove is complete.
659 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
661 if (msg_info.dt.sg_sequence == 0) {
663 * XXX KDM we use the preallocated S/G list
664 * here, but we'll need to change this to
665 * dynamic allocation if we need larger S/G
668 if (msg_info.dt.kern_sg_entries >
669 sizeof(io->io_hdr.remote_sglist) /
670 sizeof(io->io_hdr.remote_sglist[0])) {
671 printf("%s: number of S/G entries "
672 "needed %u > allocated num %zd\n",
674 msg_info.dt.kern_sg_entries,
675 sizeof(io->io_hdr.remote_sglist)/
676 sizeof(io->io_hdr.remote_sglist[0]));
679 * XXX KDM send a message back to
680 * the other side to shut down the
681 * DMA. The error will come back
682 * through via the normal channel.
686 sgl = io->io_hdr.remote_sglist;
688 sizeof(io->io_hdr.remote_sglist));
690 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
692 io->scsiio.kern_sg_entries =
693 msg_info.dt.kern_sg_entries;
694 io->scsiio.rem_sg_entries =
695 msg_info.dt.kern_sg_entries;
696 io->scsiio.kern_data_len =
697 msg_info.dt.kern_data_len;
698 io->scsiio.kern_total_len =
699 msg_info.dt.kern_total_len;
700 io->scsiio.kern_data_resid =
701 msg_info.dt.kern_data_resid;
702 io->scsiio.kern_rel_offset =
703 msg_info.dt.kern_rel_offset;
705 * Clear out per-DMA flags.
707 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
709 * Add per-DMA flags that are set for this
710 * particular DMA request.
712 io->io_hdr.flags |= msg_info.dt.flags &
715 sgl = (struct ctl_sg_entry *)
716 io->scsiio.kern_data_ptr;
718 for (i = msg_info.dt.sent_sg_entries, j = 0;
719 i < (msg_info.dt.sent_sg_entries +
720 msg_info.dt.cur_sg_entries); i++, j++) {
721 sgl[i].addr = msg_info.dt.sg_list[j].addr;
722 sgl[i].len = msg_info.dt.sg_list[j].len;
725 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
727 msg_info.dt.sg_list[j].addr,
728 msg_info.dt.sg_list[j].len,
729 sgl[i].addr, sgl[i].len, j, i);
733 memcpy(&sgl[msg_info.dt.sent_sg_entries],
735 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
739 * If this is the last piece of the I/O, we've got
740 * the full S/G list. Queue processing in the thread.
741 * Otherwise wait for the next piece.
743 if (msg_info.dt.sg_last != 0) {
744 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
750 /* Performed on the Serializing (primary) SC, XFER mode only */
751 case CTL_MSG_DATAMOVE_DONE: {
752 if (msg_info.hdr.serializing_sc == NULL) {
753 printf("%s: serializing_sc == NULL!\n",
755 /* XXX KDM now what? */
759 * We grab the sense information here in case
760 * there was a failure, so we can return status
761 * back to the initiator.
763 io = msg_info.hdr.serializing_sc;
764 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
765 io->io_hdr.status = msg_info.hdr.status;
766 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
767 io->scsiio.sense_len = msg_info.scsi.sense_len;
768 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
769 io->io_hdr.port_status = msg_info.scsi.fetd_status;
770 io->scsiio.residual = msg_info.scsi.residual;
771 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
772 sizeof(io->scsiio.sense_data));
774 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
780 /* Preformed on Originating SC, SER_ONLY mode */
782 io = msg_info.hdr.original_sc;
784 printf("%s: Major Bummer\n", __func__);
785 mtx_unlock(&ctl_softc->ctl_lock);
789 printf("pOrig %x\n",(int) ctsio);
792 io->io_hdr.msg_type = CTL_MSG_R2R;
793 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
794 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
800 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
802 * Performed on the Originating (i.e. secondary) SC in XFER
805 case CTL_MSG_FINISH_IO:
806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
807 ctl_isc_handler_finish_xfer(ctl_softc,
810 ctl_isc_handler_finish_ser_only(ctl_softc,
814 /* Preformed on Originating SC */
815 case CTL_MSG_BAD_JUJU:
816 io = msg_info.hdr.original_sc;
818 printf("%s: Bad JUJU!, original_sc is NULL!\n",
822 ctl_copy_sense_data(&msg_info, io);
824 * IO should have already been cleaned up on other
825 * SC so clear this flag so we won't send a message
826 * back to finish the IO there.
828 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
829 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
831 /* io = msg_info.hdr.serializing_sc; */
832 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
833 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
838 /* Handle resets sent from the other side */
839 case CTL_MSG_MANAGE_TASKS: {
840 struct ctl_taskio *taskio;
841 taskio = (struct ctl_taskio *)ctl_alloc_io(
842 (void *)ctl_softc->othersc_pool);
843 if (taskio == NULL) {
844 printf("ctl_isc_event_handler: can't allocate "
847 /* should I just call the proper reset func
849 mtx_unlock(&ctl_softc->ctl_lock);
852 ctl_zero_io((union ctl_io *)taskio);
853 taskio->io_hdr.io_type = CTL_IO_TASK;
854 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
855 taskio->io_hdr.nexus = msg_info.hdr.nexus;
856 taskio->task_action = msg_info.task.task_action;
857 taskio->tag_num = msg_info.task.tag_num;
858 taskio->tag_type = msg_info.task.tag_type;
860 taskio->io_hdr.start_time = time_uptime;
861 getbintime(&taskio->io_hdr.start_bt);
863 cs_prof_gettime(&taskio->io_hdr.start_ticks);
865 #endif /* CTL_TIME_IO */
866 STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
867 &taskio->io_hdr, links);
868 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
872 /* Persistent Reserve action which needs attention */
873 case CTL_MSG_PERS_ACTION:
874 presio = (struct ctl_prio *)ctl_alloc_io(
875 (void *)ctl_softc->othersc_pool);
876 if (presio == NULL) {
877 printf("ctl_isc_event_handler: can't allocate "
880 /* Need to set busy and send msg back */
881 mtx_unlock(&ctl_softc->ctl_lock);
884 ctl_zero_io((union ctl_io *)presio);
885 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
886 presio->pr_msg = msg_info.pr;
887 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
888 &presio->io_hdr, links);
891 case CTL_MSG_SYNC_FE:
894 case CTL_MSG_APS_LOCK: {
895 // It's quicker to execute this then to
898 struct ctl_page_index *page_index;
899 struct copan_aps_subpage *current_sp;
901 lun = ctl_softc->ctl_luns[msg_info.hdr.nexus.targ_lun];
902 page_index = &lun->mode_pages.index[index_to_aps_page];
903 current_sp = (struct copan_aps_subpage *)
904 (page_index->page_data +
905 (page_index->page_len * CTL_PAGE_CURRENT));
907 current_sp->lock_active = msg_info.aps.lock_flag;
911 printf("How did I get here?\n");
913 mtx_unlock(&ctl_softc->ctl_lock);
914 } else if (event == CTL_HA_EVT_MSG_SENT) {
915 if (param != CTL_HA_STATUS_SUCCESS) {
916 printf("Bad status from ctl_ha_msg_send status %d\n",
920 } else if (event == CTL_HA_EVT_DISCONNECT) {
921 printf("CTL: Got a disconnect from Isc\n");
924 printf("ctl_isc_event_handler: Unknown event %d\n", event);
933 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
935 struct scsi_sense_data *sense;
937 sense = &dest->scsiio.sense_data;
938 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
939 dest->scsiio.scsi_status = src->scsi.scsi_status;
940 dest->scsiio.sense_len = src->scsi.sense_len;
941 dest->io_hdr.status = src->hdr.status;
947 struct ctl_softc *softc;
948 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
949 struct ctl_frontend *fe;
962 /* If we're disabled, don't initialize. */
963 if (ctl_disable != 0)
966 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
968 softc = control_softc;
970 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
973 softc->dev->si_drv1 = softc;
976 * By default, return a "bad LUN" peripheral qualifier for unknown
977 * LUNs. The user can override this default using the tunable or
978 * sysctl. See the comment in ctl_inquiry_std() for more details.
980 softc->inquiry_pq_no_lun = 1;
981 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
982 &softc->inquiry_pq_no_lun);
983 sysctl_ctx_init(&softc->sysctl_ctx);
984 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
985 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
986 CTLFLAG_RD, 0, "CAM Target Layer");
988 if (softc->sysctl_tree == NULL) {
989 printf("%s: unable to allocate sysctl tree\n", __func__);
990 destroy_dev(softc->dev);
991 free(control_softc, M_DEVBUF);
992 control_softc = NULL;
996 SYSCTL_ADD_INT(&softc->sysctl_ctx,
997 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
998 "inquiry_pq_no_lun", CTLFLAG_RW,
999 &softc->inquiry_pq_no_lun, 0,
1000 "Report no lun possible for invalid LUNs");
1002 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1003 softc->open_count = 0;
1006 * Default to actually sending a SYNCHRONIZE CACHE command down to
1009 softc->flags = CTL_FLAG_REAL_SYNC;
1012 * In Copan's HA scheme, the "master" and "slave" roles are
1013 * figured out through the slot the controller is in. Although it
1014 * is an active/active system, someone has to be in charge.
1017 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1021 softc->flags |= CTL_FLAG_MASTER_SHELF;
1024 persis_offset = CTL_MAX_INITIATORS;
1027 * XXX KDM need to figure out where we want to get our target ID
1028 * and WWID. Is it different on each port?
1030 softc->target.id = 0;
1031 softc->target.wwid[0] = 0x12345678;
1032 softc->target.wwid[1] = 0x87654321;
1033 STAILQ_INIT(&softc->lun_list);
1034 STAILQ_INIT(&softc->pending_lun_queue);
1035 STAILQ_INIT(&softc->task_queue);
1036 STAILQ_INIT(&softc->incoming_queue);
1037 STAILQ_INIT(&softc->rtr_queue);
1038 STAILQ_INIT(&softc->done_queue);
1039 STAILQ_INIT(&softc->isc_queue);
1040 STAILQ_INIT(&softc->fe_list);
1041 STAILQ_INIT(&softc->be_list);
1042 STAILQ_INIT(&softc->io_pools);
1047 * We don't bother calling these with ctl_lock held here, because,
1048 * in theory, no one else can try to do anything while we're in our
1049 * module init routine.
1051 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1052 &internal_pool)!= 0){
1053 printf("ctl: can't allocate %d entry internal pool, "
1054 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1058 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1059 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1060 printf("ctl: can't allocate %d entry emergency pool, "
1061 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1062 ctl_pool_free(softc, internal_pool);
1066 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1069 printf("ctl: can't allocate %d entry other SC pool, "
1070 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1071 ctl_pool_free(softc, internal_pool);
1072 ctl_pool_free(softc, emergency_pool);
1076 softc->internal_pool = internal_pool;
1077 softc->emergency_pool = emergency_pool;
1078 softc->othersc_pool = other_pool;
1080 ctl_pool_acquire(internal_pool);
1081 ctl_pool_acquire(emergency_pool);
1082 ctl_pool_acquire(other_pool);
1085 * We used to allocate a processor LUN here. The new scheme is to
1086 * just let the user allocate LUNs as he sees fit.
1089 mtx_lock(&softc->ctl_lock);
1090 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1091 mtx_unlock(&softc->ctl_lock);
1094 error = kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
1097 printf("error creating CTL work thread!\n");
1099 ctl_pool_free(softc, internal_pool);
1100 ctl_pool_free(softc, emergency_pool);
1101 ctl_pool_free(softc, other_pool);
1104 printf("ctl: CAM Target Layer loaded\n");
1107 * Initialize the initiator and portname mappings
1109 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1112 * Initialize the ioctl front end.
1114 fe = &softc->ioctl_info.fe;
1115 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1116 fe->port_type = CTL_PORT_IOCTL;
1117 fe->num_requested_ctl_io = 100;
1118 fe->port_name = softc->ioctl_info.port_name;
1119 fe->port_online = ctl_ioctl_online;
1120 fe->port_offline = ctl_ioctl_offline;
1121 fe->onoff_arg = &softc->ioctl_info;
1122 fe->targ_enable = ctl_ioctl_targ_enable;
1123 fe->targ_disable = ctl_ioctl_targ_disable;
1124 fe->lun_enable = ctl_ioctl_lun_enable;
1125 fe->lun_disable = ctl_ioctl_lun_disable;
1126 fe->targ_lun_arg = &softc->ioctl_info;
1127 fe->fe_datamove = ctl_ioctl_datamove;
1128 fe->fe_done = ctl_ioctl_done;
1129 fe->max_targets = 15;
1130 fe->max_target_id = 15;
1132 if (ctl_frontend_register(&softc->ioctl_info.fe,
1133 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1134 printf("ctl: ioctl front end registration failed, will "
1135 "continue anyway\n");
1139 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1140 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1141 sizeof(struct callout), CTL_TIMER_BYTES);
1144 #endif /* CTL_IO_DELAY */
1152 struct ctl_softc *softc;
1153 struct ctl_lun *lun, *next_lun;
1154 struct ctl_io_pool *pool, *next_pool;
1156 softc = (struct ctl_softc *)control_softc;
1158 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1159 printf("ctl: ioctl front end deregistration failed\n");
1161 mtx_lock(&softc->ctl_lock);
1166 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1167 next_lun = STAILQ_NEXT(lun, links);
1172 * This will rip the rug out from under any FETDs or anyone else
1173 * that has a pool allocated. Since we increment our module
1174 * refcount any time someone outside the main CTL module allocates
1175 * a pool, we shouldn't have any problems here. The user won't be
1176 * able to unload the CTL module until client modules have
1177 * successfully unloaded.
1179 for (pool = STAILQ_FIRST(&softc->io_pools); pool != NULL;
1181 next_pool = STAILQ_NEXT(pool, links);
1182 ctl_pool_free(softc, pool);
1185 mtx_unlock(&softc->ctl_lock);
1188 ctl_shutdown_thread(softc->work_thread);
1191 mtx_destroy(&softc->ctl_lock);
1193 destroy_dev(softc->dev);
1195 sysctl_ctx_free(&softc->sysctl_ctx);
1197 free(control_softc, M_DEVBUF);
1198 control_softc = NULL;
1200 printf("ctl: CAM Target Layer unloaded\n");
1204 ctl_module_event_handler(module_t mod, int what, void *arg)
1209 return (ctl_init());
1213 return (EOPNOTSUPP);
1218 * XXX KDM should we do some access checks here? Bump a reference count to
1219 * prevent a CTL module from being unloaded while someone has it open?
1222 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1228 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1234 ctl_port_enable(ctl_port_type port_type)
1236 struct ctl_softc *softc;
1237 struct ctl_frontend *fe;
1239 if (ctl_is_single == 0) {
1240 union ctl_ha_msg msg_info;
1244 printf("%s: HA mode, synchronizing frontend enable\n",
1247 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1248 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1249 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1250 printf("Sync msg send error retval %d\n", isc_retval);
1252 if (!rcv_sync_msg) {
1253 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1254 sizeof(msg_info), 1);
1257 printf("CTL:Frontend Enable\n");
1259 printf("%s: single mode, skipping frontend synchronization\n",
1264 softc = control_softc;
1266 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1267 if (port_type & fe->port_type)
1270 printf("port %d\n", fe->targ_port);
1272 ctl_frontend_online(fe);
1280 ctl_port_disable(ctl_port_type port_type)
1282 struct ctl_softc *softc;
1283 struct ctl_frontend *fe;
1285 softc = control_softc;
1287 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1288 if (port_type & fe->port_type)
1289 ctl_frontend_offline(fe);
1296 * Returns 0 for success, 1 for failure.
1297 * Currently the only failure mode is if there aren't enough entries
1298 * allocated. So, in case of a failure, look at num_entries_dropped,
1299 * reallocate and try again.
1302 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1303 int *num_entries_filled, int *num_entries_dropped,
1304 ctl_port_type port_type, int no_virtual)
1306 struct ctl_softc *softc;
1307 struct ctl_frontend *fe;
1308 int entries_dropped, entries_filled;
1312 softc = control_softc;
1316 entries_dropped = 0;
1319 mtx_lock(&softc->ctl_lock);
1320 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1321 struct ctl_port_entry *entry;
1323 if ((fe->port_type & port_type) == 0)
1326 if ((no_virtual != 0)
1327 && (fe->virtual_port != 0))
1330 if (entries_filled >= num_entries_alloced) {
1334 entry = &entries[i];
1336 entry->port_type = fe->port_type;
1337 strlcpy(entry->port_name, fe->port_name,
1338 sizeof(entry->port_name));
1339 entry->physical_port = fe->physical_port;
1340 entry->virtual_port = fe->virtual_port;
1341 entry->wwnn = fe->wwnn;
1342 entry->wwpn = fe->wwpn;
1348 mtx_unlock(&softc->ctl_lock);
1350 if (entries_dropped > 0)
1353 *num_entries_dropped = entries_dropped;
1354 *num_entries_filled = entries_filled;
1360 ctl_ioctl_online(void *arg)
1362 struct ctl_ioctl_info *ioctl_info;
1364 ioctl_info = (struct ctl_ioctl_info *)arg;
1366 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1370 ctl_ioctl_offline(void *arg)
1372 struct ctl_ioctl_info *ioctl_info;
1374 ioctl_info = (struct ctl_ioctl_info *)arg;
1376 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1380 * Remove an initiator by port number and initiator ID.
1381 * Returns 0 for success, 1 for failure.
1382 * Assumes the caller does NOT hold the CTL lock.
1385 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1387 struct ctl_softc *softc;
1389 softc = control_softc;
1392 || (targ_port > CTL_MAX_PORTS)) {
1393 printf("%s: invalid port number %d\n", __func__, targ_port);
1396 if (iid > CTL_MAX_INIT_PER_PORT) {
1397 printf("%s: initiator ID %u > maximun %u!\n",
1398 __func__, iid, CTL_MAX_INIT_PER_PORT);
1402 mtx_lock(&softc->ctl_lock);
1404 softc->wwpn_iid[targ_port][iid].in_use = 0;
1406 mtx_unlock(&softc->ctl_lock);
1412 * Add an initiator to the initiator map.
1413 * Returns 0 for success, 1 for failure.
1414 * Assumes the caller does NOT hold the CTL lock.
1417 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1419 struct ctl_softc *softc;
1422 softc = control_softc;
1427 || (targ_port > CTL_MAX_PORTS)) {
1428 printf("%s: invalid port number %d\n", __func__, targ_port);
1431 if (iid > CTL_MAX_INIT_PER_PORT) {
1432 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1433 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1437 mtx_lock(&softc->ctl_lock);
1439 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1441 * We don't treat this as an error.
1443 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1444 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1445 __func__, targ_port, iid, (uintmax_t)wwpn);
1450 * This is an error, but what do we do about it? The
1451 * driver is telling us we have a new WWPN for this
1452 * initiator ID, so we pretty much need to use it.
1454 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1455 "still at that address\n", __func__, targ_port, iid,
1457 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1460 * XXX KDM clear have_ca and ua_pending on each LUN for
1464 softc->wwpn_iid[targ_port][iid].in_use = 1;
1465 softc->wwpn_iid[targ_port][iid].iid = iid;
1466 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1467 softc->wwpn_iid[targ_port][iid].port = targ_port;
1471 mtx_unlock(&softc->ctl_lock);
1477 * XXX KDM should we pretend to do something in the target/lun
1478 * enable/disable functions?
1481 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1487 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1493 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1499 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1505 * Data movement routine for the CTL ioctl frontend port.
1508 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1510 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1511 struct ctl_sg_entry ext_entry, kern_entry;
1512 int ext_sglen, ext_sg_entries, kern_sg_entries;
1513 int ext_sg_start, ext_offset;
1514 int len_to_copy, len_copied;
1515 int kern_watermark, ext_watermark;
1516 int ext_sglist_malloced;
1519 ext_sglist_malloced = 0;
1523 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1526 * If this flag is set, fake the data transfer.
1528 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1529 ctsio->ext_data_filled = ctsio->ext_data_len;
1534 * To simplify things here, if we have a single buffer, stick it in
1535 * a S/G entry and just make it a single entry S/G list.
1537 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1540 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1542 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1544 ext_sglist_malloced = 1;
1545 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1547 ctl_set_internal_failure(ctsio,
1552 ext_sg_entries = ctsio->ext_sg_entries;
1554 for (i = 0; i < ext_sg_entries; i++) {
1555 if ((len_seen + ext_sglist[i].len) >=
1556 ctsio->ext_data_filled) {
1558 ext_offset = ctsio->ext_data_filled - len_seen;
1561 len_seen += ext_sglist[i].len;
1564 ext_sglist = &ext_entry;
1565 ext_sglist->addr = ctsio->ext_data_ptr;
1566 ext_sglist->len = ctsio->ext_data_len;
1569 ext_offset = ctsio->ext_data_filled;
1572 if (ctsio->kern_sg_entries > 0) {
1573 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1574 kern_sg_entries = ctsio->kern_sg_entries;
1576 kern_sglist = &kern_entry;
1577 kern_sglist->addr = ctsio->kern_data_ptr;
1578 kern_sglist->len = ctsio->kern_data_len;
1579 kern_sg_entries = 1;
1584 ext_watermark = ext_offset;
1586 for (i = ext_sg_start, j = 0;
1587 i < ext_sg_entries && j < kern_sg_entries;) {
1588 uint8_t *ext_ptr, *kern_ptr;
1590 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1591 kern_sglist[j].len - kern_watermark);
1593 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1594 ext_ptr = ext_ptr + ext_watermark;
1595 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1599 panic("need to implement bus address support");
1601 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1604 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1605 kern_ptr = kern_ptr + kern_watermark;
1607 kern_watermark += len_to_copy;
1608 ext_watermark += len_to_copy;
1610 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1612 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1613 "bytes to user\n", len_to_copy));
1614 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1615 "to %p\n", kern_ptr, ext_ptr));
1616 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1617 ctl_set_internal_failure(ctsio,
1623 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1624 "bytes from user\n", len_to_copy));
1625 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1626 "to %p\n", ext_ptr, kern_ptr));
1627 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1628 ctl_set_internal_failure(ctsio,
1635 len_copied += len_to_copy;
1637 if (ext_sglist[i].len == ext_watermark) {
1642 if (kern_sglist[j].len == kern_watermark) {
1648 ctsio->ext_data_filled += len_copied;
1650 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1651 "kern_sg_entries: %d\n", ext_sg_entries,
1653 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1654 "kern_data_len = %d\n", ctsio->ext_data_len,
1655 ctsio->kern_data_len));
1658 /* XXX KDM set residual?? */
1661 if (ext_sglist_malloced != 0)
1662 free(ext_sglist, M_CTL);
1664 return (CTL_RETVAL_COMPLETE);
1668 * Serialize a command that went down the "wrong" side, and so was sent to
1669 * this controller for execution. The logic is a little different than the
1670 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1671 * sent back to the other side, but in the success case, we execute the
1672 * command on this side (XFER mode) or tell the other side to execute it
1676 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1678 struct ctl_softc *ctl_softc;
1679 union ctl_ha_msg msg_info;
1680 struct ctl_lun *lun;
1683 ctl_softc = control_softc;
1685 mtx_lock(&ctl_softc->ctl_lock);
1687 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
1691 * Why isn't LUN defined? The other side wouldn't
1692 * send a cmd if the LUN is undefined.
1694 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1696 /* "Logical unit not supported" */
1697 ctl_set_sense_data(&msg_info.scsi.sense_data,
1699 /*sense_format*/SSD_TYPE_NONE,
1700 /*current_error*/ 1,
1701 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1706 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1707 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1708 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1709 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1710 msg_info.hdr.serializing_sc = NULL;
1711 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1712 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1713 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1716 mtx_unlock(&ctl_softc->ctl_lock);
1721 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1723 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1724 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1726 case CTL_ACTION_BLOCK:
1727 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1728 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1731 case CTL_ACTION_PASS:
1732 case CTL_ACTION_SKIP:
1733 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1734 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1735 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1736 &ctsio->io_hdr, links);
1739 /* send msg back to other side */
1740 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1741 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1742 msg_info.hdr.msg_type = CTL_MSG_R2R;
1744 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1746 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1747 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1751 case CTL_ACTION_OVERLAP:
1752 /* OVERLAPPED COMMANDS ATTEMPTED */
1753 ctl_set_sense_data(&msg_info.scsi.sense_data,
1755 /*sense_format*/SSD_TYPE_NONE,
1756 /*current_error*/ 1,
1757 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1762 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1763 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1764 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1765 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1766 msg_info.hdr.serializing_sc = NULL;
1767 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1769 printf("BAD JUJU:Major Bummer Overlap\n");
1771 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1773 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1774 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1777 case CTL_ACTION_OVERLAP_TAG:
1778 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1779 ctl_set_sense_data(&msg_info.scsi.sense_data,
1781 /*sense_format*/SSD_TYPE_NONE,
1782 /*current_error*/ 1,
1783 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1785 /*ascq*/ ctsio->tag_num & 0xff,
1788 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1789 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1790 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1791 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1792 msg_info.hdr.serializing_sc = NULL;
1793 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1795 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1797 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1800 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1803 case CTL_ACTION_ERROR:
1805 /* "Internal target failure" */
1806 ctl_set_sense_data(&msg_info.scsi.sense_data,
1808 /*sense_format*/SSD_TYPE_NONE,
1809 /*current_error*/ 1,
1810 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1815 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1816 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1817 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1818 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1819 msg_info.hdr.serializing_sc = NULL;
1820 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1822 printf("BAD JUJU:Major Bummer HW Error\n");
1824 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1826 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1827 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1832 mtx_unlock(&ctl_softc->ctl_lock);
1837 ctl_ioctl_submit_wait(union ctl_io *io)
1839 struct ctl_fe_ioctl_params params;
1840 ctl_fe_ioctl_state last_state;
1845 bzero(¶ms, sizeof(params));
1847 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1848 cv_init(¶ms.sem, "ctlioccv");
1849 params.state = CTL_IOCTL_INPROG;
1850 last_state = params.state;
1852 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1854 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1856 /* This shouldn't happen */
1857 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1863 mtx_lock(¶ms.ioctl_mtx);
1865 * Check the state here, and don't sleep if the state has
1866 * already changed (i.e. wakeup has already occured, but we
1867 * weren't waiting yet).
1869 if (params.state == last_state) {
1870 /* XXX KDM cv_wait_sig instead? */
1871 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1873 last_state = params.state;
1875 switch (params.state) {
1876 case CTL_IOCTL_INPROG:
1877 /* Why did we wake up? */
1878 /* XXX KDM error here? */
1879 mtx_unlock(¶ms.ioctl_mtx);
1881 case CTL_IOCTL_DATAMOVE:
1882 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1885 * change last_state back to INPROG to avoid
1886 * deadlock on subsequent data moves.
1888 params.state = last_state = CTL_IOCTL_INPROG;
1890 mtx_unlock(¶ms.ioctl_mtx);
1891 ctl_ioctl_do_datamove(&io->scsiio);
1893 * Note that in some cases, most notably writes,
1894 * this will queue the I/O and call us back later.
1895 * In other cases, generally reads, this routine
1896 * will immediately call back and wake us up,
1897 * probably using our own context.
1899 io->scsiio.be_move_done(io);
1901 case CTL_IOCTL_DONE:
1902 mtx_unlock(¶ms.ioctl_mtx);
1903 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1907 mtx_unlock(¶ms.ioctl_mtx);
1908 /* XXX KDM error here? */
1911 } while (done == 0);
1913 mtx_destroy(¶ms.ioctl_mtx);
1914 cv_destroy(¶ms.sem);
1916 return (CTL_RETVAL_COMPLETE);
1920 ctl_ioctl_datamove(union ctl_io *io)
1922 struct ctl_fe_ioctl_params *params;
1924 params = (struct ctl_fe_ioctl_params *)
1925 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1927 mtx_lock(¶ms->ioctl_mtx);
1928 params->state = CTL_IOCTL_DATAMOVE;
1929 cv_broadcast(¶ms->sem);
1930 mtx_unlock(¶ms->ioctl_mtx);
1934 ctl_ioctl_done(union ctl_io *io)
1936 struct ctl_fe_ioctl_params *params;
1938 params = (struct ctl_fe_ioctl_params *)
1939 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1941 mtx_lock(¶ms->ioctl_mtx);
1942 params->state = CTL_IOCTL_DONE;
1943 cv_broadcast(¶ms->sem);
1944 mtx_unlock(¶ms->ioctl_mtx);
1948 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1950 struct ctl_fe_ioctl_startstop_info *sd_info;
1952 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1954 sd_info->hs_info.status = metatask->status;
1955 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1956 sd_info->hs_info.luns_complete =
1957 metatask->taskinfo.startstop.luns_complete;
1958 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1960 cv_broadcast(&sd_info->sem);
1964 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1966 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1968 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1970 mtx_lock(fe_bbr_info->lock);
1971 fe_bbr_info->bbr_info->status = metatask->status;
1972 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1973 fe_bbr_info->wakeup_done = 1;
1974 mtx_unlock(fe_bbr_info->lock);
1976 cv_broadcast(&fe_bbr_info->sem);
1980 * Must be called with the ctl_lock held.
1981 * Returns 0 for success, errno for failure.
1984 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1985 struct ctl_ooa *ooa_hdr)
1992 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
1993 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
1995 struct ctl_ooa_entry *cur_entry, entry;
1998 * If we've got more than we can fit, just count the
1999 * remaining entries.
2001 if (*cur_fill_num >= ooa_hdr->alloc_num)
2004 cur_entry = &ooa_hdr->entries[*cur_fill_num];
2006 bzero(&entry, sizeof(entry));
2008 entry.tag_num = io->scsiio.tag_num;
2009 entry.lun_num = lun->lun;
2011 entry.start_bt = io->io_hdr.start_bt;
2013 bcopy(io->scsiio.cdb, entry.cdb, io->scsiio.cdb_len);
2014 entry.cdb_len = io->scsiio.cdb_len;
2015 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2016 entry.cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2018 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2019 entry.cmd_flags |= CTL_OOACMD_FLAG_DMA;
2021 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2022 entry.cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2024 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2025 entry.cmd_flags |= CTL_OOACMD_FLAG_RTR;
2027 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2028 entry.cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2030 retval = copyout(&entry, cur_entry, sizeof(entry));
2040 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2041 size_t error_str_len)
2045 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2047 if (copyin(user_addr, kptr, len) != 0) {
2048 snprintf(error_str, error_str_len, "Error copying %d bytes "
2049 "from user address %p to kernel address %p", len,
2059 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2063 if (be_args == NULL)
2066 for (i = 0; i < num_be_args; i++) {
2067 free(be_args[i].kname, M_CTL);
2068 free(be_args[i].kvalue, M_CTL);
2071 free(be_args, M_CTL);
2074 static struct ctl_be_arg *
2075 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2076 char *error_str, size_t error_str_len)
2078 struct ctl_be_arg *args;
2081 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2082 error_str, error_str_len);
2087 for (i = 0; i < num_be_args; i++) {
2088 args[i].kname = NULL;
2089 args[i].kvalue = NULL;
2092 for (i = 0; i < num_be_args; i++) {
2095 args[i].kname = ctl_copyin_alloc(args[i].name,
2096 args[i].namelen, error_str, error_str_len);
2097 if (args[i].kname == NULL)
2100 if (args[i].kname[args[i].namelen - 1] != '\0') {
2101 snprintf(error_str, error_str_len, "Argument %d "
2102 "name is not NUL-terminated", i);
2106 args[i].kvalue = NULL;
2108 tmpptr = ctl_copyin_alloc(args[i].value,
2109 args[i].vallen, error_str, error_str_len);
2113 args[i].kvalue = tmpptr;
2115 if ((args[i].flags & CTL_BEARG_ASCII)
2116 && (tmpptr[args[i].vallen - 1] != '\0')) {
2117 snprintf(error_str, error_str_len, "Argument %d "
2118 "value is not NUL-terminated", i);
2126 ctl_free_args(num_be_args, args);
2132 * Escape characters that are illegal or not recommended in XML.
2135 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2141 for (; *str; str++) {
2144 retval = sbuf_printf(sb, "&");
2147 retval = sbuf_printf(sb, ">");
2150 retval = sbuf_printf(sb, "<");
2153 retval = sbuf_putc(sb, *str);
2166 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2169 struct ctl_softc *softc;
2172 softc = control_softc;
2182 * If we haven't been "enabled", don't allow any SCSI I/O
2185 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2190 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2192 printf("ctl_ioctl: can't allocate ctl_io!\n");
2198 * Need to save the pool reference so it doesn't get
2199 * spammed by the user's ctl_io.
2201 pool_tmp = io->io_hdr.pool;
2203 memcpy(io, (void *)addr, sizeof(*io));
2205 io->io_hdr.pool = pool_tmp;
2207 * No status yet, so make sure the status is set properly.
2209 io->io_hdr.status = CTL_STATUS_NONE;
2212 * The user sets the initiator ID, target and LUN IDs.
2214 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2215 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2216 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2217 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2218 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2220 retval = ctl_ioctl_submit_wait(io);
2227 memcpy((void *)addr, io, sizeof(*io));
2229 /* return this to our pool */
2234 case CTL_ENABLE_PORT:
2235 case CTL_DISABLE_PORT:
2236 case CTL_SET_PORT_WWNS: {
2237 struct ctl_frontend *fe;
2238 struct ctl_port_entry *entry;
2240 entry = (struct ctl_port_entry *)addr;
2242 mtx_lock(&softc->ctl_lock);
2243 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2249 if ((entry->port_type == CTL_PORT_NONE)
2250 && (entry->targ_port == fe->targ_port)) {
2252 * If the user only wants to enable or
2253 * disable or set WWNs on a specific port,
2254 * do the operation and we're done.
2258 } else if (entry->port_type & fe->port_type) {
2260 * Compare the user's type mask with the
2261 * particular frontend type to see if we
2268 * Make sure the user isn't trying to set
2269 * WWNs on multiple ports at the same time.
2271 if (cmd == CTL_SET_PORT_WWNS) {
2272 printf("%s: Can't set WWNs on "
2273 "multiple ports\n", __func__);
2280 * XXX KDM we have to drop the lock here,
2281 * because the online/offline operations
2282 * can potentially block. We need to
2283 * reference count the frontends so they
2286 mtx_unlock(&softc->ctl_lock);
2288 if (cmd == CTL_ENABLE_PORT) {
2289 struct ctl_lun *lun;
2291 STAILQ_FOREACH(lun, &softc->lun_list,
2293 fe->lun_enable(fe->targ_lun_arg,
2298 ctl_frontend_online(fe);
2299 } else if (cmd == CTL_DISABLE_PORT) {
2300 struct ctl_lun *lun;
2302 ctl_frontend_offline(fe);
2304 STAILQ_FOREACH(lun, &softc->lun_list,
2313 mtx_lock(&softc->ctl_lock);
2315 if (cmd == CTL_SET_PORT_WWNS)
2316 ctl_frontend_set_wwns(fe,
2317 (entry->flags & CTL_PORT_WWNN_VALID) ?
2319 (entry->flags & CTL_PORT_WWPN_VALID) ?
2320 1 : 0, entry->wwpn);
2325 mtx_unlock(&softc->ctl_lock);
2328 case CTL_GET_PORT_LIST: {
2329 struct ctl_frontend *fe;
2330 struct ctl_port_list *list;
2333 list = (struct ctl_port_list *)addr;
2335 if (list->alloc_len != (list->alloc_num *
2336 sizeof(struct ctl_port_entry))) {
2337 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2338 "alloc_num %u * sizeof(struct ctl_port_entry) "
2339 "%zu\n", __func__, list->alloc_len,
2340 list->alloc_num, sizeof(struct ctl_port_entry));
2346 list->dropped_num = 0;
2348 mtx_lock(&softc->ctl_lock);
2349 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2350 struct ctl_port_entry entry, *list_entry;
2352 if (list->fill_num >= list->alloc_num) {
2353 list->dropped_num++;
2357 entry.port_type = fe->port_type;
2358 strlcpy(entry.port_name, fe->port_name,
2359 sizeof(entry.port_name));
2360 entry.targ_port = fe->targ_port;
2361 entry.physical_port = fe->physical_port;
2362 entry.virtual_port = fe->virtual_port;
2363 entry.wwnn = fe->wwnn;
2364 entry.wwpn = fe->wwpn;
2365 if (fe->status & CTL_PORT_STATUS_ONLINE)
2370 list_entry = &list->entries[i];
2372 retval = copyout(&entry, list_entry, sizeof(entry));
2374 printf("%s: CTL_GET_PORT_LIST: copyout "
2375 "returned %d\n", __func__, retval);
2380 list->fill_len += sizeof(entry);
2382 mtx_unlock(&softc->ctl_lock);
2385 * If this is non-zero, we had a copyout fault, so there's
2386 * probably no point in attempting to set the status inside
2392 if (list->dropped_num > 0)
2393 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2395 list->status = CTL_PORT_LIST_OK;
2398 case CTL_DUMP_OOA: {
2399 struct ctl_lun *lun;
2404 mtx_lock(&softc->ctl_lock);
2405 printf("Dumping OOA queues:\n");
2406 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2407 for (io = (union ctl_io *)TAILQ_FIRST(
2408 &lun->ooa_queue); io != NULL;
2409 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2411 sbuf_new(&sb, printbuf, sizeof(printbuf),
2413 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2417 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2419 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2421 CTL_FLAG_ABORT) ? " ABORT" : "",
2423 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2424 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2426 printf("%s\n", sbuf_data(&sb));
2429 printf("OOA queues dump done\n");
2430 mtx_unlock(&softc->ctl_lock);
2434 struct ctl_lun *lun;
2435 struct ctl_ooa *ooa_hdr;
2436 uint32_t cur_fill_num;
2438 ooa_hdr = (struct ctl_ooa *)addr;
2440 if ((ooa_hdr->alloc_len == 0)
2441 || (ooa_hdr->alloc_num == 0)) {
2442 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2443 "must be non-zero\n", __func__,
2444 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2449 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2450 sizeof(struct ctl_ooa_entry))) {
2451 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2452 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2453 __func__, ooa_hdr->alloc_len,
2454 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2459 mtx_lock(&softc->ctl_lock);
2460 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2461 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2462 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2463 mtx_unlock(&softc->ctl_lock);
2464 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2465 __func__, (uintmax_t)ooa_hdr->lun_num);
2472 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2473 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2474 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2480 mtx_unlock(&softc->ctl_lock);
2484 lun = softc->ctl_luns[ooa_hdr->lun_num];
2486 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr);
2488 mtx_unlock(&softc->ctl_lock);
2490 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2491 ooa_hdr->fill_len = ooa_hdr->fill_num *
2492 sizeof(struct ctl_ooa_entry);
2494 getbintime(&ooa_hdr->cur_bt);
2496 if (cur_fill_num > ooa_hdr->alloc_num) {
2497 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2498 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2500 ooa_hdr->dropped_num = 0;
2501 ooa_hdr->status = CTL_OOA_OK;
2505 case CTL_CHECK_OOA: {
2507 struct ctl_lun *lun;
2508 struct ctl_ooa_info *ooa_info;
2511 ooa_info = (struct ctl_ooa_info *)addr;
2513 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2514 ooa_info->status = CTL_OOA_INVALID_LUN;
2517 mtx_lock(&softc->ctl_lock);
2518 lun = softc->ctl_luns[ooa_info->lun_id];
2520 mtx_unlock(&softc->ctl_lock);
2521 ooa_info->status = CTL_OOA_INVALID_LUN;
2525 ooa_info->num_entries = 0;
2526 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2527 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2528 &io->io_hdr, ooa_links)) {
2529 ooa_info->num_entries++;
2532 mtx_unlock(&softc->ctl_lock);
2533 ooa_info->status = CTL_OOA_SUCCESS;
2537 case CTL_HARD_START:
2538 case CTL_HARD_STOP: {
2539 struct ctl_fe_ioctl_startstop_info ss_info;
2540 struct cfi_metatask *metatask;
2543 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2545 cv_init(&ss_info.sem, "hard start/stop cv" );
2547 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2548 if (metatask == NULL) {
2550 mtx_destroy(&hs_mtx);
2554 if (cmd == CTL_HARD_START)
2555 metatask->tasktype = CFI_TASK_STARTUP;
2557 metatask->tasktype = CFI_TASK_SHUTDOWN;
2559 metatask->callback = ctl_ioctl_hard_startstop_callback;
2560 metatask->callback_arg = &ss_info;
2562 cfi_action(metatask);
2564 /* Wait for the callback */
2566 cv_wait_sig(&ss_info.sem, &hs_mtx);
2567 mtx_unlock(&hs_mtx);
2570 * All information has been copied from the metatask by the
2571 * time cv_broadcast() is called, so we free the metatask here.
2573 cfi_free_metatask(metatask);
2575 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2577 mtx_destroy(&hs_mtx);
2581 struct ctl_bbrread_info *bbr_info;
2582 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2584 struct cfi_metatask *metatask;
2586 bbr_info = (struct ctl_bbrread_info *)addr;
2588 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2590 bzero(&bbr_mtx, sizeof(bbr_mtx));
2591 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2593 fe_bbr_info.bbr_info = bbr_info;
2594 fe_bbr_info.lock = &bbr_mtx;
2596 cv_init(&fe_bbr_info.sem, "BBR read cv");
2597 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2599 if (metatask == NULL) {
2600 mtx_destroy(&bbr_mtx);
2601 cv_destroy(&fe_bbr_info.sem);
2605 metatask->tasktype = CFI_TASK_BBRREAD;
2606 metatask->callback = ctl_ioctl_bbrread_callback;
2607 metatask->callback_arg = &fe_bbr_info;
2608 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2609 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2610 metatask->taskinfo.bbrread.len = bbr_info->len;
2612 cfi_action(metatask);
2615 while (fe_bbr_info.wakeup_done == 0)
2616 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2617 mtx_unlock(&bbr_mtx);
2619 bbr_info->status = metatask->status;
2620 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2621 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2622 memcpy(&bbr_info->sense_data,
2623 &metatask->taskinfo.bbrread.sense_data,
2624 ctl_min(sizeof(bbr_info->sense_data),
2625 sizeof(metatask->taskinfo.bbrread.sense_data)));
2627 cfi_free_metatask(metatask);
2629 mtx_destroy(&bbr_mtx);
2630 cv_destroy(&fe_bbr_info.sem);
2634 case CTL_DELAY_IO: {
2635 struct ctl_io_delay_info *delay_info;
2637 struct ctl_lun *lun;
2638 #endif /* CTL_IO_DELAY */
2640 delay_info = (struct ctl_io_delay_info *)addr;
2643 mtx_lock(&softc->ctl_lock);
2645 if ((delay_info->lun_id > CTL_MAX_LUNS)
2646 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2647 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2649 lun = softc->ctl_luns[delay_info->lun_id];
2651 delay_info->status = CTL_DELAY_STATUS_OK;
2653 switch (delay_info->delay_type) {
2654 case CTL_DELAY_TYPE_CONT:
2656 case CTL_DELAY_TYPE_ONESHOT:
2659 delay_info->status =
2660 CTL_DELAY_STATUS_INVALID_TYPE;
2664 switch (delay_info->delay_loc) {
2665 case CTL_DELAY_LOC_DATAMOVE:
2666 lun->delay_info.datamove_type =
2667 delay_info->delay_type;
2668 lun->delay_info.datamove_delay =
2669 delay_info->delay_secs;
2671 case CTL_DELAY_LOC_DONE:
2672 lun->delay_info.done_type =
2673 delay_info->delay_type;
2674 lun->delay_info.done_delay =
2675 delay_info->delay_secs;
2678 delay_info->status =
2679 CTL_DELAY_STATUS_INVALID_LOC;
2684 mtx_unlock(&softc->ctl_lock);
2686 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2687 #endif /* CTL_IO_DELAY */
2690 case CTL_REALSYNC_SET: {
2693 syncstate = (int *)addr;
2695 mtx_lock(&softc->ctl_lock);
2696 switch (*syncstate) {
2698 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2701 softc->flags |= CTL_FLAG_REAL_SYNC;
2707 mtx_unlock(&softc->ctl_lock);
2710 case CTL_REALSYNC_GET: {
2713 syncstate = (int*)addr;
2715 mtx_lock(&softc->ctl_lock);
2716 if (softc->flags & CTL_FLAG_REAL_SYNC)
2720 mtx_unlock(&softc->ctl_lock);
2726 struct ctl_sync_info *sync_info;
2727 struct ctl_lun *lun;
2729 sync_info = (struct ctl_sync_info *)addr;
2731 mtx_lock(&softc->ctl_lock);
2732 lun = softc->ctl_luns[sync_info->lun_id];
2734 mtx_unlock(&softc->ctl_lock);
2735 sync_info->status = CTL_GS_SYNC_NO_LUN;
2738 * Get or set the sync interval. We're not bounds checking
2739 * in the set case, hopefully the user won't do something
2742 if (cmd == CTL_GETSYNC)
2743 sync_info->sync_interval = lun->sync_interval;
2745 lun->sync_interval = sync_info->sync_interval;
2747 mtx_unlock(&softc->ctl_lock);
2749 sync_info->status = CTL_GS_SYNC_OK;
2753 case CTL_GETSTATS: {
2754 struct ctl_stats *stats;
2755 struct ctl_lun *lun;
2758 stats = (struct ctl_stats *)addr;
2760 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2762 stats->status = CTL_SS_NEED_MORE_SPACE;
2763 stats->num_luns = softc->num_luns;
2767 * XXX KDM no locking here. If the LUN list changes,
2768 * things can blow up.
2770 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2771 i++, lun = STAILQ_NEXT(lun, links)) {
2772 retval = copyout(&lun->stats, &stats->lun_stats[i],
2773 sizeof(lun->stats));
2777 stats->num_luns = softc->num_luns;
2778 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2780 stats->status = CTL_SS_OK;
2782 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2784 stats->flags = CTL_STATS_FLAG_NONE;
2786 getnanouptime(&stats->timestamp);
2789 case CTL_ERROR_INJECT: {
2790 struct ctl_error_desc *err_desc, *new_err_desc;
2791 struct ctl_lun *lun;
2793 err_desc = (struct ctl_error_desc *)addr;
2795 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2797 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2799 mtx_lock(&softc->ctl_lock);
2800 lun = softc->ctl_luns[err_desc->lun_id];
2802 mtx_unlock(&softc->ctl_lock);
2803 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2804 __func__, (uintmax_t)err_desc->lun_id);
2810 * We could do some checking here to verify the validity
2811 * of the request, but given the complexity of error
2812 * injection requests, the checking logic would be fairly
2815 * For now, if the request is invalid, it just won't get
2816 * executed and might get deleted.
2818 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2821 * XXX KDM check to make sure the serial number is unique,
2822 * in case we somehow manage to wrap. That shouldn't
2823 * happen for a very long time, but it's the right thing to
2826 new_err_desc->serial = lun->error_serial;
2827 err_desc->serial = lun->error_serial;
2828 lun->error_serial++;
2830 mtx_unlock(&softc->ctl_lock);
2833 case CTL_ERROR_INJECT_DELETE: {
2834 struct ctl_error_desc *delete_desc, *desc, *desc2;
2835 struct ctl_lun *lun;
2838 delete_desc = (struct ctl_error_desc *)addr;
2841 mtx_lock(&softc->ctl_lock);
2842 lun = softc->ctl_luns[delete_desc->lun_id];
2844 mtx_unlock(&softc->ctl_lock);
2845 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2846 __func__, (uintmax_t)delete_desc->lun_id);
2850 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2851 if (desc->serial != delete_desc->serial)
2854 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2859 mtx_unlock(&softc->ctl_lock);
2860 if (delete_done == 0) {
2861 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2862 "error serial %ju on LUN %u\n", __func__,
2863 delete_desc->serial, delete_desc->lun_id);
2869 case CTL_DUMP_STRUCTS: {
2871 struct ctl_frontend *fe;
2873 printf("CTL IID to WWPN map start:\n");
2874 for (i = 0; i < CTL_MAX_PORTS; i++) {
2875 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2876 if (softc->wwpn_iid[i][j].in_use == 0)
2879 printf("port %d iid %u WWPN %#jx\n",
2880 softc->wwpn_iid[i][j].port,
2881 softc->wwpn_iid[i][j].iid,
2882 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2885 printf("CTL IID to WWPN map end\n");
2886 printf("CTL Persistent Reservation information start:\n");
2887 for (i = 0; i < CTL_MAX_LUNS; i++) {
2888 struct ctl_lun *lun;
2890 lun = softc->ctl_luns[i];
2893 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2896 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2897 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2898 if (lun->per_res[j+k].registered == 0)
2900 printf("LUN %d port %d iid %d key "
2902 (uintmax_t)scsi_8btou64(
2903 lun->per_res[j+k].res_key.key));
2907 printf("CTL Persistent Reservation information end\n");
2908 printf("CTL Frontends:\n");
2910 * XXX KDM calling this without a lock. We'd likely want
2911 * to drop the lock before calling the frontend's dump
2914 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2915 printf("Frontend %s Type %u pport %d vport %d WWNN "
2916 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2917 fe->physical_port, fe->virtual_port,
2918 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2921 * Frontends are not required to support the dump
2924 if (fe->fe_dump == NULL)
2929 printf("CTL Frontend information end\n");
2933 struct ctl_lun_req *lun_req;
2934 struct ctl_backend_driver *backend;
2936 lun_req = (struct ctl_lun_req *)addr;
2938 backend = ctl_backend_find(lun_req->backend);
2939 if (backend == NULL) {
2940 lun_req->status = CTL_LUN_ERROR;
2941 snprintf(lun_req->error_str,
2942 sizeof(lun_req->error_str),
2943 "Backend \"%s\" not found.",
2947 if (lun_req->num_be_args > 0) {
2948 lun_req->kern_be_args = ctl_copyin_args(
2949 lun_req->num_be_args,
2952 sizeof(lun_req->error_str));
2953 if (lun_req->kern_be_args == NULL) {
2954 lun_req->status = CTL_LUN_ERROR;
2959 retval = backend->ioctl(dev, cmd, addr, flag, td);
2961 if (lun_req->num_be_args > 0) {
2962 ctl_free_args(lun_req->num_be_args,
2963 lun_req->kern_be_args);
2967 case CTL_LUN_LIST: {
2969 struct ctl_lun *lun;
2970 struct ctl_lun_list *list;
2972 list = (struct ctl_lun_list *)addr;
2975 * Allocate a fixed length sbuf here, based on the length
2976 * of the user's buffer. We could allocate an auto-extending
2977 * buffer, and then tell the user how much larger our
2978 * amount of data is than his buffer, but that presents
2981 * 1. The sbuf(9) routines use a blocking malloc, and so
2982 * we can't hold a lock while calling them with an
2983 * auto-extending buffer.
2985 * 2. There is not currently a LUN reference counting
2986 * mechanism, outside of outstanding transactions on
2987 * the LUN's OOA queue. So a LUN could go away on us
2988 * while we're getting the LUN number, backend-specific
2989 * information, etc. Thus, given the way things
2990 * currently work, we need to hold the CTL lock while
2991 * grabbing LUN information.
2993 * So, from the user's standpoint, the best thing to do is
2994 * allocate what he thinks is a reasonable buffer length,
2995 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
2996 * double the buffer length and try again. (And repeat
2997 * that until he succeeds.)
2999 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3001 list->status = CTL_LUN_LIST_ERROR;
3002 snprintf(list->error_str, sizeof(list->error_str),
3003 "Unable to allocate %d bytes for LUN list",
3008 sbuf_printf(sb, "<ctllunlist>\n");
3010 mtx_lock(&softc->ctl_lock);
3012 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3013 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3014 (uintmax_t)lun->lun);
3017 * Bail out as soon as we see that we've overfilled
3023 retval = sbuf_printf(sb, "<backend_type>%s"
3024 "</backend_type>\n",
3025 (lun->backend == NULL) ? "none" :
3026 lun->backend->name);
3031 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3032 lun->be_lun->lun_type);
3037 if (lun->backend == NULL) {
3038 retval = sbuf_printf(sb, "</lun>\n");
3044 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3045 (lun->be_lun->maxlba > 0) ?
3046 lun->be_lun->maxlba + 1 : 0);
3051 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3052 lun->be_lun->blocksize);
3057 retval = sbuf_printf(sb, "<serial_number>");
3062 retval = ctl_sbuf_printf_esc(sb,
3063 lun->be_lun->serial_num);
3068 retval = sbuf_printf(sb, "</serial_number>\n");
3073 retval = sbuf_printf(sb, "<device_id>");
3078 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3083 retval = sbuf_printf(sb, "</device_id>\n");
3088 if (lun->backend->lun_info == NULL) {
3089 retval = sbuf_printf(sb, "</lun>\n");
3095 retval =lun->backend->lun_info(lun->be_lun->be_lun, sb);
3100 retval = sbuf_printf(sb, "</lun>\n");
3105 mtx_unlock(&softc->ctl_lock);
3108 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3111 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3112 snprintf(list->error_str, sizeof(list->error_str),
3113 "Out of space, %d bytes is too small",
3120 retval = copyout(sbuf_data(sb), list->lun_xml,
3123 list->fill_len = sbuf_len(sb) + 1;
3124 list->status = CTL_LUN_LIST_OK;
3129 /* XXX KDM should we fix this? */
3131 struct ctl_backend_driver *backend;
3138 * We encode the backend type as the ioctl type for backend
3139 * ioctls. So parse it out here, and then search for a
3140 * backend of this type.
3142 type = _IOC_TYPE(cmd);
3144 STAILQ_FOREACH(backend, &softc->be_list, links) {
3145 if (backend->type == type) {
3151 printf("ctl: unknown ioctl command %#lx or backend "
3156 retval = backend->ioctl(dev, cmd, addr, flag, td);
3166 ctl_get_initindex(struct ctl_nexus *nexus)
3168 if (nexus->targ_port < CTL_MAX_PORTS)
3169 return (nexus->initid.id +
3170 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3172 return (nexus->initid.id +
3173 ((nexus->targ_port - CTL_MAX_PORTS) *
3174 CTL_MAX_INIT_PER_PORT));
3178 ctl_get_resindex(struct ctl_nexus *nexus)
3180 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3184 ctl_port_idx(int port_num)
3186 if (port_num < CTL_MAX_PORTS)
3189 return(port_num - CTL_MAX_PORTS);
3193 * Note: This only works for bitmask sizes that are at least 32 bits, and
3194 * that are a power of 2.
3197 ctl_ffz(uint32_t *mask, uint32_t size)
3199 uint32_t num_chunks, num_pieces;
3202 num_chunks = (size >> 5);
3203 if (num_chunks == 0)
3205 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3207 for (i = 0; i < num_chunks; i++) {
3208 for (j = 0; j < num_pieces; j++) {
3209 if ((mask[i] & (1 << j)) == 0)
3210 return ((i << 5) + j);
3218 ctl_set_mask(uint32_t *mask, uint32_t bit)
3220 uint32_t chunk, piece;
3223 piece = bit % (sizeof(uint32_t) * 8);
3225 if ((mask[chunk] & (1 << piece)) != 0)
3228 mask[chunk] |= (1 << piece);
3234 ctl_clear_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_is_set(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)
3265 * The bus, target and lun are optional, they can be filled in later.
3266 * can_wait is used to determine whether we can wait on the malloc or not.
3269 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3270 uint32_t targ_lun, int can_wait)
3275 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3277 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3280 io->io_hdr.io_type = io_type;
3281 io->io_hdr.targ_port = targ_port;
3283 * XXX KDM this needs to change/go away. We need to move
3284 * to a preallocated pool of ctl_scsiio structures.
3286 io->io_hdr.nexus.targ_target.id = targ_target;
3287 io->io_hdr.nexus.targ_lun = targ_lun;
3294 ctl_kfree_io(union ctl_io *io)
3301 * ctl_softc, pool_type, total_ctl_io are passed in.
3302 * npool is passed out.
3305 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3306 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3309 union ctl_io *cur_io, *next_io;
3310 struct ctl_io_pool *pool;
3315 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3322 pool->type = pool_type;
3323 pool->ctl_softc = ctl_softc;
3325 mtx_lock(&ctl_softc->ctl_lock);
3326 pool->id = ctl_softc->cur_pool_id++;
3327 mtx_unlock(&ctl_softc->ctl_lock);
3329 pool->flags = CTL_POOL_FLAG_NONE;
3330 STAILQ_INIT(&pool->free_queue);
3333 * XXX KDM other options here:
3334 * - allocate a page at a time
3335 * - allocate one big chunk of memory.
3336 * Page allocation might work well, but would take a little more
3339 for (i = 0; i < total_ctl_io; i++) {
3340 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3342 if (cur_io == NULL) {
3346 cur_io->io_hdr.pool = pool;
3347 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3348 pool->total_ctl_io++;
3349 pool->free_ctl_io++;
3353 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3354 cur_io != NULL; cur_io = next_io) {
3355 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3357 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3359 free(cur_io, M_CTL);
3365 mtx_lock(&ctl_softc->ctl_lock);
3366 ctl_softc->num_pools++;
3367 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3369 * Increment our usage count if this is an external consumer, so we
3370 * can't get unloaded until the external consumer (most likely a
3371 * FETD) unloads and frees his pool.
3373 * XXX KDM will this increment the caller's module use count, or
3377 if ((pool_type != CTL_POOL_EMERGENCY)
3378 && (pool_type != CTL_POOL_INTERNAL)
3379 && (pool_type != CTL_POOL_IOCTL)
3380 && (pool_type != CTL_POOL_4OTHERSC))
3384 mtx_unlock(&ctl_softc->ctl_lock);
3394 * Caller must hold ctl_softc->ctl_lock.
3397 ctl_pool_acquire(struct ctl_io_pool *pool)
3402 if (pool->flags & CTL_POOL_FLAG_INVALID)
3411 * Caller must hold ctl_softc->ctl_lock.
3414 ctl_pool_invalidate(struct ctl_io_pool *pool)
3419 pool->flags |= CTL_POOL_FLAG_INVALID;
3425 * Caller must hold ctl_softc->ctl_lock.
3428 ctl_pool_release(struct ctl_io_pool *pool)
3433 if ((--pool->refcount == 0)
3434 && (pool->flags & CTL_POOL_FLAG_INVALID)) {
3435 ctl_pool_free(pool->ctl_softc, pool);
3442 * Must be called with ctl_softc->ctl_lock held.
3445 ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool)
3447 union ctl_io *cur_io, *next_io;
3449 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3450 cur_io != NULL; cur_io = next_io) {
3451 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3453 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, ctl_io_hdr,
3455 free(cur_io, M_CTL);
3458 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3459 ctl_softc->num_pools--;
3462 * XXX KDM will this decrement the caller's usage count or mine?
3465 if ((pool->type != CTL_POOL_EMERGENCY)
3466 && (pool->type != CTL_POOL_INTERNAL)
3467 && (pool->type != CTL_POOL_IOCTL))
3475 * This routine does not block (except for spinlocks of course).
3476 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3480 ctl_alloc_io(void *pool_ref)
3483 struct ctl_softc *ctl_softc;
3484 struct ctl_io_pool *pool, *npool;
3485 struct ctl_io_pool *emergency_pool;
3487 pool = (struct ctl_io_pool *)pool_ref;
3490 printf("%s: pool is NULL\n", __func__);
3494 emergency_pool = NULL;
3496 ctl_softc = pool->ctl_softc;
3498 mtx_lock(&ctl_softc->ctl_lock);
3500 * First, try to get the io structure from the user's pool.
3502 if (ctl_pool_acquire(pool) == 0) {
3503 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3505 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3506 pool->total_allocated++;
3507 pool->free_ctl_io--;
3508 mtx_unlock(&ctl_softc->ctl_lock);
3511 ctl_pool_release(pool);
3514 * If he doesn't have any io structures left, search for an
3515 * emergency pool and grab one from there.
3517 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3518 if (npool->type != CTL_POOL_EMERGENCY)
3521 if (ctl_pool_acquire(npool) != 0)
3524 emergency_pool = npool;
3526 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3528 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3529 npool->total_allocated++;
3530 npool->free_ctl_io--;
3531 mtx_unlock(&ctl_softc->ctl_lock);
3534 ctl_pool_release(npool);
3537 /* Drop the spinlock before we malloc */
3538 mtx_unlock(&ctl_softc->ctl_lock);
3541 * The emergency pool (if it exists) didn't have one, so try an
3542 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3544 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3547 * If the emergency pool exists but is empty, add this
3548 * ctl_io to its list when it gets freed.
3550 if (emergency_pool != NULL) {
3551 mtx_lock(&ctl_softc->ctl_lock);
3552 if (ctl_pool_acquire(emergency_pool) == 0) {
3553 io->io_hdr.pool = emergency_pool;
3554 emergency_pool->total_ctl_io++;
3556 * Need to bump this, otherwise
3557 * total_allocated and total_freed won't
3558 * match when we no longer have anything
3561 emergency_pool->total_allocated++;
3563 mtx_unlock(&ctl_softc->ctl_lock);
3565 io->io_hdr.pool = NULL;
3572 ctl_free_io_internal(union ctl_io *io, int have_lock)
3578 * If this ctl_io has a pool, return it to that pool.
3580 if (io->io_hdr.pool != NULL) {
3581 struct ctl_io_pool *pool;
3583 struct ctl_softc *ctl_softc;
3584 union ctl_io *tmp_io;
3585 unsigned long xflags;
3588 ctl_softc = control_softc;
3591 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3594 mtx_lock(&pool->ctl_softc->ctl_lock);
3598 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3599 &ctl_softc->task_queue); tmp_io != NULL; i++,
3600 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3603 printf("%s: %p is still on the task queue!\n",
3605 printf("%s: (%d): type %d "
3606 "msg %d cdb %x iptl: "
3607 "%d:%d:%d:%d tag 0x%04x "
3610 tmp_io->io_hdr.io_type,
3611 tmp_io->io_hdr.msg_type,
3612 tmp_io->scsiio.cdb[0],
3613 tmp_io->io_hdr.nexus.initid.id,
3614 tmp_io->io_hdr.nexus.targ_port,
3615 tmp_io->io_hdr.nexus.targ_target.id,
3616 tmp_io->io_hdr.nexus.targ_lun,
3617 (tmp_io->io_hdr.io_type ==
3619 tmp_io->taskio.tag_num :
3620 tmp_io->scsiio.tag_num,
3622 panic("I/O still on the task queue!");
3626 io->io_hdr.io_type = 0xff;
3627 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3628 pool->total_freed++;
3629 pool->free_ctl_io++;
3630 ctl_pool_release(pool);
3632 mtx_unlock(&pool->ctl_softc->ctl_lock);
3635 * Otherwise, just free it. We probably malloced it and
3636 * the emergency pool wasn't available.
3644 ctl_free_io(union ctl_io *io)
3646 ctl_free_io_internal(io, /*have_lock*/ 0);
3650 ctl_zero_io(union ctl_io *io)
3658 * May need to preserve linked list pointers at some point too.
3660 pool_ref = io->io_hdr.pool;
3662 memset(io, 0, sizeof(*io));
3664 io->io_hdr.pool = pool_ref;
3668 * This routine is currently used for internal copies of ctl_ios that need
3669 * to persist for some reason after we've already returned status to the
3670 * FETD. (Thus the flag set.)
3673 * Note that this makes a blind copy of all fields in the ctl_io, except
3674 * for the pool reference. This includes any memory that has been
3675 * allocated! That memory will no longer be valid after done has been
3676 * called, so this would be VERY DANGEROUS for command that actually does
3677 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3678 * start and stop commands, which don't transfer any data, so this is not a
3679 * problem. If it is used for anything else, the caller would also need to
3680 * allocate data buffer space and this routine would need to be modified to
3681 * copy the data buffer(s) as well.
3684 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3693 * May need to preserve linked list pointers at some point too.
3695 pool_ref = dest->io_hdr.pool;
3697 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3699 dest->io_hdr.pool = pool_ref;
3701 * We need to know that this is an internal copy, and doesn't need
3702 * to get passed back to the FETD that allocated it.
3704 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3709 ctl_update_power_subpage(struct copan_power_subpage *page)
3711 int num_luns, num_partitions, config_type;
3712 struct ctl_softc *softc;
3713 cs_BOOL_t aor_present, shelf_50pct_power;
3714 cs_raidset_personality_t rs_type;
3715 int max_active_luns;
3717 softc = control_softc;
3719 /* subtract out the processor LUN */
3720 num_luns = softc->num_luns - 1;
3722 * Default to 7 LUNs active, which was the only number we allowed
3725 max_active_luns = 7;
3727 num_partitions = config_GetRsPartitionInfo();
3728 config_type = config_GetConfigType();
3729 shelf_50pct_power = config_GetShelfPowerMode();
3730 aor_present = config_IsAorRsPresent();
3732 rs_type = ddb_GetRsRaidType(1);
3733 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3734 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3735 EPRINT(0, "Unsupported RS type %d!", rs_type);
3739 page->total_luns = num_luns;
3741 switch (config_type) {
3744 * In a 40 drive configuration, it doesn't matter what DC
3745 * cards we have, whether we have AOR enabled or not,
3746 * partitioning or not, or what type of RAIDset we have.
3747 * In that scenario, we can power up every LUN we present
3750 max_active_luns = num_luns;
3754 if (shelf_50pct_power == CS_FALSE) {
3756 if (aor_present == CS_TRUE) {
3758 CS_RAIDSET_PERSONALITY_RAID5) {
3759 max_active_luns = 7;
3760 } else if (rs_type ==
3761 CS_RAIDSET_PERSONALITY_RAID1){
3762 max_active_luns = 14;
3764 /* XXX KDM now what?? */
3768 CS_RAIDSET_PERSONALITY_RAID5) {
3769 max_active_luns = 8;
3770 } else if (rs_type ==
3771 CS_RAIDSET_PERSONALITY_RAID1){
3772 max_active_luns = 16;
3774 /* XXX KDM now what?? */
3780 * With 50% power in a 64 drive configuration, we
3781 * can power all LUNs we present.
3783 max_active_luns = num_luns;
3787 if (shelf_50pct_power == CS_FALSE) {
3789 if (aor_present == CS_TRUE) {
3791 CS_RAIDSET_PERSONALITY_RAID5) {
3792 max_active_luns = 7;
3793 } else if (rs_type ==
3794 CS_RAIDSET_PERSONALITY_RAID1){
3795 max_active_luns = 14;
3797 /* XXX KDM now what?? */
3801 CS_RAIDSET_PERSONALITY_RAID5) {
3802 max_active_luns = 8;
3803 } else if (rs_type ==
3804 CS_RAIDSET_PERSONALITY_RAID1){
3805 max_active_luns = 16;
3807 /* XXX KDM now what?? */
3812 if (aor_present == CS_TRUE) {
3814 CS_RAIDSET_PERSONALITY_RAID5) {
3815 max_active_luns = 14;
3816 } else if (rs_type ==
3817 CS_RAIDSET_PERSONALITY_RAID1){
3819 * We're assuming here that disk
3820 * caching is enabled, and so we're
3821 * able to power up half of each
3822 * LUN, and cache all writes.
3824 max_active_luns = num_luns;
3826 /* XXX KDM now what?? */
3830 CS_RAIDSET_PERSONALITY_RAID5) {
3831 max_active_luns = 15;
3832 } else if (rs_type ==
3833 CS_RAIDSET_PERSONALITY_RAID1){
3834 max_active_luns = 30;
3836 /* XXX KDM now what?? */
3843 * In this case, we have an unknown configuration, so we
3844 * just use the default from above.
3849 page->max_active_luns = max_active_luns;
3851 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3852 page->total_luns, page->max_active_luns);
3855 #endif /* NEEDTOPORT */
3858 * This routine could be used in the future to load default and/or saved
3859 * mode page parameters for a particuar lun.
3862 ctl_init_page_index(struct ctl_lun *lun)
3865 struct ctl_page_index *page_index;
3866 struct ctl_softc *softc;
3868 memcpy(&lun->mode_pages.index, page_index_template,
3869 sizeof(page_index_template));
3871 softc = lun->ctl_softc;
3873 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3875 page_index = &lun->mode_pages.index[i];
3877 * If this is a disk-only mode page, there's no point in
3878 * setting it up. For some pages, we have to have some
3879 * basic information about the disk in order to calculate the
3882 if ((lun->be_lun->lun_type != T_DIRECT)
3883 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3886 switch (page_index->page_code & SMPH_PC_MASK) {
3887 case SMS_FORMAT_DEVICE_PAGE: {
3888 struct scsi_format_page *format_page;
3890 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3891 panic("subpage is incorrect!");
3894 * Sectors per track are set above. Bytes per
3895 * sector need to be set here on a per-LUN basis.
3897 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3898 &format_page_default,
3899 sizeof(format_page_default));
3900 memcpy(&lun->mode_pages.format_page[
3901 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3902 sizeof(format_page_changeable));
3903 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3904 &format_page_default,
3905 sizeof(format_page_default));
3906 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3907 &format_page_default,
3908 sizeof(format_page_default));
3910 format_page = &lun->mode_pages.format_page[
3912 scsi_ulto2b(lun->be_lun->blocksize,
3913 format_page->bytes_per_sector);
3915 format_page = &lun->mode_pages.format_page[
3917 scsi_ulto2b(lun->be_lun->blocksize,
3918 format_page->bytes_per_sector);
3920 format_page = &lun->mode_pages.format_page[
3922 scsi_ulto2b(lun->be_lun->blocksize,
3923 format_page->bytes_per_sector);
3925 page_index->page_data =
3926 (uint8_t *)lun->mode_pages.format_page;
3929 case SMS_RIGID_DISK_PAGE: {
3930 struct scsi_rigid_disk_page *rigid_disk_page;
3931 uint32_t sectors_per_cylinder;
3935 #endif /* !__XSCALE__ */
3937 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3938 panic("invalid subpage value %d",
3939 page_index->subpage);
3942 * Rotation rate and sectors per track are set
3943 * above. We calculate the cylinders here based on
3944 * capacity. Due to the number of heads and
3945 * sectors per track we're using, smaller arrays
3946 * may turn out to have 0 cylinders. Linux and
3947 * FreeBSD don't pay attention to these mode pages
3948 * to figure out capacity, but Solaris does. It
3949 * seems to deal with 0 cylinders just fine, and
3950 * works out a fake geometry based on the capacity.
3952 memcpy(&lun->mode_pages.rigid_disk_page[
3953 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3954 sizeof(rigid_disk_page_default));
3955 memcpy(&lun->mode_pages.rigid_disk_page[
3956 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3957 sizeof(rigid_disk_page_changeable));
3958 memcpy(&lun->mode_pages.rigid_disk_page[
3959 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3960 sizeof(rigid_disk_page_default));
3961 memcpy(&lun->mode_pages.rigid_disk_page[
3962 CTL_PAGE_SAVED], &rigid_disk_page_default,
3963 sizeof(rigid_disk_page_default));
3965 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3969 * The divide method here will be more accurate,
3970 * probably, but results in floating point being
3971 * used in the kernel on i386 (__udivdi3()). On the
3972 * XScale, though, __udivdi3() is implemented in
3975 * The shift method for cylinder calculation is
3976 * accurate if sectors_per_cylinder is a power of
3977 * 2. Otherwise it might be slightly off -- you
3978 * might have a bit of a truncation problem.
3981 cylinders = (lun->be_lun->maxlba + 1) /
3982 sectors_per_cylinder;
3984 for (shift = 31; shift > 0; shift--) {
3985 if (sectors_per_cylinder & (1 << shift))
3988 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3992 * We've basically got 3 bytes, or 24 bits for the
3993 * cylinder size in the mode page. If we're over,
3994 * just round down to 2^24.
3996 if (cylinders > 0xffffff)
3997 cylinders = 0xffffff;
3999 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4001 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4003 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4005 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4007 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4009 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4011 page_index->page_data =
4012 (uint8_t *)lun->mode_pages.rigid_disk_page;
4015 case SMS_CACHING_PAGE: {
4017 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4018 panic("invalid subpage value %d",
4019 page_index->subpage);
4021 * Defaults should be okay here, no calculations
4024 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4025 &caching_page_default,
4026 sizeof(caching_page_default));
4027 memcpy(&lun->mode_pages.caching_page[
4028 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4029 sizeof(caching_page_changeable));
4030 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4031 &caching_page_default,
4032 sizeof(caching_page_default));
4033 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4034 &caching_page_default,
4035 sizeof(caching_page_default));
4036 page_index->page_data =
4037 (uint8_t *)lun->mode_pages.caching_page;
4040 case SMS_CONTROL_MODE_PAGE: {
4042 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4043 panic("invalid subpage value %d",
4044 page_index->subpage);
4047 * Defaults should be okay here, no calculations
4050 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4051 &control_page_default,
4052 sizeof(control_page_default));
4053 memcpy(&lun->mode_pages.control_page[
4054 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4055 sizeof(control_page_changeable));
4056 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4057 &control_page_default,
4058 sizeof(control_page_default));
4059 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4060 &control_page_default,
4061 sizeof(control_page_default));
4062 page_index->page_data =
4063 (uint8_t *)lun->mode_pages.control_page;
4067 case SMS_VENDOR_SPECIFIC_PAGE:{
4068 switch (page_index->subpage) {
4069 case PWR_SUBPAGE_CODE: {
4070 struct copan_power_subpage *current_page,
4073 memcpy(&lun->mode_pages.power_subpage[
4075 &power_page_default,
4076 sizeof(power_page_default));
4077 memcpy(&lun->mode_pages.power_subpage[
4078 CTL_PAGE_CHANGEABLE],
4079 &power_page_changeable,
4080 sizeof(power_page_changeable));
4081 memcpy(&lun->mode_pages.power_subpage[
4083 &power_page_default,
4084 sizeof(power_page_default));
4085 memcpy(&lun->mode_pages.power_subpage[
4087 &power_page_default,
4088 sizeof(power_page_default));
4089 page_index->page_data =
4090 (uint8_t *)lun->mode_pages.power_subpage;
4092 current_page = (struct copan_power_subpage *)
4093 (page_index->page_data +
4094 (page_index->page_len *
4096 saved_page = (struct copan_power_subpage *)
4097 (page_index->page_data +
4098 (page_index->page_len *
4102 case APS_SUBPAGE_CODE: {
4103 struct copan_aps_subpage *current_page,
4106 // This gets set multiple times but
4107 // it should always be the same. It's
4108 // only done during init so who cares.
4109 index_to_aps_page = i;
4111 memcpy(&lun->mode_pages.aps_subpage[
4114 sizeof(aps_page_default));
4115 memcpy(&lun->mode_pages.aps_subpage[
4116 CTL_PAGE_CHANGEABLE],
4117 &aps_page_changeable,
4118 sizeof(aps_page_changeable));
4119 memcpy(&lun->mode_pages.aps_subpage[
4122 sizeof(aps_page_default));
4123 memcpy(&lun->mode_pages.aps_subpage[
4126 sizeof(aps_page_default));
4127 page_index->page_data =
4128 (uint8_t *)lun->mode_pages.aps_subpage;
4130 current_page = (struct copan_aps_subpage *)
4131 (page_index->page_data +
4132 (page_index->page_len *
4134 saved_page = (struct copan_aps_subpage *)
4135 (page_index->page_data +
4136 (page_index->page_len *
4140 case DBGCNF_SUBPAGE_CODE: {
4141 struct copan_debugconf_subpage *current_page,
4144 memcpy(&lun->mode_pages.debugconf_subpage[
4146 &debugconf_page_default,
4147 sizeof(debugconf_page_default));
4148 memcpy(&lun->mode_pages.debugconf_subpage[
4149 CTL_PAGE_CHANGEABLE],
4150 &debugconf_page_changeable,
4151 sizeof(debugconf_page_changeable));
4152 memcpy(&lun->mode_pages.debugconf_subpage[
4154 &debugconf_page_default,
4155 sizeof(debugconf_page_default));
4156 memcpy(&lun->mode_pages.debugconf_subpage[
4158 &debugconf_page_default,
4159 sizeof(debugconf_page_default));
4160 page_index->page_data =
4161 (uint8_t *)lun->mode_pages.debugconf_subpage;
4163 current_page = (struct copan_debugconf_subpage *)
4164 (page_index->page_data +
4165 (page_index->page_len *
4167 saved_page = (struct copan_debugconf_subpage *)
4168 (page_index->page_data +
4169 (page_index->page_len *
4174 panic("invalid subpage value %d",
4175 page_index->subpage);
4181 panic("invalid page value %d",
4182 page_index->page_code & SMPH_PC_MASK);
4187 return (CTL_RETVAL_COMPLETE);
4194 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4195 * wants us to allocate the LUN and he can block.
4196 * - ctl_softc is always set
4197 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4199 * Returns 0 for success, non-zero (errno) for failure.
4202 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4203 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4205 struct ctl_lun *nlun, *lun;
4206 struct ctl_frontend *fe;
4213 * We currently only support Direct Access or Processor LUN types.
4215 switch (be_lun->lun_type) {
4223 be_lun->lun_config_status(be_lun->be_lun,
4224 CTL_LUN_CONFIG_FAILURE);
4227 if (ctl_lun == NULL) {
4228 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4229 lun->flags = CTL_LUN_MALLOCED;
4233 memset(lun, 0, sizeof(*lun));
4235 mtx_lock(&ctl_softc->ctl_lock);
4237 * See if the caller requested a particular LUN number. If so, see
4238 * if it is available. Otherwise, allocate the first available LUN.
4240 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4241 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4242 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4243 mtx_unlock(&ctl_softc->ctl_lock);
4244 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4245 printf("ctl: requested LUN ID %d is higher "
4246 "than CTL_MAX_LUNS - 1 (%d)\n",
4247 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4250 * XXX KDM return an error, or just assign
4251 * another LUN ID in this case??
4253 printf("ctl: requested LUN ID %d is already "
4254 "in use\n", be_lun->req_lun_id);
4256 if (lun->flags & CTL_LUN_MALLOCED)
4258 be_lun->lun_config_status(be_lun->be_lun,
4259 CTL_LUN_CONFIG_FAILURE);
4262 lun_number = be_lun->req_lun_id;
4264 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4265 if (lun_number == -1) {
4266 mtx_unlock(&ctl_softc->ctl_lock);
4267 printf("ctl: can't allocate LUN on target %ju, out of "
4268 "LUNs\n", (uintmax_t)target_id.id);
4269 if (lun->flags & CTL_LUN_MALLOCED)
4271 be_lun->lun_config_status(be_lun->be_lun,
4272 CTL_LUN_CONFIG_FAILURE);
4276 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4278 lun->target = target_id;
4279 lun->lun = lun_number;
4280 lun->be_lun = be_lun;
4282 * The processor LUN is always enabled. Disk LUNs come on line
4283 * disabled, and must be enabled by the backend.
4285 lun->flags = CTL_LUN_DISABLED;
4286 lun->backend = be_lun->be;
4287 be_lun->ctl_lun = lun;
4288 be_lun->lun_id = lun_number;
4289 atomic_add_int(&be_lun->be->num_luns, 1);
4290 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4291 lun->flags |= CTL_LUN_STOPPED;
4293 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4294 lun->flags |= CTL_LUN_INOPERABLE;
4296 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4297 lun->flags |= CTL_LUN_PRIMARY_SC;
4299 lun->ctl_softc = ctl_softc;
4300 TAILQ_INIT(&lun->ooa_queue);
4301 TAILQ_INIT(&lun->blocked_queue);
4302 STAILQ_INIT(&lun->error_list);
4305 * Initialize the mode page index.
4307 ctl_init_page_index(lun);
4310 * Set the poweron UA for all initiators on this LUN only.
4312 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4313 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4316 * Now, before we insert this lun on the lun list, set the lun
4317 * inventory changed UA for all other luns.
4319 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4320 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4321 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4325 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4327 ctl_softc->ctl_luns[lun_number] = lun;
4329 ctl_softc->num_luns++;
4331 /* Setup statistics gathering */
4332 lun->stats.device_type = be_lun->lun_type;
4333 lun->stats.lun_number = lun_number;
4334 if (lun->stats.device_type == T_DIRECT)
4335 lun->stats.blocksize = be_lun->blocksize;
4337 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4338 for (i = 0;i < CTL_MAX_PORTS;i++)
4339 lun->stats.ports[i].targ_port = i;
4341 mtx_unlock(&ctl_softc->ctl_lock);
4343 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4346 * Run through each registered FETD and bring it online if it isn't
4347 * already. Enable the target ID if it hasn't been enabled, and
4348 * enable this particular LUN.
4350 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4354 * XXX KDM this only works for ONE TARGET ID. We'll need
4355 * to do things differently if we go to a multiple target
4358 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4360 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4362 printf("ctl_alloc_lun: FETD %s port %d "
4363 "returned error %d for targ_enable on "
4364 "target %ju\n", fe->port_name,
4365 fe->targ_port, retval,
4366 (uintmax_t)target_id.id);
4368 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4371 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4373 printf("ctl_alloc_lun: FETD %s port %d returned error "
4374 "%d for lun_enable on target %ju lun %d\n",
4375 fe->port_name, fe->targ_port, retval,
4376 (uintmax_t)target_id.id, lun_number);
4378 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4386 * - caller holds ctl_softc->ctl_lock.
4387 * - LUN has already been marked invalid and any pending I/O has been taken
4391 ctl_free_lun(struct ctl_lun *lun)
4393 struct ctl_softc *softc;
4395 struct ctl_frontend *fe;
4397 struct ctl_lun *nlun;
4398 union ctl_io *io, *next_io;
4401 softc = lun->ctl_softc;
4403 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4405 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4407 softc->ctl_luns[lun->lun] = NULL;
4409 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4410 printf("ctl_free_lun: aieee!! freeing a LUN with "
4411 "outstanding I/O!!\n");
4415 * If we have anything pending on the RtR queue, remove it.
4417 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4419 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4420 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4421 && (io->io_hdr.nexus.targ_lun == lun->lun))
4422 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4427 * Then remove everything from the blocked queue.
4429 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4431 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4432 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4433 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4437 * Now clear out the OOA queue, and free all the I/O.
4438 * XXX KDM should we notify the FETD here? We probably need to
4439 * quiesce the LUN before deleting it.
4441 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4443 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4444 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4445 ctl_free_io_internal(io, /*have_lock*/ 1);
4451 * XXX KDM this scheme only works for a single target/multiple LUN
4452 * setup. It needs to be revamped for a multiple target scheme.
4454 * XXX KDM this results in fe->lun_disable() getting called twice,
4455 * once when ctl_disable_lun() is called, and a second time here.
4456 * We really need to re-think the LUN disable semantics. There
4457 * should probably be several steps/levels to LUN removal:
4462 * Right now we only have a disable method when communicating to
4463 * the front end ports, at least for individual LUNs.
4466 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4469 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4472 printf("ctl_free_lun: FETD %s port %d returned error "
4473 "%d for lun_disable on target %ju lun %jd\n",
4474 fe->port_name, fe->targ_port, retval,
4475 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4478 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4479 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4481 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4483 printf("ctl_free_lun: FETD %s port %d "
4484 "returned error %d for targ_disable on "
4485 "target %ju\n", fe->port_name,
4486 fe->targ_port, retval,
4487 (uintmax_t)lun->target.id);
4489 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4491 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4495 fe->port_offline(fe->onoff_arg);
4496 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4503 * Tell the backend to free resources, if this LUN has a backend.
4505 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4506 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4508 if (lun->flags & CTL_LUN_MALLOCED)
4511 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4512 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4513 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4521 ctl_create_lun(struct ctl_be_lun *be_lun)
4523 struct ctl_softc *ctl_softc;
4525 ctl_softc = control_softc;
4528 * ctl_alloc_lun() should handle all potential failure cases.
4530 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4534 ctl_add_lun(struct ctl_be_lun *be_lun)
4536 struct ctl_softc *ctl_softc;
4538 ctl_softc = control_softc;
4540 mtx_lock(&ctl_softc->ctl_lock);
4541 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4542 mtx_unlock(&ctl_softc->ctl_lock);
4544 ctl_wakeup_thread();
4550 ctl_enable_lun(struct ctl_be_lun *be_lun)
4552 struct ctl_softc *ctl_softc;
4553 struct ctl_frontend *fe, *nfe;
4554 struct ctl_lun *lun;
4557 ctl_softc = control_softc;
4559 lun = (struct ctl_lun *)be_lun->ctl_lun;
4561 mtx_lock(&ctl_softc->ctl_lock);
4562 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4564 * eh? Why did we get called if the LUN is already
4567 mtx_unlock(&ctl_softc->ctl_lock);
4570 lun->flags &= ~CTL_LUN_DISABLED;
4572 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4573 nfe = STAILQ_NEXT(fe, links);
4576 * Drop the lock while we call the FETD's enable routine.
4577 * This can lead to a callback into CTL (at least in the
4578 * case of the internal initiator frontend.
4580 mtx_unlock(&ctl_softc->ctl_lock);
4581 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4582 mtx_lock(&ctl_softc->ctl_lock);
4584 printf("%s: FETD %s port %d returned error "
4585 "%d for lun_enable on target %ju lun %jd\n",
4586 __func__, fe->port_name, fe->targ_port, retval,
4587 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4591 /* NOTE: TODO: why does lun enable affect port status? */
4592 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4597 mtx_unlock(&ctl_softc->ctl_lock);
4603 ctl_disable_lun(struct ctl_be_lun *be_lun)
4605 struct ctl_softc *ctl_softc;
4606 struct ctl_frontend *fe;
4607 struct ctl_lun *lun;
4610 ctl_softc = control_softc;
4612 lun = (struct ctl_lun *)be_lun->ctl_lun;
4614 mtx_lock(&ctl_softc->ctl_lock);
4616 if (lun->flags & CTL_LUN_DISABLED) {
4617 mtx_unlock(&ctl_softc->ctl_lock);
4620 lun->flags |= CTL_LUN_DISABLED;
4622 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4623 mtx_unlock(&ctl_softc->ctl_lock);
4625 * Drop the lock before we call the frontend's disable
4626 * routine, to avoid lock order reversals.
4628 * XXX KDM what happens if the frontend list changes while
4629 * we're traversing it? It's unlikely, but should be handled.
4631 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4633 mtx_lock(&ctl_softc->ctl_lock);
4635 printf("ctl_alloc_lun: FETD %s port %d returned error "
4636 "%d for lun_disable on target %ju lun %jd\n",
4637 fe->port_name, fe->targ_port, retval,
4638 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4642 mtx_unlock(&ctl_softc->ctl_lock);
4648 ctl_start_lun(struct ctl_be_lun *be_lun)
4650 struct ctl_softc *ctl_softc;
4651 struct ctl_lun *lun;
4653 ctl_softc = control_softc;
4655 lun = (struct ctl_lun *)be_lun->ctl_lun;
4657 mtx_lock(&ctl_softc->ctl_lock);
4658 lun->flags &= ~CTL_LUN_STOPPED;
4659 mtx_unlock(&ctl_softc->ctl_lock);
4665 ctl_stop_lun(struct ctl_be_lun *be_lun)
4667 struct ctl_softc *ctl_softc;
4668 struct ctl_lun *lun;
4670 ctl_softc = control_softc;
4672 lun = (struct ctl_lun *)be_lun->ctl_lun;
4674 mtx_lock(&ctl_softc->ctl_lock);
4675 lun->flags |= CTL_LUN_STOPPED;
4676 mtx_unlock(&ctl_softc->ctl_lock);
4682 ctl_lun_offline(struct ctl_be_lun *be_lun)
4684 struct ctl_softc *ctl_softc;
4685 struct ctl_lun *lun;
4687 ctl_softc = control_softc;
4689 lun = (struct ctl_lun *)be_lun->ctl_lun;
4691 mtx_lock(&ctl_softc->ctl_lock);
4692 lun->flags |= CTL_LUN_OFFLINE;
4693 mtx_unlock(&ctl_softc->ctl_lock);
4699 ctl_lun_online(struct ctl_be_lun *be_lun)
4701 struct ctl_softc *ctl_softc;
4702 struct ctl_lun *lun;
4704 ctl_softc = control_softc;
4706 lun = (struct ctl_lun *)be_lun->ctl_lun;
4708 mtx_lock(&ctl_softc->ctl_lock);
4709 lun->flags &= ~CTL_LUN_OFFLINE;
4710 mtx_unlock(&ctl_softc->ctl_lock);
4716 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4718 struct ctl_softc *ctl_softc;
4719 struct ctl_lun *lun;
4721 ctl_softc = control_softc;
4723 lun = (struct ctl_lun *)be_lun->ctl_lun;
4725 mtx_lock(&ctl_softc->ctl_lock);
4728 * The LUN needs to be disabled before it can be marked invalid.
4730 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4731 mtx_unlock(&ctl_softc->ctl_lock);
4735 * Mark the LUN invalid.
4737 lun->flags |= CTL_LUN_INVALID;
4740 * If there is nothing in the OOA queue, go ahead and free the LUN.
4741 * If we have something in the OOA queue, we'll free it when the
4742 * last I/O completes.
4744 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4746 mtx_unlock(&ctl_softc->ctl_lock);
4752 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4754 struct ctl_softc *ctl_softc;
4755 struct ctl_lun *lun;
4757 ctl_softc = control_softc;
4758 lun = (struct ctl_lun *)be_lun->ctl_lun;
4760 mtx_lock(&ctl_softc->ctl_lock);
4761 lun->flags |= CTL_LUN_INOPERABLE;
4762 mtx_unlock(&ctl_softc->ctl_lock);
4768 ctl_lun_operable(struct ctl_be_lun *be_lun)
4770 struct ctl_softc *ctl_softc;
4771 struct ctl_lun *lun;
4773 ctl_softc = control_softc;
4774 lun = (struct ctl_lun *)be_lun->ctl_lun;
4776 mtx_lock(&ctl_softc->ctl_lock);
4777 lun->flags &= ~CTL_LUN_INOPERABLE;
4778 mtx_unlock(&ctl_softc->ctl_lock);
4784 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4787 struct ctl_softc *softc;
4788 struct ctl_lun *lun;
4789 struct copan_aps_subpage *current_sp;
4790 struct ctl_page_index *page_index;
4793 softc = control_softc;
4795 mtx_lock(&softc->ctl_lock);
4797 lun = (struct ctl_lun *)be_lun->ctl_lun;
4800 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4801 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4805 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4807 page_index = &lun->mode_pages.index[i];
4810 if (page_index == NULL) {
4811 mtx_unlock(&softc->ctl_lock);
4812 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4813 (uintmax_t)lun->lun);
4817 if ((softc->aps_locked_lun != 0)
4818 && (softc->aps_locked_lun != lun->lun)) {
4819 printf("%s: attempt to lock LUN %llu when %llu is already "
4821 mtx_unlock(&softc->ctl_lock);
4826 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4827 (page_index->page_len * CTL_PAGE_CURRENT));
4830 current_sp->lock_active = APS_LOCK_ACTIVE;
4831 softc->aps_locked_lun = lun->lun;
4833 current_sp->lock_active = 0;
4834 softc->aps_locked_lun = 0;
4839 * If we're in HA mode, try to send the lock message to the other
4842 if (ctl_is_single == 0) {
4844 union ctl_ha_msg lock_msg;
4846 lock_msg.hdr.nexus = *nexus;
4847 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4849 lock_msg.aps.lock_flag = 1;
4851 lock_msg.aps.lock_flag = 0;
4852 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4853 sizeof(lock_msg), 0);
4854 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4855 printf("%s: APS (lock=%d) error returned from "
4856 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4857 mtx_unlock(&softc->ctl_lock);
4862 mtx_unlock(&softc->ctl_lock);
4868 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4870 struct ctl_lun *lun;
4871 struct ctl_softc *softc;
4874 softc = control_softc;
4876 mtx_lock(&softc->ctl_lock);
4878 lun = (struct ctl_lun *)be_lun->ctl_lun;
4880 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4881 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4883 mtx_unlock(&softc->ctl_lock);
4887 * Backend "memory move is complete" callback for requests that never
4888 * make it down to say RAIDCore's configuration code.
4891 ctl_config_move_done(union ctl_io *io)
4895 retval = CTL_RETVAL_COMPLETE;
4898 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4900 * XXX KDM this shouldn't happen, but what if it does?
4902 if (io->io_hdr.io_type != CTL_IO_SCSI)
4903 panic("I/O type isn't CTL_IO_SCSI!");
4905 if ((io->io_hdr.port_status == 0)
4906 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4907 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4908 io->io_hdr.status = CTL_SUCCESS;
4909 else if ((io->io_hdr.port_status != 0)
4910 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4911 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4913 * For hardware error sense keys, the sense key
4914 * specific value is defined to be a retry count,
4915 * but we use it to pass back an internal FETD
4916 * error code. XXX KDM Hopefully the FETD is only
4917 * using 16 bits for an error code, since that's
4918 * all the space we have in the sks field.
4920 ctl_set_internal_failure(&io->scsiio,
4923 io->io_hdr.port_status);
4924 free(io->scsiio.kern_data_ptr, M_CTL);
4929 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4930 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4931 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4933 * XXX KDM just assuming a single pointer here, and not a
4934 * S/G list. If we start using S/G lists for config data,
4935 * we'll need to know how to clean them up here as well.
4937 free(io->scsiio.kern_data_ptr, M_CTL);
4938 /* Hopefully the user has already set the status... */
4942 * XXX KDM now we need to continue data movement. Some
4944 * - call ctl_scsiio() again? We don't do this for data
4945 * writes, because for those at least we know ahead of
4946 * time where the write will go and how long it is. For
4947 * config writes, though, that information is largely
4948 * contained within the write itself, thus we need to
4949 * parse out the data again.
4951 * - Call some other function once the data is in?
4955 * XXX KDM call ctl_scsiio() again for now, and check flag
4956 * bits to see whether we're allocated or not.
4958 retval = ctl_scsiio(&io->scsiio);
4965 * This gets called by a backend driver when it is done with a
4966 * configuration write.
4969 ctl_config_write_done(union ctl_io *io)
4972 * If the IO_CONT flag is set, we need to call the supplied
4973 * function to continue processing the I/O, instead of completing
4976 * If there is an error, though, we don't want to keep processing.
4977 * Instead, just send status back to the initiator.
4979 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
4980 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
4981 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
4982 io->scsiio.io_cont(io);
4986 * Since a configuration write can be done for commands that actually
4987 * have data allocated, like write buffer, and commands that have
4988 * no data, like start/stop unit, we need to check here.
4990 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
4991 free(io->scsiio.kern_data_ptr, M_CTL);
4996 * SCSI release command.
4999 ctl_scsi_release(struct ctl_scsiio *ctsio)
5001 int length, longid, thirdparty_id, resv_id;
5002 struct ctl_softc *ctl_softc;
5003 struct ctl_lun *lun;
5008 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5010 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5011 ctl_softc = control_softc;
5013 switch (ctsio->cdb[0]) {
5015 struct scsi_release *cdb;
5017 cdb = (struct scsi_release *)ctsio->cdb;
5018 if ((cdb->byte2 & 0x1f) != 0) {
5019 ctl_set_invalid_field(ctsio,
5025 ctl_done((union ctl_io *)ctsio);
5026 return (CTL_RETVAL_COMPLETE);
5031 struct scsi_release_10 *cdb;
5033 cdb = (struct scsi_release_10 *)ctsio->cdb;
5035 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5036 ctl_set_invalid_field(ctsio,
5042 ctl_done((union ctl_io *)ctsio);
5043 return (CTL_RETVAL_COMPLETE);
5047 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5048 ctl_set_invalid_field(ctsio,
5054 ctl_done((union ctl_io *)ctsio);
5055 return (CTL_RETVAL_COMPLETE);
5058 if (cdb->byte2 & SR10_LONGID)
5061 thirdparty_id = cdb->thirdparty_id;
5063 resv_id = cdb->resv_id;
5064 length = scsi_2btoul(cdb->length);
5071 * XXX KDM right now, we only support LUN reservation. We don't
5072 * support 3rd party reservations, or extent reservations, which
5073 * might actually need the parameter list. If we've gotten this
5074 * far, we've got a LUN reservation. Anything else got kicked out
5075 * above. So, according to SPC, ignore the length.
5079 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5081 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5082 ctsio->kern_data_len = length;
5083 ctsio->kern_total_len = length;
5084 ctsio->kern_data_resid = 0;
5085 ctsio->kern_rel_offset = 0;
5086 ctsio->kern_sg_entries = 0;
5087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5088 ctsio->be_move_done = ctl_config_move_done;
5089 ctl_datamove((union ctl_io *)ctsio);
5091 return (CTL_RETVAL_COMPLETE);
5095 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5097 mtx_lock(&ctl_softc->ctl_lock);
5100 * According to SPC, it is not an error for an intiator to attempt
5101 * to release a reservation on a LUN that isn't reserved, or that
5102 * is reserved by another initiator. The reservation can only be
5103 * released, though, by the initiator who made it or by one of
5104 * several reset type events.
5106 if (lun->flags & CTL_LUN_RESERVED) {
5107 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5108 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5109 && (ctsio->io_hdr.nexus.targ_target.id ==
5110 lun->rsv_nexus.targ_target.id)) {
5111 lun->flags &= ~CTL_LUN_RESERVED;
5115 ctsio->scsi_status = SCSI_STATUS_OK;
5116 ctsio->io_hdr.status = CTL_SUCCESS;
5118 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5119 free(ctsio->kern_data_ptr, M_CTL);
5120 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5123 mtx_unlock(&ctl_softc->ctl_lock);
5125 ctl_done((union ctl_io *)ctsio);
5126 return (CTL_RETVAL_COMPLETE);
5130 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5132 int extent, thirdparty, longid;
5133 int resv_id, length;
5134 uint64_t thirdparty_id;
5135 struct ctl_softc *ctl_softc;
5136 struct ctl_lun *lun;
5145 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5147 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5148 ctl_softc = control_softc;
5150 switch (ctsio->cdb[0]) {
5152 struct scsi_reserve *cdb;
5154 cdb = (struct scsi_reserve *)ctsio->cdb;
5155 if ((cdb->byte2 & 0x1f) != 0) {
5156 ctl_set_invalid_field(ctsio,
5162 ctl_done((union ctl_io *)ctsio);
5163 return (CTL_RETVAL_COMPLETE);
5165 resv_id = cdb->resv_id;
5166 length = scsi_2btoul(cdb->length);
5170 struct scsi_reserve_10 *cdb;
5172 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5174 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5175 ctl_set_invalid_field(ctsio,
5181 ctl_done((union ctl_io *)ctsio);
5182 return (CTL_RETVAL_COMPLETE);
5184 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5185 ctl_set_invalid_field(ctsio,
5191 ctl_done((union ctl_io *)ctsio);
5192 return (CTL_RETVAL_COMPLETE);
5194 if (cdb->byte2 & SR10_LONGID)
5197 thirdparty_id = cdb->thirdparty_id;
5199 resv_id = cdb->resv_id;
5200 length = scsi_2btoul(cdb->length);
5206 * XXX KDM right now, we only support LUN reservation. We don't
5207 * support 3rd party reservations, or extent reservations, which
5208 * might actually need the parameter list. If we've gotten this
5209 * far, we've got a LUN reservation. Anything else got kicked out
5210 * above. So, according to SPC, ignore the length.
5214 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5216 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5217 ctsio->kern_data_len = length;
5218 ctsio->kern_total_len = length;
5219 ctsio->kern_data_resid = 0;
5220 ctsio->kern_rel_offset = 0;
5221 ctsio->kern_sg_entries = 0;
5222 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5223 ctsio->be_move_done = ctl_config_move_done;
5224 ctl_datamove((union ctl_io *)ctsio);
5226 return (CTL_RETVAL_COMPLETE);
5230 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5232 mtx_lock(&ctl_softc->ctl_lock);
5233 if (lun->flags & CTL_LUN_RESERVED) {
5234 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5235 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5236 || (ctsio->io_hdr.nexus.targ_target.id !=
5237 lun->rsv_nexus.targ_target.id)) {
5238 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5239 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5244 lun->flags |= CTL_LUN_RESERVED;
5245 lun->rsv_nexus = ctsio->io_hdr.nexus;
5247 ctsio->scsi_status = SCSI_STATUS_OK;
5248 ctsio->io_hdr.status = CTL_SUCCESS;
5251 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5252 free(ctsio->kern_data_ptr, M_CTL);
5253 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5256 mtx_unlock(&ctl_softc->ctl_lock);
5258 ctl_done((union ctl_io *)ctsio);
5259 return (CTL_RETVAL_COMPLETE);
5263 ctl_start_stop(struct ctl_scsiio *ctsio)
5265 struct scsi_start_stop_unit *cdb;
5266 struct ctl_lun *lun;
5267 struct ctl_softc *ctl_softc;
5270 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5272 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5273 ctl_softc = control_softc;
5276 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5280 * We don't support the immediate bit on a stop unit. In order to
5281 * do that, we would need to code up a way to know that a stop is
5282 * pending, and hold off any new commands until it completes, one
5283 * way or another. Then we could accept or reject those commands
5284 * depending on its status. We would almost need to do the reverse
5285 * of what we do below for an immediate start -- return the copy of
5286 * the ctl_io to the FETD with status to send to the host (and to
5287 * free the copy!) and then free the original I/O once the stop
5288 * actually completes. That way, the OOA queue mechanism can work
5289 * to block commands that shouldn't proceed. Another alternative
5290 * would be to put the copy in the queue in place of the original,
5291 * and return the original back to the caller. That could be
5294 if ((cdb->byte2 & SSS_IMMED)
5295 && ((cdb->how & SSS_START) == 0)) {
5296 ctl_set_invalid_field(ctsio,
5302 ctl_done((union ctl_io *)ctsio);
5303 return (CTL_RETVAL_COMPLETE);
5307 * We don't support the power conditions field. We need to check
5308 * this prior to checking the load/eject and start/stop bits.
5310 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5311 ctl_set_invalid_field(ctsio,
5317 ctl_done((union ctl_io *)ctsio);
5318 return (CTL_RETVAL_COMPLETE);
5322 * Media isn't removable, so we can't load or eject it.
5324 if ((cdb->how & SSS_LOEJ) != 0) {
5325 ctl_set_invalid_field(ctsio,
5331 ctl_done((union ctl_io *)ctsio);
5332 return (CTL_RETVAL_COMPLETE);
5335 if ((lun->flags & CTL_LUN_PR_RESERVED)
5336 && ((cdb->how & SSS_START)==0)) {
5339 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5340 if (!lun->per_res[residx].registered
5341 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5343 ctl_set_reservation_conflict(ctsio);
5344 ctl_done((union ctl_io *)ctsio);
5345 return (CTL_RETVAL_COMPLETE);
5350 * If there is no backend on this device, we can't start or stop
5351 * it. In theory we shouldn't get any start/stop commands in the
5352 * first place at this level if the LUN doesn't have a backend.
5353 * That should get stopped by the command decode code.
5355 if (lun->backend == NULL) {
5356 ctl_set_invalid_opcode(ctsio);
5357 ctl_done((union ctl_io *)ctsio);
5358 return (CTL_RETVAL_COMPLETE);
5362 * XXX KDM Copan-specific offline behavior.
5363 * Figure out a reasonable way to port this?
5366 mtx_lock(&ctl_softc->ctl_lock);
5368 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5369 && (lun->flags & CTL_LUN_OFFLINE)) {
5371 * If the LUN is offline, and the on/offline bit isn't set,
5372 * reject the start or stop. Otherwise, let it through.
5374 mtx_unlock(&ctl_softc->ctl_lock);
5375 ctl_set_lun_not_ready(ctsio);
5376 ctl_done((union ctl_io *)ctsio);
5378 mtx_unlock(&ctl_softc->ctl_lock);
5379 #endif /* NEEDTOPORT */
5381 * This could be a start or a stop when we're online,
5382 * or a stop/offline or start/online. A start or stop when
5383 * we're offline is covered in the case above.
5386 * In the non-immediate case, we send the request to
5387 * the backend and return status to the user when
5390 * In the immediate case, we allocate a new ctl_io
5391 * to hold a copy of the request, and send that to
5392 * the backend. We then set good status on the
5393 * user's request and return it immediately.
5395 if (cdb->byte2 & SSS_IMMED) {
5396 union ctl_io *new_io;
5398 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5399 if (new_io == NULL) {
5400 ctl_set_busy(ctsio);
5401 ctl_done((union ctl_io *)ctsio);
5403 ctl_copy_io((union ctl_io *)ctsio,
5405 retval = lun->backend->config_write(new_io);
5406 ctl_set_success(ctsio);
5407 ctl_done((union ctl_io *)ctsio);
5410 retval = lun->backend->config_write(
5411 (union ctl_io *)ctsio);
5420 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5421 * we don't really do anything with the LBA and length fields if the user
5422 * passes them in. Instead we'll just flush out the cache for the entire
5426 ctl_sync_cache(struct ctl_scsiio *ctsio)
5428 struct ctl_lun *lun;
5429 struct ctl_softc *ctl_softc;
5430 uint64_t starting_lba;
5431 uint32_t block_count;
5435 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5437 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5438 ctl_softc = control_softc;
5443 switch (ctsio->cdb[0]) {
5444 case SYNCHRONIZE_CACHE: {
5445 struct scsi_sync_cache *cdb;
5446 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5448 if (cdb->byte2 & SSC_RELADR)
5451 if (cdb->byte2 & SSC_IMMED)
5454 starting_lba = scsi_4btoul(cdb->begin_lba);
5455 block_count = scsi_2btoul(cdb->lb_count);
5458 case SYNCHRONIZE_CACHE_16: {
5459 struct scsi_sync_cache_16 *cdb;
5460 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5462 if (cdb->byte2 & SSC_RELADR)
5465 if (cdb->byte2 & SSC_IMMED)
5468 starting_lba = scsi_8btou64(cdb->begin_lba);
5469 block_count = scsi_4btoul(cdb->lb_count);
5473 ctl_set_invalid_opcode(ctsio);
5474 ctl_done((union ctl_io *)ctsio);
5476 break; /* NOTREACHED */
5481 * We don't support the immediate bit. Since it's in the
5482 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5483 * commands, we can just return the same error in either
5486 ctl_set_invalid_field(ctsio,
5492 ctl_done((union ctl_io *)ctsio);
5498 * We don't support the reladr bit either. It can only be
5499 * used with linked commands, and we don't support linked
5500 * commands. Since the bit is in the same place for the
5501 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5502 * just return the same error in either case.
5504 ctl_set_invalid_field(ctsio,
5510 ctl_done((union ctl_io *)ctsio);
5515 * We check the LBA and length, but don't do anything with them.
5516 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5517 * get flushed. This check will just help satisfy anyone who wants
5518 * to see an error for an out of range LBA.
5520 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5521 ctl_set_lba_out_of_range(ctsio);
5522 ctl_done((union ctl_io *)ctsio);
5527 * If this LUN has no backend, we can't flush the cache anyway.
5529 if (lun->backend == NULL) {
5530 ctl_set_invalid_opcode(ctsio);
5531 ctl_done((union ctl_io *)ctsio);
5536 * Check to see whether we're configured to send the SYNCHRONIZE
5537 * CACHE command directly to the back end.
5539 mtx_lock(&ctl_softc->ctl_lock);
5540 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5541 && (++(lun->sync_count) >= lun->sync_interval)) {
5542 lun->sync_count = 0;
5543 mtx_unlock(&ctl_softc->ctl_lock);
5544 retval = lun->backend->config_write((union ctl_io *)ctsio);
5546 mtx_unlock(&ctl_softc->ctl_lock);
5547 ctl_set_success(ctsio);
5548 ctl_done((union ctl_io *)ctsio);
5557 ctl_format(struct ctl_scsiio *ctsio)
5559 struct scsi_format *cdb;
5560 struct ctl_lun *lun;
5561 struct ctl_softc *ctl_softc;
5562 int length, defect_list_len;
5564 CTL_DEBUG_PRINT(("ctl_format\n"));
5566 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5567 ctl_softc = control_softc;
5569 cdb = (struct scsi_format *)ctsio->cdb;
5572 if (cdb->byte2 & SF_FMTDATA) {
5573 if (cdb->byte2 & SF_LONGLIST)
5574 length = sizeof(struct scsi_format_header_long);
5576 length = sizeof(struct scsi_format_header_short);
5579 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5581 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5582 ctsio->kern_data_len = length;
5583 ctsio->kern_total_len = length;
5584 ctsio->kern_data_resid = 0;
5585 ctsio->kern_rel_offset = 0;
5586 ctsio->kern_sg_entries = 0;
5587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5588 ctsio->be_move_done = ctl_config_move_done;
5589 ctl_datamove((union ctl_io *)ctsio);
5591 return (CTL_RETVAL_COMPLETE);
5594 defect_list_len = 0;
5596 if (cdb->byte2 & SF_FMTDATA) {
5597 if (cdb->byte2 & SF_LONGLIST) {
5598 struct scsi_format_header_long *header;
5600 header = (struct scsi_format_header_long *)
5601 ctsio->kern_data_ptr;
5603 defect_list_len = scsi_4btoul(header->defect_list_len);
5604 if (defect_list_len != 0) {
5605 ctl_set_invalid_field(ctsio,
5614 struct scsi_format_header_short *header;
5616 header = (struct scsi_format_header_short *)
5617 ctsio->kern_data_ptr;
5619 defect_list_len = scsi_2btoul(header->defect_list_len);
5620 if (defect_list_len != 0) {
5621 ctl_set_invalid_field(ctsio,
5633 * The format command will clear out the "Medium format corrupted"
5634 * status if set by the configuration code. That status is really
5635 * just a way to notify the host that we have lost the media, and
5636 * get them to issue a command that will basically make them think
5637 * they're blowing away the media.
5639 mtx_lock(&ctl_softc->ctl_lock);
5640 lun->flags &= ~CTL_LUN_INOPERABLE;
5641 mtx_unlock(&ctl_softc->ctl_lock);
5643 ctsio->scsi_status = SCSI_STATUS_OK;
5644 ctsio->io_hdr.status = CTL_SUCCESS;
5647 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5648 free(ctsio->kern_data_ptr, M_CTL);
5649 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5652 ctl_done((union ctl_io *)ctsio);
5653 return (CTL_RETVAL_COMPLETE);
5657 ctl_write_buffer(struct ctl_scsiio *ctsio)
5659 struct scsi_write_buffer *cdb;
5660 struct copan_page_header *header;
5661 struct ctl_lun *lun;
5662 struct ctl_softc *ctl_softc;
5663 int buffer_offset, len;
5668 retval = CTL_RETVAL_COMPLETE;
5670 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5672 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5673 ctl_softc = control_softc;
5674 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5676 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5677 ctl_set_invalid_field(ctsio,
5683 ctl_done((union ctl_io *)ctsio);
5684 return (CTL_RETVAL_COMPLETE);
5686 if (cdb->buffer_id != 0) {
5687 ctl_set_invalid_field(ctsio,
5693 ctl_done((union ctl_io *)ctsio);
5694 return (CTL_RETVAL_COMPLETE);
5697 len = scsi_3btoul(cdb->length);
5698 buffer_offset = scsi_3btoul(cdb->offset);
5700 if (len > sizeof(lun->write_buffer)) {
5701 ctl_set_invalid_field(ctsio,
5707 ctl_done((union ctl_io *)ctsio);
5708 return (CTL_RETVAL_COMPLETE);
5711 if (buffer_offset != 0) {
5712 ctl_set_invalid_field(ctsio,
5718 ctl_done((union ctl_io *)ctsio);
5719 return (CTL_RETVAL_COMPLETE);
5723 * If we've got a kernel request that hasn't been malloced yet,
5724 * malloc it and tell the caller the data buffer is here.
5726 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5727 ctsio->kern_data_ptr = lun->write_buffer;
5728 ctsio->kern_data_len = len;
5729 ctsio->kern_total_len = len;
5730 ctsio->kern_data_resid = 0;
5731 ctsio->kern_rel_offset = 0;
5732 ctsio->kern_sg_entries = 0;
5733 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5734 ctsio->be_move_done = ctl_config_move_done;
5735 ctl_datamove((union ctl_io *)ctsio);
5737 return (CTL_RETVAL_COMPLETE);
5740 ctl_done((union ctl_io *)ctsio);
5742 return (CTL_RETVAL_COMPLETE);
5746 * Note that this function currently doesn't actually do anything inside
5747 * CTL to enforce things if the DQue bit is turned on.
5749 * Also note that this function can't be used in the default case, because
5750 * the DQue bit isn't set in the changeable mask for the control mode page
5751 * anyway. This is just here as an example for how to implement a page
5752 * handler, and a placeholder in case we want to allow the user to turn
5753 * tagged queueing on and off.
5755 * The D_SENSE bit handling is functional, however, and will turn
5756 * descriptor sense on and off for a given LUN.
5759 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5760 struct ctl_page_index *page_index, uint8_t *page_ptr)
5762 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5763 struct ctl_lun *lun;
5764 struct ctl_softc *softc;
5768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5769 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5772 user_cp = (struct scsi_control_page *)page_ptr;
5773 current_cp = (struct scsi_control_page *)
5774 (page_index->page_data + (page_index->page_len *
5776 saved_cp = (struct scsi_control_page *)
5777 (page_index->page_data + (page_index->page_len *
5780 softc = control_softc;
5782 mtx_lock(&softc->ctl_lock);
5783 if (((current_cp->rlec & SCP_DSENSE) == 0)
5784 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5786 * Descriptor sense is currently turned off and the user
5787 * wants to turn it on.
5789 current_cp->rlec |= SCP_DSENSE;
5790 saved_cp->rlec |= SCP_DSENSE;
5791 lun->flags |= CTL_LUN_SENSE_DESC;
5793 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5794 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5796 * Descriptor sense is currently turned on, and the user
5797 * wants to turn it off.
5799 current_cp->rlec &= ~SCP_DSENSE;
5800 saved_cp->rlec &= ~SCP_DSENSE;
5801 lun->flags &= ~CTL_LUN_SENSE_DESC;
5804 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5805 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5807 csevent_log(CSC_CTL | CSC_SHELF_SW |
5809 csevent_LogType_Trace,
5810 csevent_Severity_Information,
5811 csevent_AlertLevel_Green,
5812 csevent_FRU_Firmware,
5813 csevent_FRU_Unknown,
5814 "Received untagged to untagged transition");
5815 #endif /* NEEDTOPORT */
5818 csevent_log(CSC_CTL | CSC_SHELF_SW |
5820 csevent_LogType_ConfigChange,
5821 csevent_Severity_Information,
5822 csevent_AlertLevel_Green,
5823 csevent_FRU_Firmware,
5824 csevent_FRU_Unknown,
5825 "Received untagged to tagged "
5826 "queueing transition");
5827 #endif /* NEEDTOPORT */
5829 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5830 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5834 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5836 csevent_log(CSC_CTL | CSC_SHELF_SW |
5838 csevent_LogType_ConfigChange,
5839 csevent_Severity_Warning,
5840 csevent_AlertLevel_Yellow,
5841 csevent_FRU_Firmware,
5842 csevent_FRU_Unknown,
5843 "Received tagged queueing to untagged "
5845 #endif /* NEEDTOPORT */
5847 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5848 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5852 csevent_log(CSC_CTL | CSC_SHELF_SW |
5854 csevent_LogType_Trace,
5855 csevent_Severity_Information,
5856 csevent_AlertLevel_Green,
5857 csevent_FRU_Firmware,
5858 csevent_FRU_Unknown,
5859 "Received tagged queueing to tagged "
5860 "queueing transition");
5861 #endif /* NEEDTOPORT */
5867 * Let other initiators know that the mode
5868 * parameters for this LUN have changed.
5870 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5874 lun->pending_sense[i].ua_pending |=
5878 mtx_unlock(&softc->ctl_lock);
5884 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5885 struct ctl_page_index *page_index, uint8_t *page_ptr)
5891 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5892 struct ctl_page_index *page_index, int pc)
5894 struct copan_power_subpage *page;
5896 page = (struct copan_power_subpage *)page_index->page_data +
5897 (page_index->page_len * pc);
5900 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5902 * We don't update the changable bits for this page.
5905 case SMS_PAGE_CTRL_CURRENT >> 6:
5906 case SMS_PAGE_CTRL_DEFAULT >> 6:
5907 case SMS_PAGE_CTRL_SAVED >> 6:
5909 ctl_update_power_subpage(page);
5914 EPRINT(0, "Invalid PC %d!!", pc);
5923 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5924 struct ctl_page_index *page_index, uint8_t *page_ptr)
5926 struct copan_aps_subpage *user_sp;
5927 struct copan_aps_subpage *current_sp;
5928 union ctl_modepage_info *modepage_info;
5929 struct ctl_softc *softc;
5930 struct ctl_lun *lun;
5933 retval = CTL_RETVAL_COMPLETE;
5934 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5935 (page_index->page_len * CTL_PAGE_CURRENT));
5936 softc = control_softc;
5937 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5939 user_sp = (struct copan_aps_subpage *)page_ptr;
5941 modepage_info = (union ctl_modepage_info *)
5942 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5944 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5945 modepage_info->header.subpage = page_index->subpage;
5946 modepage_info->aps.lock_active = user_sp->lock_active;
5948 mtx_lock(&softc->ctl_lock);
5951 * If there is a request to lock the LUN and another LUN is locked
5952 * this is an error. If the requested LUN is already locked ignore
5953 * the request. If no LUN is locked attempt to lock it.
5954 * if there is a request to unlock the LUN and the LUN is currently
5955 * locked attempt to unlock it. Otherwise ignore the request. i.e.
5956 * if another LUN is locked or no LUN is locked.
5958 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
5959 if (softc->aps_locked_lun == lun->lun) {
5961 * This LUN is already locked, so we're done.
5963 retval = CTL_RETVAL_COMPLETE;
5964 } else if (softc->aps_locked_lun == 0) {
5966 * No one has the lock, pass the request to the
5969 retval = lun->backend->config_write(
5970 (union ctl_io *)ctsio);
5973 * Someone else has the lock, throw out the request.
5975 ctl_set_already_locked(ctsio);
5976 free(ctsio->kern_data_ptr, M_CTL);
5977 ctl_done((union ctl_io *)ctsio);
5980 * Set the return value so that ctl_do_mode_select()
5981 * won't try to complete the command. We already
5982 * completed it here.
5984 retval = CTL_RETVAL_ERROR;
5986 } else if (softc->aps_locked_lun == lun->lun) {
5988 * This LUN is locked, so pass the unlock request to the
5991 retval = lun->backend->config_write((union ctl_io *)ctsio);
5993 mtx_unlock(&softc->ctl_lock);
5999 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6000 struct ctl_page_index *page_index,
6006 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6011 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6012 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6013 printf("page data:");
6015 printf(" %.2x",page_ptr[i]);
6021 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6022 struct ctl_page_index *page_index,
6025 struct copan_debugconf_subpage *page;
6027 page = (struct copan_debugconf_subpage *)page_index->page_data +
6028 (page_index->page_len * pc);
6031 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6032 case SMS_PAGE_CTRL_DEFAULT >> 6:
6033 case SMS_PAGE_CTRL_SAVED >> 6:
6035 * We don't update the changable or default bits for this page.
6038 case SMS_PAGE_CTRL_CURRENT >> 6:
6039 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6040 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6044 EPRINT(0, "Invalid PC %d!!", pc);
6045 #endif /* NEEDTOPORT */
6053 ctl_do_mode_select(union ctl_io *io)
6055 struct scsi_mode_page_header *page_header;
6056 struct ctl_page_index *page_index;
6057 struct ctl_scsiio *ctsio;
6058 int control_dev, page_len;
6059 int page_len_offset, page_len_size;
6060 union ctl_modepage_info *modepage_info;
6061 struct ctl_lun *lun;
6062 int *len_left, *len_used;
6065 ctsio = &io->scsiio;
6068 retval = CTL_RETVAL_COMPLETE;
6070 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6072 if (lun->be_lun->lun_type != T_DIRECT)
6077 modepage_info = (union ctl_modepage_info *)
6078 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6079 len_left = &modepage_info->header.len_left;
6080 len_used = &modepage_info->header.len_used;
6084 page_header = (struct scsi_mode_page_header *)
6085 (ctsio->kern_data_ptr + *len_used);
6087 if (*len_left == 0) {
6088 free(ctsio->kern_data_ptr, M_CTL);
6089 ctl_set_success(ctsio);
6090 ctl_done((union ctl_io *)ctsio);
6091 return (CTL_RETVAL_COMPLETE);
6092 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6094 free(ctsio->kern_data_ptr, M_CTL);
6095 ctl_set_param_len_error(ctsio);
6096 ctl_done((union ctl_io *)ctsio);
6097 return (CTL_RETVAL_COMPLETE);
6099 } else if ((page_header->page_code & SMPH_SPF)
6100 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6102 free(ctsio->kern_data_ptr, M_CTL);
6103 ctl_set_param_len_error(ctsio);
6104 ctl_done((union ctl_io *)ctsio);
6105 return (CTL_RETVAL_COMPLETE);
6110 * XXX KDM should we do something with the block descriptor?
6112 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6114 if ((control_dev != 0)
6115 && (lun->mode_pages.index[i].page_flags &
6116 CTL_PAGE_FLAG_DISK_ONLY))
6119 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6120 (page_header->page_code & SMPH_PC_MASK))
6124 * If neither page has a subpage code, then we've got a
6127 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6128 && ((page_header->page_code & SMPH_SPF) == 0)) {
6129 page_index = &lun->mode_pages.index[i];
6130 page_len = page_header->page_length;
6135 * If both pages have subpages, then the subpage numbers
6138 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6139 && (page_header->page_code & SMPH_SPF)) {
6140 struct scsi_mode_page_header_sp *sph;
6142 sph = (struct scsi_mode_page_header_sp *)page_header;
6144 if (lun->mode_pages.index[i].subpage ==
6146 page_index = &lun->mode_pages.index[i];
6147 page_len = scsi_2btoul(sph->page_length);
6154 * If we couldn't find the page, or if we don't have a mode select
6155 * handler for it, send back an error to the user.
6157 if ((page_index == NULL)
6158 || (page_index->select_handler == NULL)) {
6159 ctl_set_invalid_field(ctsio,
6162 /*field*/ *len_used,
6165 free(ctsio->kern_data_ptr, M_CTL);
6166 ctl_done((union ctl_io *)ctsio);
6167 return (CTL_RETVAL_COMPLETE);
6170 if (page_index->page_code & SMPH_SPF) {
6171 page_len_offset = 2;
6175 page_len_offset = 1;
6179 * If the length the initiator gives us isn't the one we specify in
6180 * the mode page header, or if they didn't specify enough data in
6181 * the CDB to avoid truncating this page, kick out the request.
6183 if ((page_len != (page_index->page_len - page_len_offset -
6185 || (*len_left < page_index->page_len)) {
6188 ctl_set_invalid_field(ctsio,
6191 /*field*/ *len_used + page_len_offset,
6194 free(ctsio->kern_data_ptr, M_CTL);
6195 ctl_done((union ctl_io *)ctsio);
6196 return (CTL_RETVAL_COMPLETE);
6200 * Run through the mode page, checking to make sure that the bits
6201 * the user changed are actually legal for him to change.
6203 for (i = 0; i < page_index->page_len; i++) {
6204 uint8_t *user_byte, *change_mask, *current_byte;
6208 user_byte = (uint8_t *)page_header + i;
6209 change_mask = page_index->page_data +
6210 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6211 current_byte = page_index->page_data +
6212 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6215 * Check to see whether the user set any bits in this byte
6216 * that he is not allowed to set.
6218 if ((*user_byte & ~(*change_mask)) ==
6219 (*current_byte & ~(*change_mask)))
6223 * Go through bit by bit to determine which one is illegal.
6226 for (j = 7; j >= 0; j--) {
6227 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6228 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6233 ctl_set_invalid_field(ctsio,
6236 /*field*/ *len_used + i,
6239 free(ctsio->kern_data_ptr, M_CTL);
6240 ctl_done((union ctl_io *)ctsio);
6241 return (CTL_RETVAL_COMPLETE);
6245 * Decrement these before we call the page handler, since we may
6246 * end up getting called back one way or another before the handler
6247 * returns to this context.
6249 *len_left -= page_index->page_len;
6250 *len_used += page_index->page_len;
6252 retval = page_index->select_handler(ctsio, page_index,
6253 (uint8_t *)page_header);
6256 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6257 * wait until this queued command completes to finish processing
6258 * the mode page. If it returns anything other than
6259 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6260 * already set the sense information, freed the data pointer, and
6261 * completed the io for us.
6263 if (retval != CTL_RETVAL_COMPLETE)
6264 goto bailout_no_done;
6267 * If the initiator sent us more than one page, parse the next one.
6272 ctl_set_success(ctsio);
6273 free(ctsio->kern_data_ptr, M_CTL);
6274 ctl_done((union ctl_io *)ctsio);
6278 return (CTL_RETVAL_COMPLETE);
6283 ctl_mode_select(struct ctl_scsiio *ctsio)
6285 int param_len, pf, sp;
6286 int header_size, bd_len;
6287 int len_left, len_used;
6288 struct ctl_page_index *page_index;
6289 struct ctl_lun *lun;
6290 int control_dev, page_len;
6291 union ctl_modepage_info *modepage_info;
6303 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6305 if (lun->be_lun->lun_type != T_DIRECT)
6310 switch (ctsio->cdb[0]) {
6311 case MODE_SELECT_6: {
6312 struct scsi_mode_select_6 *cdb;
6314 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6316 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6317 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6319 param_len = cdb->length;
6320 header_size = sizeof(struct scsi_mode_header_6);
6323 case MODE_SELECT_10: {
6324 struct scsi_mode_select_10 *cdb;
6326 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6328 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6329 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6331 param_len = scsi_2btoul(cdb->length);
6332 header_size = sizeof(struct scsi_mode_header_10);
6336 ctl_set_invalid_opcode(ctsio);
6337 ctl_done((union ctl_io *)ctsio);
6338 return (CTL_RETVAL_COMPLETE);
6339 break; /* NOTREACHED */
6344 * "A parameter list length of zero indicates that the Data-Out Buffer
6345 * shall be empty. This condition shall not be considered as an error."
6347 if (param_len == 0) {
6348 ctl_set_success(ctsio);
6349 ctl_done((union ctl_io *)ctsio);
6350 return (CTL_RETVAL_COMPLETE);
6354 * Since we'll hit this the first time through, prior to
6355 * allocation, we don't need to free a data buffer here.
6357 if (param_len < header_size) {
6358 ctl_set_param_len_error(ctsio);
6359 ctl_done((union ctl_io *)ctsio);
6360 return (CTL_RETVAL_COMPLETE);
6364 * Allocate the data buffer and grab the user's data. In theory,
6365 * we shouldn't have to sanity check the parameter list length here
6366 * because the maximum size is 64K. We should be able to malloc
6367 * that much without too many problems.
6369 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6370 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6371 ctsio->kern_data_len = param_len;
6372 ctsio->kern_total_len = param_len;
6373 ctsio->kern_data_resid = 0;
6374 ctsio->kern_rel_offset = 0;
6375 ctsio->kern_sg_entries = 0;
6376 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6377 ctsio->be_move_done = ctl_config_move_done;
6378 ctl_datamove((union ctl_io *)ctsio);
6380 return (CTL_RETVAL_COMPLETE);
6383 switch (ctsio->cdb[0]) {
6384 case MODE_SELECT_6: {
6385 struct scsi_mode_header_6 *mh6;
6387 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6388 bd_len = mh6->blk_desc_len;
6391 case MODE_SELECT_10: {
6392 struct scsi_mode_header_10 *mh10;
6394 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6395 bd_len = scsi_2btoul(mh10->blk_desc_len);
6399 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6403 if (param_len < (header_size + bd_len)) {
6404 free(ctsio->kern_data_ptr, M_CTL);
6405 ctl_set_param_len_error(ctsio);
6406 ctl_done((union ctl_io *)ctsio);
6407 return (CTL_RETVAL_COMPLETE);
6411 * Set the IO_CONT flag, so that if this I/O gets passed to
6412 * ctl_config_write_done(), it'll get passed back to
6413 * ctl_do_mode_select() for further processing, or completion if
6416 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6417 ctsio->io_cont = ctl_do_mode_select;
6419 modepage_info = (union ctl_modepage_info *)
6420 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6422 memset(modepage_info, 0, sizeof(*modepage_info));
6424 len_left = param_len - header_size - bd_len;
6425 len_used = header_size + bd_len;
6427 modepage_info->header.len_left = len_left;
6428 modepage_info->header.len_used = len_used;
6430 return (ctl_do_mode_select((union ctl_io *)ctsio));
6434 ctl_mode_sense(struct ctl_scsiio *ctsio)
6436 struct ctl_lun *lun;
6437 int pc, page_code, dbd, llba, subpage;
6438 int alloc_len, page_len, header_len, total_len;
6439 struct scsi_mode_block_descr *block_desc;
6440 struct ctl_page_index *page_index;
6448 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6450 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6452 if (lun->be_lun->lun_type != T_DIRECT)
6457 switch (ctsio->cdb[0]) {
6458 case MODE_SENSE_6: {
6459 struct scsi_mode_sense_6 *cdb;
6461 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6463 header_len = sizeof(struct scsi_mode_hdr_6);
6464 if (cdb->byte2 & SMS_DBD)
6467 header_len += sizeof(struct scsi_mode_block_descr);
6469 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6470 page_code = cdb->page & SMS_PAGE_CODE;
6471 subpage = cdb->subpage;
6472 alloc_len = cdb->length;
6475 case MODE_SENSE_10: {
6476 struct scsi_mode_sense_10 *cdb;
6478 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6480 header_len = sizeof(struct scsi_mode_hdr_10);
6482 if (cdb->byte2 & SMS_DBD)
6485 header_len += sizeof(struct scsi_mode_block_descr);
6486 if (cdb->byte2 & SMS10_LLBAA)
6488 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6489 page_code = cdb->page & SMS_PAGE_CODE;
6490 subpage = cdb->subpage;
6491 alloc_len = scsi_2btoul(cdb->length);
6495 ctl_set_invalid_opcode(ctsio);
6496 ctl_done((union ctl_io *)ctsio);
6497 return (CTL_RETVAL_COMPLETE);
6498 break; /* NOTREACHED */
6502 * We have to make a first pass through to calculate the size of
6503 * the pages that match the user's query. Then we allocate enough
6504 * memory to hold it, and actually copy the data into the buffer.
6506 switch (page_code) {
6507 case SMS_ALL_PAGES_PAGE: {
6513 * At the moment, values other than 0 and 0xff here are
6514 * reserved according to SPC-3.
6516 if ((subpage != SMS_SUBPAGE_PAGE_0)
6517 && (subpage != SMS_SUBPAGE_ALL)) {
6518 ctl_set_invalid_field(ctsio,
6524 ctl_done((union ctl_io *)ctsio);
6525 return (CTL_RETVAL_COMPLETE);
6528 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6529 if ((control_dev != 0)
6530 && (lun->mode_pages.index[i].page_flags &
6531 CTL_PAGE_FLAG_DISK_ONLY))
6535 * We don't use this subpage if the user didn't
6536 * request all subpages.
6538 if ((lun->mode_pages.index[i].subpage != 0)
6539 && (subpage == SMS_SUBPAGE_PAGE_0))
6543 printf("found page %#x len %d\n",
6544 lun->mode_pages.index[i].page_code &
6546 lun->mode_pages.index[i].page_len);
6548 page_len += lun->mode_pages.index[i].page_len;
6557 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6558 /* Look for the right page code */
6559 if ((lun->mode_pages.index[i].page_code &
6560 SMPH_PC_MASK) != page_code)
6563 /* Look for the right subpage or the subpage wildcard*/
6564 if ((lun->mode_pages.index[i].subpage != subpage)
6565 && (subpage != SMS_SUBPAGE_ALL))
6568 /* Make sure the page is supported for this dev type */
6569 if ((control_dev != 0)
6570 && (lun->mode_pages.index[i].page_flags &
6571 CTL_PAGE_FLAG_DISK_ONLY))
6575 printf("found page %#x len %d\n",
6576 lun->mode_pages.index[i].page_code &
6578 lun->mode_pages.index[i].page_len);
6581 page_len += lun->mode_pages.index[i].page_len;
6584 if (page_len == 0) {
6585 ctl_set_invalid_field(ctsio,
6591 ctl_done((union ctl_io *)ctsio);
6592 return (CTL_RETVAL_COMPLETE);
6598 total_len = header_len + page_len;
6600 printf("header_len = %d, page_len = %d, total_len = %d\n",
6601 header_len, page_len, total_len);
6604 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6605 ctsio->kern_sg_entries = 0;
6606 ctsio->kern_data_resid = 0;
6607 ctsio->kern_rel_offset = 0;
6608 if (total_len < alloc_len) {
6609 ctsio->residual = alloc_len - total_len;
6610 ctsio->kern_data_len = total_len;
6611 ctsio->kern_total_len = total_len;
6613 ctsio->residual = 0;
6614 ctsio->kern_data_len = alloc_len;
6615 ctsio->kern_total_len = alloc_len;
6618 switch (ctsio->cdb[0]) {
6619 case MODE_SENSE_6: {
6620 struct scsi_mode_hdr_6 *header;
6622 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6624 header->datalen = ctl_min(total_len - 1, 254);
6627 header->block_descr_len = 0;
6629 header->block_descr_len =
6630 sizeof(struct scsi_mode_block_descr);
6631 block_desc = (struct scsi_mode_block_descr *)&header[1];
6634 case MODE_SENSE_10: {
6635 struct scsi_mode_hdr_10 *header;
6638 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6640 datalen = ctl_min(total_len - 2, 65533);
6641 scsi_ulto2b(datalen, header->datalen);
6643 scsi_ulto2b(0, header->block_descr_len);
6645 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6646 header->block_descr_len);
6647 block_desc = (struct scsi_mode_block_descr *)&header[1];
6651 panic("invalid CDB type %#x", ctsio->cdb[0]);
6652 break; /* NOTREACHED */
6656 * If we've got a disk, use its blocksize in the block
6657 * descriptor. Otherwise, just set it to 0.
6660 if (control_dev != 0)
6661 scsi_ulto3b(lun->be_lun->blocksize,
6662 block_desc->block_len);
6664 scsi_ulto3b(0, block_desc->block_len);
6667 switch (page_code) {
6668 case SMS_ALL_PAGES_PAGE: {
6671 data_used = header_len;
6672 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6673 struct ctl_page_index *page_index;
6675 page_index = &lun->mode_pages.index[i];
6677 if ((control_dev != 0)
6678 && (page_index->page_flags &
6679 CTL_PAGE_FLAG_DISK_ONLY))
6683 * We don't use this subpage if the user didn't
6684 * request all subpages. We already checked (above)
6685 * to make sure the user only specified a subpage
6686 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6688 if ((page_index->subpage != 0)
6689 && (subpage == SMS_SUBPAGE_PAGE_0))
6693 * Call the handler, if it exists, to update the
6694 * page to the latest values.
6696 if (page_index->sense_handler != NULL)
6697 page_index->sense_handler(ctsio, page_index,pc);
6699 memcpy(ctsio->kern_data_ptr + data_used,
6700 page_index->page_data +
6701 (page_index->page_len * pc),
6702 page_index->page_len);
6703 data_used += page_index->page_len;
6710 data_used = header_len;
6712 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6713 struct ctl_page_index *page_index;
6715 page_index = &lun->mode_pages.index[i];
6717 /* Look for the right page code */
6718 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6721 /* Look for the right subpage or the subpage wildcard*/
6722 if ((page_index->subpage != subpage)
6723 && (subpage != SMS_SUBPAGE_ALL))
6726 /* Make sure the page is supported for this dev type */
6727 if ((control_dev != 0)
6728 && (page_index->page_flags &
6729 CTL_PAGE_FLAG_DISK_ONLY))
6733 * Call the handler, if it exists, to update the
6734 * page to the latest values.
6736 if (page_index->sense_handler != NULL)
6737 page_index->sense_handler(ctsio, page_index,pc);
6739 memcpy(ctsio->kern_data_ptr + data_used,
6740 page_index->page_data +
6741 (page_index->page_len * pc),
6742 page_index->page_len);
6743 data_used += page_index->page_len;
6749 ctsio->scsi_status = SCSI_STATUS_OK;
6751 ctsio->be_move_done = ctl_config_move_done;
6752 ctl_datamove((union ctl_io *)ctsio);
6754 return (CTL_RETVAL_COMPLETE);
6758 ctl_read_capacity(struct ctl_scsiio *ctsio)
6760 struct scsi_read_capacity *cdb;
6761 struct scsi_read_capacity_data *data;
6762 struct ctl_lun *lun;
6765 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6767 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6769 lba = scsi_4btoul(cdb->addr);
6770 if (((cdb->pmi & SRC_PMI) == 0)
6772 ctl_set_invalid_field(/*ctsio*/ ctsio,
6778 ctl_done((union ctl_io *)ctsio);
6779 return (CTL_RETVAL_COMPLETE);
6782 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6784 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6785 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6786 ctsio->residual = 0;
6787 ctsio->kern_data_len = sizeof(*data);
6788 ctsio->kern_total_len = sizeof(*data);
6789 ctsio->kern_data_resid = 0;
6790 ctsio->kern_rel_offset = 0;
6791 ctsio->kern_sg_entries = 0;
6794 * If the maximum LBA is greater than 0xfffffffe, the user must
6795 * issue a SERVICE ACTION IN (16) command, with the read capacity
6796 * serivce action set.
6798 if (lun->be_lun->maxlba > 0xfffffffe)
6799 scsi_ulto4b(0xffffffff, data->addr);
6801 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6804 * XXX KDM this may not be 512 bytes...
6806 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6808 ctsio->scsi_status = SCSI_STATUS_OK;
6810 ctsio->be_move_done = ctl_config_move_done;
6811 ctl_datamove((union ctl_io *)ctsio);
6813 return (CTL_RETVAL_COMPLETE);
6817 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6819 struct scsi_read_capacity_16 *cdb;
6820 struct scsi_read_capacity_data_long *data;
6821 struct ctl_lun *lun;
6825 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6827 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6829 alloc_len = scsi_4btoul(cdb->alloc_len);
6830 lba = scsi_8btou64(cdb->addr);
6832 if ((cdb->reladr & SRC16_PMI)
6834 ctl_set_invalid_field(/*ctsio*/ ctsio,
6840 ctl_done((union ctl_io *)ctsio);
6841 return (CTL_RETVAL_COMPLETE);
6844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6846 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6847 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6849 if (sizeof(*data) < alloc_len) {
6850 ctsio->residual = alloc_len - sizeof(*data);
6851 ctsio->kern_data_len = sizeof(*data);
6852 ctsio->kern_total_len = sizeof(*data);
6854 ctsio->residual = 0;
6855 ctsio->kern_data_len = alloc_len;
6856 ctsio->kern_total_len = alloc_len;
6858 ctsio->kern_data_resid = 0;
6859 ctsio->kern_rel_offset = 0;
6860 ctsio->kern_sg_entries = 0;
6862 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6863 /* XXX KDM this may not be 512 bytes... */
6864 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6866 ctsio->scsi_status = SCSI_STATUS_OK;
6868 ctsio->be_move_done = ctl_config_move_done;
6869 ctl_datamove((union ctl_io *)ctsio);
6871 return (CTL_RETVAL_COMPLETE);
6875 ctl_service_action_in(struct ctl_scsiio *ctsio)
6877 struct scsi_service_action_in *cdb;
6880 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
6882 cdb = (struct scsi_service_action_in *)ctsio->cdb;
6884 retval = CTL_RETVAL_COMPLETE;
6886 switch (cdb->service_action) {
6887 case SRC16_SERVICE_ACTION:
6888 retval = ctl_read_capacity_16(ctsio);
6891 ctl_set_invalid_field(/*ctsio*/ ctsio,
6897 ctl_done((union ctl_io *)ctsio);
6905 ctl_maintenance_in(struct ctl_scsiio *ctsio)
6907 struct scsi_maintenance_in *cdb;
6909 int alloc_len, total_len = 0;
6910 int num_target_port_groups;
6911 struct ctl_lun *lun;
6912 struct ctl_softc *softc;
6913 struct scsi_target_group_data *rtg_ptr;
6914 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6915 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6916 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6918 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
6920 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6921 softc = control_softc;
6922 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6924 retval = CTL_RETVAL_COMPLETE;
6925 mtx_lock(&softc->ctl_lock);
6927 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
6928 ctl_set_invalid_field(/*ctsio*/ ctsio,
6934 ctl_done((union ctl_io *)ctsio);
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];
6976 if (ctl_is_single == 0) {
6977 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
6978 &tp_desc_ptr1_2->desc_list[0];
6979 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
6980 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
6981 &tp_desc_ptr2_1->desc_list[0];
6983 tpg_desc_ptr2 = NULL;
6984 tp_desc_ptr2_1 = NULL;
6985 tp_desc_ptr2_2 = NULL;
6988 scsi_ulto4b(total_len - 4, rtg_ptr->length);
6989 if (ctl_is_single == 0) {
6990 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
6991 if (lun->flags & CTL_LUN_PRIMARY_SC) {
6992 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
6993 tpg_desc_ptr2->pref_state =
6994 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6996 tpg_desc_ptr1->pref_state =
6997 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
6998 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7001 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7002 tpg_desc_ptr1->pref_state =
7003 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7004 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7006 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7007 tpg_desc_ptr2->pref_state =
7008 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7012 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7014 tpg_desc_ptr1->support = 0;
7015 tpg_desc_ptr1->target_port_group[1] = 1;
7016 tpg_desc_ptr1->status = TPG_IMPLICIT;
7017 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7019 if (ctl_is_single == 0) {
7020 tpg_desc_ptr2->support = 0;
7021 tpg_desc_ptr2->target_port_group[1] = 2;
7022 tpg_desc_ptr2->status = TPG_IMPLICIT;
7023 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7025 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7026 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7028 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7029 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7031 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7032 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7033 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7035 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7036 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7040 mtx_unlock(&softc->ctl_lock);
7042 ctsio->be_move_done = ctl_config_move_done;
7044 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7045 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7046 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7047 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7048 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7050 ctl_datamove((union ctl_io *)ctsio);
7055 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7057 struct scsi_per_res_in *cdb;
7058 int alloc_len, total_len = 0;
7059 /* struct scsi_per_res_in_rsrv in_data; */
7060 struct ctl_lun *lun;
7061 struct ctl_softc *softc;
7063 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7065 softc = control_softc;
7067 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7069 alloc_len = scsi_2btoul(cdb->length);
7071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7074 mtx_lock(&softc->ctl_lock);
7075 switch (cdb->action) {
7076 case SPRI_RK: /* read keys */
7077 total_len = sizeof(struct scsi_per_res_in_keys) +
7079 sizeof(struct scsi_per_res_key);
7081 case SPRI_RR: /* read reservation */
7082 if (lun->flags & CTL_LUN_PR_RESERVED)
7083 total_len = sizeof(struct scsi_per_res_in_rsrv);
7085 total_len = sizeof(struct scsi_per_res_in_header);
7087 case SPRI_RC: /* report capabilities */
7088 total_len = sizeof(struct scsi_per_res_cap);
7090 case SPRI_RS: /* read full status */
7092 mtx_unlock(&softc->ctl_lock);
7093 ctl_set_invalid_field(ctsio,
7099 ctl_done((union ctl_io *)ctsio);
7100 return (CTL_RETVAL_COMPLETE);
7101 break; /* NOTREACHED */
7103 mtx_unlock(&softc->ctl_lock);
7105 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7107 if (total_len < alloc_len) {
7108 ctsio->residual = alloc_len - total_len;
7109 ctsio->kern_data_len = total_len;
7110 ctsio->kern_total_len = total_len;
7112 ctsio->residual = 0;
7113 ctsio->kern_data_len = alloc_len;
7114 ctsio->kern_total_len = alloc_len;
7117 ctsio->kern_data_resid = 0;
7118 ctsio->kern_rel_offset = 0;
7119 ctsio->kern_sg_entries = 0;
7121 mtx_lock(&softc->ctl_lock);
7122 switch (cdb->action) {
7123 case SPRI_RK: { // read keys
7124 struct scsi_per_res_in_keys *res_keys;
7127 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7130 * We had to drop the lock to allocate our buffer, which
7131 * leaves time for someone to come in with another
7132 * persistent reservation. (That is unlikely, though,
7133 * since this should be the only persistent reservation
7134 * command active right now.)
7136 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7137 (lun->pr_key_count *
7138 sizeof(struct scsi_per_res_key)))){
7139 mtx_unlock(&softc->ctl_lock);
7140 free(ctsio->kern_data_ptr, M_CTL);
7141 printf("%s: reservation length changed, retrying\n",
7146 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7148 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7149 lun->pr_key_count, res_keys->header.length);
7151 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7152 if (!lun->per_res[i].registered)
7156 * We used lun->pr_key_count to calculate the
7157 * size to allocate. If it turns out the number of
7158 * initiators with the registered flag set is
7159 * larger than that (i.e. they haven't been kept in
7160 * sync), we've got a problem.
7162 if (key_count >= lun->pr_key_count) {
7164 csevent_log(CSC_CTL | CSC_SHELF_SW |
7166 csevent_LogType_Fault,
7167 csevent_AlertLevel_Yellow,
7168 csevent_FRU_ShelfController,
7169 csevent_FRU_Firmware,
7170 csevent_FRU_Unknown,
7171 "registered keys %d >= key "
7172 "count %d", key_count,
7178 memcpy(res_keys->keys[key_count].key,
7179 lun->per_res[i].res_key.key,
7180 ctl_min(sizeof(res_keys->keys[key_count].key),
7181 sizeof(lun->per_res[i].res_key)));
7186 case SPRI_RR: { // read reservation
7187 struct scsi_per_res_in_rsrv *res;
7188 int tmp_len, header_only;
7190 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7192 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7194 if (lun->flags & CTL_LUN_PR_RESERVED)
7196 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7197 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7198 res->header.length);
7201 tmp_len = sizeof(struct scsi_per_res_in_header);
7202 scsi_ulto4b(0, res->header.length);
7207 * We had to drop the lock to allocate our buffer, which
7208 * leaves time for someone to come in with another
7209 * persistent reservation. (That is unlikely, though,
7210 * since this should be the only persistent reservation
7211 * command active right now.)
7213 if (tmp_len != total_len) {
7214 mtx_unlock(&softc->ctl_lock);
7215 free(ctsio->kern_data_ptr, M_CTL);
7216 printf("%s: reservation status changed, retrying\n",
7222 * No reservation held, so we're done.
7224 if (header_only != 0)
7228 * If the registration is an All Registrants type, the key
7229 * is 0, since it doesn't really matter.
7231 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7232 memcpy(res->data.reservation,
7233 &lun->per_res[lun->pr_res_idx].res_key,
7234 sizeof(struct scsi_per_res_key));
7236 res->data.scopetype = lun->res_type;
7239 case SPRI_RC: //report capabilities
7241 struct scsi_per_res_cap *res_cap;
7244 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7245 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7246 res_cap->flags2 |= SPRI_TMV;
7247 type_mask = SPRI_TM_WR_EX_AR |
7253 scsi_ulto2b(type_mask, res_cap->type_mask);
7256 case SPRI_RS: //read full status
7259 * This is a bug, because we just checked for this above,
7260 * and should have returned an error.
7262 panic("Invalid PR type %x", cdb->action);
7263 break; /* NOTREACHED */
7265 mtx_unlock(&softc->ctl_lock);
7267 ctsio->be_move_done = ctl_config_move_done;
7269 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7270 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7271 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7272 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7273 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7275 ctl_datamove((union ctl_io *)ctsio);
7277 return (CTL_RETVAL_COMPLETE);
7281 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7285 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7286 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7287 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7288 struct scsi_per_res_out_parms* param)
7290 union ctl_ha_msg persis_io;
7296 if (sa_res_key == 0) {
7297 mtx_lock(&softc->ctl_lock);
7298 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7299 /* validate scope and type */
7300 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7302 mtx_unlock(&softc->ctl_lock);
7303 ctl_set_invalid_field(/*ctsio*/ ctsio,
7309 ctl_done((union ctl_io *)ctsio);
7313 if (type>8 || type==2 || type==4 || type==0) {
7314 mtx_unlock(&softc->ctl_lock);
7315 ctl_set_invalid_field(/*ctsio*/ ctsio,
7321 ctl_done((union ctl_io *)ctsio);
7325 /* temporarily unregister this nexus */
7326 lun->per_res[residx].registered = 0;
7329 * Unregister everybody else and build UA for
7332 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7333 if (lun->per_res[i].registered == 0)
7337 && i <CTL_MAX_INITIATORS)
7338 lun->pending_sense[i].ua_pending |=
7340 else if (persis_offset
7341 && i >= persis_offset)
7342 lun->pending_sense[i-persis_offset
7345 lun->per_res[i].registered = 0;
7346 memset(&lun->per_res[i].res_key, 0,
7347 sizeof(struct scsi_per_res_key));
7349 lun->per_res[residx].registered = 1;
7350 lun->pr_key_count = 1;
7351 lun->res_type = type;
7352 if (lun->res_type != SPR_TYPE_WR_EX_AR
7353 && lun->res_type != SPR_TYPE_EX_AC_AR)
7354 lun->pr_res_idx = residx;
7356 mtx_unlock(&softc->ctl_lock);
7357 /* send msg to other side */
7358 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7359 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7360 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7361 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7362 persis_io.pr.pr_info.res_type = type;
7363 memcpy(persis_io.pr.pr_info.sa_res_key,
7364 param->serv_act_res_key,
7365 sizeof(param->serv_act_res_key));
7366 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7367 &persis_io, sizeof(persis_io), 0)) >
7368 CTL_HA_STATUS_SUCCESS) {
7369 printf("CTL:Persis Out error returned "
7370 "from ctl_ha_msg_send %d\n",
7374 /* not all registrants */
7375 mtx_unlock(&softc->ctl_lock);
7376 free(ctsio->kern_data_ptr, M_CTL);
7377 ctl_set_invalid_field(ctsio,
7383 ctl_done((union ctl_io *)ctsio);
7386 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7387 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7390 mtx_lock(&softc->ctl_lock);
7391 if (res_key == sa_res_key) {
7394 * The spec implies this is not good but doesn't
7395 * say what to do. There are two choices either
7396 * generate a res conflict or check condition
7397 * with illegal field in parameter data. Since
7398 * that is what is done when the sa_res_key is
7399 * zero I'll take that approach since this has
7400 * to do with the sa_res_key.
7402 mtx_unlock(&softc->ctl_lock);
7403 free(ctsio->kern_data_ptr, M_CTL);
7404 ctl_set_invalid_field(ctsio,
7410 ctl_done((union ctl_io *)ctsio);
7414 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7415 if (lun->per_res[i].registered
7416 && memcmp(param->serv_act_res_key,
7417 lun->per_res[i].res_key.key,
7418 sizeof(struct scsi_per_res_key)) != 0)
7422 lun->per_res[i].registered = 0;
7423 memset(&lun->per_res[i].res_key, 0,
7424 sizeof(struct scsi_per_res_key));
7425 lun->pr_key_count--;
7428 && i < CTL_MAX_INITIATORS)
7429 lun->pending_sense[i].ua_pending |=
7431 else if (persis_offset
7432 && i >= persis_offset)
7433 lun->pending_sense[i-persis_offset].ua_pending|=
7436 mtx_unlock(&softc->ctl_lock);
7438 free(ctsio->kern_data_ptr, M_CTL);
7439 ctl_set_reservation_conflict(ctsio);
7440 ctl_done((union ctl_io *)ctsio);
7441 return (CTL_RETVAL_COMPLETE);
7443 /* send msg to other side */
7444 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7445 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7446 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7447 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7448 persis_io.pr.pr_info.res_type = type;
7449 memcpy(persis_io.pr.pr_info.sa_res_key,
7450 param->serv_act_res_key,
7451 sizeof(param->serv_act_res_key));
7452 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7453 &persis_io, sizeof(persis_io), 0)) >
7454 CTL_HA_STATUS_SUCCESS) {
7455 printf("CTL:Persis Out error returned from "
7456 "ctl_ha_msg_send %d\n", isc_retval);
7459 /* Reserved but not all registrants */
7460 /* sa_res_key is res holder */
7461 if (memcmp(param->serv_act_res_key,
7462 lun->per_res[lun->pr_res_idx].res_key.key,
7463 sizeof(struct scsi_per_res_key)) == 0) {
7464 /* validate scope and type */
7465 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7467 ctl_set_invalid_field(/*ctsio*/ ctsio,
7473 ctl_done((union ctl_io *)ctsio);
7477 if (type>8 || type==2 || type==4 || type==0) {
7478 ctl_set_invalid_field(/*ctsio*/ ctsio,
7484 ctl_done((union ctl_io *)ctsio);
7490 * if sa_res_key != res_key remove all
7491 * registrants w/sa_res_key and generate UA
7492 * for these registrants(Registrations
7493 * Preempted) if it wasn't an exclusive
7494 * reservation generate UA(Reservations
7495 * Preempted) for all other registered nexuses
7496 * if the type has changed. Establish the new
7497 * reservation and holder. If res_key and
7498 * sa_res_key are the same do the above
7499 * except don't unregister the res holder.
7503 * Temporarily unregister so it won't get
7504 * removed or UA generated
7506 lun->per_res[residx].registered = 0;
7507 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7508 if (lun->per_res[i].registered == 0)
7511 if (memcmp(param->serv_act_res_key,
7512 lun->per_res[i].res_key.key,
7513 sizeof(struct scsi_per_res_key)) == 0) {
7514 lun->per_res[i].registered = 0;
7515 memset(&lun->per_res[i].res_key,
7517 sizeof(struct scsi_per_res_key));
7518 lun->pr_key_count--;
7521 && i < CTL_MAX_INITIATORS)
7522 lun->pending_sense[i
7525 else if (persis_offset
7526 && i >= persis_offset)
7528 i-persis_offset].ua_pending |=
7530 } else if (type != lun->res_type
7531 && (lun->res_type == SPR_TYPE_WR_EX_RO
7532 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7534 && i < CTL_MAX_INITIATORS)
7535 lun->pending_sense[i
7538 else if (persis_offset
7539 && i >= persis_offset)
7546 lun->per_res[residx].registered = 1;
7547 lun->res_type = type;
7548 if (lun->res_type != SPR_TYPE_WR_EX_AR
7549 && lun->res_type != SPR_TYPE_EX_AC_AR)
7550 lun->pr_res_idx = residx;
7553 CTL_PR_ALL_REGISTRANTS;
7555 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7556 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7557 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7558 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7559 persis_io.pr.pr_info.res_type = type;
7560 memcpy(persis_io.pr.pr_info.sa_res_key,
7561 param->serv_act_res_key,
7562 sizeof(param->serv_act_res_key));
7563 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7564 &persis_io, sizeof(persis_io), 0)) >
7565 CTL_HA_STATUS_SUCCESS) {
7566 printf("CTL:Persis Out error returned "
7567 "from ctl_ha_msg_send %d\n",
7572 * sa_res_key is not the res holder just
7573 * remove registrants
7576 mtx_lock(&softc->ctl_lock);
7578 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7579 if (memcmp(param->serv_act_res_key,
7580 lun->per_res[i].res_key.key,
7581 sizeof(struct scsi_per_res_key)) != 0)
7585 lun->per_res[i].registered = 0;
7586 memset(&lun->per_res[i].res_key, 0,
7587 sizeof(struct scsi_per_res_key));
7588 lun->pr_key_count--;
7591 && i < CTL_MAX_INITIATORS)
7592 lun->pending_sense[i].ua_pending |=
7594 else if (persis_offset
7595 && i >= persis_offset)
7597 i-persis_offset].ua_pending |=
7602 mtx_unlock(&softc->ctl_lock);
7603 free(ctsio->kern_data_ptr, M_CTL);
7604 ctl_set_reservation_conflict(ctsio);
7605 ctl_done((union ctl_io *)ctsio);
7608 mtx_unlock(&softc->ctl_lock);
7609 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7610 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7611 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7612 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7613 persis_io.pr.pr_info.res_type = type;
7614 memcpy(persis_io.pr.pr_info.sa_res_key,
7615 param->serv_act_res_key,
7616 sizeof(param->serv_act_res_key));
7617 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7618 &persis_io, sizeof(persis_io), 0)) >
7619 CTL_HA_STATUS_SUCCESS) {
7620 printf("CTL:Persis Out error returned "
7621 "from ctl_ha_msg_send %d\n",
7627 lun->PRGeneration++;
7633 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7637 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7638 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7639 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7640 msg->pr.pr_info.sa_res_key,
7641 sizeof(struct scsi_per_res_key)) != 0) {
7642 uint64_t sa_res_key;
7643 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7645 if (sa_res_key == 0) {
7646 /* temporarily unregister this nexus */
7647 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7650 * Unregister everybody else and build UA for
7653 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7654 if (lun->per_res[i].registered == 0)
7658 && i < CTL_MAX_INITIATORS)
7659 lun->pending_sense[i].ua_pending |=
7661 else if (persis_offset && i >= persis_offset)
7662 lun->pending_sense[i -
7663 persis_offset].ua_pending |=
7665 lun->per_res[i].registered = 0;
7666 memset(&lun->per_res[i].res_key, 0,
7667 sizeof(struct scsi_per_res_key));
7670 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7671 lun->pr_key_count = 1;
7672 lun->res_type = msg->pr.pr_info.res_type;
7673 if (lun->res_type != SPR_TYPE_WR_EX_AR
7674 && lun->res_type != SPR_TYPE_EX_AC_AR)
7675 lun->pr_res_idx = msg->pr.pr_info.residx;
7677 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7678 if (memcmp(msg->pr.pr_info.sa_res_key,
7679 lun->per_res[i].res_key.key,
7680 sizeof(struct scsi_per_res_key)) != 0)
7683 lun->per_res[i].registered = 0;
7684 memset(&lun->per_res[i].res_key, 0,
7685 sizeof(struct scsi_per_res_key));
7686 lun->pr_key_count--;
7689 && i < persis_offset)
7690 lun->pending_sense[i].ua_pending |=
7692 else if (persis_offset
7693 && i >= persis_offset)
7694 lun->pending_sense[i -
7695 persis_offset].ua_pending |=
7701 * Temporarily unregister so it won't get removed
7704 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7705 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7706 if (lun->per_res[i].registered == 0)
7709 if (memcmp(msg->pr.pr_info.sa_res_key,
7710 lun->per_res[i].res_key.key,
7711 sizeof(struct scsi_per_res_key)) == 0) {
7712 lun->per_res[i].registered = 0;
7713 memset(&lun->per_res[i].res_key, 0,
7714 sizeof(struct scsi_per_res_key));
7715 lun->pr_key_count--;
7717 && i < CTL_MAX_INITIATORS)
7718 lun->pending_sense[i].ua_pending |=
7720 else if (persis_offset
7721 && i >= persis_offset)
7722 lun->pending_sense[i -
7723 persis_offset].ua_pending |=
7725 } else if (msg->pr.pr_info.res_type != lun->res_type
7726 && (lun->res_type == SPR_TYPE_WR_EX_RO
7727 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7729 && i < persis_offset)
7730 lun->pending_sense[i
7733 else if (persis_offset
7734 && i >= persis_offset)
7735 lun->pending_sense[i -
7736 persis_offset].ua_pending |=
7740 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7741 lun->res_type = msg->pr.pr_info.res_type;
7742 if (lun->res_type != SPR_TYPE_WR_EX_AR
7743 && lun->res_type != SPR_TYPE_EX_AC_AR)
7744 lun->pr_res_idx = msg->pr.pr_info.residx;
7746 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7748 lun->PRGeneration++;
7754 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7758 u_int32_t param_len;
7759 struct scsi_per_res_out *cdb;
7760 struct ctl_lun *lun;
7761 struct scsi_per_res_out_parms* param;
7762 struct ctl_softc *softc;
7764 uint64_t res_key, sa_res_key;
7766 union ctl_ha_msg persis_io;
7769 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7771 retval = CTL_RETVAL_COMPLETE;
7773 softc = control_softc;
7775 cdb = (struct scsi_per_res_out *)ctsio->cdb;
7776 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7779 * We only support whole-LUN scope. The scope & type are ignored for
7780 * register, register and ignore existing key and clear.
7781 * We sometimes ignore scope and type on preempts too!!
7782 * Verify reservation type here as well.
7784 type = cdb->scope_type & SPR_TYPE_MASK;
7785 if ((cdb->action == SPRO_RESERVE)
7786 || (cdb->action == SPRO_RELEASE)) {
7787 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
7788 ctl_set_invalid_field(/*ctsio*/ ctsio,
7794 ctl_done((union ctl_io *)ctsio);
7795 return (CTL_RETVAL_COMPLETE);
7798 if (type>8 || type==2 || type==4 || type==0) {
7799 ctl_set_invalid_field(/*ctsio*/ ctsio,
7805 ctl_done((union ctl_io *)ctsio);
7806 return (CTL_RETVAL_COMPLETE);
7810 switch (cdb->action & SPRO_ACTION_MASK) {
7821 ctl_set_invalid_field(/*ctsio*/ ctsio,
7827 ctl_done((union ctl_io *)ctsio);
7828 return (CTL_RETVAL_COMPLETE);
7829 break; /* NOTREACHED */
7832 param_len = scsi_4btoul(cdb->length);
7834 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
7835 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
7836 ctsio->kern_data_len = param_len;
7837 ctsio->kern_total_len = param_len;
7838 ctsio->kern_data_resid = 0;
7839 ctsio->kern_rel_offset = 0;
7840 ctsio->kern_sg_entries = 0;
7841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7842 ctsio->be_move_done = ctl_config_move_done;
7843 ctl_datamove((union ctl_io *)ctsio);
7845 return (CTL_RETVAL_COMPLETE);
7848 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
7850 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7851 res_key = scsi_8btou64(param->res_key.key);
7852 sa_res_key = scsi_8btou64(param->serv_act_res_key);
7855 * Validate the reservation key here except for SPRO_REG_IGNO
7856 * This must be done for all other service actions
7858 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
7859 mtx_lock(&softc->ctl_lock);
7860 if (lun->per_res[residx].registered) {
7861 if (memcmp(param->res_key.key,
7862 lun->per_res[residx].res_key.key,
7863 ctl_min(sizeof(param->res_key),
7864 sizeof(lun->per_res[residx].res_key))) != 0) {
7866 * The current key passed in doesn't match
7867 * the one the initiator previously
7870 mtx_unlock(&softc->ctl_lock);
7871 free(ctsio->kern_data_ptr, M_CTL);
7872 ctl_set_reservation_conflict(ctsio);
7873 ctl_done((union ctl_io *)ctsio);
7874 return (CTL_RETVAL_COMPLETE);
7876 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
7878 * We are not registered
7880 mtx_unlock(&softc->ctl_lock);
7881 free(ctsio->kern_data_ptr, M_CTL);
7882 ctl_set_reservation_conflict(ctsio);
7883 ctl_done((union ctl_io *)ctsio);
7884 return (CTL_RETVAL_COMPLETE);
7885 } else if (res_key != 0) {
7887 * We are not registered and trying to register but
7888 * the register key isn't zero.
7890 mtx_unlock(&softc->ctl_lock);
7891 free(ctsio->kern_data_ptr, M_CTL);
7892 ctl_set_reservation_conflict(ctsio);
7893 ctl_done((union ctl_io *)ctsio);
7894 return (CTL_RETVAL_COMPLETE);
7896 mtx_unlock(&softc->ctl_lock);
7899 switch (cdb->action & SPRO_ACTION_MASK) {
7901 case SPRO_REG_IGNO: {
7904 printf("Registration received\n");
7908 * We don't support any of these options, as we report in
7909 * the read capabilities request (see
7910 * ctl_persistent_reserve_in(), above).
7912 if ((param->flags & SPR_SPEC_I_PT)
7913 || (param->flags & SPR_ALL_TG_PT)
7914 || (param->flags & SPR_APTPL)) {
7917 if (param->flags & SPR_APTPL)
7919 else if (param->flags & SPR_ALL_TG_PT)
7921 else /* SPR_SPEC_I_PT */
7924 free(ctsio->kern_data_ptr, M_CTL);
7925 ctl_set_invalid_field(ctsio,
7931 ctl_done((union ctl_io *)ctsio);
7932 return (CTL_RETVAL_COMPLETE);
7935 mtx_lock(&softc->ctl_lock);
7938 * The initiator wants to clear the
7941 if (sa_res_key == 0) {
7943 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
7944 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
7945 && !lun->per_res[residx].registered)) {
7946 mtx_unlock(&softc->ctl_lock);
7950 lun->per_res[residx].registered = 0;
7951 memset(&lun->per_res[residx].res_key,
7952 0, sizeof(lun->per_res[residx].res_key));
7953 lun->pr_key_count--;
7955 if (residx == lun->pr_res_idx) {
7956 lun->flags &= ~CTL_LUN_PR_RESERVED;
7957 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7959 if ((lun->res_type == SPR_TYPE_WR_EX_RO
7960 || lun->res_type == SPR_TYPE_EX_AC_RO)
7961 && lun->pr_key_count) {
7963 * If the reservation is a registrants
7964 * only type we need to generate a UA
7965 * for other registered inits. The
7966 * sense code should be RESERVATIONS
7970 for (i = 0; i < CTL_MAX_INITIATORS;i++){
7972 i+persis_offset].registered
7975 lun->pending_sense[i
7981 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7982 if (lun->pr_key_count==0) {
7983 lun->flags &= ~CTL_LUN_PR_RESERVED;
7985 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
7988 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7989 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7990 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
7991 persis_io.pr.pr_info.residx = residx;
7992 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7993 &persis_io, sizeof(persis_io), 0 )) >
7994 CTL_HA_STATUS_SUCCESS) {
7995 printf("CTL:Persis Out error returned from "
7996 "ctl_ha_msg_send %d\n", isc_retval);
7998 mtx_unlock(&softc->ctl_lock);
7999 } else /* sa_res_key != 0 */ {
8002 * If we aren't registered currently then increment
8003 * the key count and set the registered flag.
8005 if (!lun->per_res[residx].registered) {
8006 lun->pr_key_count++;
8007 lun->per_res[residx].registered = 1;
8010 memcpy(&lun->per_res[residx].res_key,
8011 param->serv_act_res_key,
8012 ctl_min(sizeof(param->serv_act_res_key),
8013 sizeof(lun->per_res[residx].res_key)));
8015 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8016 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8017 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8018 persis_io.pr.pr_info.residx = residx;
8019 memcpy(persis_io.pr.pr_info.sa_res_key,
8020 param->serv_act_res_key,
8021 sizeof(param->serv_act_res_key));
8022 mtx_unlock(&softc->ctl_lock);
8023 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8024 &persis_io, sizeof(persis_io), 0)) >
8025 CTL_HA_STATUS_SUCCESS) {
8026 printf("CTL:Persis Out error returned from "
8027 "ctl_ha_msg_send %d\n", isc_retval);
8030 lun->PRGeneration++;
8036 printf("Reserve executed type %d\n", type);
8038 mtx_lock(&softc->ctl_lock);
8039 if (lun->flags & CTL_LUN_PR_RESERVED) {
8041 * if this isn't the reservation holder and it's
8042 * not a "all registrants" type or if the type is
8043 * different then we have a conflict
8045 if ((lun->pr_res_idx != residx
8046 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8047 || lun->res_type != type) {
8048 mtx_unlock(&softc->ctl_lock);
8049 free(ctsio->kern_data_ptr, M_CTL);
8050 ctl_set_reservation_conflict(ctsio);
8051 ctl_done((union ctl_io *)ctsio);
8052 return (CTL_RETVAL_COMPLETE);
8054 } else /* create a reservation */ {
8056 * If it's not an "all registrants" type record
8057 * reservation holder
8059 if (type != SPR_TYPE_WR_EX_AR
8060 && type != SPR_TYPE_EX_AC_AR)
8061 lun->pr_res_idx = residx; /* Res holder */
8063 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8065 lun->flags |= CTL_LUN_PR_RESERVED;
8066 lun->res_type = type;
8068 mtx_unlock(&softc->ctl_lock);
8070 /* send msg to other side */
8071 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8072 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8073 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8074 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8075 persis_io.pr.pr_info.res_type = type;
8076 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8077 &persis_io, sizeof(persis_io), 0)) >
8078 CTL_HA_STATUS_SUCCESS) {
8079 printf("CTL:Persis Out error returned from "
8080 "ctl_ha_msg_send %d\n", isc_retval);
8086 mtx_lock(&softc->ctl_lock);
8087 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8088 /* No reservation exists return good status */
8089 mtx_unlock(&softc->ctl_lock);
8093 * Is this nexus a reservation holder?
8095 if (lun->pr_res_idx != residx
8096 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8098 * not a res holder return good status but
8101 mtx_unlock(&softc->ctl_lock);
8105 if (lun->res_type != type) {
8106 mtx_unlock(&softc->ctl_lock);
8107 free(ctsio->kern_data_ptr, M_CTL);
8108 ctl_set_illegal_pr_release(ctsio);
8109 ctl_done((union ctl_io *)ctsio);
8110 return (CTL_RETVAL_COMPLETE);
8113 /* okay to release */
8114 lun->flags &= ~CTL_LUN_PR_RESERVED;
8115 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8119 * if this isn't an exclusive access
8120 * res generate UA for all other
8123 if (type != SPR_TYPE_EX_AC
8124 && type != SPR_TYPE_WR_EX) {
8126 * temporarily unregister so we don't generate UA
8128 lun->per_res[residx].registered = 0;
8130 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8131 if (lun->per_res[i+persis_offset].registered
8134 lun->pending_sense[i].ua_pending |=
8138 lun->per_res[residx].registered = 1;
8140 mtx_unlock(&softc->ctl_lock);
8141 /* Send msg to other side */
8142 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8143 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8144 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8145 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8146 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8147 printf("CTL:Persis Out error returned from "
8148 "ctl_ha_msg_send %d\n", isc_retval);
8153 /* send msg to other side */
8155 mtx_lock(&softc->ctl_lock);
8156 lun->flags &= ~CTL_LUN_PR_RESERVED;
8158 lun->pr_key_count = 0;
8159 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8162 memset(&lun->per_res[residx].res_key,
8163 0, sizeof(lun->per_res[residx].res_key));
8164 lun->per_res[residx].registered = 0;
8166 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8167 if (lun->per_res[i].registered) {
8168 if (!persis_offset && i < CTL_MAX_INITIATORS)
8169 lun->pending_sense[i].ua_pending |=
8171 else if (persis_offset && i >= persis_offset)
8172 lun->pending_sense[i-persis_offset
8173 ].ua_pending |= CTL_UA_RES_PREEMPT;
8175 memset(&lun->per_res[i].res_key,
8176 0, sizeof(struct scsi_per_res_key));
8177 lun->per_res[i].registered = 0;
8179 lun->PRGeneration++;
8180 mtx_unlock(&softc->ctl_lock);
8181 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8182 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8183 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8184 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8185 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8186 printf("CTL:Persis Out error returned from "
8187 "ctl_ha_msg_send %d\n", isc_retval);
8191 case SPRO_PREEMPT: {
8194 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8195 residx, ctsio, cdb, param);
8197 return (CTL_RETVAL_COMPLETE);
8203 free(ctsio->kern_data_ptr, M_CTL);
8204 ctl_set_invalid_field(/*ctsio*/ ctsio,
8210 ctl_done((union ctl_io *)ctsio);
8211 return (CTL_RETVAL_COMPLETE);
8212 break; /* NOTREACHED */
8216 free(ctsio->kern_data_ptr, M_CTL);
8217 ctl_set_success(ctsio);
8218 ctl_done((union ctl_io *)ctsio);
8224 * This routine is for handling a message from the other SC pertaining to
8225 * persistent reserve out. All the error checking will have been done
8226 * so only perorming the action need be done here to keep the two
8230 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8232 struct ctl_lun *lun;
8233 struct ctl_softc *softc;
8236 softc = control_softc;
8238 mtx_lock(&softc->ctl_lock);
8240 lun = softc->ctl_luns[msg->hdr.nexus.targ_lun];
8241 switch(msg->pr.pr_info.action) {
8242 case CTL_PR_REG_KEY:
8243 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8244 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8245 lun->pr_key_count++;
8247 lun->PRGeneration++;
8248 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8249 msg->pr.pr_info.sa_res_key,
8250 sizeof(struct scsi_per_res_key));
8253 case CTL_PR_UNREG_KEY:
8254 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8255 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8256 0, sizeof(struct scsi_per_res_key));
8257 lun->pr_key_count--;
8259 /* XXX Need to see if the reservation has been released */
8260 /* if so do we need to generate UA? */
8261 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8262 lun->flags &= ~CTL_LUN_PR_RESERVED;
8263 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8265 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8266 || lun->res_type == SPR_TYPE_EX_AC_RO)
8267 && lun->pr_key_count) {
8269 * If the reservation is a registrants
8270 * only type we need to generate a UA
8271 * for other registered inits. The
8272 * sense code should be RESERVATIONS
8276 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8278 persis_offset].registered == 0)
8281 lun->pending_sense[i
8287 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8288 if (lun->pr_key_count==0) {
8289 lun->flags &= ~CTL_LUN_PR_RESERVED;
8291 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8294 lun->PRGeneration++;
8297 case CTL_PR_RESERVE:
8298 lun->flags |= CTL_LUN_PR_RESERVED;
8299 lun->res_type = msg->pr.pr_info.res_type;
8300 lun->pr_res_idx = msg->pr.pr_info.residx;
8304 case CTL_PR_RELEASE:
8306 * if this isn't an exclusive access res generate UA for all
8307 * other registrants.
8309 if (lun->res_type != SPR_TYPE_EX_AC
8310 && lun->res_type != SPR_TYPE_WR_EX) {
8311 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8312 if (lun->per_res[i+persis_offset].registered)
8313 lun->pending_sense[i].ua_pending |=
8317 lun->flags &= ~CTL_LUN_PR_RESERVED;
8318 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8322 case CTL_PR_PREEMPT:
8323 ctl_pro_preempt_other(lun, msg);
8326 lun->flags &= ~CTL_LUN_PR_RESERVED;
8328 lun->pr_key_count = 0;
8329 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8331 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8332 if (lun->per_res[i].registered == 0)
8335 && i < CTL_MAX_INITIATORS)
8336 lun->pending_sense[i].ua_pending |=
8338 else if (persis_offset
8339 && i >= persis_offset)
8340 lun->pending_sense[i-persis_offset].ua_pending|=
8342 memset(&lun->per_res[i].res_key, 0,
8343 sizeof(struct scsi_per_res_key));
8344 lun->per_res[i].registered = 0;
8346 lun->PRGeneration++;
8350 mtx_unlock(&softc->ctl_lock);
8354 ctl_read_write(struct ctl_scsiio *ctsio)
8356 struct ctl_lun *lun;
8357 struct ctl_lba_len lbalen;
8359 uint32_t num_blocks;
8360 int reladdr, fua, dpo, ebp;
8364 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8366 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8373 retval = CTL_RETVAL_COMPLETE;
8375 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8376 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8377 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8381 * XXX KDM need a lock here.
8383 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8384 if ((lun->res_type == SPR_TYPE_EX_AC
8385 && residx != lun->pr_res_idx)
8386 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8387 || lun->res_type == SPR_TYPE_EX_AC_AR)
8388 && !lun->per_res[residx].registered)) {
8389 ctl_set_reservation_conflict(ctsio);
8390 ctl_done((union ctl_io *)ctsio);
8391 return (CTL_RETVAL_COMPLETE);
8395 switch (ctsio->cdb[0]) {
8398 struct scsi_rw_6 *cdb;
8400 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8402 lba = scsi_3btoul(cdb->addr);
8403 /* only 5 bits are valid in the most significant address byte */
8405 num_blocks = cdb->length;
8407 * This is correct according to SBC-2.
8409 if (num_blocks == 0)
8415 struct scsi_rw_10 *cdb;
8417 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8419 if (cdb->byte2 & SRW10_RELADDR)
8421 if (cdb->byte2 & SRW10_FUA)
8423 if (cdb->byte2 & SRW10_DPO)
8426 if ((cdb->opcode == WRITE_10)
8427 && (cdb->byte2 & SRW10_EBP))
8430 lba = scsi_4btoul(cdb->addr);
8431 num_blocks = scsi_2btoul(cdb->length);
8434 case WRITE_VERIFY_10: {
8435 struct scsi_write_verify_10 *cdb;
8437 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8440 * XXX KDM we should do actual write verify support at some
8441 * point. This is obviously fake, we're just translating
8442 * things to a write. So we don't even bother checking the
8443 * BYTCHK field, since we don't do any verification. If
8444 * the user asks for it, we'll just pretend we did it.
8446 if (cdb->byte2 & SWV_DPO)
8449 lba = scsi_4btoul(cdb->addr);
8450 num_blocks = scsi_2btoul(cdb->length);
8455 struct scsi_rw_12 *cdb;
8457 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8459 if (cdb->byte2 & SRW12_RELADDR)
8461 if (cdb->byte2 & SRW12_FUA)
8463 if (cdb->byte2 & SRW12_DPO)
8465 lba = scsi_4btoul(cdb->addr);
8466 num_blocks = scsi_4btoul(cdb->length);
8469 case WRITE_VERIFY_12: {
8470 struct scsi_write_verify_12 *cdb;
8472 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8474 if (cdb->byte2 & SWV_DPO)
8477 lba = scsi_4btoul(cdb->addr);
8478 num_blocks = scsi_4btoul(cdb->length);
8484 struct scsi_rw_16 *cdb;
8486 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8488 if (cdb->byte2 & SRW12_RELADDR)
8490 if (cdb->byte2 & SRW12_FUA)
8492 if (cdb->byte2 & SRW12_DPO)
8495 lba = scsi_8btou64(cdb->addr);
8496 num_blocks = scsi_4btoul(cdb->length);
8499 case WRITE_VERIFY_16: {
8500 struct scsi_write_verify_16 *cdb;
8502 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8504 if (cdb->byte2 & SWV_DPO)
8507 lba = scsi_8btou64(cdb->addr);
8508 num_blocks = scsi_4btoul(cdb->length);
8513 * We got a command we don't support. This shouldn't
8514 * happen, commands should be filtered out above us.
8516 ctl_set_invalid_opcode(ctsio);
8517 ctl_done((union ctl_io *)ctsio);
8519 return (CTL_RETVAL_COMPLETE);
8520 break; /* NOTREACHED */
8524 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8525 * interesting for us, but if RAIDCore is in write-back mode,
8526 * getting it to do write-through for a particular transaction may
8530 * We don't support relative addressing. That also requires
8531 * supporting linked commands, which we don't do.
8534 ctl_set_invalid_field(ctsio,
8540 ctl_done((union ctl_io *)ctsio);
8541 return (CTL_RETVAL_COMPLETE);
8545 * The first check is to make sure we're in bounds, the second
8546 * check is to catch wrap-around problems. If the lba + num blocks
8547 * is less than the lba, then we've wrapped around and the block
8548 * range is invalid anyway.
8550 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8551 || ((lba + num_blocks) < lba)) {
8552 ctl_set_lba_out_of_range(ctsio);
8553 ctl_done((union ctl_io *)ctsio);
8554 return (CTL_RETVAL_COMPLETE);
8558 * According to SBC-3, a transfer length of 0 is not an error.
8559 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8560 * translates to 256 blocks for those commands.
8562 if (num_blocks == 0) {
8563 ctl_set_success(ctsio);
8564 ctl_done((union ctl_io *)ctsio);
8565 return (CTL_RETVAL_COMPLETE);
8569 lbalen.len = num_blocks;
8570 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8573 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8575 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8581 ctl_report_luns(struct ctl_scsiio *ctsio)
8583 struct scsi_report_luns *cdb;
8584 struct scsi_report_luns_data *lun_data;
8585 struct ctl_lun *lun, *request_lun;
8586 int num_luns, retval;
8587 uint32_t alloc_len, lun_datalen;
8588 int num_filled, well_known;
8591 retval = CTL_RETVAL_COMPLETE;
8594 cdb = (struct scsi_report_luns *)ctsio->cdb;
8596 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8598 mtx_lock(&control_softc->ctl_lock);
8599 num_luns = control_softc->num_luns;
8600 mtx_unlock(&control_softc->ctl_lock);
8602 switch (cdb->select_report) {
8603 case RPL_REPORT_DEFAULT:
8604 case RPL_REPORT_ALL:
8606 case RPL_REPORT_WELLKNOWN:
8611 ctl_set_invalid_field(ctsio,
8617 ctl_done((union ctl_io *)ctsio);
8619 break; /* NOTREACHED */
8622 alloc_len = scsi_4btoul(cdb->length);
8624 * The initiator has to allocate at least 16 bytes for this request,
8625 * so he can at least get the header and the first LUN. Otherwise
8626 * we reject the request (per SPC-3 rev 14, section 6.21).
8628 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8629 sizeof(struct scsi_report_luns_lundata))) {
8630 ctl_set_invalid_field(ctsio,
8636 ctl_done((union ctl_io *)ctsio);
8640 request_lun = (struct ctl_lun *)
8641 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8643 lun_datalen = sizeof(*lun_data) +
8644 (num_luns * sizeof(struct scsi_report_luns_lundata));
8646 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8647 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8648 ctsio->kern_sg_entries = 0;
8650 if (lun_datalen < alloc_len) {
8651 ctsio->residual = alloc_len - lun_datalen;
8652 ctsio->kern_data_len = lun_datalen;
8653 ctsio->kern_total_len = lun_datalen;
8655 ctsio->residual = 0;
8656 ctsio->kern_data_len = alloc_len;
8657 ctsio->kern_total_len = alloc_len;
8659 ctsio->kern_data_resid = 0;
8660 ctsio->kern_rel_offset = 0;
8661 ctsio->kern_sg_entries = 0;
8663 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8666 * We set this to the actual data length, regardless of how much
8667 * space we actually have to return results. If the user looks at
8668 * this value, he'll know whether or not he allocated enough space
8669 * and reissue the command if necessary. We don't support well
8670 * known logical units, so if the user asks for that, return none.
8672 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8674 mtx_lock(&control_softc->ctl_lock);
8675 for (num_filled = 0, lun = STAILQ_FIRST(&control_softc->lun_list);
8676 (lun != NULL) && (num_filled < num_luns);
8677 lun = STAILQ_NEXT(lun, links)) {
8679 if (lun->lun <= 0xff) {
8681 * Peripheral addressing method, bus number 0.
8683 lun_data->luns[num_filled].lundata[0] =
8684 RPL_LUNDATA_ATYP_PERIPH;
8685 lun_data->luns[num_filled].lundata[1] = lun->lun;
8687 } else if (lun->lun <= 0x3fff) {
8689 * Flat addressing method.
8691 lun_data->luns[num_filled].lundata[0] =
8692 RPL_LUNDATA_ATYP_FLAT |
8693 (lun->lun & RPL_LUNDATA_FLAT_LUN_MASK);
8694 #ifdef OLDCTLHEADERS
8695 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8696 (lun->lun & SRLD_BUS_LUN_MASK);
8698 lun_data->luns[num_filled].lundata[1] =
8699 #ifdef OLDCTLHEADERS
8700 lun->lun >> SRLD_BUS_LUN_BITS;
8702 lun->lun >> RPL_LUNDATA_FLAT_LUN_BITS;
8705 printf("ctl_report_luns: bogus LUN number %jd, "
8706 "skipping\n", (intmax_t)lun->lun);
8709 * According to SPC-3, rev 14 section 6.21:
8711 * "The execution of a REPORT LUNS command to any valid and
8712 * installed logical unit shall clear the REPORTED LUNS DATA
8713 * HAS CHANGED unit attention condition for all logical
8714 * units of that target with respect to the requesting
8715 * initiator. A valid and installed logical unit is one
8716 * having a PERIPHERAL QUALIFIER of 000b in the standard
8717 * INQUIRY data (see 6.4.2)."
8719 * If request_lun is NULL, the LUN this report luns command
8720 * was issued to is either disabled or doesn't exist. In that
8721 * case, we shouldn't clear any pending lun change unit
8724 if (request_lun != NULL)
8725 lun->pending_sense[initidx].ua_pending &=
8728 mtx_unlock(&control_softc->ctl_lock);
8731 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8734 ctsio->scsi_status = SCSI_STATUS_OK;
8736 ctsio->be_move_done = ctl_config_move_done;
8737 ctl_datamove((union ctl_io *)ctsio);
8743 ctl_request_sense(struct ctl_scsiio *ctsio)
8745 struct scsi_request_sense *cdb;
8746 struct scsi_sense_data *sense_ptr;
8747 struct ctl_lun *lun;
8750 scsi_sense_data_type sense_format;
8752 cdb = (struct scsi_request_sense *)ctsio->cdb;
8754 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8756 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
8759 * Determine which sense format the user wants.
8761 if (cdb->byte2 & SRS_DESC)
8762 sense_format = SSD_TYPE_DESC;
8764 sense_format = SSD_TYPE_FIXED;
8766 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
8767 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
8768 ctsio->kern_sg_entries = 0;
8771 * struct scsi_sense_data, which is currently set to 256 bytes, is
8772 * larger than the largest allowed value for the length field in the
8773 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
8775 ctsio->residual = 0;
8776 ctsio->kern_data_len = cdb->length;
8777 ctsio->kern_total_len = cdb->length;
8779 ctsio->kern_data_resid = 0;
8780 ctsio->kern_rel_offset = 0;
8781 ctsio->kern_sg_entries = 0;
8784 * If we don't have a LUN, we don't have any pending sense.
8790 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8792 * Check for pending sense, and then for pending unit attentions.
8793 * Pending sense gets returned first, then pending unit attentions.
8795 mtx_lock(&lun->ctl_softc->ctl_lock);
8796 if (ctl_is_set(lun->have_ca, initidx)) {
8797 scsi_sense_data_type stored_format;
8800 * Check to see which sense format was used for the stored
8803 stored_format = scsi_sense_type(
8804 &lun->pending_sense[initidx].sense);
8807 * If the user requested a different sense format than the
8808 * one we stored, then we need to convert it to the other
8809 * format. If we're going from descriptor to fixed format
8810 * sense data, we may lose things in translation, depending
8811 * on what options were used.
8813 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
8814 * for some reason we'll just copy it out as-is.
8816 if ((stored_format == SSD_TYPE_FIXED)
8817 && (sense_format == SSD_TYPE_DESC))
8818 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
8819 &lun->pending_sense[initidx].sense,
8820 (struct scsi_sense_data_desc *)sense_ptr);
8821 else if ((stored_format == SSD_TYPE_DESC)
8822 && (sense_format == SSD_TYPE_FIXED))
8823 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
8824 &lun->pending_sense[initidx].sense,
8825 (struct scsi_sense_data_fixed *)sense_ptr);
8827 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
8828 ctl_min(sizeof(*sense_ptr),
8829 sizeof(lun->pending_sense[initidx].sense)));
8831 ctl_clear_mask(lun->have_ca, initidx);
8833 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
8834 ctl_ua_type ua_type;
8836 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
8837 sense_ptr, sense_format);
8838 if (ua_type != CTL_UA_NONE) {
8840 /* We're reporting this UA, so clear it */
8841 lun->pending_sense[initidx].ua_pending &= ~ua_type;
8844 mtx_unlock(&lun->ctl_softc->ctl_lock);
8847 * We already have a pending error, return it.
8849 if (have_error != 0) {
8851 * We report the SCSI status as OK, since the status of the
8852 * request sense command itself is OK.
8854 ctsio->scsi_status = SCSI_STATUS_OK;
8857 * We report 0 for the sense length, because we aren't doing
8858 * autosense in this case. We're reporting sense as
8861 ctsio->sense_len = 0;
8863 ctsio->be_move_done = ctl_config_move_done;
8864 ctl_datamove((union ctl_io *)ctsio);
8866 return (CTL_RETVAL_COMPLETE);
8872 * No sense information to report, so we report that everything is
8875 ctl_set_sense_data(sense_ptr,
8878 /*current_error*/ 1,
8879 /*sense_key*/ SSD_KEY_NO_SENSE,
8884 ctsio->scsi_status = SCSI_STATUS_OK;
8887 * We report 0 for the sense length, because we aren't doing
8888 * autosense in this case. We're reporting sense as parameter data.
8890 ctsio->sense_len = 0;
8891 ctsio->be_move_done = ctl_config_move_done;
8892 ctl_datamove((union ctl_io *)ctsio);
8894 return (CTL_RETVAL_COMPLETE);
8898 ctl_tur(struct ctl_scsiio *ctsio)
8900 struct ctl_lun *lun;
8902 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8904 CTL_DEBUG_PRINT(("ctl_tur\n"));
8909 ctsio->scsi_status = SCSI_STATUS_OK;
8910 ctsio->io_hdr.status = CTL_SUCCESS;
8912 ctl_done((union ctl_io *)ctsio);
8914 return (CTL_RETVAL_COMPLETE);
8919 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
8926 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
8928 struct scsi_vpd_supported_pages *pages;
8930 struct ctl_lun *lun;
8932 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8934 sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
8935 SCSI_EVPD_NUM_SUPPORTED_PAGES;
8937 * XXX KDM GFP_??? We probably don't want to wait here,
8938 * unless we end up having a process/thread context.
8940 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
8941 if (ctsio->kern_data_ptr == NULL) {
8942 ctsio->io_hdr.status = CTL_SCSI_ERROR;
8943 ctsio->scsi_status = SCSI_STATUS_BUSY;
8944 ctl_done((union ctl_io *)ctsio);
8945 return (CTL_RETVAL_COMPLETE);
8947 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
8948 ctsio->kern_sg_entries = 0;
8950 if (sup_page_size < alloc_len) {
8951 ctsio->residual = alloc_len - sup_page_size;
8952 ctsio->kern_data_len = sup_page_size;
8953 ctsio->kern_total_len = sup_page_size;
8955 ctsio->residual = 0;
8956 ctsio->kern_data_len = alloc_len;
8957 ctsio->kern_total_len = alloc_len;
8959 ctsio->kern_data_resid = 0;
8960 ctsio->kern_rel_offset = 0;
8961 ctsio->kern_sg_entries = 0;
8964 * The control device is always connected. The disk device, on the
8965 * other hand, may not be online all the time. Need to change this
8966 * to figure out whether the disk device is actually online or not.
8969 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
8970 lun->be_lun->lun_type;
8972 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
8974 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
8975 /* Supported VPD pages */
8976 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
8978 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
8979 /* Device Identification */
8980 pages->page_list[2] = SVPD_DEVICE_ID;
8982 ctsio->scsi_status = SCSI_STATUS_OK;
8984 ctsio->be_move_done = ctl_config_move_done;
8985 ctl_datamove((union ctl_io *)ctsio);
8987 return (CTL_RETVAL_COMPLETE);
8991 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
8993 struct scsi_vpd_unit_serial_number *sn_ptr;
8994 struct ctl_lun *lun;
8995 #ifndef CTL_USE_BACKEND_SN
8999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9001 /* XXX KDM which malloc flags here?? */
9002 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9003 if (ctsio->kern_data_ptr == NULL) {
9004 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9005 ctsio->scsi_status = SCSI_STATUS_BUSY;
9006 ctl_done((union ctl_io *)ctsio);
9007 return (CTL_RETVAL_COMPLETE);
9009 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9010 ctsio->kern_sg_entries = 0;
9012 if (sizeof(*sn_ptr) < alloc_len) {
9013 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9014 ctsio->kern_data_len = sizeof(*sn_ptr);
9015 ctsio->kern_total_len = sizeof(*sn_ptr);
9017 ctsio->residual = 0;
9018 ctsio->kern_data_len = alloc_len;
9019 ctsio->kern_total_len = alloc_len;
9021 ctsio->kern_data_resid = 0;
9022 ctsio->kern_rel_offset = 0;
9023 ctsio->kern_sg_entries = 0;
9026 * The control device is always connected. The disk device, on the
9027 * other hand, may not be online all the time. Need to change this
9028 * to figure out whether the disk device is actually online or not.
9031 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9032 lun->be_lun->lun_type;
9034 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9036 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9037 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9038 #ifdef CTL_USE_BACKEND_SN
9040 * If we don't have a LUN, we just leave the serial number as
9043 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9045 strncpy((char *)sn_ptr->serial_num,
9046 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9050 * Note that we're using a non-unique serial number here,
9052 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9053 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9054 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9055 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9057 ctsio->scsi_status = SCSI_STATUS_OK;
9059 ctsio->be_move_done = ctl_config_move_done;
9060 ctl_datamove((union ctl_io *)ctsio);
9062 return (CTL_RETVAL_COMPLETE);
9067 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9069 struct scsi_vpd_device_id *devid_ptr;
9070 struct scsi_vpd_id_descriptor *desc, *desc1;
9071 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9072 struct scsi_vpd_id_t10 *t10id;
9073 struct ctl_softc *ctl_softc;
9074 struct ctl_lun *lun;
9075 struct ctl_frontend *fe;
9076 #ifndef CTL_USE_BACKEND_SN
9078 #endif /* CTL_USE_BACKEND_SN */
9081 ctl_softc = control_softc;
9082 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9084 devid_len = sizeof(struct scsi_vpd_device_id) +
9085 sizeof(struct scsi_vpd_id_descriptor) +
9086 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9087 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9088 sizeof(struct scsi_vpd_id_descriptor) +
9089 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9090 sizeof(struct scsi_vpd_id_descriptor) +
9091 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9093 /* XXX KDM which malloc flags here ?? */
9094 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9095 if (ctsio->kern_data_ptr == NULL) {
9096 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9097 ctsio->scsi_status = SCSI_STATUS_BUSY;
9098 ctl_done((union ctl_io *)ctsio);
9099 return (CTL_RETVAL_COMPLETE);
9101 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9102 ctsio->kern_sg_entries = 0;
9104 if (devid_len < alloc_len) {
9105 ctsio->residual = alloc_len - devid_len;
9106 ctsio->kern_data_len = devid_len;
9107 ctsio->kern_total_len = devid_len;
9109 ctsio->residual = 0;
9110 ctsio->kern_data_len = alloc_len;
9111 ctsio->kern_total_len = alloc_len;
9113 ctsio->kern_data_resid = 0;
9114 ctsio->kern_rel_offset = 0;
9115 ctsio->kern_sg_entries = 0;
9117 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9118 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9119 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9120 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9121 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9123 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9124 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9127 * The control device is always connected. The disk device, on the
9128 * other hand, may not be online all the time.
9131 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9132 lun->be_lun->lun_type;
9134 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9136 devid_ptr->page_code = SVPD_DEVICE_ID;
9138 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9140 mtx_lock(&ctl_softc->ctl_lock);
9142 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9145 * For Fibre channel,
9147 if (fe->port_type == CTL_PORT_FC)
9149 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9150 SVPD_ID_CODESET_ASCII;
9151 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9152 SVPD_ID_CODESET_BINARY;
9156 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9157 SVPD_ID_CODESET_ASCII;
9158 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9159 SVPD_ID_CODESET_BINARY;
9161 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9162 mtx_unlock(&ctl_softc->ctl_lock);
9165 * We're using a LUN association here. i.e., this device ID is a
9166 * per-LUN identifier.
9168 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9169 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9170 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9173 * desc1 is for the WWPN which is a port asscociation.
9175 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9176 desc1->length = CTL_WWPN_LEN;
9177 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9178 /* For testing just create the WWPN */
9180 ddb_GetWWNN((char *)desc1->identifier);
9182 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9183 /* This is so Copancontrol will return something sane */
9184 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9185 ctsio->io_hdr.nexus.targ_port!=8)
9186 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9188 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9191 be64enc(desc1->identifier, fe->wwpn);
9194 * desc2 is for the Relative Target Port(type 4h) identifier
9196 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9197 | SVPD_ID_TYPE_RELTARG;
9200 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9201 /* This is so Copancontrol will return something sane */
9202 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9203 ctsio->io_hdr.nexus.targ_port!=8)
9204 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9206 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9210 * desc3 is for the Target Port Group(type 5h) identifier
9212 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9213 | SVPD_ID_TYPE_TPORTGRP;
9215 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9216 desc3->identifier[3] = 1;
9218 desc3->identifier[3] = 2;
9220 #ifdef CTL_USE_BACKEND_SN
9222 * If we've actually got a backend, copy the device id from the
9223 * per-LUN data. Otherwise, set it to all spaces.
9227 * Copy the backend's LUN ID.
9229 strncpy((char *)t10id->vendor_spec_id,
9230 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9233 * No backend, set this to spaces.
9235 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9238 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9239 (lun != NULL) ? (int)lun->lun : 0);
9240 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9244 ctsio->scsi_status = SCSI_STATUS_OK;
9246 ctsio->be_move_done = ctl_config_move_done;
9247 ctl_datamove((union ctl_io *)ctsio);
9249 return (CTL_RETVAL_COMPLETE);
9253 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9255 struct scsi_inquiry *cdb;
9256 int alloc_len, retval;
9258 cdb = (struct scsi_inquiry *)ctsio->cdb;
9260 retval = CTL_RETVAL_COMPLETE;
9262 alloc_len = scsi_2btoul(cdb->length);
9264 switch (cdb->page_code) {
9265 case SVPD_SUPPORTED_PAGES:
9266 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9268 case SVPD_UNIT_SERIAL_NUMBER:
9269 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9271 case SVPD_DEVICE_ID:
9272 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9275 ctl_set_invalid_field(ctsio,
9281 ctl_done((union ctl_io *)ctsio);
9282 retval = CTL_RETVAL_COMPLETE;
9290 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9292 struct scsi_inquiry_data *inq_ptr;
9293 struct scsi_inquiry *cdb;
9294 struct ctl_softc *ctl_softc;
9295 struct ctl_lun *lun;
9299 ctl_softc = control_softc;
9302 * Figure out whether we're talking to a Fibre Channel port or not.
9303 * We treat the ioctl front end, and any SCSI adapters, as packetized
9306 mtx_lock(&ctl_softc->ctl_lock);
9307 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9312 mtx_unlock(&ctl_softc->ctl_lock);
9314 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9315 cdb = (struct scsi_inquiry *)ctsio->cdb;
9316 alloc_len = scsi_2btoul(cdb->length);
9319 * We malloc the full inquiry data size here and fill it
9320 * in. If the user only asks for less, we'll give him
9323 /* XXX KDM what malloc flags should we use here?? */
9324 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9325 if (ctsio->kern_data_ptr == NULL) {
9326 ctsio->io_hdr.status = CTL_SCSI_ERROR;
9327 ctsio->scsi_status = SCSI_STATUS_BUSY;
9328 ctl_done((union ctl_io *)ctsio);
9329 return (CTL_RETVAL_COMPLETE);
9331 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9332 ctsio->kern_sg_entries = 0;
9333 ctsio->kern_data_resid = 0;
9334 ctsio->kern_rel_offset = 0;
9336 if (sizeof(*inq_ptr) < alloc_len) {
9337 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9338 ctsio->kern_data_len = sizeof(*inq_ptr);
9339 ctsio->kern_total_len = sizeof(*inq_ptr);
9341 ctsio->residual = 0;
9342 ctsio->kern_data_len = alloc_len;
9343 ctsio->kern_total_len = alloc_len;
9347 * If we have a LUN configured, report it as connected. Otherwise,
9348 * report that it is offline or no device is supported, depending
9349 * on the value of inquiry_pq_no_lun.
9351 * According to the spec (SPC-4 r34), the peripheral qualifier
9352 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9354 * "A peripheral device having the specified peripheral device type
9355 * is not connected to this logical unit. However, the device
9356 * server is capable of supporting the specified peripheral device
9357 * type on this logical unit."
9359 * According to the same spec, the peripheral qualifier
9360 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9362 * "The device server is not capable of supporting a peripheral
9363 * device on this logical unit. For this peripheral qualifier the
9364 * peripheral device type shall be set to 1Fh. All other peripheral
9365 * device type values are reserved for this peripheral qualifier."
9367 * Given the text, it would seem that we probably want to report that
9368 * the LUN is offline here. There is no LUN connected, but we can
9369 * support a LUN at the given LUN number.
9371 * In the real world, though, it sounds like things are a little
9374 * - Linux, when presented with a LUN with the offline peripheral
9375 * qualifier, will create an sg driver instance for it. So when
9376 * you attach it to CTL, you wind up with a ton of sg driver
9377 * instances. (One for every LUN that Linux bothered to probe.)
9378 * Linux does this despite the fact that it issues a REPORT LUNs
9379 * to LUN 0 to get the inventory of supported LUNs.
9381 * - There is other anecdotal evidence (from Emulex folks) about
9382 * arrays that use the offline peripheral qualifier for LUNs that
9383 * are on the "passive" path in an active/passive array.
9385 * So the solution is provide a hopefully reasonable default
9386 * (return bad/no LUN) and allow the user to change the behavior
9387 * with a tunable/sysctl variable.
9390 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9391 lun->be_lun->lun_type;
9392 else if (ctl_softc->inquiry_pq_no_lun == 0)
9393 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9395 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9397 /* RMB in byte 2 is 0 */
9398 inq_ptr->version = SCSI_REV_SPC3;
9401 * According to SAM-3, even if a device only supports a single
9402 * level of LUN addressing, it should still set the HISUP bit:
9404 * 4.9.1 Logical unit numbers overview
9406 * All logical unit number formats described in this standard are
9407 * hierarchical in structure even when only a single level in that
9408 * hierarchy is used. The HISUP bit shall be set to one in the
9409 * standard INQUIRY data (see SPC-2) when any logical unit number
9410 * format described in this standard is used. Non-hierarchical
9411 * formats are outside the scope of this standard.
9413 * Therefore we set the HiSup bit here.
9415 * The reponse format is 2, per SPC-3.
9417 inq_ptr->response_format = SID_HiSup | 2;
9419 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9420 CTL_DEBUG_PRINT(("additional_length = %d\n",
9421 inq_ptr->additional_length));
9423 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9424 /* 16 bit addressing */
9426 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9427 /* XXX set the SID_MultiP bit here if we're actually going to
9428 respond on multiple ports */
9429 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9431 /* 16 bit data bus, synchronous transfers */
9432 /* XXX these flags don't apply for FC */
9434 inq_ptr->flags = SID_WBus16 | SID_Sync;
9436 * XXX KDM do we want to support tagged queueing on the control
9440 || (lun->be_lun->lun_type != T_PROCESSOR))
9441 inq_ptr->flags |= SID_CmdQue;
9443 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9444 * We have 8 bytes for the vendor name, and 16 bytes for the device
9445 * name and 4 bytes for the revision.
9447 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9449 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9451 switch (lun->be_lun->lun_type) {
9453 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9456 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9459 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9465 * XXX make this a macro somewhere so it automatically gets
9466 * incremented when we make changes.
9468 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9471 * For parallel SCSI, we support double transition and single
9472 * transition clocking. We also support QAS (Quick Arbitration
9473 * and Selection) and Information Unit transfers on both the
9474 * control and array devices.
9477 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9481 scsi_ulto2b(0x0060, inq_ptr->version1);
9482 /* SPC-3 (no version claimed) XXX should we claim a version? */
9483 scsi_ulto2b(0x0300, inq_ptr->version2);
9485 /* FCP-2 ANSI INCITS.350:2003 */
9486 scsi_ulto2b(0x0917, inq_ptr->version3);
9488 /* SPI-4 ANSI INCITS.362:200x */
9489 scsi_ulto2b(0x0B56, inq_ptr->version3);
9493 /* SBC-2 (no version claimed) XXX should we claim a version? */
9494 scsi_ulto2b(0x0320, inq_ptr->version4);
9496 switch (lun->be_lun->lun_type) {
9499 * SBC-2 (no version claimed) XXX should we claim a
9502 scsi_ulto2b(0x0320, inq_ptr->version4);
9510 ctsio->scsi_status = SCSI_STATUS_OK;
9511 if (ctsio->kern_data_len > 0) {
9512 ctsio->be_move_done = ctl_config_move_done;
9513 ctl_datamove((union ctl_io *)ctsio);
9515 ctsio->io_hdr.status = CTL_SUCCESS;
9516 ctl_done((union ctl_io *)ctsio);
9519 return (CTL_RETVAL_COMPLETE);
9523 ctl_inquiry(struct ctl_scsiio *ctsio)
9525 struct scsi_inquiry *cdb;
9528 cdb = (struct scsi_inquiry *)ctsio->cdb;
9532 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9535 * Right now, we don't support the CmdDt inquiry information.
9536 * This would be nice to support in the future. When we do
9537 * support it, we should change this test so that it checks to make
9538 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9541 if (((cdb->byte2 & SI_EVPD)
9542 && (cdb->byte2 & SI_CMDDT)))
9544 if (cdb->byte2 & SI_CMDDT) {
9546 * Point to the SI_CMDDT bit. We might change this
9547 * when we support SI_CMDDT, but since both bits would be
9548 * "wrong", this should probably just stay as-is then.
9550 ctl_set_invalid_field(ctsio,
9556 ctl_done((union ctl_io *)ctsio);
9557 return (CTL_RETVAL_COMPLETE);
9559 if (cdb->byte2 & SI_EVPD)
9560 retval = ctl_inquiry_evpd(ctsio);
9562 else if (cdb->byte2 & SI_CMDDT)
9563 retval = ctl_inquiry_cmddt(ctsio);
9566 retval = ctl_inquiry_std(ctsio);
9572 * For known CDB types, parse the LBA and length.
9575 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9577 if (io->io_hdr.io_type != CTL_IO_SCSI)
9580 switch (io->scsiio.cdb[0]) {
9583 struct scsi_rw_6 *cdb;
9585 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9587 *lba = scsi_3btoul(cdb->addr);
9588 /* only 5 bits are valid in the most significant address byte */
9595 struct scsi_rw_10 *cdb;
9597 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9599 *lba = scsi_4btoul(cdb->addr);
9600 *len = scsi_2btoul(cdb->length);
9603 case WRITE_VERIFY_10: {
9604 struct scsi_write_verify_10 *cdb;
9606 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9608 *lba = scsi_4btoul(cdb->addr);
9609 *len = scsi_2btoul(cdb->length);
9614 struct scsi_rw_12 *cdb;
9616 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9618 *lba = scsi_4btoul(cdb->addr);
9619 *len = scsi_4btoul(cdb->length);
9622 case WRITE_VERIFY_12: {
9623 struct scsi_write_verify_12 *cdb;
9625 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9627 *lba = scsi_4btoul(cdb->addr);
9628 *len = scsi_4btoul(cdb->length);
9633 struct scsi_rw_16 *cdb;
9635 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9637 *lba = scsi_8btou64(cdb->addr);
9638 *len = scsi_4btoul(cdb->length);
9641 case WRITE_VERIFY_16: {
9642 struct scsi_write_verify_16 *cdb;
9644 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9647 *lba = scsi_8btou64(cdb->addr);
9648 *len = scsi_4btoul(cdb->length);
9653 break; /* NOTREACHED */
9660 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
9662 uint64_t endlba1, endlba2;
9664 endlba1 = lba1 + len1 - 1;
9665 endlba2 = lba2 + len2 - 1;
9667 if ((endlba1 < lba2)
9668 || (endlba2 < lba1))
9669 return (CTL_ACTION_PASS);
9671 return (CTL_ACTION_BLOCK);
9675 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
9677 uint64_t lba1, lba2;
9678 uint32_t len1, len2;
9681 retval = ctl_get_lba_len(io1, &lba1, &len1);
9683 return (CTL_ACTION_ERROR);
9685 retval = ctl_get_lba_len(io2, &lba2, &len2);
9687 return (CTL_ACTION_ERROR);
9689 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
9693 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
9695 struct ctl_cmd_entry *pending_entry, *ooa_entry;
9696 ctl_serialize_action *serialize_row;
9699 * The initiator attempted multiple untagged commands at the same
9700 * time. Can't do that.
9702 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9703 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9704 && ((pending_io->io_hdr.nexus.targ_port ==
9705 ooa_io->io_hdr.nexus.targ_port)
9706 && (pending_io->io_hdr.nexus.initid.id ==
9707 ooa_io->io_hdr.nexus.initid.id))
9708 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9709 return (CTL_ACTION_OVERLAP);
9712 * The initiator attempted to send multiple tagged commands with
9713 * the same ID. (It's fine if different initiators have the same
9716 * Even if all of those conditions are true, we don't kill the I/O
9717 * if the command ahead of us has been aborted. We won't end up
9718 * sending it to the FETD, and it's perfectly legal to resend a
9719 * command with the same tag number as long as the previous
9720 * instance of this tag number has been aborted somehow.
9722 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9723 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9724 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
9725 && ((pending_io->io_hdr.nexus.targ_port ==
9726 ooa_io->io_hdr.nexus.targ_port)
9727 && (pending_io->io_hdr.nexus.initid.id ==
9728 ooa_io->io_hdr.nexus.initid.id))
9729 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9730 return (CTL_ACTION_OVERLAP_TAG);
9733 * If we get a head of queue tag, SAM-3 says that we should
9734 * immediately execute it.
9736 * What happens if this command would normally block for some other
9737 * reason? e.g. a request sense with a head of queue tag
9738 * immediately after a write. Normally that would block, but this
9739 * will result in its getting executed immediately...
9741 * We currently return "pass" instead of "skip", so we'll end up
9742 * going through the rest of the queue to check for overlapped tags.
9744 * XXX KDM check for other types of blockage first??
9746 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9747 return (CTL_ACTION_PASS);
9750 * Ordered tags have to block until all items ahead of them
9751 * have completed. If we get called with an ordered tag, we always
9752 * block, if something else is ahead of us in the queue.
9754 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
9755 return (CTL_ACTION_BLOCK);
9758 * Simple tags get blocked until all head of queue and ordered tags
9759 * ahead of them have completed. I'm lumping untagged commands in
9760 * with simple tags here. XXX KDM is that the right thing to do?
9762 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9763 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
9764 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9765 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
9766 return (CTL_ACTION_BLOCK);
9768 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
9769 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
9771 serialize_row = ctl_serialize_table[ooa_entry->seridx];
9773 switch (serialize_row[pending_entry->seridx]) {
9775 return (CTL_ACTION_BLOCK);
9776 break; /* NOTREACHED */
9777 case CTL_SER_EXTENT:
9778 return (ctl_extent_check(pending_io, ooa_io));
9779 break; /* NOTREACHED */
9781 return (CTL_ACTION_PASS);
9782 break; /* NOTREACHED */
9784 return (CTL_ACTION_SKIP);
9787 panic("invalid serialization value %d",
9788 serialize_row[pending_entry->seridx]);
9789 break; /* NOTREACHED */
9792 return (CTL_ACTION_ERROR);
9796 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
9798 * - caller holds ctl_lock
9799 * - pending_io is generally either incoming, or on the blocked queue
9800 * - starting I/O is the I/O we want to start the check with.
9803 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
9804 union ctl_io *starting_io)
9806 union ctl_io *ooa_io;
9810 * Run back along the OOA queue, starting with the current
9811 * blocked I/O and going through every I/O before it on the
9812 * queue. If starting_io is NULL, we'll just end up returning
9815 for (ooa_io = starting_io; ooa_io != NULL;
9816 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
9820 * This routine just checks to see whether
9821 * cur_blocked is blocked by ooa_io, which is ahead
9822 * of it in the queue. It doesn't queue/dequeue
9825 action = ctl_check_for_blockage(pending_io, ooa_io);
9827 case CTL_ACTION_BLOCK:
9828 case CTL_ACTION_OVERLAP:
9829 case CTL_ACTION_OVERLAP_TAG:
9830 case CTL_ACTION_SKIP:
9831 case CTL_ACTION_ERROR:
9833 break; /* NOTREACHED */
9834 case CTL_ACTION_PASS:
9837 panic("invalid action %d", action);
9838 break; /* NOTREACHED */
9842 return (CTL_ACTION_PASS);
9847 * - An I/O has just completed, and has been removed from the per-LUN OOA
9848 * queue, so some items on the blocked queue may now be unblocked.
9849 * - The caller holds ctl_softc->ctl_lock
9852 ctl_check_blocked(struct ctl_lun *lun)
9854 union ctl_io *cur_blocked, *next_blocked;
9857 * Run forward from the head of the blocked queue, checking each
9858 * entry against the I/Os prior to it on the OOA queue to see if
9859 * there is still any blockage.
9861 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
9862 * with our removing a variable on it while it is traversing the
9865 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
9866 cur_blocked != NULL; cur_blocked = next_blocked) {
9867 union ctl_io *prev_ooa;
9870 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
9873 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
9874 ctl_ooaq, ooa_links);
9877 * If cur_blocked happens to be the first item in the OOA
9878 * queue now, prev_ooa will be NULL, and the action
9879 * returned will just be CTL_ACTION_PASS.
9881 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
9884 case CTL_ACTION_BLOCK:
9885 /* Nothing to do here, still blocked */
9887 case CTL_ACTION_OVERLAP:
9888 case CTL_ACTION_OVERLAP_TAG:
9890 * This shouldn't happen! In theory we've already
9891 * checked this command for overlap...
9894 case CTL_ACTION_PASS:
9895 case CTL_ACTION_SKIP: {
9896 struct ctl_softc *softc;
9897 struct ctl_cmd_entry *entry;
9903 * The skip case shouldn't happen, this transaction
9904 * should have never made it onto the blocked queue.
9907 * This I/O is no longer blocked, we can remove it
9908 * from the blocked queue. Since this is a TAILQ
9909 * (doubly linked list), we can do O(1) removals
9910 * from any place on the list.
9912 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
9914 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
9916 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
9918 * Need to send IO back to original side to
9921 union ctl_ha_msg msg_info;
9923 msg_info.hdr.original_sc =
9924 cur_blocked->io_hdr.original_sc;
9925 msg_info.hdr.serializing_sc = cur_blocked;
9926 msg_info.hdr.msg_type = CTL_MSG_R2R;
9927 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
9928 &msg_info, sizeof(msg_info), 0)) >
9929 CTL_HA_STATUS_SUCCESS) {
9930 printf("CTL:Check Blocked error from "
9931 "ctl_ha_msg_send %d\n",
9936 opcode = cur_blocked->scsiio.cdb[0];
9937 entry = &ctl_cmd_table[opcode];
9938 softc = control_softc;
9940 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
9943 * Check this I/O for LUN state changes that may
9944 * have happened while this command was blocked.
9945 * The LUN state may have been changed by a command
9946 * ahead of us in the queue, so we need to re-check
9947 * for any states that can be caused by SCSI
9950 if (ctl_scsiio_lun_check(softc, lun, entry,
9951 &cur_blocked->scsiio) == 0) {
9952 cur_blocked->io_hdr.flags |=
9953 CTL_FLAG_IS_WAS_ON_RTR;
9954 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
9955 &cur_blocked->io_hdr, links);
9957 * In the non CTL_DONE_THREAD case, we need
9958 * to wake up the work thread here. When
9959 * we're processing completed requests from
9960 * the work thread context, we'll pop back
9961 * around and end up pulling things off the
9962 * RtR queue. When we aren't processing
9963 * things from the work thread context,
9964 * though, we won't ever check the RtR queue.
9965 * So we need to wake up the thread to clear
9966 * things off the queue. Otherwise this
9967 * transaction will just sit on the RtR queue
9968 * until a new I/O comes in. (Which may or
9969 * may not happen...)
9971 #ifndef CTL_DONE_THREAD
9972 ctl_wakeup_thread();
9975 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
9980 * This probably shouldn't happen -- we shouldn't
9981 * get CTL_ACTION_ERROR, or anything else.
9987 return (CTL_RETVAL_COMPLETE);
9991 * This routine (with one exception) checks LUN flags that can be set by
9992 * commands ahead of us in the OOA queue. These flags have to be checked
9993 * when a command initially comes in, and when we pull a command off the
9994 * blocked queue and are preparing to execute it. The reason we have to
9995 * check these flags for commands on the blocked queue is that the LUN
9996 * state may have been changed by a command ahead of us while we're on the
9999 * Ordering is somewhat important with these checks, so please pay
10000 * careful attention to the placement of any new checks.
10003 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10004 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10011 * If this shelf is a secondary shelf controller, we have to reject
10012 * any media access commands.
10015 /* No longer needed for HA */
10016 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10017 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10018 ctl_set_lun_standby(ctsio);
10025 * Check for a reservation conflict. If this command isn't allowed
10026 * even on reserved LUNs, and if this initiator isn't the one who
10027 * reserved us, reject the command with a reservation conflict.
10029 if ((lun->flags & CTL_LUN_RESERVED)
10030 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10031 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10032 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10033 || (ctsio->io_hdr.nexus.targ_target.id !=
10034 lun->rsv_nexus.targ_target.id)) {
10035 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10036 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10042 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10043 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10046 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10048 * if we aren't registered or it's a res holder type
10049 * reservation and this isn't the res holder then set a
10051 * NOTE: Commands which might be allowed on write exclusive
10052 * type reservations are checked in the particular command
10053 * for a conflict. Read and SSU are the only ones.
10055 if (!lun->per_res[residx].registered
10056 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10057 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10058 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10065 if ((lun->flags & CTL_LUN_OFFLINE)
10066 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10067 ctl_set_lun_not_ready(ctsio);
10073 * If the LUN is stopped, see if this particular command is allowed
10074 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10076 if ((lun->flags & CTL_LUN_STOPPED)
10077 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10078 /* "Logical unit not ready, initializing cmd. required" */
10079 ctl_set_lun_stopped(ctsio);
10084 if ((lun->flags & CTL_LUN_INOPERABLE)
10085 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10086 /* "Medium format corrupted" */
10087 ctl_set_medium_format_corrupted(ctsio);
10098 ctl_failover_io(union ctl_io *io, int have_lock)
10100 ctl_set_busy(&io->scsiio);
10101 ctl_done_lock(io, have_lock);
10107 struct ctl_lun *lun;
10108 struct ctl_softc *ctl_softc;
10109 union ctl_io *next_io, *pending_io;
10114 ctl_softc = control_softc;
10116 mtx_lock(&ctl_softc->ctl_lock);
10118 * Remove any cmds from the other SC from the rtr queue. These
10119 * will obviously only be for LUNs for which we're the primary.
10120 * We can't send status or get/send data for these commands.
10121 * Since they haven't been executed yet, we can just remove them.
10122 * We'll either abort them or delete them below, depending on
10123 * which HA mode we're in.
10125 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10126 io != NULL; io = next_io) {
10127 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10128 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10129 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10130 ctl_io_hdr, links);
10133 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10134 lun = ctl_softc->ctl_luns[lun_idx];
10139 * Processor LUNs are primary on both sides.
10140 * XXX will this always be true?
10142 if (lun->be_lun->lun_type == T_PROCESSOR)
10145 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10146 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10147 printf("FAILOVER: primary lun %d\n", lun_idx);
10149 * Remove all commands from the other SC. First from the
10150 * blocked queue then from the ooa queue. Once we have
10151 * removed them. Call ctl_check_blocked to see if there
10152 * is anything that can run.
10154 for (io = (union ctl_io *)TAILQ_FIRST(
10155 &lun->blocked_queue); io != NULL; io = next_io) {
10157 next_io = (union ctl_io *)TAILQ_NEXT(
10158 &io->io_hdr, blocked_links);
10160 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10161 TAILQ_REMOVE(&lun->blocked_queue,
10162 &io->io_hdr,blocked_links);
10163 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10164 TAILQ_REMOVE(&lun->ooa_queue,
10165 &io->io_hdr, ooa_links);
10167 ctl_free_io_internal(io, 1);
10171 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10172 io != NULL; io = next_io) {
10174 next_io = (union ctl_io *)TAILQ_NEXT(
10175 &io->io_hdr, ooa_links);
10177 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10179 TAILQ_REMOVE(&lun->ooa_queue,
10183 ctl_free_io_internal(io, 1);
10186 ctl_check_blocked(lun);
10187 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10188 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10190 printf("FAILOVER: primary lun %d\n", lun_idx);
10192 * Abort all commands from the other SC. We can't
10193 * send status back for them now. These should get
10194 * cleaned up when they are completed or come out
10195 * for a datamove operation.
10197 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10198 io != NULL; io = next_io) {
10199 next_io = (union ctl_io *)TAILQ_NEXT(
10200 &io->io_hdr, ooa_links);
10202 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10203 io->io_hdr.flags |= CTL_FLAG_ABORT;
10205 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10206 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10208 printf("FAILOVER: secondary lun %d\n", lun_idx);
10210 lun->flags |= CTL_LUN_PRIMARY_SC;
10213 * We send all I/O that was sent to this controller
10214 * and redirected to the other side back with
10215 * busy status, and have the initiator retry it.
10216 * Figuring out how much data has been transferred,
10217 * etc. and picking up where we left off would be
10220 * XXX KDM need to remove I/O from the blocked
10223 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10224 &lun->ooa_queue); pending_io != NULL;
10225 pending_io = next_io) {
10227 next_io = (union ctl_io *)TAILQ_NEXT(
10228 &pending_io->io_hdr, ooa_links);
10230 pending_io->io_hdr.flags &=
10231 ~CTL_FLAG_SENT_2OTHER_SC;
10233 if (pending_io->io_hdr.flags &
10234 CTL_FLAG_IO_ACTIVE) {
10235 pending_io->io_hdr.flags |=
10238 ctl_set_busy(&pending_io->scsiio);
10239 ctl_done_lock(pending_io,
10245 * Build Unit Attention
10247 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10248 lun->pending_sense[i].ua_pending |=
10249 CTL_UA_ASYM_ACC_CHANGE;
10251 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10252 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10253 printf("FAILOVER: secondary lun %d\n", lun_idx);
10255 * if the first io on the OOA is not on the RtR queue
10258 lun->flags |= CTL_LUN_PRIMARY_SC;
10260 pending_io = (union ctl_io *)TAILQ_FIRST(
10262 if (pending_io==NULL) {
10263 printf("Nothing on OOA queue\n");
10267 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10268 if ((pending_io->io_hdr.flags &
10269 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10270 pending_io->io_hdr.flags |=
10271 CTL_FLAG_IS_WAS_ON_RTR;
10272 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10273 &pending_io->io_hdr, links);
10278 printf("Tag 0x%04x is running\n",
10279 pending_io->scsiio.tag_num);
10283 next_io = (union ctl_io *)TAILQ_NEXT(
10284 &pending_io->io_hdr, ooa_links);
10285 for (pending_io=next_io; pending_io != NULL;
10286 pending_io = next_io) {
10287 pending_io->io_hdr.flags &=
10288 ~CTL_FLAG_SENT_2OTHER_SC;
10289 next_io = (union ctl_io *)TAILQ_NEXT(
10290 &pending_io->io_hdr, ooa_links);
10291 if (pending_io->io_hdr.flags &
10292 CTL_FLAG_IS_WAS_ON_RTR) {
10294 printf("Tag 0x%04x is running\n",
10295 pending_io->scsiio.tag_num);
10300 switch (ctl_check_ooa(lun, pending_io,
10301 (union ctl_io *)TAILQ_PREV(
10302 &pending_io->io_hdr, ctl_ooaq,
10305 case CTL_ACTION_BLOCK:
10306 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10307 &pending_io->io_hdr,
10309 pending_io->io_hdr.flags |=
10312 case CTL_ACTION_PASS:
10313 case CTL_ACTION_SKIP:
10314 pending_io->io_hdr.flags |=
10315 CTL_FLAG_IS_WAS_ON_RTR;
10316 STAILQ_INSERT_TAIL(
10317 &ctl_softc->rtr_queue,
10318 &pending_io->io_hdr, links);
10320 case CTL_ACTION_OVERLAP:
10321 ctl_set_overlapped_cmd(
10322 (struct ctl_scsiio *)pending_io);
10323 ctl_done_lock(pending_io,
10326 case CTL_ACTION_OVERLAP_TAG:
10327 ctl_set_overlapped_tag(
10328 (struct ctl_scsiio *)pending_io,
10329 pending_io->scsiio.tag_num & 0xff);
10330 ctl_done_lock(pending_io,
10333 case CTL_ACTION_ERROR:
10335 ctl_set_internal_failure(
10336 (struct ctl_scsiio *)pending_io,
10339 ctl_done_lock(pending_io,
10346 * Build Unit Attention
10348 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10349 lun->pending_sense[i].ua_pending |=
10350 CTL_UA_ASYM_ACC_CHANGE;
10353 panic("Unhandled HA mode failover, LUN flags = %#x, "
10354 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10358 mtx_unlock(&ctl_softc->ctl_lock);
10362 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10364 struct ctl_lun *lun;
10365 struct ctl_cmd_entry *entry;
10374 opcode = ctsio->cdb[0];
10376 mtx_lock(&ctl_softc->ctl_lock);
10378 if ((ctsio->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10379 && (ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun] != NULL)) {
10380 lun = ctl_softc->ctl_luns[ctsio->io_hdr.nexus.targ_lun];
10382 * If the LUN is invalid, pretend that it doesn't exist.
10383 * It will go away as soon as all pending I/O has been
10386 if (lun->flags & CTL_LUN_DISABLED) {
10389 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10390 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10392 if (lun->be_lun->lun_type == T_PROCESSOR) {
10393 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10397 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10398 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10401 entry = &ctl_cmd_table[opcode];
10403 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10404 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10407 * Check to see whether we can send this command to LUNs that don't
10408 * exist. This should pretty much only be the case for inquiry
10409 * and request sense. Further checks, below, really require having
10410 * a LUN, so we can't really check the command anymore. Just put
10411 * it on the rtr queue.
10414 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10417 ctl_set_unsupported_lun(ctsio);
10418 mtx_unlock(&ctl_softc->ctl_lock);
10419 ctl_done((union ctl_io *)ctsio);
10423 * Every I/O goes into the OOA queue for a particular LUN, and
10424 * stays there until completion.
10426 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10429 * Make sure we support this particular command on this LUN.
10430 * e.g., we don't support writes to the control LUN.
10432 switch (lun->be_lun->lun_type) {
10434 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10435 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10437 ctl_set_invalid_opcode(ctsio);
10438 mtx_unlock(&ctl_softc->ctl_lock);
10439 ctl_done((union ctl_io *)ctsio);
10444 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10445 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10447 ctl_set_invalid_opcode(ctsio);
10448 mtx_unlock(&ctl_softc->ctl_lock);
10449 ctl_done((union ctl_io *)ctsio);
10454 printf("Unsupported CTL LUN type %d\n",
10455 lun->be_lun->lun_type);
10456 panic("Unsupported CTL LUN type %d\n",
10457 lun->be_lun->lun_type);
10458 break; /* NOTREACHED */
10462 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10465 * If we've got a request sense, it'll clear the contingent
10466 * allegiance condition. Otherwise, if we have a CA condition for
10467 * this initiator, clear it, because it sent down a command other
10468 * than request sense.
10470 if ((opcode != REQUEST_SENSE)
10471 && (ctl_is_set(lun->have_ca, initidx)))
10472 ctl_clear_mask(lun->have_ca, initidx);
10475 * If the command has this flag set, it handles its own unit
10476 * attention reporting, we shouldn't do anything. Otherwise we
10477 * check for any pending unit attentions, and send them back to the
10478 * initiator. We only do this when a command initially comes in,
10479 * not when we pull it off the blocked queue.
10481 * According to SAM-3, section 5.3.2, the order that things get
10482 * presented back to the host is basically unit attentions caused
10483 * by some sort of reset event, busy status, reservation conflicts
10484 * or task set full, and finally any other status.
10486 * One issue here is that some of the unit attentions we report
10487 * don't fall into the "reset" category (e.g. "reported luns data
10488 * has changed"). So reporting it here, before the reservation
10489 * check, may be technically wrong. I guess the only thing to do
10490 * would be to check for and report the reset events here, and then
10491 * check for the other unit attention types after we check for a
10492 * reservation conflict.
10494 * XXX KDM need to fix this
10496 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10497 ctl_ua_type ua_type;
10499 ua_type = lun->pending_sense[initidx].ua_pending;
10500 if (ua_type != CTL_UA_NONE) {
10501 scsi_sense_data_type sense_format;
10504 sense_format = (lun->flags &
10505 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10508 sense_format = SSD_TYPE_FIXED;
10510 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10512 if (ua_type != CTL_UA_NONE) {
10513 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10514 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10516 ctsio->sense_len = SSD_FULL_SIZE;
10517 lun->pending_sense[initidx].ua_pending &=
10519 mtx_unlock(&ctl_softc->ctl_lock);
10520 ctl_done((union ctl_io *)ctsio);
10527 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10528 mtx_unlock(&ctl_softc->ctl_lock);
10529 ctl_done((union ctl_io *)ctsio);
10534 * XXX CHD this is where we want to send IO to other side if
10535 * this LUN is secondary on this SC. We will need to make a copy
10536 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10537 * the copy we send as FROM_OTHER.
10538 * We also need to stuff the address of the original IO so we can
10539 * find it easily. Something similar will need be done on the other
10540 * side so when we are done we can find the copy.
10542 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10543 union ctl_ha_msg msg_info;
10546 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10548 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10549 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10551 printf("1. ctsio %p\n", ctsio);
10553 msg_info.hdr.serializing_sc = NULL;
10554 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10555 msg_info.scsi.tag_num = ctsio->tag_num;
10556 msg_info.scsi.tag_type = ctsio->tag_type;
10557 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10559 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10561 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10562 (void *)&msg_info, sizeof(msg_info), 0)) >
10563 CTL_HA_STATUS_SUCCESS) {
10564 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10566 printf("CTL:opcode is %x\n",opcode);
10569 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10574 * XXX KDM this I/O is off the incoming queue, but hasn't
10575 * been inserted on any other queue. We may need to come
10576 * up with a holding queue while we wait for serialization
10577 * so that we have an idea of what we're waiting for from
10580 goto bailout_unlock;
10583 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10584 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10585 ctl_ooaq, ooa_links))) {
10586 case CTL_ACTION_BLOCK:
10587 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10588 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10590 goto bailout_unlock;
10591 break; /* NOTREACHED */
10592 case CTL_ACTION_PASS:
10593 case CTL_ACTION_SKIP:
10595 break; /* NOTREACHED */
10596 case CTL_ACTION_OVERLAP:
10597 ctl_set_overlapped_cmd(ctsio);
10598 mtx_unlock(&ctl_softc->ctl_lock);
10599 ctl_done((union ctl_io *)ctsio);
10601 break; /* NOTREACHED */
10602 case CTL_ACTION_OVERLAP_TAG:
10603 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10604 mtx_unlock(&ctl_softc->ctl_lock);
10605 ctl_done((union ctl_io *)ctsio);
10607 break; /* NOTREACHED */
10608 case CTL_ACTION_ERROR:
10610 ctl_set_internal_failure(ctsio,
10612 /*retry_count*/ 0);
10613 mtx_unlock(&ctl_softc->ctl_lock);
10614 ctl_done((union ctl_io *)ctsio);
10616 break; /* NOTREACHED */
10619 goto bailout_unlock;
10622 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10623 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
10626 mtx_unlock(&ctl_softc->ctl_lock);
10633 ctl_scsiio(struct ctl_scsiio *ctsio)
10636 struct ctl_cmd_entry *entry;
10638 retval = CTL_RETVAL_COMPLETE;
10640 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
10642 entry = &ctl_cmd_table[ctsio->cdb[0]];
10645 * If this I/O has been aborted, just send it straight to
10646 * ctl_done() without executing it.
10648 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
10649 ctl_done((union ctl_io *)ctsio);
10654 * All the checks should have been handled by ctl_scsiio_precheck().
10655 * We should be clear now to just execute the I/O.
10657 retval = entry->execute(ctsio);
10664 * Since we only implement one target right now, a bus reset simply resets
10665 * our single target.
10668 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
10670 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
10674 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
10675 ctl_ua_type ua_type)
10677 struct ctl_lun *lun;
10680 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
10681 union ctl_ha_msg msg_info;
10683 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10684 msg_info.hdr.nexus = io->io_hdr.nexus;
10685 if (ua_type==CTL_UA_TARG_RESET)
10686 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
10688 msg_info.task.task_action = CTL_TASK_BUS_RESET;
10689 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
10690 msg_info.hdr.original_sc = NULL;
10691 msg_info.hdr.serializing_sc = NULL;
10692 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10693 (void *)&msg_info, sizeof(msg_info), 0)) {
10698 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
10699 retval += ctl_lun_reset(lun, io, ua_type);
10705 * The LUN should always be set. The I/O is optional, and is used to
10706 * distinguish between I/Os sent by this initiator, and by other
10707 * initiators. We set unit attention for initiators other than this one.
10708 * SAM-3 is vague on this point. It does say that a unit attention should
10709 * be established for other initiators when a LUN is reset (see section
10710 * 5.7.3), but it doesn't specifically say that the unit attention should
10711 * be established for this particular initiator when a LUN is reset. Here
10712 * is the relevant text, from SAM-3 rev 8:
10714 * 5.7.2 When a SCSI initiator port aborts its own tasks
10716 * When a SCSI initiator port causes its own task(s) to be aborted, no
10717 * notification that the task(s) have been aborted shall be returned to
10718 * the SCSI initiator port other than the completion response for the
10719 * command or task management function action that caused the task(s) to
10720 * be aborted and notification(s) associated with related effects of the
10721 * action (e.g., a reset unit attention condition).
10723 * XXX KDM for now, we're setting unit attention for all initiators.
10726 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
10730 uint32_t initindex;
10735 * Run through the OOA queue and abort each I/O.
10738 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10740 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10741 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10742 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10746 * This version sets unit attention for every
10749 initindex = ctl_get_initindex(&io->io_hdr.nexus);
10750 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10751 if (initindex == i)
10753 lun->pending_sense[i].ua_pending |= ua_type;
10758 * A reset (any kind, really) clears reservations established with
10759 * RESERVE/RELEASE. It does not clear reservations established
10760 * with PERSISTENT RESERVE OUT, but we don't support that at the
10761 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
10762 * reservations made with the RESERVE/RELEASE commands, because
10763 * those commands are obsolete in SPC-3.
10765 lun->flags &= ~CTL_LUN_RESERVED;
10767 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10768 ctl_clear_mask(lun->have_ca, i);
10769 lun->pending_sense[i].ua_pending |= ua_type;
10776 ctl_abort_task(union ctl_io *io)
10779 struct ctl_lun *lun;
10780 struct ctl_softc *ctl_softc;
10783 char printbuf[128];
10787 ctl_softc = control_softc;
10793 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
10794 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
10795 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
10800 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
10801 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
10805 * Run through the OOA queue and attempt to find the given I/O.
10806 * The target port, initiator ID, tag type and tag number have to
10807 * match the values that we got from the initiator. If we have an
10808 * untagged command to abort, simply abort the first untagged command
10809 * we come to. We only allow one untagged command at a time of course.
10812 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10814 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10815 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10817 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
10819 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
10820 lun->lun, xio->scsiio.tag_num,
10821 xio->scsiio.tag_type,
10822 (xio->io_hdr.blocked_links.tqe_prev
10823 == NULL) ? "" : " BLOCKED",
10824 (xio->io_hdr.flags &
10825 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
10826 (xio->io_hdr.flags &
10827 CTL_FLAG_ABORT) ? " ABORT" : ""),
10828 (xio->io_hdr.flags &
10829 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "");
10830 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
10832 printf("%s\n", sbuf_data(&sb));
10835 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
10836 && (xio->io_hdr.nexus.initid.id ==
10837 io->io_hdr.nexus.initid.id)) {
10839 * If the abort says that the task is untagged, the
10840 * task in the queue must be untagged. Otherwise,
10841 * we just check to see whether the tag numbers
10842 * match. This is because the QLogic firmware
10843 * doesn't pass back the tag type in an abort
10847 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
10848 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
10849 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
10852 * XXX KDM we've got problems with FC, because it
10853 * doesn't send down a tag type with aborts. So we
10854 * can only really go by the tag number...
10855 * This may cause problems with parallel SCSI.
10856 * Need to figure that out!!
10858 if (xio->scsiio.tag_num == io->taskio.tag_num) {
10859 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10861 if ((io->io_hdr.flags &
10862 CTL_FLAG_FROM_OTHER_SC) == 0 &&
10863 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
10864 union ctl_ha_msg msg_info;
10866 io->io_hdr.flags |=
10867 CTL_FLAG_SENT_2OTHER_SC;
10868 msg_info.hdr.nexus = io->io_hdr.nexus;
10869 msg_info.task.task_action =
10870 CTL_TASK_ABORT_TASK;
10871 msg_info.task.tag_num =
10872 io->taskio.tag_num;
10873 msg_info.task.tag_type =
10874 io->taskio.tag_type;
10875 msg_info.hdr.msg_type =
10876 CTL_MSG_MANAGE_TASKS;
10877 msg_info.hdr.original_sc = NULL;
10878 msg_info.hdr.serializing_sc = NULL;
10880 printf("Sent Abort to other side\n");
10882 if (CTL_HA_STATUS_SUCCESS !=
10883 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10885 sizeof(msg_info), 0)) {
10889 printf("ctl_abort_task: found I/O to abort\n");
10900 * This isn't really an error. It's entirely possible for
10901 * the abort and command completion to cross on the wire.
10902 * This is more of an informative/diagnostic error.
10905 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
10906 "%d:%d:%d:%d tag %d type %d\n",
10907 io->io_hdr.nexus.initid.id,
10908 io->io_hdr.nexus.targ_port,
10909 io->io_hdr.nexus.targ_target.id,
10910 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
10911 io->taskio.tag_type);
10919 * Assumptions: caller holds ctl_softc->ctl_lock
10921 * This routine cannot block! It must be callable from an interrupt
10922 * handler as well as from the work thread.
10925 ctl_run_task_queue(struct ctl_softc *ctl_softc)
10927 union ctl_io *io, *next_io;
10929 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
10931 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
10932 io != NULL; io = next_io) {
10934 const char *task_desc;
10936 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10940 switch (io->io_hdr.io_type) {
10941 case CTL_IO_TASK: {
10942 task_desc = ctl_scsi_task_string(&io->taskio);
10943 if (task_desc != NULL) {
10945 csevent_log(CSC_CTL | CSC_SHELF_SW |
10947 csevent_LogType_Trace,
10948 csevent_Severity_Information,
10949 csevent_AlertLevel_Green,
10950 csevent_FRU_Firmware,
10951 csevent_FRU_Unknown,
10952 "CTL: received task: %s",task_desc);
10956 csevent_log(CSC_CTL | CSC_SHELF_SW |
10958 csevent_LogType_Trace,
10959 csevent_Severity_Information,
10960 csevent_AlertLevel_Green,
10961 csevent_FRU_Firmware,
10962 csevent_FRU_Unknown,
10963 "CTL: received unknown task "
10965 io->taskio.task_action,
10966 io->taskio.task_action);
10969 switch (io->taskio.task_action) {
10970 case CTL_TASK_ABORT_TASK:
10971 retval = ctl_abort_task(io);
10973 case CTL_TASK_ABORT_TASK_SET:
10975 case CTL_TASK_CLEAR_ACA:
10977 case CTL_TASK_CLEAR_TASK_SET:
10979 case CTL_TASK_LUN_RESET: {
10980 struct ctl_lun *lun;
10984 targ_lun = io->io_hdr.nexus.targ_lun;
10986 if ((targ_lun < CTL_MAX_LUNS)
10987 && (ctl_softc->ctl_luns[targ_lun] != NULL))
10988 lun = ctl_softc->ctl_luns[targ_lun];
10994 if (!(io->io_hdr.flags &
10995 CTL_FLAG_FROM_OTHER_SC)) {
10996 union ctl_ha_msg msg_info;
10998 io->io_hdr.flags |=
10999 CTL_FLAG_SENT_2OTHER_SC;
11000 msg_info.hdr.msg_type =
11001 CTL_MSG_MANAGE_TASKS;
11002 msg_info.hdr.nexus = io->io_hdr.nexus;
11003 msg_info.task.task_action =
11004 CTL_TASK_LUN_RESET;
11005 msg_info.hdr.original_sc = NULL;
11006 msg_info.hdr.serializing_sc = NULL;
11007 if (CTL_HA_STATUS_SUCCESS !=
11008 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11010 sizeof(msg_info), 0)) {
11014 retval = ctl_lun_reset(lun, io,
11018 case CTL_TASK_TARGET_RESET:
11019 retval = ctl_target_reset(ctl_softc, io,
11020 CTL_UA_TARG_RESET);
11022 case CTL_TASK_BUS_RESET:
11023 retval = ctl_bus_reset(ctl_softc, io);
11025 case CTL_TASK_PORT_LOGIN:
11027 case CTL_TASK_PORT_LOGOUT:
11030 printf("ctl_run_task_queue: got unknown task "
11031 "management event %d\n",
11032 io->taskio.task_action);
11036 io->io_hdr.status = CTL_SUCCESS;
11038 io->io_hdr.status = CTL_ERROR;
11040 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11041 ctl_io_hdr, links);
11043 * This will queue this I/O to the done queue, but the
11044 * work thread won't be able to process it until we
11045 * return and the lock is released.
11047 ctl_done_lock(io, /*have_lock*/ 1);
11052 printf("%s: invalid I/O type %d msg %d cdb %x"
11053 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11054 __func__, io->io_hdr.io_type,
11055 io->io_hdr.msg_type, io->scsiio.cdb[0],
11056 (uintmax_t)io->io_hdr.nexus.initid.id,
11057 io->io_hdr.nexus.targ_port,
11058 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11059 io->io_hdr.nexus.targ_lun,
11060 (io->io_hdr.io_type == CTL_IO_TASK) ?
11061 io->taskio.tag_num : io->scsiio.tag_num);
11062 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11063 ctl_io_hdr, links);
11064 ctl_free_io_internal(io, 1);
11070 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11074 * For HA operation. Handle commands that come in from the other
11078 ctl_handle_isc(union ctl_io *io)
11081 struct ctl_lun *lun;
11082 struct ctl_softc *ctl_softc;
11084 ctl_softc = control_softc;
11086 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
11088 switch (io->io_hdr.msg_type) {
11089 case CTL_MSG_SERIALIZE:
11090 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11093 case CTL_MSG_R2R: {
11095 struct ctl_cmd_entry *entry;
11098 * This is only used in SER_ONLY mode.
11101 opcode = io->scsiio.cdb[0];
11102 entry = &ctl_cmd_table[opcode];
11103 mtx_lock(&ctl_softc->ctl_lock);
11104 if (ctl_scsiio_lun_check(ctl_softc, lun,
11105 entry, (struct ctl_scsiio *)io) != 0) {
11106 ctl_done_lock(io, /*have_lock*/ 1);
11107 mtx_unlock(&ctl_softc->ctl_lock);
11110 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11111 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11112 &io->io_hdr, links);
11113 mtx_unlock(&ctl_softc->ctl_lock);
11116 case CTL_MSG_FINISH_IO:
11117 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11119 ctl_done_lock(io, /*have_lock*/ 0);
11122 mtx_lock(&ctl_softc->ctl_lock);
11123 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11125 STAILQ_REMOVE(&ctl_softc->task_queue,
11126 &io->io_hdr, ctl_io_hdr, links);
11127 ctl_check_blocked(lun);
11128 mtx_unlock(&ctl_softc->ctl_lock);
11131 case CTL_MSG_PERS_ACTION:
11132 ctl_hndl_per_res_out_on_other_sc(
11133 (union ctl_ha_msg *)&io->presio.pr_msg);
11136 case CTL_MSG_BAD_JUJU:
11138 ctl_done_lock(io, /*have_lock*/ 0);
11140 case CTL_MSG_DATAMOVE:
11141 /* Only used in XFER mode */
11143 ctl_datamove_remote(io);
11145 case CTL_MSG_DATAMOVE_DONE:
11146 /* Only used in XFER mode */
11148 io->scsiio.be_move_done(io);
11152 printf("%s: Invalid message type %d\n",
11153 __func__, io->io_hdr.msg_type);
11157 ctl_free_io_internal(io, 0);
11163 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11164 * there is no match.
11166 static ctl_lun_error_pattern
11167 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11169 struct ctl_cmd_entry *entry;
11170 ctl_lun_error_pattern filtered_pattern, pattern;
11173 pattern = desc->error_pattern;
11176 * XXX KDM we need more data passed into this function to match a
11177 * custom pattern, and we actually need to implement custom pattern
11180 if (pattern & CTL_LUN_PAT_CMD)
11181 return (CTL_LUN_PAT_CMD);
11183 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11184 return (CTL_LUN_PAT_ANY);
11186 opcode = ctsio->cdb[0];
11187 entry = &ctl_cmd_table[opcode];
11189 filtered_pattern = entry->pattern & pattern;
11192 * If the user requested specific flags in the pattern (e.g.
11193 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11196 * If the user did not specify any flags, it doesn't matter whether
11197 * or not the command supports the flags.
11199 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11200 (pattern & ~CTL_LUN_PAT_MASK))
11201 return (CTL_LUN_PAT_NONE);
11204 * If the user asked for a range check, see if the requested LBA
11205 * range overlaps with this command's LBA range.
11207 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11213 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11215 return (CTL_LUN_PAT_NONE);
11217 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11218 desc->lba_range.len);
11220 * A "pass" means that the LBA ranges don't overlap, so
11221 * this doesn't match the user's range criteria.
11223 if (action == CTL_ACTION_PASS)
11224 return (CTL_LUN_PAT_NONE);
11227 return (filtered_pattern);
11231 * Called with the CTL lock held.
11234 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11236 struct ctl_error_desc *desc, *desc2;
11238 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11239 ctl_lun_error_pattern pattern;
11241 * Check to see whether this particular command matches
11242 * the pattern in the descriptor.
11244 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11245 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11248 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11249 case CTL_LUN_INJ_ABORTED:
11250 ctl_set_aborted(&io->scsiio);
11252 case CTL_LUN_INJ_MEDIUM_ERR:
11253 ctl_set_medium_error(&io->scsiio);
11255 case CTL_LUN_INJ_UA:
11256 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11258 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11260 case CTL_LUN_INJ_CUSTOM:
11262 * We're assuming the user knows what he is doing.
11263 * Just copy the sense information without doing
11266 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11267 ctl_min(sizeof(desc->custom_sense),
11268 sizeof(io->scsiio.sense_data)));
11269 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11270 io->scsiio.sense_len = SSD_FULL_SIZE;
11271 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11273 case CTL_LUN_INJ_NONE:
11276 * If this is an error injection type we don't know
11277 * about, clear the continuous flag (if it is set)
11278 * so it will get deleted below.
11280 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11284 * By default, each error injection action is a one-shot
11286 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11289 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11295 #ifdef CTL_IO_DELAY
11297 ctl_datamove_timer_wakeup(void *arg)
11301 io = (union ctl_io *)arg;
11305 #endif /* CTL_IO_DELAY */
11308 * Assumption: caller does NOT hold ctl_lock
11311 ctl_datamove(union ctl_io *io)
11313 void (*fe_datamove)(union ctl_io *io);
11315 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11318 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11323 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11324 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11326 sbuf_cat(&sb, path_str);
11327 switch (io->io_hdr.io_type) {
11329 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11330 sbuf_printf(&sb, "\n");
11331 sbuf_cat(&sb, path_str);
11332 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11333 io->scsiio.tag_num, io->scsiio.tag_type);
11336 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11337 "Tag Type: %d\n", io->taskio.task_action,
11338 io->taskio.tag_num, io->taskio.tag_type);
11341 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11342 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11345 sbuf_cat(&sb, path_str);
11346 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11347 (intmax_t)time_uptime - io->io_hdr.start_time);
11349 printf("%s", sbuf_data(&sb));
11351 #endif /* CTL_TIME_IO */
11353 mtx_lock(&control_softc->ctl_lock);
11354 #ifdef CTL_IO_DELAY
11355 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11356 struct ctl_lun *lun;
11358 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11360 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11362 struct ctl_lun *lun;
11364 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11366 && (lun->delay_info.datamove_delay > 0)) {
11367 struct callout *callout;
11369 callout = (struct callout *)&io->io_hdr.timer_bytes;
11370 callout_init(callout, /*mpsafe*/ 1);
11371 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11372 callout_reset(callout,
11373 lun->delay_info.datamove_delay * hz,
11374 ctl_datamove_timer_wakeup, io);
11375 if (lun->delay_info.datamove_type ==
11376 CTL_DELAY_TYPE_ONESHOT)
11377 lun->delay_info.datamove_delay = 0;
11378 mtx_unlock(&control_softc->ctl_lock);
11384 * If we have any pending task management commands, process them
11385 * first. This is necessary to eliminate a race condition with the
11388 * - FETD submits a task management command, like an abort.
11389 * - Back end calls fe_datamove() to move the data for the aborted
11390 * command. The FETD can't really accept it, but if it did, it
11391 * would end up transmitting data for a command that the initiator
11392 * told us to abort.
11394 * We close the race by processing all pending task management
11395 * commands here (we can't block!), and then check this I/O to see
11396 * if it has been aborted. If so, return it to the back end with
11397 * bad status, so the back end can say return an error to the back end
11398 * and then when the back end returns an error, we can return the
11399 * aborted command to the FETD, so it can clean up its resources.
11401 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11402 ctl_run_task_queue(control_softc);
11405 * This command has been aborted. Set the port status, so we fail
11408 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11409 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11410 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11411 io->io_hdr.nexus.targ_port,
11412 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11413 io->io_hdr.nexus.targ_lun);
11414 io->io_hdr.status = CTL_CMD_ABORTED;
11415 io->io_hdr.port_status = 31337;
11416 mtx_unlock(&control_softc->ctl_lock);
11418 * Note that the backend, in this case, will get the
11419 * callback in its context. In other cases it may get
11420 * called in the frontend's interrupt thread context.
11422 io->scsiio.be_move_done(io);
11427 * If we're in XFER mode and this I/O is from the other shelf
11428 * controller, we need to send the DMA to the other side to
11429 * actually transfer the data to/from the host. In serialize only
11430 * mode the transfer happens below CTL and ctl_datamove() is only
11431 * called on the machine that originally received the I/O.
11433 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11434 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11435 union ctl_ha_msg msg;
11436 uint32_t sg_entries_sent;
11440 memset(&msg, 0, sizeof(msg));
11441 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11442 msg.hdr.original_sc = io->io_hdr.original_sc;
11443 msg.hdr.serializing_sc = io;
11444 msg.hdr.nexus = io->io_hdr.nexus;
11445 msg.dt.flags = io->io_hdr.flags;
11447 * We convert everything into a S/G list here. We can't
11448 * pass by reference, only by value between controllers.
11449 * So we can't pass a pointer to the S/G list, only as many
11450 * S/G entries as we can fit in here. If it's possible for
11451 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11452 * then we need to break this up into multiple transfers.
11454 if (io->scsiio.kern_sg_entries == 0) {
11455 msg.dt.kern_sg_entries = 1;
11457 * If this is in cached memory, flush the cache
11458 * before we send the DMA request to the other
11459 * controller. We want to do this in either the
11460 * read or the write case. The read case is
11461 * straightforward. In the write case, we want to
11462 * make sure nothing is in the local cache that
11463 * could overwrite the DMAed data.
11465 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11467 * XXX KDM use bus_dmamap_sync() here.
11472 * Convert to a physical address if this is a
11475 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11476 msg.dt.sg_list[0].addr =
11477 io->scsiio.kern_data_ptr;
11480 * XXX KDM use busdma here!
11483 msg.dt.sg_list[0].addr = (void *)
11484 vtophys(io->scsiio.kern_data_ptr);
11488 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11491 struct ctl_sg_entry *sgl;
11494 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11495 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11496 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11498 * XXX KDM use bus_dmamap_sync() here.
11503 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11504 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11505 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11506 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11507 msg.dt.sg_sequence = 0;
11510 * Loop until we've sent all of the S/G entries. On the
11511 * other end, we'll recompose these S/G entries into one
11512 * contiguous list before passing it to the
11514 for (sg_entries_sent = 0; sg_entries_sent <
11515 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11516 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11517 sizeof(msg.dt.sg_list[0])),
11518 msg.dt.kern_sg_entries - sg_entries_sent);
11520 if (do_sg_copy != 0) {
11521 struct ctl_sg_entry *sgl;
11524 sgl = (struct ctl_sg_entry *)
11525 io->scsiio.kern_data_ptr;
11527 * If this is in cached memory, flush the cache
11528 * before we send the DMA request to the other
11529 * controller. We want to do this in either
11530 * the * read or the write case. The read
11531 * case is straightforward. In the write
11532 * case, we want to make sure nothing is
11533 * in the local cache that could overwrite
11537 for (i = sg_entries_sent, j = 0;
11538 i < msg.dt.cur_sg_entries; i++, j++) {
11539 if ((io->io_hdr.flags &
11540 CTL_FLAG_NO_DATASYNC) == 0) {
11542 * XXX KDM use bus_dmamap_sync()
11545 if ((io->io_hdr.flags &
11546 CTL_FLAG_BUS_ADDR) == 0) {
11548 * XXX KDM use busdma.
11551 msg.dt.sg_list[j].addr =(void *)
11552 vtophys(sgl[i].addr);
11555 msg.dt.sg_list[j].addr =
11558 msg.dt.sg_list[j].len = sgl[i].len;
11562 sg_entries_sent += msg.dt.cur_sg_entries;
11563 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11564 msg.dt.sg_last = 1;
11566 msg.dt.sg_last = 0;
11569 * XXX KDM drop and reacquire the lock here?
11571 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11572 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11574 * XXX do something here.
11578 msg.dt.sent_sg_entries = sg_entries_sent;
11580 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11581 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11582 ctl_failover_io(io, /*have_lock*/ 1);
11587 * Lookup the fe_datamove() function for this particular
11591 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11592 mtx_unlock(&control_softc->ctl_lock);
11599 ctl_send_datamove_done(union ctl_io *io, int have_lock)
11601 union ctl_ha_msg msg;
11604 memset(&msg, 0, sizeof(msg));
11606 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11607 msg.hdr.original_sc = io;
11608 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11609 msg.hdr.nexus = io->io_hdr.nexus;
11610 msg.hdr.status = io->io_hdr.status;
11611 msg.scsi.tag_num = io->scsiio.tag_num;
11612 msg.scsi.tag_type = io->scsiio.tag_type;
11613 msg.scsi.scsi_status = io->scsiio.scsi_status;
11614 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11615 sizeof(io->scsiio.sense_data));
11616 msg.scsi.sense_len = io->scsiio.sense_len;
11617 msg.scsi.sense_residual = io->scsiio.sense_residual;
11618 msg.scsi.fetd_status = io->io_hdr.port_status;
11619 msg.scsi.residual = io->scsiio.residual;
11620 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11622 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
11623 ctl_failover_io(io, /*have_lock*/ have_lock);
11627 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
11628 if (isc_status > CTL_HA_STATUS_SUCCESS) {
11629 /* XXX do something if this fails */
11635 * The DMA to the remote side is done, now we need to tell the other side
11636 * we're done so it can continue with its data movement.
11639 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
11645 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11646 printf("%s: ISC DMA write failed with error %d", __func__,
11648 ctl_set_internal_failure(&io->scsiio,
11650 /*retry_count*/ rq->ret);
11653 ctl_dt_req_free(rq);
11656 * In this case, we had to malloc the memory locally. Free it.
11658 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11660 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11661 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11664 * The data is in local and remote memory, so now we need to send
11665 * status (good or back) back to the other side.
11667 ctl_send_datamove_done(io, /*have_lock*/ 0);
11671 * We've moved the data from the host/controller into local memory. Now we
11672 * need to push it over to the remote controller's memory.
11675 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
11681 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
11682 ctl_datamove_remote_write_cb);
11688 ctl_datamove_remote_write(union ctl_io *io)
11691 void (*fe_datamove)(union ctl_io *io);
11694 * - Get the data from the host/HBA into local memory.
11695 * - DMA memory from the local controller to the remote controller.
11696 * - Send status back to the remote controller.
11699 retval = ctl_datamove_remote_sgl_setup(io);
11703 /* Switch the pointer over so the FETD knows what to do */
11704 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11707 * Use a custom move done callback, since we need to send completion
11708 * back to the other controller, not to the backend on this side.
11710 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
11712 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11721 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
11730 * In this case, we had to malloc the memory locally. Free it.
11732 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11734 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11735 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11739 scsi_path_string(io, path_str, sizeof(path_str));
11740 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11741 sbuf_cat(&sb, path_str);
11742 scsi_command_string(&io->scsiio, NULL, &sb);
11743 sbuf_printf(&sb, "\n");
11744 sbuf_cat(&sb, path_str);
11745 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11746 io->scsiio.tag_num, io->scsiio.tag_type);
11747 sbuf_cat(&sb, path_str);
11748 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
11749 io->io_hdr.flags, io->io_hdr.status);
11751 printk("%s", sbuf_data(&sb));
11756 * The read is done, now we need to send status (good or bad) back
11757 * to the other side.
11759 ctl_send_datamove_done(io, /*have_lock*/ 0);
11765 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
11768 void (*fe_datamove)(union ctl_io *io);
11772 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11773 printf("%s: ISC DMA read failed with error %d", __func__,
11775 ctl_set_internal_failure(&io->scsiio,
11777 /*retry_count*/ rq->ret);
11780 ctl_dt_req_free(rq);
11782 /* Switch the pointer over so the FETD knows what to do */
11783 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11786 * Use a custom move done callback, since we need to send completion
11787 * back to the other controller, not to the backend on this side.
11789 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
11791 /* XXX KDM add checks like the ones in ctl_datamove? */
11793 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11799 ctl_datamove_remote_sgl_setup(union ctl_io *io)
11801 struct ctl_sg_entry *local_sglist, *remote_sglist;
11802 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
11803 struct ctl_softc *softc;
11808 softc = control_softc;
11810 local_sglist = io->io_hdr.local_sglist;
11811 local_dma_sglist = io->io_hdr.local_dma_sglist;
11812 remote_sglist = io->io_hdr.remote_sglist;
11813 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11815 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
11816 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
11817 local_sglist[i].len = remote_sglist[i].len;
11820 * XXX Detect the situation where the RS-level I/O
11821 * redirector on the other side has already read the
11822 * data off of the AOR RS on this side, and
11823 * transferred it to remote (mirror) memory on the
11824 * other side. Since we already have the data in
11825 * memory here, we just need to use it.
11827 * XXX KDM this can probably be removed once we
11828 * get the cache device code in and take the
11829 * current AOR implementation out.
11832 if ((remote_sglist[i].addr >=
11833 (void *)vtophys(softc->mirr->addr))
11834 && (remote_sglist[i].addr <
11835 ((void *)vtophys(softc->mirr->addr) +
11836 CacheMirrorOffset))) {
11837 local_sglist[i].addr = remote_sglist[i].addr -
11839 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
11841 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
11843 local_sglist[i].addr = remote_sglist[i].addr +
11848 printf("%s: local %p, remote %p, len %d\n",
11849 __func__, local_sglist[i].addr,
11850 remote_sglist[i].addr, local_sglist[i].len);
11854 uint32_t len_to_go;
11857 * In this case, we don't have automatically allocated
11858 * memory for this I/O on this controller. This typically
11859 * happens with internal CTL I/O -- e.g. inquiry, mode
11860 * sense, etc. Anything coming from RAIDCore will have
11861 * a mirror area available.
11863 len_to_go = io->scsiio.kern_data_len;
11866 * Clear the no datasync flag, we have to use malloced
11869 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
11872 * The difficult thing here is that the size of the various
11873 * S/G segments may be different than the size from the
11874 * remote controller. That'll make it harder when DMAing
11875 * the data back to the other side.
11877 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
11878 sizeof(io->io_hdr.remote_sglist[0])) &&
11879 (len_to_go > 0); i++) {
11880 local_sglist[i].len = ctl_min(len_to_go, 131072);
11881 CTL_SIZE_8B(local_dma_sglist[i].len,
11882 local_sglist[i].len);
11883 local_sglist[i].addr =
11884 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
11886 local_dma_sglist[i].addr = local_sglist[i].addr;
11888 if (local_sglist[i].addr == NULL) {
11891 printf("malloc failed for %zd bytes!",
11892 local_dma_sglist[i].len);
11893 for (j = 0; j < i; j++) {
11894 free(local_sglist[j].addr, M_CTL);
11896 ctl_set_internal_failure(&io->scsiio,
11898 /*retry_count*/ 4857);
11900 goto bailout_error;
11903 /* XXX KDM do we need a sync here? */
11905 len_to_go -= local_sglist[i].len;
11908 * Reset the number of S/G entries accordingly. The
11909 * original number of S/G entries is available in
11912 io->scsiio.kern_sg_entries = i;
11915 printf("%s: kern_sg_entries = %d\n", __func__,
11916 io->scsiio.kern_sg_entries);
11917 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11918 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
11919 local_sglist[i].addr, local_sglist[i].len,
11920 local_dma_sglist[i].len);
11929 ctl_send_datamove_done(io, /*have_lock*/ 0);
11935 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
11936 ctl_ha_dt_cb callback)
11938 struct ctl_ha_dt_req *rq;
11939 struct ctl_sg_entry *remote_sglist, *local_sglist;
11940 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
11941 uint32_t local_used, remote_used, total_used;
11947 rq = ctl_dt_req_alloc();
11950 * If we failed to allocate the request, and if the DMA didn't fail
11951 * anyway, set busy status. This is just a resource allocation
11955 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
11956 ctl_set_busy(&io->scsiio);
11958 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
11961 ctl_dt_req_free(rq);
11964 * The data move failed. We need to return status back
11965 * to the other controller. No point in trying to DMA
11966 * data to the remote controller.
11969 ctl_send_datamove_done(io, /*have_lock*/ 0);
11976 local_sglist = io->io_hdr.local_sglist;
11977 local_dma_sglist = io->io_hdr.local_dma_sglist;
11978 remote_sglist = io->io_hdr.remote_sglist;
11979 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11984 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
11985 rq->ret = CTL_HA_STATUS_SUCCESS;
11992 * Pull/push the data over the wire from/to the other controller.
11993 * This takes into account the possibility that the local and
11994 * remote sglists may not be identical in terms of the size of
11995 * the elements and the number of elements.
11997 * One fundamental assumption here is that the length allocated for
11998 * both the local and remote sglists is identical. Otherwise, we've
11999 * essentially got a coding error of some sort.
12001 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12003 uint32_t cur_len, dma_length;
12006 rq->id = CTL_HA_DATA_CTL;
12007 rq->command = command;
12011 * Both pointers should be aligned. But it is possible
12012 * that the allocation length is not. They should both
12013 * also have enough slack left over at the end, though,
12014 * to round up to the next 8 byte boundary.
12016 cur_len = ctl_min(local_sglist[i].len - local_used,
12017 remote_sglist[j].len - remote_used);
12020 * In this case, we have a size issue and need to decrease
12021 * the size, except in the case where we actually have less
12022 * than 8 bytes left. In that case, we need to increase
12023 * the DMA length to get the last bit.
12025 if ((cur_len & 0x7) != 0) {
12026 if (cur_len > 0x7) {
12027 cur_len = cur_len - (cur_len & 0x7);
12028 dma_length = cur_len;
12030 CTL_SIZE_8B(dma_length, cur_len);
12034 dma_length = cur_len;
12037 * If we had to allocate memory for this I/O, instead of using
12038 * the non-cached mirror memory, we'll need to flush the cache
12039 * before trying to DMA to the other controller.
12041 * We could end up doing this multiple times for the same
12042 * segment if we have a larger local segment than remote
12043 * segment. That shouldn't be an issue.
12045 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12047 * XXX KDM use bus_dmamap_sync() here.
12051 rq->size = dma_length;
12053 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12054 tmp_ptr += local_used;
12056 /* Use physical addresses when talking to ISC hardware */
12057 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12058 /* XXX KDM use busdma */
12060 rq->local = vtophys(tmp_ptr);
12063 rq->local = tmp_ptr;
12065 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12066 tmp_ptr += remote_used;
12067 rq->remote = tmp_ptr;
12069 rq->callback = NULL;
12071 local_used += cur_len;
12072 if (local_used >= local_sglist[i].len) {
12077 remote_used += cur_len;
12078 if (remote_used >= remote_sglist[j].len) {
12082 total_used += cur_len;
12084 if (total_used >= io->scsiio.kern_data_len)
12085 rq->callback = callback;
12087 if ((rq->size & 0x7) != 0) {
12088 printf("%s: warning: size %d is not on 8b boundary\n",
12089 __func__, rq->size);
12091 if (((uintptr_t)rq->local & 0x7) != 0) {
12092 printf("%s: warning: local %p not on 8b boundary\n",
12093 __func__, rq->local);
12095 if (((uintptr_t)rq->remote & 0x7) != 0) {
12096 printf("%s: warning: remote %p not on 8b boundary\n",
12097 __func__, rq->local);
12100 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12101 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12102 rq->local, rq->remote, rq->size);
12105 isc_ret = ctl_dt_single(rq);
12106 if (isc_ret == CTL_HA_STATUS_WAIT)
12109 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12110 rq->ret = CTL_HA_STATUS_SUCCESS;
12124 ctl_datamove_remote_read(union ctl_io *io)
12130 * This will send an error to the other controller in the case of a
12133 retval = ctl_datamove_remote_sgl_setup(io);
12137 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12138 ctl_datamove_remote_read_cb);
12140 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12142 * Make sure we free memory if there was an error.. The
12143 * ctl_datamove_remote_xfer() function will send the
12144 * datamove done message, or call the callback with an
12145 * error if there is a problem.
12147 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12148 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12155 * Process a datamove request from the other controller. This is used for
12156 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12157 * first. Once that is complete, the data gets DMAed into the remote
12158 * controller's memory. For reads, we DMA from the remote controller's
12159 * memory into our memory first, and then move it out to the FETD.
12161 * Should be called without the ctl_lock held.
12164 ctl_datamove_remote(union ctl_io *io)
12166 struct ctl_softc *softc;
12168 softc = control_softc;
12171 * Note that we look for an aborted I/O here, but don't do some of
12172 * the other checks that ctl_datamove() normally does. We don't
12173 * need to run the task queue, because this I/O is on the ISC
12174 * queue, which is executed by the work thread after the task queue.
12175 * We don't need to run the datamove delay code, since that should
12176 * have been done if need be on the other controller.
12178 mtx_lock(&softc->ctl_lock);
12180 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12182 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12183 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12184 io->io_hdr.nexus.targ_port,
12185 io->io_hdr.nexus.targ_target.id,
12186 io->io_hdr.nexus.targ_lun);
12187 io->io_hdr.status = CTL_CMD_ABORTED;
12188 io->io_hdr.port_status = 31338;
12190 mtx_unlock(&softc->ctl_lock);
12192 ctl_send_datamove_done(io, /*have_lock*/ 0);
12197 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12198 mtx_unlock(&softc->ctl_lock);
12199 ctl_datamove_remote_write(io);
12200 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12201 mtx_unlock(&softc->ctl_lock);
12202 ctl_datamove_remote_read(io);
12204 union ctl_ha_msg msg;
12205 struct scsi_sense_data *sense;
12209 memset(&msg, 0, sizeof(msg));
12211 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12212 msg.hdr.status = CTL_SCSI_ERROR;
12213 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12215 retry_count = 4243;
12217 sense = &msg.scsi.sense_data;
12218 sks[0] = SSD_SCS_VALID;
12219 sks[1] = (retry_count >> 8) & 0xff;
12220 sks[2] = retry_count & 0xff;
12222 /* "Internal target failure" */
12223 scsi_set_sense_data(sense,
12224 /*sense_format*/ SSD_TYPE_NONE,
12225 /*current_error*/ 1,
12226 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12229 /*type*/ SSD_ELEM_SKS,
12230 /*size*/ sizeof(sks),
12234 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12235 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12236 ctl_failover_io(io, /*have_lock*/ 1);
12237 mtx_unlock(&softc->ctl_lock);
12241 mtx_unlock(&softc->ctl_lock);
12243 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12244 CTL_HA_STATUS_SUCCESS) {
12245 /* XXX KDM what to do if this fails? */
12253 ctl_process_done(union ctl_io *io, int have_lock)
12255 struct ctl_lun *lun;
12256 struct ctl_softc *ctl_softc;
12257 void (*fe_done)(union ctl_io *io);
12258 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12260 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12263 control_softc->ctl_ports[targ_port]->fe_done;
12266 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12271 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12272 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12274 sbuf_cat(&sb, path_str);
12275 switch (io->io_hdr.io_type) {
12277 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12278 sbuf_printf(&sb, "\n");
12279 sbuf_cat(&sb, path_str);
12280 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12281 io->scsiio.tag_num, io->scsiio.tag_type);
12284 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12285 "Tag Type: %d\n", io->taskio.task_action,
12286 io->taskio.tag_num, io->taskio.tag_type);
12289 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12290 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12293 sbuf_cat(&sb, path_str);
12294 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12295 (intmax_t)time_uptime - io->io_hdr.start_time);
12297 printf("%s", sbuf_data(&sb));
12299 #endif /* CTL_TIME_IO */
12301 switch (io->io_hdr.io_type) {
12305 ctl_io_error_print(io, NULL);
12306 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12307 ctl_free_io_internal(io, /*have_lock*/ 0);
12310 return (CTL_RETVAL_COMPLETE);
12313 printf("ctl_process_done: invalid io type %d\n",
12314 io->io_hdr.io_type);
12315 panic("ctl_process_done: invalid io type %d\n",
12316 io->io_hdr.io_type);
12317 break; /* NOTREACHED */
12320 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12322 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12323 io->io_hdr.nexus.targ_lun));
12327 ctl_softc = lun->ctl_softc;
12330 * Remove this from the OOA queue.
12332 if (have_lock == 0)
12333 mtx_lock(&ctl_softc->ctl_lock);
12336 * Check to see if we have any errors to inject here. We only
12337 * inject errors for commands that don't already have errors set.
12339 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12340 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12341 ctl_inject_error(lun, io);
12344 * XXX KDM how do we treat commands that aren't completed
12347 * XXX KDM should we also track I/O latency?
12349 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12350 uint32_t blocksize;
12352 struct bintime cur_bt;
12355 if ((lun->be_lun != NULL)
12356 && (lun->be_lun->blocksize != 0))
12357 blocksize = lun->be_lun->blocksize;
12361 switch (io->io_hdr.io_type) {
12362 case CTL_IO_SCSI: {
12364 struct ctl_lba_len lbalen;
12367 switch (io->scsiio.cdb[0]) {
12378 case WRITE_VERIFY_10:
12379 case WRITE_VERIFY_12:
12380 case WRITE_VERIFY_16:
12381 memcpy(&lbalen, io->io_hdr.ctl_private[
12382 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12385 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12386 lbalen.len * blocksize;
12387 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12391 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12392 &io->io_hdr.dma_bt);
12393 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12394 io->io_hdr.num_dmas;
12395 getbintime(&cur_bt);
12396 bintime_sub(&cur_bt,
12397 &io->io_hdr.start_bt);
12400 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12404 cs_prof_gettime(&cur_ticks);
12405 lun->stats.time[CTL_STATS_READ] +=
12407 io->io_hdr.start_ticks;
12410 lun->stats.time[CTL_STATS_READ] +=
12411 jiffies - io->io_hdr.start_time;
12413 #endif /* CTL_TIME_IO */
12415 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12416 lbalen.len * blocksize;
12417 lun->stats.ports[targ_port].operations[
12418 CTL_STATS_WRITE]++;
12422 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12423 &io->io_hdr.dma_bt);
12424 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12425 io->io_hdr.num_dmas;
12426 getbintime(&cur_bt);
12427 bintime_sub(&cur_bt,
12428 &io->io_hdr.start_bt);
12431 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12434 cs_prof_gettime(&cur_ticks);
12435 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12437 io->io_hdr.start_ticks;
12438 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12439 jiffies - io->io_hdr.start_time;
12441 #endif /* CTL_TIME_IO */
12445 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12449 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12450 &io->io_hdr.dma_bt);
12451 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12452 io->io_hdr.num_dmas;
12453 getbintime(&cur_bt);
12454 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12456 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12460 cs_prof_gettime(&cur_ticks);
12461 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12463 io->io_hdr.start_ticks;
12464 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12465 jiffies - io->io_hdr.start_time;
12467 #endif /* CTL_TIME_IO */
12477 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12480 * Run through the blocked queue on this LUN and see if anything
12481 * has become unblocked, now that this transaction is done.
12483 ctl_check_blocked(lun);
12486 * If the LUN has been invalidated, free it if there is nothing
12487 * left on its OOA queue.
12489 if ((lun->flags & CTL_LUN_INVALID)
12490 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12494 * If this command has been aborted, make sure we set the status
12495 * properly. The FETD is responsible for freeing the I/O and doing
12496 * whatever it needs to do to clean up its state.
12498 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12499 io->io_hdr.status = CTL_CMD_ABORTED;
12502 * We print out status for every task management command. For SCSI
12503 * commands, we filter out any unit attention errors; they happen
12504 * on every boot, and would clutter up the log. Note: task
12505 * management commands aren't printed here, they are printed above,
12506 * since they should never even make it down here.
12508 switch (io->io_hdr.io_type) {
12509 case CTL_IO_SCSI: {
12510 int error_code, sense_key, asc, ascq;
12514 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12515 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12517 * Since this is just for printing, no need to
12518 * show errors here.
12520 scsi_extract_sense_len(&io->scsiio.sense_data,
12521 io->scsiio.sense_len,
12526 /*show_errors*/ 0);
12529 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12530 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12531 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12532 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12534 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12535 ctl_softc->skipped_prints++;
12536 if (have_lock == 0)
12537 mtx_unlock(&ctl_softc->ctl_lock);
12539 uint32_t skipped_prints;
12541 skipped_prints = ctl_softc->skipped_prints;
12543 ctl_softc->skipped_prints = 0;
12544 ctl_softc->last_print_jiffies = time_uptime;
12546 if (have_lock == 0)
12547 mtx_unlock(&ctl_softc->ctl_lock);
12548 if (skipped_prints > 0) {
12550 csevent_log(CSC_CTL | CSC_SHELF_SW |
12552 csevent_LogType_Trace,
12553 csevent_Severity_Information,
12554 csevent_AlertLevel_Green,
12555 csevent_FRU_Firmware,
12556 csevent_FRU_Unknown,
12557 "High CTL error volume, %d prints "
12558 "skipped", skipped_prints);
12561 ctl_io_error_print(io, NULL);
12564 if (have_lock == 0)
12565 mtx_unlock(&ctl_softc->ctl_lock);
12570 if (have_lock == 0)
12571 mtx_unlock(&ctl_softc->ctl_lock);
12572 ctl_io_error_print(io, NULL);
12575 if (have_lock == 0)
12576 mtx_unlock(&ctl_softc->ctl_lock);
12581 * Tell the FETD or the other shelf controller we're done with this
12582 * command. Note that only SCSI commands get to this point. Task
12583 * management commands are completed above.
12585 * We only send status to the other controller if we're in XFER
12586 * mode. In SER_ONLY mode, the I/O is done on the controller that
12587 * received the I/O (from CTL's perspective), and so the status is
12590 * XXX KDM if we hold the lock here, we could cause a deadlock
12591 * if the frontend comes back in in this context to queue
12594 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12595 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12596 union ctl_ha_msg msg;
12598 memset(&msg, 0, sizeof(msg));
12599 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12600 msg.hdr.original_sc = io->io_hdr.original_sc;
12601 msg.hdr.nexus = io->io_hdr.nexus;
12602 msg.hdr.status = io->io_hdr.status;
12603 msg.scsi.scsi_status = io->scsiio.scsi_status;
12604 msg.scsi.tag_num = io->scsiio.tag_num;
12605 msg.scsi.tag_type = io->scsiio.tag_type;
12606 msg.scsi.sense_len = io->scsiio.sense_len;
12607 msg.scsi.sense_residual = io->scsiio.sense_residual;
12608 msg.scsi.residual = io->scsiio.residual;
12609 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12610 sizeof(io->scsiio.sense_data));
12612 * We copy this whether or not this is an I/O-related
12613 * command. Otherwise, we'd have to go and check to see
12614 * whether it's a read/write command, and it really isn't
12617 memcpy(&msg.scsi.lbalen,
12618 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12619 sizeof(msg.scsi.lbalen));
12621 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12622 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12623 /* XXX do something here */
12626 ctl_free_io_internal(io, /*have_lock*/ 0);
12632 return (CTL_RETVAL_COMPLETE);
12636 * Front end should call this if it doesn't do autosense. When the request
12637 * sense comes back in from the initiator, we'll dequeue this and send it.
12640 ctl_queue_sense(union ctl_io *io)
12642 struct ctl_lun *lun;
12643 struct ctl_softc *ctl_softc;
12646 ctl_softc = control_softc;
12648 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12651 * LUN lookup will likely move to the ctl_work_thread() once we
12652 * have our new queueing infrastructure (that doesn't put things on
12653 * a per-LUN queue initially). That is so that we can handle
12654 * things like an INQUIRY to a LUN that we don't have enabled. We
12655 * can't deal with that right now.
12657 mtx_lock(&ctl_softc->ctl_lock);
12660 * If we don't have a LUN for this, just toss the sense
12663 if ((io->io_hdr.nexus.targ_lun < CTL_MAX_LUNS)
12664 && (ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun] != NULL))
12665 lun = ctl_softc->ctl_luns[io->io_hdr.nexus.targ_lun];
12669 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12672 * Already have CA set for this LUN...toss the sense information.
12674 if (ctl_is_set(lun->have_ca, initidx))
12677 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
12678 ctl_min(sizeof(lun->pending_sense[initidx].sense),
12679 sizeof(io->scsiio.sense_data)));
12680 ctl_set_mask(lun->have_ca, initidx);
12683 mtx_unlock(&ctl_softc->ctl_lock);
12687 return (CTL_RETVAL_COMPLETE);
12691 * Primary command inlet from frontend ports. All SCSI and task I/O
12692 * requests must go through this function.
12695 ctl_queue(union ctl_io *io)
12697 struct ctl_softc *ctl_softc;
12699 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12701 ctl_softc = control_softc;
12704 io->io_hdr.start_time = time_uptime;
12705 getbintime(&io->io_hdr.start_bt);
12706 #endif /* CTL_TIME_IO */
12708 mtx_lock(&ctl_softc->ctl_lock);
12710 switch (io->io_hdr.io_type) {
12712 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
12716 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
12718 * Set the task pending flag. This is necessary to close a
12719 * race condition with the FETD:
12721 * - FETD submits a task management command, like an abort.
12722 * - Back end calls fe_datamove() to move the data for the
12723 * aborted command. The FETD can't really accept it, but
12724 * if it did, it would end up transmitting data for a
12725 * command that the initiator told us to abort.
12727 * We close the race condition by setting the flag here,
12728 * and checking it in ctl_datamove(), before calling the
12729 * FETD's fe_datamove routine. If we've got a task
12730 * pending, we run the task queue and then check to see
12731 * whether our particular I/O has been aborted.
12733 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
12736 mtx_unlock(&ctl_softc->ctl_lock);
12737 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12739 break; /* NOTREACHED */
12741 mtx_unlock(&ctl_softc->ctl_lock);
12743 ctl_wakeup_thread();
12745 return (CTL_RETVAL_COMPLETE);
12748 #ifdef CTL_IO_DELAY
12750 ctl_done_timer_wakeup(void *arg)
12754 io = (union ctl_io *)arg;
12755 ctl_done_lock(io, /*have_lock*/ 0);
12757 #endif /* CTL_IO_DELAY */
12760 ctl_done_lock(union ctl_io *io, int have_lock)
12762 struct ctl_softc *ctl_softc;
12763 #ifndef CTL_DONE_THREAD
12765 #endif /* !CTL_DONE_THREAD */
12767 ctl_softc = control_softc;
12769 if (have_lock == 0)
12770 mtx_lock(&ctl_softc->ctl_lock);
12773 * Enable this to catch duplicate completion issues.
12776 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12777 printf("%s: type %d msg %d cdb %x iptl: "
12778 "%d:%d:%d:%d tag 0x%04x "
12779 "flag %#x status %x\n",
12781 io->io_hdr.io_type,
12782 io->io_hdr.msg_type,
12784 io->io_hdr.nexus.initid.id,
12785 io->io_hdr.nexus.targ_port,
12786 io->io_hdr.nexus.targ_target.id,
12787 io->io_hdr.nexus.targ_lun,
12788 (io->io_hdr.io_type ==
12790 io->taskio.tag_num :
12791 io->scsiio.tag_num,
12793 io->io_hdr.status);
12795 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12799 * This is an internal copy of an I/O, and should not go through
12800 * the normal done processing logic.
12802 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
12803 if (have_lock == 0)
12804 mtx_unlock(&ctl_softc->ctl_lock);
12809 * We need to send a msg to the serializing shelf to finish the IO
12810 * as well. We don't send a finish message to the other shelf if
12811 * this is a task management command. Task management commands
12812 * aren't serialized in the OOA queue, but rather just executed on
12813 * both shelf controllers for commands that originated on that
12816 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
12817 && (io->io_hdr.io_type != CTL_IO_TASK)) {
12818 union ctl_ha_msg msg_io;
12820 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
12821 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
12822 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
12823 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
12825 /* continue on to finish IO */
12827 #ifdef CTL_IO_DELAY
12828 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12829 struct ctl_lun *lun;
12831 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12833 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12835 struct ctl_lun *lun;
12837 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12840 && (lun->delay_info.done_delay > 0)) {
12841 struct callout *callout;
12843 callout = (struct callout *)&io->io_hdr.timer_bytes;
12844 callout_init(callout, /*mpsafe*/ 1);
12845 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12846 callout_reset(callout,
12847 lun->delay_info.done_delay * hz,
12848 ctl_done_timer_wakeup, io);
12849 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
12850 lun->delay_info.done_delay = 0;
12851 if (have_lock == 0)
12852 mtx_unlock(&ctl_softc->ctl_lock);
12856 #endif /* CTL_IO_DELAY */
12858 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
12860 #ifdef CTL_DONE_THREAD
12861 if (have_lock == 0)
12862 mtx_unlock(&ctl_softc->ctl_lock);
12864 ctl_wakeup_thread();
12865 #else /* CTL_DONE_THREAD */
12866 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
12868 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
12870 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
12872 ctl_process_done(xio, /*have_lock*/ 1);
12874 if (have_lock == 0)
12875 mtx_unlock(&ctl_softc->ctl_lock);
12876 #endif /* CTL_DONE_THREAD */
12880 ctl_done(union ctl_io *io)
12882 ctl_done_lock(io, /*have_lock*/ 0);
12886 ctl_isc(struct ctl_scsiio *ctsio)
12888 struct ctl_lun *lun;
12891 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12893 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
12895 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
12897 retval = lun->backend->data_submit((union ctl_io *)ctsio);
12904 ctl_work_thread(void *arg)
12906 struct ctl_softc *softc;
12908 struct ctl_be_lun *be_lun;
12911 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
12913 softc = (struct ctl_softc *)arg;
12917 mtx_lock(&softc->ctl_lock);
12922 * We handle the queues in this order:
12923 * - task management
12925 * - done queue (to free up resources, unblock other commands)
12929 * If those queues are empty, we break out of the loop and
12932 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
12934 ctl_run_task_queue(softc);
12937 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
12939 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
12940 ctl_handle_isc(io);
12943 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
12945 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
12946 /* clear any blocked commands, call fe_done */
12947 mtx_unlock(&softc->ctl_lock);
12950 * Call this without a lock for now. This will
12951 * depend on whether there is any way the FETD can
12952 * sleep or deadlock if called with the CTL lock
12955 retval = ctl_process_done(io, /*have_lock*/ 0);
12956 mtx_lock(&softc->ctl_lock);
12959 if (!ctl_pause_rtr) {
12960 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
12962 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
12963 mtx_unlock(&softc->ctl_lock);
12967 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
12969 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
12970 mtx_unlock(&softc->ctl_lock);
12971 ctl_scsiio_precheck(softc, &io->scsiio);
12972 mtx_lock(&softc->ctl_lock);
12976 * We might want to move this to a separate thread, so that
12977 * configuration requests (in this case LUN creations)
12978 * won't impact the I/O path.
12980 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
12981 if (be_lun != NULL) {
12982 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
12983 mtx_unlock(&softc->ctl_lock);
12984 ctl_create_lun(be_lun);
12985 mtx_lock(&softc->ctl_lock);
12989 /* XXX KDM use the PDROP flag?? */
12990 /* Sleep until we have something to do. */
12991 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
12993 /* Back to the top of the loop to see what woke us up. */
12997 retval = ctl_scsiio(&io->scsiio);
12999 case CTL_RETVAL_COMPLETE:
13003 * Probably need to make sure this doesn't happen.
13007 mtx_lock(&softc->ctl_lock);
13012 ctl_wakeup_thread()
13014 struct ctl_softc *softc;
13016 softc = control_softc;
13021 /* Initialization and failover */
13024 ctl_init_isc_msg(void)
13026 printf("CTL: Still calling this thing\n");
13031 * Initializes component into configuration defined by bootMode
13033 * returns hasc_Status:
13035 * ERROR - fatal error
13037 static ctl_ha_comp_status
13038 ctl_isc_init(struct ctl_ha_component *c)
13040 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13047 * Starts component in state requested. If component starts successfully,
13048 * it must set its own state to the requestrd state
13049 * When requested state is HASC_STATE_HA, the component may refine it
13050 * by adding _SLAVE or _MASTER flags.
13051 * Currently allowed state transitions are:
13052 * UNKNOWN->HA - initial startup
13053 * UNKNOWN->SINGLE - initial startup when no parter detected
13054 * HA->SINGLE - failover
13055 * returns ctl_ha_comp_status:
13056 * OK - component successfully started in requested state
13057 * FAILED - could not start the requested state, failover may
13059 * ERROR - fatal error detected, no future startup possible
13061 static ctl_ha_comp_status
13062 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13064 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13066 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13067 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13069 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13070 != CTL_HA_STATUS_SUCCESS) {
13071 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13072 ret = CTL_HA_COMP_STATUS_ERROR;
13074 } else if (CTL_HA_STATE_IS_HA(c->state)
13075 && CTL_HA_STATE_IS_SINGLE(state)){
13076 // HA->SINGLE transition
13080 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13082 ret = CTL_HA_COMP_STATUS_ERROR;
13084 if (CTL_HA_STATE_IS_SINGLE(state))
13093 * Quiesce component
13094 * The component must clear any error conditions (set status to OK) and
13095 * prepare itself to another Start call
13096 * returns ctl_ha_comp_status:
13100 static ctl_ha_comp_status
13101 ctl_isc_quiesce(struct ctl_ha_component *c)
13103 int ret = CTL_HA_COMP_STATUS_OK;
13110 struct ctl_ha_component ctl_ha_component_ctlisc =
13113 .state = CTL_HA_STATE_UNKNOWN,
13114 .init = ctl_isc_init,
13115 .start = ctl_isc_start,
13116 .quiesce = ctl_isc_quiesce