2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
37 #include <sys/malloc.h>
39 #include <sys/taskqueue.h>
41 #include <machine/stdarg.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/ata/ata-all.h>
48 #include <dev/ata/ata-pci.h>
51 MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
54 #define IOMASK 0xfffffffc
57 * generic PCI ATA device probe
60 ata_pci_probe(device_t dev)
62 struct ata_pci_controller *ctlr = device_get_softc(dev);
65 /* is this a storage class device ? */
66 if (pci_get_class(dev) != PCIC_STORAGE)
69 /* is this an IDE/ATA type device ? */
70 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
73 sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
74 device_set_desc_copy(dev, buffer);
75 ctlr->chipinit = ata_generic_chipinit;
77 /* we are a low priority handler */
78 return (BUS_PROBE_GENERIC);
82 ata_pci_attach(device_t dev)
84 struct ata_pci_controller *ctlr = device_get_softc(dev);
89 /* do chipset specific setups only needed once */
90 ctlr->legacy = ata_legacy(dev);
91 if (ctlr->legacy || pci_read_config(dev, PCIR_BAR(2), 4) & IOMASK)
96 ctlr->ch_attach = ata_pci_ch_attach;
97 ctlr->ch_detach = ata_pci_ch_detach;
100 /* if needed try to enable busmastering */
101 pci_enable_busmaster(dev);
102 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
104 /* if busmastering mode "stuck" use it */
105 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
106 ctlr->r_type1 = SYS_RES_IOPORT;
107 ctlr->r_rid1 = ATA_BMADDR_RID;
108 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
112 if (ctlr->chipinit(dev))
115 /* attach all channels on this controller */
116 for (unit = 0; unit < ctlr->channels; unit++) {
117 if ((ctlr->ichannels & (1 << unit)) == 0)
119 child = device_add_child(dev, "ata",
120 ((unit == 0 || unit == 1) && ctlr->legacy) ?
121 unit : devclass_find_free_unit(ata_devclass, 2));
123 device_printf(dev, "failed to add ata child device\n");
125 device_set_ivars(child, (void *)(intptr_t)unit);
127 bus_generic_attach(dev);
132 ata_pci_detach(device_t dev)
134 struct ata_pci_controller *ctlr = device_get_softc(dev);
136 /* detach & delete all children */
137 device_delete_children(dev);
140 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
141 bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
142 if (ctlr->r_irq_rid != ATA_IRQ_RID)
143 pci_release_msi(dev);
145 if (ctlr->chipdeinit != NULL)
146 ctlr->chipdeinit(dev);
149 bus_space_unmap(rman_get_bustag(ctlr->r_res2),
150 rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2));
152 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
156 bus_space_unmap(rman_get_bustag(ctlr->r_res1),
157 rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1));
159 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
166 ata_pci_suspend(device_t dev)
168 struct ata_pci_controller *ctlr = device_get_softc(dev);
171 bus_generic_suspend(dev);
173 error = ctlr->suspend(dev);
178 ata_pci_resume(device_t dev)
180 struct ata_pci_controller *ctlr = device_get_softc(dev);
184 error = ctlr->resume(dev);
185 bus_generic_resume(dev);
190 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
193 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
197 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
200 return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
204 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
207 return (pci_read_config(dev, reg, width));
211 ata_pci_write_config(device_t dev, device_t child, int reg,
212 uint32_t val, int width)
215 pci_write_config(dev, reg, val, width);
219 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
220 u_long start, u_long end, u_long count, u_int flags)
222 struct ata_pci_controller *controller = device_get_softc(dev);
223 struct resource *res = NULL;
225 if (device_get_devclass(child) == ata_devclass) {
226 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
229 if (type == SYS_RES_IOPORT) {
232 if (controller->legacy) {
233 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
235 end = start + count - 1;
237 myrid = PCIR_BAR(0) + (unit << 3);
238 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
239 SYS_RES_IOPORT, &myrid,
240 start, end, count, flags);
242 case ATA_CTLADDR_RID:
243 if (controller->legacy) {
244 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
246 count = ATA_CTLIOSIZE;
247 end = start + count - 1;
249 myrid = PCIR_BAR(1) + (unit << 3);
250 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
251 SYS_RES_IOPORT, &myrid,
252 start, end, count, flags);
256 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
257 if (controller->legacy) {
258 int irq = (unit == 0 ? 14 : 15);
260 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
261 SYS_RES_IRQ, rid, irq, irq, 1, flags);
263 res = controller->r_irq;
266 if (type == SYS_RES_IRQ) {
267 if (*rid != ATA_IRQ_RID)
269 res = controller->r_irq;
271 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
272 type, rid, start, end, count, flags);
279 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
283 if (device_get_devclass(child) == ata_devclass) {
284 struct ata_pci_controller *controller = device_get_softc(dev);
285 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
287 if (type == SYS_RES_IOPORT) {
290 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
292 PCIR_BAR(0) + (unit << 3), r);
293 case ATA_CTLADDR_RID:
294 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
296 PCIR_BAR(1) + (unit << 3), r);
301 if (type == SYS_RES_IRQ) {
302 if (rid != ATA_IRQ_RID)
304 if (controller->legacy) {
305 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
306 SYS_RES_IRQ, rid, r);
311 if (type == SYS_RES_IRQ) {
312 if (rid != ATA_IRQ_RID)
316 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
324 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
325 int flags, driver_filter_t *filter, driver_intr_t *function,
326 void *argument, void **cookiep)
328 struct ata_pci_controller *controller = device_get_softc(dev);
330 if (controller->legacy) {
331 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
332 flags, filter, function, argument, cookiep);
334 struct ata_pci_controller *controller = device_get_softc(dev);
337 if (filter != NULL) {
338 printf("ata-pci.c: we cannot use a filter here\n");
341 if (device_get_devclass(child) == ata_devclass)
342 unit = ((struct ata_channel *)device_get_softc(child))->unit;
344 unit = ATA_PCI_MAX_CH - 1;
345 controller->interrupt[unit].function = function;
346 controller->interrupt[unit].argument = argument;
347 *cookiep = controller;
353 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
356 struct ata_pci_controller *controller = device_get_softc(dev);
358 if (controller->legacy) {
359 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
361 struct ata_pci_controller *controller = device_get_softc(dev);
364 if (device_get_devclass(child) == ata_devclass)
365 unit = ((struct ata_channel *)device_get_softc(child))->unit;
367 unit = ATA_PCI_MAX_CH - 1;
368 controller->interrupt[unit].function = NULL;
369 controller->interrupt[unit].argument = NULL;
375 ata_generic_setmode(device_t dev, int target, int mode)
378 return (min(mode, ATA_UDMA2));
382 ata_generic_chipinit(device_t dev)
384 struct ata_pci_controller *ctlr = device_get_softc(dev);
386 if (ata_setup_interrupt(dev, ata_generic_intr))
388 ctlr->setmode = ata_generic_setmode;
393 ata_pci_ch_attach(device_t dev)
395 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
396 struct ata_channel *ch = device_get_softc(dev);
397 struct resource *io = NULL, *ctlio = NULL;
400 rid = ATA_IOADDR_RID;
401 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
404 rid = ATA_CTLADDR_RID;
405 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
406 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
410 ata_pci_dmainit(dev);
412 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
413 ch->r_io[i].res = io;
414 ch->r_io[i].offset = i;
416 ch->r_io[ATA_CONTROL].res = ctlio;
417 ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
418 ch->r_io[ATA_IDX_ADDR].res = io;
419 ata_default_registers(dev);
421 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
422 ch->r_io[i].res = ctlr->r_res1;
423 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
432 ata_pci_ch_detach(device_t dev)
434 struct ata_channel *ch = device_get_softc(dev);
436 ata_pci_dmafini(dev);
438 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
439 ch->r_io[ATA_CONTROL].res);
440 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
441 ch->r_io[ATA_IDX_ADDR].res);
447 ata_pci_status(device_t dev)
449 struct ata_pci_controller *controller =
450 device_get_softc(device_get_parent(dev));
451 struct ata_channel *ch = device_get_softc(dev);
453 if ((dumping || !controller->legacy) &&
454 ((ch->flags & ATA_ALWAYS_DMASTAT) ||
455 (ch->dma.flags & ATA_DMA_ACTIVE))) {
456 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
458 if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
460 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
463 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
465 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
472 ata_pci_hw(device_t dev)
474 struct ata_channel *ch = device_get_softc(dev);
477 ch->hw.status = ata_pci_status;
481 ata_pci_dmastart(struct ata_request *request)
483 struct ata_channel *ch = device_get_softc(request->parent);
485 ATA_DEBUG_RQ(request, "dmastart");
487 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
488 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
489 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
490 ch->dma.flags |= ATA_DMA_ACTIVE;
491 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
492 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
493 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
494 ATA_BMCMD_START_STOP);
499 ata_pci_dmastop(struct ata_request *request)
501 struct ata_channel *ch = device_get_softc(request->parent);
504 ATA_DEBUG_RQ(request, "dmastop");
506 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
507 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
508 ch->dma.flags &= ~ATA_DMA_ACTIVE;
509 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
510 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
515 ata_pci_dmareset(device_t dev)
517 struct ata_channel *ch = device_get_softc(dev);
518 struct ata_request *request;
520 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
521 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
522 ch->dma.flags &= ~ATA_DMA_ACTIVE;
523 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
524 if ((request = ch->running)) {
525 device_printf(dev, "DMA reset calling unload\n");
526 ch->dma.unload(request);
531 ata_pci_dmainit(device_t dev)
533 struct ata_channel *ch = device_get_softc(dev);
536 ch->dma.start = ata_pci_dmastart;
537 ch->dma.stop = ata_pci_dmastop;
538 ch->dma.reset = ata_pci_dmareset;
542 ata_pci_dmafini(device_t dev)
549 ata_pci_print_child(device_t dev, device_t child)
553 retval = bus_print_child_header(dev, child);
554 retval += printf(" at channel %d",
555 (int)(intptr_t)device_get_ivars(child));
556 retval += bus_print_child_footer(dev, child);
562 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
566 snprintf(buf, buflen, "channel=%d",
567 (int)(intptr_t)device_get_ivars(child));
572 ata_pci_get_dma_tag(device_t bus, device_t child)
575 return (bus_get_dma_tag(bus));
578 static device_method_t ata_pci_methods[] = {
579 /* device interface */
580 DEVMETHOD(device_probe, ata_pci_probe),
581 DEVMETHOD(device_attach, ata_pci_attach),
582 DEVMETHOD(device_detach, ata_pci_detach),
583 DEVMETHOD(device_suspend, ata_pci_suspend),
584 DEVMETHOD(device_resume, ata_pci_resume),
585 DEVMETHOD(device_shutdown, bus_generic_shutdown),
588 DEVMETHOD(bus_read_ivar, ata_pci_read_ivar),
589 DEVMETHOD(bus_write_ivar, ata_pci_write_ivar),
590 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
591 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
592 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
593 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
594 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
595 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
596 DEVMETHOD(pci_read_config, ata_pci_read_config),
597 DEVMETHOD(pci_write_config, ata_pci_write_config),
598 DEVMETHOD(bus_print_child, ata_pci_print_child),
599 DEVMETHOD(bus_child_location_str, ata_pci_child_location_str),
600 DEVMETHOD(bus_get_dma_tag, ata_pci_get_dma_tag),
605 devclass_t ata_pci_devclass;
607 static driver_t ata_pci_driver = {
610 sizeof(struct ata_pci_controller),
613 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL);
614 MODULE_VERSION(atapci, 1);
615 MODULE_DEPEND(atapci, ata, 1, 1, 1);
618 ata_pcichannel_probe(device_t dev)
621 if ((intptr_t)device_get_ivars(dev) < 0)
623 device_set_desc(dev, "ATA channel");
625 return ata_probe(dev);
629 ata_pcichannel_attach(device_t dev)
631 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
632 struct ata_channel *ch = device_get_softc(dev);
640 ch->unit = (intptr_t)device_get_ivars(dev);
642 resource_int_value(device_get_name(dev),
643 device_get_unit(dev), "pm_level", &ch->pm_level);
645 if ((error = ctlr->ch_attach(dev)))
648 return ata_attach(dev);
652 ata_pcichannel_detach(device_t dev)
654 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
655 struct ata_channel *ch = device_get_softc(dev);
662 if ((error = ata_detach(dev)))
666 return (ctlr->ch_detach(dev));
671 ata_pcichannel_suspend(device_t dev)
673 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
674 struct ata_channel *ch = device_get_softc(dev);
680 if ((error = ata_suspend(dev)))
683 if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
690 ata_pcichannel_resume(device_t dev)
692 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
693 struct ata_channel *ch = device_get_softc(dev);
699 if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
702 return ata_resume(dev);
706 ata_pcichannel_reset(device_t dev)
708 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
709 struct ata_channel *ch = device_get_softc(dev);
711 /* if DMA engine present reset it */
715 /* reset the controller HW */
719 ata_generic_reset(dev);
723 ata_pcichannel_setmode(device_t dev, int target, int mode)
725 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
728 return (ctlr->setmode(dev, target, mode));
730 return (ata_generic_setmode(dev, target, mode));
734 ata_pcichannel_getrev(device_t dev, int target)
736 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
737 struct ata_channel *ch = device_get_softc(dev);
739 if (ch->flags & ATA_SATA) {
741 return (ctlr->getrev(dev, target));
748 static device_method_t ata_pcichannel_methods[] = {
749 /* device interface */
750 DEVMETHOD(device_probe, ata_pcichannel_probe),
751 DEVMETHOD(device_attach, ata_pcichannel_attach),
752 DEVMETHOD(device_detach, ata_pcichannel_detach),
753 DEVMETHOD(device_shutdown, bus_generic_shutdown),
754 DEVMETHOD(device_suspend, ata_pcichannel_suspend),
755 DEVMETHOD(device_resume, ata_pcichannel_resume),
758 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
759 DEVMETHOD(ata_getrev, ata_pcichannel_getrev),
760 DEVMETHOD(ata_reset, ata_pcichannel_reset),
765 driver_t ata_pcichannel_driver = {
767 ata_pcichannel_methods,
768 sizeof(struct ata_channel),
771 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL);
774 * misc support fucntions
777 ata_legacy(device_t dev)
779 return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
780 (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
781 ((pci_read_config(dev, PCIR_PROGIF, 1) &
782 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
783 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
784 (!pci_read_config(dev, PCIR_BAR(0), 4) &&
785 !pci_read_config(dev, PCIR_BAR(1), 4) &&
786 !pci_read_config(dev, PCIR_BAR(2), 4) &&
787 !pci_read_config(dev, PCIR_BAR(3), 4) &&
788 !pci_read_config(dev, PCIR_BAR(5), 4)));
792 ata_generic_intr(void *data)
794 struct ata_pci_controller *ctlr = data;
795 struct ata_channel *ch;
798 for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
799 if ((ch = ctlr->interrupt[unit].argument))
800 ctlr->interrupt[unit].function(ch);
805 ata_setup_interrupt(device_t dev, void *intr_func)
807 struct ata_pci_controller *ctlr = device_get_softc(dev);
811 if (resource_int_value(device_get_name(dev),
812 device_get_unit(dev), "msi", &i) == 0 && i != 0)
814 if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
815 ctlr->r_irq_rid = 0x1;
818 ctlr->r_irq_rid = ATA_IRQ_RID;
820 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
821 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
822 device_printf(dev, "unable to map interrupt\n");
824 pci_release_msi(dev);
827 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
828 intr_func, ctlr, &ctlr->handle))) {
829 device_printf(dev, "unable to setup interrupt\n");
830 bus_release_resource(dev,
831 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
833 pci_release_msi(dev);
841 ata_set_desc(device_t dev)
843 struct ata_pci_controller *ctlr = device_get_softc(dev);
846 sprintf(buffer, "%s %s %s controller",
847 ata_pcivendor2str(dev), ctlr->chip->text,
848 ata_mode2str(ctlr->chip->max_dma));
849 device_set_desc_copy(dev, buffer);
852 const struct ata_chip_id *
853 ata_match_chip(device_t dev, const struct ata_chip_id *index)
858 devid = pci_get_devid(dev);
859 revid = pci_get_revid(dev);
860 while (index->chipid != 0) {
861 if (devid == index->chipid && revid >= index->chiprev)
868 const struct ata_chip_id *
869 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
871 const struct ata_chip_id *idx;
876 if (device_get_children(device_get_parent(dev), &children, &nchildren))
879 for (i = 0; i < nchildren; i++) {
880 s = pci_get_slot(children[i]);
881 if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
882 idx = ata_match_chip(children[i], index);
884 free(children, M_TEMP);
889 free(children, M_TEMP);
894 ata_pcivendor2str(device_t dev)
896 switch (pci_get_vendor(dev)) {
897 case ATA_ACARD_ID: return "Acard";
898 case ATA_ACER_LABS_ID: return "AcerLabs";
899 case ATA_AMD_ID: return "AMD";
900 case ATA_ADAPTEC_ID: return "Adaptec";
901 case ATA_ATI_ID: return "ATI";
902 case ATA_CYRIX_ID: return "Cyrix";
903 case ATA_CYPRESS_ID: return "Cypress";
904 case ATA_HIGHPOINT_ID: return "HighPoint";
905 case ATA_INTEL_ID: return "Intel";
906 case ATA_ITE_ID: return "ITE";
907 case ATA_JMICRON_ID: return "JMicron";
908 case ATA_MARVELL_ID: return "Marvell";
909 case ATA_MARVELL2_ID: return "Marvell";
910 case ATA_NATIONAL_ID: return "National";
911 case ATA_NETCELL_ID: return "Netcell";
912 case ATA_NVIDIA_ID: return "nVidia";
913 case ATA_PROMISE_ID: return "Promise";
914 case ATA_SERVERWORKS_ID: return "ServerWorks";
915 case ATA_SILICON_IMAGE_ID: return "SiI";
916 case ATA_SIS_ID: return "SiS";
917 case ATA_VIA_ID: return "VIA";
918 case ATA_CENATEK_ID: return "Cenatek";
919 case ATA_MICRON_ID: return "Micron";
920 default: return "Generic";
925 ata_mode2idx(int mode)
927 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
928 return (mode & ATA_MODE_MASK) + 8;
929 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
930 return (mode & ATA_MODE_MASK) + 5;
931 return (mode & ATA_MODE_MASK) - ATA_PIO0;