setup-pci.c 23 KB

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