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