pci.c 12 KB

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