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-pci.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 - 2008 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  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD$");
   29 
   30 #include "opt_ata.h"
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/kernel.h>
   34 #include <sys/module.h>
   35 #include <sys/ata.h>
   36 #include <sys/bus.h>
   37 #include <sys/conf.h>
   38 #include <sys/malloc.h>
   39 #include <sys/sema.h>
   40 #include <sys/taskqueue.h>
   41 #include <vm/uma.h>
   42 #include <machine/stdarg.h>
   43 #include <machine/resource.h>
   44 #include <machine/bus.h>
   45 #include <sys/rman.h>
   46 #include <dev/pci/pcivar.h>
   47 #include <dev/pci/pcireg.h>
   48 #include <dev/ata/ata-all.h>
   49 #include <dev/ata/ata-pci.h>
   50 #include <ata_if.h>
   51 
   52 MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
   53 
   54 /* misc defines */
   55 #define IOMASK                  0xfffffffc
   56 
   57 /*
   58  * generic PCI ATA device probe
   59  */
   60 int
   61 ata_pci_probe(device_t dev)
   62 {
   63     struct ata_pci_controller *ctlr = device_get_softc(dev);
   64     char buffer[64];
   65 
   66     /* is this a storage class device ? */
   67     if (pci_get_class(dev) != PCIC_STORAGE)
   68         return (ENXIO);
   69 
   70     /* is this an IDE/ATA type device ? */
   71     if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
   72         return (ENXIO);
   73     
   74     sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
   75     device_set_desc_copy(dev, buffer);
   76     ctlr->chipinit = ata_generic_chipinit;
   77 
   78     /* we are a low priority handler */
   79     return (BUS_PROBE_GENERIC);
   80 }
   81 
   82 int
   83 ata_pci_attach(device_t dev)
   84 {
   85     struct ata_pci_controller *ctlr = device_get_softc(dev);
   86     device_t child;
   87     u_int32_t cmd;
   88     int unit;
   89 
   90     /* do chipset specific setups only needed once */
   91     ctlr->legacy = ata_legacy(dev);
   92     if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
   93         ctlr->channels = 2;
   94     else
   95         ctlr->channels = 1;
   96     ctlr->ichannels = -1;
   97     ctlr->ch_attach = ata_pci_ch_attach;
   98     ctlr->ch_detach = ata_pci_ch_detach;
   99     ctlr->dev = dev;
  100 
  101     /* if needed try to enable busmastering */
  102     pci_enable_busmaster(dev);
  103     cmd = pci_read_config(dev, PCIR_COMMAND, 2);
  104 
  105     /* if busmastering mode "stuck" use it */
  106     if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
  107         ctlr->r_type1 = SYS_RES_IOPORT;
  108         ctlr->r_rid1 = ATA_BMADDR_RID;
  109         ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
  110                                               RF_ACTIVE);
  111     }
  112 
  113     if (ctlr->chipinit(dev))
  114         return ENXIO;
  115 
  116     /* attach all channels on this controller */
  117     for (unit = 0; unit < ctlr->channels; unit++) {
  118         if ((ctlr->ichannels & (1 << unit)) == 0)
  119             continue;
  120         child = device_add_child(dev, "ata",
  121             ((unit == 0 || unit == 1) && ctlr->legacy) ?
  122             unit : devclass_find_free_unit(ata_devclass, 2));
  123         if (child == NULL)
  124             device_printf(dev, "failed to add ata child device\n");
  125         else
  126             device_set_ivars(child, (void *)(intptr_t)unit);
  127     }
  128     bus_generic_attach(dev);
  129     return 0;
  130 }
  131 
  132 int
  133 ata_pci_detach(device_t dev)
  134 {
  135     struct ata_pci_controller *ctlr = device_get_softc(dev);
  136 
  137     /* detach & delete all children */
  138     device_delete_children(dev);
  139 
  140     if (ctlr->r_irq) {
  141         bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
  142         bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
  143         if (ctlr->r_irq_rid != ATA_IRQ_RID)
  144             pci_release_msi(dev);
  145     }
  146     if (ctlr->chipdeinit != NULL)
  147         ctlr->chipdeinit(dev);
  148     if (ctlr->r_res2) {
  149 #ifdef __sparc64__
  150         bus_space_unmap(rman_get_bustag(ctlr->r_res2),
  151             rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2));
  152 #endif
  153         bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
  154     }
  155     if (ctlr->r_res1) {
  156 #ifdef __sparc64__
  157         bus_space_unmap(rman_get_bustag(ctlr->r_res1),
  158             rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1));
  159 #endif
  160         bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
  161     }
  162 
  163     return 0;
  164 }
  165 
  166 int
  167 ata_pci_suspend(device_t dev)
  168 {
  169     struct ata_pci_controller *ctlr = device_get_softc(dev);
  170     int error = 0;
  171  
  172     bus_generic_suspend(dev);
  173     if (ctlr->suspend)
  174         error = ctlr->suspend(dev);
  175     return error;
  176 }
  177   
  178 int
  179 ata_pci_resume(device_t dev)
  180 {
  181     struct ata_pci_controller *ctlr = device_get_softc(dev);
  182     int error = 0;
  183  
  184     if (ctlr->resume)
  185         error = ctlr->resume(dev);
  186     bus_generic_resume(dev);
  187     return error;
  188 }
  189 
  190 int
  191 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
  192 {
  193 
  194         return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
  195 }
  196 
  197 int
  198 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
  199 {
  200 
  201         return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
  202 }
  203 
  204 uint32_t
  205 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
  206 {
  207 
  208         return (pci_read_config(dev, reg, width));
  209 }
  210 
  211 void
  212 ata_pci_write_config(device_t dev, device_t child, int reg, 
  213     uint32_t val, int width)
  214 {
  215 
  216         pci_write_config(dev, reg, val, width);
  217 }
  218 
  219 struct resource *
  220 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
  221                        u_long start, u_long end, u_long count, u_int flags)
  222 {
  223         struct ata_pci_controller *controller = device_get_softc(dev);
  224         struct resource *res = NULL;
  225 
  226         if (device_get_devclass(child) == ata_devclass) {
  227                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
  228                 int myrid;
  229 
  230                 if (type == SYS_RES_IOPORT) {
  231                         switch (*rid) {
  232                         case ATA_IOADDR_RID:
  233                             if (controller->legacy) {
  234                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
  235                                 count = ATA_IOSIZE;
  236                                 end = start + count - 1;
  237                             }
  238                             myrid = PCIR_BAR(0) + (unit << 3);
  239                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
  240                                 SYS_RES_IOPORT, &myrid,
  241                                 start, end, count, flags);
  242                             break;
  243                         case ATA_CTLADDR_RID:
  244                             if (controller->legacy) {
  245                                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
  246                                     ATA_CTLOFFSET;
  247                                 count = ATA_CTLIOSIZE;
  248                                 end = start + count - 1;
  249                             }
  250                             myrid = PCIR_BAR(1) + (unit << 3);
  251                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
  252                                 SYS_RES_IOPORT, &myrid,
  253                                 start, end, count, flags);
  254                             break;
  255                         }
  256                 }
  257                 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
  258                         if (controller->legacy) {
  259                             int irq = (unit == 0 ? 14 : 15);
  260             
  261                             res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
  262                                 SYS_RES_IRQ, rid, irq, irq, 1, flags);
  263                         } else
  264                             res = controller->r_irq;
  265                 }
  266         } else {
  267                 if (type == SYS_RES_IRQ) {
  268                         if (*rid != ATA_IRQ_RID)
  269                                 return (NULL);
  270                         res = controller->r_irq;
  271                 } else {
  272                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
  273                              type, rid, start, end, count, flags);
  274                 }
  275         }
  276         return (res);
  277 }
  278 
  279 int
  280 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
  281                          struct resource *r)
  282 {
  283 
  284         if (device_get_devclass(child) == ata_devclass) {
  285                 struct ata_pci_controller *controller = device_get_softc(dev);
  286                 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
  287 
  288                 if (type == SYS_RES_IOPORT) {
  289                         switch (rid) {
  290                         case ATA_IOADDR_RID:
  291                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
  292                                 SYS_RES_IOPORT,
  293                                 PCIR_BAR(0) + (unit << 3), r);
  294                         case ATA_CTLADDR_RID:
  295                             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
  296                                 SYS_RES_IOPORT,
  297                                 PCIR_BAR(1) + (unit << 3), r);
  298                         default:
  299                             return ENOENT;
  300                         }
  301                 }
  302                 if (type == SYS_RES_IRQ) {
  303                         if (rid != ATA_IRQ_RID)
  304                                 return ENOENT;
  305                         if (controller->legacy) {
  306                                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
  307                                     SYS_RES_IRQ, rid, r);
  308                         } else  
  309                                 return 0;
  310                 }
  311         } else {
  312                 if (type == SYS_RES_IRQ) {
  313                         if (rid != ATA_IRQ_RID)
  314                                 return (ENOENT);
  315                         return (0);
  316                 } else {
  317                         return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
  318                             type, rid, r));
  319                 }
  320         }
  321         return (EINVAL);
  322 }
  323 
  324 int
  325 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
  326                    int flags, driver_filter_t *filter, driver_intr_t *function, 
  327                    void *argument, void **cookiep)
  328 {
  329         struct ata_pci_controller *controller = device_get_softc(dev);
  330 
  331         if (controller->legacy) {
  332                 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
  333                               flags, filter, function, argument, cookiep);
  334         } else {
  335                 struct ata_pci_controller *controller = device_get_softc(dev);
  336                 int unit;
  337 
  338                 if (filter != NULL) {
  339                         printf("ata-pci.c: we cannot use a filter here\n");
  340                         return (EINVAL);
  341                 }
  342                 if (device_get_devclass(child) == ata_devclass)
  343                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
  344                 else
  345                         unit = ATA_PCI_MAX_CH - 1;
  346                 controller->interrupt[unit].function = function;
  347                 controller->interrupt[unit].argument = argument;
  348                 *cookiep = controller;
  349                 return 0;
  350         }
  351 }
  352 
  353 int
  354 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
  355                       void *cookie)
  356 {
  357         struct ata_pci_controller *controller = device_get_softc(dev);
  358 
  359         if (controller->legacy) {
  360                 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
  361         } else {
  362                 struct ata_pci_controller *controller = device_get_softc(dev);
  363                 int unit;
  364 
  365                 if (device_get_devclass(child) == ata_devclass)
  366                         unit = ((struct ata_channel *)device_get_softc(child))->unit;
  367                 else
  368                         unit = ATA_PCI_MAX_CH - 1;
  369                 controller->interrupt[unit].function = NULL;
  370                 controller->interrupt[unit].argument = NULL;
  371                 return 0;
  372         }
  373 }
  374     
  375 int
  376 ata_generic_setmode(device_t dev, int target, int mode)
  377 {
  378 
  379         return (min(mode, ATA_UDMA2));
  380 }
  381 
  382 int
  383 ata_generic_chipinit(device_t dev)
  384 {
  385     struct ata_pci_controller *ctlr = device_get_softc(dev);
  386 
  387     if (ata_setup_interrupt(dev, ata_generic_intr))
  388         return ENXIO;
  389     ctlr->setmode = ata_generic_setmode;
  390     return 0;
  391 }
  392 
  393 int
  394 ata_pci_ch_attach(device_t dev)
  395 {
  396     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  397     struct ata_channel *ch = device_get_softc(dev);
  398     struct resource *io = NULL, *ctlio = NULL;
  399     int i, rid;
  400 
  401     rid = ATA_IOADDR_RID;
  402     if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
  403         return ENXIO;
  404 
  405     rid = ATA_CTLADDR_RID;
  406     if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
  407         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
  408         return ENXIO;
  409     }
  410 
  411     ata_pci_dmainit(dev);
  412 
  413     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
  414         ch->r_io[i].res = io;
  415         ch->r_io[i].offset = i;
  416     }
  417     ch->r_io[ATA_CONTROL].res = ctlio;
  418     ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
  419     ch->r_io[ATA_IDX_ADDR].res = io;
  420     ata_default_registers(dev);
  421     if (ctlr->r_res1) {
  422         for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
  423             ch->r_io[i].res = ctlr->r_res1;
  424             ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
  425         }
  426     }
  427 
  428     ata_pci_hw(dev);
  429     return 0;
  430 }
  431 
  432 int
  433 ata_pci_ch_detach(device_t dev)
  434 {
  435     struct ata_channel *ch = device_get_softc(dev);
  436 
  437     ata_pci_dmafini(dev);
  438 
  439     bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
  440         ch->r_io[ATA_CONTROL].res);
  441     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
  442         ch->r_io[ATA_IDX_ADDR].res);
  443 
  444     return (0);
  445 }
  446 
  447 int
  448 ata_pci_status(device_t dev)
  449 {
  450     struct ata_pci_controller *controller =
  451         device_get_softc(device_get_parent(dev));
  452     struct ata_channel *ch = device_get_softc(dev);
  453 
  454     if ((dumping || !controller->legacy) &&
  455         ((ch->flags & ATA_ALWAYS_DMASTAT) ||
  456          (ch->dma.flags & ATA_DMA_ACTIVE))) {
  457         int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
  458 
  459         if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
  460             return 0;
  461         ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
  462         DELAY(1);
  463     }
  464     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
  465         DELAY(100);
  466         if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
  467             return 0;
  468     }
  469     return 1;
  470 }
  471 
  472 void
  473 ata_pci_hw(device_t dev)
  474 {
  475     struct ata_channel *ch = device_get_softc(dev);
  476 
  477     ata_generic_hw(dev);
  478     ch->hw.status = ata_pci_status;
  479 }
  480 
  481 static int
  482 ata_pci_dmastart(struct ata_request *request)
  483 {
  484     struct ata_channel *ch = device_get_softc(request->parent);
  485 
  486     ATA_DEBUG_RQ(request, "dmastart");
  487 
  488     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) | 
  489                  (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
  490     ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
  491     ch->dma.flags |= ATA_DMA_ACTIVE;
  492     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
  493                  (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
  494                  ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
  495                  ATA_BMCMD_START_STOP);
  496     return 0;
  497 }
  498 
  499 static int
  500 ata_pci_dmastop(struct ata_request *request)
  501 {
  502     struct ata_channel *ch = device_get_softc(request->parent);
  503     int error;
  504 
  505     ATA_DEBUG_RQ(request, "dmastop");
  506 
  507     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 
  508                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
  509     ch->dma.flags &= ~ATA_DMA_ACTIVE;
  510     error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
  511     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
  512     return error;
  513 }
  514 
  515 static void
  516 ata_pci_dmareset(device_t dev)
  517 {
  518     struct ata_channel *ch = device_get_softc(dev);
  519     struct ata_request *request;
  520 
  521     ATA_IDX_OUTB(ch, ATA_BMCMD_PORT, 
  522                  ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
  523     ch->dma.flags &= ~ATA_DMA_ACTIVE;
  524     ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
  525     if ((request = ch->running)) {
  526         device_printf(dev, "DMA reset calling unload\n");
  527         ch->dma.unload(request);
  528     }
  529 }
  530 
  531 void
  532 ata_pci_dmainit(device_t dev)
  533 {
  534     struct ata_channel *ch = device_get_softc(dev);
  535 
  536     ata_dmainit(dev);
  537     ch->dma.start = ata_pci_dmastart;
  538     ch->dma.stop = ata_pci_dmastop;
  539     ch->dma.reset = ata_pci_dmareset;
  540 }
  541 
  542 void
  543 ata_pci_dmafini(device_t dev)
  544 {
  545 
  546     ata_dmafini(dev);
  547 }
  548 
  549 int
  550 ata_pci_print_child(device_t dev, device_t child)
  551 {
  552         int retval;
  553 
  554         retval = bus_print_child_header(dev, child);
  555         retval += printf(" at channel %d",
  556             (int)(intptr_t)device_get_ivars(child));
  557         retval += bus_print_child_footer(dev, child);
  558 
  559         return (retval);
  560 }
  561 
  562 int
  563 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
  564     size_t buflen)
  565 {
  566 
  567         snprintf(buf, buflen, "channel=%d",
  568             (int)(intptr_t)device_get_ivars(child));
  569         return (0);
  570 }
  571 
  572 static bus_dma_tag_t
  573 ata_pci_get_dma_tag(device_t bus, device_t child)
  574 {
  575 
  576         return (bus_get_dma_tag(bus));
  577 }
  578 
  579 static device_method_t ata_pci_methods[] = {
  580     /* device interface */
  581     DEVMETHOD(device_probe,             ata_pci_probe),
  582     DEVMETHOD(device_attach,            ata_pci_attach),
  583     DEVMETHOD(device_detach,            ata_pci_detach),
  584     DEVMETHOD(device_suspend,           ata_pci_suspend),
  585     DEVMETHOD(device_resume,            ata_pci_resume),
  586     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
  587 
  588     /* bus methods */
  589     DEVMETHOD(bus_read_ivar,            ata_pci_read_ivar),
  590     DEVMETHOD(bus_write_ivar,           ata_pci_write_ivar),
  591     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
  592     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
  593     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
  594     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
  595     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
  596     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
  597     DEVMETHOD(pci_read_config,          ata_pci_read_config),
  598     DEVMETHOD(pci_write_config,         ata_pci_write_config),
  599     DEVMETHOD(bus_print_child,          ata_pci_print_child),
  600     DEVMETHOD(bus_child_location_str,   ata_pci_child_location_str),
  601     DEVMETHOD(bus_get_dma_tag,          ata_pci_get_dma_tag),
  602 
  603     DEVMETHOD_END
  604 };
  605 
  606 devclass_t ata_pci_devclass;
  607 
  608 static driver_t ata_pci_driver = {
  609     "atapci",
  610     ata_pci_methods,
  611     sizeof(struct ata_pci_controller),
  612 };
  613 
  614 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL);
  615 MODULE_VERSION(atapci, 1);
  616 MODULE_DEPEND(atapci, ata, 1, 1, 1);
  617 
  618 static int
  619 ata_pcichannel_probe(device_t dev)
  620 {
  621 
  622     if ((intptr_t)device_get_ivars(dev) < 0)
  623             return (ENXIO);
  624     device_set_desc(dev, "ATA channel");
  625 
  626     return ata_probe(dev);
  627 }
  628 
  629 static int
  630 ata_pcichannel_attach(device_t dev)
  631 {
  632     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  633     struct ata_channel *ch = device_get_softc(dev);
  634     int error;
  635 
  636     if (ch->attached)
  637         return (0);
  638     ch->attached = 1;
  639 
  640     ch->dev = dev;
  641     ch->unit = (intptr_t)device_get_ivars(dev);
  642 
  643     resource_int_value(device_get_name(dev),
  644         device_get_unit(dev), "pm_level", &ch->pm_level);
  645 
  646     if ((error = ctlr->ch_attach(dev)))
  647         return error;
  648 
  649     return ata_attach(dev);
  650 }
  651 
  652 static int
  653 ata_pcichannel_detach(device_t dev)
  654 {
  655     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  656     struct ata_channel *ch = device_get_softc(dev);
  657     int error;
  658 
  659     if (!ch->attached)
  660         return (0);
  661     ch->attached = 0;
  662 
  663     if ((error = ata_detach(dev)))
  664         return error;
  665 
  666     if (ctlr->ch_detach)
  667         return (ctlr->ch_detach(dev));
  668 
  669     return (0);
  670 }
  671 static int
  672 ata_pcichannel_suspend(device_t dev)
  673 {
  674     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  675     struct ata_channel *ch = device_get_softc(dev);
  676     int error;
  677 
  678     if (!ch->attached)
  679         return (0);
  680 
  681     if ((error = ata_suspend(dev)))
  682         return (error);
  683 
  684     if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
  685         return (error);
  686 
  687     return (0);
  688 }
  689 
  690 static int
  691 ata_pcichannel_resume(device_t dev)
  692 {
  693     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  694     struct ata_channel *ch = device_get_softc(dev);
  695     int error;
  696 
  697     if (!ch->attached)
  698         return (0);
  699 
  700     if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
  701         return (error);
  702 
  703     return ata_resume(dev);
  704 }
  705 
  706 
  707 #ifndef ATA_CAM
  708 static int
  709 ata_pcichannel_locking(device_t dev, int mode)
  710 {
  711     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  712     struct ata_channel *ch = device_get_softc(dev);
  713 
  714     if (ctlr->locking)
  715         return ctlr->locking(dev, mode);
  716     else
  717         return ch->unit;
  718 }
  719 #endif
  720 
  721 static void
  722 ata_pcichannel_reset(device_t dev)
  723 {
  724     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  725     struct ata_channel *ch = device_get_softc(dev);
  726 
  727     /* if DMA engine present reset it  */
  728     if (ch->dma.reset)
  729         ch->dma.reset(dev);
  730 
  731     /* reset the controller HW */
  732     if (ctlr->reset)
  733         ctlr->reset(dev);
  734     else
  735         ata_generic_reset(dev);
  736 }
  737 
  738 static int
  739 ata_pcichannel_setmode(device_t dev, int target, int mode)
  740 {
  741         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  742 
  743         if (ctlr->setmode)
  744                 return (ctlr->setmode(dev, target, mode));
  745         else
  746                 return (ata_generic_setmode(dev, target, mode));
  747 }
  748 
  749 static int
  750 ata_pcichannel_getrev(device_t dev, int target)
  751 {
  752         struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
  753         struct ata_channel *ch = device_get_softc(dev);
  754 
  755         if (ch->flags & ATA_SATA) {
  756                 if (ctlr->getrev)
  757                         return (ctlr->getrev(dev, target));
  758                 else 
  759                         return (0xff);
  760         } else
  761                 return (0);
  762 }
  763 
  764 static device_method_t ata_pcichannel_methods[] = {
  765     /* device interface */
  766     DEVMETHOD(device_probe,     ata_pcichannel_probe),
  767     DEVMETHOD(device_attach,    ata_pcichannel_attach),
  768     DEVMETHOD(device_detach,    ata_pcichannel_detach),
  769     DEVMETHOD(device_shutdown,  bus_generic_shutdown),
  770     DEVMETHOD(device_suspend,   ata_pcichannel_suspend),
  771     DEVMETHOD(device_resume,    ata_pcichannel_resume),
  772 
  773     /* ATA methods */
  774     DEVMETHOD(ata_setmode,      ata_pcichannel_setmode),
  775     DEVMETHOD(ata_getrev,       ata_pcichannel_getrev),
  776 #ifndef ATA_CAM
  777     DEVMETHOD(ata_locking,      ata_pcichannel_locking),
  778 #endif
  779     DEVMETHOD(ata_reset,        ata_pcichannel_reset),
  780 
  781     DEVMETHOD_END
  782 };
  783 
  784 driver_t ata_pcichannel_driver = {
  785     "ata",
  786     ata_pcichannel_methods,
  787     sizeof(struct ata_channel),
  788 };
  789 
  790 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL);
  791 
  792 /*
  793  * misc support fucntions
  794  */
  795 int
  796 ata_legacy(device_t dev)
  797 {
  798     return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
  799              (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
  800              ((pci_read_config(dev, PCIR_PROGIF, 1) &
  801                (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
  802               (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
  803             (!pci_read_config(dev, PCIR_BAR(0), 4) &&
  804              !pci_read_config(dev, PCIR_BAR(1), 4) &&
  805              !pci_read_config(dev, PCIR_BAR(2), 4) &&
  806              !pci_read_config(dev, PCIR_BAR(3), 4) &&
  807              !pci_read_config(dev, PCIR_BAR(5), 4)));
  808 }
  809 
  810 void
  811 ata_generic_intr(void *data)
  812 {
  813     struct ata_pci_controller *ctlr = data;
  814     struct ata_channel *ch;
  815     int unit;
  816 
  817     for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
  818         if ((ch = ctlr->interrupt[unit].argument))
  819             ctlr->interrupt[unit].function(ch);
  820     }
  821 }
  822 
  823 int
  824 ata_setup_interrupt(device_t dev, void *intr_func)
  825 {
  826     struct ata_pci_controller *ctlr = device_get_softc(dev);
  827     int i, msi = 0;
  828 
  829     if (!ctlr->legacy) {
  830         if (resource_int_value(device_get_name(dev),
  831                 device_get_unit(dev), "msi", &i) == 0 && i != 0)
  832             msi = 1;
  833         if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
  834             ctlr->r_irq_rid = 0x1;
  835         } else {
  836             msi = 0;
  837             ctlr->r_irq_rid = ATA_IRQ_RID;
  838         }
  839         if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
  840                 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
  841             device_printf(dev, "unable to map interrupt\n");
  842             if (msi)
  843                     pci_release_msi(dev);
  844             return ENXIO;
  845         }
  846         if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
  847                             intr_func, ctlr, &ctlr->handle))) {
  848             device_printf(dev, "unable to setup interrupt\n");
  849             bus_release_resource(dev,
  850                 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
  851             if (msi)
  852                     pci_release_msi(dev);
  853             return ENXIO;
  854         }
  855     }
  856     return 0;
  857 }
  858 
  859 void
  860 ata_set_desc(device_t dev)
  861 {
  862     struct ata_pci_controller *ctlr = device_get_softc(dev);
  863     char buffer[128];
  864 
  865     sprintf(buffer, "%s %s %s controller",
  866             ata_pcivendor2str(dev), ctlr->chip->text, 
  867             ata_mode2str(ctlr->chip->max_dma));
  868     device_set_desc_copy(dev, buffer);
  869 }
  870 
  871 const struct ata_chip_id *
  872 ata_match_chip(device_t dev, const struct ata_chip_id *index)
  873 {
  874     uint32_t devid;
  875     uint8_t revid;
  876 
  877     devid = pci_get_devid(dev);
  878     revid = pci_get_revid(dev);
  879     while (index->chipid != 0) {
  880         if (devid == index->chipid && revid >= index->chiprev)
  881             return (index);
  882         index++;
  883     }
  884     return (NULL);
  885 }
  886 
  887 const struct ata_chip_id *
  888 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
  889 {
  890     const struct ata_chip_id *idx;
  891     device_t *children;
  892     int nchildren, i;
  893     uint8_t s;
  894 
  895     if (device_get_children(device_get_parent(dev), &children, &nchildren))
  896         return (NULL);
  897 
  898     for (i = 0; i < nchildren; i++) {
  899         s = pci_get_slot(children[i]);
  900         if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
  901             idx = ata_match_chip(children[i], index);
  902             if (idx != NULL) {
  903                 free(children, M_TEMP);
  904                 return (idx);
  905             }
  906         }
  907     }
  908     free(children, M_TEMP);
  909     return (NULL);
  910 }
  911 
  912 const char *
  913 ata_pcivendor2str(device_t dev)
  914 {
  915     switch (pci_get_vendor(dev)) {
  916     case ATA_ACARD_ID:          return "Acard";
  917     case ATA_ACER_LABS_ID:      return "AcerLabs";
  918     case ATA_AMD_ID:            return "AMD";
  919     case ATA_ADAPTEC_ID:        return "Adaptec";
  920     case ATA_ATI_ID:            return "ATI";
  921     case ATA_CYRIX_ID:          return "Cyrix";
  922     case ATA_CYPRESS_ID:        return "Cypress";
  923     case ATA_HIGHPOINT_ID:      return "HighPoint";
  924     case ATA_INTEL_ID:          return "Intel";
  925     case ATA_ITE_ID:            return "ITE";
  926     case ATA_JMICRON_ID:        return "JMicron";
  927     case ATA_MARVELL_ID:        return "Marvell";
  928     case ATA_MARVELL2_ID:       return "Marvell";
  929     case ATA_NATIONAL_ID:       return "National";
  930     case ATA_NETCELL_ID:        return "Netcell";
  931     case ATA_NVIDIA_ID:         return "nVidia";
  932     case ATA_PROMISE_ID:        return "Promise";
  933     case ATA_SERVERWORKS_ID:    return "ServerWorks";
  934     case ATA_SILICON_IMAGE_ID:  return "SiI";
  935     case ATA_SIS_ID:            return "SiS";
  936     case ATA_VIA_ID:            return "VIA";
  937     case ATA_CENATEK_ID:        return "Cenatek";
  938     case ATA_MICRON_ID:         return "Micron";
  939     default:                    return "Generic";
  940     }
  941 }
  942 
  943 int
  944 ata_mode2idx(int mode)
  945 {
  946     if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
  947         return (mode & ATA_MODE_MASK) + 8;
  948     if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
  949         return (mode & ATA_MODE_MASK) + 5;
  950     return (mode & ATA_MODE_MASK) - ATA_PIO0;
  951 }

Cache object: a48f91ff381d24ae8694ef297ec13808


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