setup-pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. #endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
  464. /**
  465. * ide_setup_pci_controller - set up IDE PCI
  466. * @dev: PCI device
  467. * @d: IDE PCI data
  468. * @noisy: verbose flag
  469. * @config: returned as 1 if we configured the hardware
  470. *
  471. * Set up the PCI and controller side of the IDE interface. This brings
  472. * up the PCI side of the device, checks that the device is enabled
  473. * and enables it if need be
  474. */
  475. static int ide_setup_pci_controller(struct pci_dev *dev, ide_pci_device_t *d, int noisy, int *config)
  476. {
  477. int ret;
  478. u32 class_rev;
  479. u16 pcicmd;
  480. if (noisy)
  481. ide_setup_pci_noise(dev, d);
  482. ret = ide_pci_enable(dev, d);
  483. if (ret < 0)
  484. goto out;
  485. ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
  486. if (ret < 0) {
  487. printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
  488. goto out;
  489. }
  490. if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
  491. ret = ide_pci_configure(dev, d);
  492. if (ret < 0)
  493. goto out;
  494. *config = 1;
  495. printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
  496. }
  497. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
  498. class_rev &= 0xff;
  499. if (noisy)
  500. printk(KERN_INFO "%s: chipset revision %d\n", d->name, class_rev);
  501. out:
  502. return ret;
  503. }
  504. /**
  505. * ide_pci_setup_ports - configure ports/devices on PCI IDE
  506. * @dev: PCI device
  507. * @d: IDE pci device info
  508. * @pciirq: IRQ line
  509. * @index: ata index to update
  510. *
  511. * Scan the interfaces attached to this device and do any
  512. * necessary per port setup. Attach the devices and ask the
  513. * generic DMA layer to do its work for us.
  514. *
  515. * Normally called automaticall from do_ide_pci_setup_device,
  516. * but is also used directly as a helper function by some controllers
  517. * where the chipset setup is not the default PCI IDE one.
  518. */
  519. void ide_pci_setup_ports(struct pci_dev *dev, ide_pci_device_t *d, int pciirq, ata_index_t *index)
  520. {
  521. int port;
  522. int at_least_one_hwif_enabled = 0;
  523. ide_hwif_t *hwif, *mate = NULL;
  524. u8 tmp;
  525. index->all = 0xf0f0;
  526. /*
  527. * Set up the IDE ports
  528. */
  529. for (port = 0; port <= 1; ++port) {
  530. ide_pci_enablebit_t *e = &(d->enablebits[port]);
  531. if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
  532. (tmp & e->mask) != e->val))
  533. continue; /* port not enabled */
  534. if (d->channels <= port)
  535. break;
  536. if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
  537. continue;
  538. /* setup proper ancestral information */
  539. hwif->gendev.parent = &dev->dev;
  540. if (hwif->channel) {
  541. index->b.high = hwif->index;
  542. } else {
  543. index->b.low = hwif->index;
  544. }
  545. if (d->init_iops)
  546. d->init_iops(hwif);
  547. if (d->autodma == NODMA)
  548. goto bypass_legacy_dma;
  549. if(d->init_setup_dma)
  550. d->init_setup_dma(dev, d, hwif);
  551. else
  552. ide_hwif_setup_dma(dev, d, hwif);
  553. bypass_legacy_dma:
  554. if (d->init_hwif)
  555. /* Call chipset-specific routine
  556. * for each enabled hwif
  557. */
  558. d->init_hwif(hwif);
  559. mate = hwif;
  560. at_least_one_hwif_enabled = 1;
  561. }
  562. if (!at_least_one_hwif_enabled)
  563. printk(KERN_INFO "%s: neither IDE port enabled (BIOS)\n", d->name);
  564. }
  565. EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
  566. /*
  567. * ide_setup_pci_device() looks at the primary/secondary interfaces
  568. * on a PCI IDE device and, if they are enabled, prepares the IDE driver
  569. * for use with them. This generic code works for most PCI chipsets.
  570. *
  571. * One thing that is not standardized is the location of the
  572. * primary/secondary interface "enable/disable" bits. For chipsets that
  573. * we "know" about, this information is in the ide_pci_device_t struct;
  574. * for all other chipsets, we just assume both interfaces are enabled.
  575. */
  576. static int do_ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d,
  577. ata_index_t *index, u8 noisy)
  578. {
  579. static ata_index_t ata_index = { .b = { .low = 0xff, .high = 0xff } };
  580. int tried_config = 0;
  581. int pciirq, ret;
  582. ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
  583. if (ret < 0)
  584. goto out;
  585. /*
  586. * Can we trust the reported IRQ?
  587. */
  588. pciirq = dev->irq;
  589. /* Is it an "IDE storage" device in non-PCI mode? */
  590. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
  591. if (noisy)
  592. printk(KERN_INFO "%s: not 100%% native mode: "
  593. "will probe irqs later\n", d->name);
  594. /*
  595. * This allows offboard ide-pci cards the enable a BIOS,
  596. * verify interrupt settings of split-mirror pci-config
  597. * space, place chipset into init-mode, and/or preserve
  598. * an interrupt if the card is not native ide support.
  599. */
  600. ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
  601. if (ret < 0)
  602. goto out;
  603. pciirq = ret;
  604. } else if (tried_config) {
  605. if (noisy)
  606. printk(KERN_INFO "%s: will probe irqs later\n", d->name);
  607. pciirq = 0;
  608. } else if (!pciirq) {
  609. if (noisy)
  610. printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
  611. d->name, pciirq);
  612. pciirq = 0;
  613. } else {
  614. if (d->init_chipset) {
  615. ret = d->init_chipset(dev, d->name);
  616. if (ret < 0)
  617. goto out;
  618. }
  619. if (noisy)
  620. printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
  621. d->name, pciirq);
  622. }
  623. /* FIXME: silent failure can happen */
  624. *index = ata_index;
  625. ide_pci_setup_ports(dev, d, pciirq, index);
  626. out:
  627. return ret;
  628. }
  629. int ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d)
  630. {
  631. ide_hwif_t *hwif = NULL, *mate = NULL;
  632. ata_index_t index_list;
  633. int ret;
  634. ret = do_ide_setup_pci_device(dev, d, &index_list, 1);
  635. if (ret < 0)
  636. goto out;
  637. if ((index_list.b.low & 0xf0) != 0xf0)
  638. hwif = &ide_hwifs[index_list.b.low];
  639. if ((index_list.b.high & 0xf0) != 0xf0)
  640. mate = &ide_hwifs[index_list.b.high];
  641. if (hwif)
  642. probe_hwif_init_with_fixup(hwif, d->fixup);
  643. if (mate)
  644. probe_hwif_init_with_fixup(mate, d->fixup);
  645. if (hwif)
  646. ide_proc_register_port(hwif);
  647. if (mate)
  648. ide_proc_register_port(mate);
  649. out:
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(ide_setup_pci_device);
  653. int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
  654. ide_pci_device_t *d)
  655. {
  656. struct pci_dev *pdev[] = { dev1, dev2 };
  657. ata_index_t index_list[2];
  658. int ret, i;
  659. for (i = 0; i < 2; i++) {
  660. ret = do_ide_setup_pci_device(pdev[i], d, index_list + i, !i);
  661. /*
  662. * FIXME: Mom, mom, they stole me the helper function to undo
  663. * do_ide_setup_pci_device() on the first device!
  664. */
  665. if (ret < 0)
  666. goto out;
  667. }
  668. for (i = 0; i < 2; i++) {
  669. u8 idx[2] = { index_list[i].b.low, index_list[i].b.high };
  670. int j;
  671. for (j = 0; j < 2; j++) {
  672. if ((idx[j] & 0xf0) != 0xf0)
  673. probe_hwif_init(ide_hwifs + idx[j]);
  674. }
  675. }
  676. for (i = 0; i < 2; i++) {
  677. u8 idx[2] = { index_list[i].b.low, index_list[i].b.high };
  678. int j;
  679. for (j = 0; j < 2; j++) {
  680. if ((idx[j] & 0xf0) != 0xf0)
  681. ide_proc_register_port(ide_hwifs + idx[j]);
  682. }
  683. }
  684. out:
  685. return ret;
  686. }
  687. EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
  688. #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
  689. /*
  690. * Module interfaces
  691. */
  692. static int pre_init = 1; /* Before first ordered IDE scan */
  693. static LIST_HEAD(ide_pci_drivers);
  694. /*
  695. * __ide_pci_register_driver - attach IDE driver
  696. * @driver: pci driver
  697. * @module: owner module of the driver
  698. *
  699. * Registers a driver with the IDE layer. The IDE layer arranges that
  700. * boot time setup is done in the expected device order and then
  701. * hands the controllers off to the core PCI code to do the rest of
  702. * the work.
  703. *
  704. * The driver_data of the driver table must point to an ide_pci_device_t
  705. * describing the interface.
  706. *
  707. * Returns are the same as for pci_register_driver
  708. */
  709. int __ide_pci_register_driver(struct pci_driver *driver, struct module *module,
  710. const char *mod_name)
  711. {
  712. if(!pre_init)
  713. return __pci_register_driver(driver, module, mod_name);
  714. driver->driver.owner = module;
  715. list_add_tail(&driver->node, &ide_pci_drivers);
  716. return 0;
  717. }
  718. EXPORT_SYMBOL_GPL(__ide_pci_register_driver);
  719. /**
  720. * ide_scan_pcidev - find an IDE driver for a device
  721. * @dev: PCI device to check
  722. *
  723. * Look for an IDE driver to handle the device we are considering.
  724. * This is only used during boot up to get the ordering correct. After
  725. * boot up the pci layer takes over the job.
  726. */
  727. static int __init ide_scan_pcidev(struct pci_dev *dev)
  728. {
  729. struct list_head *l;
  730. struct pci_driver *d;
  731. list_for_each(l, &ide_pci_drivers)
  732. {
  733. d = list_entry(l, struct pci_driver, node);
  734. if(d->id_table)
  735. {
  736. const struct pci_device_id *id = pci_match_id(d->id_table, dev);
  737. if(id != NULL)
  738. {
  739. if(d->probe(dev, id) >= 0)
  740. {
  741. dev->driver = d;
  742. return 1;
  743. }
  744. }
  745. }
  746. }
  747. return 0;
  748. }
  749. /**
  750. * ide_scan_pcibus - perform the initial IDE driver scan
  751. * @scan_direction: set for reverse order scanning
  752. *
  753. * Perform the initial bus rather than driver ordered scan of the
  754. * PCI drivers. After this all IDE pci handling becomes standard
  755. * module ordering not traditionally ordered.
  756. */
  757. void __init ide_scan_pcibus (int scan_direction)
  758. {
  759. struct pci_dev *dev = NULL;
  760. struct pci_driver *d;
  761. struct list_head *l, *n;
  762. pre_init = 0;
  763. if (!scan_direction) {
  764. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  765. ide_scan_pcidev(dev);
  766. }
  767. } else {
  768. while ((dev = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  769. ide_scan_pcidev(dev);
  770. }
  771. }
  772. /*
  773. * Hand the drivers over to the PCI layer now we
  774. * are post init.
  775. */
  776. list_for_each_safe(l, n, &ide_pci_drivers) {
  777. list_del(l);
  778. d = list_entry(l, struct pci_driver, node);
  779. if (__pci_register_driver(d, d->driver.owner,
  780. d->driver.mod_name)) {
  781. printk(KERN_ERR "%s: failed to register driver "
  782. "for %s\n", __FUNCTION__,
  783. d->driver.mod_name);
  784. }
  785. }
  786. }
  787. #endif