pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * PCI handling of I2O controller
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * A lot of the I2O message side code from this is taken from the Red
  14. * Creek RCPCI45 adapter driver by Red Creek Communications
  15. *
  16. * Fixes/additions:
  17. * Philipp Rumpf
  18. * Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
  19. * Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
  20. * Deepak Saxena <deepak@plexity.net>
  21. * Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
  22. * Alan Cox <alan@redhat.com>:
  23. * Ported to Linux 2.5.
  24. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  25. * Minor fixes for 2.6.
  26. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  27. * Support for sysfs included.
  28. */
  29. #include <linux/pci.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/i2o.h>
  32. /* Module internal functions from other sources */
  33. extern struct i2o_controller *i2o_iop_alloc(void);
  34. extern void i2o_iop_free(struct i2o_controller *);
  35. extern int i2o_iop_add(struct i2o_controller *);
  36. extern void i2o_iop_remove(struct i2o_controller *);
  37. extern int i2o_driver_dispatch(struct i2o_controller *, u32);
  38. /* PCI device id table for all I2O controllers */
  39. static struct pci_device_id __devinitdata i2o_pci_ids[] = {
  40. {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O << 8, 0xffff00)},
  41. {PCI_DEVICE(PCI_VENDOR_ID_DPT, 0xa511)},
  42. {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
  43. .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
  44. {0}
  45. };
  46. /**
  47. * i2o_pci_free - Frees the DMA memory for the I2O controller
  48. * @c: I2O controller to free
  49. *
  50. * Remove all allocated DMA memory and unmap memory IO regions. If MTRR
  51. * is enabled, also remove it again.
  52. */
  53. static void i2o_pci_free(struct i2o_controller *c)
  54. {
  55. struct device *dev;
  56. dev = &c->pdev->dev;
  57. i2o_dma_free(dev, &c->out_queue);
  58. i2o_dma_free(dev, &c->status_block);
  59. kfree(c->lct);
  60. i2o_dma_free(dev, &c->dlct);
  61. i2o_dma_free(dev, &c->hrt);
  62. i2o_dma_free(dev, &c->status);
  63. if (c->raptor && c->in_queue.virt)
  64. iounmap(c->in_queue.virt);
  65. if (c->base.virt)
  66. iounmap(c->base.virt);
  67. }
  68. /**
  69. * i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
  70. * @c: I2O controller
  71. *
  72. * Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
  73. * IO mappings are also done here. If MTRR is enabled, also do add memory
  74. * regions here.
  75. *
  76. * Returns 0 on success or negative error code on failure.
  77. */
  78. static int __devinit i2o_pci_alloc(struct i2o_controller *c)
  79. {
  80. struct pci_dev *pdev = c->pdev;
  81. struct device *dev = &pdev->dev;
  82. int i;
  83. for (i = 0; i < 6; i++) {
  84. /* Skip I/O spaces */
  85. if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
  86. if (!c->base.phys) {
  87. c->base.phys = pci_resource_start(pdev, i);
  88. c->base.len = pci_resource_len(pdev, i);
  89. /*
  90. * If we know what card it is, set the size
  91. * correctly. Code is taken from dpt_i2o.c
  92. */
  93. if (pdev->device == 0xa501) {
  94. if (pdev->subsystem_device >= 0xc032 &&
  95. pdev->subsystem_device <= 0xc03b) {
  96. if (c->base.len > 0x400000)
  97. c->base.len = 0x400000;
  98. } else {
  99. if (c->base.len > 0x100000)
  100. c->base.len = 0x100000;
  101. }
  102. }
  103. if (!c->raptor)
  104. break;
  105. } else {
  106. c->in_queue.phys = pci_resource_start(pdev, i);
  107. c->in_queue.len = pci_resource_len(pdev, i);
  108. break;
  109. }
  110. }
  111. }
  112. if (i == 6) {
  113. printk(KERN_ERR "%s: I2O controller has no memory regions"
  114. " defined.\n", c->name);
  115. i2o_pci_free(c);
  116. return -EINVAL;
  117. }
  118. /* Map the I2O controller */
  119. if (c->raptor) {
  120. printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
  121. printk(KERN_INFO " BAR0 at 0x%08lX size=%ld\n",
  122. (unsigned long)c->base.phys, (unsigned long)c->base.len);
  123. printk(KERN_INFO " BAR1 at 0x%08lX size=%ld\n",
  124. (unsigned long)c->in_queue.phys,
  125. (unsigned long)c->in_queue.len);
  126. } else
  127. printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
  128. c->name, (unsigned long)c->base.phys,
  129. (unsigned long)c->base.len);
  130. c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
  131. if (!c->base.virt) {
  132. printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
  133. return -ENOMEM;
  134. }
  135. if (c->raptor) {
  136. c->in_queue.virt =
  137. ioremap_nocache(c->in_queue.phys, c->in_queue.len);
  138. if (!c->in_queue.virt) {
  139. printk(KERN_ERR "%s: Unable to map controller.\n",
  140. c->name);
  141. i2o_pci_free(c);
  142. return -ENOMEM;
  143. }
  144. } else
  145. c->in_queue = c->base;
  146. c->irq_status = c->base.virt + I2O_IRQ_STATUS;
  147. c->irq_mask = c->base.virt + I2O_IRQ_MASK;
  148. c->in_port = c->base.virt + I2O_IN_PORT;
  149. c->out_port = c->base.virt + I2O_OUT_PORT;
  150. if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
  151. i2o_pci_free(c);
  152. return -ENOMEM;
  153. }
  154. if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
  155. i2o_pci_free(c);
  156. return -ENOMEM;
  157. }
  158. if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
  159. i2o_pci_free(c);
  160. return -ENOMEM;
  161. }
  162. if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
  163. GFP_KERNEL)) {
  164. i2o_pci_free(c);
  165. return -ENOMEM;
  166. }
  167. if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
  168. i2o_pci_free(c);
  169. return -ENOMEM;
  170. }
  171. pci_set_drvdata(pdev, c);
  172. return 0;
  173. }
  174. /**
  175. * i2o_pci_interrupt - Interrupt handler for I2O controller
  176. * @irq: interrupt line
  177. * @dev_id: pointer to the I2O controller
  178. * @r: pointer to registers
  179. *
  180. * Handle an interrupt from a PCI based I2O controller. This turns out
  181. * to be rather simple. We keep the controller pointer in the cookie.
  182. */
  183. static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
  184. {
  185. struct i2o_controller *c = dev_id;
  186. u32 m;
  187. irqreturn_t rc = IRQ_NONE;
  188. while (readl(c->irq_status) & I2O_IRQ_OUTBOUND_POST) {
  189. m = readl(c->out_port);
  190. if (m == I2O_QUEUE_EMPTY) {
  191. /*
  192. * Old 960 steppings had a bug in the I2O unit that
  193. * caused the queue to appear empty when it wasn't.
  194. */
  195. m = readl(c->out_port);
  196. if (unlikely(m == I2O_QUEUE_EMPTY))
  197. break;
  198. }
  199. /* dispatch it */
  200. if (i2o_driver_dispatch(c, m))
  201. /* flush it if result != 0 */
  202. i2o_flush_reply(c, m);
  203. rc = IRQ_HANDLED;
  204. }
  205. return rc;
  206. }
  207. /**
  208. * i2o_pci_irq_enable - Allocate interrupt for I2O controller
  209. *
  210. * Allocate an interrupt for the I2O controller, and activate interrupts
  211. * on the I2O controller.
  212. *
  213. * Returns 0 on success or negative error code on failure.
  214. */
  215. static int i2o_pci_irq_enable(struct i2o_controller *c)
  216. {
  217. struct pci_dev *pdev = c->pdev;
  218. int rc;
  219. wmb();
  220. writel(0xffffffff, c->irq_mask);
  221. wmb();
  222. if (pdev->irq) {
  223. rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
  224. c->name, c);
  225. if (rc < 0) {
  226. printk(KERN_ERR "%s: unable to allocate interrupt %d."
  227. "\n", c->name, pdev->irq);
  228. return rc;
  229. }
  230. }
  231. writel(0x00000000, c->irq_mask);
  232. wmb();
  233. printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
  234. return 0;
  235. }
  236. /**
  237. * i2o_pci_irq_disable - Free interrupt for I2O controller
  238. * @c: I2O controller
  239. *
  240. * Disable interrupts in I2O controller and then free interrupt.
  241. */
  242. static void i2o_pci_irq_disable(struct i2o_controller *c)
  243. {
  244. wmb();
  245. writel(0xffffffff, c->irq_mask);
  246. wmb();
  247. if (c->pdev->irq > 0)
  248. free_irq(c->pdev->irq, c);
  249. }
  250. /**
  251. * i2o_pci_probe - Probe the PCI device for an I2O controller
  252. * @dev: PCI device to test
  253. * @id: id which matched with the PCI device id table
  254. *
  255. * Probe the PCI device for any device which is a memory of the
  256. * Intelligent, I2O class or an Adaptec Zero Channel Controller. We
  257. * attempt to set up each such device and register it with the core.
  258. *
  259. * Returns 0 on success or negative error code on failure.
  260. */
  261. static int __devinit i2o_pci_probe(struct pci_dev *pdev,
  262. const struct pci_device_id *id)
  263. {
  264. struct i2o_controller *c;
  265. int rc;
  266. struct pci_dev *i960 = NULL;
  267. printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
  268. if ((pdev->class & 0xff) > 1) {
  269. printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
  270. "(skipping).\n", pci_name(pdev));
  271. return -ENODEV;
  272. }
  273. if ((rc = pci_enable_device(pdev))) {
  274. printk(KERN_WARNING "i2o: couldn't enable device %s\n",
  275. pci_name(pdev));
  276. return rc;
  277. }
  278. if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
  279. printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
  280. pci_name(pdev));
  281. rc = -ENODEV;
  282. goto disable;
  283. }
  284. pci_set_master(pdev);
  285. c = i2o_iop_alloc();
  286. if (IS_ERR(c)) {
  287. printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
  288. pci_name(pdev));
  289. rc = PTR_ERR(c);
  290. goto disable;
  291. } else
  292. printk(KERN_INFO "%s: controller found (%s)\n", c->name,
  293. pci_name(pdev));
  294. c->pdev = pdev;
  295. c->device.parent = get_device(&pdev->dev);
  296. /* Cards that fall apart if you hit them with large I/O loads... */
  297. if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
  298. c->short_req = 1;
  299. printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
  300. c->name);
  301. }
  302. if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
  303. /*
  304. * Expose the ship behind i960 for initialization, or it will
  305. * failed
  306. */
  307. i960 =
  308. pci_find_slot(c->pdev->bus->number,
  309. PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
  310. if (i960)
  311. pci_write_config_word(i960, 0x42, 0);
  312. c->promise = 1;
  313. }
  314. /* Cards that go bananas if you quiesce them before you reset them. */
  315. if (pdev->vendor == PCI_VENDOR_ID_DPT) {
  316. c->no_quiesce = 1;
  317. if (pdev->device == 0xa511)
  318. c->raptor = 1;
  319. }
  320. if ((rc = i2o_pci_alloc(c))) {
  321. printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
  322. " failed\n", c->name);
  323. goto free_controller;
  324. }
  325. if (i2o_pci_irq_enable(c)) {
  326. printk(KERN_ERR "%s: unable to enable interrupts for I2O "
  327. "controller\n", c->name);
  328. goto free_pci;
  329. }
  330. if ((rc = i2o_iop_add(c)))
  331. goto uninstall;
  332. if (i960)
  333. pci_write_config_word(i960, 0x42, 0x03ff);
  334. get_device(&c->device);
  335. return 0;
  336. uninstall:
  337. i2o_pci_irq_disable(c);
  338. free_pci:
  339. i2o_pci_free(c);
  340. free_controller:
  341. i2o_iop_free(c);
  342. put_device(c->device.parent);
  343. disable:
  344. pci_disable_device(pdev);
  345. return rc;
  346. }
  347. /**
  348. * i2o_pci_remove - Removes a I2O controller from the system
  349. * pdev: I2O controller which should be removed
  350. *
  351. * Reset the I2O controller, disable interrupts and remove all allocated
  352. * resources.
  353. */
  354. static void __devexit i2o_pci_remove(struct pci_dev *pdev)
  355. {
  356. struct i2o_controller *c;
  357. c = pci_get_drvdata(pdev);
  358. i2o_iop_remove(c);
  359. i2o_pci_irq_disable(c);
  360. i2o_pci_free(c);
  361. pci_disable_device(pdev);
  362. printk(KERN_INFO "%s: Controller removed.\n", c->name);
  363. put_device(c->device.parent);
  364. put_device(&c->device);
  365. };
  366. /* PCI driver for I2O controller */
  367. static struct pci_driver i2o_pci_driver = {
  368. .name = "PCI_I2O",
  369. .id_table = i2o_pci_ids,
  370. .probe = i2o_pci_probe,
  371. .remove = __devexit_p(i2o_pci_remove),
  372. };
  373. /**
  374. * i2o_pci_init - registers I2O PCI driver in PCI subsystem
  375. *
  376. * Returns > 0 on success or negative error code on failure.
  377. */
  378. int __init i2o_pci_init(void)
  379. {
  380. return pci_register_driver(&i2o_pci_driver);
  381. };
  382. /**
  383. * i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
  384. */
  385. void __exit i2o_pci_exit(void)
  386. {
  387. pci_unregister_driver(&i2o_pci_driver);
  388. };
  389. EXPORT_SYMBOL(i2o_dma_realloc);
  390. MODULE_DEVICE_TABLE(pci, i2o_pci_ids);