The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


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

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

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1998 - 2004 Søren Schmidt <sos@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer,
   10  *    without modification, immediately at the beginning of the file.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 #include "opt_ata.h"
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/ata.h>
   36 #include <sys/kernel.h>
   37 #include <sys/endian.h>
   38 #include <sys/ctype.h>
   39 #include <sys/conf.h>
   40 #include <sys/bus.h>
   41 #include <sys/bio.h>
   42 #include <sys/malloc.h>
   43 #include <sys/sysctl.h>
   44 #include <sys/sema.h>
   45 #include <sys/taskqueue.h>
   46 #include <vm/uma.h>
   47 #include <machine/stdarg.h>
   48 #include <machine/resource.h>
   49 #include <machine/bus.h>
   50 #include <sys/rman.h>
   51 #ifdef __alpha__
   52 #include <machine/md_var.h>
   53 #endif
   54 #include <geom/geom_disk.h>
   55 #include <dev/ata/ata-all.h>
   56 #include <dev/ata/ata-disk.h>
   57 #include <dev/ata/ata-raid.h>
   58 
   59 /* device structures */
   60 static  d_ioctl_t       ata_ioctl;
   61 static struct cdevsw ata_cdevsw = {
   62         .d_version =    D_VERSION,
   63         .d_flags =      D_NEEDGIANT,
   64         .d_ioctl =      ata_ioctl,
   65         .d_name =       "ata",
   66 };
   67 
   68 /* prototypes */
   69 static void ata_shutdown(void *, int);
   70 static void ata_interrupt(void *);
   71 static int ata_getparam(struct ata_device *, u_int8_t);
   72 static void ata_identify_devices(struct ata_channel *);
   73 static void ata_boot_attach(void);
   74 static void bswap(int8_t *, int);
   75 static void btrim(int8_t *, int);
   76 static void bpack(int8_t *, int8_t *, int);
   77 static void ata_init(void);
   78 
   79 /* global vars */
   80 MALLOC_DEFINE(M_ATA, "ATA generic", "ATA driver generic layer");
   81 devclass_t ata_devclass;
   82 uma_zone_t ata_zone;
   83 int ata_wc = 1;
   84 
   85 /* local vars */
   86 static struct intr_config_hook *ata_delayed_attach = NULL;
   87 static int ata_dma = 1;
   88 static int atapi_dma = 0;
   89 static int ata_resuming = 0;
   90 
   91 /* sysctl vars */
   92 SYSCTL_NODE(_hw, OID_AUTO, ata, CTLFLAG_RD, 0, "ATA driver parameters");
   93 TUNABLE_INT("hw.ata.ata_dma", &ata_dma);
   94 SYSCTL_INT(_hw_ata, OID_AUTO, ata_dma, CTLFLAG_RDTUN, &ata_dma, 0,
   95            "ATA disk DMA mode control");
   96 TUNABLE_INT("hw.ata.wc", &ata_wc);
   97 SYSCTL_INT(_hw_ata, OID_AUTO, wc, CTLFLAG_RDTUN, &ata_wc, 0,
   98            "ATA disk write caching");
   99 TUNABLE_INT("hw.ata.atapi_dma", &atapi_dma);
  100 SYSCTL_INT(_hw_ata, OID_AUTO, atapi_dma, CTLFLAG_RDTUN, &atapi_dma, 0,
  101            "ATAPI device DMA mode control");
  102 
  103 /*
  104  * newbus device interface related functions
  105  */
  106 int
  107 ata_probe(device_t dev)
  108 {
  109     struct ata_channel *ch;
  110 
  111     if (!dev || !(ch = device_get_softc(dev)))
  112         return ENXIO;
  113 
  114     if (ch->r_irq)
  115         return EEXIST;
  116 
  117     return 0;
  118 }
  119 
  120 int
  121 ata_attach(device_t dev)
  122 {
  123     struct ata_channel *ch;
  124     int error, rid;
  125 
  126     if (!dev || !(ch = device_get_softc(dev)))
  127         return ENXIO;
  128 
  129     /* initialize the softc basics */
  130     ch->device[MASTER].channel = ch;
  131     ch->device[MASTER].unit = ATA_MASTER;
  132     ch->device[MASTER].mode = ATA_PIO;
  133     ch->device[SLAVE].channel = ch;
  134     ch->device[SLAVE].unit = ATA_SLAVE;
  135     ch->device[SLAVE].mode = ATA_PIO;
  136     ch->dev = dev;
  137     ch->state = ATA_IDLE;
  138     bzero(&ch->state_mtx, sizeof(struct mtx));
  139     mtx_init(&ch->state_mtx, "ATA state lock", NULL, MTX_DEF);
  140 
  141     /* initialise device(s) on this channel */
  142     while (ch->locking(ch, ATA_LF_LOCK) != ch->unit)
  143         tsleep(&error, PRIBIO, "ataatch", 1);
  144     ch->hw.reset(ch);
  145     ch->locking(ch, ATA_LF_UNLOCK);
  146 
  147     rid = ATA_IRQ_RID;
  148     ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  149                                        RF_SHAREABLE | RF_ACTIVE);
  150     if (!ch->r_irq) {
  151         ata_printf(ch, -1, "unable to allocate interrupt\n");
  152         return ENXIO;
  153     }
  154     if ((error = bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS,
  155                                 ata_interrupt, ch, &ch->ih))) {
  156         ata_printf(ch, -1, "unable to setup interrupt\n");
  157         return error;
  158     }
  159 
  160     /* initialize queue and associated lock */
  161     bzero(&ch->queue_mtx, sizeof(struct mtx));
  162     mtx_init(&ch->queue_mtx, "ATA queue lock", NULL, MTX_DEF);
  163     TAILQ_INIT(&ch->ata_queue);
  164 
  165     /* do not attach devices if we are in early boot */
  166     if (ata_delayed_attach)
  167         return 0;
  168 
  169     ata_identify_devices(ch);
  170 
  171     if (ch->device[MASTER].attach)
  172         ch->device[MASTER].attach(&ch->device[MASTER]);
  173     if (ch->device[SLAVE].attach)
  174         ch->device[SLAVE].attach(&ch->device[SLAVE]);
  175 #ifdef DEV_ATAPICAM
  176     atapi_cam_attach_bus(ch);
  177 #endif
  178     return 0;
  179 }
  180 
  181 int
  182 ata_detach(device_t dev)
  183 {
  184     struct ata_channel *ch;
  185 
  186     if (!dev || !(ch = device_get_softc(dev)) || !ch->r_irq)
  187         return ENXIO;
  188 
  189     /* mark devices on this channel as detaching */
  190     ch->device[MASTER].flags |= ATA_D_DETACHING;
  191     ch->device[SLAVE].flags |= ATA_D_DETACHING;
  192 
  193     /* fail outstanding requests on this channel */
  194     ata_fail_requests(ch, NULL);
  195 
  196     /* unlock the channel */
  197     mtx_lock(&ch->state_mtx);
  198     ch->state = ATA_IDLE;
  199     mtx_unlock(&ch->state_mtx);
  200     ch->locking(ch, ATA_LF_UNLOCK);
  201 
  202     /* detach devices on this channel */
  203     if (ch->device[MASTER].detach)
  204         ch->device[MASTER].detach(&ch->device[MASTER]);
  205     if (ch->device[SLAVE].detach)
  206         ch->device[SLAVE].detach(&ch->device[SLAVE]);
  207 #ifdef DEV_ATAPICAM
  208     atapi_cam_detach_bus(ch);
  209 #endif
  210 
  211     /* flush cache and powerdown device */
  212     if (ch->device[MASTER].param) {
  213         if (ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
  214             ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
  215         ata_controlcmd(&ch->device[MASTER], ATA_SLEEP, 0, 0, 0);
  216         free(ch->device[MASTER].param, M_ATA);
  217         ch->device[MASTER].param = NULL;
  218     }
  219     if (ch->device[SLAVE].param) {
  220         if (ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
  221             ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
  222         ata_controlcmd(&ch->device[SLAVE], ATA_SLEEP, 0, 0, 0);
  223         free(ch->device[SLAVE].param, M_ATA);
  224         ch->device[SLAVE].param = NULL;
  225     }
  226     ch->device[MASTER].mode = ATA_PIO;
  227     ch->device[SLAVE].mode = ATA_PIO;
  228     ch->devices = 0;
  229 
  230     bus_teardown_intr(dev, ch->r_irq, ch->ih);
  231     bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
  232     ch->r_irq = NULL;
  233     mtx_destroy(&ch->queue_mtx);
  234     return 0;
  235 }
  236 
  237 int
  238 ata_reinit(struct ata_channel *ch)
  239 {
  240     int devices, misdev, newdev;
  241 
  242     if (!ch->r_irq)
  243         return ENXIO;
  244 
  245     if (bootverbose)
  246         ata_printf(ch, -1, "reiniting channel ..\n");
  247 
  248     /* poll for locking of this channel */
  249     while (ch->locking(ch, ATA_LF_LOCK) != ch->unit)
  250         tsleep(&devices, PRIBIO, "atarint", 1);
  251 
  252     ata_catch_inflight(ch);
  253 
  254     /* grap the channel lock no matter what */
  255     mtx_lock(&ch->state_mtx);
  256     ch->state = ATA_ACTIVE;
  257     mtx_unlock(&ch->state_mtx);
  258 
  259     if (ch->flags & ATA_IMMEDIATE_MODE)
  260         return EIO;
  261     else
  262         ch->flags |= ATA_IMMEDIATE_MODE;
  263 
  264     devices = ch->devices;
  265 
  266     ch->hw.reset(ch);
  267 
  268     if (bootverbose)
  269         ata_printf(ch, -1, "resetting done ..\n");
  270 
  271     /* detach what left the channel during reset */
  272     if ((misdev = devices & ~ch->devices)) {
  273         if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
  274             ch->device[MASTER].detach) {
  275             ata_fail_requests(ch, &ch->device[MASTER]);
  276             ch->device[MASTER].detach(&ch->device[MASTER]);
  277             free(ch->device[MASTER].param, M_ATA);
  278             ch->device[MASTER].param = NULL;
  279         }
  280         if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
  281             ch->device[SLAVE].detach) {
  282             ata_fail_requests(ch, &ch->device[SLAVE]);
  283             ch->device[SLAVE].detach(&ch->device[SLAVE]);
  284             free(ch->device[SLAVE].param, M_ATA);
  285             ch->device[SLAVE].param = NULL;
  286         }
  287     }
  288 
  289     /* identify what is present on the channel now */
  290     ata_identify_devices(ch);
  291 
  292     /* detach what left the channel during identify */
  293     if ((misdev = devices & ~ch->devices)) {
  294         if ((misdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
  295             ch->device[MASTER].detach) {
  296             ata_fail_requests(ch, &ch->device[MASTER]);
  297             ch->device[MASTER].detach(&ch->device[MASTER]);
  298             free(ch->device[MASTER].param, M_ATA);
  299             ch->device[MASTER].param = NULL;
  300         }
  301         if ((misdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
  302             ch->device[SLAVE].detach) {
  303             ata_fail_requests(ch, &ch->device[SLAVE]);
  304             ch->device[SLAVE].detach(&ch->device[SLAVE]);
  305             free(ch->device[SLAVE].param, M_ATA);
  306             ch->device[SLAVE].param = NULL;
  307         }
  308     }
  309 
  310     ch->flags &= ~ATA_IMMEDIATE_MODE;
  311     mtx_lock(&ch->state_mtx);
  312     ch->state = ATA_IDLE;
  313     mtx_unlock(&ch->state_mtx);
  314     ch->locking(ch, ATA_LF_UNLOCK);
  315 
  316     /* attach new devices */
  317     if ((newdev = ~devices & ch->devices)) {
  318         if ((newdev & (ATA_ATA_MASTER | ATA_ATAPI_MASTER)) &&
  319             ch->device[MASTER].attach)
  320             ch->device[MASTER].attach(&ch->device[MASTER]);
  321         if ((newdev & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE)) &&
  322             ch->device[SLAVE].attach)
  323             ch->device[SLAVE].attach(&ch->device[SLAVE]);
  324     }
  325 
  326 #ifdef DEV_ATAPICAM
  327     atapi_cam_reinit_bus(ch);
  328 #endif
  329 
  330     if (bootverbose)
  331         ata_printf(ch, -1, "device config done ..\n");
  332 
  333     ata_start(ch);
  334     return 0;
  335 }
  336 
  337 int
  338 ata_suspend(device_t dev)
  339 {
  340     struct ata_channel *ch;
  341 
  342     if (!dev || !(ch = device_get_softc(dev)))
  343         return ENXIO;
  344 
  345     while (1) {
  346         mtx_lock(&ch->state_mtx);
  347         if (ch->state == ATA_IDLE) {
  348             ch->state = ATA_ACTIVE;
  349             mtx_unlock(&ch->state_mtx);
  350             break;
  351         }
  352         mtx_unlock(&ch->state_mtx);
  353         tsleep(ch, PRIBIO, "atasusp", hz/10);
  354     }
  355     ch->locking(ch, ATA_LF_UNLOCK);
  356     return 0;
  357 }
  358 
  359 int
  360 ata_resume(device_t dev)
  361 {
  362     struct ata_channel *ch;
  363     int error;
  364 
  365     if (!dev || !(ch = device_get_softc(dev)))
  366         return ENXIO;
  367 
  368     ata_resuming = 1;
  369     error = ata_reinit(ch);
  370     ata_start(ch);
  371     ata_resuming = 0;
  372     return error;
  373 }
  374 
  375 static void
  376 ata_shutdown(void *arg, int howto)
  377 {
  378     struct ata_channel *ch;
  379     int ctlr;
  380 
  381     if (panicstr != NULL)
  382         return;
  383 
  384     /* flush cache on all devices */
  385     for (ctlr = 0; ctlr < devclass_get_maxunit(ata_devclass); ctlr++) {
  386         if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
  387             continue;
  388         if (ch->device[MASTER].param &&
  389             ch->device[MASTER].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
  390             ata_controlcmd(&ch->device[MASTER], ATA_FLUSHCACHE, 0, 0, 0);
  391         if (ch->device[SLAVE].param &&
  392             ch->device[SLAVE].param->support.command2 & ATA_SUPPORT_FLUSHCACHE)
  393             ata_controlcmd(&ch->device[SLAVE], ATA_FLUSHCACHE, 0, 0, 0);
  394     }
  395 }
  396 
  397 static void
  398 ata_interrupt(void *data)
  399 {
  400     struct ata_channel *ch = (struct ata_channel *)data;
  401     struct ata_request *request;
  402 
  403     mtx_lock(&ch->state_mtx);
  404     do {
  405         /* do we have a running request */
  406         if (!(request = ch->running))
  407             break;
  408 
  409         ATA_DEBUG_RQ(request, "interrupt");
  410 
  411         /* ignore interrupt if device is busy */
  412         if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
  413             DELAY(100);
  414             if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
  415                 break;
  416         }
  417 
  418         /* check for the right state */
  419         if (ch->state == ATA_ACTIVE) {
  420             request->flags |= ATA_R_INTR_SEEN;
  421             ch->state = ATA_INTERRUPT;
  422         }
  423         else {
  424             ata_prtdev(request->device,
  425                        "interrupt state=%d unexpected\n", ch->state);
  426             break;
  427         }
  428 
  429         if (ch->hw.end_transaction(request) == ATA_OP_FINISHED) {
  430             ch->running = NULL;
  431             if (ch->flags & ATA_IMMEDIATE_MODE)
  432                 ch->state = ATA_ACTIVE;
  433             else
  434                 ch->state = ATA_IDLE;
  435             mtx_unlock(&ch->state_mtx);
  436             ch->locking(ch, ATA_LF_UNLOCK);
  437             ata_finish(request);
  438             return;
  439         }
  440         else {
  441             request->flags &= ~ATA_R_INTR_SEEN;
  442             ch->state = ATA_ACTIVE;
  443         }
  444     } while (0);
  445     mtx_unlock(&ch->state_mtx);
  446 }
  447 
  448 /*
  449  * device related interfaces
  450  */
  451 static int
  452 ata_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
  453           int32_t flag, struct thread *td)
  454 {
  455     struct ata_cmd *iocmd = (struct ata_cmd *)addr;
  456     device_t device = devclass_get_device(ata_devclass, iocmd->channel);
  457     struct ata_channel *ch;
  458     struct ata_device *atadev;
  459     struct ata_request *request;
  460     caddr_t buf;
  461     int error = ENOTTY;
  462 
  463     if (cmd != IOCATA)
  464         return error;
  465 
  466     DROP_GIANT();
  467     switch (iocmd->cmd) {
  468     case ATAGMAXCHANNEL:
  469         iocmd->u.maxchan = devclass_get_maxunit(ata_devclass);
  470         error = 0;
  471         break;
  472 
  473     case ATAGPARM:
  474         if (!device || !(ch = device_get_softc(device))) {
  475             error = ENXIO;
  476             break;
  477         }
  478         iocmd->u.param.type[MASTER] =
  479             ch->devices & (ATA_ATA_MASTER | ATA_ATAPI_MASTER);
  480         iocmd->u.param.type[SLAVE] =
  481             ch->devices & (ATA_ATA_SLAVE | ATA_ATAPI_SLAVE);
  482         if (ch->device[MASTER].name)
  483             strcpy(iocmd->u.param.name[MASTER], ch->device[MASTER].name);
  484         if (ch->device[SLAVE].name)
  485             strcpy(iocmd->u.param.name[SLAVE], ch->device[SLAVE].name);
  486         if (ch->device[MASTER].param)
  487             bcopy(ch->device[MASTER].param, &iocmd->u.param.params[MASTER],
  488                   sizeof(struct ata_params));
  489         if (ch->device[SLAVE].param)
  490             bcopy(ch->device[SLAVE].param, &iocmd->u.param.params[SLAVE],
  491                   sizeof(struct ata_params));
  492         error = 0;
  493         break;
  494 
  495     case ATAGMODE:
  496         if (!device || !(ch = device_get_softc(device))) {
  497             error = ENXIO;
  498             break;
  499         }
  500         iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
  501         iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
  502         error = 0;
  503         break;
  504 
  505     case ATASMODE:
  506         if (!device || !(ch = device_get_softc(device))) {
  507             error = ENXIO;
  508             break;
  509         }
  510         if (iocmd->u.mode.mode[MASTER] >= 0 && ch->device[MASTER].param)
  511             ch->device[MASTER].setmode(&ch->device[MASTER],
  512                                        iocmd->u.mode.mode[MASTER]);
  513         iocmd->u.mode.mode[MASTER] = ch->device[MASTER].mode;
  514         if (iocmd->u.mode.mode[SLAVE] >= 0 && ch->device[SLAVE].param)
  515             ch->device[SLAVE].setmode(&ch->device[SLAVE],
  516                                       iocmd->u.mode.mode[SLAVE]);
  517         iocmd->u.mode.mode[SLAVE] = ch->device[SLAVE].mode;
  518         error = 0;
  519         break;
  520 
  521    case ATAREQUEST:
  522         if (!device || !(ch = device_get_softc(device))) {
  523             error = ENXIO;
  524             break;
  525         }
  526         if (!(atadev = &ch->device[iocmd->device])) {
  527             error = ENODEV;
  528             break;
  529         }
  530         if (!(buf = malloc(iocmd->u.request.count, M_ATA, M_NOWAIT))) {
  531             error = ENOMEM;
  532             break;
  533         }
  534         if (!(request = ata_alloc_request())) {
  535             error = ENOMEM;
  536             free(buf, M_ATA);
  537             break;
  538         }
  539         if (iocmd->u.request.flags & ATA_CMD_WRITE) {
  540             error = copyin(iocmd->u.request.data, buf, iocmd->u.request.count);
  541             if (error) {
  542                 free(buf, M_ATA);
  543                 ata_free_request(request);
  544                 break;
  545             }
  546         }
  547 
  548         request->device = atadev;
  549 
  550         if (iocmd->u.request.flags & ATA_CMD_ATAPI) {
  551             request->flags = ATA_R_ATAPI;
  552             bcopy(iocmd->u.request.u.atapi.ccb, request->u.atapi.ccb, 16);
  553         }
  554         else {
  555             request->u.ata.command = iocmd->u.request.u.ata.command;
  556             request->u.ata.feature = iocmd->u.request.u.ata.feature;
  557             request->u.ata.lba = iocmd->u.request.u.ata.lba;
  558             request->u.ata.count = iocmd->u.request.u.ata.count;
  559         }
  560 
  561         request->timeout = iocmd->u.request.timeout;
  562         request->data = buf;
  563         request->bytecount = iocmd->u.request.count;
  564         request->transfersize = request->bytecount;
  565 
  566         if (iocmd->u.request.flags & ATA_CMD_CONTROL)
  567             request->flags |= ATA_R_CONTROL;
  568         if (iocmd->u.request.flags & ATA_CMD_READ)
  569             request->flags |= ATA_R_READ;
  570         if (iocmd->u.request.flags & ATA_CMD_WRITE)
  571             request->flags |= ATA_R_WRITE;
  572 
  573         ata_queue_request(request);
  574 
  575         iocmd->u.request.u.ata.command = request->u.ata.command;
  576         iocmd->u.request.u.ata.feature = request->u.ata.feature;
  577         iocmd->u.request.u.ata.lba = request->u.ata.lba;
  578         iocmd->u.request.u.ata.count = request->u.ata.count;
  579         if (request->result)
  580             iocmd->u.request.error = request->result;
  581         else {
  582             if (iocmd->u.request.flags & ATA_CMD_READ)
  583                 error = copyout(buf,
  584                                 iocmd->u.request.data, iocmd->u.request.count);
  585             else
  586                 error = 0;
  587         }
  588         free(buf, M_ATA);
  589         ata_free_request(request);
  590         break;
  591 
  592     case ATAREINIT:
  593         if (!device || !(ch = device_get_softc(device))) {
  594             error = ENXIO;
  595             break;
  596         }
  597         error = ata_reinit(ch);
  598         ata_start(ch);
  599         break;
  600 
  601     case ATAATTACH:
  602         if (!device) {
  603             error =  ENXIO;
  604             break;
  605         }
  606         /* SOS should enable channel HW on controller XXX */
  607         error = ata_probe(device);
  608         if (!error)
  609             error = ata_attach(device);
  610         break;
  611 
  612     case ATADETACH:
  613         if (!device) {
  614             error = ENXIO;
  615             break;
  616         }
  617         error = ata_detach(device);
  618         /* SOS should disable channel HW on controller XXX */
  619         break;
  620 
  621 
  622 #ifdef DEV_ATARAID
  623     case ATARAIDCREATE:
  624         error = ata_raid_create(&iocmd->u.raid_setup);
  625         break;
  626 
  627     case ATARAIDDELETE:
  628         error = ata_raid_delete(iocmd->channel);
  629         break;
  630 
  631     case ATARAIDSTATUS:
  632         error = ata_raid_status(iocmd->channel, &iocmd->u.raid_status);
  633         break;
  634 
  635     case ATARAIDADDSPARE:
  636         error = ata_raid_addspare(iocmd->channel, iocmd->u.raid_spare.disk);
  637         break;
  638 
  639     case ATARAIDREBUILD:
  640         error = ata_raid_rebuild(iocmd->channel);
  641         break;
  642 #endif
  643     }
  644     PICKUP_GIANT();
  645     return error;
  646 }
  647 
  648 /*
  649  * device probe functions
  650  */
  651 static int
  652 ata_getparam(struct ata_device *atadev, u_int8_t command)
  653 {
  654     struct ata_request *request;
  655     int error = ENOMEM;
  656 
  657     if (!atadev->param)
  658         atadev->param = malloc(sizeof(struct ata_params), M_ATA, M_NOWAIT);
  659     if (atadev->param) {
  660         request = ata_alloc_request();
  661         if (request) {
  662             int retries = 2;
  663             while (retries-- > 0) {
  664                 request->device = atadev;
  665                 request->timeout = 5;
  666                 request->retries = 0;
  667                 request->u.ata.command = command;
  668                 request->flags = (ATA_R_READ | ATA_R_IMMEDIATE);
  669                 request->data = (caddr_t)atadev->param;
  670                 request->bytecount = sizeof(struct ata_params);
  671                 request->donecount = 0;
  672                 request->transfersize = DEV_BSIZE;
  673                 ata_queue_request(request);
  674                 if (!(error = request->result))
  675                     break;
  676             }
  677             ata_free_request(request);
  678         }
  679         if (!error && (isprint(atadev->param->model[0]) ||
  680                        isprint(atadev->param->model[1]))) {
  681             struct ata_params *atacap = atadev->param;
  682 #if BYTE_ORDER == BIG_ENDIAN
  683             int16_t *ptr;
  684 
  685             for (ptr = (int16_t *)atacap;
  686                  ptr < (int16_t *)atacap + sizeof(struct ata_params)/2; ptr++) {
  687                 *ptr = bswap16(*ptr);
  688             }
  689 #endif
  690             if (!(!strncmp(atacap->model, "FX", 2) ||
  691                   !strncmp(atacap->model, "NEC", 3) ||
  692                   !strncmp(atacap->model, "Pioneer", 7) ||
  693                   !strncmp(atacap->model, "SHARP", 5))) {
  694                 bswap(atacap->model, sizeof(atacap->model));
  695                 bswap(atacap->revision, sizeof(atacap->revision));
  696                 bswap(atacap->serial, sizeof(atacap->serial));
  697             }
  698             btrim(atacap->model, sizeof(atacap->model));
  699             bpack(atacap->model, atacap->model, sizeof(atacap->model));
  700             btrim(atacap->revision, sizeof(atacap->revision));
  701             bpack(atacap->revision, atacap->revision, sizeof(atacap->revision));
  702             btrim(atacap->serial, sizeof(atacap->serial));
  703             bpack(atacap->serial, atacap->serial, sizeof(atacap->serial));
  704             if (bootverbose)
  705                 ata_prtdev(atadev,
  706                            "pio=0x%02x wdma=0x%02x udma=0x%02x cable=%spin\n",
  707                            ata_pmode(atacap), ata_wmode(atacap),
  708                            ata_umode(atacap),
  709                            (atacap->hwres & ATA_CABLE_ID) ? "80":"40");
  710         }
  711         else {
  712             if (!error)
  713                 error = ENXIO;
  714             if (atadev->param) {
  715                 free(atadev->param, M_ATA);
  716                 atadev->param = NULL;
  717             }
  718         }
  719     }
  720     return error;
  721 }
  722 
  723 static void
  724 ata_identify_devices(struct ata_channel *ch)
  725 {
  726     if (ch->devices & ATA_ATA_SLAVE) {
  727         if (ata_getparam(&ch->device[SLAVE], ATA_ATA_IDENTIFY))
  728             ch->devices &= ~ATA_ATA_SLAVE;
  729 #ifdef DEV_ATADISK
  730         else
  731             ch->device[SLAVE].attach = ad_attach;
  732 #endif
  733     }
  734     if (ch->devices & ATA_ATAPI_SLAVE) {
  735         if (ata_getparam(&ch->device[SLAVE], ATA_ATAPI_IDENTIFY))
  736             ch->devices &= ~ATA_ATAPI_SLAVE;
  737         else {
  738             ata_controlcmd(&ch->device[SLAVE], ATA_ATAPI_RESET, 0, 0, 0);
  739             switch (ch->device[SLAVE].param->config & ATA_ATAPI_TYPE_MASK) {
  740 #ifdef DEV_ATAPICD
  741             case ATA_ATAPI_TYPE_CDROM:
  742                 ch->device[SLAVE].attach = acd_attach;
  743                 break;
  744 #endif
  745 #ifdef DEV_ATAPIFD
  746             case ATA_ATAPI_TYPE_DIRECT:
  747                 ch->device[SLAVE].attach = afd_attach;
  748                 break;
  749 #endif
  750 #ifdef DEV_ATAPIST
  751             case ATA_ATAPI_TYPE_TAPE:
  752                 ch->device[SLAVE].attach = ast_attach;
  753                 break;
  754 #endif
  755             }
  756         }
  757     }
  758     if (ch->devices & ATA_ATA_MASTER) {
  759         if (ata_getparam(&ch->device[MASTER], ATA_ATA_IDENTIFY))
  760             ch->devices &= ~ATA_ATA_MASTER;
  761 #ifdef DEV_ATADISK
  762         else
  763             ch->device[MASTER].attach = ad_attach;
  764 #endif
  765     }
  766     if (ch->devices & ATA_ATAPI_MASTER) {
  767         if (ata_getparam(&ch->device[MASTER], ATA_ATAPI_IDENTIFY))
  768             ch->devices &= ~ATA_ATAPI_MASTER;
  769         else {
  770             ata_controlcmd(&ch->device[MASTER], ATA_ATAPI_RESET, 0, 0, 0);
  771             switch (ch->device[MASTER].param->config & ATA_ATAPI_TYPE_MASK) {
  772 #ifdef DEV_ATAPICD
  773             case ATA_ATAPI_TYPE_CDROM:
  774                 ch->device[MASTER].attach = acd_attach;
  775                 break;
  776 #endif
  777 #ifdef DEV_ATAPIFD
  778             case ATA_ATAPI_TYPE_DIRECT:
  779                 ch->device[MASTER].attach = afd_attach;
  780                 break;
  781 #endif
  782 #ifdef DEV_ATAPIST
  783             case ATA_ATAPI_TYPE_TAPE:
  784                 ch->device[MASTER].attach = ast_attach;
  785                 break;
  786 #endif
  787             }
  788         }
  789     }
  790 
  791     /* setup basic transfer mode by setting PIO mode and DMA if supported */
  792     if (ch->device[MASTER].param) {
  793         ch->device[MASTER].setmode(&ch->device[MASTER], ATA_PIO_MAX);
  794         if ((((ch->devices & ATA_ATAPI_MASTER) && atapi_dma &&
  795               (ch->device[MASTER].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR &&
  796               ata_umode(ch->device[MASTER].param) >= ATA_UDMA2) ||
  797              ((ch->devices & ATA_ATA_MASTER) && ata_dma)) && ch->dma)
  798             ch->device[MASTER].setmode(&ch->device[MASTER], ATA_DMA_MAX);
  799 
  800     }
  801     if (ch->device[SLAVE].param) {
  802         ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_PIO_MAX);
  803         if ((((ch->devices & ATA_ATAPI_SLAVE) && atapi_dma &&
  804               (ch->device[SLAVE].param->config&ATA_DRQ_MASK) != ATA_DRQ_INTR &&
  805               ata_umode(ch->device[SLAVE].param) >= ATA_UDMA2) ||
  806              ((ch->devices & ATA_ATA_SLAVE) && ata_dma)) && ch->dma)
  807             ch->device[SLAVE].setmode(&ch->device[SLAVE], ATA_DMA_MAX);
  808     }
  809 }
  810 
  811 static void
  812 ata_boot_attach(void)
  813 {
  814     struct ata_channel *ch;
  815     int ctlr;
  816 
  817     if (ata_delayed_attach) {
  818         config_intrhook_disestablish(ata_delayed_attach);
  819         free(ata_delayed_attach, M_TEMP);
  820         ata_delayed_attach = NULL;
  821     }
  822 
  823     /*
  824      * run through all ata devices and look for real ATA & ATAPI devices
  825      * using the hints we found in the early probe, this avoids some of
  826      * the delays probing of non-exsistent devices can cause.
  827      */
  828     for (ctlr=0; ctlr<devclass_get_maxunit(ata_devclass); ctlr++) {
  829         if (!(ch = devclass_get_softc(ata_devclass, ctlr)))
  830             continue;
  831         ata_identify_devices(ch);
  832         if (ch->device[MASTER].attach)
  833             ch->device[MASTER].attach(&ch->device[MASTER]);
  834         if (ch->device[SLAVE].attach)
  835             ch->device[SLAVE].attach(&ch->device[SLAVE]);
  836 #ifdef DEV_ATAPICAM
  837         atapi_cam_attach_bus(ch);
  838 #endif
  839     }
  840 #ifdef DEV_ATARAID
  841     ata_raid_attach();
  842 #endif
  843 }
  844 
  845 /*
  846  * misc support functions
  847  */
  848 void
  849 ata_udelay(int interval)
  850 {
  851     if (interval < (1000000/hz) || ata_delayed_attach || ata_resuming)
  852         DELAY(interval);
  853     else
  854         tsleep(&interval, PRIBIO, "ataslp", interval/(1000000/hz));
  855 }
  856 
  857 static void
  858 bswap(int8_t *buf, int len)
  859 {
  860     u_int16_t *ptr = (u_int16_t*)(buf + len);
  861 
  862     while (--ptr >= (u_int16_t*)buf)
  863         *ptr = ntohs(*ptr);
  864 }
  865 
  866 static void
  867 btrim(int8_t *buf, int len)
  868 {
  869     int8_t *ptr;
  870 
  871     for (ptr = buf; ptr < buf+len; ++ptr)
  872         if (!*ptr || *ptr == '_')
  873             *ptr = ' ';
  874     for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
  875         *ptr = 0;
  876 }
  877 
  878 static void
  879 bpack(int8_t *src, int8_t *dst, int len)
  880 {
  881     int i, j, blank;
  882 
  883     for (i = j = blank = 0 ; i < len; i++) {
  884         if (blank && src[i] == ' ') continue;
  885         if (blank && src[i] != ' ') {
  886             dst[j++] = src[i];
  887             blank = 0;
  888             continue;
  889         }
  890         if (src[i] == ' ') {
  891             blank = 1;
  892             if (i == 0)
  893                 continue;
  894         }
  895         dst[j++] = src[i];
  896     }
  897     if (j < len)
  898         dst[j] = 0x00;
  899 }
  900 
  901 int
  902 ata_printf(struct ata_channel *ch, int device, const char * fmt, ...)
  903 {
  904     va_list ap;
  905     int ret;
  906 
  907     if (device == -1)
  908         ret = printf("ata%d: ", device_get_unit(ch->dev));
  909     else {
  910         if (ch->device[ATA_DEV(device)].name)
  911             ret = printf("%s: ", ch->device[ATA_DEV(device)].name);
  912         else
  913             ret = printf("ata%d-%s: ", device_get_unit(ch->dev),
  914                          (device == ATA_MASTER) ? "master" : "slave");
  915     }
  916     va_start(ap, fmt);
  917     ret += vprintf(fmt, ap);
  918     va_end(ap);
  919     return ret;
  920 }
  921 
  922 int
  923 ata_prtdev(struct ata_device *atadev, const char * fmt, ...)
  924 {
  925     va_list ap;
  926     int ret;
  927 
  928     if (atadev->name)
  929         ret = printf("%s: ", atadev->name);
  930     else
  931         ret = printf("ata%d-%s: ", device_get_unit(atadev->channel->dev),
  932                      (atadev->unit == ATA_MASTER) ? "master" : "slave");
  933     va_start(ap, fmt);
  934     ret += vprintf(fmt, ap);
  935     va_end(ap);
  936     return ret;
  937 }
  938 
  939 void
  940 ata_set_name(struct ata_device *atadev, char *name, int lun)
  941 {
  942     atadev->name = malloc(strlen(name) + 4, M_ATA, M_NOWAIT);
  943     if (atadev->name)
  944         sprintf(atadev->name, "%s%d", name, lun);
  945 }
  946 
  947 void
  948 ata_free_name(struct ata_device *atadev)
  949 {
  950     if (atadev->name)
  951         free(atadev->name, M_ATA);
  952     atadev->name = NULL;
  953 }
  954 
  955 int
  956 ata_get_lun(u_int32_t *map)
  957 {
  958     int lun = ffs(~*map) - 1;
  959 
  960     *map |= (1 << lun);
  961     return lun;
  962 }
  963 
  964 int
  965 ata_test_lun(u_int32_t *map, int lun)
  966 {
  967     return (*map & (1 << lun));
  968 }
  969 
  970 void
  971 ata_free_lun(u_int32_t *map, int lun)
  972 {
  973     *map &= ~(1 << lun);
  974 }
  975 
  976 char *
  977 ata_mode2str(int mode)
  978 {
  979     switch (mode) {
  980     case ATA_PIO: return "BIOSPIO";
  981     case ATA_PIO0: return "PIO0";
  982     case ATA_PIO1: return "PIO1";
  983     case ATA_PIO2: return "PIO2";
  984     case ATA_PIO3: return "PIO3";
  985     case ATA_PIO4: return "PIO4";
  986     case ATA_DMA: return "BIOSDMA";
  987     case ATA_WDMA0: return "WDMA0";
  988     case ATA_WDMA1: return "WDMA1";
  989     case ATA_WDMA2: return "WDMA2";
  990     case ATA_UDMA0: return "UDMA16";
  991     case ATA_UDMA1: return "UDMA25";
  992     case ATA_UDMA2: return "UDMA33";
  993     case ATA_UDMA3: return "UDMA40";
  994     case ATA_UDMA4: return "UDMA66";
  995     case ATA_UDMA5: return "UDMA100";
  996     case ATA_UDMA6: return "UDMA133";
  997     case ATA_SA150: return "SATA150";
  998     default: return "???";
  999     }
 1000 }
 1001 
 1002 int
 1003 ata_pmode(struct ata_params *ap)
 1004 {
 1005     if (ap->atavalid & ATA_FLAG_64_70) {
 1006         if (ap->apiomodes & 0x02)
 1007             return ATA_PIO4;
 1008         if (ap->apiomodes & 0x01)
 1009             return ATA_PIO3;
 1010     }
 1011     if (ap->mwdmamodes & 0x04)
 1012         return ATA_PIO4;
 1013     if (ap->mwdmamodes & 0x02)
 1014         return ATA_PIO3;
 1015     if (ap->mwdmamodes & 0x01)
 1016         return ATA_PIO2;
 1017     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
 1018         return ATA_PIO2;
 1019     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
 1020         return ATA_PIO1;
 1021     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
 1022         return ATA_PIO0;
 1023     return ATA_PIO0;
 1024 }
 1025 
 1026 int
 1027 ata_wmode(struct ata_params *ap)
 1028 {
 1029     if (ap->mwdmamodes & 0x04)
 1030         return ATA_WDMA2;
 1031     if (ap->mwdmamodes & 0x02)
 1032         return ATA_WDMA1;
 1033     if (ap->mwdmamodes & 0x01)
 1034         return ATA_WDMA0;
 1035     return -1;
 1036 }
 1037 
 1038 int
 1039 ata_umode(struct ata_params *ap)
 1040 {
 1041     if (ap->atavalid & ATA_FLAG_88) {
 1042         if (ap->udmamodes & 0x40)
 1043             return ATA_UDMA6;
 1044         if (ap->udmamodes & 0x20)
 1045             return ATA_UDMA5;
 1046         if (ap->udmamodes & 0x10)
 1047             return ATA_UDMA4;
 1048         if (ap->udmamodes & 0x08)
 1049             return ATA_UDMA3;
 1050         if (ap->udmamodes & 0x04)
 1051             return ATA_UDMA2;
 1052         if (ap->udmamodes & 0x02)
 1053             return ATA_UDMA1;
 1054         if (ap->udmamodes & 0x01)
 1055             return ATA_UDMA0;
 1056     }
 1057     return -1;
 1058 }
 1059 
 1060 int
 1061 ata_limit_mode(struct ata_device *atadev, int mode, int maxmode)
 1062 {
 1063     if (maxmode && mode > maxmode)
 1064         mode = maxmode;
 1065 
 1066     if (mode >= ATA_UDMA0 && ata_umode(atadev->param) > 0)
 1067         return min(mode, ata_umode(atadev->param));
 1068 
 1069     if (mode >= ATA_WDMA0 && ata_wmode(atadev->param) > 0)
 1070         return min(mode, ata_wmode(atadev->param));
 1071 
 1072     if (mode > ata_pmode(atadev->param))
 1073         return min(mode, ata_pmode(atadev->param));
 1074 
 1075     return mode;
 1076 }
 1077 
 1078 static void
 1079 ata_init(void)
 1080 {
 1081     /* register controlling device */
 1082     make_dev(&ata_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "ata");
 1083 
 1084     /* register boot attach to be run when interrupts are enabled */
 1085     if (!(ata_delayed_attach = (struct intr_config_hook *)
 1086                                malloc(sizeof(struct intr_config_hook),
 1087                                       M_TEMP, M_NOWAIT | M_ZERO))) {
 1088         printf("ata: malloc of delayed attach hook failed\n");
 1089         return;
 1090     }
 1091     ata_delayed_attach->ich_func = (void*)ata_boot_attach;
 1092     if (config_intrhook_establish(ata_delayed_attach) != 0) {
 1093         printf("ata: config_intrhook_establish failed\n");
 1094         free(ata_delayed_attach, M_TEMP);
 1095     }
 1096 
 1097     /* register handler to flush write caches on shutdown */
 1098     if ((EVENTHANDLER_REGISTER(shutdown_post_sync, ata_shutdown,
 1099                                NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
 1100         printf("ata: shutdown event registration failed!\n");
 1101 
 1102     /* init our UMA zone for ATA requests */
 1103     ata_zone = uma_zcreate("ata_request", sizeof(struct ata_request),
 1104                            NULL, NULL, NULL, NULL, 0, 0);
 1105 }
 1106 SYSINIT(atadev, SI_SUB_DRIVERS, SI_ORDER_SECOND, ata_init, NULL)

Cache object: b4cea2b8f946ae8927c8617810c3e6a5


[ 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.