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$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
38 #include <sys/malloc.h>
40 #include <sys/taskqueue.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.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>
52 MALLOC_DEFINE(M_ATAPCI, "ata_pci", "ATA driver PCI");
55 #define IOMASK 0xfffffffc
58 * generic PCI ATA device probe
61 ata_pci_probe(device_t dev)
63 struct ata_pci_controller *ctlr = device_get_softc(dev);
66 /* is this a storage class device ? */
67 if (pci_get_class(dev) != PCIC_STORAGE)
70 /* is this an IDE/ATA type device ? */
71 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
74 sprintf(buffer, "%s ATA controller", ata_pcivendor2str(dev));
75 device_set_desc_copy(dev, buffer);
76 ctlr->chipinit = ata_generic_chipinit;
78 /* we are a low priority handler */
79 return (BUS_PROBE_GENERIC);
83 ata_pci_attach(device_t dev)
85 struct ata_pci_controller *ctlr = device_get_softc(dev);
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)
97 ctlr->ch_attach = ata_pci_ch_attach;
98 ctlr->ch_detach = ata_pci_ch_detach;
101 /* if needed try to enable busmastering */
102 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
103 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
104 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
105 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
108 /* if busmastering mode "stuck" use it */
109 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
110 ctlr->r_type1 = SYS_RES_IOPORT;
111 ctlr->r_rid1 = ATA_BMADDR_RID;
112 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
116 if (ctlr->chipinit(dev))
119 /* attach all channels on this controller */
120 for (unit = 0; unit < ctlr->channels; unit++) {
121 if ((ctlr->ichannels & (1 << unit)) == 0)
123 child = device_add_child(dev, "ata",
124 ((unit == 0 || unit == 1) && ctlr->legacy) ?
125 unit : devclass_find_free_unit(ata_devclass, 2));
127 device_printf(dev, "failed to add ata child device\n");
129 device_set_ivars(child, (void *)(intptr_t)unit);
131 bus_generic_attach(dev);
136 ata_pci_detach(device_t dev)
138 struct ata_pci_controller *ctlr = device_get_softc(dev);
142 /* detach & delete all children */
143 if (!device_get_children(dev, &children, &nchildren)) {
144 for (i = 0; i < nchildren; i++)
145 device_delete_child(dev, children[i]);
146 free(children, M_TEMP);
149 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
150 bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
151 if (ctlr->r_irq_rid != ATA_IRQ_RID)
152 pci_release_msi(dev);
154 if (ctlr->chipdeinit != NULL)
155 ctlr->chipdeinit(dev);
157 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
159 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
165 ata_pci_suspend(device_t dev)
167 struct ata_pci_controller *ctlr = device_get_softc(dev);
170 bus_generic_suspend(dev);
172 error = ctlr->suspend(dev);
177 ata_pci_resume(device_t dev)
179 struct ata_pci_controller *ctlr = device_get_softc(dev);
183 error = ctlr->resume(dev);
184 bus_generic_resume(dev);
189 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
192 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
196 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
199 return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
203 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
206 return (pci_read_config(dev, reg, width));
210 ata_pci_write_config(device_t dev, device_t child, int reg,
211 uint32_t val, int width)
214 pci_write_config(dev, reg, val, width);
218 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
219 u_long start, u_long end, u_long count, u_int flags)
221 struct ata_pci_controller *controller = device_get_softc(dev);
222 struct resource *res = NULL;
224 if (device_get_devclass(child) == ata_devclass) {
225 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
228 if (type == SYS_RES_IOPORT) {
231 if (controller->legacy) {
232 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
234 end = start + count - 1;
236 myrid = PCIR_BAR(0) + (unit << 3);
237 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
238 SYS_RES_IOPORT, &myrid,
239 start, end, count, flags);
241 case ATA_CTLADDR_RID:
242 if (controller->legacy) {
243 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
245 count = ATA_CTLIOSIZE;
246 end = start + count - 1;
248 myrid = PCIR_BAR(1) + (unit << 3);
249 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
250 SYS_RES_IOPORT, &myrid,
251 start, end, count, flags);
255 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
256 if (controller->legacy) {
257 int irq = (unit == 0 ? 14 : 15);
259 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
260 SYS_RES_IRQ, rid, irq, irq, 1, flags);
262 res = controller->r_irq;
265 if (type == SYS_RES_IRQ) {
266 if (*rid != ATA_IRQ_RID)
268 res = controller->r_irq;
270 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
271 type, rid, start, end, count, flags);
278 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
282 if (device_get_devclass(child) == ata_devclass) {
283 struct ata_pci_controller *controller = device_get_softc(dev);
284 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
286 if (type == SYS_RES_IOPORT) {
289 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
291 PCIR_BAR(0) + (unit << 3), r);
292 case ATA_CTLADDR_RID:
293 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
295 PCIR_BAR(1) + (unit << 3), r);
300 if (type == SYS_RES_IRQ) {
301 if (rid != ATA_IRQ_RID)
303 if (controller->legacy) {
304 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
305 SYS_RES_IRQ, rid, r);
310 if (type == SYS_RES_IRQ) {
311 if (rid != ATA_IRQ_RID)
315 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
323 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
324 int flags, driver_filter_t *filter, driver_intr_t *function,
325 void *argument, void **cookiep)
327 struct ata_pci_controller *controller = device_get_softc(dev);
329 if (controller->legacy) {
330 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
331 flags, filter, function, argument, cookiep);
333 struct ata_pci_controller *controller = device_get_softc(dev);
336 if (filter != NULL) {
337 printf("ata-pci.c: we cannot use a filter here\n");
340 if (device_get_devclass(child) == ata_devclass)
341 unit = ((struct ata_channel *)device_get_softc(child))->unit;
343 unit = ATA_PCI_MAX_CH - 1;
344 controller->interrupt[unit].function = function;
345 controller->interrupt[unit].argument = argument;
346 *cookiep = controller;
352 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
355 struct ata_pci_controller *controller = device_get_softc(dev);
357 if (controller->legacy) {
358 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
360 struct ata_pci_controller *controller = device_get_softc(dev);
363 if (device_get_devclass(child) == ata_devclass)
364 unit = ((struct ata_channel *)device_get_softc(child))->unit;
366 unit = ATA_PCI_MAX_CH - 1;
367 controller->interrupt[unit].function = NULL;
368 controller->interrupt[unit].argument = NULL;
374 ata_generic_setmode(device_t dev, int target, int mode)
377 return (min(mode, ATA_UDMA2));
381 ata_generic_chipinit(device_t dev)
383 struct ata_pci_controller *ctlr = device_get_softc(dev);
385 if (ata_setup_interrupt(dev, ata_generic_intr))
387 ctlr->setmode = ata_generic_setmode;
392 ata_pci_ch_attach(device_t dev)
394 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
395 struct ata_channel *ch = device_get_softc(dev);
396 struct resource *io = NULL, *ctlio = NULL;
399 rid = ATA_IOADDR_RID;
400 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
403 rid = ATA_CTLADDR_RID;
404 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
405 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
409 ata_pci_dmainit(dev);
411 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
412 ch->r_io[i].res = io;
413 ch->r_io[i].offset = i;
415 ch->r_io[ATA_CONTROL].res = ctlio;
416 ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
417 ch->r_io[ATA_IDX_ADDR].res = io;
418 ata_default_registers(dev);
420 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
421 ch->r_io[i].res = ctlr->r_res1;
422 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
431 ata_pci_ch_detach(device_t dev)
433 struct ata_channel *ch = device_get_softc(dev);
435 ata_pci_dmafini(dev);
437 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
438 ch->r_io[ATA_CONTROL].res);
439 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
440 ch->r_io[ATA_IDX_ADDR].res);
446 ata_pci_status(device_t dev)
448 struct ata_pci_controller *controller =
449 device_get_softc(device_get_parent(dev));
450 struct ata_channel *ch = device_get_softc(dev);
452 if ((dumping || !controller->legacy) &&
453 ((ch->flags & ATA_ALWAYS_DMASTAT) ||
454 (ch->dma.flags & ATA_DMA_ACTIVE))) {
455 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
457 if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
459 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
462 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
464 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
471 ata_pci_hw(device_t dev)
473 struct ata_channel *ch = device_get_softc(dev);
476 ch->hw.status = ata_pci_status;
480 ata_pci_dmastart(struct ata_request *request)
482 struct ata_channel *ch = device_get_softc(request->parent);
484 ATA_DEBUG_RQ(request, "dmastart");
486 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
487 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
488 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
489 ch->dma.flags |= ATA_DMA_ACTIVE;
490 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
491 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
492 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
493 ATA_BMCMD_START_STOP);
498 ata_pci_dmastop(struct ata_request *request)
500 struct ata_channel *ch = device_get_softc(request->parent);
503 ATA_DEBUG_RQ(request, "dmastop");
505 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
506 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
507 ch->dma.flags &= ~ATA_DMA_ACTIVE;
508 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
509 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
514 ata_pci_dmareset(device_t dev)
516 struct ata_channel *ch = device_get_softc(dev);
517 struct ata_request *request;
519 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
520 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
521 ch->dma.flags &= ~ATA_DMA_ACTIVE;
522 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
523 if ((request = ch->running)) {
524 device_printf(dev, "DMA reset calling unload\n");
525 ch->dma.unload(request);
530 ata_pci_dmainit(device_t dev)
532 struct ata_channel *ch = device_get_softc(dev);
535 ch->dma.start = ata_pci_dmastart;
536 ch->dma.stop = ata_pci_dmastop;
537 ch->dma.reset = ata_pci_dmareset;
541 ata_pci_dmafini(device_t dev)
548 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
552 snprintf(buf, buflen, "channel=%d",
553 (int)(intptr_t)device_get_ivars(child));
557 static device_method_t ata_pci_methods[] = {
558 /* device interface */
559 DEVMETHOD(device_probe, ata_pci_probe),
560 DEVMETHOD(device_attach, ata_pci_attach),
561 DEVMETHOD(device_detach, ata_pci_detach),
562 DEVMETHOD(device_suspend, ata_pci_suspend),
563 DEVMETHOD(device_resume, ata_pci_resume),
564 DEVMETHOD(device_shutdown, bus_generic_shutdown),
567 DEVMETHOD(bus_read_ivar, ata_pci_read_ivar),
568 DEVMETHOD(bus_write_ivar, ata_pci_write_ivar),
569 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
570 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
571 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
572 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
573 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
574 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
575 DEVMETHOD(pci_read_config, ata_pci_read_config),
576 DEVMETHOD(pci_write_config, ata_pci_write_config),
577 DEVMETHOD(bus_child_location_str, ata_pci_child_location_str),
582 devclass_t ata_pci_devclass;
584 static driver_t ata_pci_driver = {
587 sizeof(struct ata_pci_controller),
590 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
591 MODULE_VERSION(atapci, 1);
592 MODULE_DEPEND(atapci, ata, 1, 1, 1);
595 ata_pcichannel_probe(device_t dev)
599 if ((intptr_t)device_get_ivars(dev) < 0)
601 sprintf(buffer, "ATA channel %d", (int)(intptr_t)device_get_ivars(dev));
602 device_set_desc_copy(dev, buffer);
604 return ata_probe(dev);
608 ata_pcichannel_attach(device_t dev)
610 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
611 struct ata_channel *ch = device_get_softc(dev);
619 ch->unit = (intptr_t)device_get_ivars(dev);
621 resource_int_value(device_get_name(dev),
622 device_get_unit(dev), "pm_level", &ch->pm_level);
624 if ((error = ctlr->ch_attach(dev)))
627 return ata_attach(dev);
631 ata_pcichannel_detach(device_t dev)
633 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
634 struct ata_channel *ch = device_get_softc(dev);
641 if ((error = ata_detach(dev)))
645 return (ctlr->ch_detach(dev));
650 ata_pcichannel_suspend(device_t dev)
652 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
653 struct ata_channel *ch = device_get_softc(dev);
659 if ((error = ata_suspend(dev)))
662 if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
669 ata_pcichannel_resume(device_t dev)
671 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
672 struct ata_channel *ch = device_get_softc(dev);
678 if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
681 return ata_resume(dev);
686 ata_pcichannel_locking(device_t dev, int mode)
688 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
689 struct ata_channel *ch = device_get_softc(dev);
692 return ctlr->locking(dev, mode);
698 ata_pcichannel_reset(device_t dev)
700 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
701 struct ata_channel *ch = device_get_softc(dev);
703 /* if DMA engine present reset it */
707 /* reset the controller HW */
711 ata_generic_reset(dev);
715 ata_pcichannel_setmode(device_t dev, int target, int mode)
717 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
720 return (ctlr->setmode(dev, target, mode));
722 return (ata_generic_setmode(dev, target, mode));
726 ata_pcichannel_getrev(device_t dev, int target)
728 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
729 struct ata_channel *ch = device_get_softc(dev);
731 if (ch->flags & ATA_SATA) {
733 return (ctlr->getrev(dev, target));
740 static device_method_t ata_pcichannel_methods[] = {
741 /* device interface */
742 DEVMETHOD(device_probe, ata_pcichannel_probe),
743 DEVMETHOD(device_attach, ata_pcichannel_attach),
744 DEVMETHOD(device_detach, ata_pcichannel_detach),
745 DEVMETHOD(device_shutdown, bus_generic_shutdown),
746 DEVMETHOD(device_suspend, ata_pcichannel_suspend),
747 DEVMETHOD(device_resume, ata_pcichannel_resume),
750 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
751 DEVMETHOD(ata_getrev, ata_pcichannel_getrev),
752 DEVMETHOD(ata_locking, ata_pcichannel_locking),
753 DEVMETHOD(ata_reset, ata_pcichannel_reset),
758 driver_t ata_pcichannel_driver = {
760 ata_pcichannel_methods,
761 sizeof(struct ata_channel),
764 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, 0, 0);
768 * misc support fucntions
771 ata_legacy(device_t dev)
773 return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
774 (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
775 ((pci_read_config(dev, PCIR_PROGIF, 1) &
776 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
777 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
778 (!pci_read_config(dev, PCIR_BAR(0), 4) &&
779 !pci_read_config(dev, PCIR_BAR(1), 4) &&
780 !pci_read_config(dev, PCIR_BAR(2), 4) &&
781 !pci_read_config(dev, PCIR_BAR(3), 4) &&
782 !pci_read_config(dev, PCIR_BAR(5), 4)));
786 ata_generic_intr(void *data)
788 struct ata_pci_controller *ctlr = data;
789 struct ata_channel *ch;
792 for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
793 if ((ch = ctlr->interrupt[unit].argument))
794 ctlr->interrupt[unit].function(ch);
799 ata_setup_interrupt(device_t dev, void *intr_func)
801 struct ata_pci_controller *ctlr = device_get_softc(dev);
805 if (resource_int_value(device_get_name(dev),
806 device_get_unit(dev), "msi", &i) == 0 && i != 0)
808 if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
809 ctlr->r_irq_rid = 0x1;
812 ctlr->r_irq_rid = ATA_IRQ_RID;
814 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
815 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
816 device_printf(dev, "unable to map interrupt\n");
818 pci_release_msi(dev);
821 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
822 intr_func, ctlr, &ctlr->handle))) {
823 device_printf(dev, "unable to setup interrupt\n");
824 bus_release_resource(dev,
825 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
827 pci_release_msi(dev);
835 ata_set_desc(device_t dev)
837 struct ata_pci_controller *ctlr = device_get_softc(dev);
840 sprintf(buffer, "%s %s %s controller",
841 ata_pcivendor2str(dev), ctlr->chip->text,
842 ata_mode2str(ctlr->chip->max_dma));
843 device_set_desc_copy(dev, buffer);
847 ata_match_chip(device_t dev, struct ata_chip_id *index)
852 devid = pci_get_devid(dev);
853 revid = pci_get_revid(dev);
854 while (index->chipid != 0) {
855 if (devid == index->chipid && revid >= index->chiprev)
863 ata_find_chip(device_t dev, struct ata_chip_id *index, int slot)
865 struct ata_chip_id *idx;
870 if (device_get_children(device_get_parent(dev), &children, &nchildren))
873 for (i = 0; i < nchildren; i++) {
874 s = pci_get_slot(children[i]);
875 if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
876 idx = ata_match_chip(children[i], index);
878 free(children, M_TEMP);
883 free(children, M_TEMP);
888 ata_pcivendor2str(device_t dev)
890 switch (pci_get_vendor(dev)) {
891 case ATA_ACARD_ID: return "Acard";
892 case ATA_ACER_LABS_ID: return "AcerLabs";
893 case ATA_AMD_ID: return "AMD";
894 case ATA_ADAPTEC_ID: return "Adaptec";
895 case ATA_ATI_ID: return "ATI";
896 case ATA_CYRIX_ID: return "Cyrix";
897 case ATA_CYPRESS_ID: return "Cypress";
898 case ATA_HIGHPOINT_ID: return "HighPoint";
899 case ATA_INTEL_ID: return "Intel";
900 case ATA_ITE_ID: return "ITE";
901 case ATA_JMICRON_ID: return "JMicron";
902 case ATA_MARVELL_ID: return "Marvell";
903 case ATA_MARVELL2_ID: return "Marvell";
904 case ATA_NATIONAL_ID: return "National";
905 case ATA_NETCELL_ID: return "Netcell";
906 case ATA_NVIDIA_ID: return "nVidia";
907 case ATA_PROMISE_ID: return "Promise";
908 case ATA_SERVERWORKS_ID: return "ServerWorks";
909 case ATA_SILICON_IMAGE_ID: return "SiI";
910 case ATA_SIS_ID: return "SiS";
911 case ATA_VIA_ID: return "VIA";
912 case ATA_CENATEK_ID: return "Cenatek";
913 case ATA_MICRON_ID: return "Micron";
914 default: return "Generic";
919 ata_mode2idx(int mode)
921 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
922 return (mode & ATA_MODE_MASK) + 8;
923 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
924 return (mode & ATA_MODE_MASK) + 5;
925 return (mode & ATA_MODE_MASK) - ATA_PIO0;