setup-pci.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. 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. static int secondpdc = 0;
  528. u8 tmp;
  529. index->all = 0xf0f0;
  530. /*
  531. * Set up the IDE ports
  532. */
  533. for (port = 0; port <= 1; ++port) {
  534. ide_pci_enablebit_t *e = &(d->enablebits[port]);
  535. /*
  536. * If this is a Promise FakeRaid controller,
  537. * the 2nd controller will be marked as
  538. * disabled while it is actually there and enabled
  539. * by the bios for raid purposes.
  540. * Skip the normal "is it enabled" test for those.
  541. */
  542. if ((d->flags & IDEPCI_FLAG_FORCE_PDC) &&
  543. (secondpdc++==1) && (port==1))
  544. goto controller_ok;
  545. if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
  546. (tmp & e->mask) != e->val))
  547. continue; /* port not enabled */
  548. controller_ok:
  549. if (d->channels <= port)
  550. break;
  551. if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
  552. continue;
  553. /* setup proper ancestral information */
  554. hwif->gendev.parent = &dev->dev;
  555. if (hwif->channel) {
  556. index->b.high = hwif->index;
  557. } else {
  558. index->b.low = hwif->index;
  559. }
  560. if (d->init_iops)
  561. d->init_iops(hwif);
  562. if (d->autodma == NODMA)
  563. goto bypass_legacy_dma;
  564. if(d->init_setup_dma)
  565. d->init_setup_dma(dev, d, hwif);
  566. else
  567. ide_hwif_setup_dma(dev, d, hwif);
  568. bypass_legacy_dma:
  569. if (d->init_hwif)
  570. /* Call chipset-specific routine
  571. * for each enabled hwif
  572. */
  573. d->init_hwif(hwif);
  574. mate = hwif;
  575. at_least_one_hwif_enabled = 1;
  576. }
  577. if (!at_least_one_hwif_enabled)
  578. printk(KERN_INFO "%s: neither IDE port enabled (BIOS)\n", d->name);
  579. }
  580. EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
  581. /*
  582. * ide_setup_pci_device() looks at the primary/secondary interfaces
  583. * on a PCI IDE device and, if they are enabled, prepares the IDE driver
  584. * for use with them. This generic code works for most PCI chipsets.
  585. *
  586. * One thing that is not standardized is the location of the
  587. * primary/secondary interface "enable/disable" bits. For chipsets that
  588. * we "know" about, this information is in the ide_pci_device_t struct;
  589. * for all other chipsets, we just assume both interfaces are enabled.
  590. */
  591. static int do_ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d,
  592. ata_index_t *index, u8 noisy)
  593. {
  594. static ata_index_t ata_index = { .b = { .low = 0xff, .high = 0xff } };
  595. int tried_config = 0;
  596. int pciirq, ret;
  597. ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
  598. if (ret < 0)
  599. goto out;
  600. /*
  601. * Can we trust the reported IRQ?
  602. */
  603. pciirq = dev->irq;
  604. /* Is it an "IDE storage" device in non-PCI mode? */
  605. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
  606. if (noisy)
  607. printk(KERN_INFO "%s: not 100%% native mode: "
  608. "will probe irqs later\n", d->name);
  609. /*
  610. * This allows offboard ide-pci cards the enable a BIOS,
  611. * verify interrupt settings of split-mirror pci-config
  612. * space, place chipset into init-mode, and/or preserve
  613. * an interrupt if the card is not native ide support.
  614. */
  615. ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
  616. if (ret < 0)
  617. goto out;
  618. pciirq = ret;
  619. } else if (tried_config) {
  620. if (noisy)
  621. printk(KERN_INFO "%s: will probe irqs later\n", d->name);
  622. pciirq = 0;
  623. } else if (!pciirq) {
  624. if (noisy)
  625. printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
  626. d->name, pciirq);
  627. pciirq = 0;
  628. } else {
  629. if (d->init_chipset) {
  630. ret = d->init_chipset(dev, d->name);
  631. if (ret < 0)
  632. goto out;
  633. }
  634. if (noisy)
  635. #ifdef __sparc__
  636. printk(KERN_INFO "%s: 100%% native mode on irq %s\n",
  637. d->name, __irq_itoa(pciirq));
  638. #else
  639. printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
  640. d->name, pciirq);
  641. #endif
  642. }
  643. /* FIXME: silent failure can happen */
  644. *index = ata_index;
  645. ide_pci_setup_ports(dev, d, pciirq, index);
  646. out:
  647. return ret;
  648. }
  649. int ide_setup_pci_device(struct pci_dev *dev, ide_pci_device_t *d)
  650. {
  651. ata_index_t index_list;
  652. int ret;
  653. ret = do_ide_setup_pci_device(dev, d, &index_list, 1);
  654. if (ret < 0)
  655. goto out;
  656. if ((index_list.b.low & 0xf0) != 0xf0)
  657. probe_hwif_init_with_fixup(&ide_hwifs[index_list.b.low], d->fixup);
  658. if ((index_list.b.high & 0xf0) != 0xf0)
  659. probe_hwif_init_with_fixup(&ide_hwifs[index_list.b.high], d->fixup);
  660. create_proc_ide_interfaces();
  661. out:
  662. return ret;
  663. }
  664. EXPORT_SYMBOL_GPL(ide_setup_pci_device);
  665. int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
  666. ide_pci_device_t *d)
  667. {
  668. struct pci_dev *pdev[] = { dev1, dev2 };
  669. ata_index_t index_list[2];
  670. int ret, i;
  671. for (i = 0; i < 2; i++) {
  672. ret = do_ide_setup_pci_device(pdev[i], d, index_list + i, !i);
  673. /*
  674. * FIXME: Mom, mom, they stole me the helper function to undo
  675. * do_ide_setup_pci_device() on the first device!
  676. */
  677. if (ret < 0)
  678. goto out;
  679. }
  680. for (i = 0; i < 2; i++) {
  681. u8 idx[2] = { index_list[i].b.low, index_list[i].b.high };
  682. int j;
  683. for (j = 0; j < 2; j++) {
  684. if ((idx[j] & 0xf0) != 0xf0)
  685. probe_hwif_init(ide_hwifs + idx[j]);
  686. }
  687. }
  688. create_proc_ide_interfaces();
  689. out:
  690. return ret;
  691. }
  692. EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
  693. /*
  694. * Module interfaces
  695. */
  696. static int pre_init = 1; /* Before first ordered IDE scan */
  697. static LIST_HEAD(ide_pci_drivers);
  698. /*
  699. * ide_register_pci_driver - attach IDE driver
  700. * @driver: pci driver
  701. *
  702. * Registers a driver with the IDE layer. The IDE layer arranges that
  703. * boot time setup is done in the expected device order and then
  704. * hands the controllers off to the core PCI code to do the rest of
  705. * the work.
  706. *
  707. * The driver_data of the driver table must point to an ide_pci_device_t
  708. * describing the interface.
  709. *
  710. * Returns are the same as for pci_register_driver
  711. */
  712. int ide_pci_register_driver(struct pci_driver *driver)
  713. {
  714. if(!pre_init)
  715. return pci_module_init(driver);
  716. list_add_tail(&driver->node, &ide_pci_drivers);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(ide_pci_register_driver);
  720. /**
  721. * ide_unregister_pci_driver - unregister an IDE driver
  722. * @driver: driver to remove
  723. *
  724. * Unregister a currently installed IDE driver. Returns are the same
  725. * as for pci_unregister_driver
  726. */
  727. void ide_pci_unregister_driver(struct pci_driver *driver)
  728. {
  729. if(!pre_init)
  730. pci_unregister_driver(driver);
  731. else
  732. list_del(&driver->node);
  733. }
  734. EXPORT_SYMBOL_GPL(ide_pci_unregister_driver);
  735. /**
  736. * ide_scan_pcidev - find an IDE driver for a device
  737. * @dev: PCI device to check
  738. *
  739. * Look for an IDE driver to handle the device we are considering.
  740. * This is only used during boot up to get the ordering correct. After
  741. * boot up the pci layer takes over the job.
  742. */
  743. static int __init ide_scan_pcidev(struct pci_dev *dev)
  744. {
  745. struct list_head *l;
  746. struct pci_driver *d;
  747. list_for_each(l, &ide_pci_drivers)
  748. {
  749. d = list_entry(l, struct pci_driver, node);
  750. if(d->id_table)
  751. {
  752. const struct pci_device_id *id = pci_match_device(d->id_table, dev);
  753. if(id != NULL)
  754. {
  755. if(d->probe(dev, id) >= 0)
  756. {
  757. dev->driver = d;
  758. return 1;
  759. }
  760. }
  761. }
  762. }
  763. return 0;
  764. }
  765. /**
  766. * ide_scan_pcibus - perform the initial IDE driver scan
  767. * @scan_direction: set for reverse order scanning
  768. *
  769. * Perform the initial bus rather than driver ordered scan of the
  770. * PCI drivers. After this all IDE pci handling becomes standard
  771. * module ordering not traditionally ordered.
  772. */
  773. void __init ide_scan_pcibus (int scan_direction)
  774. {
  775. struct pci_dev *dev = NULL;
  776. struct pci_driver *d;
  777. struct list_head *l, *n;
  778. pre_init = 0;
  779. if (!scan_direction) {
  780. while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  781. ide_scan_pcidev(dev);
  782. }
  783. } else {
  784. while ((dev = pci_find_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  785. ide_scan_pcidev(dev);
  786. }
  787. }
  788. /*
  789. * Hand the drivers over to the PCI layer now we
  790. * are post init.
  791. */
  792. list_for_each_safe(l, n, &ide_pci_drivers)
  793. {
  794. list_del(l);
  795. d = list_entry(l, struct pci_driver, node);
  796. pci_register_driver(d);
  797. }
  798. }