setup-pci.c 23 KB

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