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