2 * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
3 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
34 * Peripheral driver interface between CAM and CTL (CAM Target Layer).
36 * Author: Ken Merry <ken@FreeBSD.org>
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/malloc.h>
51 #include <sys/endian.h>
53 #include <sys/sysctl.h>
54 #include <sys/types.h>
55 #include <sys/systm.h>
56 #include <sys/taskqueue.h>
57 #include <machine/bus.h>
60 #include <cam/cam_ccb.h>
61 #include <cam/cam_periph.h>
62 #include <cam/cam_queue.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_debug.h>
65 #include <cam/cam_sim.h>
66 #include <cam/cam_xpt.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_message.h>
71 #include <cam/ctl/ctl_io.h>
72 #include <cam/ctl/ctl.h>
73 #include <cam/ctl/ctl_frontend.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_error.h>
80 target_id_t target_id;
84 char port_name[DEV_IDLEN];
85 struct mtx lun_softc_mtx;
86 STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
87 STAILQ_ENTRY(ctlfe_softc) links;
90 STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
91 struct mtx ctlfe_list_mtx;
92 static char ctlfe_mtx_desc[] = "ctlfelist";
95 CTLFE_LUN_NONE = 0x00,
96 CTLFE_LUN_WILDCARD = 0x01
99 struct ctlfe_lun_softc {
100 struct ctlfe_softc *parent_softc;
101 struct cam_periph *periph;
102 ctlfe_lun_flags flags;
103 int ctios_sent; /* Number of active CTIOs */
104 int refcount; /* Number of active xpt_action() */
105 int atios_alloced; /* Number of ATIOs not freed */
106 int inots_alloced; /* Number of INOTs not freed */
107 struct task refdrain_task;
108 TAILQ_HEAD(, ccb_hdr) work_queue;
109 STAILQ_ENTRY(ctlfe_lun_softc) links;
113 CTLFE_CMD_NONE = 0x00,
114 CTLFE_CMD_PIECEWISE = 0x01
117 struct ctlfe_cmd_info {
118 int cur_transfer_index;
119 size_t cur_transfer_off;
120 ctlfe_cmd_flags flags;
122 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
123 * bytes on amd64. So with 32 elements, this is 256 bytes on
124 * i386 and 512 bytes on amd64.
126 #define CTLFE_MAX_SEGS 32
127 bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
131 * When we register the adapter/bus, request that this many ctl_ios be
132 * allocated. This should be the maximum supported by the adapter, but we
133 * currently don't have a way to get that back from the path inquiry.
134 * XXX KDM add that to the path inquiry.
136 #define CTLFE_REQ_CTL_IO 4096
138 * Number of Accept Target I/O CCBs to allocate and queue down to the
140 * XXX KDM should this be controlled by CTL?
142 #define CTLFE_ATIO_PER_LUN 1024
144 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
145 * allocate and queue down to the adapter per LUN.
146 * XXX KDM should this be controlled by CTL?
148 #define CTLFE_IN_PER_LUN 1024
151 * Timeout (in seconds) on CTIO CCB doing DMA or sending status
153 #define CTLFE_TIMEOUT 5
156 * Turn this on to enable extra debugging prints.
162 MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
164 #define io_ptr ppriv_ptr0
166 /* This is only used in the CTIO */
167 #define ccb_atio ppriv_ptr1
169 #define PRIV_CCB(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
170 #define PRIV_INFO(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
172 static int ctlfeinitialize(void);
173 static int ctlfeshutdown(void);
174 static periph_init_t ctlfeperiphinit;
175 static periph_deinit_t ctlfeperiphdeinit;
176 static void ctlfeasync(void *callback_arg, uint32_t code,
177 struct cam_path *path, void *arg);
178 static periph_ctor_t ctlferegister;
179 static periph_oninv_t ctlfeoninvalidate;
180 static periph_dtor_t ctlfecleanup;
181 static periph_start_t ctlfestart;
182 static void ctlfedone(struct cam_periph *periph,
183 union ccb *done_ccb);
185 static void ctlfe_onoffline(void *arg, int online);
186 static void ctlfe_online(void *arg);
187 static void ctlfe_offline(void *arg);
188 static int ctlfe_lun_enable(void *arg, int lun_id);
189 static int ctlfe_lun_disable(void *arg, int lun_id);
190 static void ctlfe_dump_sim(struct cam_sim *sim);
191 static void ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
192 static void ctlfe_datamove(union ctl_io *io);
193 static void ctlfe_done(union ctl_io *io);
194 static void ctlfe_dump(void);
195 static void ctlfe_free_ccb(struct cam_periph *periph,
197 static void ctlfe_requeue_ccb(struct cam_periph *periph,
198 union ccb *ccb, int unlock);
200 static struct periph_driver ctlfe_driver =
202 ctlfeperiphinit, "ctl",
203 TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
204 CAM_PERIPH_DRV_EARLY,
208 static struct ctl_frontend ctlfe_frontend =
211 .init = ctlfeinitialize,
212 .fe_dump = ctlfe_dump,
213 .shutdown = ctlfeshutdown,
215 CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
218 ctlfeinitialize(void)
221 STAILQ_INIT(&ctlfe_softc_list);
222 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
223 periphdriver_register(&ctlfe_driver);
232 error = periphdriver_unregister(&ctlfe_driver);
235 mtx_destroy(&ctlfe_list_mtx);
240 ctlfeperiphinit(void)
244 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
245 AC_CONTRACT, ctlfeasync, NULL, NULL);
246 if (status != CAM_REQ_CMP) {
247 printf("ctl: Failed to attach async callback due to CAM "
248 "status 0x%x!\n", status);
253 ctlfeperiphdeinit(void)
256 /* XXX: It would be good to tear down active ports here. */
257 if (!TAILQ_EMPTY(&ctlfe_driver.units))
259 xpt_register_async(0, ctlfeasync, NULL, NULL);
264 ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
266 struct ctlfe_softc *softc;
269 printf("%s: entered\n", __func__);
272 mtx_lock(&ctlfe_list_mtx);
273 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
274 if (softc->path_id == xpt_path_path_id(path))
277 mtx_unlock(&ctlfe_list_mtx);
280 * When a new path gets registered, and it is capable of target
281 * mode, go ahead and attach. Later on, we may need to be more
282 * selective, but for now this will be sufficient.
285 case AC_PATH_REGISTERED: {
286 struct ctl_port *port;
287 struct ccb_pathinq *cpi;
290 cpi = (struct ccb_pathinq *)arg;
292 /* Don't attach if it doesn't support target mode */
293 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
295 printf("%s: SIM %s%d doesn't support target mode\n",
296 __func__, cpi->dev_name, cpi->unit_number);
303 printf("%s: CTL port for CAM path %u already exists\n",
304 __func__, xpt_path_path_id(path));
310 * We're in an interrupt context here, so we have to
311 * use M_NOWAIT. Of course this means trouble if we
312 * can't allocate memory.
314 softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
316 printf("%s: unable to malloc %zd bytes for softc\n",
317 __func__, sizeof(*softc));
321 softc->path_id = cpi->ccb_h.path_id;
322 softc->target_id = cpi->initiator_id;
323 softc->sim = xpt_path_sim(path);
324 softc->hba_misc = cpi->hba_misc;
326 softc->maxio = cpi->maxio;
328 softc->maxio = DFLTPHYS;
329 mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
330 STAILQ_INIT(&softc->lun_softc_list);
333 port->frontend = &ctlfe_frontend;
336 * XXX KDM should we be more accurate here ?
338 if (cpi->transport == XPORT_FC)
339 port->port_type = CTL_PORT_FC;
340 else if (cpi->transport == XPORT_SAS)
341 port->port_type = CTL_PORT_SAS;
343 port->port_type = CTL_PORT_SCSI;
345 /* XXX KDM what should the real number be here? */
346 port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
347 snprintf(softc->port_name, sizeof(softc->port_name),
348 "%s%d", cpi->dev_name, cpi->unit_number);
350 * XXX KDM it would be nice to allocate storage in the
351 * frontend structure itself.
353 port->port_name = softc->port_name;
354 port->physical_port = cpi->bus_id;
355 port->virtual_port = 0;
356 port->port_online = ctlfe_online;
357 port->port_offline = ctlfe_offline;
358 port->onoff_arg = softc;
359 port->lun_enable = ctlfe_lun_enable;
360 port->lun_disable = ctlfe_lun_disable;
361 port->targ_lun_arg = softc;
362 port->fe_datamove = ctlfe_datamove;
363 port->fe_done = ctlfe_done;
364 port->targ_port = -1;
366 retval = ctl_port_register(port);
368 printf("%s: ctl_port_register() failed with "
369 "error %d!\n", __func__, retval);
370 mtx_destroy(&softc->lun_softc_mtx);
371 free(softc, M_CTLFE);
374 mtx_lock(&ctlfe_list_mtx);
375 STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
376 mtx_unlock(&ctlfe_list_mtx);
381 case AC_PATH_DEREGISTERED: {
385 * XXX KDM are we certain at this point that there
386 * are no outstanding commands for this frontend?
388 mtx_lock(&ctlfe_list_mtx);
389 STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
391 mtx_unlock(&ctlfe_list_mtx);
392 ctl_port_deregister(&softc->port);
393 mtx_destroy(&softc->lun_softc_mtx);
394 free(softc, M_CTLFE);
399 struct ac_contract *ac;
401 ac = (struct ac_contract *)arg;
403 switch (ac->contract_number) {
404 case AC_CONTRACT_DEV_CHG: {
405 struct ac_device_changed *dev_chg;
408 dev_chg = (struct ac_device_changed *)ac->contract_data;
410 printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
411 __func__, dev_chg->wwpn, dev_chg->port,
412 xpt_path_path_id(path), dev_chg->target,
413 (dev_chg->arrived == 0) ? "left" : "arrived");
416 printf("%s: CTL port for CAM path %u not "
417 "found!\n", __func__,
418 xpt_path_path_id(path));
421 if (dev_chg->arrived != 0) {
422 retval = ctl_add_initiator(&softc->port,
423 dev_chg->target, dev_chg->wwpn, NULL);
425 retval = ctl_remove_initiator(&softc->port,
430 printf("%s: could not %s port %d iid %u "
431 "WWPN %#jx!\n", __func__,
432 (dev_chg->arrived != 0) ? "add" :
433 "remove", softc->port.targ_port,
435 (uintmax_t)dev_chg->wwpn);
440 printf("%s: unsupported contract number %ju\n",
441 __func__, (uintmax_t)ac->contract_number);
452 ctlferegister(struct cam_periph *periph, void *arg)
454 struct ctlfe_softc *bus_softc;
455 struct ctlfe_lun_softc *softc;
456 union ccb en_lun_ccb;
460 softc = (struct ctlfe_lun_softc *)arg;
461 bus_softc = softc->parent_softc;
463 TAILQ_INIT(&softc->work_queue);
464 softc->periph = periph;
465 periph->softc = softc;
467 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
468 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
469 en_lun_ccb.cel.grp6_len = 0;
470 en_lun_ccb.cel.grp7_len = 0;
471 en_lun_ccb.cel.enable = 1;
472 xpt_action(&en_lun_ccb);
473 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
474 if (status != CAM_REQ_CMP) {
475 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
476 __func__, en_lun_ccb.ccb_h.status);
480 status = CAM_REQ_CMP;
482 for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
484 union ctl_io *new_io;
485 struct ctlfe_cmd_info *cmd_info;
487 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
489 if (new_ccb == NULL) {
490 status = CAM_RESRC_UNAVAIL;
493 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
494 if (new_io == NULL) {
495 free(new_ccb, M_CTLFE);
496 status = CAM_RESRC_UNAVAIL;
499 cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
501 if (cmd_info == NULL) {
503 free(new_ccb, M_CTLFE);
504 status = CAM_RESRC_UNAVAIL;
507 PRIV_INFO(new_io) = cmd_info;
508 softc->atios_alloced++;
509 new_ccb->ccb_h.io_ptr = new_io;
511 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
512 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
513 new_ccb->ccb_h.cbfcnp = ctlfedone;
514 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
516 status = new_ccb->ccb_h.status;
517 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
518 free(cmd_info, M_CTLFE);
520 free(new_ccb, M_CTLFE);
525 status = cam_periph_acquire(periph);
526 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
527 xpt_print(periph->path, "%s: could not acquire reference "
528 "count, status = %#x\n", __func__, status);
533 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
534 "status 0x%x\n", __func__, status);
535 return (CAM_REQ_CMP_ERR);
538 for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
540 union ctl_io *new_io;
542 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
544 if (new_ccb == NULL) {
545 status = CAM_RESRC_UNAVAIL;
548 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
549 if (new_io == NULL) {
550 free(new_ccb, M_CTLFE);
551 status = CAM_RESRC_UNAVAIL;
554 softc->inots_alloced++;
555 new_ccb->ccb_h.io_ptr = new_io;
557 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
558 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
559 new_ccb->ccb_h.cbfcnp = ctlfedone;
560 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
562 status = new_ccb->ccb_h.status;
563 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
565 * Note that we don't free the CCB here. If the
566 * status is not CAM_REQ_INPROG, then we're
567 * probably talking to a SIM that says it is
568 * target-capable but doesn't support the
569 * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the
570 * older API. In that case, it'll call xpt_done()
571 * on the CCB, and we need to free it in our done
572 * routine as a result.
578 || (status != CAM_REQ_INPROG)) {
579 xpt_print(periph->path, "%s: could not allocate immediate "
580 "notify CCBs, status 0x%x\n", __func__, status);
581 return (CAM_REQ_CMP_ERR);
583 mtx_lock(&bus_softc->lun_softc_mtx);
584 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
585 mtx_unlock(&bus_softc->lun_softc_mtx);
586 return (CAM_REQ_CMP);
590 ctlfeoninvalidate(struct cam_periph *periph)
592 union ccb en_lun_ccb;
594 struct ctlfe_softc *bus_softc;
595 struct ctlfe_lun_softc *softc;
597 softc = (struct ctlfe_lun_softc *)periph->softc;
599 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
600 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
601 en_lun_ccb.cel.grp6_len = 0;
602 en_lun_ccb.cel.grp7_len = 0;
603 en_lun_ccb.cel.enable = 0;
604 xpt_action(&en_lun_ccb);
605 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
606 if (status != CAM_REQ_CMP) {
607 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
608 __func__, en_lun_ccb.ccb_h.status);
610 * XXX KDM what do we do now?
614 bus_softc = softc->parent_softc;
615 mtx_lock(&bus_softc->lun_softc_mtx);
616 STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
617 mtx_unlock(&bus_softc->lun_softc_mtx);
621 ctlfecleanup(struct cam_periph *periph)
623 struct ctlfe_lun_softc *softc;
625 softc = (struct ctlfe_lun_softc *)periph->softc;
627 KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
628 __func__, softc->ctios_sent));
629 KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
630 __func__, softc->refcount));
631 KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
632 __func__, softc->atios_alloced));
633 KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
634 __func__, softc->inots_alloced));
636 free(softc, M_CTLFE);
640 ctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
641 ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
642 u_int16_t *sglist_cnt)
644 struct ctlfe_softc *bus_softc;
645 struct ctlfe_cmd_info *cmd_info;
646 struct ctl_sg_entry *ctl_sglist;
647 bus_dma_segment_t *cam_sglist;
651 cmd_info = PRIV_INFO(io);
652 bus_softc = softc->parent_softc;
655 * Set the direction, relative to the initiator.
657 *flags &= ~CAM_DIR_MASK;
658 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
659 *flags |= CAM_DIR_IN;
661 *flags |= CAM_DIR_OUT;
663 *flags &= ~CAM_DATA_MASK;
664 idx = cmd_info->cur_transfer_index;
665 off = cmd_info->cur_transfer_off;
666 cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
667 if (io->scsiio.kern_sg_entries == 0) { /* No S/G list. */
669 /* One time shift for SRR offset. */
670 off += io->scsiio.ext_data_filled;
671 io->scsiio.ext_data_filled = 0;
673 *data_ptr = io->scsiio.kern_data_ptr + off;
674 if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
675 *dxfer_len = io->scsiio.kern_data_len - off;
677 *dxfer_len = bus_softc->maxio;
678 cmd_info->cur_transfer_off += bus_softc->maxio;
679 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
683 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
684 *flags |= CAM_DATA_PADDR;
686 *flags |= CAM_DATA_VADDR;
687 } else { /* S/G list with physical or virtual pointers. */
688 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
690 /* One time shift for SRR offset. */
691 while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
692 io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
696 off += io->scsiio.ext_data_filled;
697 io->scsiio.ext_data_filled = 0;
699 cam_sglist = cmd_info->cam_sglist;
701 for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
702 cam_sglist[i].ds_addr = (bus_addr_t)ctl_sglist[i + idx].addr + off;
703 if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
704 cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
705 *dxfer_len += cam_sglist[i].ds_len;
707 cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
708 cmd_info->cur_transfer_index = idx + i;
709 cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
710 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
711 *dxfer_len += cam_sglist[i].ds_len;
712 if (ctl_sglist[i].len != 0)
716 if (i == (CTLFE_MAX_SEGS - 1) &&
717 idx + i < (io->scsiio.kern_sg_entries - 1)) {
718 cmd_info->cur_transfer_index = idx + i + 1;
719 cmd_info->cur_transfer_off = 0;
720 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
727 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
728 *flags |= CAM_DATA_SG_PADDR;
730 *flags |= CAM_DATA_SG;
731 *data_ptr = (uint8_t *)cam_sglist;
736 ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
738 struct ctlfe_lun_softc *softc;
739 struct ctlfe_cmd_info *cmd_info;
740 struct ccb_hdr *ccb_h;
741 struct ccb_accept_tio *atio;
742 struct ccb_scsiio *csio;
749 softc = (struct ctlfe_lun_softc *)periph->softc;
752 ccb_h = TAILQ_FIRST(&softc->work_queue);
754 xpt_release_ccb(start_ccb);
758 /* Take the ATIO off the work queue */
759 TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
760 atio = (struct ccb_accept_tio *)ccb_h;
761 io = (union ctl_io *)ccb_h->io_ptr;
762 csio = &start_ccb->csio;
764 flags = atio->ccb_h.flags &
765 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
766 cmd_info = PRIV_INFO(io);
767 cmd_info->cur_transfer_index = 0;
768 cmd_info->cur_transfer_off = 0;
771 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
773 * Datamove call, we need to setup the S/G list.
775 ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
779 * We're done, send status back.
781 if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
782 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
783 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
785 /* Tell the SIM that we've aborted this ATIO */
787 printf("%s: tag %04x abort\n", __func__, atio->tag_id);
789 KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
790 ("func_code %#x is not ATIO", atio->ccb_h.func_code));
791 start_ccb->ccb_h.func_code = XPT_ABORT;
792 start_ccb->cab.abort_ccb = (union ccb *)atio;
793 xpt_action(start_ccb);
795 ctlfe_requeue_ccb(periph, (union ccb *)atio,
798 /* XPT_ABORT is not queued, so we can take next I/O. */
803 csio->sglist_cnt = 0;
806 if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
807 (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
808 ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
809 io->io_hdr.status == CTL_SUCCESS)) {
810 flags |= CAM_SEND_STATUS;
811 scsi_status = io->scsiio.scsi_status;
812 csio->sense_len = io->scsiio.sense_len;
814 printf("%s: tag %04x status %x\n", __func__,
815 atio->tag_id, io->io_hdr.status);
817 if (csio->sense_len != 0) {
818 csio->sense_data = io->scsiio.sense_data;
819 flags |= CAM_SEND_SENSE;
824 printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
825 (flags & CAM_SEND_STATUS) ? "done" : "datamove",
826 atio->tag_id, flags, data_ptr, dxfer_len);
830 * Valid combinations:
831 * - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
833 * - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
835 * - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
839 if (((flags & CAM_SEND_STATUS)
840 && (((flags & CAM_DATA_SG) != 0)
842 || (csio->sglist_cnt != 0)))
843 || (((flags & CAM_SEND_STATUS) == 0)
845 || ((flags & CAM_DATA_SG)
846 && (csio->sglist_cnt == 0))
847 || (((flags & CAM_DATA_SG) == 0)
848 && (csio->sglist_cnt != 0))) {
849 printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
850 "%d sg %u\n", __func__, atio->tag_id,
851 atio_cdb_ptr(atio)[0], flags, dxfer_len,
853 printf("%s: tag %04x io status %#x\n", __func__,
854 atio->tag_id, io->io_hdr.status);
861 (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
865 /*data_ptr*/ data_ptr,
866 /*dxfer_len*/ dxfer_len,
867 /*timeout*/ CTLFE_TIMEOUT * 1000);
868 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
869 start_ccb->ccb_h.ccb_atio = atio;
870 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
871 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
872 io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
876 cam_periph_unlock(periph);
877 xpt_action(start_ccb);
878 cam_periph_lock(periph);
882 * If we still have work to do, ask for another CCB.
884 if (!TAILQ_EMPTY(&softc->work_queue))
885 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
889 ctlfe_drain(void *context, int pending)
891 struct cam_periph *periph = context;
892 struct ctlfe_lun_softc *softc = periph->softc;
894 cam_periph_lock(periph);
895 while (softc->refcount != 0) {
896 cam_periph_sleep(periph, &softc->refcount, PRIBIO,
899 cam_periph_unlock(periph);
900 cam_periph_release(periph);
904 ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
906 struct ctlfe_lun_softc *softc;
908 struct ctlfe_cmd_info *cmd_info;
910 softc = (struct ctlfe_lun_softc *)periph->softc;
911 io = ccb->ccb_h.io_ptr;
913 switch (ccb->ccb_h.func_code) {
914 case XPT_ACCEPT_TARGET_IO:
915 softc->atios_alloced--;
916 cmd_info = PRIV_INFO(io);
917 free(cmd_info, M_CTLFE);
919 case XPT_IMMEDIATE_NOTIFY:
920 case XPT_NOTIFY_ACKNOWLEDGE:
921 softc->inots_alloced--;
930 KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
931 __func__, softc->atios_alloced));
932 KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
933 __func__, softc->inots_alloced));
936 * If we have received all of our CCBs, we can release our
937 * reference on the peripheral driver. It will probably go away
940 if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
941 if (softc->refcount == 0) {
942 cam_periph_release_locked(periph);
944 TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
945 taskqueue_enqueue(taskqueue_thread,
946 &softc->refdrain_task);
952 * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
955 ctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
957 struct ctlfe_lun_softc *softc;
960 if (periph->flags & CAM_PERIPH_INVALID) {
961 mtx = cam_periph_mtx(periph);
962 ctlfe_free_ccb(periph, ccb);
968 cam_periph_unlock(periph);
971 * For a wildcard attachment, commands can come in with a specific
972 * target/lun. Reset the target and LUN fields back to the wildcard
973 * values before we send them back down to the SIM.
975 softc = (struct ctlfe_lun_softc *)periph->softc;
976 if (softc->flags & CTLFE_LUN_WILDCARD) {
977 ccb->ccb_h.target_id = CAM_TARGET_WILDCARD;
978 ccb->ccb_h.target_lun = CAM_LUN_WILDCARD;
985 ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
988 uint32_t num_blocks, nbc;
989 uint8_t *cmdbyt = atio_cdb_ptr(atio);
991 nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */
997 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
998 lba = scsi_3btoul(cdb->addr);
1000 num_blocks = cdb->length;
1001 if (num_blocks == 0)
1005 scsi_ulto3b(lba, cdb->addr);
1006 cdb->length = num_blocks;
1012 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1013 lba = scsi_4btoul(cdb->addr);
1014 num_blocks = scsi_2btoul(cdb->length);
1017 scsi_ulto4b(lba, cdb->addr);
1018 scsi_ulto2b(num_blocks, cdb->length);
1024 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1025 lba = scsi_4btoul(cdb->addr);
1026 num_blocks = scsi_4btoul(cdb->length);
1029 scsi_ulto4b(lba, cdb->addr);
1030 scsi_ulto4b(num_blocks, cdb->length);
1036 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1037 lba = scsi_8btou64(cdb->addr);
1038 num_blocks = scsi_4btoul(cdb->length);
1041 scsi_u64to8b(lba, cdb->addr);
1042 scsi_ulto4b(num_blocks, cdb->length);
1052 ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1054 struct ctlfe_lun_softc *softc;
1055 struct ctlfe_softc *bus_softc;
1056 struct ctlfe_cmd_info *cmd_info;
1057 struct ccb_accept_tio *atio = NULL;
1058 union ctl_io *io = NULL;
1062 KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1063 ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1065 printf("%s: entered, func_code = %#x\n", __func__,
1066 done_ccb->ccb_h.func_code);
1070 * At this point CTL has no known use case for device queue freezes.
1071 * In case some SIM think different -- drop its freeze right here.
1073 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1074 cam_release_devq(periph->path,
1078 /*getcount_only*/0);
1079 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1082 softc = (struct ctlfe_lun_softc *)periph->softc;
1083 bus_softc = softc->parent_softc;
1084 mtx = cam_periph_mtx(periph);
1087 switch (done_ccb->ccb_h.func_code) {
1088 case XPT_ACCEPT_TARGET_IO: {
1090 atio = &done_ccb->atio;
1091 status = atio->ccb_h.status & CAM_STATUS_MASK;
1092 if (status != CAM_CDB_RECVD) {
1093 ctlfe_free_ccb(periph, done_ccb);
1099 * Allocate a ctl_io, pass it to CTL, and wait for the
1103 io = done_ccb->ccb_h.io_ptr;
1104 cmd_info = PRIV_INFO(io);
1107 /* Save pointers on both sides */
1108 PRIV_CCB(io) = done_ccb;
1109 PRIV_INFO(io) = cmd_info;
1110 done_ccb->ccb_h.io_ptr = io;
1113 * Only SCSI I/O comes down this path, resets, etc. come
1114 * down the immediate notify path below.
1116 io->io_hdr.io_type = CTL_IO_SCSI;
1117 io->io_hdr.nexus.initid = atio->init_id;
1118 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1119 if (bus_softc->hba_misc & PIM_EXTLUNS) {
1120 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1121 CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1123 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1125 io->scsiio.tag_num = atio->tag_id;
1126 switch (atio->tag_action) {
1127 case CAM_TAG_ACTION_NONE:
1128 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1130 case MSG_SIMPLE_TASK:
1131 io->scsiio.tag_type = CTL_TAG_SIMPLE;
1133 case MSG_HEAD_OF_QUEUE_TASK:
1134 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1136 case MSG_ORDERED_TASK:
1137 io->scsiio.tag_type = CTL_TAG_ORDERED;
1140 io->scsiio.tag_type = CTL_TAG_ACA;
1143 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1144 printf("%s: unhandled tag type %#x!!\n", __func__,
1148 if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1149 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1150 __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1152 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1153 bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1156 printf("%s: %u:%u:%u: tag %04x CDB %02x\n", __func__,
1157 io->io_hdr.nexus.initid,
1158 io->io_hdr.nexus.targ_port,
1159 io->io_hdr.nexus.targ_lun,
1160 io->scsiio.tag_num, io->scsiio.cdb[0]);
1166 case XPT_CONT_TARGET_IO: {
1168 uint32_t srr_off = 0;
1170 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1171 io = (union ctl_io *)atio->ccb_h.io_ptr;
1173 softc->ctios_sent--;
1175 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1176 __func__, atio->tag_id, done_ccb->ccb_h.flags);
1179 * Handle SRR case were the data pointer is pushed back hack
1181 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1182 && done_ccb->csio.msg_ptr != NULL
1183 && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1184 && done_ccb->csio.msg_ptr[1] == 5
1185 && done_ccb->csio.msg_ptr[2] == 0) {
1188 (done_ccb->csio.msg_ptr[3] << 24)
1189 | (done_ccb->csio.msg_ptr[4] << 16)
1190 | (done_ccb->csio.msg_ptr[5] << 8)
1191 | (done_ccb->csio.msg_ptr[6]);
1195 * If we have an SRR and we're still sending data, we
1196 * should be able to adjust offsets and cycle again.
1197 * It is possible only if offset is from this datamove.
1199 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1200 srr_off >= io->scsiio.kern_rel_offset &&
1201 srr_off < io->scsiio.kern_rel_offset +
1202 io->scsiio.kern_data_len) {
1203 io->scsiio.kern_data_resid =
1204 io->scsiio.kern_rel_offset +
1205 io->scsiio.kern_data_len - srr_off;
1206 io->scsiio.ext_data_filled = srr_off;
1207 io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1208 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1209 xpt_release_ccb(done_ccb);
1210 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1212 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1217 * If status was being sent, the back end data is now history.
1218 * Hack it up and resubmit a new command with the CDB adjusted.
1219 * If the SIM does the right thing, all of the resid math
1222 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1223 xpt_release_ccb(done_ccb);
1224 if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1225 done_ccb = (union ccb *)atio;
1229 * Fall through to doom....
1233 if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1234 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1235 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1238 * If we were sending status back to the initiator, free up
1239 * resources. If we were doing a datamove, call the
1240 * datamove done routine.
1242 if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1244 * If we asked to send sense data but it wasn't sent,
1245 * queue the I/O back to CTL for later REQUEST SENSE.
1247 if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1248 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1249 (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1250 (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1251 PRIV_INFO(io) = PRIV_INFO(
1252 (union ctl_io *)atio->ccb_h.io_ptr);
1253 ctl_queue_sense(atio->ccb_h.io_ptr);
1254 atio->ccb_h.io_ptr = io;
1257 /* Abort ATIO if CTIO sending status has failed. */
1258 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1260 done_ccb->ccb_h.func_code = XPT_ABORT;
1261 done_ccb->cab.abort_ccb = (union ccb *)atio;
1262 xpt_action(done_ccb);
1265 xpt_release_ccb(done_ccb);
1266 ctlfe_requeue_ccb(periph, (union ccb *)atio,
1270 struct ctlfe_cmd_info *cmd_info;
1271 struct ccb_scsiio *csio;
1273 csio = &done_ccb->csio;
1274 cmd_info = PRIV_INFO(io);
1276 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1279 * Translate CAM status to CTL status. Success
1280 * does not change the overall, ctl_io status. In
1281 * that case we just set port_status to 0. If we
1282 * have a failure, though, set a data phase error
1283 * for the overall ctl_io.
1285 switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1287 io->scsiio.kern_data_resid -=
1288 csio->dxfer_len - csio->resid;
1289 io->io_hdr.port_status = 0;
1293 * XXX KDM we probably need to figure out a
1294 * standard set of errors that the SIM
1295 * drivers should return in the event of a
1296 * data transfer failure. A data phase
1297 * error will at least point the user to a
1298 * data transfer error of some sort.
1299 * Hopefully the SIM printed out some
1300 * additional information to give the user
1301 * a clue what happened.
1303 io->io_hdr.port_status = 0xbad1;
1304 ctl_set_data_phase_error(&io->scsiio);
1306 * XXX KDM figure out residual.
1311 * If we had to break this S/G list into multiple
1312 * pieces, figure out where we are in the list, and
1313 * continue sending pieces if necessary.
1315 if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1316 io->io_hdr.port_status == 0 && csio->resid == 0) {
1321 flags = atio->ccb_h.flags &
1322 (CAM_DIS_DISCONNECT|
1323 CAM_TAG_ACTION_VALID);
1325 ctlfedata(softc, io, &flags, &data_ptr,
1326 &dxfer_len, &csio->sglist_cnt);
1328 if (((flags & CAM_SEND_STATUS) == 0)
1329 && (dxfer_len == 0)) {
1330 printf("%s: tag %04x no status or "
1331 "len cdb = %02x\n", __func__,
1333 atio_cdb_ptr(atio)[0]);
1334 printf("%s: tag %04x io status %#x\n",
1335 __func__, atio->tag_id,
1343 (flags & CAM_TAG_ACTION_VALID) ?
1344 MSG_SIMPLE_Q_TAG : 0,
1348 /*data_ptr*/ data_ptr,
1349 /*dxfer_len*/ dxfer_len,
1350 CTLFE_TIMEOUT * 1000);
1352 csio->ccb_h.flags |= CAM_UNLOCKED;
1354 csio->ccb_h.ccb_atio = atio;
1355 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1356 softc->ctios_sent++;
1358 xpt_action((union ccb *)csio);
1361 * Release the CTIO. The ATIO will be sent back
1362 * down to the SIM once we send status.
1364 xpt_release_ccb(done_ccb);
1367 /* Call the backend move done callback */
1368 io->scsiio.be_move_done(io);
1374 case XPT_IMMEDIATE_NOTIFY: {
1376 struct ccb_immediate_notify *inot;
1379 inot = &done_ccb->cin1;
1380 io = done_ccb->ccb_h.io_ptr;
1385 io->io_hdr.io_type = CTL_IO_TASK;
1386 PRIV_CCB(io) = done_ccb;
1387 inot->ccb_h.io_ptr = io;
1388 io->io_hdr.nexus.initid = inot->initiator_id;
1389 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1390 if (bus_softc->hba_misc & PIM_EXTLUNS) {
1391 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1392 CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1394 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1396 /* XXX KDM should this be the tag_id? */
1397 io->taskio.tag_num = inot->seq_id;
1399 status = inot->ccb_h.status & CAM_STATUS_MASK;
1401 case CAM_SCSI_BUS_RESET:
1402 io->taskio.task_action = CTL_TASK_BUS_RESET;
1405 io->taskio.task_action = CTL_TASK_TARGET_RESET;
1407 case CAM_MESSAGE_RECV:
1408 switch (inot->arg) {
1409 case MSG_ABORT_TASK_SET:
1410 io->taskio.task_action =
1411 CTL_TASK_ABORT_TASK_SET;
1413 case MSG_TARGET_RESET:
1414 io->taskio.task_action = CTL_TASK_TARGET_RESET;
1416 case MSG_ABORT_TASK:
1417 io->taskio.task_action = CTL_TASK_ABORT_TASK;
1419 case MSG_LOGICAL_UNIT_RESET:
1420 io->taskio.task_action = CTL_TASK_LUN_RESET;
1422 case MSG_CLEAR_TASK_SET:
1423 io->taskio.task_action =
1424 CTL_TASK_CLEAR_TASK_SET;
1427 io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1429 case MSG_QUERY_TASK:
1430 io->taskio.task_action = CTL_TASK_QUERY_TASK;
1432 case MSG_QUERY_TASK_SET:
1433 io->taskio.task_action =
1434 CTL_TASK_QUERY_TASK_SET;
1436 case MSG_QUERY_ASYNC_EVENT:
1437 io->taskio.task_action =
1438 CTL_TASK_QUERY_ASYNC_EVENT;
1444 xpt_print(periph->path,
1445 "%s: unsupported INOT message 0x%x\n",
1446 __func__, inot->arg);
1452 xpt_print(periph->path,
1453 "%s: unsupported INOT status 0x%x\n",
1456 case CAM_REQ_ABORTED:
1457 case CAM_REQ_INVALID:
1458 case CAM_DEV_NOT_THERE:
1459 case CAM_PROVIDE_FAIL:
1460 ctlfe_free_ccb(periph, done_ccb);
1463 if (send_ctl_io != 0) {
1466 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1467 done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1468 xpt_action(done_ccb);
1472 case XPT_NOTIFY_ACKNOWLEDGE:
1473 /* Queue this back down to the SIM as an immediate notify. */
1474 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1475 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1476 ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1478 case XPT_SET_SIM_KNOB:
1479 case XPT_GET_SIM_KNOB:
1480 case XPT_GET_SIM_KNOB_OLD:
1483 panic("%s: unexpected CCB type %#x", __func__,
1484 done_ccb->ccb_h.func_code);
1493 ctlfe_onoffline(void *arg, int online)
1495 struct ctlfe_softc *bus_softc;
1498 struct cam_path *path;
1501 bus_softc = (struct ctlfe_softc *)arg;
1505 status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1506 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1507 if (status != CAM_REQ_CMP) {
1508 printf("%s: unable to create path!\n", __func__);
1511 ccb = xpt_alloc_ccb();
1512 xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1513 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1519 * Bits 63-60: 0x5 NAA, IEEE registered name
1520 * Bits 59-36: 0x000ED5 IEEE Company name assigned to Copan
1521 * Bits 35-12: Copan SSN (Sequential Serial Number)
1522 * Bits 11-8: Type of port:
1526 * Bits 7-0: 0 == Node Name, >0 == Port Number
1529 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1531 printf("%s: %s current WWNN %#jx\n", __func__,
1532 bus_softc->port_name,
1533 ccb->knob.xport_specific.fc.wwnn);
1534 printf("%s: %s current WWPN %#jx\n", __func__,
1535 bus_softc->port_name,
1536 ccb->knob.xport_specific.fc.wwpn);
1539 * If the user has specified a WWNN/WWPN, send them
1540 * down to the SIM. Otherwise, record what the SIM
1543 if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1544 != ccb->knob.xport_specific.fc.wwnn) {
1545 ccb->knob.xport_specific.fc.wwnn =
1546 bus_softc->port.wwnn;
1549 ctl_port_set_wwns(&bus_softc->port,
1550 true, ccb->knob.xport_specific.fc.wwnn,
1553 if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1554 != ccb->knob.xport_specific.fc.wwpn) {
1555 ccb->knob.xport_specific.fc.wwpn =
1556 bus_softc->port.wwpn;
1559 ctl_port_set_wwns(&bus_softc->port,
1561 true, ccb->knob.xport_specific.fc.wwpn);
1565 if (set_wwnn != 0) {
1566 printf("%s: %s new WWNN %#jx\n", __func__,
1567 bus_softc->port_name,
1568 ccb->knob.xport_specific.fc.wwnn);
1569 printf("%s: %s new WWPN %#jx\n", __func__,
1570 bus_softc->port_name,
1571 ccb->knob.xport_specific.fc.wwpn);
1574 printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1575 bus_softc->port_name);
1578 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1579 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1581 ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1584 ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1586 ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1590 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1591 printf("%s: SIM %s (path id %d) target %s failed with "
1593 __func__, bus_softc->port_name, bus_softc->path_id,
1594 (online != 0) ? "enable" : "disable",
1597 printf("%s: SIM %s (path id %d) target %s succeeded\n",
1598 __func__, bus_softc->port_name, bus_softc->path_id,
1599 (online != 0) ? "enable" : "disable");
1602 xpt_free_path(path);
1607 ctlfe_online(void *arg)
1609 struct ctlfe_softc *bus_softc;
1610 struct cam_path *path;
1612 struct ctlfe_lun_softc *lun_softc;
1613 struct cam_periph *periph;
1615 bus_softc = (struct ctlfe_softc *)arg;
1618 * Create the wildcard LUN before bringing the port online.
1620 status = xpt_create_path(&path, /*periph*/ NULL,
1621 bus_softc->path_id, CAM_TARGET_WILDCARD,
1623 if (status != CAM_REQ_CMP) {
1624 printf("%s: unable to create path for wildcard periph\n",
1629 lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1631 xpt_path_lock(path);
1632 periph = cam_periph_find(path, "ctl");
1633 if (periph != NULL) {
1634 /* We've already got a periph, no need to alloc a new one. */
1635 xpt_path_unlock(path);
1636 xpt_free_path(path);
1637 free(lun_softc, M_CTLFE);
1640 lun_softc->parent_softc = bus_softc;
1641 lun_softc->flags |= CTLFE_LUN_WILDCARD;
1643 status = cam_periph_alloc(ctlferegister,
1654 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1655 const struct cam_status_entry *entry;
1657 entry = cam_fetch_status_entry(status);
1658 printf("%s: CAM error %s (%#x) returned from "
1659 "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1660 entry->status_text : "Unknown", status);
1661 free(lun_softc, M_CTLFE);
1664 xpt_path_unlock(path);
1665 ctlfe_onoffline(arg, /*online*/ 1);
1666 xpt_free_path(path);
1670 ctlfe_offline(void *arg)
1672 struct ctlfe_softc *bus_softc;
1673 struct cam_path *path;
1675 struct cam_periph *periph;
1677 bus_softc = (struct ctlfe_softc *)arg;
1679 ctlfe_onoffline(arg, /*online*/ 0);
1682 * Disable the wildcard LUN for this port now that we have taken
1685 status = xpt_create_path(&path, /*periph*/ NULL,
1686 bus_softc->path_id, CAM_TARGET_WILDCARD,
1688 if (status != CAM_REQ_CMP) {
1689 printf("%s: unable to create path for wildcard periph\n",
1693 xpt_path_lock(path);
1694 if ((periph = cam_periph_find(path, "ctl")) != NULL)
1695 cam_periph_invalidate(periph);
1696 xpt_path_unlock(path);
1697 xpt_free_path(path);
1701 * This will get called to enable a LUN on every bus that is attached to
1702 * CTL. So we only need to create a path/periph for this particular bus.
1705 ctlfe_lun_enable(void *arg, int lun_id)
1707 struct ctlfe_softc *bus_softc;
1708 struct ctlfe_lun_softc *softc;
1709 struct cam_path *path;
1710 struct cam_periph *periph;
1713 bus_softc = (struct ctlfe_softc *)arg;
1714 if (bus_softc->hba_misc & PIM_EXTLUNS)
1715 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1717 status = xpt_create_path(&path, /*periph*/ NULL,
1718 bus_softc->path_id, bus_softc->target_id, lun_id);
1719 /* XXX KDM need some way to return status to CTL here? */
1720 if (status != CAM_REQ_CMP) {
1721 printf("%s: could not create path, status %#x\n", __func__,
1726 softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1727 xpt_path_lock(path);
1728 periph = cam_periph_find(path, "ctl");
1729 if (periph != NULL) {
1730 /* We've already got a periph, no need to alloc a new one. */
1731 xpt_path_unlock(path);
1732 xpt_free_path(path);
1733 free(softc, M_CTLFE);
1736 softc->parent_softc = bus_softc;
1738 status = cam_periph_alloc(ctlferegister,
1749 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1750 const struct cam_status_entry *entry;
1752 entry = cam_fetch_status_entry(status);
1753 printf("%s: CAM error %s (%#x) returned from "
1754 "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1755 entry->status_text : "Unknown", status);
1756 free(softc, M_CTLFE);
1759 xpt_path_unlock(path);
1760 xpt_free_path(path);
1765 * This will get called when the user removes a LUN to disable that LUN
1766 * on every bus that is attached to CTL.
1769 ctlfe_lun_disable(void *arg, int lun_id)
1771 struct ctlfe_softc *softc;
1772 struct ctlfe_lun_softc *lun_softc;
1774 softc = (struct ctlfe_softc *)arg;
1775 if (softc->hba_misc & PIM_EXTLUNS)
1776 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1778 mtx_lock(&softc->lun_softc_mtx);
1779 STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1780 struct cam_path *path;
1782 path = lun_softc->periph->path;
1784 if ((xpt_path_target_id(path) == softc->target_id)
1785 && (xpt_path_lun_id(path) == lun_id)) {
1789 if (lun_softc == NULL) {
1790 mtx_unlock(&softc->lun_softc_mtx);
1791 printf("%s: can't find lun %d\n", __func__, lun_id);
1794 cam_periph_acquire(lun_softc->periph);
1795 mtx_unlock(&softc->lun_softc_mtx);
1797 cam_periph_lock(lun_softc->periph);
1798 cam_periph_invalidate(lun_softc->periph);
1799 cam_periph_unlock(lun_softc->periph);
1800 cam_periph_release(lun_softc->periph);
1805 ctlfe_dump_sim(struct cam_sim *sim)
1808 printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
1809 sim->sim_name, sim->unit_number,
1810 sim->max_tagged_dev_openings, sim->max_dev_openings);
1814 * Assumes that the SIM lock is held.
1817 ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1819 struct ccb_hdr *hdr;
1820 struct cam_periph *periph;
1823 periph = softc->periph;
1826 TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
1827 union ctl_io *io = hdr->io_ptr;
1832 * Only regular SCSI I/O is put on the work
1833 * queue, so we can print sense here. There may be no
1834 * sense if it's no the queue for a DMA, but this serves to
1835 * print out the CCB as well.
1837 * XXX KDM switch this over to scsi_sense_print() when
1838 * CTL is merged in with CAM.
1840 ctl_io_error_print(io, NULL);
1843 * Print DMA status if we are DMA_QUEUED.
1845 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1846 xpt_print(periph->path,
1847 "Total %u, Current %u, Resid %u\n",
1848 io->scsiio.kern_total_len,
1849 io->scsiio.kern_data_len,
1850 io->scsiio.kern_data_resid);
1854 xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1855 xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1859 * Datamove/done routine called by CTL. Put ourselves on the queue to
1860 * receive a CCB from CAM so we can queue the continue I/O request down
1864 ctlfe_datamove(union ctl_io *io)
1867 struct cam_periph *periph;
1868 struct ctlfe_lun_softc *softc;
1870 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1871 ("Unexpected io_type (%d) in ctlfe_datamove", io->io_hdr.io_type));
1873 io->scsiio.ext_data_filled = 0;
1875 periph = xpt_path_periph(ccb->ccb_h.path);
1876 cam_periph_lock(periph);
1877 softc = (struct ctlfe_lun_softc *)periph->softc;
1878 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1879 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1880 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1881 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1883 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1884 cam_periph_unlock(periph);
1888 ctlfe_done(union ctl_io *io)
1891 struct cam_periph *periph;
1892 struct ctlfe_lun_softc *softc;
1895 periph = xpt_path_periph(ccb->ccb_h.path);
1896 cam_periph_lock(periph);
1897 softc = (struct ctlfe_lun_softc *)periph->softc;
1899 if (io->io_hdr.io_type == CTL_IO_TASK) {
1901 * Send the notify acknowledge down to the SIM, to let it
1902 * know we processed the task management command.
1904 ccb->ccb_h.status = CAM_REQ_INPROG;
1905 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1906 switch (io->taskio.task_status) {
1907 case CTL_TASK_FUNCTION_COMPLETE:
1908 ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1910 case CTL_TASK_FUNCTION_SUCCEEDED:
1911 ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1912 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1914 case CTL_TASK_FUNCTION_REJECTED:
1915 ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1916 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1918 case CTL_TASK_LUN_DOES_NOT_EXIST:
1919 ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1920 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1922 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1923 ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1924 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1927 ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1929 } else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1930 ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1933 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1934 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1936 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1939 cam_periph_unlock(periph);
1945 struct ctlfe_softc *bus_softc;
1946 struct ctlfe_lun_softc *lun_softc;
1948 STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1949 ctlfe_dump_sim(bus_softc->sim);
1950 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1951 ctlfe_dump_queue(lun_softc);