xref: /freebsd/sys/dev/ata/ata-pci.c (revision adeb92a24c57f97d5cd3c3c45be239cbb23aed68)
1 /*-
2  * Copyright (c) 1998,1999,2000,2001,2002 S�ren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/disk.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/bio.h>
38 #include <sys/malloc.h>
39 #include <sys/devicestat.h>
40 #include <sys/sysctl.h>
41 #include <machine/stdarg.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
44 #ifdef __alpha__
45 #include <machine/md_var.h>
46 #endif
47 #include <sys/rman.h>
48 #include <pci/pcivar.h>
49 #include <pci/pcireg.h>
50 #include <dev/ata/ata-all.h>
51 
52 /* device structures */
53 struct ata_pci_softc {
54     struct resource *bmio;
55     int bmaddr;
56     struct resource *irq;
57     int irqcnt;
58 };
59 
60 /* misc defines */
61 #define IOMASK			0xfffffffc
62 #define GRANDPARENT(dev)	device_get_parent(device_get_parent(dev))
63 #define ATA_MASTERDEV(dev)	((pci_get_progif(dev) & 0x80) && \
64 				 (pci_get_progif(dev) & 0x05) != 0x05)
65 
66 int
67 ata_find_dev(device_t dev, u_int32_t devid, u_int32_t revid)
68 {
69     device_t *children;
70     int nchildren, i;
71 
72     if (device_get_children(device_get_parent(dev), &children, &nchildren))
73 	return 0;
74 
75     for (i = 0; i < nchildren; i++) {
76 	if (pci_get_devid(children[i]) == devid &&
77 	    pci_get_revid(children[i]) >= revid) {
78 	    free(children, M_TEMP);
79 	    return 1;
80 	}
81     }
82     free(children, M_TEMP);
83     return 0;
84 }
85 
86 static void
87 ata_via_southbridge_fixup(device_t dev)
88 {
89     device_t *children;
90     int nchildren, i;
91 
92     if (device_get_children(device_get_parent(dev), &children, &nchildren))
93 	return;
94 
95     for (i = 0; i < nchildren; i++) {
96 	if (pci_get_devid(children[i]) == 0x03051106 ||		/* VIA VT8363 */
97 	    pci_get_devid(children[i]) == 0x03911106 ||		/* VIA VT8371 */
98 	    pci_get_devid(children[i]) == 0x31021106 ||		/* VIA VT8662 */
99 	    pci_get_devid(children[i]) == 0x31121106) {		/* VIA VT8361 */
100 	    u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
101 
102 	    if ((reg76 & 0xf0) != 0xd0) {
103 		device_printf(dev,
104 		"Correcting VIA config for southbridge data corruption bug\n");
105 		pci_write_config(children[i], 0x75, 0x80, 1);
106 		pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
107 	    }
108 	    break;
109 	}
110     }
111     free(children, M_TEMP);
112 }
113 
114 static const char *
115 ata_pci_match(device_t dev)
116 {
117     if (pci_get_class(dev) != PCIC_STORAGE)
118 	return NULL;
119 
120     switch (pci_get_devid(dev)) {
121     /* supported chipsets */
122     case 0x12308086:
123 	return "Intel PIIX ATA controller";
124 
125     case 0x70108086:
126 	return "Intel PIIX3 ATA controller";
127 
128     case 0x71118086:
129     case 0x71998086:
130     case 0x84ca8086:
131 	return "Intel PIIX4 ATA33 controller";
132 
133     case 0x24218086:
134 	return "Intel ICH0 ATA33 controller";
135 
136     case 0x24118086:
137     case 0x76018086:
138 	return "Intel ICH ATA66 controller";
139 
140     case 0x244a8086:
141     case 0x244b8086:
142 	return "Intel ICH2 ATA100 controller";
143 
144     case 0x248a8086:
145     case 0x248b8086:
146 	return "Intel ICH3 ATA100 controller";
147 
148     case 0x522910b9:
149 	if (pci_get_revid(dev) >= 0xc4)
150 	    return "AcerLabs Aladdin ATA100 controller";
151 	else if (pci_get_revid(dev) >= 0xc2)
152 	    return "AcerLabs Aladdin ATA66 controller";
153 	else if (pci_get_revid(dev) >= 0x20)
154 	    return "AcerLabs Aladdin ATA33 controller";
155 	else
156 	    return "AcerLabs Aladdin ATA controller";
157 
158     case 0x05711106:
159 	if (ata_find_dev(dev, 0x05861106, 0x02))
160 	    return "VIA 82C586 ATA33 controller";
161 	if (ata_find_dev(dev, 0x05861106, 0))
162 	    return "VIA 82C586 ATA controller";
163 	if (ata_find_dev(dev, 0x05961106, 0x12))
164 	    return "VIA 82C596 ATA66 controller";
165 	if (ata_find_dev(dev, 0x05961106, 0))
166 	    return "VIA 82C596 ATA33 controller";
167 	if (ata_find_dev(dev, 0x06861106, 0x40) ||
168 	    ata_find_dev(dev, 0x82311106, 0) ||
169 	    ata_find_dev(dev, 0x30741106, 0))
170 	    return "VIA 82C686 ATA100 controller";
171 	if (ata_find_dev(dev, 0x06861106, 0))
172 	    return "VIA 82C686 ATA66 controller";
173 	return "VIA Apollo ATA controller";
174 
175     case 0x55131039:
176 	if (ata_find_dev(dev, 0x06301039, 0x30) ||
177 	    ata_find_dev(dev, 0x06331039, 0x00) ||
178 	    ata_find_dev(dev, 0x06351039, 0x00) ||
179 	    ata_find_dev(dev, 0x06401039, 0x00) ||
180 	    ata_find_dev(dev, 0x06451039, 0x00) ||
181 	    ata_find_dev(dev, 0x06501039, 0x00) ||
182 	    ata_find_dev(dev, 0x07301039, 0x00) ||
183 	    ata_find_dev(dev, 0x07331039, 0x00) ||
184 	    ata_find_dev(dev, 0x07351039, 0x00) ||
185 	    ata_find_dev(dev, 0x07401039, 0x00) ||
186 	    ata_find_dev(dev, 0x07451039, 0x00) ||
187 	    ata_find_dev(dev, 0x07501039, 0x00))
188 	    return "SiS 5591 ATA100 controller";
189 	else if (ata_find_dev(dev, 0x05301039, 0x00) ||
190 	    ata_find_dev(dev, 0x05401039, 0x00) ||
191 	    ata_find_dev(dev, 0x06201039, 0x00) ||
192 	    ata_find_dev(dev, 0x06301039, 0x00))
193 	    return "SiS 5591 ATA66 controller";
194 	else
195 	    return "SiS 5591 ATA33 controller";
196 
197     case 0x06491095:
198 	return "CMD 649 ATA100 controller";
199 
200     case 0x06481095:
201 	return "CMD 648 ATA66 controller";
202 
203     case 0x06461095:
204 	return "CMD 646 ATA controller";
205 
206     case 0xc6931080:
207 	if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
208 	    return "Cypress 82C693 ATA controller";
209 	return NULL;
210 
211     case 0x01021078:
212 	return "Cyrix 5530 ATA33 controller";
213 
214     case 0x74091022:
215 	return "AMD 756 ATA66 controller";
216 
217     case 0x74111022:
218 	return "AMD 766 ATA100 controller";
219 
220     case 0x02111166:
221 	return "ServerWorks ROSB4 ATA33 controller";
222 
223     case 0x4d33105a:
224 	return "Promise ATA33 controller";
225 
226     case 0x4d38105a:
227 	return "Promise ATA66 controller";
228 
229     case 0x0d30105a:
230     case 0x4d30105a:
231 	return "Promise ATA100 controller";
232 
233     case 0x4d68105a:
234     case 0x6268105a:
235 	return "Promise TX2 ATA100 controller";
236 
237     case 0x4d69105a:
238 	return "Promise TX2 ATA133 controller";
239 
240     case 0x00041103:
241 	switch (pci_get_revid(dev)) {
242 	case 0x00:
243 	case 0x01:
244 	    return "HighPoint HPT366 ATA66 controller";
245 	case 0x02:
246 	    return "HighPoint HPT368 ATA66 controller";
247 	case 0x03:
248 	case 0x04:
249 	    return "HighPoint HPT370 ATA100 controller";
250 	case 0x05:
251 	    return "HighPoint HPT372 ATA133 controller";
252 	}
253 	return NULL;
254 
255    /* unsupported but known chipsets, generic DMA only */
256     case 0x10001042:
257     case 0x10011042:
258 	return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
259 
260     case 0x06401095:
261 	return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
262 
263     /* unknown chipsets, try generic DMA if it seems possible */
264     default:
265 	if (pci_get_class(dev) == PCIC_STORAGE &&
266 	    (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
267 	    return "Generic PCI ATA controller";
268     }
269     return NULL;
270 }
271 
272 static int
273 ata_pci_probe(device_t dev)
274 {
275     const char *desc = ata_pci_match(dev);
276 
277     if (desc) {
278 	device_set_desc(dev, desc);
279 	return 0;
280     }
281     else
282 	return ENXIO;
283 }
284 
285 static int
286 ata_pci_add_child(device_t dev, int unit)
287 {
288     device_t child;
289 
290     /* check if this is located at one of the std addresses */
291     if (ATA_MASTERDEV(dev)) {
292 	if (!(child = device_add_child(dev, "ata", unit)))
293 	    return ENOMEM;
294     }
295     else {
296 	if (!(child = device_add_child(dev, "ata",
297 			   devclass_find_free_unit(devclass_find("ata"), 2))))
298 	    return ENOMEM;
299     }
300     return 0;
301 }
302 
303 static int
304 ata_pci_attach(device_t dev)
305 {
306     struct ata_pci_softc *sc = device_get_softc(dev);
307     u_int8_t class, subclass;
308     u_int32_t type, cmd;
309     int rid;
310 
311     /* set up vendor-specific stuff */
312     type = pci_get_devid(dev);
313     class = pci_get_class(dev);
314     subclass = pci_get_subclass(dev);
315     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
316 
317     if (!(cmd & PCIM_CMD_PORTEN)) {
318 	device_printf(dev, "ATA channel disabled by BIOS\n");
319 	return 0;
320     }
321 
322     /* is busmastering supported ? */
323     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) ==
324 	(PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
325 
326 	/* is there a valid port range to connect to ? */
327 	rid = 0x20;
328 	sc->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
329 				      0, ~0, 1, RF_ACTIVE);
330 	if (!sc->bmio)
331 	    device_printf(dev, "Busmastering DMA not configured\n");
332     }
333     else
334 	device_printf(dev, "Busmastering DMA not supported\n");
335 
336     /* do extra chipset specific setups */
337     switch (type) {
338     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
339 	pci_write_config(dev, 0x53,
340 			 (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
341 	break;
342 
343     case 0x4d38105a: /* Promise 66 & 100 (before TX2) need the clock changed */
344     case 0x4d30105a:
345     case 0x0d30105a:
346 	ATA_OUTB(sc->bmio, 0x11, ATA_INB(sc->bmio, 0x11) | 0x0a);
347 	/* FALLTHROUGH */
348 
349     case 0x4d33105a: /* Promise (before TX2) need burst mode turned on */
350 	ATA_OUTB(sc->bmio, 0x1f, ATA_INB(sc->bmio, 0x1f) | 0x01);
351 	break;
352 
353     case 0x00041103: /* HighPoint HPT 366/368/370/372 */
354 	switch (pci_get_revid(dev)) {
355 	case 0x00:	/* HPT 366 */
356 	case 0x01:
357 	    /* turn off interrupt prediction */
358 	    pci_write_config(dev, 0x51,
359 	    		     (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
360 	    break;
361 
362 	case 0x02:	/* HPT 368 */
363 	case 0x03:	/* HPT 370 */
364 	case 0x04:
365 	case 0x05:	/* HPT 372 */
366 	    /* turn off interrupt prediction */
367 	    pci_write_config(dev, 0x51,
368 	    		     (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
369 	    pci_write_config(dev, 0x55,
370 	    		     (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
371 	    /* turn on interrupts */
372 	    pci_write_config(dev, 0x5a,
373 	    		     (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
374 
375 	}
376 	break;
377 
378     case 0x05711106: /* VIA 82C586, '596, '686 default setup */
379 	/* prepare for ATA-66 on the 82C686a and rev 0x12 and newer 82C596's */
380 	if ((ata_find_dev(dev, 0x06861106, 0) &&
381 	     !ata_find_dev(dev, 0x06861106, 0x40)) ||
382 	    ata_find_dev(dev, 0x05961106, 0x12))
383 	    pci_write_config(dev, 0x50, 0x030b030b, 4);
384 
385 	/* the southbridge might need the data corruption fix */
386 	if (ata_find_dev(dev, 0x06861106, 0x40) ||
387 	    ata_find_dev(dev, 0x82311106, 0x10))
388 	    ata_via_southbridge_fixup(dev);
389 	/* FALLTHROUGH */
390 
391     case 0x74091022: /* AMD 756 default setup */
392     case 0x74111022: /* AMD 766 default setup */
393 
394 	/* set prefetch, postwrite */
395 	pci_write_config(dev, 0x41, pci_read_config(dev, 0x41, 1) | 0xf0, 1);
396 
397 	/* set fifo configuration half'n'half */
398 	pci_write_config(dev, 0x43,
399 			 (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
400 
401 	/* set status register read retry */
402 	pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
403 
404 	/* set DMA read & end-of-sector fifo flush */
405 	pci_write_config(dev, 0x46,
406 			 (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
407 
408 	/* set sector size */
409 	pci_write_config(dev, 0x60, DEV_BSIZE, 2);
410 	pci_write_config(dev, 0x68, DEV_BSIZE, 2);
411 	break;
412 
413     case 0x10001042:   /* RZ 100? known bad, no DMA */
414     case 0x10011042:
415     case 0x06401095:   /* CMD 640 known bad, no DMA */
416 	sc->bmio = NULL;
417 	device_printf(dev, "Busmastering DMA disabled\n");
418     }
419 
420     if (sc->bmio) {
421 	sc->bmaddr = rman_get_start(sc->bmio);
422 	BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
423 			     SYS_RES_IOPORT, rid, sc->bmio);
424 	sc->bmio = NULL;
425     }
426 
427     /*
428      * the Cypress chip is a mess, it contains two ATA functions, but
429      * both channels are visible on the first one.
430      * simply ignore the second function for now, as the right
431      * solution (ignoring the second channel on the first function)
432      * doesn't work with the crappy ATA interrupt setup on the alpha.
433      */
434     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
435 	return 0;
436 
437     ata_pci_add_child(dev, 0);
438 
439     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
440 	ata_pci_add_child(dev, 1);
441 
442     return bus_generic_attach(dev);
443 }
444 
445 static int
446 ata_pci_intr(struct ata_softc *scp)
447 {
448     u_int8_t dmastat;
449 
450     /*
451      * since we might share the IRQ with another device, and in some
452      * cases with our twin channel, we only want to process interrupts
453      * that we know this channel generated.
454      */
455     switch (scp->chiptype) {
456     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
457 	if (((dmastat = ata_dmastatus(scp)) &
458 	    (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
459 	    return 1;
460 	ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
461 	DELAY(1);
462 	return 0;
463 
464     case 0x06481095:	/* CMD 648 */
465     case 0x06491095:	/* CMD 649 */
466         if (!(pci_read_config(device_get_parent(scp->dev), 0x71, 1) &
467 	      (scp->channel ? 0x08 : 0x04)))
468 	    return 1;
469 	break;
470 
471     case 0x4d33105a:	/* Promise Ultra/Fasttrak 33 */
472     case 0x4d38105a:	/* Promise Ultra/Fasttrak 66 */
473     case 0x4d30105a:	/* Promise Ultra/Fasttrak 100 */
474     case 0x0d30105a:	/* Promise OEM ATA100 */
475 	if (!(ATA_INL(scp->r_bmio, (scp->channel ? 0x14 : 0x1c)) &
476 	      (scp->channel ? 0x00004000 : 0x00000400)))
477 	    return 1;
478     	break;
479 
480     case 0x4d68105a:	/* Promise TX2 ATA100 */
481     case 0x6268105a:	/* Promise TX2 ATA100 */
482     case 0x4d69105a:	/* Promise TX2 ATA133 */
483 	ATA_OUTB(scp->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
484 	if (!(ATA_INB(scp->r_bmio, ATA_BMDEVSPEC_1) & 0x20))
485 	    return 1;
486 	break;
487     }
488 
489     if (scp->flags & ATA_DMA_ACTIVE) {
490 	if (!((dmastat = ata_dmastatus(scp)) & ATA_BMSTAT_INTERRUPT))
491 	    return 1;
492 	ATA_OUTB(scp->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
493 	DELAY(1);
494     }
495     return 0;
496 }
497 
498 static int
499 ata_pci_print_child(device_t dev, device_t child)
500 {
501     struct ata_softc *scp = device_get_softc(child);
502     int retval = 0;
503 
504     retval += bus_print_child_header(dev, child);
505     retval += printf(": at 0x%lx", rman_get_start(scp->r_io));
506 
507     if (ATA_MASTERDEV(dev))
508 	retval += printf(" irq %d", 14 + scp->channel);
509 
510     retval += bus_print_child_footer(dev, child);
511 
512     return retval;
513 }
514 
515 static struct resource *
516 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
517 		       u_long start, u_long end, u_long count, u_int flags)
518 {
519     struct ata_pci_softc *sc = device_get_softc(dev);
520     struct resource *res = NULL;
521     int channel = ((struct ata_softc *)device_get_softc(child))->channel;
522     int myrid;
523 
524     if (type == SYS_RES_IOPORT) {
525 	switch (*rid) {
526 	case ATA_IOADDR_RID:
527 	    if (ATA_MASTERDEV(dev)) {
528 		myrid = 0;
529 		start = (channel ? ATA_SECONDARY : ATA_PRIMARY);
530 		end = start + ATA_IOSIZE - 1;
531 		count = ATA_IOSIZE;
532 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
533 					 SYS_RES_IOPORT, &myrid,
534 					 start, end, count, flags);
535 	    }
536 	    else {
537 		myrid = 0x10 + 8 * channel;
538 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
539 					 SYS_RES_IOPORT, &myrid,
540 					 start, end, count, flags);
541 	    }
542 	    break;
543 
544 	case ATA_ALTADDR_RID:
545 	    if (ATA_MASTERDEV(dev)) {
546 		myrid = 0;
547 		start = (channel ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
548 		end = start + ATA_ALTIOSIZE - 1;
549 		count = ATA_ALTIOSIZE;
550 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
551 					 SYS_RES_IOPORT, &myrid,
552 					 start, end, count, flags);
553 	    }
554 	    else {
555 		myrid = 0x14 + 8 * channel;
556 	    	res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
557 					 SYS_RES_IOPORT, &myrid,
558 					 start, end, count, flags);
559 		if (res) {
560 			start = rman_get_start(res) + 2;
561 			end = rman_get_start(res) + ATA_ALTIOSIZE - 1;
562 			count = ATA_ALTIOSIZE;
563 			BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
564 					     SYS_RES_IOPORT, myrid, res);
565 	    		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
566 						 SYS_RES_IOPORT, &myrid,
567 						 start, end, count, flags);
568 		}
569 	    }
570 	    break;
571 
572 	case ATA_BMADDR_RID:
573 	    if (sc->bmaddr) {
574 		myrid = 0x20;
575 		start = (channel == 0 ? sc->bmaddr : sc->bmaddr + ATA_BMIOSIZE);
576 		end = start + ATA_BMIOSIZE - 1;
577 		count = ATA_BMIOSIZE;
578 		res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
579 					  SYS_RES_IOPORT, &myrid,
580 					  start, end, count, flags);
581 	    }
582 	}
583 	return res;
584     }
585 
586     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
587 	if (ATA_MASTERDEV(dev)) {
588 #ifdef __alpha__
589 	    return alpha_platform_alloc_ide_intr(channel);
590 #else
591 	    int irq = (channel == 0 ? 14 : 15);
592 
593 	    return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
594 				      SYS_RES_IRQ, rid, irq, irq, 1, flags);
595 #endif
596 	}
597 	else {
598 	    /* primary and secondary channels share interrupt, keep track */
599 	    if (!sc->irq)
600 		sc->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
601 					     SYS_RES_IRQ, rid, 0, ~0, 1, flags);
602 	    sc->irqcnt++;
603 	    return sc->irq;
604 	}
605     }
606     return 0;
607 }
608 
609 static int
610 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
611 			 struct resource *r)
612 {
613     struct ata_pci_softc *sc = device_get_softc(dev);
614     int channel = ((struct ata_softc *)device_get_softc(child))->channel;
615 
616     if (type == SYS_RES_IOPORT) {
617 	switch (rid) {
618 	case ATA_IOADDR_RID:
619 	    if (ATA_MASTERDEV(dev))
620 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
621 					    SYS_RES_IOPORT, 0x0, r);
622 	    else
623 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
624 					    SYS_RES_IOPORT, 0x10+8*channel, r);
625 	    break;
626 
627 	case ATA_ALTADDR_RID:
628 	    if (ATA_MASTERDEV(dev))
629 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
630 					    SYS_RES_IOPORT, 0x0, r);
631 	    else
632 		return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
633 					    SYS_RES_IOPORT, 0x14+8*channel, r);
634 	    break;
635 
636 	case ATA_BMADDR_RID:
637 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
638 					SYS_RES_IOPORT, 0x20, r);
639 	default:
640 	    return ENOENT;
641 	}
642     }
643     if (type == SYS_RES_IRQ) {
644 	if (rid != ATA_IRQ_RID)
645 	    return ENOENT;
646 
647 	if (ATA_MASTERDEV(dev)) {
648 #ifdef __alpha__
649 	    return alpha_platform_release_ide_intr(channel, r);
650 #else
651 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
652 					SYS_RES_IRQ, rid, r);
653 #endif
654 	}
655 	else {
656 	    /* primary and secondary channels share interrupt, keep track */
657 	    if (--sc->irqcnt)
658 		return 0;
659 	    sc->irq = 0;
660 	    return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
661 					SYS_RES_IRQ, rid, r);
662 	}
663     }
664     return EINVAL;
665 }
666 
667 static int
668 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq,
669 		   int flags, driver_intr_t *intr, void *arg,
670 		   void **cookiep)
671 {
672     if (ATA_MASTERDEV(dev)) {
673 #ifdef __alpha__
674 	return alpha_platform_setup_ide_intr(child, irq, intr, arg, cookiep);
675 #else
676 	return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
677 			      flags, intr, arg, cookiep);
678 #endif
679     }
680     else
681 	return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
682 			      flags, intr, arg, cookiep);
683 }
684 
685 static int
686 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
687 		      void *cookie)
688 {
689     if (ATA_MASTERDEV(dev)) {
690 #ifdef __alpha__
691 	return alpha_platform_teardown_ide_intr(child, irq, cookie);
692 #else
693 	return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
694 #endif
695     }
696     else
697 	return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
698 }
699 
700 static device_method_t ata_pci_methods[] = {
701     /* device interface */
702     DEVMETHOD(device_probe,		ata_pci_probe),
703     DEVMETHOD(device_attach,		ata_pci_attach),
704     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
705     DEVMETHOD(device_suspend,		bus_generic_suspend),
706     DEVMETHOD(device_resume,		bus_generic_resume),
707 
708     /* bus methods */
709     DEVMETHOD(bus_print_child,		ata_pci_print_child),
710     DEVMETHOD(bus_alloc_resource,	ata_pci_alloc_resource),
711     DEVMETHOD(bus_release_resource,	ata_pci_release_resource),
712     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
713     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
714     DEVMETHOD(bus_setup_intr,		ata_pci_setup_intr),
715     DEVMETHOD(bus_teardown_intr,	ata_pci_teardown_intr),
716     { 0, 0 }
717 };
718 
719 static driver_t ata_pci_driver = {
720     "atapci",
721     ata_pci_methods,
722     sizeof(struct ata_pci_softc),
723 };
724 
725 static devclass_t ata_pci_devclass;
726 
727 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
728 
729 static int
730 ata_pcisub_probe(device_t dev)
731 {
732     struct ata_softc *scp = device_get_softc(dev);
733     device_t *children;
734     int count, i;
735 
736     /* find channel number on this controller */
737     device_get_children(device_get_parent(dev), &children, &count);
738     for (i = 0; i < count; i++) {
739 	if (children[i] == dev)
740 	    scp->channel = i;
741     }
742     free(children, M_TEMP);
743     scp->chiptype = pci_get_devid(device_get_parent(dev));
744     scp->intr_func = ata_pci_intr;
745     return ata_probe(dev);
746 }
747 
748 static device_method_t ata_pcisub_methods[] = {
749     /* device interface */
750     DEVMETHOD(device_probe,	ata_pcisub_probe),
751     DEVMETHOD(device_attach,	ata_attach),
752     DEVMETHOD(device_detach,	ata_detach),
753     DEVMETHOD(device_resume,	ata_resume),
754     { 0, 0 }
755 };
756 
757 static driver_t ata_pcisub_driver = {
758     "ata",
759     ata_pcisub_methods,
760     sizeof(struct ata_softc),
761 };
762 
763 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);
764