diff --git a/sys/dev/usb/umass.c b/sys/dev/usb/umass.c index 82c19bed853d..61d3c22b8918 100644 --- a/sys/dev/usb/umass.c +++ b/sys/dev/usb/umass.c @@ -1,6 +1,6 @@ /*- * Copyright (c) 1999 MAEKAWA Masahide , - * Nick Hibma + * Nick Hibma * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,149 +30,621 @@ /* * Universal Serial Bus Mass Storage Class Bulk-Only Transport * http://www.usb.org/developers/usbmassbulk_09.pdf + * XXX Add URL to CBI spec in www.usb.org + */ + +/* + * The driver handles 3 Wire Protocols + * - Command/Bulk/Interrupt (CBI) + * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI) + * - Mass Storage Bulk-Only (BBB) + * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases) * - * Relevant parts have been quoted in the source. + * Over these wire protocols it handles the following command protocols + * - SCSI + * - UFI (floppy command set) + * - 8070i (ATA/ATAPI) + * + * UFI and 8070i are transformed versions of the SCSI command set. The + * sc->transform method is used to convert the commands into the appropriate + * format (if at all necessary). For example, UFI requires all commands to be + * 12 bytes in length amongst other things. + * + * The source code below is marked and can be split into a number of pieces + * (in this order): + * + * - probe/attach/detach + * - generic transfer routines + * - BBB + * - CBI + * - CBI_I (in addition to functions from CBI) + * - CAM (Common Access Method) + * - SCSI + * - UFI + * - 8070i + * + * The protocols are implemented using a state machine, for the transfers as + * well as for the resets. The state machine is contained in umass_*_state. + * The state machine is started through either umass_*_transfer or + * umass_*_reset. + * + * The reason for doing this is a) CAM performs a lot better this way and b) it + * avoids using tsleep from interrupt context (for example after a failed + * transfer). */ -/* To do: - * x The umass_usb_transfer routine uses synchroneous transfers. This - * should be changed to async and state handling. +/* + * The SCSI related part of this driver has been derived from the + * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org). + * + * The CAM layer uses so called actions which are messages sent to the host + * adapter for completion. The actions come in through umass_cam_action. The + * appropriate block of routines is called depending on the transport protocol + * in use. When the transfer has finished, these routines call + * umass_cam_cb again to complete the CAM command. */ -/* Authors: (with short acronyms for comments) - * NWH - Nick Hibma +/* XXX Should we split the driver into a number of files? umass.c, + * umass_scsi.c, umass_8070.c, umass_ufi.c, umass_bbb.c, umass_cbi.c or + * something similar? */ -#include "opt_usb.h" - #include #include #include #include #include -#include #include #include #include #include #include +#include #include #include #include #include +#include -#ifdef UMASS_DEBUG -#define DPRINTF(m, x) if (umassdebug & (m)) logprintf x -#define UDMASS_CAM 0x00010000 -#define UDMASS_SCSI 0x00020000 -#define UDMASS_USB 0x00040000 -#define UDMASS_BULK 0x00080000 -#define UDMASS_ALL 0xffff0000 -int umassdebug = UDMASS_CAM|UDMASS_BULK|UDMASS_USB; -#else -#define DPRINTF(m, x) +#ifdef UMASS_DO_CAM_RESCAN +#include +#include #endif -typedef struct umass_softc { - USBBASEDEVICE sc_dev; /* base device */ - usbd_interface_handle sc_iface; /* the interface we use */ +#ifdef UMASS_DEBUG +#define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0) +#define DPRINTF(m, x) if (umassdebug & (m)) logprintf x +#define UDMASS_GEN 0x00010000 /* general */ +#define UDMASS_SCSI 0x00020000 /* scsi */ +#define UDMASS_UFI 0x00040000 /* ufi command set */ +#define UDMASS_8070 0x00080000 /* 8070i command set */ +#define UDMASS_USB 0x00100000 /* USB general */ +#define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */ +#define UDMASS_CBI 0x00400000 /* CBI transfers */ +#define UDMASS_ALL 0xffff0000 /* all of the above */ +int umassdebug = UDMASS_ALL; +#else +#define DIF(m, x) /* nop */ +#define DPRINTF(m, x) /* nop */ +#endif - u_int8_t sc_bulkout; /* bulk-out Endpoint Address */ - usbd_pipe_handle sc_bulkout_pipe; - u_int8_t sc_bulkin; /* bulk-in Endpoint Address */ - usbd_pipe_handle sc_bulkin_pipe; - struct cam_sim *sim; /* SCSI Interface Module */ - struct cam_path *path; /* XPT path */ -} umass_softc_t; +/* Generic definitions */ -#define USBD_COMMAND_FAILED USBD_INVAL /* redefine some errors for */ - -#define UPROTO_MASS_ZIP 0x50 /* letter 'P' for protoype */ - -#define UMASS_SCSIID_HOST 0x00 -#define UMASS_SCSIID_DEVICE 0x01 - -#define DIR_OUT 0 +/* Direction for umass_*_transfer */ +#define DIR_NONE 0 #define DIR_IN 1 -#define DIR_NONE 2 +#define DIR_OUT 2 -/* Bulk-Only specific request */ -#define UR_RESET 0xff +/* device name */ +#define DEVNAME "umass" +#define DEVNAME_SIM "umass-" + +#define UMASS_MAX_TRANSFER_SIZE 65536 +/* The transfer speed determines the timeout value */ +#define UMASS_DEFAULT_TRANSFER_SPEED 150 /* in kb/s, conservative est. */ +#define UMASS_FLOPPY_TRANSFER_SPEED 20 +#define UMASS_ZIP100_TRANSFER_SPEED 650 + +/* CAM specific definitions */ + +/* The bus id, whatever that is */ +#define UMASS_SCSI_BUS 0 + +/* All USB drives are 'connected' to one SIM (SCSI controller). umass3 + * ends up being target 3 on that SIM. When a request for target 3 + * comes in we fetch the softc with devclass_get_softc(target_id). + * + * The SIM is the highest target number. This makes sure that umass0 corresponds + * to target 0 on the USB SCSI bus. + */ +#ifndef UMASS_DEBUG +#define UMASS_SCSIID_MAX 32 /* maximum number of drives expected */ +#else +/* while debugging avoid unnecessary clutter in the output at umass_cam_rescan + * (XPT_PATH_INQ) + */ +#define UMASS_SCSIID_MAX 3 /* maximum number of drives expected */ +#endif +#define UMASS_SCSIID_HOST UMASS_SCSIID_MAX + +#define MS_TO_TICKS(ms) ((ms) * hz / 1000) + + +/* Bulk-Only features */ + +#define UR_BBB_RESET 0xff /* Bulk-Only reset */ -/* Bulk-Only Mass Storage features */ /* Command Block Wrapper */ typedef struct { uDWord dCBWSignature; -#define CBWSIGNATURE 0x43425355 +# define CBWSIGNATURE 0x43425355 uDWord dCBWTag; uDWord dCBWDataTransferLength; uByte bCBWFlags; -#define CBWFLAGS_OUT 0x00 -#define CBWFLAGS_IN 0x80 +# define CBWFLAGS_OUT 0x00 +# define CBWFLAGS_IN 0x80 uByte bCBWLUN; uByte bCDBLength; - uByte CBWCDB[16]; -} usb_bulk_cbw_t; -#define USB_BULK_CBW_SIZE 31 +# define CBWCDBLENGTH 16 + uByte CBWCDB[CBWCDBLENGTH]; +} umass_bbb_cbw_t; +#define UMASS_BBB_CBW_SIZE 31 /* Command Status Wrapper */ typedef struct { uDWord dCSWSignature; -#define CSWSIGNATURE 0x53425355 +# define CSWSIGNATURE 0x53425355 uDWord dCSWTag; uDWord dCSWDataResidue; uByte bCSWStatus; -#define CSWSTATUS_GOOD 0x0 -#define CSWSTATUS_FAILED 0x1 -#define CSWSTATUS_PHASE 0x2 -} usb_bulk_csw_t; -#define USB_BULK_CSW_SIZE 13 +# define CSWSTATUS_GOOD 0x0 +# define CSWSTATUS_FAILED 0x1 +# define CSWSTATUS_PHASE 0x2 +} umass_bbb_csw_t; +#define UMASS_BBB_CSW_SIZE 13 + +/* CBI features */ + +#define UR_CBI_ADSC 0x00 + +typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */ + +typedef union { + struct { + unsigned char type; + #define IDB_TYPE_CCI 0x00 + unsigned char value; + #define IDB_VALUE_PASS 0x00 + #define IDB_VALUE_FAIL 0x01 + #define IDB_VALUE_PHASE 0x02 + #define IDB_VALUE_PERSISTENT 0x03 + #define IDB_VALUE_STATUS_MASK 0x03 + } common; + + struct { + unsigned char asc; + unsigned char ascq; + } ufi; +} umass_cbi_sbl_t; + +struct umass_softc; /* see below */ + +typedef void (*transfer_cb_f) __P((struct umass_softc *sc, void *priv, + int residue, int status)); +#define STATUS_CMD_OK 0 /* everything ok */ +#define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */ +#define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */ +#define STATUS_WIRE_FAILED 3 /* couldn't even get command across */ + +typedef void (*wire_reset_f) __P((struct umass_softc *sc, int status)); +typedef void (*wire_transfer_f) __P((struct umass_softc *sc, int lun, + void *cmd, int cmdlen, void *data, int datalen, + int dir, transfer_cb_f cb, void *priv)); +typedef void (*wire_state_f) __P((usbd_xfer_handle xfer, + usbd_private_handle priv, usbd_status err)); + +typedef int (*command_transform_f) __P((struct umass_softc *sc, + unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen)); + + +/* the per device structure */ +struct umass_softc { + USBBASEDEVICE sc_dev; /* base device */ + unsigned char flags; /* various device flags */ +# define UMASS_FLAGS_GONE 0x01 /* devices is no more */ + + unsigned char drive; +# define DRIVE_GENERIC 0 /* use defaults for this one */ +# define ZIP_100 1 /* to be used for quirks */ +# define SHUTTLE_EUSB 2 + + unsigned char quirks; + /* The drive does not support Test Unit Ready. Convert to + * Start Unit + * Y-E Data + */ +# define NO_TEST_UNIT_READY 0x01 + /* The drive does not reset the Unit Attention state after + * REQUEST SENSE has been sent. The INQUIRY command does not reset + * the UA either, and so CAM runs in circles trying to retrieve the + * initial INQUIRY data. + * Y-E Data + */ +# define RS_NO_CLEAR_UA 0x02 /* no REQUEST SENSE on INQUIRY*/ + + unsigned int proto; +# define PROTO_UNKNOWN 0x0000 /* unknown protocol */ +# define PROTO_BBB 0x0001 /* USB wire protocol */ +# define PROTO_CBI 0x0002 +# define PROTO_CBI_I 0x0004 +# define PROTO_WIRE 0x00ff /* USB wire protocol mask */ +# define PROTO_SCSI 0x0100 /* command protocol */ +# define PROTO_8070 0x0200 +# define PROTO_UFI 0x0400 +# define PROTO_COMMAND 0xff00 /* command protocol mask */ + + usbd_interface_handle iface; /* Mass Storage interface */ + int ifaceno; /* MS iface number */ + + u_int8_t bulkin; /* bulk-in Endpoint Address */ + u_int8_t bulkout; /* bulk-out Endpoint Address */ + u_int8_t intrin; /* intr-in Endp. (CBI) */ + usbd_pipe_handle bulkin_pipe; + usbd_pipe_handle bulkout_pipe; + usbd_pipe_handle intrin_pipe; + + /* Reset the device in a wire protocol specific way */ + wire_reset_f reset; + + /* The start of a wire transfer. It prepares the whole transfer (cmd, + * data, and status stage) and initiates it. It is up to the state + * machine (below) to handle the various stages and errors in these + */ + wire_transfer_f transfer; + + /* The state machine, handling the various states during a transfer */ + wire_state_f state; + + /* The command transform function is used to conver the SCSI commands + * into their derivatives, like UFI, ATAPI, and friends. + */ + command_transform_f transform; /* command transform */ + + /* Bulk specific variables for transfers in progress */ + umass_bbb_cbw_t cbw; /* command block wrapper */ + umass_bbb_csw_t csw; /* command status wrapper*/ + /* CBI specific variables for transfers in progress */ + umass_cbi_cbl_t cbl; /* command block */ + umass_cbi_sbl_t sbl; /* status block */ + + /* generic variables for transfers in progress */ + /* ctrl transfer requests */ + usb_device_request_t request; + + /* xfer handles + * Most of our operations are initiated from interrupt context, so + * we need to avoid using the one that is in use. We want to avoid + * allocating them in the interrupt context as well. + */ + /* indices into array below */ +# define XFER_BBB_CBW 0 /* Bulk-Only */ +# define XFER_BBB_DATA 1 +# define XFER_BBB_DCLEAR 2 +# define XFER_BBB_CSW1 3 +# define XFER_BBB_CSW2 4 +# define XFER_BBB_SCLEAR 5 +# define XFER_BBB_RESET1 6 +# define XFER_BBB_RESET2 7 +# define XFER_BBB_RESET3 8 + +# define XFER_CBI_CB 0 /* CBI */ +# define XFER_CBI_DATA 1 +# define XFER_CBI_STATUS 2 +# define XFER_CBI_DCLEAR 3 +# define XFER_CBI_SCLEAR 4 +# define XFER_CBI_RESET1 5 +# define XFER_CBI_RESET2 6 +# define XFER_CBI_RESET3 7 + +# define XFER_NR 9 /* maximum number */ + + usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */ + + int transfer_dir; /* data direction */ + void *transfer_data; /* data buffer */ + int transfer_datalen; /* (maximum) length */ + int transfer_actlen; /* actual length */ + transfer_cb_f transfer_cb; /* callback */ + void *transfer_priv; /* for callback */ + int transfer_status; + + int transfer_state; +# define TSTATE_IDLE 0 +# define TSTATE_BBB_COMMAND 1 /* CBW transfer */ +# define TSTATE_BBB_DATA 2 /* Data transfer */ +# define TSTATE_BBB_DCLEAR 3 /* clear endpt stall */ +# define TSTATE_BBB_STATUS1 4 /* clear endpt stall */ +# define TSTATE_BBB_SCLEAR 5 /* clear endpt stall */ +# define TSTATE_BBB_STATUS2 6 /* CSW transfer */ +# define TSTATE_BBB_RESET1 7 /* reset command */ +# define TSTATE_BBB_RESET2 8 /* in clear stall */ +# define TSTATE_BBB_RESET3 9 /* out clear stall */ +# define TSTATE_CBI_COMMAND 10 /* command transfer */ +# define TSTATE_CBI_DATA 11 /* data transfer */ +# define TSTATE_CBI_STATUS 12 /* status transfer */ +# define TSTATE_CBI_DCLEAR 13 /* clear ep stall */ +# define TSTATE_CBI_SCLEAR 14 /* clear ep stall */ +# define TSTATE_CBI_RESET1 15 /* reset command */ +# define TSTATE_CBI_RESET2 16 /* in clear stall */ +# define TSTATE_CBI_RESET3 17 /* out clear stall */ +# define TSTATE_STATES 18 /* # of states above */ + + + /* SCSI/CAM specific variables */ + unsigned char cam_scsi_sense[16]; + + int transfer_speed; /* in kb/s */ + int timeout; /* in msecs */ +}; + +#ifdef UMASS_DEBUG +char *states[TSTATE_STATES+1] = { + /* should be kept in sync with the list at transfer_state */ + "Idle", + "BBB CBW", + "BBB Data", + "BBB Data bulk-in/-out clear stall", + "BBB CSW, 1st attempt", + "BBB CSW bulk-in clear stall", + "BBB CSW, 2nd attempt", + "BBB Reset", + "BBB bulk-in clear stall", + "BBB bulk-out clear stall", + "CBI Command", + "CBI Data", + "CBI Status", + "CBI Data bulk-in/-out clear stall", + "CBI Status intr-in clear stall", + "CBI Reset", + "CBI bulk-in clear stall", + "CBI bulk-out clear stall", + NULL +}; +#endif + +struct cam_sim *umass_sim; /* SCSI Interface Module */ +struct cam_path *umass_path; /* and its path */ + + +/* USB device probe/attach/detach functions */ USB_DECLARE_DRIVER(umass); +static int umass_match_proto __P((struct umass_softc *sc, + usbd_interface_handle iface)); -/* USB related functions */ -usbd_status umass_usb_transfer __P((usbd_interface_handle iface, +/* generic transfer functions */ +static usbd_status umass_setup_transfer __P((struct umass_softc *sc, usbd_pipe_handle pipe, - void *buf, int buflen, - int flags, int *xfer_size)); + void *buffer, int buflen, int flags, + usbd_xfer_handle xfer)); +static usbd_status umass_setup_ctrl_transfer __P((struct umass_softc *sc, + usbd_device_handle dev, + usb_device_request_t *req, + void *buffer, int buflen, int flags, + usbd_xfer_handle xfer)); +static void umass_clear_endpoint_stall __P((struct umass_softc *sc, + u_int8_t endpt, usbd_pipe_handle pipe, + int state, usbd_xfer_handle xfer)); +static void umass_reset __P((struct umass_softc *sc, + transfer_cb_f cb, void *priv)); /* Bulk-Only related functions */ -usbd_status umass_bulk_reset __P((umass_softc_t *sc)); -usbd_status umass_bulk_transfer __P((umass_softc_t *sc, int lun, +static void umass_bbb_reset __P((struct umass_softc *sc, int status)); +static void umass_bbb_transfer __P((struct umass_softc *sc, int lun, void *cmd, int cmdlen, - void *data, int datalen, - int dir, int *residue)); + void *data, int datalen, int dir, + transfer_cb_f cb, void *priv)); +static void umass_bbb_state __P((usbd_xfer_handle xfer, + usbd_private_handle priv, + usbd_status err)); + +/* CBI related functions */ +static int umass_cbi_adsc __P((struct umass_softc *sc, char *buffer,int buflen, + usbd_xfer_handle xfer)); +static void umass_cbi_reset __P((struct umass_softc *sc, int status)); +static void umass_cbi_transfer __P((struct umass_softc *sc, int lun, + void *cmd, int cmdlen, + void *data, int datalen, int dir, + transfer_cb_f cb, void *priv)); +static void umass_cbi_state __P((usbd_xfer_handle xfer, + usbd_private_handle priv, usbd_status err)); /* CAM related functions */ static void umass_cam_action __P((struct cam_sim *sim, union ccb *ccb)); static void umass_cam_poll __P((struct cam_sim *sim)); -static int umass_cam_attach __P((umass_softc_t *sc)); -static void umass_cam_detach __P((umass_softc_t *sc)); +static void umass_cam_cb __P((struct umass_softc *sc, void *priv, + int residue, int status)); +static void umass_cam_sense_cb __P((struct umass_softc *sc, void *priv, + int residue, int status)); + +#ifdef UMASS_DO_CAM_RESCAN +static void umass_cam_rescan __P((struct umass_softc *sc)); +#endif + +static int umass_cam_attach_sim __P((void)); +static int umass_cam_attach __P((struct umass_softc *sc)); +static int umass_cam_detach_sim __P((void)); +static int umass_cam_detach __P((struct umass_softc *sc)); +/* SCSI specific functions */ +static int umass_scsi_transform __P((struct umass_softc *sc, + unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen)); + +/* UFI specific functions */ +static int umass_ufi_transform __P((struct umass_softc *sc, + unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen)); + +/* 8070 specific functions */ +static int umass_8070_transform __P((struct umass_softc *sc, + unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen)); + + +#ifdef UMASS_DEBUG +/* General debugging functions */ +static void umass_bbb_dump_cbw __P((struct umass_softc *sc, + umass_bbb_cbw_t *cbw)); +static void umass_bbb_dump_csw __P((struct umass_softc *sc, + umass_bbb_csw_t *csw)); +static void umass_dump_buffer __P((struct umass_softc *sc, u_int8_t *buffer, + int buflen, int printlen)); +#endif + + +/* + * USB device probe/attach/detach + */ + +/* + * Match the device we are seeing with the devices supported. Fill in the + * proto and drive fields in the softc accordingly. + * This function is called from both probe and attach. + */ + +static int +umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface) +{ + usbd_device_handle dev; + usb_device_descriptor_t *dd; + usb_interface_descriptor_t *id; + + /* + * Fill in sc->drive and sc->proto and return a match + * value if both are determined and 0 otherwise. + */ + + sc->drive = DRIVE_GENERIC; + sc->proto = PROTO_UNKNOWN; + sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED; + + usbd_interface2device_handle(iface, &dev); + dd = usbd_get_device_descriptor(dev); + + if (UGETW(dd->idVendor) == USB_VENDOR_SHUTTLE + && UGETW(dd->idProduct) == USB_PRODUCT_SHUTTLE_EUSB) { + sc->drive = SHUTTLE_EUSB; +#if CBI_I + sc->proto = PROTO_8070 | PROTO_CBI_I; +#else + sc->proto = PROTO_8070 | PROTO_CBI; +#endif + return UMATCH_VENDOR_PRODUCT; + } + + if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA + && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) { + + /* Revisions < 1.28 do not handle the inerrupt endpoint + * very well. + */ + if (UGETW(dd->bcdDevice) < 0x128) + sc->proto = PROTO_UFI | PROTO_CBI; + else +#if CBI_I + sc->proto = PROTO_UFI | PROTO_CBI_I; +#else + sc->proto = PROTO_UFI | PROTO_CBI; +#endif + /* + * Revisions < 1.28 do not have the TEST UNIT READY command + * Revisions == 1.28 have a broken TEST UNIT READY + */ + if (UGETW(dd->bcdDevice) <= 0x128) + sc->quirks |= NO_TEST_UNIT_READY; + + sc->quirks |= RS_NO_CLEAR_UA; + sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED; + return UMATCH_VENDOR_PRODUCT_REV; + } + + + + id = usbd_get_interface_descriptor(iface); + if (!id + || id->bInterfaceClass != UCLASS_MASS) + return UMATCH_NONE; + + switch (id->bInterfaceSubClass) { + case USUBCLASS_SCSI: + sc->proto |= PROTO_SCSI; + break; + case USUBCLASS_UFI: + sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED; + sc->proto |= PROTO_UFI; + break; + case USUBCLASS_SFF8070I: + sc->proto |= PROTO_8070; + break; + default: + DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n", + USBDEVNAME(sc->sc_dev), id->bInterfaceSubClass)); + return(UMATCH_NONE); + } + + switch (id->bInterfaceProtocol) { + case UPROTO_MASS_CBI: + sc->proto |= PROTO_CBI; + break; + case UPROTO_MASS_CBI_I: +#if CBI_I + sc->proto |= PROTO_CBI_I; +#else + sc->proto |= PROTO_CBI; +#endif + break; + case UPROTO_MASS_BBB: + sc->proto |= PROTO_BBB; + break; + case UPROTO_MASS_BBB_P: + sc->drive = ZIP_100; + sc->proto |= PROTO_BBB; + sc->transfer_speed = UMASS_ZIP100_TRANSFER_SPEED; + break; + default: + DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n", + USBDEVNAME(sc->sc_dev), id->bInterfaceProtocol)); + return(UMATCH_NONE); + } + + /* The timeout is based on the maximum expected transfer size + * divided by the expected transfer speed. + * We multiply by 4 to make sure a busy system doesn't make things + * fail. + */ + sc->timeout = 4 * (1000 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed); + sc->timeout += 1; /* add some leeway for spin up of a drive */ + + return(UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO); +} USB_MATCH(umass) { USB_MATCH_START(umass, uaa); - usb_interface_descriptor_t *id; + struct umass_softc *sc = device_get_softc(self); - if (!uaa->iface) + if (uaa->iface == NULL) return(UMATCH_NONE); - id = usbd_get_interface_descriptor(uaa->iface); - if (id - && id->bInterfaceClass == UCLASS_MASS - && id->bInterfaceSubClass == USUBCLASS_SCSI - && id->bInterfaceProtocol == UPROTO_MASS_ZIP) - /* probe the Iomega USB Zip 100 drive */ - return(UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO); - - return(UMATCH_NONE); + return(umass_match_proto(sc, uaa->iface)); } USB_ATTACH(umass) @@ -184,30 +656,68 @@ USB_ATTACH(umass) int i; int err; - sc->sc_iface = uaa->iface; - sc->sc_bulkout_pipe = NULL; - sc->sc_bulkin_pipe = NULL; + /* + * the softc struct is bzero-ed in device_set_driver. We can safely + * call umass_detach without specifically initialising the struct. + */ usbd_devinfo(uaa->device, 0, devinfo); USB_ATTACH_SETUP; - id = usbd_get_interface_descriptor(sc->sc_iface); - printf("%s: %s, iclass %d/%d/%d\n", USBDEVNAME(sc->sc_dev), devinfo, - id->bInterfaceClass, id->bInterfaceSubClass, - id->bInterfaceProtocol); + sc->iface = uaa->iface; + sc->ifaceno = uaa->ifaceno; + + /* initialise the proto and drive values in the umass_softc (again) */ + (void) umass_match_proto(sc, sc->iface); + + id = usbd_get_interface_descriptor(sc->iface); + printf("%s: %s", USBDEVNAME(sc->sc_dev), devinfo); +#ifdef UMASS_DEBUG + printf(", "); + switch (sc->proto&PROTO_COMMAND) { + case PROTO_SCSI: + printf("SCSI"); + break; + case PROTO_8070: + printf("8070i"); + break; + case PROTO_UFI: + printf("UFI"); + break; + default: + printf("(unknown 0x%02x)", sc->proto&PROTO_COMMAND); + break; + } + printf(" over "); + switch (sc->proto&PROTO_WIRE) { + case PROTO_BBB: + printf("Bulk-Only"); + break; + case PROTO_CBI: /* uses Comand/Bulk pipes */ + printf("CBI"); + break; + case PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */ + printf("CBI with CCI"); + break; + default: + printf("(unknown 0x%02x)", sc->proto&PROTO_WIRE); + } +#endif + printf("\n"); /* - * A Bulk-Only Mass Storage device supports the following endpoints, - * in addition to the Endpoint 0 for Control transfer that is required - * of all USB devices: - * (a) bulk-in endpoint. - * (b) bulk-out endpoint. + * In addition to the Control endpoint the following endpoints + * are required: + * a) bulk-in endpoint. + * b) bulk-out endpoint. + * and for Control/Bulk/Interrupt with CCI (CBI_I) + * c) intr-in * * The endpoint addresses are not fixed, so we have to read them * from the device descriptors of the current interface. */ for (i = 0 ; i < id->bNumEndpoints ; i++) { - ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); + ed = usbd_interface2endpoint_descriptor(sc->iface, i); if (!ed) { printf("%s: could not read endpoint descriptor\n", USBDEVNAME(sc->sc_dev)); @@ -215,126 +725,279 @@ USB_ATTACH(umass) } if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { - sc->sc_bulkin = ed->bEndpointAddress; + sc->bulkin = ed->bEndpointAddress; } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { - sc->sc_bulkout = ed->bEndpointAddress; + sc->bulkout = ed->bEndpointAddress; + } else if (sc->proto & PROTO_CBI_I + && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN + && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { + sc->intrin = ed->bEndpointAddress; +#ifdef UMASS_DEBUG + if (UGETW(ed->wMaxPacketSize) > 2) { + DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n", + USBDEVNAME(sc->sc_dev), + UGETW(ed->wMaxPacketSize))); + } +#endif } } - /* Open the bulk-in and -out pipe */ - err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout, - USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); - if (err) { - DPRINTF(UDMASS_USB, ("cannot open bulk out pipe (address %d)\n", - sc->sc_bulkout)); - USB_ATTACH_ERROR_RETURN; - } - err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin, - USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe); - if (err) { - DPRINTF(UDMASS_USB, ("cannot open bulk in pipe (address %d)\n", - sc->sc_bulkin)); - usbd_close_pipe(sc->sc_bulkout_pipe); + /* check whether we found all the endpoints we need */ + if (!sc->bulkin || !sc->bulkout + || (sc->proto & PROTO_CBI_I && !sc->intrin) ) { + DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n", + USBDEVNAME(sc->sc_dev), + sc->bulkin, sc->bulkout, sc->intrin)); + umass_detach(self); USB_ATTACH_ERROR_RETURN; } - /* attach the device to the SCSI layer */ - err = umass_cam_attach(sc); + /* Open the bulk-in and -out pipe */ + err = usbd_open_pipe(sc->iface, sc->bulkout, + USBD_EXCLUSIVE_USE, &sc->bulkout_pipe); if (err) { - usbd_close_pipe(sc->sc_bulkout_pipe); - usbd_close_pipe(sc->sc_bulkin_pipe); + DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n", + USBDEVNAME(sc->sc_dev), sc->bulkout)); + umass_detach(self); USB_ATTACH_ERROR_RETURN; } + err = usbd_open_pipe(sc->iface, sc->bulkin, + USBD_EXCLUSIVE_USE, &sc->bulkin_pipe); + if (err) { + DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n", + USBDEVNAME(sc->sc_dev), sc->bulkin)); + umass_detach(self); + USB_ATTACH_ERROR_RETURN; + } + /* Open the intr-in pipe if the protocol is CBI with CCI. + * Note: early versions of the Zip drive do have an interrupt pipe, but + * this pipe is unused + * + * We do not open the interrupt pipe as an interrupt pipe, but as a + * normal bulk endpoint. We send an IN transfer down the wire at the + * appropriate time, because we know exactly when to expect data on + * that endpoint. This saves bandwidth, but more important, makes the + * code for handling the data on that endpoint simpler. No data + * arriving concurently. + */ + if (sc->proto & PROTO_CBI_I) { + err = usbd_open_pipe(sc->iface, sc->intrin, + USBD_EXCLUSIVE_USE, &sc->intrin_pipe); + if (err) { + DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n", + USBDEVNAME(sc->sc_dev), sc->intrin)); + umass_detach(self); + USB_ATTACH_ERROR_RETURN; + } + } + + /* initialisation of generic part */ + sc->transfer_state = TSTATE_IDLE; + + /* request a sufficient number of xfer handles */ + for (i = 0; i < XFER_NR; i++) { + sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device); + if (!sc->transfer_xfer[i]) { + DPRINTF(UDMASS_USB, ("%s: Out of memory\n", + USBDEVNAME(sc->sc_dev))); + umass_detach(self); + USB_ATTACH_ERROR_RETURN; + } + } + + /* Initialise the wire protocol specific methods */ + if (sc->proto & PROTO_BBB) { + sc->reset = umass_bbb_reset; + sc->transfer = umass_bbb_transfer; + sc->state = umass_bbb_state; + } else if ((sc->proto & PROTO_CBI) || (sc->proto & PROTO_CBI_I)) { + sc->reset = umass_cbi_reset; + sc->transfer = umass_cbi_transfer; + sc->state = umass_cbi_state; +#ifdef UMASS_DEBUG + } else { + panic("%s:%d: Unknown proto 0x%02x\n", + __FILE__, __LINE__, sc->proto); +#endif + } + + if (sc->proto & PROTO_SCSI) + sc->transform = umass_scsi_transform; + else if (sc->proto & PROTO_UFI) + sc->transform = umass_ufi_transform; + else if (sc->proto & PROTO_8070) + sc->transform = umass_8070_transform; +#ifdef UMASS_DEBUG + else + panic("No transformation defined for command proto 0x%02x\n", + sc->proto & PROTO_COMMAND); +#endif + + /* From here onwards the device can be used. */ + + if ((sc->proto & PROTO_SCSI) || + (sc->proto & PROTO_8070) || + (sc->proto & PROTO_UFI)) { + /* Prepare the SCSI command block */ + memset(&sc->cam_scsi_sense, 0, sizeof(sc->cam_scsi_sense)); + sc->cam_scsi_sense[0] = REQUEST_SENSE; + + /* If this is the first device register the SIM */ + if (umass_sim == NULL) { + err = umass_cam_attach_sim(); + if (err) { + umass_detach(self); + USB_ATTACH_ERROR_RETURN; + } + } + + /* Attach the new device to our SCSI host controller (SIM) */ + err = umass_cam_attach(sc); + if (err) { + umass_detach(self); + USB_ATTACH_ERROR_RETURN; + } + } else { + panic("%s:%d: Unknown proto 0x%02x\n", + __FILE__, __LINE__, sc->proto); + } + + DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev))); USB_ATTACH_SUCCESS_RETURN; } -#if defined(__FreeBSD__) -static int -umass_detach(device_t self) +USB_DETACH(umass) { - umass_softc_t *sc = device_get_softc(self); + USB_DETACH_START(umass, sc); + int err = 0; + int i; DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev))); - /* detach from the SCSI host controller (completely) */ - umass_cam_detach(sc); + sc->flags |= UMASS_FLAGS_GONE; - /* remove the bulk pipes) */ - if (sc->sc_bulkout_pipe) - usbd_close_pipe(sc->sc_bulkout_pipe); - if (sc->sc_bulkin_pipe) - usbd_close_pipe(sc->sc_bulkin_pipe); + if ((sc->proto & PROTO_SCSI) || + (sc->proto & PROTO_8070) || + (sc->proto & PROTO_UFI)) + /* detach the device from the SCSI host controller (SIM) */ + err = umass_cam_detach(sc); - device_set_desc(sc->sc_dev, NULL); + for (i = 0; i < XFER_NR; i++) + if (sc->transfer_xfer[i]) + usbd_free_xfer(sc->transfer_xfer[i]); - return(0); + /* remove all the pipes */ + if (sc->bulkout_pipe) + usbd_close_pipe(sc->bulkout_pipe); + if (sc->bulkin_pipe) + usbd_close_pipe(sc->bulkin_pipe); + if (sc->intrin_pipe) + usbd_close_pipe(sc->intrin_pipe); + + return(err); } -#endif -/* Performs a request over a pipe. - * - * flags: Can be set to USBD_SHORT_XFER_OK - * xfer_size: if not null returns the nr. of bytes transferred - * - * If the returned error is USBD_STALLED the pipe stall has - * been cleared again. +/* + * Generic functions to handle transfers */ -usbd_status -umass_usb_transfer(usbd_interface_handle iface, usbd_pipe_handle pipe, - void *buf, int buflen, int flags, int *xfer_size) +static usbd_status +umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe, + void *buffer, int buflen, int flags, + usbd_xfer_handle xfer) { - usbd_device_handle dev; - usbd_xfer_handle xfer; - usbd_private_handle priv; - void *buffer; - int size; usbd_status err; - /* A transfer is done synchronously. We create and schedule the - * transfer and then wait for it to complete - */ + /* Initialiase a USB transfer and then schedule it */ - usbd_interface2device_handle(iface, &dev); + (void) usbd_setup_xfer(xfer, pipe, (void *) sc, buffer, buflen, + flags, sc->timeout, sc->state); - xfer = usbd_alloc_xfer(dev); - if (!xfer) { - DPRINTF(UDMASS_USB, ("Not enough memory\n")); - return USBD_NOMEM; - } - - (void) usbd_setup_xfer(xfer, pipe, 0, buf, buflen, - flags, 3000 /*ms*/, NULL); - err = usbd_sync_transfer(xfer); - if (err) { - DPRINTF(UDMASS_USB, ("transfer failed, %s\n", - usbd_errstr(err))); - usbd_free_xfer(xfer); + err = usbd_transfer(xfer); + if (err && err != USBD_IN_PROGRESS) { + DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err))); return(err); } - usbd_get_xfer_status(xfer, &priv, &buffer, &size, &err); - - if (xfer_size) - *xfer_size = size; - - usbd_free_xfer(xfer); - return(USBD_NORMAL_COMPLETION); + return (USBD_NORMAL_COMPLETION); } +static usbd_status +umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle dev, + usb_device_request_t *req, + void *buffer, int buflen, int flags, + usbd_xfer_handle xfer) +{ + usbd_status err; + /* Initialiase a USB control transfer and then schedule it */ -usbd_status -umass_bulk_reset(umass_softc_t *sc) + (void) usbd_setup_default_xfer(xfer, dev, (void *) sc, + sc->timeout, req, buffer, buflen, + flags, sc->state); + + err = usbd_transfer(xfer); + if (err && err != USBD_IN_PROGRESS) { + DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err))); + + /* do not reset, as this would make us loop */ + return(err); + } + + return (USBD_NORMAL_COMPLETION); +} + +static void +umass_clear_endpoint_stall(struct umass_softc *sc, + u_int8_t endpt, usbd_pipe_handle pipe, + int state, usbd_xfer_handle xfer) { usbd_device_handle dev; - usb_device_request_t req; - usbd_status err; - usb_interface_descriptor_t *id; + + DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n", + USBDEVNAME(sc->sc_dev), endpt)); + + usbd_interface2device_handle(sc->iface, &dev); + + sc->transfer_state = state; + + usbd_clear_endpoint_toggle(pipe); + + sc->request.bmRequestType = UT_WRITE_ENDPOINT; + sc->request.bRequest = UR_CLEAR_FEATURE; + USETW(sc->request.wValue, UF_ENDPOINT_HALT); + USETW(sc->request.wIndex, endpt); + USETW(sc->request.wLength, 0); + umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, xfer); +} + +static void +umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv) +{ + sc->transfer_cb = cb; + sc->transfer_priv = priv; + + /* The reset is a forced reset, so no error (yet) */ + sc->reset(sc, STATUS_CMD_OK); +} + +/* + * Bulk protocol specific functions + */ + +static void +umass_bbb_reset(struct umass_softc *sc, int status) +{ + usbd_device_handle dev; + + KASSERT(sc->proto & PROTO_BBB, + ("sc->proto == 0x%02x wrong for umass_bbb_reset\n", sc->proto)); /* * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class) @@ -343,97 +1006,87 @@ umass_bulk_reset(umass_softc_t *sc) * a) a Bulk-Only Mass Storage Reset * b) a Clear Feature HALT to the Bulk-In endpoint * c) a Clear Feature HALT to the Bulk-Out endpoint + * + * This is done in 3 steps, states: + * TSTATE_BBB_RESET1 + * TSTATE_BBB_RESET2 + * TSTATE_BBB_RESET3 + * + * If the reset doesn't succeed, the device should be port reset. */ - DPRINTF(UDMASS_BULK, ("%s: Reset\n", + DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n", USBDEVNAME(sc->sc_dev))); + + sc->transfer_state = TSTATE_BBB_RESET1; + sc->transfer_status = status; - usbd_interface2device_handle(sc->sc_iface, &dev); - id = usbd_get_interface_descriptor(sc->sc_iface); + usbd_interface2device_handle(sc->iface, &dev); - /* the reset command is a class specific interface request */ - req.bmRequestType = UT_WRITE_CLASS_INTERFACE; - req.bRequest = UR_RESET; - USETW(req.wValue, 0); - USETW(req.wIndex, id->bInterfaceNumber); - USETW(req.wLength, 0); - - err = usbd_do_request(dev, &req, 0); - if (err) { - printf("%s: Reset failed, %s\n", - USBDEVNAME(sc->sc_dev), usbd_errstr(err)); - /* XXX we should port_reset the device */ - return(err); - } - - usbd_clear_endpoint_stall(sc->sc_bulkout_pipe); - usbd_clear_endpoint_stall(sc->sc_bulkin_pipe); - - /* - * XXX we should convert this into a more friendly delay. - * Perhaps a tsleep (or is this routine run from int context?) - */ - - DELAY(2500000 /*us*/); - - return(USBD_NORMAL_COMPLETION); + /* reset is a class specific interface write */ + sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE; + sc->request.bRequest = UR_BBB_RESET; + USETW(sc->request.wValue, 0); + USETW(sc->request.wIndex, sc->ifaceno); + USETW(sc->request.wLength, 0); + umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, + sc->transfer_xfer[XFER_BBB_RESET1]); } -/* - * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly - * a data phase of datalen bytes from/to data and finally a csw read - * phase. - * - * If the data direction was inbound a maximum of datalen bytes - * is stored in the buffer pointed to by data. - * The status returned is USBD_NORMAL_COMPLETION, - * USBD_IOERROR, USBD_COMMAND_FAILED. - * In the last case *residue is set to the residue from the CSW, - * otherwise to 0. - * - * For the functionality of this subroutine see the Mass Storage - * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of - * the spec). - */ - -usbd_status -umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen, - void *data, int datalen, int dir, int *residue) +static void +umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen, + void *data, int datalen, int dir, + transfer_cb_f cb, void *priv) { - static int dCBWtag = 42; /* tag to be used in transfers, - * incremented at each transfer */ - usb_bulk_cbw_t cbw; /* command block wrapper struct */ - usb_bulk_csw_t csw; /* command status wrapper struct */ - u_int32_t n = 0; /* number of bytes transported */ - usbd_status err; + static int dCBWtag = 42; /* unique for CBW of transfer */ -#ifdef UMASS_DEBUG - u_int8_t *c = cmd; - - /* check the given arguments */ - if (!data && datalen > 0) { /* no buffer for transfer */ - DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n", - USBDEVNAME(sc->sc_dev))); - return USBD_IOERROR; - } - - DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)" - ", data: %d bytes, dir: %s\n", - USBDEVNAME(sc->sc_dev), - cmdlen, c[0], c[1], c[2], c[3], c[4], c[5], - (cmdlen > 6? "...":""), - datalen, (dir == DIR_IN? "in":"out"))); -#endif - - if (dir == DIR_NONE || datalen == 0) { /* make sure they correspond */ - datalen = 0; - dir = DIR_NONE; - } - - *residue = 0; /* reset residue */ + KASSERT(sc->proto & PROTO_BBB, + ("sc->proto == 0x%02x wrong for umass_bbb_transfer\n", + sc->proto)); /* - * Determine the direction of transferring data and data length. + * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly + * a data phase of datalen bytes from/to the device and finally a + * csw read phase. + * If the data direction was inbound a maximum of datalen bytes + * is stored in the buffer pointed to by data. + * + * umass_bbb_transfer initialises the transfer and lets the state + * machine in umass_bbb_state handle the completion. It uses the + * following states: + * TSTATE_BBB_COMMAND + * -> TSTATE_BBB_DATA + * -> TSTATE_BBB_STATUS + * -> TSTATE_BBB_STATUS2 + * -> TSTATE_BBB_IDLE + * + * An error in any of those states will invoke + * umass_bbb_reset. + */ + + /* check the given arguments */ + KASSERT(datalen == 0 || data != NULL, + ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev))); + KASSERT(cmdlen <= CBWCDBLENGTH, + ("%s: cmdlen exceeds CDB length in CBW (%d > %d)", + USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH)); + KASSERT(dir == DIR_NONE || datalen > 0, + ("%s: datalen == 0 while direction is not NONE\n", + USBDEVNAME(sc->sc_dev))); + KASSERT(datalen == 0 || dir != DIR_NONE, + ("%s: direction is NONE while datalen is not zero\n", + USBDEVNAME(sc->sc_dev))); + KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE, + ("%s: CBW struct does not have the right size (%d vs. %d)\n", + USBDEVNAME(sc->sc_dev), + sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE)); + KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE, + ("%s: CSW struct does not have the right size (%d vs. %d)\n", + USBDEVNAME(sc->sc_dev), + sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE)); + + /* + * Determine the direction of the data transfer and the length. * * dCBWDataTransferLength (datalen) : * This field indicates the number of bytes of data that the host @@ -452,145 +1105,732 @@ umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen, * 1 = data In from device to host */ - - /* - * Command transport phase - */ - /* Fill in the Command Block Wrapper */ - USETDW(cbw.dCBWSignature, CBWSIGNATURE); - USETDW(cbw.dCBWTag, dCBWtag++); - USETDW(cbw.dCBWDataTransferLength, datalen); - /* we do not check for DIR_NONE below (see text on dCBWFlags above) */ - cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT); - cbw.bCBWLUN = lun; - cbw.bCDBLength = cmdlen; - bcopy(cmd, cbw.CBWCDB, cmdlen); + USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE); + USETDW(sc->cbw.dCBWTag, dCBWtag); + dCBWtag++; /* cannot be done in macro (it will be done 4 times) */ + USETDW(sc->cbw.dCBWDataTransferLength, datalen); + /* DIR_NONE is treated as DIR_OUT (0x00) */ + sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT); + sc->cbw.bCBWLUN = lun; + sc->cbw.bCDBLength = cmdlen; + bcopy(cmd, sc->cbw.CBWCDB, cmdlen); + + DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw)); + + /* store the details for the data transfer phase */ + sc->transfer_dir = dir; + sc->transfer_data = data; + sc->transfer_datalen = datalen; + sc->transfer_actlen = 0; + sc->transfer_cb = cb; + sc->transfer_priv = priv; + sc->transfer_status = STATUS_CMD_OK; + + /* move from idle to the command state */ + sc->transfer_state = TSTATE_BBB_COMMAND; /* Send the CBW from host to device via bulk-out endpoint. */ - err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkout_pipe, - &cbw, USB_BULK_CBW_SIZE, 0, NULL); - if (err) { - DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n", - USBDEVNAME(sc->sc_dev))); - /* If the device detects that the CBW is invalid, then the - * device may STALL both bulk endpoints and require a - * Bulk-Only MS Reset - */ - umass_bulk_reset(sc); - return(USBD_IOERROR); + if (umass_setup_transfer(sc, sc->bulkout_pipe, + &sc->cbw, UMASS_BBB_CBW_SIZE, 0, + sc->transfer_xfer[XFER_BBB_CBW])) { + umass_bbb_reset(sc, STATUS_WIRE_FAILED); } +} +static void +umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv, + usbd_status err) +{ + struct umass_softc *sc = (struct umass_softc *) priv; + usbd_xfer_handle next_xfer; + + KASSERT(sc->proto & PROTO_BBB, + ("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto)); + /* - * Data transport phase (only if there is data to be sent/received) + * State handling for BBB transfers. + * + * The subroutine is rather long. It steps through the states given in + * Annex A of the Bulk-Only specification. + * Each state first does the error handling of the previous transfer + * and then prepares the next transfer. + * Each transfer is done asynchroneously so after the request/transfer + * has been submitted you will find a 'return;'. */ - if (dir == DIR_IN) { - /* we allow short transfers for bulk-in pipes */ - err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe, - data, datalen, - USBD_SHORT_XFER_OK, &n); - if (err) - DPRINTF(UDMASS_BULK, ("%s: failed to receive data, " - "(%d bytes, n = %d), %s\n", - USBDEVNAME(sc->sc_dev), - datalen, n, usbd_errstr(err))); - } else if (dir == DIR_OUT) { - err = umass_usb_transfer(sc->sc_iface, - sc->sc_bulkout_pipe, - data, datalen, 0, &n); - if (err) - DPRINTF(UDMASS_BULK, ("%s: failed to send data, " - "(%d bytes, n = %d), %s\n", - USBDEVNAME(sc->sc_dev), - datalen, n, usbd_errstr(err))); - } - if (err && err != USBD_STALLED) - return(USBD_IOERROR); + DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n", + USBDEVNAME(sc->sc_dev), sc->transfer_state, + states[sc->transfer_state], xfer, usbd_errstr(err))); + switch (sc->transfer_state) { - /* - * Status transport phase - */ - - /* Read the Command Status Wrapper via bulk-in endpoint. */ - err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe, - &csw, USB_BULK_CSW_SIZE, 0, NULL); - /* Try again if the bulk-in pipe was stalled */ - if (err == USBD_STALLED) { - err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe); - if (!err) { - err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe, - &csw, USB_BULK_CSW_SIZE, 0, NULL); - } - } - if (err && err != USBD_STALLED) - return(USBD_IOERROR); - - /* - * Check the CSW for status and validity, and check for fatal errors - */ - - /* Invalid CSW: Wrong signature or wrong tag might indicate - * that the device is confused -> reset it. - * Other fatal errors: STALL on read of CSW and Phase error - * or unknown status. - */ - if (err == USBD_STALLED - || UGETDW(csw.dCSWSignature) != CSWSIGNATURE - || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag) - || csw.bCSWStatus == CSWSTATUS_PHASE - || csw.bCSWStatus > CSWSTATUS_PHASE) { + /***** Bulk Transfer *****/ + case TSTATE_BBB_COMMAND: + /* Command transport phase, error handling */ if (err) { - printf("%s: failed to read CSW, %s\n", - USBDEVNAME(sc->sc_dev), usbd_errstr(err)); - } else if (csw.bCSWStatus == CSWSTATUS_PHASE) { - printf("%s: Phase Error, residue = %d, n = %d\n", - USBDEVNAME(sc->sc_dev), - UGETDW(csw.dCSWDataResidue), n); - } else if (csw.bCSWStatus > CSWSTATUS_PHASE) { - printf("%s: Unknown status %d in CSW\n", - USBDEVNAME(sc->sc_dev), csw.bCSWStatus); - } else { - printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n", - USBDEVNAME(sc->sc_dev), - UGETDW(csw.dCSWSignature), - UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag)); + DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n", + USBDEVNAME(sc->sc_dev))); + /* If the device detects that the CBW is invalid, then + * the device may STALL both bulk endpoints and require + * a Bulk-Reset + */ + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; } - umass_bulk_reset(sc); - return(USBD_IOERROR); - } - if (csw.bCSWStatus == CSWSTATUS_FAILED) { - DPRINTF(UDMASS_BULK, ("%s: Command Failed, " - "residue = %d, n = %d\n", - USBDEVNAME(sc->sc_dev), - UGETDW(csw.dCSWDataResidue), n)); - *residue = UGETDW(csw.dCSWDataResidue); - return(USBD_COMMAND_FAILED); + /* Data transport phase, setup transfer */ + sc->transfer_state = TSTATE_BBB_DATA; + if (sc->transfer_dir == DIR_IN) { + if (umass_setup_transfer(sc, sc->bulkin_pipe, + sc->transfer_data, sc->transfer_datalen, + USBD_SHORT_XFER_OK, + sc->transfer_xfer[XFER_BBB_DATA])) + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + + return; + } else if (sc->transfer_dir == DIR_OUT) { + if (umass_setup_transfer(sc, sc->bulkout_pipe, + sc->transfer_data, sc->transfer_datalen, + 0, /* fixed length transfer */ + sc->transfer_xfer[XFER_BBB_DATA])) + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + + return; + } else { + DPRINTF(UDMASS_BBB, ("%s: no data phase\n", + USBDEVNAME(sc->sc_dev))); + } + + /* FALLTHROUGH if no data phase, err == 0 */ + case TSTATE_BBB_DATA: + /* Command transport phase, error handling (ignored if no data + * phase (fallthrough from previous state)) */ + if (sc->transfer_dir != DIR_NONE) { + /* retrieve the length of the transfer that was done */ + usbd_get_xfer_status(xfer, NULL, NULL, + &sc->transfer_actlen, NULL); + + if (err) { + DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, " + "%s\n", USBDEVNAME(sc->sc_dev), + (sc->transfer_dir == DIR_IN?"in":"out"), + sc->transfer_datalen,usbd_errstr(err))); + + if (err == USBD_STALLED) { + umass_clear_endpoint_stall(sc, + (sc->transfer_dir == DIR_IN? + sc->bulkin:sc->bulkout), + (sc->transfer_dir == DIR_IN? + sc->bulkin_pipe:sc->bulkout_pipe), + TSTATE_BBB_DCLEAR, + sc->transfer_xfer[XFER_BBB_DCLEAR]); + return; + } else { + /* Unless the error is a pipe stall the + * error is fatal. + */ + umass_bbb_reset(sc,STATUS_WIRE_FAILED); + return; + } + } + } + + DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN) + umass_dump_buffer(sc, sc->transfer_data, + sc->transfer_datalen, 48)); + + + + /* FALLTHROUGH, err == 0 (no data phase or successfull) */ + case TSTATE_BBB_DCLEAR: /* stall clear after data phase */ + case TSTATE_BBB_SCLEAR: /* stall clear after status phase */ + /* Reading of CSW after bulk stall condition in data phase + * (TSTATE_BBB_DATA2) or bulk-in stall condition after + * reading CSW (TSTATE_BBB_SCLEAR). + * In the case of no data phase or successfull data phase, + * err == 0 and the following if block is passed. + */ + if (err) { /* should not occur */ + /* try the transfer below, even if clear stall failed */ + DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed" + ", %s\n", USBDEVNAME(sc->sc_dev), + (sc->transfer_dir == DIR_IN? "in":"out"), + usbd_errstr(err))); + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + } + + /* Status transport phase, setup transfer */ + if (sc->transfer_state == TSTATE_BBB_COMMAND || + sc->transfer_state == TSTATE_BBB_DATA || + sc->transfer_state == TSTATE_BBB_DCLEAR) { + /* After no data phase, successfull data phase and + * after clearing bulk-in/-out stall condition + */ + sc->transfer_state = TSTATE_BBB_STATUS1; + next_xfer = sc->transfer_xfer[XFER_BBB_CSW1]; + } else { + /* After first attempt of fetching CSW */ + sc->transfer_state = TSTATE_BBB_STATUS2; + next_xfer = sc->transfer_xfer[XFER_BBB_CSW2]; + } + + /* Read the Command Status Wrapper via bulk-in endpoint. */ + if (umass_setup_transfer(sc, sc->bulkin_pipe, + &sc->csw, UMASS_BBB_CSW_SIZE, 0, + next_xfer)) { + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + } + + return; + case TSTATE_BBB_STATUS1: /* first attempt */ + case TSTATE_BBB_STATUS2: /* second attempt */ + /* Status transfer, error handling */ + if (err) { + DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err), + (sc->transfer_state == TSTATE_BBB_STATUS1? + ", retrying":""))); + + /* If this was the first attempt at fetching the CSW + * retry it, otherwise fail. + */ + if (sc->transfer_state == TSTATE_BBB_STATUS1) { + umass_clear_endpoint_stall(sc, + sc->bulkin, sc->bulkin_pipe, + TSTATE_BBB_SCLEAR, + sc->transfer_xfer[XFER_BBB_SCLEAR]); + return; + } else { + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + } + } + + DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw)); + + /* Check CSW and handle any error */ + if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) { + /* Invalid CSW: Wrong signature or wrong tag might + * indicate that the device is confused -> reset it. + */ + printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n", + USBDEVNAME(sc->sc_dev), + UGETDW(sc->csw.dCSWSignature), + CSWSIGNATURE); + + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + } else if (UGETDW(sc->csw.dCSWTag) + != UGETDW(sc->cbw.dCBWTag)) { + printf("%s: Invalid CSW: tag %d should be %d\n", + USBDEVNAME(sc->sc_dev), + UGETDW(sc->csw.dCSWTag), + UGETDW(sc->cbw.dCBWTag)); + + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + + /* CSW is valid here */ + } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) { + printf("%s: Invalid CSW: status %d > %d\n", + USBDEVNAME(sc->sc_dev), + sc->csw.bCSWStatus, + CSWSTATUS_PHASE); + + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) { + printf("%s: Phase Error, residue = %d\n", + USBDEVNAME(sc->sc_dev), + UGETDW(sc->csw.dCSWDataResidue)); + + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + + } else if (sc->transfer_actlen > sc->transfer_datalen) { + /* Buffer overrun! Don't let this go by unnoticed */ + panic("%s: transferred %d bytes instead of %d bytes\n", + USBDEVNAME(sc->sc_dev), + sc->transfer_actlen, sc->transfer_datalen); + } else if (sc->transfer_datalen - sc->transfer_actlen + != UGETDW(sc->csw.dCSWDataResidue)) { + DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n", + USBDEVNAME(sc->sc_dev), + sc->transfer_datalen - sc->transfer_actlen, + UGETDW(sc->csw.dCSWDataResidue))); + + umass_bbb_reset(sc, STATUS_WIRE_FAILED); + return; + + } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) { + DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n", + USBDEVNAME(sc->sc_dev), + UGETDW(sc->csw.dCSWDataResidue))); + + /* SCSI command failed but transfer was succesful */ + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + UGETDW(sc->csw.dCSWDataResidue), + STATUS_CMD_FAILED); + + return; + + } else { /* success */ + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + UGETDW(sc->csw.dCSWDataResidue), + STATUS_CMD_OK); + + return; + } + + /***** Bulk Reset *****/ + case TSTATE_BBB_RESET1: + if (err) + printf("%s: BBB reset failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + + umass_clear_endpoint_stall(sc, + sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2, + sc->transfer_xfer[XFER_BBB_RESET2]); + + return; + case TSTATE_BBB_RESET2: + if (err) /* should not occur */ + printf("%s: BBB bulk-in clear stall failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + /* no error recovery, otherwise we end up in a loop */ + + umass_clear_endpoint_stall(sc, + sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3, + sc->transfer_xfer[XFER_BBB_RESET3]); + + return; + case TSTATE_BBB_RESET3: + if (err) /* should not occur */ + printf("%s: BBB bulk-out clear stall failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + /* no error recovery, otherwise we end up in a loop */ + + sc->transfer_state = TSTATE_IDLE; + if (sc->transfer_priv) { + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen, + sc->transfer_status); + } + + return; + + /***** Default *****/ + default: + panic("%s: Unknown state %d\n", + USBDEVNAME(sc->sc_dev), sc->transfer_state); } +} + +/* + * Command/Bulk/Interrupt (CBI) specific functions + */ + +static int +umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, + usbd_xfer_handle xfer) +{ + usbd_device_handle dev; + + KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), + ("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto)); + + usbd_interface2device_handle(sc->iface, &dev); + + sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE; + sc->request.bRequest = UR_CBI_ADSC; + USETW(sc->request.wValue, 0); + USETW(sc->request.wIndex, sc->ifaceno); + USETW(sc->request.wLength, buflen); + return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer, + buflen, 0, xfer); +} + + +static void +umass_cbi_reset(struct umass_softc *sc, int status) +{ + int i; +# define SEND_DIAGNOSTIC_CMDLEN 12 + + KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), + ("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto)); /* - * XXX a residue not equal to 0 might indicate that something - * is wrong. Does CAM high level drivers check this for us? + * Command Block Reset Protocol + * + * First send a reset request to the device. Then clear + * any possibly stalled bulk endpoints. + + * This is done in 3 steps, states: + * TSTATE_CBI_RESET1 + * TSTATE_CBI_RESET2 + * TSTATE_CBI_RESET3 + * + * If the reset doesn't succeed, the device should be port reset. */ - return(USBD_NORMAL_COMPLETION); + DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n", + USBDEVNAME(sc->sc_dev))); + + KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN, + ("%s: CBL struct is too small (%d < %d)\n", + USBDEVNAME(sc->sc_dev), + sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN)); + + sc->transfer_state = TSTATE_CBI_RESET1; + sc->transfer_status = status; + + /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between + * the two the last 10 bytes of the cbl is filled with 0xff (section + * 2.2 of the CBI spec). + */ + sc->cbl[0] = 0x1d; /* Command Block Reset */ + sc->cbl[1] = 0x04; + for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++) + sc->cbl[i] = 0xff; + + umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, + sc->transfer_xfer[XFER_CBI_RESET1]); + /* XXX if the command fails we should reset the port on the bub */ +} + +static void +umass_cbi_transfer(struct umass_softc *sc, int lun, + void *cmd, int cmdlen, void *data, int datalen, int dir, + transfer_cb_f cb, void *priv) +{ + KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), + ("sc->proto == 0x%02x wrong for umass_cbi_transfer\n", + sc->proto)); + + /* + * Do a CBI transfer with cmdlen bytes from cmd, possibly + * a data phase of datalen bytes from/to the device and finally a + * csw read phase. + * If the data direction was inbound a maximum of datalen bytes + * is stored in the buffer pointed to by data. + * + * umass_cbi_transfer initialises the transfer and lets the state + * machine in umass_cbi_state handle the completion. It uses the + * following states: + * TSTATE_CBI_COMMAND + * -> XXX fill in + * + * An error in any of those states will invoke + * umass_cbi_reset. + */ + + /* check the given arguments */ + KASSERT(datalen == 0 || data != NULL, + ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev))); + KASSERT(datalen == 0 || dir != DIR_NONE, + ("%s: direction is NONE while datalen is not zero\n", + USBDEVNAME(sc->sc_dev))); + + /* store the details for the data transfer phase */ + sc->transfer_dir = dir; + sc->transfer_data = data; + sc->transfer_datalen = datalen; + sc->transfer_actlen = 0; + sc->transfer_cb = cb; + sc->transfer_priv = priv; + sc->transfer_status = STATUS_CMD_OK; + + /* move from idle to the command state */ + sc->transfer_state = TSTATE_CBI_COMMAND; + + /* Send the Command Block from host to device via control endpoint. */ + if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB])) + umass_cbi_reset(sc, STATUS_WIRE_FAILED); +} + +static void +umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv, + usbd_status err) +{ + struct umass_softc *sc = (struct umass_softc *) priv; + + KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), + ("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto)); + + /* + * State handling for CBI transfers. + */ + + DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n", + USBDEVNAME(sc->sc_dev), sc->transfer_state, + states[sc->transfer_state], xfer, usbd_errstr(err))); + + switch (sc->transfer_state) { + + /***** CBI Transfer *****/ + case TSTATE_CBI_COMMAND: + if (err == USBD_STALLED) { + DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n", + USBDEVNAME(sc->sc_dev))); + /* Status transport by control pipe (section 2.3.2.1). + * The command contained in the command block failed. + * + * The control pipe has already been unstalled by the + * USB stack. + * Section 2.4.3.1.1 states that the bulk in endpoints + * should not stalled at this point. + */ + + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen, + STATUS_CMD_FAILED); + + return; + } else if (err) { + DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n", + USBDEVNAME(sc->sc_dev))); + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + + return; + } + + sc->transfer_state = TSTATE_CBI_DATA; + if (sc->transfer_dir == DIR_IN) { + if (umass_setup_transfer(sc, sc->bulkin_pipe, + sc->transfer_data, sc->transfer_datalen, + USBD_SHORT_XFER_OK, + sc->transfer_xfer[XFER_CBI_DATA])) + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + + } else if (sc->transfer_dir == DIR_OUT) { + if (umass_setup_transfer(sc, sc->bulkout_pipe, + sc->transfer_data, sc->transfer_datalen, + 0, /* fixed length transfer */ + sc->transfer_xfer[XFER_CBI_DATA])) + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + + } else if (sc->proto & PROTO_CBI_I) { + DPRINTF(UDMASS_CBI, ("%s: no data phase\n", + USBDEVNAME(sc->sc_dev))); + sc->transfer_state = TSTATE_CBI_STATUS; + if (umass_setup_transfer(sc, sc->intrin_pipe, + &sc->sbl, sizeof(sc->sbl), + 0, /* fixed length transfer */ + sc->transfer_xfer[XFER_CBI_STATUS])){ + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + } + } else { + DPRINTF(UDMASS_CBI, ("%s: no data phase\n", + USBDEVNAME(sc->sc_dev))); + /* No command completion interrupt. Request + * sense data. + */ + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + 0, STATUS_CMD_UNKNOWN); + } + + return; + + case TSTATE_CBI_DATA: + /* retrieve the length of the transfer that was done */ + usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL); + + if (err) { + DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, " + "%s\n", USBDEVNAME(sc->sc_dev), + (sc->transfer_dir == DIR_IN?"in":"out"), + sc->transfer_datalen,usbd_errstr(err))); + + if (err == USBD_STALLED) { + umass_clear_endpoint_stall(sc, + sc->bulkin, sc->bulkin_pipe, + TSTATE_CBI_DCLEAR, + sc->transfer_xfer[XFER_CBI_DCLEAR]); + } else { + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + } + return; + } + + DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN) + umass_dump_buffer(sc, sc->transfer_data, + sc->transfer_actlen, 48)); + + if (sc->proto & PROTO_CBI_I) { + sc->transfer_state = TSTATE_CBI_STATUS; + memset(&sc->sbl, 0, sizeof(sc->sbl)); + if (umass_setup_transfer(sc, sc->intrin_pipe, + &sc->sbl, sizeof(sc->sbl), + 0, /* fixed length transfer */ + sc->transfer_xfer[XFER_CBI_STATUS])){ + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + } + } else { + /* No command completion interrupt. Request + * sense to get status of command. + */ + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen - sc->transfer_actlen, + STATUS_CMD_UNKNOWN); + } + return; + + case TSTATE_CBI_STATUS: + if (err) { + DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n", + USBDEVNAME(sc->sc_dev))); + /* Status transport by interrupt pipe (section 2.3.2.2). + */ + + if (err == USBD_STALLED) { + umass_clear_endpoint_stall(sc, + sc->intrin, sc->intrin_pipe, + TSTATE_CBI_SCLEAR, + sc->transfer_xfer[XFER_CBI_SCLEAR]); + } else { + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + } + return; + } + + /* Dissect the information in the buffer */ + + if (sc->proto & PROTO_UFI) { + int status; + + /* Section 3.4.3.1.3 specifies that the UFI command + * protocol returns an ASC and ASCQ in the interrupt + * data block. + */ + + DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, " + "ASCQ = 0x%02x\n", + USBDEVNAME(sc->sc_dev), + sc->sbl.ufi.asc, sc->sbl.ufi.ascq)); + + if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) + status = STATUS_CMD_OK; + else + status = STATUS_CMD_FAILED; + + /* No sense, command successfull */ + } else { + /* Command Interrupt Data Block */ + DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n", + USBDEVNAME(sc->sc_dev), + sc->sbl.common.type, sc->sbl.common.value)); + + if (sc->sbl.common.type == IDB_TYPE_CCI) { + int err; + + if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK) + == IDB_VALUE_PASS) { + err = STATUS_CMD_OK; + } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK) + == IDB_VALUE_FAIL || + (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) + == IDB_VALUE_PERSISTENT) { + err = STATUS_CMD_FAILED; + } else { + err = STATUS_WIRE_FAILED; + } + + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen, + err); + } + } + return; + + case TSTATE_CBI_DCLEAR: + if (err) { /* should not occur */ + printf("%s: CBI bulk-in/out stall clear failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + umass_cbi_reset(sc, STATUS_WIRE_FAILED); + } + + sc->transfer_state = TSTATE_IDLE; + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen, + STATUS_CMD_FAILED); + return; + + case TSTATE_CBI_SCLEAR: + if (err) /* should not occur */ + printf("%s: CBI intr-in stall clear failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + + /* Something really bad is going on. Reset the device */ + umass_cbi_reset(sc, STATUS_CMD_FAILED); + return; + + /***** CBI Reset *****/ + case TSTATE_CBI_RESET1: + if (err) + printf("%s: CBI reset failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + + umass_clear_endpoint_stall(sc, + sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2, + sc->transfer_xfer[XFER_CBI_RESET2]); + + return; + case TSTATE_CBI_RESET2: + if (err) /* should not occur */ + printf("%s: CBI bulk-in stall clear failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + /* no error recovery, otherwise we end up in a loop */ + + umass_clear_endpoint_stall(sc, + sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3, + sc->transfer_xfer[XFER_CBI_RESET3]); + + return; + case TSTATE_CBI_RESET3: + if (err) /* should not occur */ + printf("%s: CBI bulk-out stall clear failed, %s\n", + USBDEVNAME(sc->sc_dev), usbd_errstr(err)); + /* no error recovery, otherwise we end up in a loop */ + + sc->transfer_state = TSTATE_IDLE; + if (sc->transfer_priv) { + sc->transfer_cb(sc, sc->transfer_priv, + sc->transfer_datalen, + sc->transfer_status); + } + + return; + + + /***** Default *****/ + default: + panic("%s: Unknown state %d\n", + USBDEVNAME(sc->sc_dev), sc->transfer_state); + } } - /* - * CAM specific functions + * CAM specific functions (used by SCSI, UFI, 8070) */ static int -umass_cam_attach(umass_softc_t *sc) +umass_cam_attach_sim() { - struct cam_devq *devq; + struct cam_devq *devq; /* Per device Queue */ /* A HBA is attached to the CAM layer. * @@ -600,49 +1840,170 @@ umass_cam_attach(umass_softc_t *sc) /* SCSI transparent command set */ - devq = cam_simq_alloc(1); /* Maximum Openings */ + devq = cam_simq_alloc(1 /*maximum openings*/); if (devq == NULL) return(ENOMEM); - sc->sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, "umass", sc, - device_get_unit(sc->sc_dev), 1, 0, devq); - if (sc->sim == NULL) { + + umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, DEVNAME, + NULL /*priv*/, 0 /*unit number*/, + 1 /*maximum device openings*/, + 0 /*maximum tagged device openings*/, + devq); + if (umass_sim == NULL) { cam_simq_free(devq); return(ENOMEM); } - if(xpt_bus_register(sc->sim, 0) != CAM_SUCCESS) { - cam_sim_free(sc->sim, 1); + if(xpt_bus_register(umass_sim, 0) != CAM_SUCCESS) return(ENOMEM); - } - if (xpt_create_path(&sc->path, NULL, cam_sim_path(sc->sim), - CAM_TARGET_WILDCARD, - CAM_LUN_WILDCARD) != CAM_REQ_CMP) { - xpt_bus_deregister(cam_sim_path(sc->sim)); - cam_sim_free(sc->sim, 1); + if (xpt_create_path(&umass_path, NULL, cam_sim_path(umass_sim), + UMASS_SCSIID_HOST, 0) + != CAM_REQ_CMP) return(ENOMEM); - } return(0); } +#ifdef UMASS_DO_CAM_RESCAN +/* this function is only used from umass_cam_rescan, so mention + * prototype down here. + */ +static void umass_cam_rescan_callback(struct cam_periph *periph,union ccb *ccb); + +static void +umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) +{ +#ifdef UMASS_DEBUG + struct umass_softc *sc = devclass_get_softc(umass_devclass, + ccb->ccb_h.target_id); + + if (ccb->ccb_h.status != CAM_REQ_CMP) { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan failed, 0x%04x\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccb->ccb_h.status)); + } else { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan succeeded, freeing resources.\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); + } +#endif + + xpt_free_path(ccb->ccb_h.path); + free(ccb, M_USBDEV); +} + +static void +umass_cam_rescan(struct umass_softc *sc) +{ + struct cam_path *path; + union ccb *ccb = malloc(sizeof(union ccb), M_USBDEV, M_WAITOK); + + memset(ccb, 0, sizeof(union ccb)); + + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: scanning bus for new device %d\n", + USBDEVNAME(sc->sc_dev), cam_sim_path(umass_sim), + device_get_unit(sc->sc_dev), 0, + device_get_unit(sc->sc_dev))); + + if (xpt_create_path(&path, xpt_periph, cam_sim_path(umass_sim), + device_get_unit(sc->sc_dev), 0) + != CAM_REQ_CMP) + return; + + xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/); + ccb->ccb_h.func_code = XPT_SCAN_BUS; + ccb->ccb_h.cbfcnp = umass_cam_rescan_callback; + ccb->crcn.flags = CAM_FLAG_NONE; + xpt_action(ccb); + + /* The scan is in progress now. */ +} +#endif + +static int +umass_cam_attach(struct umass_softc *sc) +{ + /* SIM already attached at module load. The device is a target on the + * one SIM we registered: target device_get_unit(self). + */ + + /* The artificial limit UMASS_SCSIID_MAX is there because CAM expects + * a limit to the number of targets that are present on a SIM. + */ + if (device_get_unit(sc->sc_dev) > UMASS_SCSIID_MAX) { + printf("%s: Increase UMASS_SCSIID_MAX (currently %d) in %s " + "and try again.\n", USBDEVNAME(sc->sc_dev), + UMASS_SCSIID_MAX, __FILE__); + return(1); + } + +#ifdef UMASS_DO_CAM_RESCAN + if (!cold) { + /* Notify CAM of the new device. Any failure is benign, as the + * user can still do it by hand (camcontrol rescan ). + * Only do this if we are not booting, because CAM does a scan + * after booting has completed, when interrupts have been + * enabled. + */ + umass_cam_rescan(sc); + } +#endif + + return(0); /* always succesfull */ +} + /* umass_cam_detach * detach from the CAM layer */ -static void -umass_cam_detach(umass_softc_t *sc) +static int +umass_cam_detach_sim() { - printf("%s: Woops! This will panic your system.\n" - "Detachment of the drive is not supported currently.\n", - USBDEVNAME(sc->sc_dev)); + if (umass_sim) + return(EBUSY); /* XXX CAM can't handle disappearing SIMs yet */ - xpt_async(AC_LOST_DEVICE, sc->path, NULL); - xpt_free_path(sc->path); - xpt_bus_deregister(cam_sim_path(sc->sim)); - cam_sim_free(sc->sim, /*free_devq*/TRUE); + if (umass_path) { + /* XXX do we need to send an asynchroneous event for the SIM? + xpt_async(AC_LOST_DEVICE, umass_path, NULL); + */ + xpt_free_path(umass_path); + umass_path = NULL; + } + + if (umass_sim) { + if (xpt_bus_deregister(cam_sim_path(umass_sim))) + cam_sim_free(umass_sim, /*free_devq*/TRUE); + else + return(EBUSY); + + umass_sim = NULL; + } + + return(0); } +static int +umass_cam_detach(struct umass_softc *sc) +{ + struct cam_path *path; + + /* detach of sim not done until module unload */ + DPRINTF(UDMASS_SCSI, ("%s: losing CAM device entry\n", + USBDEVNAME(sc->sc_dev))); + + if (xpt_create_path(&path, NULL, cam_sim_path(umass_sim), + device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD) + != CAM_REQ_CMP) + return(ENOMEM); + xpt_async(AC_LOST_DEVICE, path, NULL); + xpt_free_path(path); + + return(0); +} + + /* umass_cam_action * CAM requests for action come through here @@ -651,121 +2012,196 @@ umass_cam_detach(umass_softc_t *sc) static void umass_cam_action(struct cam_sim *sim, union ccb *ccb) { - umass_softc_t *sc = (umass_softc_t *) sim->softc; - usbd_status err; + struct umass_softc *sc = devclass_get_softc(umass_devclass, + ccb->ccb_h.target_id); + /* The softc is still there, but marked as going away. umass_cam_detach + * has not yet notified CAM of the lost device however. + */ + if (sc && (sc->flags & UMASS_FLAGS_GONE)) { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " + "Invalid target (gone)\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccb->ccb_h.func_code)); + ccb->ccb_h.status = CAM_TID_INVALID; + xpt_done(ccb); + return; + } + + /* Verify, depending on the operation to perform, that we either got a + * valid sc, because an existing target was referenced, or otherwise + * the SIM is addressed. + * + * This avoids bombing out at a printf and does give the CAM layer some + * sensible feedback on errors. + */ + switch (ccb->ccb_h.func_code) { + case XPT_SCSI_IO: + case XPT_RESET_DEV: + case XPT_GET_TRAN_SETTINGS: + case XPT_SET_TRAN_SETTINGS: + case XPT_CALC_GEOMETRY: + /* the opcodes requiring a target. These should never occur. */ + if (sc == NULL) { + printf("%s:%d:%d:%d:func_code 0x%04x: " + "Invalid target\n", + DEVNAME_SIM, UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccb->ccb_h.func_code); + + ccb->ccb_h.status = CAM_TID_INVALID; + xpt_done(ccb); + return; + } + break; + case XPT_PATH_INQ: + case XPT_NOOP: + /* The opcodes sometimes aimed at a target (sc is valid), + * sometimes aimed at the SIM (sc is invalid and target is + * CAM_TARGET_WILDCARD) + */ + if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " + "Invalid target\n", + DEVNAME_SIM, UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccb->ccb_h.func_code)); + + ccb->ccb_h.status = CAM_TID_INVALID; + xpt_done(ccb); + return; + } + break; + default: + /* XXX Hm, we should check the input parameters */ + } + + /* Perform the requested action */ switch (ccb->ccb_h.func_code) { case XPT_SCSI_IO: { struct ccb_scsiio *csio = &ccb->csio; /* deref union */ int dir; - int residue; + unsigned char *cmd; + int cmdlen; - DPRINTF(UDMASS_CAM, ("%s: XPT_SCSI_IO %d:%d:%d\n", - USBDEVNAME(sc->sc_dev), - ccb->ccb_h.path_id, ccb->ccb_h.target_id, - ccb->ccb_h.target_lun)); + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: " + "cmd: 0x%02x, flags: 0x%02x, " + "%db cmd/%db data/%db sense\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + csio->cdb_io.cdb_bytes[0], + ccb->ccb_h.flags & CAM_DIR_MASK, + csio->cdb_len, csio->dxfer_len, + csio->sense_len)); -#ifdef UMASS_DEBUG - if (ccb->ccb_h.target_id != UMASS_SCSIID_DEVICE - || ccb->ccb_h.target_lun != 0) { - DPRINTF(UDMASS_CAM, ("%s: Wrong SCSI ID %d or LUN %d", - USBDEVNAME(sc->sc_dev), - ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); - ccb->ccb_h.status = CAM_REQ_CMP_ERR; + /* clear the end of the buffer to make sure we don't send out + * garbage. + */ + DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK) + == CAM_DIR_OUT) + umass_dump_buffer(sc, csio->data_ptr, + csio->dxfer_len, 48)); + + if (sc->transfer_state != TSTATE_IDLE) { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: " + "I/O requested while busy (state %d, %s)\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + sc->transfer_state,states[sc->transfer_state])); + ccb->ccb_h.status = CAM_SCSI_BUSY; xpt_done(ccb); return; } -#endif - if (ccb->ccb_h.flags == CAM_DIR_NONE) - dir = DIR_NONE; - else if (ccb->ccb_h.flags & CAM_DIR_IN) + switch(ccb->ccb_h.flags&CAM_DIR_MASK) { + case CAM_DIR_IN: dir = DIR_IN; - else + break; + case CAM_DIR_OUT: dir = DIR_OUT; - - err = umass_bulk_transfer(sc, ccb->ccb_h.target_lun, - csio->cdb_io.cdb_bytes, csio->cdb_len, - (void *)csio->data_ptr, csio->dxfer_len, - dir, &residue); - /* FAILED commands are supposed to be SCSI failed commands - * and are therefore considered to be successfull CDW/CSW - * transfers. PHASE errors are more serious and should return - * an error to the CAM system. - * - * XXX This is however more based on empirical evidence than on - * hard proof from the Bulk-Only spec. - */ - if (!err) { - ccb->ccb_h.status = CAM_REQ_CMP; - } else if (err == USBD_COMMAND_FAILED) { - csio->resid = residue; - ccb->ccb_h.status = CAM_REQ_CMP_ERR; - } else if (err) { - ccb->ccb_h.status = CAM_REQ_CMP_ERR; + break; + default: + dir = DIR_NONE; } - xpt_done(ccb); + + ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED; + if (sc->transform(sc, csio->cdb_io.cdb_bytes, csio->cdb_len, + &cmd, &cmdlen)) { + sc->transfer(sc, ccb->ccb_h.target_lun, cmd, cmdlen, + csio->data_ptr, + csio->dxfer_len, dir, + umass_cam_cb, (void *) ccb); + } else { + ccb->ccb_h.status = CAM_REQ_INVALID; + xpt_done(ccb); + } + break; } case XPT_PATH_INQ: { struct ccb_pathinq *cpi = &ccb->cpi; - DPRINTF(UDMASS_CAM, ("%s: XPT_PATH_INQ\n", - USBDEVNAME(sc->sc_dev))); + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n", + (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), + UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); + /* host specific information */ cpi->version_num = 1; cpi->hba_inquiry = 0; cpi->target_sprt = 0; cpi->hba_misc = 0; cpi->hba_eng_cnt = 0; - cpi->max_target = 1; /* one target: the drive */ - cpi->max_lun = 0; /* no LUN's */ + cpi->max_target = UMASS_SCSIID_MAX; /* one target */ + cpi->max_lun = 0; /* no LUN's supported */ cpi->initiator_id = UMASS_SCSIID_HOST; - cpi->bus_id = cam_sim_bus(sim); - cpi->base_transfer_speed = 700; strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); - strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN); + strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN); strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); cpi->unit_number = cam_sim_unit(sim); + cpi->bus_id = UMASS_SCSI_BUS; + if (sc) + cpi->base_transfer_speed = sc->transfer_speed; cpi->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); break; } - case XPT_RESET_BUS: case XPT_RESET_DEV: { - DPRINTF(UDMASS_CAM, ("%s: XPT_RESET_{BUS,DEV}\n", - USBDEVNAME(sc->sc_dev))); + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); - err = umass_bulk_reset(sc); - if (err) - ccb->ccb_h.status = CAM_REQ_CMP_ERR; - else - ccb->ccb_h.status = CAM_REQ_CMP; - - xpt_done(ccb); + ccb->ccb_h.status = CAM_REQ_INPROG; + umass_reset(sc, umass_cam_cb, (void *) ccb); break; } case XPT_GET_TRAN_SETTINGS: { - DPRINTF(UDMASS_CAM, ("%s: XPT_GET_TRAN_SETTINGS (XXX)\n", - USBDEVNAME(sc->sc_dev))); - /* XXX not implemented */ + struct ccb_trans_settings *cts = &ccb->cts; - ccb->ccb_h.status = CAM_REQ_INVALID; + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); + + cts->valid = 0; + cts->flags = 0; /* no disconnection, tagging */ + + ccb->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); break; } case XPT_SET_TRAN_SETTINGS: { - DPRINTF(UDMASS_CAM, ("%s: XPT_SET_TRAN_SETTINGS (XXX)\n", - USBDEVNAME(sc->sc_dev))); - /* XXX not implemented */ + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); - ccb->ccb_h.status = CAM_REQ_INVALID; + ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; xpt_done(ccb); break; } @@ -773,23 +2209,57 @@ umass_cam_action(struct cam_sim *sim, union ccb *ccb) { struct ccb_calc_geometry *ccg = &ccb->ccg; - DPRINTF(UDMASS_CAM, ("%s: XPT_CALC_GEOMETRY volume size %d\n", - USBDEVNAME(sc->sc_dev), ccg->volume_size)); + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " + "Volume size = %d\n", + USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccg->volume_size)); - /* fill in the missing fields in the struct */ - ccg->heads = 64; - ccg->secs_per_track = 32; - ccg->cylinders = ccg->volume_size / ccg->heads - / ccg->secs_per_track; + /* XXX We should probably ask the drive for the details + * instead of cluching them up ourselves + */ + if (sc->drive == ZIP_100) { + ccg->heads = 64; + ccg->secs_per_track = 32; + ccg->cylinders = ccg->volume_size / ccg->heads + / ccg->secs_per_track; + ccb->ccb_h.status = CAM_REQ_CMP; + break; + } else if (sc->proto & PROTO_UFI) { + ccg->heads = 2; + if (ccg->volume_size == 2880) + ccg->secs_per_track = 18; + else + ccg->secs_per_track = 9; + ccg->cylinders = 80; + break; + } else { + ccb->ccb_h.status = CAM_REQ_CMP_ERR; + } + + xpt_done(ccb); + break; + } + case XPT_NOOP: + { + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n", + (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), + UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); ccb->ccb_h.status = CAM_REQ_CMP; xpt_done(ccb); break; } default: - DPRINTF(UDMASS_CAM, ("%s: CAM function 0x%x not implemented\n", - USBDEVNAME(sc->sc_dev), ccb->ccb_h.func_code)); - ccb->ccb_h.status = CAM_REQ_INVALID; + DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " + "Not implemented\n", + (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), + UMASS_SCSI_BUS, + ccb->ccb_h.target_id, ccb->ccb_h.target_lun, + ccb->ccb_h.func_code)); + + ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; xpt_done(ccb); break; } @@ -802,8 +2272,299 @@ umass_cam_action(struct cam_sim *sim, union ccb *ccb) static void umass_cam_poll(struct cam_sim *sim) { +#ifdef UMASS_DEBUG + struct umass_softc *sc = (struct umass_softc *) sim->softc; + + DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n", + USBDEVNAME(sc->sc_dev))); +#endif + /* nop */ } -DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, usbd_driver_load, 0); +/* umass_cam_cb + * finalise a completed CAM command + */ + +static void +umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status) +{ + union ccb *ccb = (union ccb *) priv; + struct ccb_scsiio *csio = &ccb->csio; /* deref union */ + + csio->resid = residue; + + switch (status) { + case STATUS_CMD_OK: + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + break; + + case STATUS_CMD_UNKNOWN: + case STATUS_CMD_FAILED: + switch (ccb->ccb_h.func_code) { + case XPT_SCSI_IO: + { + unsigned char *cmd; + int cmdlen; + + /* fetch sense data */ + DPRINTF(UDMASS_SCSI,("%s: Fetching %d/%db sense data\n", + USBDEVNAME(sc->sc_dev), csio->sense_len, + sizeof(struct scsi_sense_data))); + + sc->cam_scsi_sense[4] = csio->sense_len; + + if (sc->transform(sc, (char *) &sc->cam_scsi_sense, + sizeof(sc->cam_scsi_sense), + &cmd, &cmdlen)) { + sc->transfer(sc, ccb->ccb_h.target_lun, + cmd, cmdlen, + &csio->sense_data, + csio->sense_len, DIR_IN, + umass_cam_sense_cb, (void *) ccb); + } else { +#ifdef UMASS_DEBUG + panic("transform(REQUEST_SENSE) failed\n"); +#else + csio->resid = sc->transfer_datalen; + ccb->ccb_h.status = CAM_REQ_CMP_ERR; + xpt_done(ccb); +#endif + } + break; + } + case XPT_RESET_DEV: + ccb->ccb_h.status = CAM_REQ_CMP; + xpt_done(ccb); + break; + default: + panic("umass_cam_cb called for func_code %d\n", + ccb->ccb_h.func_code); + } + break; + + case STATUS_WIRE_FAILED: + /* the wire protocol failed and will have recovered + * (hopefully). We return an error to CAM and let CAM retry + * the command if necessary. + */ + ccb->ccb_h.status = CAM_REQ_CMP_ERR; + xpt_done(ccb); + break; + default: + panic("%s: Unknown status %d in umass_cam_cb\n", + USBDEVNAME(sc->sc_dev), status); + } +} + +/* Finalise a completed autosense operation + */ +static void +umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status) +{ + union ccb *ccb = (union ccb *) priv; + struct ccb_scsiio *csio = &ccb->csio; /* deref union */ + + switch (status) { + case STATUS_CMD_OK: + case STATUS_CMD_UNKNOWN: + /* Getting sense data succeeded. The length of the sense data + * is not returned in any way. The sense data itself contains + * the length of the sense data that is valid. + */ + if (sc->quirks & RS_NO_CLEAR_UA + && csio->cdb_io.cdb_bytes[0] == INQUIRY + && (csio->sense_data.flags & SSD_KEY) + == SSD_KEY_UNIT_ATTENTION) { + /* Ignore unit attention errors in the case where + * the Unit Attention state is not cleared on + * REQUEST SENSE. They will appear again at the next + * command. The INQUIRY command succeeded in any case. + */ + ccb->ccb_h.status = CAM_REQ_CMP; + } else if ((csio->sense_data.flags & SSD_KEY) + == SSD_KEY_NO_SENSE) { + /* No problem after all (in the case of CBI without + * CCI) + */ + ccb->ccb_h.status = CAM_REQ_CMP; + } else { + ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR + | CAM_AUTOSNS_VALID; + csio->scsi_status = SCSI_STATUS_CHECK_COND; + } + xpt_done(ccb); + break; + + default: + DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n", + USBDEVNAME(sc->sc_dev), status)); + ccb->ccb_h.status = CAM_AUTOSENSE_FAIL; + xpt_done(ccb); + } +} + + +static int +umass_driver_load(module_t mod, int what, void *arg) +{ + int err; + + switch (what) { + case MOD_UNLOAD: + err = umass_cam_detach_sim(); + if (err) + return(err); + return(usbd_driver_load(mod, what, arg)); + case MOD_LOAD: + /* We don't attach to CAM at this point, because it will try + * and malloc memory for it. This is not possible when the + * boot loader loads umass as a module before the kernel + * has been bootstrapped. + */ + default: + return(usbd_driver_load(mod, what, arg)); + } +} + +/* + * SCSI specific functions + */ + +static int +umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen) +{ + *rcmd = cmd; /* trivial copy */ + *rcmdlen = cmdlen; + + return 1; /* success */ +} + +/* + * UFI specific functions + */ + +static int +umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen) +{ + *rcmd = cmd; + *rcmdlen = 12; /* XXX cmd[(cmdlen+1)..12] contains garbage */ + + switch (cmd[0]) { + case TEST_UNIT_READY: + if (sc->quirks & NO_TEST_UNIT_READY) { + DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY " + "to START_UNIT\n", USBDEVNAME(sc->sc_dev))); + cmd[0] = START_STOP_UNIT; + cmd[4] = SSS_START; + } + return 1; + case INQUIRY: + case START_STOP_UNIT: + case MODE_SENSE: + case PREVENT_ALLOW: + case READ_10: + case READ_12: + case READ_CAPACITY: + case REQUEST_SENSE: + case REZERO_UNIT: + case POSITION_TO_ELEMENT: /* SEEK_10 */ + case SEND_DIAGNOSTIC: + case WRITE_10: + case WRITE_12: + /* FORMAT_UNIT */ + /* MODE_SELECT */ + /* READ_FORMAT_CAPACITY */ + /* VERIFY */ + /* WRITE_AND_VERIFY */ + return 1; /* success */ + default: + return 0; /* success */ + } +} + +/* + * 8070 specific functions + */ +static int +umass_8070_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, + unsigned char **rcmd, int *rcmdlen) +{ + return 0; /* failure */ +} + + +/* (even the comment is missing) */ + +DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0); + + + +#ifdef UMASS_DEBUG +static void +umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw) +{ + int clen = cbw->bCDBLength; + int dlen = UGETDW(cbw->dCBWDataTransferLength); + u_int8_t *c = cbw->CBWCDB; + int tag = UGETDW(cbw->dCBWTag); + int flags = cbw->bCBWFlags; + + DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db " + "(0x%02x%02x%02x%02x%02x%02x%s), " + "data = %d bytes, dir = %s\n", + USBDEVNAME(sc->sc_dev), tag, clen, + c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""), + dlen, (flags == CBWFLAGS_IN? "in": + (flags == CBWFLAGS_OUT? "out":"")))); +} + +static void +umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw) +{ + int sig = UGETDW(csw->dCSWSignature); + int tag = UGETW(csw->dCSWTag); + int res = UGETDW(csw->dCSWDataResidue); + int status = csw->bCSWStatus; + + DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, " + "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev), + tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"), + tag, res, + status, (status == CSWSTATUS_GOOD? "good": + (status == CSWSTATUS_FAILED? "failed": + (status == CSWSTATUS_PHASE? "phase":""))))); +} + +static void +umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen, + int printlen) +{ + int i, j; + char s1[40]; + char s2[40]; + char s3[5]; + + s1[0] = '\0'; + s3[0] = '\0'; + + sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen); + for (i = 0; i < buflen && i < printlen; i++) { + j = i % 16; + if (j == 0 && i != 0) { + DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n", + USBDEVNAME(sc->sc_dev), s1, s2)); + s2[0] = '\0'; + } + sprintf(&s1[j*2], "%02x", buffer[i] & 0xff); + } + if (buflen > printlen) + sprintf(s3, " ..."); + DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n", + USBDEVNAME(sc->sc_dev), s1, s2, s3)); +} +#endif diff --git a/sys/dev/usb/usb.h b/sys/dev/usb/usb.h index 7ed338cb2464..7c48ed28de4f 100644 --- a/sys/dev/usb/usb.h +++ b/sys/dev/usb/usb.h @@ -364,8 +364,8 @@ typedef struct { #define USUBCLASS_SCSI 6 #define UPROTO_MASS_CBI_I 0 #define UPROTO_MASS_CBI 1 -#define UPROTO_MASS_BULK 2 -#define UPROTO_MASS_BULK_P 80 /* 'P' for the Iomega Zip drive */ +#define UPROTO_MASS_BBB 2 +#define UPROTO_MASS_BBB_P 80 /* 'P' for the Iomega Zip drive */ #define UCLASS_HUB 9 #define USUBCLASS_HUB 0 #define UCLASS_DATA 10