blob: f33fd81f77a464d37fd6022bdfaa85ec3b338de6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
30#include <linux/pci.h>
31#include <linux/interrupt.h>
32#include <linux/i2o.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Module internal functions from other sources */
35extern struct i2o_controller *i2o_iop_alloc(void);
36extern void i2o_iop_free(struct i2o_controller *);
37
38extern int i2o_iop_add(struct i2o_controller *);
39extern void i2o_iop_remove(struct i2o_controller *);
40
Markus Lidelf88e1192005-06-23 22:02:14 -070041extern int i2o_driver_dispatch(struct i2o_controller *, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* PCI device id table for all I2O controllers */
44static struct pci_device_id __devinitdata i2o_pci_ids[] = {
45 {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O << 8, 0xffff00)},
46 {PCI_DEVICE(PCI_VENDOR_ID_DPT, 0xa511)},
Markus Lidel61fbfa82005-06-23 22:02:11 -070047 {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
48 .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 {0}
50};
51
52/**
53 * i2o_dma_realloc - Realloc DMA memory
54 * @dev: struct device pointer to the PCI device of the I2O controller
55 * @addr: pointer to a i2o_dma struct DMA buffer
56 * @len: new length of memory
57 * @gfp_mask: GFP mask
58 *
59 * If there was something allocated in the addr, free it first. If len > 0
60 * than try to allocate it and write the addresses back to the addr
61 * structure. If len == 0 set the virtual address to NULL.
62 *
63 * Returns the 0 on success or negative error code on failure.
64 */
65int i2o_dma_realloc(struct device *dev, struct i2o_dma *addr, size_t len,
66 unsigned int gfp_mask)
67{
68 i2o_dma_free(dev, addr);
69
70 if (len)
71 return i2o_dma_alloc(dev, addr, len, gfp_mask);
72
73 return 0;
74};
75
76/**
77 * i2o_pci_free - Frees the DMA memory for the I2O controller
78 * @c: I2O controller to free
79 *
80 * Remove all allocated DMA memory and unmap memory IO regions. If MTRR
81 * is enabled, also remove it again.
82 */
83static void i2o_pci_free(struct i2o_controller *c)
84{
85 struct device *dev;
86
87 dev = &c->pdev->dev;
88
89 i2o_dma_free(dev, &c->out_queue);
90 i2o_dma_free(dev, &c->status_block);
Markus Lidelf88e1192005-06-23 22:02:14 -070091 kfree(c->lct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 i2o_dma_free(dev, &c->dlct);
93 i2o_dma_free(dev, &c->hrt);
94 i2o_dma_free(dev, &c->status);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (c->raptor && c->in_queue.virt)
97 iounmap(c->in_queue.virt);
98
99 if (c->base.virt)
100 iounmap(c->base.virt);
101}
102
103/**
104 * i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
105 * @c: I2O controller
106 *
107 * Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
108 * IO mappings are also done here. If MTRR is enabled, also do add memory
109 * regions here.
110 *
111 * Returns 0 on success or negative error code on failure.
112 */
113static int __devinit i2o_pci_alloc(struct i2o_controller *c)
114{
115 struct pci_dev *pdev = c->pdev;
116 struct device *dev = &pdev->dev;
117 int i;
118
119 for (i = 0; i < 6; i++) {
120 /* Skip I/O spaces */
121 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
122 if (!c->base.phys) {
123 c->base.phys = pci_resource_start(pdev, i);
124 c->base.len = pci_resource_len(pdev, i);
125
126 /*
127 * If we know what card it is, set the size
128 * correctly. Code is taken from dpt_i2o.c
129 */
130 if (pdev->device == 0xa501) {
131 if (pdev->subsystem_device >= 0xc032 &&
132 pdev->subsystem_device <= 0xc03b) {
133 if (c->base.len > 0x400000)
134 c->base.len = 0x400000;
135 } else {
136 if (c->base.len > 0x100000)
137 c->base.len = 0x100000;
138 }
139 }
140 if (!c->raptor)
141 break;
142 } else {
143 c->in_queue.phys = pci_resource_start(pdev, i);
144 c->in_queue.len = pci_resource_len(pdev, i);
145 break;
146 }
147 }
148 }
149
150 if (i == 6) {
151 printk(KERN_ERR "%s: I2O controller has no memory regions"
152 " defined.\n", c->name);
153 i2o_pci_free(c);
154 return -EINVAL;
155 }
156
157 /* Map the I2O controller */
158 if (c->raptor) {
159 printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
160 printk(KERN_INFO " BAR0 at 0x%08lX size=%ld\n",
161 (unsigned long)c->base.phys, (unsigned long)c->base.len);
162 printk(KERN_INFO " BAR1 at 0x%08lX size=%ld\n",
163 (unsigned long)c->in_queue.phys,
164 (unsigned long)c->in_queue.len);
165 } else
166 printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
167 c->name, (unsigned long)c->base.phys,
168 (unsigned long)c->base.len);
169
Markus Lidel61fbfa82005-06-23 22:02:11 -0700170 c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (!c->base.virt) {
172 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
173 return -ENOMEM;
174 }
175
176 if (c->raptor) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700177 c->in_queue.virt =
178 ioremap_nocache(c->in_queue.phys, c->in_queue.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!c->in_queue.virt) {
180 printk(KERN_ERR "%s: Unable to map controller.\n",
181 c->name);
182 i2o_pci_free(c);
183 return -ENOMEM;
184 }
185 } else
186 c->in_queue = c->base;
187
Markus Lidelf88e1192005-06-23 22:02:14 -0700188 c->irq_mask = c->base.virt + I2O_IRQ_MASK;
189 c->in_port = c->base.virt + I2O_IN_PORT;
190 c->out_port = c->base.virt + I2O_OUT_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
193 i2o_pci_free(c);
194 return -ENOMEM;
195 }
196
197 if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
198 i2o_pci_free(c);
199 return -ENOMEM;
200 }
201
202 if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
203 i2o_pci_free(c);
204 return -ENOMEM;
205 }
206
207 if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
208 GFP_KERNEL)) {
209 i2o_pci_free(c);
210 return -ENOMEM;
211 }
212
213 if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
214 i2o_pci_free(c);
215 return -ENOMEM;
216 }
217
218 pci_set_drvdata(pdev, c);
219
220 return 0;
221}
222
223/**
224 * i2o_pci_interrupt - Interrupt handler for I2O controller
225 * @irq: interrupt line
226 * @dev_id: pointer to the I2O controller
227 * @r: pointer to registers
228 *
229 * Handle an interrupt from a PCI based I2O controller. This turns out
230 * to be rather simple. We keep the controller pointer in the cookie.
231 */
232static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
233{
234 struct i2o_controller *c = dev_id;
235 struct device *dev = &c->pdev->dev;
Markus Lidelf88e1192005-06-23 22:02:14 -0700236 u32 mv = readl(c->out_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /*
239 * Old 960 steppings had a bug in the I2O unit that caused
240 * the queue to appear empty when it wasn't.
241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (mv == I2O_QUEUE_EMPTY) {
Markus Lidelf88e1192005-06-23 22:02:14 -0700243 mv = readl(c->out_port);
244 if (unlikely(mv == I2O_QUEUE_EMPTY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return IRQ_NONE;
Markus Lidelf88e1192005-06-23 22:02:14 -0700246 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pr_debug("%s: 960 bug detected\n", c->name);
248 }
249
250 while (mv != I2O_QUEUE_EMPTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* dispatch it */
Markus Lidelf88e1192005-06-23 22:02:14 -0700252 if (i2o_driver_dispatch(c, mv))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* flush it if result != 0 */
254 i2o_flush_reply(c, mv);
255
256 /*
257 * That 960 bug again...
258 */
Markus Lidelf88e1192005-06-23 22:02:14 -0700259 mv = readl(c->out_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (mv == I2O_QUEUE_EMPTY)
Markus Lidelf88e1192005-06-23 22:02:14 -0700261 mv = readl(c->out_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Markus Lidelf88e1192005-06-23 22:02:14 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return IRQ_HANDLED;
265}
266
267/**
268 * i2o_pci_irq_enable - Allocate interrupt for I2O controller
269 *
270 * Allocate an interrupt for the I2O controller, and activate interrupts
271 * on the I2O controller.
272 *
273 * Returns 0 on success or negative error code on failure.
274 */
275static int i2o_pci_irq_enable(struct i2o_controller *c)
276{
277 struct pci_dev *pdev = c->pdev;
278 int rc;
279
Markus Lidelf88e1192005-06-23 22:02:14 -0700280 wmb();
281 writel(0xffffffff, c->irq_mask);
282 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 if (pdev->irq) {
285 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
286 c->name, c);
287 if (rc < 0) {
288 printk(KERN_ERR "%s: unable to allocate interrupt %d."
289 "\n", c->name, pdev->irq);
290 return rc;
291 }
292 }
293
Markus Lidelf88e1192005-06-23 22:02:14 -0700294 writel(0x00000000, c->irq_mask);
295 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
298
299 return 0;
300}
301
302/**
303 * i2o_pci_irq_disable - Free interrupt for I2O controller
304 * @c: I2O controller
305 *
306 * Disable interrupts in I2O controller and then free interrupt.
307 */
308static void i2o_pci_irq_disable(struct i2o_controller *c)
309{
Markus Lidelf88e1192005-06-23 22:02:14 -0700310 wmb();
311 writel(0xffffffff, c->irq_mask);
312 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 if (c->pdev->irq > 0)
315 free_irq(c->pdev->irq, c);
316}
317
318/**
319 * i2o_pci_probe - Probe the PCI device for an I2O controller
320 * @dev: PCI device to test
321 * @id: id which matched with the PCI device id table
322 *
323 * Probe the PCI device for any device which is a memory of the
324 * Intelligent, I2O class or an Adaptec Zero Channel Controller. We
325 * attempt to set up each such device and register it with the core.
326 *
327 * Returns 0 on success or negative error code on failure.
328 */
329static int __devinit i2o_pci_probe(struct pci_dev *pdev,
330 const struct pci_device_id *id)
331{
332 struct i2o_controller *c;
333 int rc;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700334 struct pci_dev *i960 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
337
338 if ((pdev->class & 0xff) > 1) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700339 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
340 "(skipping).\n", pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -ENODEV;
342 }
343
344 if ((rc = pci_enable_device(pdev))) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700345 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
346 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return rc;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700351 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
352 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 rc = -ENODEV;
354 goto disable;
355 }
356
357 pci_set_master(pdev);
358
359 c = i2o_iop_alloc();
360 if (IS_ERR(c)) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700361 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
362 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 rc = PTR_ERR(c);
364 goto disable;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700365 } else
366 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
367 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 c->pdev = pdev;
Markus Lidelf88e1192005-06-23 22:02:14 -0700370 c->device.parent = get_device(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Cards that fall apart if you hit them with large I/O loads... */
373 if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
374 c->short_req = 1;
375 printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
376 c->name);
377 }
378
379 if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700380 /*
381 * Expose the ship behind i960 for initialization, or it will
382 * failed
383 */
384 i960 =
385 pci_find_slot(c->pdev->bus->number,
386 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
387
388 if (i960)
389 pci_write_config_word(i960, 0x42, 0);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 c->promise = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 /* Cards that go bananas if you quiesce them before you reset them. */
395 if (pdev->vendor == PCI_VENDOR_ID_DPT) {
396 c->no_quiesce = 1;
397 if (pdev->device == 0xa511)
398 c->raptor = 1;
399 }
400
401 if ((rc = i2o_pci_alloc(c))) {
402 printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
403 " failed\n", c->name);
404 goto free_controller;
405 }
406
407 if (i2o_pci_irq_enable(c)) {
408 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
409 "controller\n", c->name);
410 goto free_pci;
411 }
412
413 if ((rc = i2o_iop_add(c)))
414 goto uninstall;
415
Markus Lidel61fbfa82005-06-23 22:02:11 -0700416 if (i960)
417 pci_write_config_word(i960, 0x42, 0x03ff);
418
Markus Lidelf88e1192005-06-23 22:02:14 -0700419 get_device(&c->device);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422
423 uninstall:
424 i2o_pci_irq_disable(c);
425
426 free_pci:
427 i2o_pci_free(c);
428
429 free_controller:
430 i2o_iop_free(c);
Markus Lidelf88e1192005-06-23 22:02:14 -0700431 put_device(c->device.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 disable:
434 pci_disable_device(pdev);
435
436 return rc;
437}
438
439/**
440 * i2o_pci_remove - Removes a I2O controller from the system
441 * pdev: I2O controller which should be removed
442 *
443 * Reset the I2O controller, disable interrupts and remove all allocated
444 * resources.
445 */
446static void __devexit i2o_pci_remove(struct pci_dev *pdev)
447{
448 struct i2o_controller *c;
449 c = pci_get_drvdata(pdev);
450
451 i2o_iop_remove(c);
452 i2o_pci_irq_disable(c);
453 i2o_pci_free(c);
454
Markus Lidelf88e1192005-06-23 22:02:14 -0700455 pci_disable_device(pdev);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printk(KERN_INFO "%s: Controller removed.\n", c->name);
458
Markus Lidelf88e1192005-06-23 22:02:14 -0700459 put_device(c->device.parent);
460 put_device(&c->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
463/* PCI driver for I2O controller */
464static struct pci_driver i2o_pci_driver = {
Markus Lidelf88e1192005-06-23 22:02:14 -0700465 .name = "PCI_I2O",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 .id_table = i2o_pci_ids,
467 .probe = i2o_pci_probe,
468 .remove = __devexit_p(i2o_pci_remove),
469};
470
471/**
472 * i2o_pci_init - registers I2O PCI driver in PCI subsystem
473 *
474 * Returns > 0 on success or negative error code on failure.
475 */
476int __init i2o_pci_init(void)
477{
478 return pci_register_driver(&i2o_pci_driver);
479};
480
481/**
482 * i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
483 */
484void __exit i2o_pci_exit(void)
485{
486 pci_unregister_driver(&i2o_pci_driver);
487};
488
489EXPORT_SYMBOL(i2o_dma_realloc);
490MODULE_DEVICE_TABLE(pci, i2o_pci_ids);