2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2016 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, 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, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * USB Mass Storage Class Bulk-Only (BBB) Transport target.
34 * http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
36 * This code implements the USB Mass Storage frontend driver for the CAM
37 * Target Layer (ctl(4)) subsystem.
40 #include <sys/cdefs.h>
41 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/refcount.h>
48 #include <sys/stdint.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
57 #include <cam/scsi/scsi_all.h>
58 #include <cam/scsi/scsi_da.h>
59 #include <cam/ctl/ctl_io.h>
60 #include <cam/ctl/ctl.h>
61 #include <cam/ctl/ctl_backend.h>
62 #include <cam/ctl/ctl_error.h>
63 #include <cam/ctl/ctl_frontend.h>
64 #include <cam/ctl/ctl_debug.h>
65 #include <cam/ctl/ctl_ha.h>
66 #include <cam/ctl/ctl_ioctl.h>
67 #include <cam/ctl/ctl_private.h>
69 SYSCTL_NODE(_hw_usb, OID_AUTO, cfumass, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
70 "CAM Target Layer USB Mass Storage Frontend");
72 SYSCTL_INT(_hw_usb_cfumass, OID_AUTO, debug, CTLFLAG_RWTUN,
73 &debug, 1, "Enable debug messages");
74 static int max_lun = 0;
75 SYSCTL_INT(_hw_usb_cfumass, OID_AUTO, max_lun, CTLFLAG_RWTUN,
76 &max_lun, 1, "Maximum advertised LUN number");
77 static int ignore_stop = 1;
78 SYSCTL_INT(_hw_usb_cfumass, OID_AUTO, ignore_stop, CTLFLAG_RWTUN,
79 &ignore_stop, 1, "Ignore START STOP UNIT with START and LOEJ bits cleared");
82 * The driver uses a single, global CTL port. It could create its ports
83 * in cfumass_attach() instead, but that would make it impossible to specify
84 * "port cfumass0" in ctl.conf(5), as the port generally wouldn't exist
85 * at the time ctld(8) gets run.
87 struct ctl_port cfumass_port;
88 bool cfumass_port_online;
89 volatile u_int cfumass_refcount;
91 #ifndef CFUMASS_BULK_SIZE
92 #define CFUMASS_BULK_SIZE (1U << 17) /* bytes */
96 * USB transfer definitions.
98 #define CFUMASS_T_COMMAND 0
99 #define CFUMASS_T_DATA_OUT 1
100 #define CFUMASS_T_DATA_IN 2
101 #define CFUMASS_T_STATUS 3
102 #define CFUMASS_T_MAX 4
105 * USB interface specific control requests.
107 #define UR_RESET 0xff /* Bulk-Only Mass Storage Reset */
108 #define UR_GET_MAX_LUN 0xfe /* Get Max LUN */
111 * Command Block Wrapper.
113 struct cfumass_cbw_t {
114 uDWord dCBWSignature;
115 #define CBWSIGNATURE 0x43425355 /* "USBC" */
117 uDWord dCBWDataTransferLength;
119 #define CBWFLAGS_OUT 0x00
120 #define CBWFLAGS_IN 0x80
123 #define CBWCBLENGTH 16
124 uByte CBWCB[CBWCBLENGTH];
127 #define CFUMASS_CBW_SIZE 31
128 CTASSERT(sizeof(struct cfumass_cbw_t) == CFUMASS_CBW_SIZE);
131 * Command Status Wrapper.
133 struct cfumass_csw_t {
134 uDWord dCSWSignature;
135 #define CSWSIGNATURE 0x53425355 /* "USBS" */
137 uDWord dCSWDataResidue;
139 #define CSWSTATUS_GOOD 0x0
140 #define CSWSTATUS_FAILED 0x1
141 #define CSWSTATUS_PHASE 0x2
144 #define CFUMASS_CSW_SIZE 13
145 CTASSERT(sizeof(struct cfumass_csw_t) == CFUMASS_CSW_SIZE);
147 struct cfumass_softc {
149 struct usb_device *sc_udev;
150 struct usb_xfer *sc_xfer[CFUMASS_T_MAX];
152 struct cfumass_cbw_t *sc_cbw;
153 struct cfumass_csw_t *sc_csw;
160 * This is used to communicate between CTL callbacks
161 * and USB callbacks; basically, it holds the state
162 * for the current command ("the" command, since there
163 * is no queueing in USB Mass Storage).
165 bool sc_current_stalled;
168 * The following are set upon receiving a SCSI command.
171 int sc_current_transfer_length;
172 int sc_current_flags;
175 * The following are set in ctl_datamove().
177 int sc_current_residue;
178 union ctl_io *sc_ctl_io;
181 * The following is set in cfumass_done().
183 int sc_current_status;
186 * Number of requests queued to CTL.
188 volatile u_int sc_queued;
194 static device_probe_t cfumass_probe;
195 static device_attach_t cfumass_attach;
196 static device_detach_t cfumass_detach;
197 static device_suspend_t cfumass_suspend;
198 static device_resume_t cfumass_resume;
199 static usb_handle_request_t cfumass_handle_request;
201 static usb_callback_t cfumass_t_command_callback;
202 static usb_callback_t cfumass_t_data_callback;
203 static usb_callback_t cfumass_t_status_callback;
205 static device_method_t cfumass_methods[] = {
207 DEVMETHOD(usb_handle_request, cfumass_handle_request),
209 /* Device interface. */
210 DEVMETHOD(device_probe, cfumass_probe),
211 DEVMETHOD(device_attach, cfumass_attach),
212 DEVMETHOD(device_detach, cfumass_detach),
213 DEVMETHOD(device_suspend, cfumass_suspend),
214 DEVMETHOD(device_resume, cfumass_resume),
219 static driver_t cfumass_driver = {
221 .methods = cfumass_methods,
222 .size = sizeof(struct cfumass_softc),
225 DRIVER_MODULE(cfumass, uhub, cfumass_driver, NULL, NULL);
226 MODULE_VERSION(cfumass, 0);
227 MODULE_DEPEND(cfumass, usb, 1, 1, 1);
228 MODULE_DEPEND(cfumass, usb_template, 1, 1, 1);
230 static struct usb_config cfumass_config[CFUMASS_T_MAX] = {
231 [CFUMASS_T_COMMAND] = {
233 .endpoint = UE_ADDR_ANY,
234 .direction = UE_DIR_OUT,
235 .bufsize = sizeof(struct cfumass_cbw_t),
236 .callback = &cfumass_t_command_callback,
237 .usb_mode = USB_MODE_DEVICE,
240 [CFUMASS_T_DATA_OUT] = {
242 .endpoint = UE_ADDR_ANY,
243 .direction = UE_DIR_OUT,
244 .bufsize = CFUMASS_BULK_SIZE,
245 .flags = {.proxy_buffer = 1, .short_xfer_ok = 1,
247 .callback = &cfumass_t_data_callback,
248 .usb_mode = USB_MODE_DEVICE,
251 [CFUMASS_T_DATA_IN] = {
253 .endpoint = UE_ADDR_ANY,
254 .direction = UE_DIR_IN,
255 .bufsize = CFUMASS_BULK_SIZE,
256 .flags = {.proxy_buffer = 1, .short_xfer_ok = 1,
258 .callback = &cfumass_t_data_callback,
259 .usb_mode = USB_MODE_DEVICE,
262 [CFUMASS_T_STATUS] = {
264 .endpoint = UE_ADDR_ANY,
265 .direction = UE_DIR_IN,
266 .bufsize = sizeof(struct cfumass_csw_t),
267 .flags = {.short_xfer_ok = 1},
268 .callback = &cfumass_t_status_callback,
269 .usb_mode = USB_MODE_DEVICE,
274 * CTL frontend interface.
276 static int cfumass_init(void);
277 static int cfumass_shutdown(void);
278 static void cfumass_online(void *arg);
279 static void cfumass_offline(void *arg);
280 static void cfumass_datamove(union ctl_io *io);
281 static void cfumass_done(union ctl_io *io);
283 static struct ctl_frontend cfumass_frontend = {
285 .init = cfumass_init,
286 .shutdown = cfumass_shutdown,
288 CTL_FRONTEND_DECLARE(ctlcfumass, cfumass_frontend);
290 #define CFUMASS_DEBUG(S, X, ...) \
293 device_printf(S->sc_dev, "%s: " X "\n", \
294 __func__, ## __VA_ARGS__); \
298 #define CFUMASS_WARN(S, X, ...) \
301 device_printf(S->sc_dev, "WARNING: %s: " X "\n",\
302 __func__, ## __VA_ARGS__); \
306 #define CFUMASS_LOCK(X) mtx_lock(&X->sc_mtx)
307 #define CFUMASS_UNLOCK(X) mtx_unlock(&X->sc_mtx)
309 static void cfumass_transfer_start(struct cfumass_softc *sc,
311 static void cfumass_terminate(struct cfumass_softc *sc);
314 cfumass_probe(device_t dev)
316 struct usb_attach_arg *uaa;
317 struct usb_interface_descriptor *id;
319 uaa = device_get_ivars(dev);
321 if (uaa->usb_mode != USB_MODE_DEVICE)
325 * Check for a compliant device.
327 id = usbd_get_interface_descriptor(uaa->iface);
329 (id->bInterfaceClass != UICLASS_MASS) ||
330 (id->bInterfaceSubClass != UISUBCLASS_SCSI) ||
331 (id->bInterfaceProtocol != UIPROTO_MASS_BBB)) {
335 return (BUS_PROBE_GENERIC);
339 cfumass_attach(device_t dev)
341 struct cfumass_softc *sc;
342 struct usb_attach_arg *uaa;
345 sc = device_get_softc(dev);
346 uaa = device_get_ivars(dev);
349 sc->sc_udev = uaa->device;
351 CFUMASS_DEBUG(sc, "go");
353 usbd_set_power_mode(uaa->device, USB_POWER_MODE_SAVE);
354 device_set_usb_desc(dev);
356 mtx_init(&sc->sc_mtx, "cfumass", NULL, MTX_DEF);
357 refcount_acquire(&cfumass_refcount);
359 error = usbd_transfer_setup(uaa->device,
360 &uaa->info.bIfaceIndex, sc->sc_xfer, cfumass_config,
361 CFUMASS_T_MAX, sc, &sc->sc_mtx);
363 CFUMASS_WARN(sc, "usbd_transfer_setup() failed: %s",
365 refcount_release(&cfumass_refcount);
370 usbd_xfer_get_frame_buffer(sc->sc_xfer[CFUMASS_T_COMMAND], 0);
372 usbd_xfer_get_frame_buffer(sc->sc_xfer[CFUMASS_T_STATUS], 0);
374 sc->sc_ctl_initid = ctl_add_initiator(&cfumass_port, -1, 0, NULL);
375 if (sc->sc_ctl_initid < 0) {
376 CFUMASS_WARN(sc, "ctl_add_initiator() failed with error %d",
378 usbd_transfer_unsetup(sc->sc_xfer, CFUMASS_T_MAX);
379 refcount_release(&cfumass_refcount);
383 refcount_init(&sc->sc_queued, 0);
386 cfumass_transfer_start(sc, CFUMASS_T_COMMAND);
393 cfumass_detach(device_t dev)
395 struct cfumass_softc *sc;
398 sc = device_get_softc(dev);
400 CFUMASS_DEBUG(sc, "go");
403 cfumass_terminate(sc);
405 usbd_transfer_unsetup(sc->sc_xfer, CFUMASS_T_MAX);
407 if (sc->sc_ctl_initid != -1) {
408 error = ctl_remove_initiator(&cfumass_port, sc->sc_ctl_initid);
410 CFUMASS_WARN(sc, "ctl_remove_initiator() failed "
411 "with error %d", error);
413 sc->sc_ctl_initid = -1;
416 mtx_destroy(&sc->sc_mtx);
417 refcount_release(&cfumass_refcount);
423 cfumass_suspend(device_t dev)
425 struct cfumass_softc *sc;
427 sc = device_get_softc(dev);
428 CFUMASS_DEBUG(sc, "go");
434 cfumass_resume(device_t dev)
436 struct cfumass_softc *sc;
438 sc = device_get_softc(dev);
439 CFUMASS_DEBUG(sc, "go");
445 cfumass_transfer_start(struct cfumass_softc *sc, uint8_t xfer_index)
448 usbd_transfer_start(sc->sc_xfer[xfer_index]);
452 cfumass_transfer_stop_and_drain(struct cfumass_softc *sc, uint8_t xfer_index)
455 usbd_transfer_stop(sc->sc_xfer[xfer_index]);
457 usbd_transfer_drain(sc->sc_xfer[xfer_index]);
462 cfumass_terminate(struct cfumass_softc *sc)
467 cfumass_transfer_stop_and_drain(sc, CFUMASS_T_COMMAND);
468 cfumass_transfer_stop_and_drain(sc, CFUMASS_T_DATA_IN);
469 cfumass_transfer_stop_and_drain(sc, CFUMASS_T_DATA_OUT);
471 if (sc->sc_ctl_io != NULL) {
472 CFUMASS_DEBUG(sc, "terminating CTL transfer");
473 ctl_set_data_phase_error(&sc->sc_ctl_io->scsiio);
474 ctl_datamove_done(sc->sc_ctl_io, false);
475 sc->sc_ctl_io = NULL;
478 cfumass_transfer_stop_and_drain(sc, CFUMASS_T_STATUS);
480 refcount_acquire(&sc->sc_queued);
481 last = refcount_release(&sc->sc_queued);
485 CFUMASS_DEBUG(sc, "%d CTL tasks pending", sc->sc_queued);
486 msleep(__DEVOLATILE(void *, &sc->sc_queued), &sc->sc_mtx,
487 0, "cfumass_reset", hz / 100);
492 cfumass_handle_request(device_t dev,
493 const void *preq, void **pptr, uint16_t *plen,
494 uint16_t offset, uint8_t *pstate)
496 static uint8_t max_lun_tmp;
497 struct cfumass_softc *sc;
498 const struct usb_device_request *req;
501 sc = device_get_softc(dev);
503 is_complete = *pstate;
505 CFUMASS_DEBUG(sc, "go");
510 if ((req->bmRequestType == UT_WRITE_CLASS_INTERFACE) &&
511 (req->bRequest == UR_RESET)) {
512 CFUMASS_WARN(sc, "received Bulk-Only Mass Storage Reset");
516 cfumass_terminate(sc);
517 cfumass_transfer_start(sc, CFUMASS_T_COMMAND);
520 CFUMASS_DEBUG(sc, "Bulk-Only Mass Storage Reset done");
524 if ((req->bmRequestType == UT_READ_CLASS_INTERFACE) &&
525 (req->bRequest == UR_GET_MAX_LUN)) {
526 CFUMASS_DEBUG(sc, "received Get Max LUN");
530 * The protocol doesn't support LUN numbers higher
531 * than 15. Also, some initiators (namely Windows XP
532 * SP3 Version 2002) can't properly query the number
533 * of LUNs, resulting in inaccessible "fake" ones - thus
534 * the default limit of one LUN.
536 if (max_lun < 0 || max_lun > 15) {
538 "invalid hw.usb.cfumass.max_lun, must be "
539 "between 0 and 15; defaulting to 0");
542 max_lun_tmp = max_lun;
544 *pptr = &max_lun_tmp;
555 cfumass_quirk(struct cfumass_softc *sc, unsigned char *cdb, int cdb_len)
557 struct scsi_start_stop_unit *sssu;
560 case START_STOP_UNIT:
562 * Some initiators - eg OSX, Darwin Kernel Version 15.6.0,
563 * root:xnu-3248.60.11~2/RELEASE_X86_64 - attempt to stop
564 * the unit on eject, but fail to start it when it's plugged
565 * back. Just ignore the command.
568 if (cdb_len < sizeof(*sssu)) {
569 CFUMASS_DEBUG(sc, "received START STOP UNIT with "
570 "bCDBLength %d, should be %zd",
571 cdb_len, sizeof(*sssu));
575 sssu = (struct scsi_start_stop_unit *)cdb;
576 if ((sssu->how & SSS_PC_MASK) != 0)
579 if ((sssu->how & SSS_START) != 0)
582 if ((sssu->how & SSS_LOEJ) != 0)
585 if (ignore_stop == 0) {
587 } else if (ignore_stop == 1) {
588 CFUMASS_WARN(sc, "ignoring START STOP UNIT request");
590 CFUMASS_DEBUG(sc, "ignoring START STOP UNIT request");
593 sc->sc_current_status = 0;
594 cfumass_transfer_start(sc, CFUMASS_T_STATUS);
605 cfumass_t_command_callback(struct usb_xfer *xfer, usb_error_t usb_error)
607 struct cfumass_softc *sc;
612 sc = usbd_xfer_softc(xfer);
614 KASSERT(sc->sc_ctl_io == NULL,
615 ("sc_ctl_io is %p, should be NULL", sc->sc_ctl_io));
617 switch (USB_GET_STATE(xfer)) {
618 case USB_ST_TRANSFERRED:
619 CFUMASS_DEBUG(sc, "USB_ST_TRANSFERRED");
621 signature = UGETDW(sc->sc_cbw->dCBWSignature);
622 if (signature != CBWSIGNATURE) {
623 CFUMASS_WARN(sc, "wrong dCBWSignature 0x%08x, "
624 "should be 0x%08x", signature, CBWSIGNATURE);
628 if (sc->sc_cbw->bCDBLength <= 0 ||
629 sc->sc_cbw->bCDBLength > sizeof(sc->sc_cbw->CBWCB)) {
630 CFUMASS_WARN(sc, "invalid bCDBLength %d, should be <= %zd",
631 sc->sc_cbw->bCDBLength, sizeof(sc->sc_cbw->CBWCB));
635 sc->sc_current_stalled = false;
636 sc->sc_current_status = 0;
637 sc->sc_current_tag = UGETDW(sc->sc_cbw->dCBWTag);
638 sc->sc_current_transfer_length =
639 UGETDW(sc->sc_cbw->dCBWDataTransferLength);
640 sc->sc_current_flags = sc->sc_cbw->bCBWFlags;
643 * Make sure to report proper residue if the datamove wasn't
644 * required, or wasn't called due to SCSI error.
646 sc->sc_current_residue = sc->sc_current_transfer_length;
648 if (cfumass_quirk(sc,
649 sc->sc_cbw->CBWCB, sc->sc_cbw->bCDBLength) != 0)
652 if (!cfumass_port_online) {
653 CFUMASS_DEBUG(sc, "cfumass port is offline; stalling");
654 usbd_xfer_set_stall(xfer);
659 * Those CTL functions cannot be called with mutex held.
662 io = ctl_alloc_io(cfumass_port.ctl_pool_ref);
664 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = sc;
665 io->io_hdr.io_type = CTL_IO_SCSI;
666 io->io_hdr.nexus.initid = sc->sc_ctl_initid;
667 io->io_hdr.nexus.targ_port = cfumass_port.targ_port;
668 io->io_hdr.nexus.targ_lun = ctl_decode_lun(sc->sc_cbw->bCBWLUN);
669 io->scsiio.tag_num = UGETDW(sc->sc_cbw->dCBWTag);
670 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
671 io->scsiio.cdb_len = sc->sc_cbw->bCDBLength;
672 memcpy(io->scsiio.cdb, sc->sc_cbw->CBWCB, sc->sc_cbw->bCDBLength);
673 refcount_acquire(&sc->sc_queued);
674 error = ctl_queue(io);
675 if (error != CTL_RETVAL_COMPLETE) {
677 "ctl_queue() failed; error %d; stalling", error);
679 refcount_release(&sc->sc_queued);
681 usbd_xfer_set_stall(xfer);
690 CFUMASS_DEBUG(sc, "USB_ST_SETUP");
692 usbd_xfer_set_frame_len(xfer, 0, sizeof(*sc->sc_cbw));
693 usbd_transfer_submit(xfer);
697 if (usb_error == USB_ERR_CANCELLED) {
698 CFUMASS_DEBUG(sc, "USB_ERR_CANCELLED");
702 CFUMASS_DEBUG(sc, "USB_ST_ERROR: %s", usbd_errstr(usb_error));
709 cfumass_t_data_callback(struct usb_xfer *xfer, usb_error_t usb_error)
711 struct cfumass_softc *sc = usbd_xfer_softc(xfer);
712 union ctl_io *io = sc->sc_ctl_io;
714 struct ctl_sg_entry sg_entry, *sglist;
715 int actlen, sumlen, sg_count;
717 switch (USB_GET_STATE(xfer)) {
718 case USB_ST_TRANSFERRED:
719 CFUMASS_DEBUG(sc, "USB_ST_TRANSFERRED");
721 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
722 sc->sc_current_residue -= actlen;
723 io->scsiio.ext_data_filled += actlen;
724 io->scsiio.kern_data_resid -= actlen;
725 if (actlen < sumlen ||
726 sc->sc_current_residue == 0 ||
727 io->scsiio.kern_data_resid == 0) {
728 sc->sc_ctl_io = NULL;
729 ctl_datamove_done(io, false);
736 CFUMASS_DEBUG(sc, "USB_ST_SETUP");
738 if (io->scsiio.kern_sg_entries > 0) {
739 sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
740 sg_count = io->scsiio.kern_sg_entries;
743 sglist->addr = io->scsiio.kern_data_ptr;
744 sglist->len = io->scsiio.kern_data_len;
748 sumlen = io->scsiio.ext_data_filled -
749 io->scsiio.kern_rel_offset;
750 while (sumlen >= sglist->len && sg_count > 0) {
751 sumlen -= sglist->len;
755 KASSERT(sg_count > 0, ("Run out of S/G list entries"));
757 max_bulk = usbd_xfer_max_len(xfer);
758 actlen = min(sglist->len - sumlen, max_bulk);
759 actlen = min(actlen, sc->sc_current_transfer_length -
760 io->scsiio.ext_data_filled);
761 CFUMASS_DEBUG(sc, "requested %d, done %d, max_bulk %d, "
762 "segment %zd => transfer %d",
763 sc->sc_current_transfer_length, io->scsiio.ext_data_filled,
764 max_bulk, sglist->len - sumlen, actlen);
766 usbd_xfer_set_frame_data(xfer, 0,
767 (uint8_t *)sglist->addr + sumlen, actlen);
768 usbd_transfer_submit(xfer);
772 if (usb_error == USB_ERR_CANCELLED) {
773 CFUMASS_DEBUG(sc, "USB_ERR_CANCELLED");
776 CFUMASS_DEBUG(sc, "USB_ST_ERROR: %s", usbd_errstr(usb_error));
782 cfumass_t_status_callback(struct usb_xfer *xfer, usb_error_t usb_error)
784 struct cfumass_softc *sc;
786 sc = usbd_xfer_softc(xfer);
788 KASSERT(sc->sc_ctl_io == NULL,
789 ("sc_ctl_io is %p, should be NULL", sc->sc_ctl_io));
791 switch (USB_GET_STATE(xfer)) {
792 case USB_ST_TRANSFERRED:
793 CFUMASS_DEBUG(sc, "USB_ST_TRANSFERRED");
795 cfumass_transfer_start(sc, CFUMASS_T_COMMAND);
800 CFUMASS_DEBUG(sc, "USB_ST_SETUP");
802 if (sc->sc_current_residue > 0 && !sc->sc_current_stalled) {
803 CFUMASS_DEBUG(sc, "non-zero residue, stalling");
804 usbd_xfer_set_stall(xfer);
805 sc->sc_current_stalled = true;
808 USETDW(sc->sc_csw->dCSWSignature, CSWSIGNATURE);
809 USETDW(sc->sc_csw->dCSWTag, sc->sc_current_tag);
810 USETDW(sc->sc_csw->dCSWDataResidue, sc->sc_current_residue);
811 sc->sc_csw->bCSWStatus = sc->sc_current_status;
813 usbd_xfer_set_frame_len(xfer, 0, sizeof(*sc->sc_csw));
814 usbd_transfer_submit(xfer);
818 if (usb_error == USB_ERR_CANCELLED) {
819 CFUMASS_DEBUG(sc, "USB_ERR_CANCELLED");
823 CFUMASS_DEBUG(sc, "USB_ST_ERROR: %s",
824 usbd_errstr(usb_error));
831 cfumass_online(void *arg __unused)
834 cfumass_port_online = true;
838 cfumass_offline(void *arg __unused)
841 cfumass_port_online = false;
845 cfumass_datamove(union ctl_io *io)
847 struct cfumass_softc *sc;
849 sc = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
851 CFUMASS_DEBUG(sc, "go");
855 KASSERT(sc->sc_ctl_io == NULL,
856 ("sc_ctl_io is %p, should be NULL", sc->sc_ctl_io));
859 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
861 * Verify that CTL wants us to send the data in the direction
862 * expected by the initiator.
864 if (sc->sc_current_flags != CBWFLAGS_IN) {
865 CFUMASS_WARN(sc, "wrong bCBWFlags 0x%x, should be 0x%x",
866 sc->sc_current_flags, CBWFLAGS_IN);
870 cfumass_transfer_start(sc, CFUMASS_T_DATA_IN);
872 if (sc->sc_current_flags != CBWFLAGS_OUT) {
873 CFUMASS_WARN(sc, "wrong bCBWFlags 0x%x, should be 0x%x",
874 sc->sc_current_flags, CBWFLAGS_OUT);
878 cfumass_transfer_start(sc, CFUMASS_T_DATA_OUT);
885 ctl_set_data_phase_error(&io->scsiio);
886 ctl_datamove_done(io, true);
887 sc->sc_ctl_io = NULL;
891 cfumass_done(union ctl_io *io)
893 struct cfumass_softc *sc;
895 sc = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
897 CFUMASS_DEBUG(sc, "go");
899 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
900 ("invalid CTL status %#x", io->io_hdr.status));
901 KASSERT(sc->sc_ctl_io == NULL,
902 ("sc_ctl_io is %p, should be NULL", sc->sc_ctl_io));
904 if (io->io_hdr.io_type == CTL_IO_TASK &&
905 io->taskio.task_action == CTL_TASK_I_T_NEXUS_RESET) {
907 * Implicit task termination has just completed; nothing to do.
914 * Do not return status for aborted commands.
915 * There are exceptions, but none supported by CTL yet.
917 if (((io->io_hdr.flags & CTL_FLAG_ABORT) &&
918 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) ||
919 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT)) {
924 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
925 sc->sc_current_status = 0;
927 sc->sc_current_status = 1;
929 /* XXX: How should we report BUSY, RESERVATION CONFLICT, etc? */
930 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR &&
931 io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)
937 cfumass_transfer_start(sc, CFUMASS_T_STATUS);
940 refcount_release(&sc->sc_queued);
948 cfumass_port.frontend = &cfumass_frontend;
949 cfumass_port.port_type = CTL_PORT_UMASS;
950 cfumass_port.num_requested_ctl_io = 1;
951 cfumass_port.port_name = "cfumass";
952 cfumass_port.physical_port = 0;
953 cfumass_port.virtual_port = 0;
954 cfumass_port.port_online = cfumass_online;
955 cfumass_port.port_offline = cfumass_offline;
956 cfumass_port.onoff_arg = NULL;
957 cfumass_port.fe_datamove = cfumass_datamove;
958 cfumass_port.fe_done = cfumass_done;
959 cfumass_port.targ_port = -1;
961 error = ctl_port_register(&cfumass_port);
963 printf("%s: ctl_port_register() failed "
964 "with error %d", __func__, error);
967 cfumass_port_online = true;
968 refcount_init(&cfumass_refcount, 0);
974 cfumass_shutdown(void)
978 if (cfumass_refcount > 0) {
980 printf("%s: still have %u attachments; "
981 "returning EBUSY\n", __func__, cfumass_refcount);
986 error = ctl_port_deregister(&cfumass_port);
988 printf("%s: ctl_port_deregister() failed "
989 "with error %d\n", __func__, error);