[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/ata/ata-usb.c

Version: -  FREEBSD  -  FREEBSD8  -  FREEBSD7  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  OPENSOLARIS  -  minix-3-1-1  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

    1 /*-
    2  * Copyright (c) 2006 - 2008 Søren Schmidt <sos@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Copyright (c) 2006 Hans Petter Selasky
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer,
   13  *    without modification, immediately at the beginning of the file.
   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.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD$");
   32 
   33 #include <sys/stdint.h>
   34 #include <sys/stddef.h>
   35 #include <sys/param.h>
   36 #include <sys/queue.h>
   37 #include <sys/types.h>
   38 #include <sys/systm.h>
   39 #include <sys/kernel.h>
   40 #include <sys/bus.h>
   41 #include <sys/linker_set.h>
   42 #include <sys/module.h>
   43 #include <sys/lock.h>
   44 #include <sys/mutex.h>
   45 #include <sys/condvar.h>
   46 #include <sys/sysctl.h>
   47 #include <sys/sx.h>
   48 #include <sys/unistd.h>
   49 #include <sys/callout.h>
   50 #include <sys/malloc.h>
   51 #include <sys/priv.h>
   52 #include <machine/bus.h>
   53 
   54 #include "usbdevs.h"
   55 #include <dev/usb/usb.h>
   56 #include <dev/usb/usbdi.h>
   57 
   58 #include <sys/ata.h>
   59 #include <sys/bio.h>
   60 #include <sys/sema.h>
   61 #include <sys/taskqueue.h>
   62 #include <vm/uma.h>
   63 
   64 #include <dev/ata/ata-all.h>
   65 #include <ata_if.h>
   66 
   67 #define ATAUSB_BULK_SIZE (1<<17)
   68 
   69 /* Command Block Wrapper */
   70 struct bbb_cbw {
   71         uint8_t signature[4];
   72 #define CBWSIGNATURE            0x43425355
   73 
   74         uint8_t tag[4];
   75         uint8_t transfer_length[4];
   76         uint8_t flags;
   77 #define CBWFLAGS_OUT            0x00
   78 #define CBWFLAGS_IN             0x80
   79 
   80         uint8_t lun;
   81         uint8_t length;
   82 #define CBWCDBLENGTH            16
   83 
   84         uint8_t cdb[CBWCDBLENGTH];
   85 } __packed;
   86 
   87 /* Command Status Wrapper */
   88 struct bbb_csw {
   89         uint8_t signature[4];
   90 #define CSWSIGNATURE            0x53425355
   91 
   92         uint8_t tag[4];
   93         uint8_t residue[4];
   94         uint8_t status;
   95 #define CSWSTATUS_GOOD          0x0
   96 #define CSWSTATUS_FAILED        0x1
   97 #define CSWSTATUS_PHASE         0x2
   98 } __packed;
   99 
  100 /* USB-ATA 'controller' softc */
  101 struct atausb2_softc {
  102         struct bbb_cbw cbw;
  103         struct bbb_csw csw;
  104         struct mtx locked_mtx;
  105 
  106         struct ata_channel *locked_ch;
  107         struct ata_channel *restart_ch;
  108         struct ata_request *ata_request;
  109 
  110 #define ATAUSB_T_BBB_RESET1        0
  111 #define ATAUSB_T_BBB_RESET2        1
  112 #define ATAUSB_T_BBB_RESET3        2
  113 #define ATAUSB_T_BBB_COMMAND       3
  114 #define ATAUSB_T_BBB_DATA_READ     4
  115 #define ATAUSB_T_BBB_DATA_RD_CS    5
  116 #define ATAUSB_T_BBB_DATA_WRITE    6
  117 #define ATAUSB_T_BBB_DATA_WR_CS    7
  118 #define ATAUSB_T_BBB_STATUS        8
  119 #define ATAUSB_T_BBB_MAX           9
  120 
  121 #define ATAUSB_T_MAX ATAUSB_T_BBB_MAX
  122 
  123         struct usb_xfer *xfer[ATAUSB_T_MAX];
  124         caddr_t ata_data;
  125         device_t dev;
  126 
  127         uint32_t timeout;
  128         uint32_t ata_donecount;
  129         uint32_t ata_bytecount;
  130 
  131         uint8_t last_xfer_no;
  132         uint8_t usb2_speed;
  133         uint8_t intr_stalled;
  134         uint8_t maxlun;
  135         uint8_t iface_no;
  136         uint8_t status_try;
  137 };
  138 
  139 static const int atausbdebug = 0;
  140 
  141 /* prototypes */
  142 
  143 static device_probe_t atausb2_probe;
  144 static device_attach_t atausb2_attach;
  145 static device_detach_t atausb2_detach;
  146 
  147 static usb_callback_t atausb2_t_bbb_reset1_callback;
  148 static usb_callback_t atausb2_t_bbb_reset2_callback;
  149 static usb_callback_t atausb2_t_bbb_reset3_callback;
  150 static usb_callback_t atausb2_t_bbb_command_callback;
  151 static usb_callback_t atausb2_t_bbb_data_read_callback;
  152 static usb_callback_t atausb2_t_bbb_data_rd_cs_callback;
  153 static usb_callback_t atausb2_t_bbb_data_write_callback;
  154 static usb_callback_t atausb2_t_bbb_data_wr_cs_callback;
  155 static usb_callback_t atausb2_t_bbb_status_callback;
  156 static usb_callback_t atausb2_tr_error;
  157 
  158 static void atausb2_cancel_request(struct atausb2_softc *sc);
  159 static void atausb2_transfer_start(struct atausb2_softc *sc, uint8_t xfer_no);
  160 static void atausb2_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
  161             uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error);
  162 static int ata_usbchannel_begin_transaction(struct ata_request *request);
  163 static int ata_usbchannel_end_transaction(struct ata_request *request);
  164 
  165 static device_probe_t ata_usbchannel_probe;
  166 static device_attach_t ata_usbchannel_attach;
  167 static device_detach_t ata_usbchannel_detach;
  168 
  169 static ata_setmode_t ata_usbchannel_setmode;
  170 static ata_locking_t ata_usbchannel_locking;
  171 
  172 /*
  173  * USB frontend part
  174  */
  175 
  176 struct usb_config atausb2_config[ATAUSB_T_BBB_MAX] = {
  177 
  178         [ATAUSB_T_BBB_RESET1] = {
  179                 .type = UE_CONTROL,
  180                 .endpoint = 0x00,       /* Control pipe */
  181                 .direction = UE_DIR_ANY,
  182                 .bufsize = sizeof(struct usb_device_request),
  183                 .flags = {},
  184                 .callback = &atausb2_t_bbb_reset1_callback,
  185                 .timeout = 5000,        /* 5 seconds */
  186                 .interval = 500,        /* 500 milliseconds */
  187         },
  188 
  189         [ATAUSB_T_BBB_RESET2] = {
  190                 .type = UE_CONTROL,
  191                 .endpoint = 0x00,       /* Control pipe */
  192                 .direction = UE_DIR_ANY,
  193                 .bufsize = sizeof(struct usb_device_request),
  194                 .flags = {},
  195                 .callback = &atausb2_t_bbb_reset2_callback,
  196                 .timeout = 5000,        /* 5 seconds */
  197                 .interval = 50, /* 50 milliseconds */
  198         },
  199 
  200         [ATAUSB_T_BBB_RESET3] = {
  201                 .type = UE_CONTROL,
  202                 .endpoint = 0x00,       /* Control pipe */
  203                 .direction = UE_DIR_ANY,
  204                 .bufsize = sizeof(struct usb_device_request),
  205                 .flags = {},
  206                 .callback = &atausb2_t_bbb_reset3_callback,
  207                 .timeout = 5000,        /* 5 seconds */
  208                 .interval = 50, /* 50 milliseconds */
  209         },
  210 
  211         [ATAUSB_T_BBB_COMMAND] = {
  212                 .type = UE_BULK,
  213                 .endpoint = UE_ADDR_ANY,
  214                 .direction = UE_DIR_OUT,
  215                 .bufsize = sizeof(struct bbb_cbw),
  216                 .flags = {},
  217                 .callback = &atausb2_t_bbb_command_callback,
  218                 .timeout = 5000,        /* 5 seconds */
  219         },
  220 
  221         [ATAUSB_T_BBB_DATA_READ] = {
  222                 .type = UE_BULK,
  223                 .endpoint = UE_ADDR_ANY,
  224                 .direction = UE_DIR_IN,
  225                 .bufsize = ATAUSB_BULK_SIZE,
  226                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
  227                 .callback = &atausb2_t_bbb_data_read_callback,
  228                 .timeout = 0,   /* overwritten later */
  229         },
  230 
  231         [ATAUSB_T_BBB_DATA_RD_CS] = {
  232                 .type = UE_CONTROL,
  233                 .endpoint = 0x00,       /* Control pipe */
  234                 .direction = UE_DIR_ANY,
  235                 .bufsize = sizeof(struct usb_device_request),
  236                 .flags = {},
  237                 .callback = &atausb2_t_bbb_data_rd_cs_callback,
  238                 .timeout = 5000,        /* 5 seconds */
  239         },
  240 
  241         [ATAUSB_T_BBB_DATA_WRITE] = {
  242                 .type = UE_BULK,
  243                 .endpoint = UE_ADDR_ANY,
  244                 .direction = UE_DIR_OUT,
  245                 .bufsize = ATAUSB_BULK_SIZE,
  246                 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,},
  247                 .callback = &atausb2_t_bbb_data_write_callback,
  248                 .timeout = 0,   /* overwritten later */
  249         },
  250 
  251         [ATAUSB_T_BBB_DATA_WR_CS] = {
  252                 .type = UE_CONTROL,
  253                 .endpoint = 0x00,       /* Control pipe */
  254                 .direction = UE_DIR_ANY,
  255                 .bufsize = sizeof(struct usb_device_request),
  256                 .flags = {},
  257                 .callback = &atausb2_t_bbb_data_wr_cs_callback,
  258                 .timeout = 5000,        /* 5 seconds */
  259         },
  260 
  261         [ATAUSB_T_BBB_STATUS] = {
  262                 .type = UE_BULK,
  263                 .endpoint = UE_ADDR_ANY,
  264                 .direction = UE_DIR_IN,
  265                 .bufsize = sizeof(struct bbb_csw),
  266                 .flags = {.short_xfer_ok = 1,},
  267                 .callback = &atausb2_t_bbb_status_callback,
  268                 .timeout = 5000,        /* ms */
  269         },
  270 };
  271 
  272 static devclass_t atausb2_devclass;
  273 
  274 static device_method_t atausb2_methods[] = {
  275         DEVMETHOD(device_probe, atausb2_probe),
  276         DEVMETHOD(device_attach, atausb2_attach),
  277         DEVMETHOD(device_detach, atausb2_detach),
  278         {0, 0}
  279 };
  280 
  281 static driver_t atausb2_driver = {
  282         .name = "atausb",
  283         .methods = atausb2_methods,
  284         .size = sizeof(struct atausb2_softc),
  285 };
  286 
  287 DRIVER_MODULE(atausb, uhub, atausb2_driver, atausb2_devclass, 0, 0);
  288 MODULE_DEPEND(atausb, usb, 1, 1, 1);
  289 MODULE_VERSION(atausb, 1);
  290 
  291 static int
  292 atausb2_probe(device_t dev)
  293 {
  294         struct usb_attach_arg *uaa = device_get_ivars(dev);
  295         struct usb_interface_descriptor *id;
  296 
  297         if (uaa->usb_mode != USB_MODE_HOST) {
  298                 return (ENXIO);
  299         }
  300         if (uaa->use_generic == 0) {
  301                 /* give other drivers a try first */
  302                 return (ENXIO);
  303         }
  304         id = usbd_get_interface_descriptor(uaa->iface);
  305         if ((!id) || (id->bInterfaceClass != UICLASS_MASS)) {
  306                 return (ENXIO);
  307         }
  308         switch (id->bInterfaceSubClass) {
  309         case UISUBCLASS_QIC157:
  310         case UISUBCLASS_RBC:
  311         case UISUBCLASS_SCSI:
  312         case UISUBCLASS_SFF8020I:
  313         case UISUBCLASS_SFF8070I:
  314         case UISUBCLASS_UFI:
  315                 switch (id->bInterfaceProtocol) {
  316                 case UIPROTO_MASS_CBI:
  317                 case UIPROTO_MASS_CBI_I:
  318                 case UIPROTO_MASS_BBB:
  319                 case UIPROTO_MASS_BBB_OLD:
  320                         return (0);
  321                 default:
  322                         return (0);
  323                 }
  324                 break;
  325         default:
  326                 return (0);
  327         }
  328 }
  329 
  330 static int
  331 atausb2_attach(device_t dev)
  332 {
  333         struct atausb2_softc *sc = device_get_softc(dev);
  334         struct usb_attach_arg *uaa = device_get_ivars(dev);
  335         struct usb_interface_descriptor *id;
  336         const char *proto, *subclass;
  337         struct usb_device_request request;
  338         device_t child;
  339         uint16_t i;
  340         uint8_t maxlun;
  341         uint8_t has_intr;
  342         int err;
  343 
  344         device_set_usb_desc(dev);
  345 
  346         sc->dev = dev;
  347         sc->maxlun = 0;
  348         sc->locked_ch = NULL;
  349         sc->restart_ch = NULL;
  350         sc->usb2_speed = usbd_get_speed(uaa->device);
  351         mtx_init(&sc->locked_mtx, "ATAUSB lock", NULL, (MTX_DEF | MTX_RECURSE));
  352 
  353         id = usbd_get_interface_descriptor(uaa->iface);
  354         switch (id->bInterfaceProtocol) {
  355         case UIPROTO_MASS_BBB:
  356         case UIPROTO_MASS_BBB_OLD:
  357                 proto = "Bulk-Only";
  358                 break;
  359         case UIPROTO_MASS_CBI:
  360                 proto = "CBI";
  361                 break;
  362         case UIPROTO_MASS_CBI_I:
  363                 proto = "CBI with CCI";
  364                 break;
  365         default:
  366                 proto = "Unknown";
  367         }
  368 
  369         switch (id->bInterfaceSubClass) {
  370         case UISUBCLASS_RBC:
  371                 subclass = "RBC";
  372                 break;
  373         case UISUBCLASS_QIC157:
  374         case UISUBCLASS_SFF8020I:
  375         case UISUBCLASS_SFF8070I:
  376                 subclass = "ATAPI";
  377                 break;
  378         case UISUBCLASS_SCSI:
  379                 subclass = "SCSI";
  380                 break;
  381         case UISUBCLASS_UFI:
  382                 subclass = "UFI";
  383                 break;
  384         default:
  385                 subclass = "Unknown";
  386         }
  387 
  388         has_intr = (id->bInterfaceProtocol == UIPROTO_MASS_CBI_I);
  389         sc->iface_no = id->bInterfaceNumber;
  390 
  391         device_printf(dev, "using %s over %s\n", subclass, proto);
  392         if (strcmp(proto, "Bulk-Only") ||
  393             (strcmp(subclass, "ATAPI") && strcmp(subclass, "SCSI"))) {
  394                 goto detach;
  395         }
  396         err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex,
  397             sc->xfer, atausb2_config, ATAUSB_T_BBB_MAX, sc,
  398             &sc->locked_mtx);
  399 
  400         /* skip reset first time */
  401         sc->last_xfer_no = ATAUSB_T_BBB_COMMAND;
  402 
  403         if (err) {
  404                 device_printf(sc->dev, "could not setup required "
  405                     "transfers, %s\n", usbd_errstr(err));
  406                 goto detach;
  407         }
  408         /* get number of devices so we can add matching channels */
  409         request.bmRequestType = UT_READ_CLASS_INTERFACE;
  410         request.bRequest = 0xfe;        /* GET_MAX_LUN; */
  411         USETW(request.wValue, 0);
  412         USETW(request.wIndex, sc->iface_no);
  413         USETW(request.wLength, sizeof(maxlun));
  414         err = usbd_do_request(uaa->device, &Giant, &request, &maxlun);
  415 
  416         if (err) {
  417                 if (bootverbose) {
  418                         device_printf(sc->dev, "get maxlun not supported %s\n",
  419                             usbd_errstr(err));
  420                 }
  421         } else {
  422                 sc->maxlun = maxlun;
  423                 if (bootverbose) {
  424                         device_printf(sc->dev, "maxlun=%d\n", sc->maxlun);
  425                 }
  426         }
  427 
  428         /* ata channels are children to this USB control device */
  429         for (i = 0; i <= sc->maxlun; i++) {
  430                 if ((child = device_add_child(sc->dev, "ata",
  431                     devclass_find_free_unit(ata_devclass, 2))) == NULL) {
  432                         device_printf(sc->dev, "failed to add ata child device\n");
  433                 } else
  434                     device_set_ivars(child, (void *)(intptr_t)i);
  435         }
  436         bus_generic_attach(sc->dev);
  437 
  438         return (0);
  439 
  440 detach:
  441         atausb2_detach(dev);
  442         return (ENXIO);
  443 }
  444 
  445 static int
  446 atausb2_detach(device_t dev)
  447 {
  448         struct atausb2_softc *sc = device_get_softc(dev);
  449         device_t *children;
  450         int nchildren, i;
  451 
  452         /* teardown our statemachine */
  453 
  454         usbd_transfer_unsetup(sc->xfer, ATAUSB_T_MAX);
  455 
  456         /* detach & delete all children, if any */
  457 
  458         if (!device_get_children(dev, &children, &nchildren)) {
  459                 for (i = 0; i < nchildren; i++) {
  460                         device_delete_child(dev, children[i]);
  461                 }
  462                 free(children, M_TEMP);
  463         }
  464         mtx_destroy(&sc->locked_mtx);
  465         return (0);
  466 }
  467 
  468 static void
  469 atausb2_transfer_start(struct atausb2_softc *sc, uint8_t xfer_no)
  470 {
  471         if (atausbdebug) {
  472                 device_printf(sc->dev, "BBB transfer %d\n", xfer_no);
  473         }
  474         if (sc->xfer[xfer_no]) {
  475                 sc->last_xfer_no = xfer_no;
  476                 usbd_transfer_start(sc->xfer[xfer_no]);
  477         } else {
  478                 atausb2_cancel_request(sc);
  479         }
  480 }
  481 
  482 static void
  483 atausb2_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
  484 {
  485         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  486         struct usb_device_request req;
  487         struct usb_page_cache *pc;
  488 
  489         switch (USB_GET_STATE(xfer)) {
  490         case USB_ST_TRANSFERRED:
  491                 atausb2_transfer_start(sc, ATAUSB_T_BBB_RESET2);
  492                 return;
  493 
  494         case USB_ST_SETUP:
  495                 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
  496                 req.bRequest = 0xff;    /* bulk-only reset */
  497                 USETW(req.wValue, 0);
  498                 req.wIndex[0] = sc->iface_no;
  499                 req.wIndex[1] = 0;
  500                 USETW(req.wLength, 0);
  501 
  502                 pc = usbd_xfer_get_frame(xfer, 0);
  503                 usbd_copy_in(pc, 0, &req, sizeof(req));
  504 
  505                 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
  506                 usbd_xfer_set_frames(xfer, 1);
  507                 usbd_transfer_submit(xfer);
  508                 return;
  509 
  510         default:                        /* Error */
  511                 atausb2_tr_error(xfer, error);
  512                 return;
  513 
  514         }
  515 }
  516 
  517 static void
  518 atausb2_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
  519 {
  520         atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_RESET3,
  521             ATAUSB_T_BBB_DATA_READ, error);
  522 }
  523 
  524 static void
  525 atausb2_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
  526 {
  527         atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_COMMAND,
  528             ATAUSB_T_BBB_DATA_WRITE, error);
  529 }
  530 
  531 static void
  532 atausb2_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
  533     uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
  534 {
  535         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  536 
  537         switch (USB_GET_STATE(xfer)) {
  538         case USB_ST_TRANSFERRED:
  539 tr_transferred:
  540                 atausb2_transfer_start(sc, next_xfer);
  541                 return;
  542 
  543         case USB_ST_SETUP:
  544                 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) {
  545                         goto tr_transferred;
  546                 }
  547                 return;
  548 
  549         default:                        /* Error */
  550                 atausb2_tr_error(xfer, error);
  551                 return;
  552 
  553         }
  554 }
  555 
  556 static void
  557 atausb2_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
  558 {
  559         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  560         struct ata_request *request = sc->ata_request;
  561         struct ata_channel *ch;
  562         struct usb_page_cache *pc;
  563         uint32_t tag;
  564 
  565         switch (USB_GET_STATE(xfer)) {
  566         case USB_ST_TRANSFERRED:
  567                 atausb2_transfer_start
  568                     (sc, ((request->flags & ATA_R_READ) ? ATAUSB_T_BBB_DATA_READ :
  569                     (request->flags & ATA_R_WRITE) ? ATAUSB_T_BBB_DATA_WRITE :
  570                     ATAUSB_T_BBB_STATUS));
  571                 return;
  572 
  573         case USB_ST_SETUP:
  574 
  575                 sc->status_try = 0;
  576 
  577                 if (request) {
  578                         ch = device_get_softc(request->parent);
  579 
  580                         sc->timeout = (request->timeout * 1000) + 5000;
  581 
  582                         tag = UGETDW(sc->cbw.tag) + 1;
  583 
  584                         USETDW(sc->cbw.signature, CBWSIGNATURE);
  585                         USETDW(sc->cbw.tag, tag);
  586                         USETDW(sc->cbw.transfer_length, request->bytecount);
  587                         sc->cbw.flags = (request->flags & ATA_R_READ) ? CBWFLAGS_IN : CBWFLAGS_OUT;
  588                         sc->cbw.lun = ch->unit;
  589                         sc->cbw.length = 16;
  590                         bzero(sc->cbw.cdb, 16);
  591                         bcopy(request->u.atapi.ccb, sc->cbw.cdb, 12);   /* XXX SOS */
  592 
  593                         pc = usbd_xfer_get_frame(xfer, 0);
  594                         usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
  595 
  596                         usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
  597                         usbd_transfer_submit(xfer);
  598                 }
  599                 return;
  600 
  601         default:                        /* Error */
  602                 atausb2_tr_error(xfer, error);
  603                 return;
  604 
  605         }
  606 }
  607 
  608 static void
  609 atausb2_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
  610 {
  611         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  612         uint32_t max_bulk = usbd_xfer_max_len(xfer);
  613         struct usb_page_cache *pc;
  614         int actlen, sumlen;
  615 
  616         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
  617 
  618         switch (USB_GET_STATE(xfer)) {
  619         case USB_ST_TRANSFERRED:
  620 
  621                 pc = usbd_xfer_get_frame(xfer, 0);
  622                 usbd_copy_out(pc, 0, sc->ata_data, actlen);
  623 
  624                 sc->ata_bytecount -= actlen;
  625                 sc->ata_data += actlen;
  626                 sc->ata_donecount += actlen;
  627 
  628                 if (actlen < sumlen) {
  629                         /* short transfer */
  630                         sc->ata_bytecount = 0;
  631                 }
  632         case USB_ST_SETUP:
  633 
  634                 if (atausbdebug > 1) {
  635                         device_printf(sc->dev, "%s: max_bulk=%d, ata_bytecount=%d\n",
  636                             __FUNCTION__, max_bulk, sc->ata_bytecount);
  637                 }
  638                 if (sc->ata_bytecount == 0) {
  639                         atausb2_transfer_start(sc, ATAUSB_T_BBB_STATUS);
  640                         return;
  641                 }
  642                 if (max_bulk > sc->ata_bytecount) {
  643                         max_bulk = sc->ata_bytecount;
  644                 }
  645                 usbd_xfer_set_timeout(xfer, sc->timeout);
  646                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
  647 
  648                 usbd_transfer_submit(xfer);
  649                 return;
  650 
  651         default:                        /* Error */
  652                 if (error == USB_ERR_CANCELLED) {
  653                         atausb2_tr_error(xfer, error);
  654                 } else {
  655                         atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_RD_CS);
  656                 }
  657                 return;
  658 
  659         }
  660 }
  661 
  662 static void
  663 atausb2_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
  664 {
  665         atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_STATUS,
  666             ATAUSB_T_BBB_DATA_READ, error);
  667 }
  668 
  669 static void
  670 atausb2_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
  671 {
  672         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  673         struct usb_page_cache *pc;
  674         uint32_t max_bulk = usbd_xfer_max_len(xfer);
  675         int actlen;
  676 
  677         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
  678 
  679         switch (USB_GET_STATE(xfer)) {
  680         case USB_ST_TRANSFERRED:
  681 
  682                 sc->ata_bytecount -= actlen;
  683                 sc->ata_data += actlen;
  684                 sc->ata_donecount += actlen;
  685 
  686         case USB_ST_SETUP:
  687 
  688                 if (atausbdebug > 1) {
  689                         device_printf(sc->dev, "%s: max_bulk=%d, ata_bytecount=%d\n",
  690                             __FUNCTION__, max_bulk, sc->ata_bytecount);
  691                 }
  692                 if (sc->ata_bytecount == 0) {
  693                         atausb2_transfer_start(sc, ATAUSB_T_BBB_STATUS);
  694                         return;
  695                 }
  696                 if (max_bulk > sc->ata_bytecount) {
  697                         max_bulk = sc->ata_bytecount;
  698                 }
  699 
  700                 pc = usbd_xfer_get_frame(xfer, 0);
  701                 usbd_copy_in(pc, 0, sc->ata_data, max_bulk);
  702                 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
  703                 usbd_xfer_set_timeout(xfer, sc->timeout);
  704 
  705                 usbd_transfer_submit(xfer);
  706                 return;
  707 
  708         default:                        /* Error */
  709                 if (error == USB_ERR_CANCELLED) {
  710                         atausb2_tr_error(xfer, error);
  711                 } else {
  712                         atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_WR_CS);
  713                 }
  714                 return;
  715 
  716         }
  717 }
  718 
  719 static void
  720 atausb2_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
  721 {
  722         atausb2_t_bbb_data_clear_stall_callback(xfer, ATAUSB_T_BBB_STATUS,
  723             ATAUSB_T_BBB_DATA_WRITE, error);
  724 }
  725 
  726 static void
  727 atausb2_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
  728 {
  729         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  730         struct ata_request *request = sc->ata_request;
  731         struct usb_page_cache *pc;
  732         uint32_t residue;
  733         int actlen;
  734 
  735         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
  736 
  737         switch (USB_GET_STATE(xfer)) {
  738         case USB_ST_TRANSFERRED:
  739 
  740                 if (actlen < sizeof(sc->csw)) {
  741                         bzero(&sc->csw, sizeof(sc->csw));
  742                 }
  743                 pc = usbd_xfer_get_frame(xfer, 0);
  744                 usbd_copy_out(pc, 0, &sc->csw, actlen);
  745 
  746                 if (request->flags & (ATA_R_READ | ATA_R_WRITE)) {
  747                         request->donecount = sc->ata_donecount;
  748                 }
  749                 residue = UGETDW(sc->csw.residue);
  750 
  751                 if (!residue) {
  752                         residue = (request->bytecount - request->donecount);
  753                 }
  754                 if (residue > request->bytecount) {
  755                         if (atausbdebug) {
  756                                 device_printf(sc->dev, "truncating residue from %d "
  757                                     "to %d bytes\n", residue,
  758                                     request->bytecount);
  759                         }
  760                         residue = request->bytecount;
  761                 }
  762                 /* check CSW and handle eventual error */
  763                 if (UGETDW(sc->csw.signature) != CSWSIGNATURE) {
  764                         if (atausbdebug) {
  765                                 device_printf(sc->dev, "bad CSW signature 0x%08x != 0x%08x\n",
  766                                     UGETDW(sc->csw.signature), CSWSIGNATURE);
  767                         }
  768                         goto tr_error;
  769                 } else if (UGETDW(sc->csw.tag) != UGETDW(sc->cbw.tag)) {
  770                         if (atausbdebug) {
  771                                 device_printf(sc->dev, "bad CSW tag %d != %d\n",
  772                                     UGETDW(sc->csw.tag), UGETDW(sc->cbw.tag));
  773                         }
  774                         goto tr_error;
  775                 } else if (sc->csw.status > CSWSTATUS_PHASE) {
  776                         if (atausbdebug) {
  777                                 device_printf(sc->dev, "bad CSW status %d > %d\n",
  778                                     sc->csw.status, CSWSTATUS_PHASE);
  779                         }
  780                         goto tr_error;
  781                 } else if (sc->csw.status == CSWSTATUS_PHASE) {
  782                         if (atausbdebug) {
  783                                 device_printf(sc->dev, "phase error residue = %d\n", residue);
  784                         }
  785                         goto tr_error;
  786                 } else if (request->donecount > request->bytecount) {
  787                         if (atausbdebug) {
  788                                 device_printf(sc->dev, "buffer overrun %d > %d\n",
  789                                     request->donecount, request->bytecount);
  790                         }
  791                         goto tr_error;
  792                 } else if (sc->csw.status == CSWSTATUS_FAILED) {
  793                         if (atausbdebug) {
  794                                 device_printf(sc->dev, "CSWSTATUS_FAILED\n");
  795                         }
  796                         request->error = ATA_E_ATAPI_SENSE_MASK;
  797                 }
  798                 sc->last_xfer_no = ATAUSB_T_BBB_COMMAND;
  799 
  800                 sc->ata_request = NULL;
  801 
  802                 /* drop the USB transfer lock while doing the ATA interrupt */
  803                 mtx_unlock(&sc->locked_mtx);
  804 
  805                 ata_interrupt(device_get_softc(request->parent));
  806 
  807                 mtx_lock(&sc->locked_mtx);
  808                 return;
  809 
  810         case USB_ST_SETUP:
  811                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
  812                 usbd_transfer_submit(xfer);
  813                 return;
  814 
  815         default:
  816 tr_error:
  817                 if (error == USB_ERR_CANCELLED || sc->status_try) {
  818                         atausb2_tr_error(xfer, error);
  819                 } else {
  820                         sc->status_try = 1;
  821                         atausb2_transfer_start(sc, ATAUSB_T_BBB_DATA_RD_CS);
  822                 }
  823                 return;
  824 
  825         }
  826 }
  827 
  828 static void
  829 atausb2_cancel_request(struct atausb2_softc *sc)
  830 {
  831         struct ata_request *request;
  832 
  833         mtx_assert(&sc->locked_mtx, MA_OWNED);
  834 
  835         request = sc->ata_request;
  836         sc->ata_request = NULL;
  837         sc->last_xfer_no = ATAUSB_T_BBB_RESET1;
  838 
  839         if (request) {
  840                 request->error = ATA_E_ATAPI_SENSE_MASK;
  841 
  842                 mtx_unlock(&sc->locked_mtx);
  843 
  844                 ata_interrupt(device_get_softc(request->parent));
  845 
  846                 mtx_lock(&sc->locked_mtx);
  847         }
  848 }
  849 
  850 static void
  851 atausb2_tr_error(struct usb_xfer *xfer, usb_error_t error)
  852 {
  853         struct atausb2_softc *sc = usbd_xfer_softc(xfer);
  854 
  855         if (error != USB_ERR_CANCELLED) {
  856 
  857                 if (atausbdebug) {
  858                         device_printf(sc->dev, "transfer failed, %s, in state %d "
  859                             "-> BULK reset\n", usbd_errstr(error),
  860                             sc->last_xfer_no);
  861                 }
  862         }
  863         atausb2_cancel_request(sc);
  864 }
  865 
  866 /*
  867  * ATA backend part
  868  */
  869 struct atapi_inquiry {
  870         uint8_t device_type;
  871         uint8_t device_modifier;
  872         uint8_t version;
  873         uint8_t response_format;
  874         uint8_t length;
  875         uint8_t reserved[2];
  876         uint8_t flags;
  877         uint8_t vendor[8];
  878         uint8_t product[16];
  879         uint8_t revision[4];
  880         /* uint8_t    crap[60]; */
  881 } __packed;
  882 
  883 static int
  884 ata_usbchannel_begin_transaction(struct ata_request *request)
  885 {
  886         struct atausb2_softc *sc =
  887         device_get_softc(device_get_parent(request->parent));
  888         int error;
  889 
  890         if (atausbdebug > 1) {
  891                 device_printf(request->dev, "begin_transaction %s\n",
  892                     ata_cmd2str(request));
  893         }
  894         mtx_lock(&sc->locked_mtx);
  895 
  896         /* sanity, just in case */
  897         if (sc->ata_request) {
  898                 device_printf(request->dev, "begin is busy, "
  899                     "state = %d\n", sc->last_xfer_no);
  900                 request->result = EBUSY;
  901                 error = ATA_OP_FINISHED;
  902                 goto done;
  903         }
  904         /*
  905          * XXX SOS convert the request into the format used, only BBB for
  906          * now
  907          */
  908 
  909         /* ATA/ATAPI IDENTIFY needs special treatment */
  910         if (!(request->flags & ATA_R_ATAPI)) {
  911                 if (request->u.ata.command != ATA_ATAPI_IDENTIFY) {
  912                         device_printf(request->dev, "%s unsupported\n",
  913                             ata_cmd2str(request));
  914                         request->result = EIO;
  915                         error = ATA_OP_FINISHED;
  916                         goto done;
  917                 }
  918                 request->flags |= ATA_R_ATAPI;
  919                 bzero(request->u.atapi.ccb, 16);
  920                 request->u.atapi.ccb[0] = ATAPI_INQUIRY;
  921                 request->u.atapi.ccb[4] = 255;  /* sizeof(struct
  922                                                  * atapi_inquiry); */
  923                 request->data += 256;   /* arbitrary offset into ata_param */
  924                 request->bytecount = 255;       /* sizeof(struct
  925                                                  * atapi_inquiry); */
  926         }
  927         if (sc->xfer[sc->last_xfer_no]) {
  928 
  929                 sc->ata_request = request;
  930                 sc->ata_bytecount = request->bytecount;
  931                 sc->ata_data = request->data;
  932                 sc->ata_donecount = 0;
  933 
  934                 usbd_transfer_start(sc->xfer[sc->last_xfer_no]);
  935                 error = ATA_OP_CONTINUES;
  936         } else {
  937                 request->result = EIO;
  938                 error = ATA_OP_FINISHED;
  939         }
  940 
  941 done:
  942         mtx_unlock(&sc->locked_mtx);
  943         return (error);
  944 }
  945 
  946 static int
  947 ata_usbchannel_end_transaction(struct ata_request *request)
  948 {
  949         if (atausbdebug > 1) {
  950                 device_printf(request->dev, "end_transaction %s\n",
  951                     ata_cmd2str(request));
  952         }
  953         /*
  954          * XXX SOS convert the request from the format used, only BBB for
  955          * now
  956          */
  957 
  958         /* ATA/ATAPI IDENTIFY needs special treatment */
  959         if ((request->flags & ATA_R_ATAPI) &&
  960             (request->u.atapi.ccb[0] == ATAPI_INQUIRY)) {
  961                 struct ata_device *atadev = device_get_softc(request->dev);
  962                 struct atapi_inquiry *inquiry = (struct atapi_inquiry *)request->data;
  963                 uint16_t *ptr;
  964 
  965                 /* convert inquiry data into simple ata_param like format */
  966                 atadev->param.config = ATA_PROTO_ATAPI | ATA_PROTO_ATAPI_12;
  967                 atadev->param.config |= (inquiry->device_type & 0x1f) << 8;
  968                 bzero(atadev->param.model, sizeof(atadev->param.model));
  969                 strncpy(atadev->param.model, inquiry->vendor, 8);
  970                 strcpy(atadev->param.model, "  ");
  971                 strncpy(atadev->param.model, inquiry->product, 16);
  972                 ptr = (uint16_t *)(atadev->param.model + sizeof(atadev->param.model));
  973                 while (--ptr >= (uint16_t *)atadev->param.model) {
  974                         *ptr = ntohs(*ptr);
  975                 }
  976                 strncpy(atadev->param.revision, inquiry->revision, 4);
  977                 ptr = (uint16_t *)(atadev->param.revision + sizeof(atadev->param.revision));
  978                 while (--ptr >= (uint16_t *)atadev->param.revision) {
  979                         *ptr = ntohs(*ptr);
  980                 }
  981                 request->result = 0;
  982         }
  983         return (ATA_OP_FINISHED);
  984 }
  985 
  986 static int
  987 ata_usbchannel_probe(device_t dev)
  988 {
  989         char buffer[32];
  990 
  991         snprintf(buffer, sizeof(buffer), "USB lun %d",
  992             (int)(intptr_t)device_get_ivars(dev));
  993         device_set_desc_copy(dev, buffer);
  994 
  995         return (0);
  996 }
  997 
  998 static int
  999 ata_usbchannel_attach(device_t dev)
 1000 {
 1001         struct ata_channel *ch = device_get_softc(dev);
 1002 
 1003         if (ch->attached)
 1004                 return (0);
 1005         ch->attached = 1;
 1006 
 1007         /* initialize the softc basics */
 1008         ch->dev = dev;
 1009         ch->unit = (intptr_t)device_get_ivars(dev);
 1010         ch->state = ATA_IDLE;
 1011         ch->hw.begin_transaction = ata_usbchannel_begin_transaction;
 1012         ch->hw.end_transaction = ata_usbchannel_end_transaction;
 1013         ch->hw.status = NULL;
 1014         ch->hw.command = NULL;
 1015         bzero(&ch->state_mtx, sizeof(struct mtx));
 1016         mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
 1017         bzero(&ch->queue_mtx, sizeof(struct mtx));
 1018         mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
 1019         TAILQ_INIT(&ch->ata_queue);
 1020 
 1021         /* XXX SOS reset the controller HW, the channel and device(s) */
 1022         /* ATA_RESET(dev); */
 1023 
 1024         /* probe and attach device on this channel */
 1025         ch->devices = ATA_ATAPI_MASTER;
 1026         if (!ata_delayed_attach) {
 1027                 ata_identify(dev);
 1028         }
 1029         return (0);
 1030 }
 1031 
 1032 static int
 1033 ata_usbchannel_detach(device_t dev)
 1034 {
 1035         struct ata_channel *ch = device_get_softc(dev);
 1036         device_t *children;
 1037         int nchildren, i;
 1038 
 1039         if (!ch->attached)
 1040                 return (0);
 1041         ch->attached = 0;
 1042 
 1043         /* detach & delete all children */
 1044         if (!device_get_children(dev, &children, &nchildren)) {
 1045                 for (i = 0; i < nchildren; i++)
 1046                         if (children[i])
 1047                                 device_delete_child(dev, children[i]);
 1048                 free(children, M_TEMP);
 1049         }
 1050         mtx_destroy(&ch->state_mtx);
 1051         mtx_destroy(&ch->queue_mtx);
 1052         return (0);
 1053 }
 1054 
 1055 static void
 1056 ata_usbchannel_setmode(device_t parent, device_t dev)
 1057 {
 1058         struct atausb2_softc *sc = device_get_softc(GRANDPARENT(dev));
 1059         struct ata_device *atadev = device_get_softc(dev);
 1060 
 1061         if (sc->usb2_speed == USB_SPEED_HIGH)
 1062                 atadev->mode = ATA_USB2;
 1063         else
 1064                 atadev->mode = ATA_USB1;
 1065 }
 1066 
 1067 static int
 1068 ata_usbchannel_locking(device_t dev, int flags)
 1069 {
 1070         struct atausb2_softc *sc = device_get_softc(device_get_parent(dev));
 1071         struct ata_channel *ch = device_get_softc(dev);
 1072         int res = -1;
 1073 
 1074         mtx_lock(&sc->locked_mtx);
 1075         switch (flags) {
 1076         case ATA_LF_LOCK:
 1077                 if (sc->locked_ch == NULL)
 1078                         sc->locked_ch = ch;
 1079                 if (sc->locked_ch != ch)
 1080                         sc->restart_ch = ch;
 1081                 break;
 1082 
 1083         case ATA_LF_UNLOCK:
 1084                 if (sc->locked_ch == ch) {
 1085                         sc->locked_ch = NULL;
 1086                         if (sc->restart_ch) {
 1087                                 ch = sc->restart_ch;
 1088                                 sc->restart_ch = NULL;
 1089                                 mtx_unlock(&sc->locked_mtx);
 1090                                 ata_start(ch->dev);
 1091                                 return (res);
 1092                         }
 1093                 }
 1094                 break;
 1095 
 1096         case ATA_LF_WHICH:
 1097                 break;
 1098         }
 1099         if (sc->locked_ch) {
 1100                 res = sc->locked_ch->unit;
 1101         }
 1102         mtx_unlock(&sc->locked_mtx);
 1103         return (res);
 1104 }
 1105 
 1106 static device_method_t ata_usbchannel_methods[] = {
 1107         /* device interface */
 1108         DEVMETHOD(device_probe, ata_usbchannel_probe),
 1109         DEVMETHOD(device_attach, ata_usbchannel_attach),
 1110         DEVMETHOD(device_detach, ata_usbchannel_detach),
 1111 
 1112         /* ATA methods */
 1113         DEVMETHOD(ata_setmode, ata_usbchannel_setmode),
 1114         DEVMETHOD(ata_locking, ata_usbchannel_locking),
 1115         /* DEVMETHOD(ata_reset,            ata_usbchannel_reset), */
 1116 
 1117         {0, 0}
 1118 };
 1119 
 1120 static driver_t ata_usbchannel_driver = {
 1121         "ata",
 1122         ata_usbchannel_methods,
 1123         sizeof(struct ata_channel),
 1124 };
 1125 
 1126 DRIVER_MODULE(ata, atausb, ata_usbchannel_driver, ata_devclass, 0, 0);
 1127 MODULE_DEPEND(atausb, ata, 1, 1, 1);

Cache object: 110d7220ea48e2c245aa5f8b16b335c3


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.