2 * PCI handling of I2O controller
4 * Copyright (C) 1999-2002 Red Hat Software
6 * Written by Alan Cox, Building Number Three Ltd
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.
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
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.
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2o.h>
34 /* Module internal functions from other sources */
35 extern struct i2o_controller *i2o_iop_alloc(void);
36 extern void i2o_iop_free(struct i2o_controller *);
38 extern int i2o_iop_add(struct i2o_controller *);
39 extern void i2o_iop_remove(struct i2o_controller *);
41 extern int i2o_driver_dispatch(struct i2o_controller *, u32,
42 struct i2o_message *);
44 /* PCI device id table for all I2O controllers */
45 static struct pci_device_id __devinitdata i2o_pci_ids[] = {
46 {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O << 8, 0xffff00)},
47 {PCI_DEVICE(PCI_VENDOR_ID_DPT, 0xa511)},
48 {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
49 .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
54 * i2o_dma_realloc - Realloc DMA memory
55 * @dev: struct device pointer to the PCI device of the I2O controller
56 * @addr: pointer to a i2o_dma struct DMA buffer
57 * @len: new length of memory
60 * If there was something allocated in the addr, free it first. If len > 0
61 * than try to allocate it and write the addresses back to the addr
62 * structure. If len == 0 set the virtual address to NULL.
64 * Returns the 0 on success or negative error code on failure.
66 int i2o_dma_realloc(struct device *dev, struct i2o_dma *addr, size_t len,
67 unsigned int gfp_mask)
69 i2o_dma_free(dev, addr);
72 return i2o_dma_alloc(dev, addr, len, gfp_mask);
78 * i2o_pci_free - Frees the DMA memory for the I2O controller
79 * @c: I2O controller to free
81 * Remove all allocated DMA memory and unmap memory IO regions. If MTRR
82 * is enabled, also remove it again.
84 static void i2o_pci_free(struct i2o_controller *c)
90 i2o_dma_free(dev, &c->out_queue);
91 i2o_dma_free(dev, &c->status_block);
94 i2o_dma_free(dev, &c->dlct);
95 i2o_dma_free(dev, &c->hrt);
96 i2o_dma_free(dev, &c->status);
98 if (c->raptor && c->in_queue.virt)
99 iounmap(c->in_queue.virt);
102 iounmap(c->base.virt);
106 * i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
109 * Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
110 * IO mappings are also done here. If MTRR is enabled, also do add memory
113 * Returns 0 on success or negative error code on failure.
115 static int __devinit i2o_pci_alloc(struct i2o_controller *c)
117 struct pci_dev *pdev = c->pdev;
118 struct device *dev = &pdev->dev;
121 for (i = 0; i < 6; i++) {
122 /* Skip I/O spaces */
123 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
125 c->base.phys = pci_resource_start(pdev, i);
126 c->base.len = pci_resource_len(pdev, i);
129 * If we know what card it is, set the size
130 * correctly. Code is taken from dpt_i2o.c
132 if (pdev->device == 0xa501) {
133 if (pdev->subsystem_device >= 0xc032 &&
134 pdev->subsystem_device <= 0xc03b) {
135 if (c->base.len > 0x400000)
136 c->base.len = 0x400000;
138 if (c->base.len > 0x100000)
139 c->base.len = 0x100000;
145 c->in_queue.phys = pci_resource_start(pdev, i);
146 c->in_queue.len = pci_resource_len(pdev, i);
153 printk(KERN_ERR "%s: I2O controller has no memory regions"
154 " defined.\n", c->name);
159 /* Map the I2O controller */
161 printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
162 printk(KERN_INFO " BAR0 at 0x%08lX size=%ld\n",
163 (unsigned long)c->base.phys, (unsigned long)c->base.len);
164 printk(KERN_INFO " BAR1 at 0x%08lX size=%ld\n",
165 (unsigned long)c->in_queue.phys,
166 (unsigned long)c->in_queue.len);
168 printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
169 c->name, (unsigned long)c->base.phys,
170 (unsigned long)c->base.len);
172 c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
174 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
180 ioremap_nocache(c->in_queue.phys, c->in_queue.len);
181 if (!c->in_queue.virt) {
182 printk(KERN_ERR "%s: Unable to map controller.\n",
188 c->in_queue = c->base;
190 c->irq_mask = c->base.virt + 0x34;
191 c->post_port = c->base.virt + 0x40;
192 c->reply_port = c->base.virt + 0x44;
194 if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
199 if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
204 if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
209 if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
215 if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
220 pci_set_drvdata(pdev, c);
226 * i2o_pci_interrupt - Interrupt handler for I2O controller
227 * @irq: interrupt line
228 * @dev_id: pointer to the I2O controller
229 * @r: pointer to registers
231 * Handle an interrupt from a PCI based I2O controller. This turns out
232 * to be rather simple. We keep the controller pointer in the cookie.
234 static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
236 struct i2o_controller *c = dev_id;
237 struct device *dev = &c->pdev->dev;
238 struct i2o_message *m;
242 * Old 960 steppings had a bug in the I2O unit that caused
243 * the queue to appear empty when it wasn't.
245 mv = I2O_REPLY_READ32(c);
246 if (mv == I2O_QUEUE_EMPTY) {
247 mv = I2O_REPLY_READ32(c);
248 if (unlikely(mv == I2O_QUEUE_EMPTY)) {
251 pr_debug("%s: 960 bug detected\n", c->name);
254 while (mv != I2O_QUEUE_EMPTY) {
256 * Map the message from the page frame map to kernel virtual.
257 * Because bus_to_virt is deprecated, we have calculate the
258 * location by ourself!
260 m = i2o_msg_out_to_virt(c, mv);
263 * Ensure this message is seen coherently but cachably by
266 dma_sync_single_for_cpu(dev, mv, MSG_FRAME_SIZE * 4,
270 if (i2o_driver_dispatch(c, mv, m))
271 /* flush it if result != 0 */
272 i2o_flush_reply(c, mv);
275 * That 960 bug again...
277 mv = I2O_REPLY_READ32(c);
278 if (mv == I2O_QUEUE_EMPTY)
279 mv = I2O_REPLY_READ32(c);
285 * i2o_pci_irq_enable - Allocate interrupt for I2O controller
287 * Allocate an interrupt for the I2O controller, and activate interrupts
288 * on the I2O controller.
290 * Returns 0 on success or negative error code on failure.
292 static int i2o_pci_irq_enable(struct i2o_controller *c)
294 struct pci_dev *pdev = c->pdev;
297 I2O_IRQ_WRITE32(c, 0xffffffff);
300 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
303 printk(KERN_ERR "%s: unable to allocate interrupt %d."
304 "\n", c->name, pdev->irq);
309 I2O_IRQ_WRITE32(c, 0x00000000);
311 printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
317 * i2o_pci_irq_disable - Free interrupt for I2O controller
320 * Disable interrupts in I2O controller and then free interrupt.
322 static void i2o_pci_irq_disable(struct i2o_controller *c)
324 I2O_IRQ_WRITE32(c, 0xffffffff);
326 if (c->pdev->irq > 0)
327 free_irq(c->pdev->irq, c);
331 * i2o_pci_probe - Probe the PCI device for an I2O controller
332 * @dev: PCI device to test
333 * @id: id which matched with the PCI device id table
335 * Probe the PCI device for any device which is a memory of the
336 * Intelligent, I2O class or an Adaptec Zero Channel Controller. We
337 * attempt to set up each such device and register it with the core.
339 * Returns 0 on success or negative error code on failure.
341 static int __devinit i2o_pci_probe(struct pci_dev *pdev,
342 const struct pci_device_id *id)
344 struct i2o_controller *c;
346 struct pci_dev *i960 = NULL;
348 printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
350 if ((pdev->class & 0xff) > 1) {
351 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
352 "(skipping).\n", pci_name(pdev));
356 if ((rc = pci_enable_device(pdev))) {
357 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
362 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
363 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
369 pci_set_master(pdev);
373 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
378 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
382 c->device = pdev->dev;
384 /* Cards that fall apart if you hit them with large I/O loads... */
385 if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
387 printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
391 if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
393 * Expose the ship behind i960 for initialization, or it will
397 pci_find_slot(c->pdev->bus->number,
398 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
401 pci_write_config_word(i960, 0x42, 0);
406 /* Cards that go bananas if you quiesce them before you reset them. */
407 if (pdev->vendor == PCI_VENDOR_ID_DPT) {
409 if (pdev->device == 0xa511)
413 if ((rc = i2o_pci_alloc(c))) {
414 printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
415 " failed\n", c->name);
416 goto free_controller;
419 if (i2o_pci_irq_enable(c)) {
420 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
421 "controller\n", c->name);
425 if ((rc = i2o_iop_add(c)))
429 pci_write_config_word(i960, 0x42, 0x03ff);
434 i2o_pci_irq_disable(c);
443 pci_disable_device(pdev);
449 * i2o_pci_remove - Removes a I2O controller from the system
450 * pdev: I2O controller which should be removed
452 * Reset the I2O controller, disable interrupts and remove all allocated
455 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
457 struct i2o_controller *c;
458 c = pci_get_drvdata(pdev);
461 i2o_pci_irq_disable(c);
464 printk(KERN_INFO "%s: Controller removed.\n", c->name);
467 pci_disable_device(pdev);
470 /* PCI driver for I2O controller */
471 static struct pci_driver i2o_pci_driver = {
472 .name = "I2O controller",
473 .id_table = i2o_pci_ids,
474 .probe = i2o_pci_probe,
475 .remove = __devexit_p(i2o_pci_remove),
479 * i2o_pci_init - registers I2O PCI driver in PCI subsystem
481 * Returns > 0 on success or negative error code on failure.
483 int __init i2o_pci_init(void)
485 return pci_register_driver(&i2o_pci_driver);
489 * i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
491 void __exit i2o_pci_exit(void)
493 pci_unregister_driver(&i2o_pci_driver);
496 EXPORT_SYMBOL(i2o_dma_realloc);
497 MODULE_DEVICE_TABLE(pci, i2o_pci_ids);