setup-pci.c 24 KB

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