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