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 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
102 if (!(cmd & PCIM_CMD_BUSMASTEREN)) {
103 pci_write_config(dev, PCIR_COMMAND, cmd | PCIM_CMD_BUSMASTEREN, 2);
104 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
107 /* if busmastering mode "stuck" use it */
108 if ((cmd & PCIM_CMD_BUSMASTEREN) == PCIM_CMD_BUSMASTEREN) {
109 ctlr->r_type1 = SYS_RES_IOPORT;
110 ctlr->r_rid1 = ATA_BMADDR_RID;
111 ctlr->r_res1 = bus_alloc_resource_any(dev, ctlr->r_type1, &ctlr->r_rid1,
115 if (ctlr->chipinit(dev))
118 /* attach all channels on this controller */
119 for (unit = 0; unit < ctlr->channels; unit++) {
120 if ((ctlr->ichannels & (1 << unit)) == 0)
122 child = device_add_child(dev, "ata",
123 ((unit == 0 || unit == 1) && ctlr->legacy) ?
124 unit : devclass_find_free_unit(ata_devclass, 2));
126 device_printf(dev, "failed to add ata child device\n");
128 device_set_ivars(child, (void *)(intptr_t)unit);
130 bus_generic_attach(dev);
135 ata_pci_detach(device_t dev)
137 struct ata_pci_controller *ctlr = device_get_softc(dev);
139 /* detach & delete all children */
140 device_delete_children(dev);
143 bus_teardown_intr(dev, ctlr->r_irq, ctlr->handle);
144 bus_release_resource(dev, SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
145 if (ctlr->r_irq_rid != ATA_IRQ_RID)
146 pci_release_msi(dev);
148 if (ctlr->chipdeinit != NULL)
149 ctlr->chipdeinit(dev);
152 bus_space_unmap(rman_get_bustag(ctlr->r_res2),
153 rman_get_bushandle(ctlr->r_res2), rman_get_size(ctlr->r_res2));
155 bus_release_resource(dev, ctlr->r_type2, ctlr->r_rid2, ctlr->r_res2);
159 bus_space_unmap(rman_get_bustag(ctlr->r_res1),
160 rman_get_bushandle(ctlr->r_res1), rman_get_size(ctlr->r_res1));
162 bus_release_resource(dev, ctlr->r_type1, ctlr->r_rid1, ctlr->r_res1);
169 ata_pci_suspend(device_t dev)
171 struct ata_pci_controller *ctlr = device_get_softc(dev);
174 bus_generic_suspend(dev);
176 error = ctlr->suspend(dev);
181 ata_pci_resume(device_t dev)
183 struct ata_pci_controller *ctlr = device_get_softc(dev);
187 error = ctlr->resume(dev);
188 bus_generic_resume(dev);
193 ata_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
196 return (BUS_READ_IVAR(device_get_parent(dev), dev, which, result));
200 ata_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
203 return (BUS_WRITE_IVAR(device_get_parent(dev), dev, which, value));
207 ata_pci_read_config(device_t dev, device_t child, int reg, int width)
210 return (pci_read_config(dev, reg, width));
214 ata_pci_write_config(device_t dev, device_t child, int reg,
215 uint32_t val, int width)
218 pci_write_config(dev, reg, val, width);
222 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
223 u_long start, u_long end, u_long count, u_int flags)
225 struct ata_pci_controller *controller = device_get_softc(dev);
226 struct resource *res = NULL;
228 if (device_get_devclass(child) == ata_devclass) {
229 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
232 if (type == SYS_RES_IOPORT) {
235 if (controller->legacy) {
236 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
238 end = start + count - 1;
240 myrid = PCIR_BAR(0) + (unit << 3);
241 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
242 SYS_RES_IOPORT, &myrid,
243 start, end, count, flags);
245 case ATA_CTLADDR_RID:
246 if (controller->legacy) {
247 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) +
249 count = ATA_CTLIOSIZE;
250 end = start + count - 1;
252 myrid = PCIR_BAR(1) + (unit << 3);
253 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
254 SYS_RES_IOPORT, &myrid,
255 start, end, count, flags);
259 if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
260 if (controller->legacy) {
261 int irq = (unit == 0 ? 14 : 15);
263 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
264 SYS_RES_IRQ, rid, irq, irq, 1, flags);
266 res = controller->r_irq;
269 if (type == SYS_RES_IRQ) {
270 if (*rid != ATA_IRQ_RID)
272 res = controller->r_irq;
274 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
275 type, rid, start, end, count, flags);
282 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
286 if (device_get_devclass(child) == ata_devclass) {
287 struct ata_pci_controller *controller = device_get_softc(dev);
288 int unit = ((struct ata_channel *)device_get_softc(child))->unit;
290 if (type == SYS_RES_IOPORT) {
293 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
295 PCIR_BAR(0) + (unit << 3), r);
296 case ATA_CTLADDR_RID:
297 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
299 PCIR_BAR(1) + (unit << 3), r);
304 if (type == SYS_RES_IRQ) {
305 if (rid != ATA_IRQ_RID)
307 if (controller->legacy) {
308 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
309 SYS_RES_IRQ, rid, r);
314 if (type == SYS_RES_IRQ) {
315 if (rid != ATA_IRQ_RID)
319 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
327 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
328 int flags, driver_filter_t *filter, driver_intr_t *function,
329 void *argument, void **cookiep)
331 struct ata_pci_controller *controller = device_get_softc(dev);
333 if (controller->legacy) {
334 return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
335 flags, filter, function, argument, cookiep);
337 struct ata_pci_controller *controller = device_get_softc(dev);
340 if (filter != NULL) {
341 printf("ata-pci.c: we cannot use a filter here\n");
344 if (device_get_devclass(child) == ata_devclass)
345 unit = ((struct ata_channel *)device_get_softc(child))->unit;
347 unit = ATA_PCI_MAX_CH - 1;
348 controller->interrupt[unit].function = function;
349 controller->interrupt[unit].argument = argument;
350 *cookiep = controller;
356 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
359 struct ata_pci_controller *controller = device_get_softc(dev);
361 if (controller->legacy) {
362 return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
364 struct ata_pci_controller *controller = device_get_softc(dev);
367 if (device_get_devclass(child) == ata_devclass)
368 unit = ((struct ata_channel *)device_get_softc(child))->unit;
370 unit = ATA_PCI_MAX_CH - 1;
371 controller->interrupt[unit].function = NULL;
372 controller->interrupt[unit].argument = NULL;
378 ata_generic_setmode(device_t dev, int target, int mode)
381 return (min(mode, ATA_UDMA2));
385 ata_generic_chipinit(device_t dev)
387 struct ata_pci_controller *ctlr = device_get_softc(dev);
389 if (ata_setup_interrupt(dev, ata_generic_intr))
391 ctlr->setmode = ata_generic_setmode;
396 ata_pci_ch_attach(device_t dev)
398 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
399 struct ata_channel *ch = device_get_softc(dev);
400 struct resource *io = NULL, *ctlio = NULL;
403 rid = ATA_IOADDR_RID;
404 if (!(io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE)))
407 rid = ATA_CTLADDR_RID;
408 if (!(ctlio = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,RF_ACTIVE))){
409 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
413 ata_pci_dmainit(dev);
415 for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
416 ch->r_io[i].res = io;
417 ch->r_io[i].offset = i;
419 ch->r_io[ATA_CONTROL].res = ctlio;
420 ch->r_io[ATA_CONTROL].offset = ctlr->legacy ? 0 : 2;
421 ch->r_io[ATA_IDX_ADDR].res = io;
422 ata_default_registers(dev);
424 for (i = ATA_BMCMD_PORT; i <= ATA_BMDTP_PORT; i++) {
425 ch->r_io[i].res = ctlr->r_res1;
426 ch->r_io[i].offset = (i - ATA_BMCMD_PORT) + (ch->unit*ATA_BMIOSIZE);
435 ata_pci_ch_detach(device_t dev)
437 struct ata_channel *ch = device_get_softc(dev);
439 ata_pci_dmafini(dev);
441 bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
442 ch->r_io[ATA_CONTROL].res);
443 bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
444 ch->r_io[ATA_IDX_ADDR].res);
450 ata_pci_status(device_t dev)
452 struct ata_pci_controller *controller =
453 device_get_softc(device_get_parent(dev));
454 struct ata_channel *ch = device_get_softc(dev);
456 if ((dumping || !controller->legacy) &&
457 ((ch->flags & ATA_ALWAYS_DMASTAT) ||
458 (ch->dma.flags & ATA_DMA_ACTIVE))) {
459 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
461 if ((bmstat & ATA_BMSTAT_INTERRUPT) == 0)
463 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
466 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
468 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
475 ata_pci_hw(device_t dev)
477 struct ata_channel *ch = device_get_softc(dev);
480 ch->hw.status = ata_pci_status;
484 ata_pci_dmastart(struct ata_request *request)
486 struct ata_channel *ch = device_get_softc(request->parent);
488 ATA_DEBUG_RQ(request, "dmastart");
490 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, (ATA_IDX_INB(ch, ATA_BMSTAT_PORT) |
491 (ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR)));
492 ATA_IDX_OUTL(ch, ATA_BMDTP_PORT, request->dma->sg_bus);
493 ch->dma.flags |= ATA_DMA_ACTIVE;
494 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
495 (ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_WRITE_READ) |
496 ((request->flags & ATA_R_READ) ? ATA_BMCMD_WRITE_READ : 0)|
497 ATA_BMCMD_START_STOP);
502 ata_pci_dmastop(struct ata_request *request)
504 struct ata_channel *ch = device_get_softc(request->parent);
507 ATA_DEBUG_RQ(request, "dmastop");
509 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
510 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
511 ch->dma.flags &= ~ATA_DMA_ACTIVE;
512 error = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
513 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
518 ata_pci_dmareset(device_t dev)
520 struct ata_channel *ch = device_get_softc(dev);
521 struct ata_request *request;
523 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
524 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
525 ch->dma.flags &= ~ATA_DMA_ACTIVE;
526 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, ATA_BMSTAT_INTERRUPT | ATA_BMSTAT_ERROR);
527 if ((request = ch->running)) {
528 device_printf(dev, "DMA reset calling unload\n");
529 ch->dma.unload(request);
534 ata_pci_dmainit(device_t dev)
536 struct ata_channel *ch = device_get_softc(dev);
539 ch->dma.start = ata_pci_dmastart;
540 ch->dma.stop = ata_pci_dmastop;
541 ch->dma.reset = ata_pci_dmareset;
545 ata_pci_dmafini(device_t dev)
552 ata_pci_print_child(device_t dev, device_t child)
556 retval = bus_print_child_header(dev, child);
557 retval += printf(" at channel %d",
558 (int)(intptr_t)device_get_ivars(child));
559 retval += bus_print_child_footer(dev, child);
565 ata_pci_child_location_str(device_t dev, device_t child, char *buf,
569 snprintf(buf, buflen, "channel=%d",
570 (int)(intptr_t)device_get_ivars(child));
574 static device_method_t ata_pci_methods[] = {
575 /* device interface */
576 DEVMETHOD(device_probe, ata_pci_probe),
577 DEVMETHOD(device_attach, ata_pci_attach),
578 DEVMETHOD(device_detach, ata_pci_detach),
579 DEVMETHOD(device_suspend, ata_pci_suspend),
580 DEVMETHOD(device_resume, ata_pci_resume),
581 DEVMETHOD(device_shutdown, bus_generic_shutdown),
584 DEVMETHOD(bus_read_ivar, ata_pci_read_ivar),
585 DEVMETHOD(bus_write_ivar, ata_pci_write_ivar),
586 DEVMETHOD(bus_alloc_resource, ata_pci_alloc_resource),
587 DEVMETHOD(bus_release_resource, ata_pci_release_resource),
588 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
589 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
590 DEVMETHOD(bus_setup_intr, ata_pci_setup_intr),
591 DEVMETHOD(bus_teardown_intr, ata_pci_teardown_intr),
592 DEVMETHOD(pci_read_config, ata_pci_read_config),
593 DEVMETHOD(pci_write_config, ata_pci_write_config),
594 DEVMETHOD(bus_print_child, ata_pci_print_child),
595 DEVMETHOD(bus_child_location_str, ata_pci_child_location_str),
600 devclass_t ata_pci_devclass;
602 static driver_t ata_pci_driver = {
605 sizeof(struct ata_pci_controller),
608 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, NULL, NULL);
609 MODULE_VERSION(atapci, 1);
610 MODULE_DEPEND(atapci, ata, 1, 1, 1);
613 ata_pcichannel_probe(device_t dev)
616 if ((intptr_t)device_get_ivars(dev) < 0)
618 device_set_desc(dev, "ATA channel");
620 return ata_probe(dev);
624 ata_pcichannel_attach(device_t dev)
626 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
627 struct ata_channel *ch = device_get_softc(dev);
635 ch->unit = (intptr_t)device_get_ivars(dev);
637 resource_int_value(device_get_name(dev),
638 device_get_unit(dev), "pm_level", &ch->pm_level);
640 if ((error = ctlr->ch_attach(dev)))
643 return ata_attach(dev);
647 ata_pcichannel_detach(device_t dev)
649 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
650 struct ata_channel *ch = device_get_softc(dev);
657 if ((error = ata_detach(dev)))
661 return (ctlr->ch_detach(dev));
666 ata_pcichannel_suspend(device_t dev)
668 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
669 struct ata_channel *ch = device_get_softc(dev);
675 if ((error = ata_suspend(dev)))
678 if (ctlr->ch_suspend != NULL && (error = ctlr->ch_suspend(dev)))
685 ata_pcichannel_resume(device_t dev)
687 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
688 struct ata_channel *ch = device_get_softc(dev);
694 if (ctlr->ch_resume != NULL && (error = ctlr->ch_resume(dev)))
697 return ata_resume(dev);
701 ata_pcichannel_reset(device_t dev)
703 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
704 struct ata_channel *ch = device_get_softc(dev);
706 /* if DMA engine present reset it */
710 /* reset the controller HW */
714 ata_generic_reset(dev);
718 ata_pcichannel_setmode(device_t dev, int target, int mode)
720 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
723 return (ctlr->setmode(dev, target, mode));
725 return (ata_generic_setmode(dev, target, mode));
729 ata_pcichannel_getrev(device_t dev, int target)
731 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
732 struct ata_channel *ch = device_get_softc(dev);
734 if (ch->flags & ATA_SATA) {
736 return (ctlr->getrev(dev, target));
743 static device_method_t ata_pcichannel_methods[] = {
744 /* device interface */
745 DEVMETHOD(device_probe, ata_pcichannel_probe),
746 DEVMETHOD(device_attach, ata_pcichannel_attach),
747 DEVMETHOD(device_detach, ata_pcichannel_detach),
748 DEVMETHOD(device_shutdown, bus_generic_shutdown),
749 DEVMETHOD(device_suspend, ata_pcichannel_suspend),
750 DEVMETHOD(device_resume, ata_pcichannel_resume),
753 DEVMETHOD(ata_setmode, ata_pcichannel_setmode),
754 DEVMETHOD(ata_getrev, ata_pcichannel_getrev),
755 DEVMETHOD(ata_reset, ata_pcichannel_reset),
760 driver_t ata_pcichannel_driver = {
762 ata_pcichannel_methods,
763 sizeof(struct ata_channel),
766 DRIVER_MODULE(ata, atapci, ata_pcichannel_driver, ata_devclass, NULL, NULL);
769 * misc support fucntions
772 ata_legacy(device_t dev)
774 return (((pci_read_config(dev, PCIR_SUBCLASS, 1) == PCIS_STORAGE_IDE) &&
775 (pci_read_config(dev, PCIR_PROGIF, 1)&PCIP_STORAGE_IDE_MASTERDEV)&&
776 ((pci_read_config(dev, PCIR_PROGIF, 1) &
777 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC)) !=
778 (PCIP_STORAGE_IDE_MODEPRIM | PCIP_STORAGE_IDE_MODESEC))) ||
779 (!pci_read_config(dev, PCIR_BAR(0), 4) &&
780 !pci_read_config(dev, PCIR_BAR(1), 4) &&
781 !pci_read_config(dev, PCIR_BAR(2), 4) &&
782 !pci_read_config(dev, PCIR_BAR(3), 4) &&
783 !pci_read_config(dev, PCIR_BAR(5), 4)));
787 ata_generic_intr(void *data)
789 struct ata_pci_controller *ctlr = data;
790 struct ata_channel *ch;
793 for (unit = 0; unit < ATA_PCI_MAX_CH; unit++) {
794 if ((ch = ctlr->interrupt[unit].argument))
795 ctlr->interrupt[unit].function(ch);
800 ata_setup_interrupt(device_t dev, void *intr_func)
802 struct ata_pci_controller *ctlr = device_get_softc(dev);
806 if (resource_int_value(device_get_name(dev),
807 device_get_unit(dev), "msi", &i) == 0 && i != 0)
809 if (msi && pci_msi_count(dev) > 0 && pci_alloc_msi(dev, &msi) == 0) {
810 ctlr->r_irq_rid = 0x1;
813 ctlr->r_irq_rid = ATA_IRQ_RID;
815 if (!(ctlr->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
816 &ctlr->r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
817 device_printf(dev, "unable to map interrupt\n");
819 pci_release_msi(dev);
822 if ((bus_setup_intr(dev, ctlr->r_irq, ATA_INTR_FLAGS, NULL,
823 intr_func, ctlr, &ctlr->handle))) {
824 device_printf(dev, "unable to setup interrupt\n");
825 bus_release_resource(dev,
826 SYS_RES_IRQ, ctlr->r_irq_rid, ctlr->r_irq);
828 pci_release_msi(dev);
836 ata_set_desc(device_t dev)
838 struct ata_pci_controller *ctlr = device_get_softc(dev);
841 sprintf(buffer, "%s %s %s controller",
842 ata_pcivendor2str(dev), ctlr->chip->text,
843 ata_mode2str(ctlr->chip->max_dma));
844 device_set_desc_copy(dev, buffer);
847 const struct ata_chip_id *
848 ata_match_chip(device_t dev, const struct ata_chip_id *index)
853 devid = pci_get_devid(dev);
854 revid = pci_get_revid(dev);
855 while (index->chipid != 0) {
856 if (devid == index->chipid && revid >= index->chiprev)
863 const struct ata_chip_id *
864 ata_find_chip(device_t dev, const struct ata_chip_id *index, int slot)
866 const struct ata_chip_id *idx;
871 if (device_get_children(device_get_parent(dev), &children, &nchildren))
874 for (i = 0; i < nchildren; i++) {
875 s = pci_get_slot(children[i]);
876 if ((slot >= 0 && s == slot) || (slot < 0 && s <= -slot)) {
877 idx = ata_match_chip(children[i], index);
879 free(children, M_TEMP);
884 free(children, M_TEMP);
889 ata_pcivendor2str(device_t dev)
891 switch (pci_get_vendor(dev)) {
892 case ATA_ACARD_ID: return "Acard";
893 case ATA_ACER_LABS_ID: return "AcerLabs";
894 case ATA_AMD_ID: return "AMD";
895 case ATA_ADAPTEC_ID: return "Adaptec";
896 case ATA_ATI_ID: return "ATI";
897 case ATA_CYRIX_ID: return "Cyrix";
898 case ATA_CYPRESS_ID: return "Cypress";
899 case ATA_HIGHPOINT_ID: return "HighPoint";
900 case ATA_INTEL_ID: return "Intel";
901 case ATA_ITE_ID: return "ITE";
902 case ATA_JMICRON_ID: return "JMicron";
903 case ATA_MARVELL_ID: return "Marvell";
904 case ATA_MARVELL2_ID: return "Marvell";
905 case ATA_NATIONAL_ID: return "National";
906 case ATA_NETCELL_ID: return "Netcell";
907 case ATA_NVIDIA_ID: return "nVidia";
908 case ATA_PROMISE_ID: return "Promise";
909 case ATA_SERVERWORKS_ID: return "ServerWorks";
910 case ATA_SILICON_IMAGE_ID: return "SiI";
911 case ATA_SIS_ID: return "SiS";
912 case ATA_VIA_ID: return "VIA";
913 case ATA_CENATEK_ID: return "Cenatek";
914 case ATA_MICRON_ID: return "Micron";
915 default: return "Generic";
920 ata_mode2idx(int mode)
922 if ((mode & ATA_DMA_MASK) == ATA_UDMA0)
923 return (mode & ATA_MODE_MASK) + 8;
924 if ((mode & ATA_DMA_MASK) == ATA_WDMA0)
925 return (mode & ATA_MODE_MASK) + 5;
926 return (mode & ATA_MODE_MASK) - ATA_PIO0;