setup-pci.c 21 KB

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