patch-2_6_7-vs1_9_1_12
[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         printk(KERN_INFO "%s: IDE controller at PCI slot %s\n",
289                          d->name, pci_name(dev));
290 }
291
292 EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
293
294
295 /**
296  *      ide_pci_enable  -       do PCI enables
297  *      @dev: PCI device
298  *      @d: IDE pci device data
299  *
300  *      Enable the IDE PCI device. We attempt to enable the device in full
301  *      but if that fails then we only need BAR4 so we will enable that.
302  *      
303  *      Returns zero on success or an error code
304  */
305  
306 static int ide_pci_enable(struct pci_dev *dev, ide_pci_device_t *d)
307 {
308         
309         if (pci_enable_device(dev)) {
310                 if (pci_enable_device_bars(dev, 1 << 4)) {
311                         printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
312                                 "Could not enable device.\n", d->name);
313                         return -EBUSY;
314                 } else
315                         printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
316         }
317
318         /*
319          * assume all devices can do 32-bit dma for now. we can add a
320          * dma mask field to the ide_pci_device_t if we need it (or let
321          * lower level driver set the dma mask)
322          */
323         if (pci_set_dma_mask(dev, 0xffffffff)) {
324                 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
325                 return -EBUSY;
326         }
327          
328         /* FIXME: Temporary - until we put in the hotplug interface logic
329            Check that the bits we want are not in use by someone else */
330         if (pci_request_region(dev, 4, "ide_tmp"))
331                 return -EBUSY;
332         pci_release_region(dev, 4);
333         
334         return 0;       
335 }
336
337 /**
338  *      ide_pci_configure       -       configure an unconfigured device
339  *      @dev: PCI device
340  *      @d: IDE pci device data
341  *
342  *      Enable and configure the PCI device we have been passed.
343  *      Returns zero on success or an error code.
344  */
345  
346 static int ide_pci_configure(struct pci_dev *dev, ide_pci_device_t *d)
347 {
348         u16 pcicmd = 0;
349         /*
350          * PnP BIOS was *supposed* to have setup this device, but we
351          * can do it ourselves, so long as the BIOS has assigned an IRQ
352          * (or possibly the device is using a "legacy header" for IRQs).
353          * Maybe the user deliberately *disabled* the device,
354          * but we'll eventually ignore it again if no drives respond.
355          */
356         if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO)) 
357         {
358                 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
359                 return -ENODEV;
360         }
361         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
362                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
363                 return -EIO;
364         }
365         if (!(pcicmd & PCI_COMMAND_IO)) {
366                 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
367                 return -ENXIO;
368         }
369         return 0;
370 }
371
372 /**
373  *      ide_pci_check_iomem     -       check a register is I/O
374  *      @dev: pci device
375  *      @d: ide_pci_device
376  *      @bar: bar number
377  *
378  *      Checks if a BAR is configured and points to MMIO space. If so
379  *      print an error and return an error code. Otherwise return 0
380  */
381  
382 static int ide_pci_check_iomem(struct pci_dev *dev, ide_pci_device_t *d, int bar)
383 {
384         ulong flags = pci_resource_flags(dev, bar);
385         
386         /* Unconfigured ? */
387         if (!flags || pci_resource_len(dev, bar) == 0)
388                 return 0;
389
390         /* I/O space */         
391         if(flags & PCI_BASE_ADDRESS_IO_MASK)
392                 return 0;
393                 
394         /* Bad */
395         printk(KERN_ERR "%s: IO baseregs (BIOS) are reported "
396                         "as MEM, report to "
397                         "<andre@linux-ide.org>.\n", d->name);
398         return -EINVAL;
399 }
400
401 /**
402  *      ide_hwif_configure      -       configure an IDE interface
403  *      @dev: PCI device holding interface
404  *      @d: IDE pci data
405  *      @mate: Paired interface if any
406  *
407  *      Perform the initial set up for the hardware interface structure. This
408  *      is done per interface port rather than per PCI device. There may be
409  *      more than one port per device.
410  *
411  *      Returns the new hardware interface structure, or NULL on a failure
412  */
413  
414 static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *mate, int port, int irq)
415 {
416         unsigned long ctl = 0, base = 0;
417         ide_hwif_t *hwif;
418
419         if ((d->flags & IDEPCI_FLAG_ISA_PORTS) == 0) {
420                 /*  Possibly we should fail if these checks report true */
421                 ide_pci_check_iomem(dev, d, 2*port);
422                 ide_pci_check_iomem(dev, d, 2*port+1);
423  
424                 ctl  = pci_resource_start(dev, 2*port+1);
425                 base = pci_resource_start(dev, 2*port);
426                 if ((ctl && !base) || (base && !ctl)) {
427                         printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
428                                 "for port %d, skipping\n", d->name, port);
429                         return NULL;
430                 }
431         }
432         if (!ctl)
433         {
434                 /* Use default values */
435                 ctl = port ? 0x374 : 0x3f4;
436                 base = port ? 0x170 : 0x1f0;
437         }
438         if ((hwif = ide_match_hwif(base, d->bootable, d->name)) == NULL)
439                 return NULL;    /* no room in ide_hwifs[] */
440         if (hwif->io_ports[IDE_DATA_OFFSET] != base ||
441             hwif->io_ports[IDE_CONTROL_OFFSET] != (ctl | 2)) {
442                 memset(&hwif->hw, 0, sizeof(hwif->hw));
443                 ide_init_hwif_ports(&hwif->hw, base, (ctl | 2), NULL);
444                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
445                 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
446         }
447         hwif->chipset = ide_pci;
448         hwif->pci_dev = dev;
449         hwif->cds = (struct ide_pci_device_s *) d;
450         hwif->channel = port;
451
452         if (!hwif->irq)
453                 hwif->irq = irq;
454         if (mate) {
455                 hwif->mate = mate;
456                 mate->mate = hwif;
457         }
458         return hwif;
459 }
460
461 /**
462  *      ide_hwif_setup_dma      -       configure DMA interface
463  *      @dev: PCI device
464  *      @d: IDE pci data
465  *      @hwif: Hardware interface we are configuring
466  *
467  *      Set up the DMA base for the interface. Enable the master bits as
468  *      necessary and attempt to bring the device DMA into a ready to use
469  *      state
470  */
471  
472 #ifndef CONFIG_BLK_DEV_IDEDMA_PCI
473 static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
474 {
475 }
476 #else
477 static void ide_hwif_setup_dma(struct pci_dev *dev, ide_pci_device_t *d, ide_hwif_t *hwif)
478 {
479         u16 pcicmd;
480         pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
481
482         if ((d->autodma == AUTODMA) ||
483             ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
484              (dev->class & 0x80))) {
485                 unsigned long dma_base = ide_get_or_set_dma_base(hwif);
486                 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
487                         /*
488                          * Set up BM-DMA capability
489                          * (PnP BIOS should have done this)
490                          */
491                         if ((d->flags & IDEPCI_FLAG_FORCE_MASTER) == 0) {
492                                 /*
493                                  * default DMA off if we had to
494                                  * configure it here
495                                  */
496                                 hwif->autodma = 0;
497                         }
498                         pci_set_master(dev);
499                         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
500                                 printk(KERN_ERR "%s: %s error updating PCICMD\n",
501                                         hwif->name, d->name);
502                                 dma_base = 0;
503                         }
504                 }
505                 if (dma_base) {
506                         if (d->init_dma) {
507                                 d->init_dma(hwif, dma_base);
508                         } else {
509                                 ide_setup_dma(hwif, dma_base, 8);
510                         }
511                 } else {
512                         printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
513                                 "(BIOS)\n", hwif->name, d->name);
514                 }
515         }
516 }
517 #endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
518
519 /**
520  *      ide_setup_pci_controller        -       set up IDE PCI
521  *      @dev: PCI device
522  *      @d: IDE PCI data
523  *      @noisy: verbose flag
524  *      @config: returned as 1 if we configured the hardware
525  *
526  *      Set up the PCI and controller side of the IDE interface. This brings
527  *      up the PCI side of the device, checks that the device is enabled
528  *      and enables it if need be
529  */
530  
531 static int ide_setup_pci_controller(struct pci_dev *dev, ide_pci_device_t *d, int noisy, int *config)
532 {
533         int ret = 0;
534         u32 class_rev;
535         u16 pcicmd;
536
537         if (!noautodma)
538                 ret = 1;
539
540         if (noisy)
541                 ide_setup_pci_noise(dev, d);
542
543         if (ide_pci_enable(dev, d))
544                 return -EBUSY;
545                 
546         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
547                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
548                 return -EIO;
549         }
550         if (!(pcicmd & PCI_COMMAND_IO)) {       /* is device disabled? */
551                 if (ide_pci_configure(dev, d))
552                         return -ENODEV;
553                 /* default DMA off if we had to configure it here */
554                 ret = 0;
555                 *config = 1;
556                 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
557         }
558
559         pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
560         class_rev &= 0xff;
561         if (noisy)
562                 printk(KERN_INFO "%s: chipset revision %d\n", d->name, class_rev);
563         return ret;
564 }
565
566 /**
567  *      ide_pci_setup_ports     -       configure ports/devices on PCI IDE
568  *      @dev: PCI device
569  *      @d: IDE pci device info
570  *      @autodma: Should we enable DMA
571  *      @pciirq: IRQ line
572  *      @index: ata index to update
573  *
574  *      Scan the interfaces attached to this device and do any
575  *      necessary per port setup. Attach the devices and ask the
576  *      generic DMA layer to do its work for us.
577  *
578  *      Normally called automaticall from do_ide_pci_setup_device,
579  *      but is also used directly as a helper function by some controllers
580  *      where the chipset setup is not the default PCI IDE one.
581  */
582  
583 void ide_pci_setup_ports(struct pci_dev *dev, ide_pci_device_t *d, int autodma, int pciirq, ata_index_t *index)
584 {
585         int port;
586         int at_least_one_hwif_enabled = 0;
587         ide_hwif_t *hwif, *mate = NULL;
588         static int secondpdc = 0;
589         u8 tmp;
590
591         index->all = 0xf0f0;
592
593         /*
594          * Set up the IDE ports
595          */
596          
597         for (port = 0; port <= 1; ++port) {
598                 ide_pci_enablebit_t *e = &(d->enablebits[port]);
599         
600                 /* 
601                  * If this is a Promise FakeRaid controller,
602                  * the 2nd controller will be marked as 
603                  * disabled while it is actually there and enabled
604                  * by the bios for raid purposes. 
605                  * Skip the normal "is it enabled" test for those.
606                  */
607                 if ((d->flags & IDEPCI_FLAG_FORCE_PDC) &&
608                     (secondpdc++==1) && (port==1))
609                         goto controller_ok;
610                         
611                 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
612                     (tmp & e->mask) != e->val))
613                         continue;       /* port not enabled */
614 controller_ok:
615
616                 if (d->channels <= port)
617                         break;
618         
619                 if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
620                         continue;
621
622                 /* setup proper ancestral information */
623                 hwif->gendev.parent = &dev->dev;
624
625                 if (hwif->channel) {
626                         index->b.high = hwif->index;
627                 } else {
628                         index->b.low = hwif->index;
629                 }
630
631                 
632                 if (d->init_iops)
633                         d->init_iops(hwif);
634
635                 if (d->autodma == NODMA)
636                         goto bypass_legacy_dma;
637                 if (d->autodma == NOAUTODMA)
638                         autodma = 0;
639                 if (autodma)
640                         hwif->autodma = 1;
641                         
642                 if(d->init_setup_dma)
643                         d->init_setup_dma(dev, d, hwif);
644                 else
645                         ide_hwif_setup_dma(dev, d, hwif);
646 bypass_legacy_dma:
647                 if (d->init_hwif)
648                         /* Call chipset-specific routine
649                          * for each enabled hwif
650                          */
651                         d->init_hwif(hwif);
652
653                 mate = hwif;
654                 at_least_one_hwif_enabled = 1;
655         }
656         if (!at_least_one_hwif_enabled)
657                 printk(KERN_INFO "%s: neither IDE port enabled (BIOS)\n", d->name);
658 }
659
660 EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
661
662 /*
663  * ide_setup_pci_device() looks at the primary/secondary interfaces
664  * on a PCI IDE device and, if they are enabled, prepares the IDE driver
665  * for use with them.  This generic code works for most PCI chipsets.
666  *
667  * One thing that is not standardized is the location of the
668  * primary/secondary interface "enable/disable" bits.  For chipsets that
669  * we "know" about, this information is in the ide_pci_device_t struct;
670  * for all other chipsets, we just assume both interfaces are enabled.
671  */
672 static ata_index_t do_ide_setup_pci_device (struct pci_dev *dev, ide_pci_device_t *d, u8 noisy)
673 {
674         int autodma = 0;
675         int pciirq = 0;
676         int tried_config = 0;
677         ata_index_t index = { .b = { .low = 0xff, .high = 0xff } };
678
679         if((autodma = ide_setup_pci_controller(dev, d, noisy, &tried_config)) < 0)
680                 return index;
681
682         /*
683          * Can we trust the reported IRQ?
684          */
685         pciirq = dev->irq;
686
687         /* Is it an "IDE storage" device in non-PCI mode? */
688         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
689                 if (noisy)
690                         printk(KERN_INFO "%s: not 100%% native mode: "
691                                 "will probe irqs later\n", d->name);
692                 /*
693                  * This allows offboard ide-pci cards the enable a BIOS,
694                  * verify interrupt settings of split-mirror pci-config
695                  * space, place chipset into init-mode, and/or preserve
696                  * an interrupt if the card is not native ide support.
697                  */
698                 pciirq = (d->init_chipset) ? d->init_chipset(dev, d->name) : 0;
699         } else if (tried_config) {
700                 if (noisy)
701                         printk(KERN_INFO "%s: will probe irqs later\n", d->name);
702                 pciirq = 0;
703         } else if (!pciirq) {
704                 if (noisy)
705                         printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
706                                 d->name, pciirq);
707                 pciirq = 0;
708         } else {
709                 if (d->init_chipset)
710                 {
711                         if(d->init_chipset(dev, d->name) < 0)
712                                 return index;
713                 }
714                 if (noisy)
715 #ifdef __sparc__
716                         printk(KERN_INFO "%s: 100%% native mode on irq %s\n",
717                                d->name, __irq_itoa(pciirq));
718 #else
719                         printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
720                                 d->name, pciirq);
721 #endif
722         }
723         
724         if(pciirq < 0)          /* Error not an IRQ */
725                 return index;
726
727         ide_pci_setup_ports(dev, d, autodma, pciirq, &index);
728
729         return index;
730 }
731
732 void ide_setup_pci_device (struct pci_dev *dev, ide_pci_device_t *d)
733 {
734         ata_index_t index_list = do_ide_setup_pci_device(dev, d, 1);
735
736         if ((index_list.b.low & 0xf0) != 0xf0)
737                 probe_hwif_init(&ide_hwifs[index_list.b.low]);
738         if ((index_list.b.high & 0xf0) != 0xf0)
739                 probe_hwif_init(&ide_hwifs[index_list.b.high]);
740
741         create_proc_ide_interfaces();
742 }
743
744 EXPORT_SYMBOL_GPL(ide_setup_pci_device);
745
746 void ide_setup_pci_devices (struct pci_dev *dev, struct pci_dev *dev2, ide_pci_device_t *d)
747 {
748         ata_index_t index_list  = do_ide_setup_pci_device(dev, d, 1);
749         ata_index_t index_list2 = do_ide_setup_pci_device(dev2, d, 0);
750
751         if ((index_list.b.low & 0xf0) != 0xf0)
752                 probe_hwif_init(&ide_hwifs[index_list.b.low]);
753         if ((index_list.b.high & 0xf0) != 0xf0)
754                 probe_hwif_init(&ide_hwifs[index_list.b.high]);
755         if ((index_list2.b.low & 0xf0) != 0xf0)
756                 probe_hwif_init(&ide_hwifs[index_list2.b.low]);
757         if ((index_list2.b.high & 0xf0) != 0xf0)
758                 probe_hwif_init(&ide_hwifs[index_list2.b.high]);
759
760         create_proc_ide_interfaces();
761 }
762
763 EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
764
765 /*
766  *      Module interfaces
767  */
768  
769 static int pre_init = 1;                /* Before first ordered IDE scan */
770 static LIST_HEAD(ide_pci_drivers);
771
772 /*
773  *      ide_register_pci_driver         -       attach IDE driver
774  *      @driver: pci driver
775  *
776  *      Registers a driver with the IDE layer. The IDE layer arranges that
777  *      boot time setup is done in the expected device order and then 
778  *      hands the controllers off to the core PCI code to do the rest of
779  *      the work.
780  *
781  *      The driver_data of the driver table must point to an ide_pci_device_t
782  *      describing the interface.
783  *
784  *      Returns are the same as for pci_register_driver
785  */
786
787 int ide_pci_register_driver(struct pci_driver *driver)
788 {
789         if(!pre_init)
790                 return pci_module_init(driver);
791         list_add_tail(&driver->node, &ide_pci_drivers);
792         return 0;
793 }
794
795 EXPORT_SYMBOL_GPL(ide_pci_register_driver);
796
797 /**
798  *      ide_unregister_pci_driver       -       unregister an IDE driver
799  *      @driver: driver to remove
800  *
801  *      Unregister a currently installed IDE driver. Returns are the same
802  *      as for pci_unregister_driver
803  */
804  
805 void ide_pci_unregister_driver(struct pci_driver *driver)
806 {
807         if(!pre_init)
808                 pci_unregister_driver(driver);
809         else
810                 list_del(&driver->node);
811 }
812
813 EXPORT_SYMBOL_GPL(ide_pci_unregister_driver);
814
815 /**
816  *      ide_scan_pcidev         -       find an IDE driver for a device
817  *      @dev: PCI device to check
818  *
819  *      Look for an IDE driver to handle the device we are considering.
820  *      This is only used during boot up to get the ordering correct. After
821  *      boot up the pci layer takes over the job.
822  */
823  
824 static int __init ide_scan_pcidev(struct pci_dev *dev)
825 {
826         struct list_head *l;
827         struct pci_driver *d;
828         
829         list_for_each(l, &ide_pci_drivers)
830         {
831                 d = list_entry(l, struct pci_driver, node);
832                 if(d->id_table)
833                 {
834                         const struct pci_device_id *id = pci_match_device(d->id_table, dev);
835                         if(id != NULL)
836                         {
837                                 if(d->probe(dev, id) >= 0)
838                                 {
839                                         dev->driver = d;
840                                         return 1;
841                                 }
842                         }
843                 }
844         }
845         return 0;
846 }
847
848 /**
849  *      ide_scan_pcibus         -       perform the initial IDE driver scan
850  *      @scan_direction: set for reverse order scanning
851  *
852  *      Perform the initial bus rather than driver ordered scan of the
853  *      PCI drivers. After this all IDE pci handling becomes standard
854  *      module ordering not traditionally ordered.
855  */
856         
857 void __init ide_scan_pcibus (int scan_direction)
858 {
859         struct pci_dev *dev = NULL;
860         struct pci_driver *d;
861         struct list_head *l, *n;
862
863         pre_init = 0;
864         if (!scan_direction) {
865                 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
866                         ide_scan_pcidev(dev);
867                 }
868         } else {
869                 while ((dev = pci_find_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
870                         ide_scan_pcidev(dev);
871                 }
872         }
873         
874         /*
875          *      Hand the drivers over to the PCI layer now we
876          *      are post init.
877          */
878
879         list_for_each_safe(l, n, &ide_pci_drivers)
880         {
881                 list_del(l);
882                 d = list_entry(l, struct pci_driver, node);
883                 pci_register_driver(d);
884         }
885 }