2 * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
33 * Peripheral driver interface between CAM and CTL (CAM Target Layer).
35 * Author: Ken Merry <ken@FreeBSD.org>
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/malloc.h>
50 #include <sys/endian.h>
52 #include <sys/sysctl.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <machine/bus.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_queue.h>
61 #include <cam/cam_xpt_periph.h>
62 #include <cam/cam_debug.h>
63 #include <cam/cam_sim.h>
64 #include <cam/cam_xpt.h>
66 #include <cam/scsi/scsi_all.h>
67 #include <cam/scsi/scsi_message.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_util.h>
73 #include <cam/ctl/ctl_error.h>
76 CTLFE_CCB_DEFAULT = 0x00,
77 CTLFE_CCB_WAITING = 0x01
81 struct ctl_frontend fe;
84 char port_name[DEV_IDLEN];
85 STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
86 STAILQ_ENTRY(ctlfe_softc) links;
89 STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
90 struct mtx ctlfe_list_mtx;
91 static char ctlfe_mtx_desc[] = "ctlfelist";
92 static int ctlfe_dma_enabled = 1;
93 #ifdef CTLFE_INIT_ENABLE
94 static int ctlfe_max_targets = 1;
95 static int ctlfe_num_targets = 0;
99 CTLFE_LUN_NONE = 0x00,
100 CTLFE_LUN_WILDCARD = 0x01
103 struct ctlfe_lun_softc {
104 struct ctlfe_softc *parent_softc;
105 struct cam_periph *periph;
106 ctlfe_lun_flags flags;
107 struct callout dma_callout;
108 uint64_t ccbs_alloced;
111 uint64_t ctios_returned;
113 uint64_t atios_returned;
115 uint64_t inots_returned;
116 /* bus_dma_tag_t dma_tag; */
117 TAILQ_HEAD(, ccb_hdr) work_queue;
118 STAILQ_ENTRY(ctlfe_lun_softc) links;
122 CTLFE_CMD_NONE = 0x00,
123 CTLFE_CMD_PIECEWISE = 0x01
127 * The size limit of this structure is CTL_PORT_PRIV_SIZE, from ctl_io.h.
128 * Currently that is 600 bytes.
130 struct ctlfe_lun_cmd_info {
131 int cur_transfer_index;
132 ctlfe_cmd_flags flags;
134 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
135 * bytes on amd64. So with 32 elements, this is 256 bytes on
136 * i386 and 512 bytes on amd64.
138 bus_dma_segment_t cam_sglist[32];
142 * When we register the adapter/bus, request that this many ctl_ios be
143 * allocated. This should be the maximum supported by the adapter, but we
144 * currently don't have a way to get that back from the path inquiry.
145 * XXX KDM add that to the path inquiry.
147 #define CTLFE_REQ_CTL_IO 4096
149 * Number of Accept Target I/O CCBs to allocate and queue down to the
151 * XXX KDM should this be controlled by CTL?
153 #define CTLFE_ATIO_PER_LUN 1024
155 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
156 * allocate and queue down to the adapter per LUN.
157 * XXX KDM should this be controlled by CTL?
159 #define CTLFE_IN_PER_LUN 1024
162 * Timeout (in seconds) on CTIO CCB allocation for doing a DMA or sending
163 * status to the initiator. The SIM is expected to have its own timeouts,
164 * so we're not putting this timeout around the CCB execution time. The
165 * SIM should timeout and let us know if it has an issue.
167 #define CTLFE_DMA_TIMEOUT 60
170 * Turn this on to enable extra debugging prints.
177 * Use randomly assigned WWNN/WWPN values. This is to work around an issue
178 * in the FreeBSD initiator that makes it unable to rescan the target if
179 * the target gets rebooted and the WWNN/WWPN stay the same.
185 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, dma_enabled, CTLFLAG_RW,
186 &ctlfe_dma_enabled, 0, "DMA enabled");
187 MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
189 #define ccb_type ppriv_field0
190 /* This is only used in the ATIO */
191 #define io_ptr ppriv_ptr1
193 /* This is only used in the CTIO */
194 #define ccb_atio ppriv_ptr1
196 int ctlfeinitialize(void);
197 void ctlfeshutdown(void);
198 static periph_init_t ctlfeinit;
199 static void ctlfeasync(void *callback_arg, uint32_t code,
200 struct cam_path *path, void *arg);
201 static periph_ctor_t ctlferegister;
202 static periph_oninv_t ctlfeoninvalidate;
203 static periph_dtor_t ctlfecleanup;
204 static periph_start_t ctlfestart;
205 static void ctlfedone(struct cam_periph *periph,
206 union ccb *done_ccb);
208 static void ctlfe_onoffline(void *arg, int online);
209 static void ctlfe_online(void *arg);
210 static void ctlfe_offline(void *arg);
211 static int ctlfe_targ_enable(void *arg, struct ctl_id targ_id);
212 static int ctlfe_targ_disable(void *arg, struct ctl_id targ_id);
213 static int ctlfe_lun_enable(void *arg, struct ctl_id targ_id,
215 static int ctlfe_lun_disable(void *arg, struct ctl_id targ_id,
217 static void ctlfe_dump_sim(struct cam_sim *sim);
218 static void ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
219 static void ctlfe_dma_timeout(void *arg);
220 static void ctlfe_datamove_done(union ctl_io *io);
221 static void ctlfe_dump(void);
223 static struct periph_driver ctlfe_driver =
226 TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0
229 static int ctlfe_module_event_handler(module_t, int /*modeventtype_t*/, void *);
232 * We're not using PERIPHDRIVER_DECLARE(), because it runs at SI_SUB_DRIVERS,
233 * and that happens before CTL gets initialised.
235 static moduledata_t ctlfe_moduledata = {
237 ctlfe_module_event_handler,
241 DECLARE_MODULE(ctlfe, ctlfe_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
242 MODULE_VERSION(ctlfe, 1);
243 MODULE_DEPEND(ctlfe, ctl, 1, 1, 1);
244 MODULE_DEPEND(ctlfe, cam, 1, 1, 1);
246 extern struct ctl_softc *control_softc;
247 extern int ctl_disable;
260 /* Don't initialize if we're disabled */
261 if (ctl_disable != 0)
264 STAILQ_INIT(&ctlfe_softc_list);
266 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
268 KASSERT(control_softc != NULL, ("CTL is not initialized!"));
270 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
271 AC_CONTRACT, ctlfeasync, NULL, NULL);
273 if (status != CAM_REQ_CMP) {
274 printf("ctl: Failed to attach async callback due to CAM "
275 "status 0x%x!\n", status);
280 ctlfe_module_event_handler(module_t mod, int what, void *arg)
285 periphdriver_register(&ctlfe_driver);
295 ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
299 printf("%s: entered\n", __func__);
303 * When a new path gets registered, and it is capable of target
304 * mode, go ahead and attach. Later on, we may need to be more
305 * selective, but for now this will be sufficient.
308 case AC_PATH_REGISTERED: {
309 struct ctl_frontend *fe;
310 struct ctlfe_softc *bus_softc;
311 struct ccb_pathinq *cpi;
314 cpi = (struct ccb_pathinq *)arg;
316 /* Don't attach if it doesn't support target mode */
317 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
319 printf("%s: SIM %s%d doesn't support target mode\n",
320 __func__, cpi->dev_name, cpi->unit_number);
325 #ifdef CTLFE_INIT_ENABLE
326 if (ctlfe_num_targets >= ctlfe_max_targets) {
330 ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP,
333 printf("%s: unable to malloc CCB!\n", __func__);
336 xpt_setup_ccb(&ccb->ccb_h, cpi->ccb_h.path,
339 sim = xpt_path_sim(cpi->ccb_h.path);
341 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
342 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
343 ccb->knob.xport_specific.fc.role = KNOB_ROLE_INITIATOR;
345 /* We should hold the SIM lock here */
346 mtx_assert(sim->mtx, MA_OWNED);
350 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
352 printf("%s: SIM %s%d (path id %d) initiator "
353 "enable failed with status %#x\n",
354 __func__, cpi->dev_name,
355 cpi->unit_number, cpi->ccb_h.path_id,
358 printf("%s: SIM %s%d (path id %d) initiator "
359 "enable succeeded\n",
360 __func__, cpi->dev_name,
361 cpi->unit_number, cpi->ccb_h.path_id);
371 printf("%s: ctlfe_num_targets = %d\n", __func__,
373 #endif /* CTLFE_INIT_ENABLE */
376 * We're in an interrupt context here, so we have to
377 * use M_NOWAIT. Of course this means trouble if we
378 * can't allocate memory.
380 bus_softc = malloc(sizeof(*bus_softc), M_CTLFE,
382 if (bus_softc == NULL) {
383 printf("%s: unable to malloc %zd bytes for softc\n",
384 __func__, sizeof(*bus_softc));
388 bus_softc->path_id = cpi->ccb_h.path_id;
389 bus_softc->sim = xpt_path_sim(cpi->ccb_h.path);
390 STAILQ_INIT(&bus_softc->lun_softc_list);
395 * XXX KDM should we be more accurate here ?
397 if (cpi->transport == XPORT_FC)
398 fe->port_type = CTL_PORT_FC;
400 fe->port_type = CTL_PORT_SCSI;
402 /* XXX KDM what should the real number be here? */
403 fe->num_requested_ctl_io = 4096;
404 snprintf(bus_softc->port_name, sizeof(bus_softc->port_name),
405 "%s%d", cpi->dev_name, cpi->unit_number);
407 * XXX KDM it would be nice to allocate storage in the
408 * frontend structure itself.
410 fe->port_name = bus_softc->port_name;
411 fe->physical_port = cpi->unit_number;
412 fe->virtual_port = cpi->bus_id;
413 fe->port_online = ctlfe_online;
414 fe->port_offline = ctlfe_offline;
415 fe->onoff_arg = bus_softc;
416 fe->targ_enable = ctlfe_targ_enable;
417 fe->targ_disable = ctlfe_targ_disable;
418 fe->lun_enable = ctlfe_lun_enable;
419 fe->lun_disable = ctlfe_lun_disable;
420 fe->targ_lun_arg = bus_softc;
421 fe->fe_datamove = ctlfe_datamove_done;
422 fe->fe_done = ctlfe_datamove_done;
423 fe->fe_dump = ctlfe_dump;
425 * XXX KDM the path inquiry doesn't give us the maximum
426 * number of targets supported.
428 fe->max_targets = cpi->max_target;
429 fe->max_target_id = cpi->max_target;
432 * XXX KDM need to figure out whether we're the master or
436 printf("%s: calling ctl_frontend_register() for %s%d\n",
437 __func__, cpi->dev_name, cpi->unit_number);
439 retval = ctl_frontend_register(fe, /*master_SC*/ 1);
441 printf("%s: ctl_frontend_register() failed with "
442 "error %d!\n", __func__, retval);
443 free(bus_softc, M_CTLFE);
446 mtx_lock(&ctlfe_list_mtx);
447 STAILQ_INSERT_TAIL(&ctlfe_softc_list, bus_softc, links);
448 mtx_unlock(&ctlfe_list_mtx);
453 case AC_PATH_DEREGISTERED: {
454 struct ctlfe_softc *softc = NULL;
456 mtx_lock(&ctlfe_list_mtx);
457 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
458 if (softc->path_id == xpt_path_path_id(path)) {
459 STAILQ_REMOVE(&ctlfe_softc_list, softc,
464 mtx_unlock(&ctlfe_list_mtx);
468 * XXX KDM are we certain at this point that there
469 * are no outstanding commands for this frontend?
471 ctl_frontend_deregister(&softc->fe);
472 free(softc, M_CTLFE);
477 struct ac_contract *ac;
479 ac = (struct ac_contract *)arg;
481 switch (ac->contract_number) {
482 case AC_CONTRACT_DEV_CHG: {
483 struct ac_device_changed *dev_chg;
484 struct ctlfe_softc *softc;
487 dev_chg = (struct ac_device_changed *)ac->contract_data;
489 printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
490 __func__, dev_chg->wwpn, dev_chg->port,
491 xpt_path_path_id(path), dev_chg->target,
492 (dev_chg->arrived == 0) ? "left" : "arrived");
496 mtx_lock(&ctlfe_list_mtx);
497 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
498 if (softc->path_id == xpt_path_path_id(path)) {
503 mtx_unlock(&ctlfe_list_mtx);
506 printf("%s: CTL port for CAM path %u not "
507 "found!\n", __func__,
508 xpt_path_path_id(path));
511 if (dev_chg->arrived != 0) {
512 retval = ctl_add_initiator(dev_chg->wwpn,
513 softc->fe.targ_port, dev_chg->target);
515 retval = ctl_remove_initiator(
516 softc->fe.targ_port, dev_chg->target);
520 printf("%s: could not %s port %d iid %u "
521 "WWPN %#jx!\n", __func__,
522 (dev_chg->arrived != 0) ? "add" :
523 "remove", softc->fe.targ_port,
525 (uintmax_t)dev_chg->wwpn);
530 printf("%s: unsupported contract number %ju\n",
531 __func__, (uintmax_t)ac->contract_number);
542 ctlferegister(struct cam_periph *periph, void *arg)
544 struct ctlfe_softc *bus_softc;
545 struct ctlfe_lun_softc *softc;
547 union ccb en_lun_ccb;
551 softc = (struct ctlfe_lun_softc *)arg;
552 bus_softc = softc->parent_softc;
553 sim = xpt_path_sim(periph->path);
555 TAILQ_INIT(&softc->work_queue);
556 softc->periph = periph;
558 callout_init_mtx(&softc->dma_callout, sim->mtx, /*flags*/ 0);
559 periph->softc = softc;
561 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
562 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
563 en_lun_ccb.cel.grp6_len = 0;
564 en_lun_ccb.cel.grp7_len = 0;
565 en_lun_ccb.cel.enable = 1;
566 xpt_action(&en_lun_ccb);
567 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
568 if (status != CAM_REQ_CMP) {
569 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
570 __func__, en_lun_ccb.ccb_h.status);
574 status = CAM_REQ_CMP;
576 for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
579 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
581 if (new_ccb == NULL) {
582 status = CAM_RESRC_UNAVAIL;
585 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
586 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
587 new_ccb->ccb_h.cbfcnp = ctlfedone;
590 status = new_ccb->ccb_h.status;
591 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
592 free(new_ccb, M_CTLFE);
597 status = cam_periph_acquire(periph);
598 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
599 xpt_print(periph->path, "%s: could not acquire reference "
600 "count, status = %#x\n", __func__, status);
605 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
606 "status 0x%x\n", __func__, status);
607 return (CAM_REQ_CMP_ERR);
610 for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
613 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
615 if (new_ccb == NULL) {
616 status = CAM_RESRC_UNAVAIL;
620 xpt_setup_ccb(&new_ccb->ccb_h, periph->path, /*priority*/ 1);
621 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
622 new_ccb->ccb_h.cbfcnp = ctlfedone;
625 status = new_ccb->ccb_h.status;
626 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
628 * Note that we don't free the CCB here. If the
629 * status is not CAM_REQ_INPROG, then we're
630 * probably talking to a SIM that says it is
631 * target-capable but doesn't support the
632 * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the
633 * older API. In that case, it'll call xpt_done()
634 * on the CCB, and we need to free it in our done
635 * routine as a result.
641 || (status != CAM_REQ_INPROG)) {
642 xpt_print(periph->path, "%s: could not allocate immediate "
643 "notify CCBs, status 0x%x\n", __func__, status);
644 return (CAM_REQ_CMP_ERR);
646 return (CAM_REQ_CMP);
650 ctlfeoninvalidate(struct cam_periph *periph)
652 union ccb en_lun_ccb;
654 struct ctlfe_lun_softc *softc;
656 softc = (struct ctlfe_lun_softc *)periph->softc;
658 xpt_setup_ccb(&en_lun_ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
659 en_lun_ccb.ccb_h.func_code = XPT_EN_LUN;
660 en_lun_ccb.cel.grp6_len = 0;
661 en_lun_ccb.cel.grp7_len = 0;
662 en_lun_ccb.cel.enable = 0;
663 xpt_action(&en_lun_ccb);
664 status = (en_lun_ccb.ccb_h.status & CAM_STATUS_MASK);
665 if (status != CAM_REQ_CMP) {
666 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
667 __func__, en_lun_ccb.ccb_h.status);
669 * XXX KDM what do we do now?
672 xpt_print(periph->path, "LUN removed, %ju ATIOs outstanding, %ju "
673 "INOTs outstanding, %d refs\n", softc->atios_sent -
674 softc->atios_returned, softc->inots_sent -
675 softc->inots_returned, periph->refcount);
679 ctlfecleanup(struct cam_periph *periph)
681 struct ctlfe_lun_softc *softc;
682 struct ctlfe_softc *bus_softc;
684 xpt_print(periph->path, "%s: Called\n", __func__);
686 softc = (struct ctlfe_lun_softc *)periph->softc;
687 bus_softc = softc->parent_softc;
689 STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
692 * XXX KDM is there anything else that needs to be done here?
695 callout_stop(&softc->dma_callout);
697 free(softc, M_CTLFE);
701 ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
703 struct ctlfe_lun_softc *softc;
704 struct ccb_hdr *ccb_h;
706 softc = (struct ctlfe_lun_softc *)periph->softc;
708 softc->ccbs_alloced++;
710 start_ccb->ccb_h.ccb_type = CTLFE_CCB_DEFAULT;
712 ccb_h = TAILQ_FIRST(&softc->work_queue);
713 if (periph->immediate_priority <= periph->pinfo.priority) {
714 panic("shouldn't get to the CCB waiting case!");
715 start_ccb->ccb_h.ccb_type = CTLFE_CCB_WAITING;
716 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
718 periph->immediate_priority = CAM_PRIORITY_NONE;
719 wakeup(&periph->ccb_list);
720 } else if (ccb_h == NULL) {
722 xpt_release_ccb(start_ccb);
724 struct ccb_accept_tio *atio;
725 struct ccb_scsiio *csio;
732 /* Take the ATIO off the work queue */
733 TAILQ_REMOVE(&softc->work_queue, ccb_h, periph_links.tqe);
734 atio = (struct ccb_accept_tio *)ccb_h;
735 io = (union ctl_io *)ccb_h->io_ptr;
736 csio = &start_ccb->csio;
738 flags = atio->ccb_h.flags &
739 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
742 || (io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
744 * We're done, send status back.
746 flags |= CAM_SEND_STATUS;
748 scsi_status = SCSI_STATUS_BUSY;
750 } else if ((io->io_hdr.status & CTL_STATUS_MASK) ==
752 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
755 * If this command was aborted, we don't
756 * need to send status back to the SIM.
757 * Just free the CTIO and ctl_io, and
758 * recycle the ATIO back to the SIM.
760 xpt_print(periph->path, "%s: aborted "
761 "command 0x%04x discarded\n",
762 __func__, io->scsiio.tag_num);
765 * For a wildcard attachment, commands can
766 * come in with a specific target/lun. Reset
767 * the target and LUN fields back to the
768 * wildcard values before we send them back
769 * down to the SIM. The SIM has a wildcard
770 * LUN enabled, not whatever target/lun
771 * these happened to be.
773 if (softc->flags & CTLFE_LUN_WILDCARD) {
774 atio->ccb_h.target_id =
776 atio->ccb_h.target_lun =
780 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
781 cam_release_devq(periph->path,
786 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
789 ccb_h = TAILQ_FIRST(&softc->work_queue);
791 if (atio->ccb_h.func_code !=
792 XPT_ACCEPT_TARGET_IO) {
793 xpt_print(periph->path, "%s: func_code "
794 "is %#x\n", __func__,
795 atio->ccb_h.func_code);
797 start_ccb->ccb_h.func_code = XPT_ABORT;
798 start_ccb->cab.abort_ccb = (union ccb *)atio;
799 start_ccb->ccb_h.cbfcnp = ctlfedone;
801 /* Tell the SIM that we've aborted this ATIO */
802 xpt_action(start_ccb);
804 xpt_release_ccb(start_ccb);
807 * Send the ATIO back down to the SIM.
809 xpt_action((union ccb *)atio);
813 * If we still have work to do, ask for
814 * another CCB. Otherwise, deactivate our
818 xpt_schedule(periph, /*priority*/ 1);
820 callout_stop(&softc->dma_callout);
824 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
825 scsi_status = io->scsiio.scsi_status;
826 csio->sense_len = io->scsiio.sense_len;
831 printf("%s: tag %04x io is NULL\n", __func__,
835 printf("%s: tag %04x status %x\n", __func__,
836 atio->tag_id, io->io_hdr.status);
839 csio->sglist_cnt = 0;
840 if (csio->sense_len != 0) {
841 csio->sense_data = io->scsiio.sense_data;
842 flags |= CAM_SEND_SENSE;
843 } else if (scsi_status == SCSI_STATUS_CHECK_COND) {
844 xpt_print(periph->path, "%s: check condition "
845 "with no sense\n", __func__);
848 struct ctlfe_lun_cmd_info *cmd_info;
851 * Datamove call, we need to setup the S/G list.
854 cmd_info = (struct ctlfe_lun_cmd_info *)
855 io->io_hdr.port_priv;
857 KASSERT(sizeof(*cmd_info) < CTL_PORT_PRIV_SIZE,
858 ("%s: sizeof(struct ctlfe_lun_cmd_info) %zd < "
859 "CTL_PORT_PRIV_SIZE %d", __func__,
860 sizeof(*cmd_info), CTL_PORT_PRIV_SIZE));
861 io->io_hdr.flags &= ~CTL_FLAG_DMA_QUEUED;
864 * Need to zero this, in case it has been used for
865 * a previous datamove for this particular I/O.
867 bzero(cmd_info, sizeof(*cmd_info));
871 * Set the direction, relative to the initiator.
873 flags &= ~CAM_DIR_MASK;
874 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
878 flags |= CAM_DIR_OUT;
880 csio->cdb_len = atio->cdb_len;
882 flags &= ~CAM_DATA_MASK;
883 if (io->scsiio.kern_sg_entries == 0) {
885 data_ptr = io->scsiio.kern_data_ptr;
886 dxfer_len = io->scsiio.kern_data_len;
887 csio->sglist_cnt = 0;
889 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
890 flags |= CAM_DATA_PADDR;
892 flags |= CAM_DATA_VADDR;
893 } else if (io->scsiio.kern_sg_entries <=
894 (sizeof(cmd_info->cam_sglist)/
895 sizeof(cmd_info->cam_sglist[0]))) {
897 * S/G list with physical or virtual pointers.
898 * Just populate the CAM S/G list with the
902 struct ctl_sg_entry *ctl_sglist;
903 bus_dma_segment_t *cam_sglist;
905 ctl_sglist = (struct ctl_sg_entry *)
906 io->scsiio.kern_data_ptr;
907 cam_sglist = cmd_info->cam_sglist;
909 for (i = 0; i < io->scsiio.kern_sg_entries;i++){
910 cam_sglist[i].ds_addr =
911 (bus_addr_t)ctl_sglist[i].addr;
912 cam_sglist[i].ds_len =
915 csio->sglist_cnt = io->scsiio.kern_sg_entries;
916 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
917 flags |= CAM_DATA_SG_PADDR;
919 flags &= ~CAM_DATA_SG;
920 data_ptr = (uint8_t *)cam_sglist;
921 dxfer_len = io->scsiio.kern_data_len;
923 /* S/G list with virtual pointers */
924 struct ctl_sg_entry *sglist;
928 * If we have more S/G list pointers than
929 * will fit in the available storage in the
930 * cmd_info structure inside the ctl_io header,
931 * then we need to send down the pointers
932 * one element at a time.
935 sglist = (struct ctl_sg_entry *)
936 io->scsiio.kern_data_ptr;
937 ti = &cmd_info->cur_transfer_index;
938 data_ptr = sglist[*ti].addr;
939 dxfer_len = sglist[*ti].len;
940 csio->sglist_cnt = 0;
941 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
945 io->scsiio.ext_data_filled += dxfer_len;
947 if (io->scsiio.ext_data_filled >
948 io->scsiio.kern_total_len) {
949 xpt_print(periph->path, "%s: tag 0x%04x "
950 "fill len %u > total %u\n",
951 __func__, io->scsiio.tag_num,
952 io->scsiio.ext_data_filled,
953 io->scsiio.kern_total_len);
958 printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
959 (flags & CAM_SEND_STATUS) ? "done" : "datamove",
960 atio->tag_id, flags, data_ptr, dxfer_len);
964 * Valid combinations:
965 * - CAM_SEND_STATUS, SCATTER_VALID = 0, dxfer_len = 0,
967 * - CAM_SEND_STATUS = 0, SCATTER_VALID = 0, dxfer_len != 0,
969 * - CAM_SEND_STATUS = 0, SCATTER_VALID, dxfer_len != 0,
973 if (((flags & CAM_SEND_STATUS)
974 && (((flags & CAM_SCATTER_VALID) != 0)
976 || (csio->sglist_cnt != 0)))
977 || (((flags & CAM_SEND_STATUS) == 0)
979 || ((flags & CAM_SCATTER_VALID)
980 && (csio->sglist_cnt == 0))
981 || (((flags & CAM_SCATTER_VALID) == 0)
982 && (csio->sglist_cnt != 0))) {
983 printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
984 "%d sg %u\n", __func__, atio->tag_id,
985 atio->cdb_io.cdb_bytes[0], flags, dxfer_len,
988 printf("%s: tag %04x io status %#x\n", __func__,
989 atio->tag_id, io->io_hdr.status);
991 printf("%s: tag %04x no associated io\n",
992 __func__, atio->tag_id);
1000 (flags & CAM_TAG_ACTION_VALID) ?
1001 MSG_SIMPLE_Q_TAG : 0,
1005 /*data_ptr*/ data_ptr,
1006 /*dxfer_len*/ dxfer_len,
1007 /*timeout*/ 5 * 1000);
1008 start_ccb->ccb_h.ccb_atio = atio;
1009 if (((flags & CAM_SEND_STATUS) == 0)
1011 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1013 softc->ctios_sent++;
1015 xpt_action(start_ccb);
1017 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1018 cam_release_devq(periph->path,
1022 /*getcount_only*/0);
1023 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
1026 ccb_h = TAILQ_FIRST(&softc->work_queue);
1029 * If we still have work to do, ask for another CCB. Otherwise,
1030 * deactivate our callout.
1033 xpt_schedule(periph, /*priority*/ 1);
1035 callout_stop(&softc->dma_callout);
1039 ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
1041 struct ctlfe_lun_softc *softc;
1043 softc = (struct ctlfe_lun_softc *)periph->softc;
1045 switch (ccb->ccb_h.func_code) {
1046 case XPT_ACCEPT_TARGET_IO:
1047 softc->atios_returned++;
1049 case XPT_IMMEDIATE_NOTIFY:
1050 case XPT_NOTIFY_ACKNOWLEDGE:
1051 softc->inots_returned++;
1059 KASSERT(softc->atios_returned <= softc->atios_sent, ("%s: "
1060 "atios_returned %ju > atios_sent %ju", __func__,
1061 softc->atios_returned, softc->atios_sent));
1062 KASSERT(softc->inots_returned <= softc->inots_sent, ("%s: "
1063 "inots_returned %ju > inots_sent %ju", __func__,
1064 softc->inots_returned, softc->inots_sent));
1067 * If we have received all of our CCBs, we can release our
1068 * reference on the peripheral driver. It will probably go away
1071 if ((softc->atios_returned == softc->atios_sent)
1072 && (softc->inots_returned == softc->inots_sent)) {
1073 cam_periph_release_locked(periph);
1078 ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1081 uint32_t num_blocks, nbc;
1082 uint8_t *cmdbyt = (atio->ccb_h.flags & CAM_CDB_POINTER)?
1083 atio->cdb_io.cdb_ptr : atio->cdb_io.cdb_bytes;
1085 nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */
1087 switch (cmdbyt[0]) {
1091 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1092 lba = scsi_3btoul(cdb->addr);
1094 num_blocks = cdb->length;
1095 if (num_blocks == 0)
1099 scsi_ulto3b(lba, cdb->addr);
1100 cdb->length = num_blocks;
1106 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1107 lba = scsi_4btoul(cdb->addr);
1108 num_blocks = scsi_2btoul(cdb->length);
1111 scsi_ulto4b(lba, cdb->addr);
1112 scsi_ulto2b(num_blocks, cdb->length);
1118 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1119 lba = scsi_4btoul(cdb->addr);
1120 num_blocks = scsi_4btoul(cdb->length);
1123 scsi_ulto4b(lba, cdb->addr);
1124 scsi_ulto4b(num_blocks, cdb->length);
1130 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1131 lba = scsi_8btou64(cdb->addr);
1132 num_blocks = scsi_4btoul(cdb->length);
1135 scsi_u64to8b(lba, cdb->addr);
1136 scsi_ulto4b(num_blocks, cdb->length);
1146 ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1148 struct ctlfe_lun_softc *softc;
1149 struct ctlfe_softc *bus_softc;
1150 struct ccb_accept_tio *atio = NULL;
1151 union ctl_io *io = NULL;
1154 printf("%s: entered, func_code = %#x, type = %#lx\n", __func__,
1155 done_ccb->ccb_h.func_code, done_ccb->ccb_h.ccb_type);
1158 softc = (struct ctlfe_lun_softc *)periph->softc;
1159 bus_softc = softc->parent_softc;
1161 if (done_ccb->ccb_h.ccb_type == CTLFE_CCB_WAITING) {
1162 panic("shouldn't get to the CCB waiting case!");
1163 wakeup(&done_ccb->ccb_h.cbfcnp);
1168 * If the peripheral is invalid, ATIOs and immediate notify CCBs
1169 * need to be freed. Most of the ATIOs and INOTs that come back
1170 * will be CCBs that are being returned from the SIM as a result of
1171 * our disabling the LUN.
1173 * Other CCB types are handled in their respective cases below.
1175 if (periph->flags & CAM_PERIPH_INVALID) {
1176 switch (done_ccb->ccb_h.func_code) {
1177 case XPT_ACCEPT_TARGET_IO:
1178 case XPT_IMMEDIATE_NOTIFY:
1179 case XPT_NOTIFY_ACKNOWLEDGE:
1180 ctlfe_free_ccb(periph, done_ccb);
1187 switch (done_ccb->ccb_h.func_code) {
1188 case XPT_ACCEPT_TARGET_IO: {
1190 atio = &done_ccb->atio;
1192 softc->atios_returned++;
1196 * Allocate a ctl_io, pass it to CTL, and wait for the
1199 io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1201 atio->ccb_h.flags &= ~CAM_DIR_MASK;
1202 atio->ccb_h.flags |= CAM_DIR_NONE;
1204 printf("%s: ctl_alloc_io failed!\n", __func__);
1207 * XXX KDM need to set SCSI_STATUS_BUSY, but there
1208 * is no field in the ATIO structure to do that,
1209 * and we aren't able to allocate a ctl_io here.
1212 atio->sense_len = 0;
1213 done_ccb->ccb_h.io_ptr = NULL;
1214 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
1216 xpt_schedule(periph, /*priority*/ 1);
1221 /* Save pointers on both sides */
1222 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = done_ccb;
1223 done_ccb->ccb_h.io_ptr = io;
1226 * Only SCSI I/O comes down this path, resets, etc. come
1227 * down the immediate notify path below.
1229 io->io_hdr.io_type = CTL_IO_SCSI;
1230 io->io_hdr.nexus.initid.id = atio->init_id;
1231 io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1232 io->io_hdr.nexus.targ_target.id = atio->ccb_h.target_id;
1233 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1234 io->scsiio.tag_num = atio->tag_id;
1235 switch (atio->tag_action) {
1236 case CAM_TAG_ACTION_NONE:
1237 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1239 case MSG_SIMPLE_TASK:
1240 io->scsiio.tag_type = CTL_TAG_SIMPLE;
1242 case MSG_HEAD_OF_QUEUE_TASK:
1243 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1245 case MSG_ORDERED_TASK:
1246 io->scsiio.tag_type = CTL_TAG_ORDERED;
1249 io->scsiio.tag_type = CTL_TAG_ACA;
1252 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1253 printf("%s: unhandled tag type %#x!!\n", __func__,
1257 if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1258 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1259 __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1261 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1262 bcopy(atio->cdb_io.cdb_bytes, io->scsiio.cdb,
1263 io->scsiio.cdb_len);
1266 printf("%s: %ju:%d:%ju:%d: tag %04x CDB %02x\n", __func__,
1267 (uintmax_t)io->io_hdr.nexus.initid.id,
1268 io->io_hdr.nexus.targ_port,
1269 (uintmax_t)io->io_hdr.nexus.targ_target.id,
1270 io->io_hdr.nexus.targ_lun,
1271 io->scsiio.tag_num, io->scsiio.cdb[0]);
1277 case XPT_CONT_TARGET_IO: {
1279 uint32_t srr_off = 0;
1281 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1282 io = (union ctl_io *)atio->ccb_h.io_ptr;
1284 softc->ctios_returned++;
1286 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1287 __func__, atio->tag_id, done_ccb->ccb_h.flags);
1290 * Handle SRR case were the data pointer is pushed back hack
1292 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1293 && done_ccb->csio.msg_ptr != NULL
1294 && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1295 && done_ccb->csio.msg_ptr[1] == 5
1296 && done_ccb->csio.msg_ptr[2] == 0) {
1299 (done_ccb->csio.msg_ptr[3] << 24)
1300 | (done_ccb->csio.msg_ptr[4] << 16)
1301 | (done_ccb->csio.msg_ptr[5] << 8)
1302 | (done_ccb->csio.msg_ptr[6]);
1305 if (srr && (done_ccb->ccb_h.flags & CAM_SEND_STATUS)) {
1307 * If status was being sent, the back end data is now
1308 * history. Hack it up and resubmit a new command with
1309 * the CDB adjusted. If the SIM does the right thing,
1310 * all of the resid math should work.
1312 softc->ccbs_freed++;
1313 xpt_release_ccb(done_ccb);
1315 if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1316 done_ccb = (union ccb *)atio;
1320 * Fall through to doom....
1324 * If we have an srr and we're still sending data, we
1325 * should be able to adjust offsets and cycle again.
1327 io->scsiio.kern_rel_offset =
1328 io->scsiio.ext_data_filled = srr_off;
1329 io->scsiio.ext_data_len = io->scsiio.kern_total_len -
1330 io->scsiio.kern_rel_offset;
1331 softc->ccbs_freed++;
1332 io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1333 xpt_release_ccb(done_ccb);
1334 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1336 xpt_schedule(periph, /*priority*/ 1);
1341 * If we were sending status back to the initiator, free up
1342 * resources. If we were doing a datamove, call the
1343 * datamove done routine.
1345 if (done_ccb->ccb_h.flags & CAM_SEND_STATUS) {
1346 softc->ccbs_freed++;
1347 xpt_release_ccb(done_ccb);
1350 * For a wildcard attachment, commands can come in
1351 * with a specific target/lun. Reset the target
1352 * and LUN fields back to the wildcard values before
1353 * we send them back down to the SIM. The SIM has
1354 * a wildcard LUN enabled, not whatever target/lun
1355 * these happened to be.
1357 if (softc->flags & CTLFE_LUN_WILDCARD) {
1358 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
1359 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
1361 if (periph->flags & CAM_PERIPH_INVALID) {
1362 ctlfe_free_ccb(periph, (union ccb *)atio);
1365 xpt_action((union ccb *)atio);
1366 softc->atios_sent++;
1369 struct ctlfe_lun_cmd_info *cmd_info;
1370 struct ccb_scsiio *csio;
1372 csio = &done_ccb->csio;
1373 cmd_info = (struct ctlfe_lun_cmd_info *)
1374 io->io_hdr.port_priv;
1376 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1378 io->scsiio.ext_data_len += csio->dxfer_len;
1379 if (io->scsiio.ext_data_len >
1380 io->scsiio.kern_total_len) {
1381 xpt_print(periph->path, "%s: tag 0x%04x "
1382 "done len %u > total %u sent %u\n",
1383 __func__, io->scsiio.tag_num,
1384 io->scsiio.ext_data_len,
1385 io->scsiio.kern_total_len,
1386 io->scsiio.ext_data_filled);
1389 * Translate CAM status to CTL status. Success
1390 * does not change the overall, ctl_io status. In
1391 * that case we just set port_status to 0. If we
1392 * have a failure, though, set a data phase error
1393 * for the overall ctl_io.
1395 switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1397 io->io_hdr.port_status = 0;
1401 * XXX KDM we probably need to figure out a
1402 * standard set of errors that the SIM
1403 * drivers should return in the event of a
1404 * data transfer failure. A data phase
1405 * error will at least point the user to a
1406 * data transfer error of some sort.
1407 * Hopefully the SIM printed out some
1408 * additional information to give the user
1409 * a clue what happened.
1411 io->io_hdr.port_status = 0xbad1;
1412 ctl_set_data_phase_error(&io->scsiio);
1414 * XXX KDM figure out residual.
1419 * If we had to break this S/G list into multiple
1420 * pieces, figure out where we are in the list, and
1421 * continue sending pieces if necessary.
1423 if ((cmd_info->flags & CTLFE_CMD_PIECEWISE)
1424 && (io->io_hdr.port_status == 0)
1425 && (cmd_info->cur_transfer_index <
1426 io->scsiio.kern_sg_entries)) {
1427 struct ctl_sg_entry *sglist;
1429 uint8_t scsi_status;
1434 sglist = (struct ctl_sg_entry *)
1435 io->scsiio.kern_data_ptr;
1436 ti = &cmd_info->cur_transfer_index;
1437 flags = atio->ccb_h.flags &
1438 (CAM_DIS_DISCONNECT|
1439 CAM_TAG_ACTION_VALID|
1443 * Set the direction, relative to the initiator.
1445 flags &= ~CAM_DIR_MASK;
1446 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1448 flags |= CAM_DIR_IN;
1450 flags |= CAM_DIR_OUT;
1452 data_ptr = sglist[*ti].addr;
1453 dxfer_len = sglist[*ti].len;
1458 if (((flags & CAM_SEND_STATUS) == 0)
1459 && (dxfer_len == 0)) {
1460 printf("%s: tag %04x no status or "
1461 "len cdb = %02x\n", __func__,
1463 atio->cdb_io.cdb_bytes[0]);
1464 printf("%s: tag %04x io status %#x\n",
1465 __func__, atio->tag_id,
1473 (flags & CAM_TAG_ACTION_VALID) ?
1474 MSG_SIMPLE_Q_TAG : 0,
1478 /*data_ptr*/ data_ptr,
1479 /*dxfer_len*/ dxfer_len,
1480 /*timeout*/ 5 * 1000);
1483 csio->ccb_h.ccb_atio = atio;
1484 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1485 softc->ctios_sent++;
1486 xpt_action((union ccb *)csio);
1489 * Release the CTIO. The ATIO will be sent back
1490 * down to the SIM once we send status.
1492 softc->ccbs_freed++;
1493 xpt_release_ccb(done_ccb);
1495 /* Call the backend move done callback */
1496 io->scsiio.be_move_done(io);
1501 case XPT_IMMEDIATE_NOTIFY: {
1503 struct ccb_immediate_notify *inot;
1507 inot = &done_ccb->cin1;
1509 softc->inots_returned++;
1511 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1513 printf("%s: got XPT_IMMEDIATE_NOTIFY status %#x tag %#x "
1514 "seq %#x\n", __func__, inot->ccb_h.status,
1515 inot->tag_id, inot->seq_id);
1517 io = ctl_alloc_io(bus_softc->fe.ctl_pool_ref);
1524 io->io_hdr.io_type = CTL_IO_TASK;
1525 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr =done_ccb;
1526 inot->ccb_h.io_ptr = io;
1527 io->io_hdr.nexus.initid.id = inot->initiator_id;
1528 io->io_hdr.nexus.targ_port = bus_softc->fe.targ_port;
1529 io->io_hdr.nexus.targ_target.id = inot->ccb_h.target_id;
1530 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1531 /* XXX KDM should this be the tag_id? */
1532 io->taskio.tag_num = inot->seq_id;
1534 status = inot->ccb_h.status & CAM_STATUS_MASK;
1536 case CAM_SCSI_BUS_RESET:
1537 io->taskio.task_action = CTL_TASK_BUS_RESET;
1540 io->taskio.task_action = CTL_TASK_TARGET_RESET;
1542 case CAM_MESSAGE_RECV:
1543 switch (inot->arg) {
1544 case MSG_ABORT_TASK_SET:
1546 * XXX KDM this isn't currently
1547 * supported by CTL. It ends up
1550 io->taskio.task_action =
1551 CTL_TASK_ABORT_TASK_SET;
1553 case MSG_TARGET_RESET:
1554 io->taskio.task_action =
1555 CTL_TASK_TARGET_RESET;
1557 case MSG_ABORT_TASK:
1558 io->taskio.task_action =
1559 CTL_TASK_ABORT_TASK;
1561 case MSG_LOGICAL_UNIT_RESET:
1562 io->taskio.task_action =
1565 case MSG_CLEAR_TASK_SET:
1567 * XXX KDM this isn't currently
1568 * supported by CTL. It ends up
1571 io->taskio.task_action =
1572 CTL_TASK_CLEAR_TASK_SET;
1575 io->taskio.task_action =
1582 xpt_print(periph->path, "%s: "
1583 "unsupported message 0x%x\n",
1584 __func__, inot->arg);
1589 case CAM_REQ_ABORTED:
1591 * This request was sent back by the driver.
1592 * XXX KDM what do we do here?
1596 case CAM_REQ_INVALID:
1597 case CAM_PROVIDE_FAIL:
1600 * We should only get here if we're talking
1601 * to a talking to a SIM that is target
1602 * capable but supports the old API. In
1603 * that case, we need to just free the CCB.
1604 * If we actually send a notify acknowledge,
1605 * it will send that back with an error as
1609 if ((status != CAM_REQ_INVALID)
1610 && (status != CAM_PROVIDE_FAIL))
1611 xpt_print(periph->path, "%s: "
1612 "unsupported CAM status "
1613 "0x%x\n", __func__, status);
1616 ctlfe_free_ccb(periph, done_ccb);
1620 if (send_ctl_io != 0) {
1624 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1625 done_ccb->ccb_h.func_code =
1626 XPT_NOTIFY_ACKNOWLEDGE;
1627 xpt_action(done_ccb);
1630 xpt_print(periph->path, "%s: could not allocate "
1631 "ctl_io for immediate notify!\n", __func__);
1632 /* requeue this to the adapter */
1633 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1634 done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1635 xpt_action(done_ccb);
1639 cam_release_devq(periph->path,
1641 /*opening reduction*/ 0,
1643 /*getcount_only*/ 0);
1647 case XPT_NOTIFY_ACKNOWLEDGE:
1649 * Queue this back down to the SIM as an immediate notify.
1651 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1652 xpt_action(done_ccb);
1653 softc->inots_sent++;
1657 * XPT_ABORT is an immediate CCB, we shouldn't get here.
1659 panic("%s: XPT_ABORT CCB returned!", __func__);
1661 case XPT_SET_SIM_KNOB:
1662 case XPT_GET_SIM_KNOB:
1665 panic("%s: unexpected CCB type %#x", __func__,
1666 done_ccb->ccb_h.func_code);
1672 ctlfe_onoffline(void *arg, int online)
1674 struct ctlfe_softc *bus_softc;
1677 struct cam_path *path;
1678 struct cam_sim *sim;
1681 bus_softc = (struct ctlfe_softc *)arg;
1685 sim = bus_softc->sim;
1687 mtx_assert(sim->mtx, MA_OWNED);
1689 status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1690 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1691 if (status != CAM_REQ_CMP) {
1692 printf("%s: unable to create path!\n", __func__);
1695 ccb = (union ccb *)malloc(sizeof(*ccb), M_TEMP, M_NOWAIT | M_ZERO);
1697 printf("%s: unable to malloc CCB!\n", __func__);
1698 xpt_free_path(path);
1701 xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1706 * Bits 63-60: 0x5 NAA, IEEE registered name
1707 * Bits 59-36: 0x000ED5 IEEE Company name assigned to Copan
1708 * Bits 35-12: Copan SSN (Sequential Serial Number)
1709 * Bits 11-8: Type of port:
1713 * Bits 7-0: 0 == Node Name, >0 == Port Number
1718 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1724 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1726 uint64_t random_bits;
1729 printf("%s: %s current WWNN %#jx\n", __func__,
1730 bus_softc->port_name,
1731 ccb->knob.xport_specific.fc.wwnn);
1732 printf("%s: %s current WWPN %#jx\n", __func__,
1733 bus_softc->port_name,
1734 ccb->knob.xport_specific.fc.wwpn);
1737 arc4rand(&random_bits, sizeof(random_bits), 0);
1741 * XXX KDM this is a bit of a kludge for now. We
1742 * take the current WWNN/WWPN from the card, and
1743 * replace the company identifier and the NL-Port
1744 * indicator and the port number (for the WWPN).
1745 * This should be replaced later with ddb_GetWWNN,
1746 * or possibly a more centralized scheme. (It
1747 * would be nice to have the WWNN/WWPN for each
1748 * port stored in the ctl_frontend structure.)
1751 ccb->knob.xport_specific.fc.wwnn =
1753 0x0000000fffffff00ULL) |
1754 /* Company ID */ 0x5000ED5000000000ULL |
1755 /* NL-Port */ 0x0300;
1756 ccb->knob.xport_specific.fc.wwpn =
1758 0x0000000fffffff00ULL) |
1759 /* Company ID */ 0x5000ED5000000000ULL |
1760 /* NL-Port */ 0x3000 |
1761 /* Port Num */ (bus_softc->fe.targ_port & 0xff);
1764 * This is a bit of an API break/reversal, but if
1765 * we're doing the random WWNN that's a little
1766 * different anyway. So record what we're actually
1767 * using with the frontend code so it's reported
1770 bus_softc->fe.wwnn =
1771 ccb->knob.xport_specific.fc.wwnn;
1772 bus_softc->fe.wwpn =
1773 ccb->knob.xport_specific.fc.wwpn;
1775 #else /* RANDOM_WWNN */
1777 * If the user has specified a WWNN/WWPN, send them
1778 * down to the SIM. Otherwise, record what the SIM
1781 if ((bus_softc->fe.wwnn != 0)
1782 && (bus_softc->fe.wwpn != 0)) {
1783 ccb->knob.xport_specific.fc.wwnn =
1785 ccb->knob.xport_specific.fc.wwpn =
1789 bus_softc->fe.wwnn =
1790 ccb->knob.xport_specific.fc.wwnn;
1791 bus_softc->fe.wwpn =
1792 ccb->knob.xport_specific.fc.wwpn;
1794 #endif /* RANDOM_WWNN */
1797 if (set_wwnn != 0) {
1798 printf("%s: %s new WWNN %#jx\n", __func__,
1799 bus_softc->port_name,
1800 ccb->knob.xport_specific.fc.wwnn);
1801 printf("%s: %s new WWPN %#jx\n", __func__,
1802 bus_softc->port_name,
1803 ccb->knob.xport_specific.fc.wwpn);
1806 printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1807 bus_softc->port_name);
1810 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1811 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1813 ccb->knob.xport_specific.valid |= KNOB_VALID_ADDRESS;
1816 ccb->knob.xport_specific.fc.role = KNOB_ROLE_TARGET;
1818 ccb->knob.xport_specific.fc.role = KNOB_ROLE_NONE;
1822 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1823 printf("%s: SIM %s (path id %d) target %s failed with "
1825 __func__, bus_softc->port_name, bus_softc->path_id,
1826 (online != 0) ? "enable" : "disable",
1829 printf("%s: SIM %s (path id %d) target %s succeeded\n",
1830 __func__, bus_softc->port_name, bus_softc->path_id,
1831 (online != 0) ? "enable" : "disable");
1834 xpt_free_path(path);
1842 ctlfe_online(void *arg)
1844 struct ctlfe_softc *bus_softc;
1845 struct cam_path *path;
1847 struct ctlfe_lun_softc *lun_softc;
1848 struct cam_sim *sim;
1850 bus_softc = (struct ctlfe_softc *)arg;
1851 sim = bus_softc->sim;
1856 * Create the wildcard LUN before bringing the port online.
1858 status = xpt_create_path(&path, /*periph*/ NULL,
1859 bus_softc->path_id, CAM_TARGET_WILDCARD,
1861 if (status != CAM_REQ_CMP) {
1862 printf("%s: unable to create path for wildcard periph\n",
1864 CAM_SIM_UNLOCK(sim);
1868 lun_softc = malloc(sizeof(*lun_softc), M_CTLFE,
1870 if (lun_softc == NULL) {
1871 xpt_print(path, "%s: unable to allocate softc for "
1872 "wildcard periph\n", __func__);
1873 xpt_free_path(path);
1874 CAM_SIM_UNLOCK(sim);
1878 lun_softc->parent_softc = bus_softc;
1879 lun_softc->flags |= CTLFE_LUN_WILDCARD;
1881 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, lun_softc, links);
1884 status = cam_periph_alloc(ctlferegister,
1895 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1896 const struct cam_status_entry *entry;
1898 entry = cam_fetch_status_entry(status);
1900 printf("%s: CAM error %s (%#x) returned from "
1901 "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1902 entry->status_text : "Unknown", status);
1905 xpt_free_path(path);
1907 ctlfe_onoffline(arg, /*online*/ 1);
1909 CAM_SIM_UNLOCK(sim);
1913 ctlfe_offline(void *arg)
1915 struct ctlfe_softc *bus_softc;
1916 struct cam_path *path;
1918 struct cam_periph *periph;
1919 struct cam_sim *sim;
1921 bus_softc = (struct ctlfe_softc *)arg;
1922 sim = bus_softc->sim;
1926 ctlfe_onoffline(arg, /*online*/ 0);
1929 * Disable the wildcard LUN for this port now that we have taken
1932 status = xpt_create_path(&path, /*periph*/ NULL,
1933 bus_softc->path_id, CAM_TARGET_WILDCARD,
1935 if (status != CAM_REQ_CMP) {
1936 CAM_SIM_UNLOCK(sim);
1937 printf("%s: unable to create path for wildcard periph\n",
1943 if ((periph = cam_periph_find(path, "ctl")) != NULL)
1944 cam_periph_invalidate(periph);
1946 xpt_free_path(path);
1948 CAM_SIM_UNLOCK(sim);
1952 ctlfe_targ_enable(void *arg, struct ctl_id targ_id)
1958 ctlfe_targ_disable(void *arg, struct ctl_id targ_id)
1964 * This will get called to enable a LUN on every bus that is attached to
1965 * CTL. So we only need to create a path/periph for this particular bus.
1968 ctlfe_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1970 struct ctlfe_softc *bus_softc;
1971 struct ctlfe_lun_softc *softc;
1972 struct cam_path *path;
1973 struct cam_periph *periph;
1974 struct cam_sim *sim;
1977 bus_softc = (struct ctlfe_softc *)arg;
1978 sim = bus_softc->sim;
1980 status = xpt_create_path_unlocked(&path, /*periph*/ NULL,
1982 targ_id.id, lun_id);
1983 /* XXX KDM need some way to return status to CTL here? */
1984 if (status != CAM_REQ_CMP) {
1985 printf("%s: could not create path, status %#x\n", __func__,
1990 softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1992 periph = cam_periph_find(path, "ctl");
1993 if (periph != NULL) {
1994 /* We've already got a periph, no need to alloc a new one. */
1995 xpt_free_path(path);
1996 free(softc, M_CTLFE);
1997 CAM_SIM_UNLOCK(sim);
2001 softc->parent_softc = bus_softc;
2002 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
2004 status = cam_periph_alloc(ctlferegister,
2015 xpt_free_path(path);
2017 CAM_SIM_UNLOCK(sim);
2023 * This will get called when the user removes a LUN to disable that LUN
2024 * on every bus that is attached to CTL.
2027 ctlfe_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
2029 struct ctlfe_softc *softc;
2030 struct ctlfe_lun_softc *lun_softc;
2031 struct cam_sim *sim;
2033 softc = (struct ctlfe_softc *)arg;
2037 STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
2038 struct cam_path *path;
2040 path = lun_softc->periph->path;
2042 if ((xpt_path_target_id(path) == targ_id.id)
2043 && (xpt_path_lun_id(path) == lun_id)) {
2047 if (lun_softc == NULL) {
2048 CAM_SIM_UNLOCK(sim);
2049 printf("%s: can't find target %d lun %d\n", __func__,
2050 targ_id.id, lun_id);
2054 cam_periph_invalidate(lun_softc->periph);
2056 CAM_SIM_UNLOCK(sim);
2062 ctlfe_dump_sim(struct cam_sim *sim)
2066 printf("%s%d: max tagged openings: %d, max dev openings: %d\n",
2067 sim->sim_name, sim->unit_number,
2068 sim->max_tagged_dev_openings, sim->max_dev_openings);
2069 printf("%s%d: max_ccbs: %u, ccb_count: %u\n",
2070 sim->sim_name, sim->unit_number,
2071 sim->max_ccbs, sim->ccb_count);
2072 printf("%s%d: ccb_freeq is %sempty\n",
2073 sim->sim_name, sim->unit_number,
2074 (SLIST_FIRST(&sim->ccb_freeq) == NULL) ? "" : "NOT ");
2075 printf("%s%d: alloc_queue.entries %d, alloc_openings %d\n",
2076 sim->sim_name, sim->unit_number,
2077 sim->devq->alloc_queue.entries, sim->devq->alloc_openings);
2078 printf("%s%d: qfrozen_cnt:", sim->sim_name, sim->unit_number);
2079 for (i = 0; i < CAM_RL_VALUES; i++) {
2080 printf("%s%u", (i != 0) ? ":" : "",
2081 sim->devq->alloc_queue.qfrozen_cnt[i]);
2087 * Assumes that the SIM lock is held.
2090 ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
2092 struct ccb_hdr *hdr;
2093 struct cam_periph *periph;
2096 periph = softc->periph;
2099 TAILQ_FOREACH(hdr, &softc->work_queue, periph_links.tqe) {
2107 * This can happen when we get an ATIO but can't allocate
2108 * a ctl_io. See the XPT_ACCEPT_TARGET_IO case in ctlfedone().
2111 struct ccb_scsiio *csio;
2113 csio = (struct ccb_scsiio *)hdr;
2115 xpt_print(periph->path, "CCB %#x ctl_io allocation "
2116 "failed\n", csio->tag_id);
2121 * Only regular SCSI I/O is put on the work
2122 * queue, so we can print sense here. There may be no
2123 * sense if it's no the queue for a DMA, but this serves to
2124 * print out the CCB as well.
2126 * XXX KDM switch this over to scsi_sense_print() when
2127 * CTL is merged in with CAM.
2129 ctl_io_error_print(io, NULL);
2132 * We're sending status back to the
2133 * initiator, so we're on the queue waiting
2134 * for a CTIO to do that.
2136 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2140 * Otherwise, we're on the queue waiting to
2141 * do a data transfer.
2143 xpt_print(periph->path, "Total %u, Current %u, Resid %u\n",
2144 io->scsiio.kern_total_len, io->scsiio.kern_data_len,
2145 io->scsiio.kern_data_resid);
2148 xpt_print(periph->path, "%d requests total waiting for CCBs\n",
2150 xpt_print(periph->path, "%ju CCBs oustanding (%ju allocated, %ju "
2151 "freed)\n", (uintmax_t)(softc->ccbs_alloced -
2152 softc->ccbs_freed), (uintmax_t)softc->ccbs_alloced,
2153 (uintmax_t)softc->ccbs_freed);
2154 xpt_print(periph->path, "%ju CTIOs outstanding (%ju sent, %ju "
2155 "returned\n", (uintmax_t)(softc->ctios_sent -
2156 softc->ctios_returned), softc->ctios_sent,
2157 softc->ctios_returned);
2161 * This function is called when we fail to get a CCB for a DMA or status return
2162 * to the initiator within the specified time period.
2164 * The callout code should insure that we hold the sim mutex here.
2167 ctlfe_dma_timeout(void *arg)
2169 struct ctlfe_lun_softc *softc;
2170 struct cam_periph *periph;
2171 struct cam_sim *sim;
2174 softc = (struct ctlfe_lun_softc *)arg;
2175 periph = softc->periph;
2176 sim = xpt_path_sim(periph->path);
2182 if (TAILQ_FIRST(&softc->work_queue) == NULL) {
2183 xpt_print(periph->path, "TIMEOUT triggered after %d "
2184 "seconds, but nothing on work queue??\n",
2189 xpt_print(periph->path, "TIMEOUT (%d seconds) waiting for DMA to "
2190 "start\n", CTLFE_DMA_TIMEOUT);
2192 ctlfe_dump_queue(softc);
2194 ctlfe_dump_sim(sim);
2196 xpt_print(periph->path, "calling xpt_schedule() to attempt to "
2197 "unstick our queue\n");
2199 xpt_schedule(periph, /*priority*/ 1);
2201 xpt_print(periph->path, "xpt_schedule() call complete\n");
2205 * Datamove/done routine called by CTL. Put ourselves on the queue to
2206 * receive a CCB from CAM so we can queue the continue I/O request down
2210 ctlfe_datamove_done(union ctl_io *io)
2213 struct cam_sim *sim;
2214 struct cam_periph *periph;
2215 struct ctlfe_lun_softc *softc;
2217 ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2219 sim = xpt_path_sim(ccb->ccb_h.path);
2223 periph = xpt_path_periph(ccb->ccb_h.path);
2225 softc = (struct ctlfe_lun_softc *)periph->softc;
2227 if (io->io_hdr.io_type == CTL_IO_TASK) {
2229 * Task management commands don't require any further
2230 * communication back to the adapter. Requeue the CCB
2231 * to the adapter, and free the CTL I/O.
2233 xpt_print(ccb->ccb_h.path, "%s: returning task I/O "
2234 "tag %#x seq %#x\n", __func__,
2235 ccb->cin1.tag_id, ccb->cin1.seq_id);
2237 * Send the notify acknowledge down to the SIM, to let it
2238 * know we processed the task management command.
2240 ccb->ccb_h.status = CAM_REQ_INPROG;
2241 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
2245 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
2246 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
2248 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
2250 TAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
2254 * Reset the timeout for our latest active DMA.
2256 callout_reset(&softc->dma_callout,
2257 CTLFE_DMA_TIMEOUT * hz,
2258 ctlfe_dma_timeout, softc);
2260 * Ask for the CAM transport layer to send us a CCB to do
2261 * the DMA or send status, unless ctlfe_dma_enabled is set
2264 if (ctlfe_dma_enabled != 0)
2265 xpt_schedule(periph, /*priority*/ 1);
2268 CAM_SIM_UNLOCK(sim);
2274 struct ctlfe_softc *bus_softc;
2276 STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
2277 struct ctlfe_lun_softc *lun_softc;
2279 ctlfe_dump_sim(bus_softc->sim);
2281 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links) {
2282 ctlfe_dump_queue(lun_softc);