Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / disk / ata / ata-pci.c
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: src/sys/dev/ata/ata-pci.c,v 1.32.2.15 2003/06/06 13:27:05 fjoe Exp $
29  * $DragonFly: src/sys/dev/disk/ata/ata-pci.c,v 1.19 2005/04/05 22:37:16 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/disk.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/buf.h>
39 #include <sys/malloc.h>
40 #include <sys/devicestat.h>
41 #include <sys/sysctl.h>
42 #include <machine/stdarg.h>
43 #include <machine/resource.h>
44 #include <machine/bus.h>
45 #include <machine/clock.h>
46 #ifdef __alpha__
47 #include <machine/md_var.h>
48 #endif
49 #include <sys/rman.h>
50 #include <bus/pci/pcivar.h>
51 #include <bus/pci/pcireg.h>
52 #include "ata-all.h"
53
54 /* device structures */
55 struct ata_pci_controller {
56     struct resource *bmio;
57     int bmaddr;
58     struct resource *irq;
59     int irqcnt;
60 };
61
62 /* misc defines */
63 #define IOMASK                  0xfffffffc
64 #define GRANDPARENT(dev)        device_get_parent(device_get_parent(dev))
65 #define ATA_MASTERDEV(dev)      ((pci_get_progif(dev) & 0x80) && \
66                                  (pci_get_progif(dev) & 0x05) != 0x05)
67
68 int
69 ata_find_dev(device_t dev, u_int32_t devid, u_int32_t revid)
70 {
71     device_t *children;
72     int nchildren, i;
73
74     if (device_get_children(device_get_parent(dev), &children, &nchildren))
75         return 0;
76
77     for (i = 0; i < nchildren; i++) {
78         if (pci_get_devid(children[i]) == devid &&
79             pci_get_revid(children[i]) >= revid) {
80             free(children, M_TEMP);
81             return 1;
82         }
83     }
84     free(children, M_TEMP);
85     return 0;
86 }
87
88 static void
89 ata_via_southbridge_fixup(device_t dev)
90 {
91     device_t *children;
92     int nchildren, i;
93
94     if (device_get_children(device_get_parent(dev), &children, &nchildren))
95         return;
96
97     for (i = 0; i < nchildren; i++) {
98         if (pci_get_devid(children[i]) == 0x03051106 ||         /* VIA VT8363 */
99             pci_get_devid(children[i]) == 0x03911106 ||         /* VIA VT8371 */
100             pci_get_devid(children[i]) == 0x31021106 ||         /* VIA VT8662 */
101             pci_get_devid(children[i]) == 0x31121106) {         /* VIA VT8361 */
102             u_int8_t reg76 = pci_read_config(children[i], 0x76, 1);
103
104             if ((reg76 & 0xf0) != 0xd0) {
105                 device_printf(dev,
106                 "Correcting VIA config for southbridge data corruption bug\n");
107                 pci_write_config(children[i], 0x75, 0x80, 1);
108                 pci_write_config(children[i], 0x76, (reg76 & 0x0f) | 0xd0, 1);
109             }
110             break;
111         }
112     }
113     free(children, M_TEMP);
114 }
115
116 static const char *
117 ata_pci_match(device_t dev)
118 {
119     if (pci_get_class(dev) != PCIC_STORAGE)
120         return NULL;
121
122     switch (pci_get_devid(dev)) {
123     /* supported chipsets */
124     case 0x12308086:
125         return "Intel PIIX ATA controller";
126
127     case 0x70108086:
128         return "Intel PIIX3 ATA controller";
129
130     case 0x71118086:
131     case 0x71998086:
132     case 0x84ca8086:
133         return "Intel PIIX4 ATA33 controller";
134
135     case 0x24218086:
136         return "Intel ICH0 ATA33 controller";
137
138     case 0x24118086:
139     case 0x76018086:
140         return "Intel ICH ATA66 controller";
141
142     case 0x244a8086:
143     case 0x244b8086:
144         return "Intel ICH2 ATA100 controller";
145
146     case 0x248a8086:
147     case 0x248b8086:
148         return "Intel ICH3 ATA100 controller";
149
150     case 0x24ca8086:
151     case 0x24cb8086:
152         return "Intel ICH4 ATA100 controller";
153
154     case 0x24d18086:
155         return "Intel ICH5 SATA150 controller";
156
157     case 0x24db8086:
158         return "Intel ICH5 ATA100 controller";
159
160     case 0x26518086:
161         return "Intel ICH6/W SATA150 controller";
162
163     case 0x26528086:
164         return "Intel ICH6R/RW SATA150 controller";
165
166     case 0x266f8086:
167         return "Intel ICH6R/RW ATA100 controller";
168
169     case 0x522910b9:
170         if (pci_get_revid(dev) >= 0xc4)
171             return "AcerLabs Aladdin ATA100 controller";
172         else if (pci_get_revid(dev) >= 0xc2)
173             return "AcerLabs Aladdin ATA66 controller";
174         else if (pci_get_revid(dev) >= 0x20)
175             return "AcerLabs Aladdin ATA33 controller";
176         else
177             return "AcerLabs Aladdin ATA controller";
178
179     case 0x05711106: 
180         if (ata_find_dev(dev, 0x05861106, 0x02))
181             return "VIA 82C586 ATA33 controller";
182         if (ata_find_dev(dev, 0x05861106, 0))
183             return "VIA 82C586 ATA controller";
184         if (ata_find_dev(dev, 0x05961106, 0x12))
185             return "VIA 82C596 ATA66 controller";
186         if (ata_find_dev(dev, 0x05961106, 0))
187             return "VIA 82C596 ATA33 controller";
188         if (ata_find_dev(dev, 0x06861106, 0x40))
189             return "VIA 82C686 ATA100 controller";
190         if (ata_find_dev(dev, 0x06861106, 0x10))
191             return "VIA 82C686 ATA66 controller";
192         if (ata_find_dev(dev, 0x06861106, 0))
193             return "VIA 82C686 ATA33 controller";
194         if (ata_find_dev(dev, 0x82311106, 0))
195             return "VIA 8231 ATA100 controller";
196         if (ata_find_dev(dev, 0x30741106, 0) ||
197             ata_find_dev(dev, 0x31091106, 0))
198             return "VIA 8233 ATA100 controller";
199         if (ata_find_dev(dev, 0x31471106, 0))
200             return "VIA 8233 ATA133 controller";
201         if (ata_find_dev(dev, 0x31771106, 0))
202             return "VIA 8235 ATA133 controller";
203         if (ata_find_dev(dev, 0x31491106, 0))
204             return "VIA 8237 ATA133 controller";
205         return "VIA Apollo ATA controller";
206
207     case 0x31491106:
208          return "VIA 8237 SATA 150 controller";
209
210     case 0x55131039:
211         if (ata_find_dev(dev, 0x07461039, 0))
212             return "SiS 5591 ATA133 controller";
213         if (ata_find_dev(dev, 0x06301039, 0x30) ||
214             ata_find_dev(dev, 0x06331039, 0) ||
215             ata_find_dev(dev, 0x06351039, 0) ||
216             ata_find_dev(dev, 0x06401039, 0) ||
217             ata_find_dev(dev, 0x06451039, 0) ||
218             ata_find_dev(dev, 0x06461039, 0) ||
219             ata_find_dev(dev, 0x06481039, 0) ||
220             ata_find_dev(dev, 0x06501039, 0) ||
221             ata_find_dev(dev, 0x07301039, 0) ||
222             ata_find_dev(dev, 0x07331039, 0) ||
223             ata_find_dev(dev, 0x07351039, 0) ||
224             ata_find_dev(dev, 0x07401039, 0) ||
225             ata_find_dev(dev, 0x07451039, 0) ||
226             ata_find_dev(dev, 0x07501039, 0))
227             return "SiS 5591 ATA100 controller";
228         else if (ata_find_dev(dev, 0x05301039, 0) ||
229             ata_find_dev(dev, 0x05401039, 0) ||
230             ata_find_dev(dev, 0x06201039, 0) ||
231             ata_find_dev(dev, 0x06301039, 0))
232             return "SiS 5591 ATA66 controller";
233         else
234             return "SiS 5591 ATA33 controller";
235
236     case 0x35121095:
237         return "SiI 3512 SATA controller";
238
239     case 0x31121095:
240         return "SiI 3112 SATA controller";
241
242     case 0x31141095:
243         return "SiI 3114 SATA controller";
244
245     case 0x31241095:
246         return "SiI 3124 SATA controller";
247
248     case 0x06801095:
249         return "SiI 0680 ATA133 controller";
250
251     case 0x06491095:
252         return "CMD 649 ATA100 controller";
253
254     case 0x06481095:
255         return "CMD 648 ATA66 controller";
256
257     case 0x06461095:
258         return "CMD 646 ATA controller";
259
260     case 0xc6931080:
261         if (pci_get_subclass(dev) == PCIS_STORAGE_IDE)
262             return "Cypress 82C693 ATA controller";
263         return NULL;
264
265     case 0x01021078:
266         return "Cyrix 5530 ATA33 controller";
267
268     case 0x74091022:
269         return "AMD 756 ATA66 controller";
270
271     case 0x74111022:
272         return "AMD 766 ATA100 controller";
273
274     case 0x74411022:
275         return "AMD 768 ATA100 controller";
276
277     case 0x74691022:
278         return "AMD 8111 UltraATA/133 controller";
279
280     case 0x01bc10de:
281         return "nVIDIA nForce1 ATA100 controller";
282
283     case 0x006510de:
284         return "nVIDIA nForce2 ATA133 controller";
285
286     case 0x00d510de:
287         return "nVIDIA nForce3 ATA133 controller";
288
289     case 0x00e310de:
290         return "nVIDIA nForce3 PRO S1 controller";
291
292     case 0x00e510de:
293         return "nVIDIA nForce3 PRO controller";
294
295     case 0x02111166:
296         return "ServerWorks ROSB4 ATA33 controller";
297
298     case 0x02121166:
299         if (pci_get_revid(dev) >= 0x92)
300             return "ServerWorks CSB5 ATA100 controller";
301         else
302             return "ServerWorks CSB5 ATA66 controller";
303
304     case 0x02131166:
305         return "ServerWorks CSB6 ATA100 controller (channel 0+1)";
306
307     case 0x02171166:
308         return "ServerWorks CSB6 ATA66 controller (channel 2)";
309
310     case 0x4d33105a:
311         return "Promise ATA33 controller";
312
313     case 0x0d38105a:
314     case 0x4d38105a:
315         return "Promise ATA66 controller";
316
317     case 0x0d30105a:
318     case 0x4d30105a:
319         return "Promise ATA100 controller";
320
321     case 0x4d68105a:
322     case 0x6268105a: 
323         if (pci_get_devid(GRANDPARENT(dev)) == 0x00221011 &&
324             pci_get_class(GRANDPARENT(dev)) == PCIC_BRIDGE) {
325             static long start = 0, end = 0;
326
327             /* we belive we are on a TX4, now do our (simple) magic */
328             if (pci_get_slot(dev) == 1) {
329                 bus_get_resource(dev, SYS_RES_IRQ, 0, &start, &end);
330                 return "Promise TX4 ATA100 controller (channel 0+1)";
331             }
332             else if (pci_get_slot(dev) == 2 && start && end) {
333                 bus_set_resource(dev, SYS_RES_IRQ, 0, start, end);
334                 start = end = 0;
335                 return "Promise TX4 ATA100 controller (channel 2+3)";
336             }
337             else
338                 start = end = 0;
339         }
340         return "Promise TX2 ATA100 controller";
341
342     case 0x4d69105a:
343     case 0x5275105a:
344     case 0x6269105a: 
345     case 0x7275105a: 
346         return "Promise TX2 ATA133 controller";
347
348     case 0x00041103:
349         switch (pci_get_revid(dev)) {
350         case 0x00:
351         case 0x01:
352             return "HighPoint HPT366 ATA66 controller";
353         case 0x02:
354             return "HighPoint HPT368 ATA66 controller";
355         case 0x03:
356         case 0x04:
357             return "HighPoint HPT370 ATA100 controller";
358         case 0x05:
359             return "HighPoint HPT372 ATA133 controller";
360         }
361         return NULL;
362
363     case 0x00051103:
364         switch (pci_get_revid(dev)) {
365         case 0x01:
366         case 0x02:
367             return "HighPoint HPT372 ATA133 controller";
368         }
369         return NULL;
370
371     case 0x00081103:
372         switch (pci_get_revid(dev)) {
373         case 0x07:
374             return "HighPoint HPT374 ATA133 controller";
375         }
376         return NULL;
377
378     case 0x000116ca:
379         return "Cenatek Rocket Drive controller";
380
381    /* unsupported but known chipsets, generic DMA only */
382     case 0x10001042:
383     case 0x10011042:
384         return "RZ 100? ATA controller !WARNING! buggy chip data loss possible";
385
386     case 0x06401095:
387         return "CMD 640 ATA controller !WARNING! buggy chip data loss possible";
388
389     /* unknown chipsets, try generic DMA if it seems possible */
390     default:
391         if (pci_get_class(dev) == PCIC_STORAGE &&
392             (pci_get_subclass(dev) == PCIS_STORAGE_IDE))
393             return "Generic PCI ATA controller";
394     }
395     return NULL;
396 }
397
398 static int
399 ata_pci_probe(device_t dev)
400 {
401     const char *desc = ata_pci_match(dev);
402     
403     if (desc) {
404         device_set_desc(dev, desc);
405         return 0;
406     } 
407     else
408         return ENXIO;
409 }
410
411 static int
412 ata_pci_add_child(device_t dev, int unit)
413 {
414     device_t child;
415
416     /* check if this is located at one of the std addresses */
417     if (ATA_MASTERDEV(dev)) {
418         if (!(child = device_add_child(dev, "ata", unit)))
419             return ENOMEM;
420     }
421     else {
422         if (!(child = device_add_child(dev, "ata", 2)))
423             return ENOMEM;
424     }
425     return 0;
426 }
427
428 static int
429 ata_pci_attach(device_t dev)
430 {
431     struct ata_pci_controller *controller = device_get_softc(dev);
432     u_int8_t class, subclass;
433     u_int32_t type, cmd;
434     int rid;
435     int flags;
436
437     /* set up vendor-specific stuff */
438     type = pci_get_devid(dev);
439     class = pci_get_class(dev);
440     subclass = pci_get_subclass(dev);
441     cmd = pci_read_config(dev, PCIR_COMMAND, 4);
442
443     if (!(cmd & PCIM_CMD_PORTEN)) {
444         device_printf(dev, "ATA channel disabled by BIOS\n");
445         return 0;
446     }
447
448     /* is busmastering supported ? */
449     if ((cmd & (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) == 
450         (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN)) {
451
452         /* is there a valid port range to connect to ? */
453         rid = 0x20;
454         controller->bmio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
455                                               0, ~0, 1, RF_ACTIVE);
456         if (!controller->bmio)
457             device_printf(dev, "Busmastering DMA not configured\n");
458     }
459     else
460         device_printf(dev, "Busmastering DMA not supported\n");
461
462     /* do extra chipset specific setups */
463     flags = 0;
464
465     switch (type) {
466     case 0x522910b9: /* Aladdin need to activate the ATAPI FIFO */
467         pci_write_config(dev, 0x53, 
468                          (pci_read_config(dev, 0x53, 1) & ~0x01) | 0x02, 1);
469         break;
470
471     case 0x4d38105a: /* Promise 66 & 100 (before TX2) need the clock changed */
472     case 0x4d30105a:
473     case 0x0d30105a:
474         ATA_OUTB(controller->bmio, 0x11, ATA_INB(controller->bmio, 0x11)|0x0a);
475         /* FALLTHROUGH */
476
477     case 0x4d33105a: /* Promise (before TX2) need burst mode turned on */
478         ATA_OUTB(controller->bmio, 0x1f, ATA_INB(controller->bmio, 0x1f)|0x01);
479         break;
480
481     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
482         if (pci_get_revid(dev) < 2) {   /* HPT 366 */
483             /* turn off interrupt prediction */
484             pci_write_config(dev, 0x51, 
485                              (pci_read_config(dev, 0x51, 1) & ~0x80), 1);
486             break;
487         }
488         if (pci_get_revid(dev) < 5) {   /* HPT368/370 */
489             /* turn off interrupt prediction */
490             pci_write_config(dev, 0x51,
491                              (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
492             pci_write_config(dev, 0x55,
493                              (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
494
495             /* turn on interrupts */
496             pci_write_config(dev, 0x5a,
497                              (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
498
499             /* set clocks etc */
500             pci_write_config(dev, 0x5b, 0x22, 1);
501             break;
502         }
503         /* FALLTHROUGH */
504
505     case 0x00051103:    /* HighPoint HPT372 */
506     case 0x00081103:    /* HighPoint HPT374 */
507         /* turn off interrupt prediction */
508         pci_write_config(dev, 0x51, (pci_read_config(dev, 0x51, 1) & ~0x03), 1);
509         pci_write_config(dev, 0x55, (pci_read_config(dev, 0x55, 1) & ~0x03), 1);
510
511         /* turn on interrupts */
512         pci_write_config(dev, 0x5a, (pci_read_config(dev, 0x5a, 1) & ~0x10), 1);
513
514         /* set clocks etc */
515         pci_write_config(dev, 0x5b,
516                          (pci_read_config(dev, 0x5b, 1) & 0x01) | 0x20, 1);
517         break;
518
519     case 0x05711106: /* VIA 82C586, '596, '686 default setup */
520         /* prepare for ATA-66 on the 82C686a and 82C596b */
521         if ((ata_find_dev(dev, 0x06861106, 0x10) && 
522              !ata_find_dev(dev, 0x06861106, 0x40)) || 
523             ata_find_dev(dev, 0x05961106, 0x12))
524             pci_write_config(dev, 0x50, 0x030b030b, 4);   
525
526         /* the southbridge might need the data corruption fix */
527         if (ata_find_dev(dev, 0x06861106, 0x40) ||
528             ata_find_dev(dev, 0x82311106, 0x10))
529             ata_via_southbridge_fixup(dev);
530
531         /* set fifo configuration half'n'half */
532         pci_write_config(dev, 0x43, 
533                          (pci_read_config(dev, 0x43, 1) & 0x90) | 0x2a, 1);
534
535         /* set status register read retry */
536         pci_write_config(dev, 0x44, pci_read_config(dev, 0x44, 1) | 0x08, 1);
537
538         /* set DMA read & end-of-sector fifo flush */
539         pci_write_config(dev, 0x46, 
540                          (pci_read_config(dev, 0x46, 1) & 0x0c) | 0xf0, 1);
541
542         /* set sector size */
543         pci_write_config(dev, 0x60, DEV_BSIZE, 2);
544         pci_write_config(dev, 0x68, DEV_BSIZE, 2);
545         break;
546     case 0x74111022: /* AMD 766 default setup */
547         flags = 1;      /* bugged */
548         /* fall through */
549     case 0x74091022: /* AMD 756 default setup */
550     case 0x74411022: /* AMD 768 default setup */
551     case 0x746d1022: /* AMD 8111 default setup */
552         if (flags) {
553                 pci_write_config(dev, 0x41,  
554                                 pci_read_config(dev, 0x41, 1) & 0x0f, 1);
555         } else {
556                 pci_write_config(dev, 0x41, 
557                                 pci_read_config(dev, 0x41, 1) | 0xf0, 1);
558         }
559         break;
560     case 0x01bc10de: /* NVIDIA nForce1 default setup */
561     case 0x006510de: /* NVIDIA nForce2 default setup */
562         flags = 1;
563         /* fall through */
564     case 0x00d510de: /* NVIDIA nForce3 default setup */
565         if (flags) {
566                 pci_write_config(dev, 0x51, 
567                                 pci_read_config(dev, 0x51, 1) & 0x0f, 1);  
568         } else {
569                 pci_write_config(dev, 0x51,
570                                 pci_read_config(dev, 0x51, 1) | 0xf0, 1);  
571         }
572         break;
573
574     case 0x02111166: /* ServerWorks ROSB4 enable UDMA33 */
575         pci_write_config(dev, 0x64,   
576                          (pci_read_config(dev, 0x64, 4) & ~0x00002000) |
577                          0x00004000, 4);
578         break;
579         
580     case 0x02121166: /* ServerWorks CSB5 enable UDMA66/100 depending on rev */
581         pci_write_config(dev, 0x5a,   
582                          (pci_read_config(dev, 0x5a, 1) & ~0x40) |
583                          (pci_get_revid(dev) >= 0x92) ? 0x03 : 0x02, 1);
584         break;
585
586     case 0x06801095: /* SiI 0680 set ATA reference clock speed */
587         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
588             pci_write_config(dev, 0x8a, 
589                              (pci_read_config(dev, 0x8a, 1) & 0x0F) | 0x10, 1);
590         if ((pci_read_config(dev, 0x8a, 1) & 0x30) != 0x10)
591             device_printf(dev, "SiI 0680 could not set clock\n");
592         break;
593
594     case 0x06491095:
595     case 0x06481095:
596     case 0x06461095: /* CMD 646 enable interrupts, set DMA read mode */
597         pci_write_config(dev, 0x71, 0x01, 1);
598         break;
599
600     case 0x10001042:   /* RZ 100? known bad, no DMA */
601     case 0x10011042:
602     case 0x06401095:   /* CMD 640 known bad, no DMA */
603         controller->bmio = NULL;
604         device_printf(dev, "Busmastering DMA disabled\n");
605     }
606
607     if (controller->bmio) {
608         controller->bmaddr = rman_get_start(controller->bmio);
609         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
610                              SYS_RES_IOPORT, rid, controller->bmio);
611         controller->bmio = NULL;
612     }
613
614     /*
615      * the Cypress chip is a mess, it contains two ATA functions, but 
616      * both channels are visible on the first one.
617      * simply ignore the second function for now, as the right
618      * solution (ignoring the second channel on the first function)
619      * doesn't work with the crappy ATA interrupt setup on the alpha.
620      */
621     if (pci_get_devid(dev) == 0xc6931080 && pci_get_function(dev) > 1)
622         return 0;
623
624     ata_pci_add_child(dev, 0);
625
626     if (ATA_MASTERDEV(dev) || pci_read_config(dev, 0x18, 4) & IOMASK)
627         ata_pci_add_child(dev, 1);
628
629     return bus_generic_attach(dev);
630 }
631
632 static int
633 ata_pci_intr(struct ata_channel *ch)
634 {
635     u_int8_t dmastat;
636
637     /* 
638      * since we might share the IRQ with another device, and in some
639      * cases with our twin channel, we only want to process interrupts
640      * that we know this channel generated.
641      */
642     switch (ch->chiptype) {
643     case 0x00041103:    /* HighPoint HPT366/368/370/372 */
644     case 0x00051103:    /* HighPoint HPT372 */
645     case 0x00081103:    /* HighPoint HPT374 */
646         if (((dmastat = ata_dmastatus(ch)) &
647             (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) != ATA_BMSTAT_INTERRUPT)
648             return 1;
649         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
650         DELAY(1);
651         return 0;
652
653     case 0x06481095:    /* CMD 648 */
654     case 0x06491095:    /* CMD 649 */
655         if (!(pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
656               (ch->unit ? 0x08 : 0x04)))
657             return 1;
658 #if !defined(NO_ATANG)
659         pci_write_config(device_get_parent(ch->dev), 0x71,
660                 pci_read_config(device_get_parent(ch->dev), 0x71, 1) &
661                 ~(ch->unit ? 0x04 : 0x08), 1);
662         break;
663 #endif
664
665     case 0x06801095:   /* SiI 680 */
666         if (!(pci_read_config(device_get_parent(ch->dev),
667                                 (ch->unit ? 0xb1 : 0xa1), 1) & 0x08))
668             return 1;
669         break;
670
671     case 0x4d33105a:    /* Promise Ultra/Fasttrak 33 */
672     case 0x0d38105a:    /* Promise Fasttrak 66 */
673     case 0x4d38105a:    /* Promise Ultra/Fasttrak 66 */
674     case 0x0d30105a:    /* Promise OEM ATA100 */
675     case 0x4d30105a:    /* Promise Ultra/Fasttrak 100 */
676         if (!(ATA_INL(ch->r_bmio, (ch->unit ? 0x14 : 0x1c)) &
677               (ch->unit ? 0x00004000 : 0x00000400)))
678             return 1;
679         break;
680
681     case 0x4d68105a:    /* Promise TX2 ATA100 */
682     case 0x6268105a:    /* Promise TX2 ATA100 */
683     case 0x4d69105a:    /* Promise TX2 ATA133 */
684     case 0x5275105a:    /* Promise TX2 ATA133 */
685     case 0x6269105a:    /* Promise TX2 ATA133 */
686     case 0x7275105a:    /* Promise TX2 ATA133 */
687         ATA_OUTB(ch->r_bmio, ATA_BMDEVSPEC_0, 0x0b);
688         if (!(ATA_INB(ch->r_bmio, ATA_BMDEVSPEC_1) & 0x20))
689             return 1;
690         break;
691
692     case 0x24d18086:   /* Intel ICH5 SATA150 */
693     case 0x24db8086:   /* Intel ICH5 ATA100 */
694     case 0x26518086:   /* Intel ICH6 SATA150 */
695     case 0x26528086:   /* Intel ICH6R SATA150 */
696         dmastat = ATA_INB(ch->r_bmio, ATA_BMSTAT_PORT);
697         if ((dmastat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
698                 ATA_BMSTAT_INTERRUPT)
699             return 1;
700         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat &
701             ~(ATA_BMSTAT_DMA_SIMPLEX | ATA_BMSTAT_ERROR));
702         DELAY(1);
703         return 0;
704
705     }
706
707     if (ch->flags & ATA_DMA_ACTIVE) {
708         if (!((dmastat = ata_dmastatus(ch)) & ATA_BMSTAT_INTERRUPT))
709             return 1;
710         ATA_OUTB(ch->r_bmio, ATA_BMSTAT_PORT, dmastat | ATA_BMSTAT_INTERRUPT);
711         DELAY(1);
712     }
713     return 0;
714 }
715
716 static int
717 ata_pci_print_child(device_t dev, device_t child)
718 {
719     struct ata_channel *ch = device_get_softc(child);
720     int retval = 0;
721
722     retval += bus_print_child_header(dev, child);
723     retval += printf(": at 0x%lx", rman_get_start(ch->r_io));
724
725     if (ATA_MASTERDEV(dev))
726         retval += printf(" irq %d", 14 + ch->unit);
727     
728     retval += bus_print_child_footer(dev, child);
729
730     return retval;
731 }
732
733 static struct resource *
734 ata_pci_alloc_resource(device_t dev, device_t child, int type, int *rid,
735                        u_long start, u_long end, u_long count, u_int flags)
736 {
737     struct ata_pci_controller *controller = device_get_softc(dev);
738     struct resource *res = NULL;
739     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
740     int myrid;
741
742     if (type == SYS_RES_IOPORT) {
743         switch (*rid) {
744         case ATA_IOADDR_RID:
745             if (ATA_MASTERDEV(dev)) {
746                 myrid = 0;
747                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY);
748                 end = start + ATA_IOSIZE - 1;
749                 count = ATA_IOSIZE;
750                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
751                                          SYS_RES_IOPORT, &myrid,
752                                          start, end, count, flags);
753             }
754             else {
755                 myrid = 0x10 + 8 * unit;
756                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
757                                          SYS_RES_IOPORT, &myrid,
758                                          start, end, count, flags);
759             }
760             break;
761
762         case ATA_ALTADDR_RID:
763             if (ATA_MASTERDEV(dev)) {
764                 myrid = 0;
765                 start = (unit ? ATA_SECONDARY : ATA_PRIMARY) + ATA_ALTOFFSET;
766                 end = start + ATA_ALTIOSIZE - 1;
767                 count = ATA_ALTIOSIZE;
768                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
769                                          SYS_RES_IOPORT, &myrid,
770                                          start, end, count, flags);
771             }
772             else {
773                 myrid = 0x14 + 8 * unit;
774                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
775                                          SYS_RES_IOPORT, &myrid,
776                                          start, end, count, flags);
777                 if (res) {
778                         start = rman_get_start(res) + 2;
779                         end = start + ATA_ALTIOSIZE - 1;
780                         count = ATA_ALTIOSIZE;
781                         BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
782                                              SYS_RES_IOPORT, myrid, res);
783                         res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev,
784                                                  SYS_RES_IOPORT, &myrid,
785                                                  start, end, count, flags);
786                 }
787             }
788             break;
789
790         case ATA_BMADDR_RID:
791             if (controller->bmaddr) {
792                 myrid = 0x20;
793                 start = (unit == 0 ? 
794                          controller->bmaddr : controller->bmaddr+ATA_BMIOSIZE);
795                 end = start + ATA_BMIOSIZE - 1;
796                 count = ATA_BMIOSIZE;
797                 res = BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
798                                          SYS_RES_IOPORT, &myrid,
799                                          start, end, count, flags);
800             }
801         }
802         return res;
803     }
804
805     if (type == SYS_RES_IRQ && *rid == ATA_IRQ_RID) {
806         if (ATA_MASTERDEV(dev)) {
807 #ifdef __alpha__
808             return alpha_platform_alloc_ide_intr(unit);
809 #else
810             int irq = (unit == 0 ? 14 : 15);
811
812             return BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
813                                       SYS_RES_IRQ, rid, irq, irq, 1, flags);
814 #endif
815         }
816         else {
817             /* primary and secondary channels share interrupt, keep track */
818             if (!controller->irq)
819                 controller->irq = BUS_ALLOC_RESOURCE(device_get_parent(dev), 
820                                                      dev, SYS_RES_IRQ,
821                                                      rid, 0, ~0, 1, flags);
822             controller->irqcnt++;
823             return controller->irq;
824         }
825     }
826     return 0;
827 }
828
829 static int
830 ata_pci_release_resource(device_t dev, device_t child, int type, int rid,
831                          struct resource *r)
832 {
833     struct ata_pci_controller *controller = device_get_softc(dev);
834     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
835
836     if (type == SYS_RES_IOPORT) {
837         switch (rid) {
838         case ATA_IOADDR_RID:
839             if (ATA_MASTERDEV(dev))
840                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
841                                             SYS_RES_IOPORT, 0x0, r);
842             else
843                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
844                                             SYS_RES_IOPORT, 0x10 + 8 * unit, r);
845             break;
846
847         case ATA_ALTADDR_RID:
848             if (ATA_MASTERDEV(dev))
849                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
850                                             SYS_RES_IOPORT, 0x0, r);
851             else
852                 return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
853                                             SYS_RES_IOPORT, 0x14 + 8 * unit, r);
854             break;
855
856         case ATA_BMADDR_RID:
857             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
858                                         SYS_RES_IOPORT, 0x20, r);
859         default:
860             return ENOENT;
861         }
862     }
863     if (type == SYS_RES_IRQ) {
864         if (rid != ATA_IRQ_RID)
865             return ENOENT;
866
867         if (ATA_MASTERDEV(dev)) {
868 #ifdef __alpha__
869             return alpha_platform_release_ide_intr(unit, r);
870 #else
871             return BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
872                                         SYS_RES_IRQ, rid, r);
873 #endif
874         }
875         else {
876             /* primary and secondary channels share interrupt, keep track */
877             if (--controller->irqcnt)
878                 return 0;
879             controller->irq = NULL;
880             return BUS_RELEASE_RESOURCE(device_get_parent(dev), dev,
881                                         SYS_RES_IRQ, rid, r);
882         }
883     }
884     return EINVAL;
885 }
886
887 static int
888 ata_pci_setup_intr(device_t dev, device_t child, struct resource *irq, 
889                    int flags, driver_intr_t *intr, void *arg,
890                    void **cookiep)
891 {
892     if (ATA_MASTERDEV(dev)) {
893 #ifdef __alpha__
894         return alpha_platform_setup_ide_intr(irq, intr, arg, cookiep);
895 #else
896         return BUS_SETUP_INTR(device_get_parent(dev), child, irq,
897                               flags, intr, arg, cookiep);
898 #endif
899     }
900     else
901         return BUS_SETUP_INTR(device_get_parent(dev), dev, irq,
902                               flags, intr, arg, cookiep);
903 }
904
905 static int
906 ata_pci_teardown_intr(device_t dev, device_t child, struct resource *irq,
907                       void *cookie)
908 {
909     if (ATA_MASTERDEV(dev)) {
910 #ifdef __alpha__
911         return alpha_platform_teardown_ide_intr(irq, cookie);
912 #else
913         return BUS_TEARDOWN_INTR(device_get_parent(dev), child, irq, cookie);
914 #endif
915     }
916     else
917         return BUS_TEARDOWN_INTR(device_get_parent(dev), dev, irq, cookie);
918 }
919
920 static device_method_t ata_pci_methods[] = {
921     /* device interface */
922     DEVMETHOD(device_probe,             ata_pci_probe),
923     DEVMETHOD(device_attach,            ata_pci_attach),
924     DEVMETHOD(device_shutdown,          bus_generic_shutdown),
925     DEVMETHOD(device_suspend,           bus_generic_suspend),
926     DEVMETHOD(device_resume,            bus_generic_resume),
927
928     /* bus methods */
929     DEVMETHOD(bus_print_child,          ata_pci_print_child),
930     DEVMETHOD(bus_alloc_resource,       ata_pci_alloc_resource),
931     DEVMETHOD(bus_release_resource,     ata_pci_release_resource),
932     DEVMETHOD(bus_activate_resource,    bus_generic_activate_resource),
933     DEVMETHOD(bus_deactivate_resource,  bus_generic_deactivate_resource),
934     DEVMETHOD(bus_setup_intr,           ata_pci_setup_intr),
935     DEVMETHOD(bus_teardown_intr,        ata_pci_teardown_intr),
936     { 0, 0 }
937 };
938
939 static driver_t ata_pci_driver = {
940     "atapci",
941     ata_pci_methods,
942     sizeof(struct ata_pci_controller),
943 };
944
945 static devclass_t ata_pci_devclass;
946
947 DRIVER_MODULE(atapci, pci, ata_pci_driver, ata_pci_devclass, 0, 0);
948
949 static int
950 ata_pcisub_probe(device_t dev)
951 {
952     struct ata_channel *ch = device_get_softc(dev);
953     device_t *children;
954     int count, i;
955
956     /* find channel number on this controller */
957     device_get_children(device_get_parent(dev), &children, &count);
958     for (i = 0; i < count; i++) {
959         if (children[i] == dev)
960             ch->unit = i;
961     }
962     free(children, M_TEMP);
963     ch->chiptype = pci_get_devid(device_get_parent(dev));
964     ch->intr_func = ata_pci_intr;
965     return ata_probe(dev);
966 }
967
968 static device_method_t ata_pcisub_methods[] = {
969     /* device interface */
970     DEVMETHOD(device_probe,     ata_pcisub_probe),
971     DEVMETHOD(device_attach,    ata_attach),
972     DEVMETHOD(device_detach,    ata_detach),
973     DEVMETHOD(device_resume,    ata_resume),
974     { 0, 0 }
975 };
976
977 static driver_t ata_pcisub_driver = {
978     "ata",
979     ata_pcisub_methods,
980     sizeof(struct ata_channel),
981 };
982
983 DRIVER_MODULE(ata, atapci, ata_pcisub_driver, ata_devclass, 0, 0);