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