pci.c 11 KB

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