This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / ide / setup-pci.c
1 /*
2  *  linux/drivers/ide/setup-pci.c               Version 1.10    2002/08/19
3  *
4  *  Copyright (c) 1998-2000  Andre Hedrick <andre@linux-ide.org>
5  *
6  *  Copyright (c) 1995-1998  Mark Lord
7  *  May be copied or modified under the terms of the GNU General Public License
8  *
9  *  Recent Changes
10  *      Split the set up function into multiple functions
11  *      Use pci_set_master
12  *      Fix misreporting of I/O v MMIO problems
13  *      Initial fixups for simplex devices
14  */
15
16 /*
17  *  This module provides support for automatic detection and
18  *  configuration of all PCI IDE interfaces present in a system.  
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/timer.h>
28 #include <linux/mm.h>
29 #include <linux/interrupt.h>
30 #include <linux/ide.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34
35
36 /**
37  *      ide_match_hwif  -       match a PCI IDE against an ide_hwif
38  *      @io_base: I/O base of device
39  *      @bootable: set if its bootable
40  *      @name: name of device
41  *
42  *      Match a PCI IDE port against an entry in ide_hwifs[],
43  *      based on io_base port if possible. Return the matching hwif,
44  *      or a new hwif. If we find an error (clashing, out of devices, etc)
45  *      return NULL
46  *
47  *      FIXME: we need to handle mmio matches here too
48  */
49
50 static ide_hwif_t *ide_match_hwif(unsigned long io_base, u8 bootable, const char *name)
51 {
52         int h;
53         ide_hwif_t *hwif;
54
55         /*
56          * Look for a hwif with matching io_base specified using
57          * parameters to ide_setup().
58          */
59         for (h = 0; h < MAX_HWIFS; ++h) {
60                 hwif = &ide_hwifs[h];
61                 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
62                         if (hwif->chipset == ide_forced)
63                                 return hwif; /* a perfect match */
64                 }
65         }
66         /*
67          * Look for a hwif with matching io_base default value.
68          * If chipset is "ide_unknown", then claim that hwif slot.
69          * Otherwise, some other chipset has already claimed it..  :(
70          */
71         for (h = 0; h < MAX_HWIFS; ++h) {
72                 hwif = &ide_hwifs[h];
73                 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
74                         if (hwif->chipset == ide_unknown)
75                                 return hwif; /* match */
76                         printk(KERN_ERR "%s: port 0x%04lx already claimed by %s\n",
77                                 name, io_base, hwif->name);
78                         return NULL;    /* already claimed */
79                 }
80         }
81         /*
82          * Okay, there is no hwif matching our io_base,
83          * so we'll just claim an unassigned slot.
84          * Give preference to claiming other slots before claiming ide0/ide1,
85          * just in case there's another interface yet-to-be-scanned
86          * which uses ports 1f0/170 (the ide0/ide1 defaults).
87          *
88          * Unless there is a bootable card that does not use the standard
89          * ports 1f0/170 (the ide0/ide1 defaults). The (bootable) flag.
90          */
91         if (bootable) {
92                 for (h = 0; h < MAX_HWIFS; ++h) {
93                         hwif = &ide_hwifs[h];
94                         if (hwif->chipset == ide_unknown)
95                                 return hwif;    /* pick an unused entry */
96                 }
97         } else {
98                 for (h = 2; h < MAX_HWIFS; ++h) {
99                         hwif = ide_hwifs + h;
100                         if (hwif->chipset == ide_unknown)
101                                 return hwif;    /* pick an unused entry */
102                 }
103         }
104         for (h = 0; h < 2; ++h) {
105                 hwif = ide_hwifs + h;
106                 if (hwif->chipset == ide_unknown)
107                         return hwif;    /* pick an unused entry */
108         }
109         printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n", name);
110         return NULL;
111 }
112
113 /**
114  *      ide_setup_pci_baseregs  -       place a PCI IDE controller native
115  *      @dev: PCI device of interface to switch native
116  *      @name: Name of interface
117  *
118  *      We attempt to place the PCI interface into PCI native mode. If
119  *      we succeed the BARs are ok and the controller is in PCI mode.
120  *      Returns 0 on success or an errno code. 
121  *
122  *      FIXME: if we program the interface and then fail to set the BARS
123  *      we don't switch it back to legacy mode. Do we actually care ??
124  */
125  
126 static int ide_setup_pci_baseregs (struct pci_dev *dev, const char *name)
127 {
128         u8 progif = 0;
129
130         /*
131          * Place both IDE interfaces into PCI "native" mode:
132          */
133         if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
134                          (progif & 5) != 5) {
135                 if ((progif & 0xa) != 0xa) {
136                         printk(KERN_INFO "%s: device not capable of full "
137                                 "native PCI mode\n", name);
138                         return -EOPNOTSUPP;
139                 }
140                 printk("%s: placing both ports into native PCI mode\n", name);
141                 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
142                 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
143                     (progif & 5) != 5) {
144                         printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted "
145                                 "0x%04x, got 0x%04x\n",
146                                 name, progif|5, progif);
147                         return -EOPNOTSUPP;
148                 }
149         }
150         return 0;
151 }
152
153 #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
154
155 #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED
156 /*
157  * Long lost data from 2.0.34 that is now in 2.0.39
158  *
159  * This was used in ./drivers/block/triton.c to do DMA Base address setup
160  * when PnP failed.  Oh the things we forget.  I believe this was part
161  * of SFF-8038i that has been withdrawn from public access... :-((
162  */
163 #define DEFAULT_BMIBA   0xe800  /* in case BIOS did not init it */
164 #define DEFAULT_BMCRBA  0xcc00  /* VIA's default value */
165 #define DEFAULT_BMALIBA 0xd400  /* ALI's default value */
166 #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */
167
168 /**
169  *      ide_get_or_set_dma_base         -       setup BMIBA
170  *      @hwif: Interface
171  *
172  *      Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space:
173  *      If need be we set up the DMA base. Where a device has a partner that
174  *      is already in DMA mode we check and enforce IDE simplex rules.
175  */
176
177 static unsigned long ide_get_or_set_dma_base (ide_hwif_t *hwif)
178 {
179         unsigned long   dma_base = 0;
180         struct pci_dev  *dev = hwif->pci_dev;
181
182 #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED
183         int second_chance = 0;
184
185 second_chance_to_dma:
186 #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */
187
188         if ((hwif->mmio) && (hwif->dma_base))
189                 return hwif->dma_base;
190
191         if (hwif->mate && hwif->mate->dma_base) {
192                 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
193         } else {
194                 dma_base = (hwif->mmio) ?
195                         ((unsigned long) hwif->hwif_data) :
196                         (pci_resource_start(dev, 4));
197                 if (!dma_base) {
198                         printk(KERN_ERR "%s: dma_base is invalid (0x%04lx)\n",
199                                 hwif->cds->name, dma_base);
200                         dma_base = 0;
201                 }
202         }
203
204 #ifdef CONFIG_BLK_DEV_IDEDMA_FORCED
205         /* FIXME - should use pci_assign_resource surely */
206         if ((!dma_base) && (!second_chance)) {
207                 unsigned long set_bmiba = 0;
208                 second_chance++;
209                 switch(dev->vendor) {
210                         case PCI_VENDOR_ID_AL:
211                                 set_bmiba = DEFAULT_BMALIBA; break;
212                         case PCI_VENDOR_ID_VIA:
213                                 set_bmiba = DEFAULT_BMCRBA; break;
214                         case PCI_VENDOR_ID_INTEL:
215                                 set_bmiba = DEFAULT_BMIBA; break;
216                         default:
217                                 return dma_base;
218                 }
219                 pci_write_config_dword(dev, 0x20, set_bmiba|1);
220                 goto second_chance_to_dma;
221         }
222 #endif /* CONFIG_BLK_DEV_IDEDMA_FORCED */
223
224         if (dma_base) {
225                 u8 simplex_stat = 0;
226                 dma_base += hwif->channel ? 8 : 0;
227
228                 switch(dev->device) {
229                         case PCI_DEVICE_ID_AL_M5219:
230                         case PCI_DEVICE_ID_AL_M5229:
231                         case PCI_DEVICE_ID_AMD_VIPER_7409:
232                         case PCI_DEVICE_ID_CMD_643:
233                         case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE:
234                                 simplex_stat = hwif->INB(dma_base + 2);
235                                 hwif->OUTB((simplex_stat&0x60),(dma_base + 2));
236                                 simplex_stat = hwif->INB(dma_base + 2);
237                                 if (simplex_stat & 0x80) {
238                                         printk(KERN_INFO "%s: simplex device: "
239                                                 "DMA forced\n",
240                                                 hwif->cds->name);
241                                 }
242                                 break;
243                         default:
244                                 /*
245                                  * If the device claims "simplex" DMA,
246                                  * this means only one of the two interfaces
247                                  * can be trusted with DMA at any point in time.
248                                  * So we should enable DMA only on one of the
249                                  * two interfaces.
250                                  */
251                                 simplex_stat = hwif->INB(dma_base + 2);
252                                 if (simplex_stat & 0x80) {
253                                         /* simplex device? */
254 #if 0                                   
255 /*
256  *      At this point we haven't probed the drives so we can't make the
257  *      appropriate decision. Really we should defer this problem
258  *      until we tune the drive then try to grab DMA ownership if we want
259  *      to be the DMA end. This has to be become dynamic to handle hot
260  *      plug.
261  */
262                                         /* Don't enable DMA on a simplex channel with no drives */
263                                         if (!hwif->drives[0].present && !hwif->drives[1].present)
264                                         {
265                                                 printk(KERN_INFO "%s: simplex device with no drives: DMA disabled\n",
266                                                                 hwif->cds->name);
267                                                 dma_base = 0;
268                                         }
269                                         /* If our other channel has DMA then we cannot */
270                                         else 
271 #endif                                  
272                                         if(hwif->mate && hwif->mate->dma_base) 
273                                         {
274                                                 printk(KERN_INFO "%s: simplex device: "
275                                                         "DMA disabled\n",
276                                                         hwif->cds->name);
277                                                 dma_base = 0;
278                                         }
279                                 }
280                 }
281         }
282         return dma_base;
283 }
284 #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
285
286 void ide_setup_pci_noise (struct pci_dev *dev, ide_pci_device_t *d)
287 {
288         if ((d->vendor != dev->vendor) && (d->device != dev->device)) {
289                 printk(KERN_INFO "%s: unknown IDE controller at PCI slot "
290                         "%s, VID=%04x, DID=%04x\n",
291                         d->name, pci_name(dev), dev->vendor, dev->device);
292         } else {
293                 printk(KERN_INFO "%s: IDE controller at PCI slot %s\n",
294                         d->name, pci_name(dev));
295         }
296 }
297
298 EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
299
300
301 /**
302  *      ide_pci_enable  -       do PCI enables
303  *      @dev: PCI device
304  *      @d: IDE pci device data
305  *
306  *      Enable the IDE PCI device. We attempt to enable the device in full
307  *      but if that fails then we only need BAR4 so we will enable that.
308  *      
309  *      Returns zero on success or an error code
310  */
311  
312 static int ide_pci_enable(struct pci_dev *dev, ide_pci_device_t *d)
313 {
314         
315         if (pci_enable_device(dev)) {
316                 if (pci_enable_device_bars(dev, 1 << 4)) {
317                         printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
318                                 "Could not enable device.\n", d->name);
319                         return -EBUSY;
320                 } else
321                         printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
322         }
323
324         /*
325          * assume all devices can do 32-bit dma for now. we can add a
326          * dma mask field to the ide_pci_device_t if we need it (or let
327          * lower level driver set the dma mask)
328          */
329         if (pci_set_dma_mask(dev, 0xffffffff)) {
330                 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
331                 return -EBUSY;
332         }
333          
334         /* FIXME: Temporary - until we put in the hotplug interface logic
335            Check that the bits we want are not in use by someone else */
336         if (pci_request_region(dev, 4, "ide_tmp"))
337                 return -EBUSY;
338         pci_release_region(dev, 4);
339         
340         return 0;       
341 }
342
343 /**
344  *      ide_pci_configure       -       configure an unconfigured device
345  *      @dev: PCI device
346  *      @d: IDE pci device data
347  *
348  *      Enable and configure the PCI device we have been passed.
349  *      Returns zero on success or an error code.
350  */
351  
352 static int ide_pci_configure(struct pci_dev *dev, ide_pci_device_t *d)
353 {
354         u16 pcicmd = 0;
355         /*
356          * PnP BIOS was *supposed* to have setup this device, but we
357          * can do it ourselves, so long as the BIOS has assigned an IRQ
358          * (or possibly the device is using a "legacy header" for IRQs).
359          * Maybe the user deliberately *disabled* the device,
360          * but we'll eventually ignore it again if no drives respond.
361          */
362         if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO)) 
363         {
364                 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
365                 return -ENODEV;
366         }
367         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
368                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
369                 return -EIO;
370         }
371         if (!(pcicmd & PCI_COMMAND_IO)) {
372                 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
373                 return -ENXIO;
374         }
375         return 0;
376 }
377
378 /**
379  *      ide_pci_check_iomem     -       check a register is I/O
380  *      @dev: pci device
381  *      @d: ide_pci_device
382  *      @bar: bar number
383  *
384  *      Checks if a BAR is configured and points to MMIO space. If so
385  *      print an error and return an error code. Otherwise return 0
386  */
387  
388 static int ide_pci_check_iomem(struct pci_dev *dev, ide_pci_device_t *d, int bar)
389 {
390         ulong flags = pci_resource_flags(dev, bar);
391         
392         /* Unconfigured ? */
393         if (!flags || pci_resource_len(dev, bar) == 0)
394                 return 0;
395
396         /* I/O space */         
397         if(flags & PCI_BASE_ADDRESS_IO_MASK)
398                 return 0;
399                 
400         /* Bad */
401         printk(KERN_ERR "%s: IO baseregs (BIOS) are reported "
402                         "as MEM, report to "
403                         "<andre@linux-ide.org>.\n", d->name);
404         return -EINVAL;
405 }
406
407 /**
408  *      ide_hwif_configure      -       configure an IDE interface
409  *      @dev: PCI device holding interface
410  *      @d: IDE pci data
411  *      @mate: Paired interface if any
412  *
413  *      Perform the initial set up for the hardware interface structure. This
414  *      is done per interface port rather than per PCI device. There may be
415  *      more than one port per device.
416  *
417  *      Returns the new hardware interface structure, or NULL on a failure
418  */
419  
420 static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *mate, int port, int irq)
421 {
422         unsigned long ctl = 0, base = 0;
423         ide_hwif_t *hwif;
424
425         if(!d->isa_ports)
426         {
427                 /*  Possibly we should fail if these checks report true */
428                 ide_pci_check_iomem(dev, d, 2*port);
429                 ide_pci_check_iomem(dev, d, 2*port+1);
430  
431                 ctl  = pci_resource_start(dev, 2*port+1);
432                 base = pci_resource_start(dev, 2*port);
433                 if ((ctl && !base) || (base && !ctl)) {
434                         printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
435                                 "for port %d, skipping\n", d->name, port);
436                         return NULL;
437                 }
438         }
439         if (!ctl)
440         {
441                 /* Use default values */
442                 ctl = port ? 0x374 : 0x3f4;
443                 base = port ? 0x170 : 0x1f0;
444         }
445         if ((hwif = ide_match_hwif(base, d->bootable, d->name)) == NULL)
446                 return NULL;    /* no room in ide_hwifs[] */
447         if (hwif->io_ports[IDE_DATA_OFFSET] != base ||
448             hwif->io_ports[IDE_CONTROL_OFFSET] != (ctl | 2)) {
449                 memset(&hwif->hw, 0, sizeof(hwif->hw));
450                 ide_init_hwif_ports(&hwif->hw, base, (ctl | 2), NULL);
451                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
452                 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
453         }
454         hwif->chipset = ide_pci;
455         hwif->pci_dev = dev;
456         hwif->cds = (struct ide_pci_device_s *) d;
457         hwif->channel = port;
458
459         if (!hwif->irq)
460                 hwif->irq = irq;
461         if (mate) {
462                 hwif->mate = mate;
463                 mate->mate = hwif;
464         }
465         return hwif;
466 }
467
468 /**
469  *      ide_hwif_setup_dma      -       configure DMA interface
470  *      @dev: PCI device
471  *      @d: IDE pci data
472  *      @hwif: Hardware interface we are configuring
473  *
474  *      Set up the DMA base for the interface. Enable the master bits as
475  *      necessary and attempt to bring the device DMA into a ready to use
476  *      state
477  */
478  
479 #ifndef CONFIG_BLK_DEV_IDEDMA_PCI
480 static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
481 {
482 }
483 #else
484 static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
485 {
486         u16 pcicmd;
487         pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
488
489         if ((d->autodma == AUTODMA) ||
490             ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
491              (dev->class & 0x80))) {
492                 unsigned long dma_base = ide_get_or_set_dma_base(hwif);
493                 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
494                         /*
495                          * Set up BM-DMA capability
496                          * (PnP BIOS should have done this)
497                          */
498                         if (!((d->device == PCI_DEVICE_ID_CYRIX_5530_IDE && d->vendor == PCI_VENDOR_ID_CYRIX)
499                             ||(d->device == PCI_DEVICE_ID_NS_SCx200_IDE && d->vendor == PCI_VENDOR_ID_NS)))
500                         {
501                                 /*
502                                  * default DMA off if we had to
503                                  * configure it here
504                                  */
505                                 hwif->autodma = 0;
506                         }
507                         pci_set_master(dev);
508                         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
509                                 printk(KERN_ERR "%s: %s error updating PCICMD\n",
510                                         hwif->name, d->name);
511                                 dma_base = 0;
512                         }
513                 }
514                 if (dma_base) {
515                         if (d->init_dma) {
516                                 d->init_dma(hwif, dma_base);
517                         } else {
518                                 ide_setup_dma(hwif, dma_base, 8);
519                         }
520                 } else {
521                         printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
522                                 "(BIOS)\n", hwif->name, d->name);
523                 }
524         }
525 }
526 #endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
527
528 /**
529  *      ide_setup_pci_controller        -       set up IDE PCI
530  *      @dev: PCI device
531  *      @d: IDE PCI data
532  *      @noisy: verbose flag
533  *      @config: returned as 1 if we configured the hardware
534  *
535  *      Set up the PCI and controller side of the IDE interface. This brings
536  *      up the PCI side of the device, checks that the device is enabled
537  *      and enables it if need be
538  */
539  
540 static int ide_setup_pci_controller(struct pci_dev *dev, ide_pci_device_t *d, int noisy, int *config)
541 {
542         int ret = 0;
543         u32 class_rev;
544         u16 pcicmd;
545
546         if (!noautodma)
547                 ret = 1;
548
549         if (noisy)
550                 ide_setup_pci_noise(dev, d);
551
552         if (ide_pci_enable(dev, d))
553                 return -EBUSY;
554                 
555         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
556                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
557                 return -EIO;
558         }
559         if (!(pcicmd & PCI_COMMAND_IO)) {       /* is device disabled? */
560                 if (ide_pci_configure(dev, d))
561                         return -ENODEV;
562                 /* default DMA off if we had to configure it here */
563                 ret = 0;
564                 *config = 1;
565                 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
566         }
567
568         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
569         class_rev &= 0xff;
570         if (noisy)
571                 printk(KERN_INFO "%s: chipset revision %d\n", d->name, class_rev);
572         return ret;
573 }
574
575 /**
576  *      ide_pci_setup_ports     -       configure ports/devices on PCI IDE
577  *      @dev: PCI device
578  *      @d: IDE pci device info
579  *      @autodma: Should we enable DMA
580  *      @pciirq: IRQ line
581  *      @index: ata index to update
582  *
583  *      Scan the interfaces attached to this device and do any
584  *      necessary per port setup. Attach the devices and ask the
585  *      generic DMA layer to do its work for us.
586  *
587  *      Normally called automaticall from do_ide_pci_setup_device,
588  *      but is also used directly as a helper function by some controllers
589  *      where the chipset setup is not the default PCI IDE one.
590  */
591  
592 void ide_pci_setup_ports(struct pci_dev *dev, ide_pci_device_t *d, int autodma, int pciirq, ata_index_t *index)
593 {
594         int port;
595         int at_least_one_hwif_enabled = 0;
596         ide_hwif_t *hwif, *mate = NULL;
597         static int secondpdc = 0;
598         u8 tmp;
599
600         index->all = 0xf0f0;
601
602         /*
603          * Set up the IDE ports
604          */
605          
606         for (port = 0; port <= 1; ++port) {
607                 ide_pci_enablebit_t *e = &(d->enablebits[port]);
608         
609                 /* 
610                  * If this is a Promise FakeRaid controller,
611                  * the 2nd controller will be marked as 
612                  * disabled while it is actually there and enabled
613                  * by the bios for raid purposes. 
614                  * Skip the normal "is it enabled" test for those.
615                  */
616                 if (((d->vendor == PCI_VENDOR_ID_PROMISE) &&
617                      ((d->device == PCI_DEVICE_ID_PROMISE_20262) ||
618                       (d->device == PCI_DEVICE_ID_PROMISE_20265))) &&
619                     (secondpdc++==1) && (port==1))
620                         goto controller_ok;
621                         
622                 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
623                     (tmp & e->mask) != e->val))
624                         continue;       /* port not enabled */
625 controller_ok:
626
627                 if (d->channels <= port)
628                         break;
629         
630                 if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
631                         continue;
632
633                 /* setup proper ancestral information */
634                 hwif->gendev.parent = &dev->dev;
635
636                 if (hwif->channel) {
637                         index->b.high = hwif->index;
638                 } else {
639                         index->b.low = hwif->index;
640                 }
641
642                 
643                 if (d->init_iops)
644                         d->init_iops(hwif);
645
646                 if (d->autodma == NODMA)
647                         goto bypass_legacy_dma;
648                 if (d->autodma == NOAUTODMA)
649                         autodma = 0;
650                 if (autodma)
651                         hwif->autodma = 1;
652                         
653                 if(d->init_setup_dma)
654                         d->init_setup_dma(dev, d, hwif);
655                 else
656                         ide_hwif_setup_dma(dev, d, hwif);
657 bypass_legacy_dma:
658                 if (d->init_hwif)
659                         /* Call chipset-specific routine
660                          * for each enabled hwif
661                          */
662                         d->init_hwif(hwif);
663
664                 mate = hwif;
665                 at_least_one_hwif_enabled = 1;
666         }
667         if (!at_least_one_hwif_enabled)
668                 printk(KERN_INFO "%s: neither IDE port enabled (BIOS)\n", d->name);
669 }
670
671 EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
672
673 /*
674  * ide_setup_pci_device() looks at the primary/secondary interfaces
675  * on a PCI IDE device and, if they are enabled, prepares the IDE driver
676  * for use with them.  This generic code works for most PCI chipsets.
677  *
678  * One thing that is not standardized is the location of the
679  * primary/secondary interface "enable/disable" bits.  For chipsets that
680  * we "know" about, this information is in the ide_pci_device_t struct;
681  * for all other chipsets, we just assume both interfaces are enabled.
682  */
683 static ata_index_t do_ide_setup_pci_device (struct pci_dev *dev, ide_pci_device_t *d, u8 noisy)
684 {
685         int autodma = 0;
686         int pciirq = 0;
687         int tried_config = 0;
688         ata_index_t index = { .b = { .low = 0xff, .high = 0xff } };
689
690         if((autodma = ide_setup_pci_controller(dev, d, noisy, &tried_config)) < 0)
691                 return index;
692
693         /*
694          * Can we trust the reported IRQ?
695          */
696         pciirq = dev->irq;
697
698         /* Is it an "IDE storage" device in non-PCI mode? */
699         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
700                 if (noisy)
701                         printk(KERN_INFO "%s: not 100%% native mode: "
702                                 "will probe irqs later\n", d->name);
703                 /*
704                  * This allows offboard ide-pci cards the enable a BIOS,
705                  * verify interrupt settings of split-mirror pci-config
706                  * space, place chipset into init-mode, and/or preserve
707                  * an interrupt if the card is not native ide support.
708                  */
709                 pciirq = (d->init_chipset) ? d->init_chipset(dev, d->name) : 0;
710         } else if (tried_config) {
711                 if (noisy)
712                         printk(KERN_INFO "%s: will probe irqs later\n", d->name);
713                 pciirq = 0;
714         } else if (!pciirq) {
715                 if (noisy)
716                         printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
717                                 d->name, pciirq);
718                 pciirq = 0;
719         } else {
720                 if (d->init_chipset)
721                 {
722                         if(d->init_chipset(dev, d->name) < 0)
723                                 return index;
724                 }
725                 if (noisy)
726 #ifdef __sparc__
727                         printk(KERN_INFO "%s: 100%% native mode on irq %s\n",
728                                d->name, __irq_itoa(pciirq));
729 #else
730                         printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
731                                 d->name, pciirq);
732 #endif
733         }
734         
735         if(pciirq < 0)          /* Error not an IRQ */
736                 return index;
737
738         ide_pci_setup_ports(dev, d, autodma, pciirq, &index);
739
740         return index;
741 }
742
743 void ide_setup_pci_device (struct pci_dev *dev, ide_pci_device_t *d)
744 {
745         ata_index_t index_list = do_ide_setup_pci_device(dev, d, 1);
746
747         if ((index_list.b.low & 0xf0) != 0xf0)
748                 probe_hwif_init(&ide_hwifs[index_list.b.low]);
749         if ((index_list.b.high & 0xf0) != 0xf0)
750                 probe_hwif_init(&ide_hwifs[index_list.b.high]);
751
752         create_proc_ide_interfaces();
753 }
754
755 EXPORT_SYMBOL_GPL(ide_setup_pci_device);
756
757 void ide_setup_pci_devices (struct pci_dev *dev, struct pci_dev *dev2, ide_pci_device_t *d)
758 {
759         ata_index_t index_list  = do_ide_setup_pci_device(dev, d, 1);
760         ata_index_t index_list2 = do_ide_setup_pci_device(dev2, d, 0);
761
762         if ((index_list.b.low & 0xf0) != 0xf0)
763                 probe_hwif_init(&ide_hwifs[index_list.b.low]);
764         if ((index_list.b.high & 0xf0) != 0xf0)
765                 probe_hwif_init(&ide_hwifs[index_list.b.high]);
766         if ((index_list2.b.low & 0xf0) != 0xf0)
767                 probe_hwif_init(&ide_hwifs[index_list2.b.low]);
768         if ((index_list2.b.high & 0xf0) != 0xf0)
769                 probe_hwif_init(&ide_hwifs[index_list2.b.high]);
770
771         create_proc_ide_interfaces();
772 }
773
774 EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
775
776 /*
777  *      Module interfaces
778  */
779  
780 static int pre_init = 1;                /* Before first ordered IDE scan */
781 static LIST_HEAD(ide_pci_drivers);
782
783 /*
784  *      ide_register_pci_driver         -       attach IDE driver
785  *      @driver: pci driver
786  *
787  *      Registers a driver with the IDE layer. The IDE layer arranges that
788  *      boot time setup is done in the expected device order and then 
789  *      hands the controllers off to the core PCI code to do the rest of
790  *      the work.
791  *
792  *      The driver_data of the driver table must point to an ide_pci_device_t
793  *      describing the interface.
794  *
795  *      Returns are the same as for pci_register_driver
796  */
797
798 int ide_pci_register_driver(struct pci_driver *driver)
799 {
800         if(!pre_init)
801                 return pci_module_init(driver);
802         list_add_tail(&driver->node, &ide_pci_drivers);
803         return 0;
804 }
805
806 EXPORT_SYMBOL_GPL(ide_pci_register_driver);
807
808 /**
809  *      ide_unregister_pci_driver       -       unregister an IDE driver
810  *      @driver: driver to remove
811  *
812  *      Unregister a currently installed IDE driver. Returns are the same
813  *      as for pci_unregister_driver
814  */
815  
816 void ide_pci_unregister_driver(struct pci_driver *driver)
817 {
818         if(!pre_init)
819                 pci_unregister_driver(driver);
820         else
821                 list_del(&driver->node);
822 }
823
824 EXPORT_SYMBOL_GPL(ide_pci_unregister_driver);
825
826 /**
827  *      ide_scan_pcidev         -       find an IDE driver for a device
828  *      @dev: PCI device to check
829  *
830  *      Look for an IDE driver to handle the device we are considering.
831  *      This is only used during boot up to get the ordering correct. After
832  *      boot up the pci layer takes over the job.
833  */
834  
835 static int __init ide_scan_pcidev(struct pci_dev *dev)
836 {
837         struct list_head *l;
838         struct pci_driver *d;
839         
840         list_for_each(l, &ide_pci_drivers)
841         {
842                 d = list_entry(l, struct pci_driver, node);
843                 if(d->id_table)
844                 {
845                         const struct pci_device_id *id = pci_match_device(d->id_table, dev);
846                         if(id != NULL)
847                         {
848                                 if(d->probe(dev, id) >= 0)
849                                 {
850                                         dev->driver = d;
851                                         return 1;
852                                 }
853                         }
854                 }
855         }
856         return 0;
857 }
858
859 /**
860  *      ide_scan_pcibus         -       perform the initial IDE driver scan
861  *      @scan_direction: set for reverse order scanning
862  *
863  *      Perform the initial bus rather than driver ordered scan of the
864  *      PCI drivers. After this all IDE pci handling becomes standard
865  *      module ordering not traditionally ordered.
866  */
867         
868 void __init ide_scan_pcibus (int scan_direction)
869 {
870         struct pci_dev *dev = NULL;
871         struct pci_driver *d;
872         struct list_head *l, *n;
873
874         pre_init = 0;
875         if (!scan_direction) {
876                 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
877                         ide_scan_pcidev(dev);
878                 }
879         } else {
880                 while ((dev = pci_find_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
881                         ide_scan_pcidev(dev);
882                 }
883         }
884         
885         /*
886          *      Hand the drivers over to the PCI layer now we
887          *      are post init.
888          */
889
890         list_for_each_safe(l, n, &ide_pci_drivers)
891         {
892                 list_del(l);
893                 d = list_entry(l, struct pci_driver, node);
894                 pci_register_driver(d);
895         }
896 }