setup-pci.c 22 KB

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