blob: 66c03e8825703a408b21c239ff5e4781fab71ffc [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>
Markus Lidel9e875452005-06-23 22:02:21 -070033#include "core.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Markus Lideldc9352a2005-08-09 14:30:57 -070035#define OSM_DESCRIPTION "I2O-subsystem"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* PCI device id table for all I2O controllers */
38static struct pci_device_id __devinitdata i2o_pci_ids[] = {
39 {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O << 8, 0xffff00)},
40 {PCI_DEVICE(PCI_VENDOR_ID_DPT, 0xa511)},
Markus Lidel61fbfa82005-06-23 22:02:11 -070041 {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
42 .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 {0}
44};
45
46/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * 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 */
53static void i2o_pci_free(struct i2o_controller *c)
54{
55 struct device *dev;
56
57 dev = &c->pdev->dev;
58
59 i2o_dma_free(dev, &c->out_queue);
60 i2o_dma_free(dev, &c->status_block);
Markus Lidelf88e1192005-06-23 22:02:14 -070061 kfree(c->lct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 i2o_dma_free(dev, &c->dlct);
63 i2o_dma_free(dev, &c->hrt);
64 i2o_dma_free(dev, &c->status);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (c->raptor && c->in_queue.virt)
67 iounmap(c->in_queue.virt);
68
69 if (c->base.virt)
70 iounmap(c->base.virt);
Markus Lideldc9352a2005-08-09 14:30:57 -070071
72 pci_release_regions(c->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
75/**
76 * i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
77 * @c: I2O controller
78 *
79 * Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
80 * IO mappings are also done here. If MTRR is enabled, also do add memory
81 * regions here.
82 *
83 * Returns 0 on success or negative error code on failure.
84 */
85static int __devinit i2o_pci_alloc(struct i2o_controller *c)
86{
87 struct pci_dev *pdev = c->pdev;
88 struct device *dev = &pdev->dev;
89 int i;
90
Markus Lideldc9352a2005-08-09 14:30:57 -070091 if (pci_request_regions(pdev, OSM_DESCRIPTION)) {
92 printk(KERN_ERR "%s: device already claimed\n", c->name);
93 return -ENODEV;
94 }
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 for (i = 0; i < 6; i++) {
97 /* Skip I/O spaces */
98 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
99 if (!c->base.phys) {
100 c->base.phys = pci_resource_start(pdev, i);
101 c->base.len = pci_resource_len(pdev, i);
102
103 /*
104 * If we know what card it is, set the size
105 * correctly. Code is taken from dpt_i2o.c
106 */
107 if (pdev->device == 0xa501) {
108 if (pdev->subsystem_device >= 0xc032 &&
109 pdev->subsystem_device <= 0xc03b) {
110 if (c->base.len > 0x400000)
111 c->base.len = 0x400000;
112 } else {
113 if (c->base.len > 0x100000)
114 c->base.len = 0x100000;
115 }
116 }
117 if (!c->raptor)
118 break;
119 } else {
120 c->in_queue.phys = pci_resource_start(pdev, i);
121 c->in_queue.len = pci_resource_len(pdev, i);
122 break;
123 }
124 }
125 }
126
127 if (i == 6) {
128 printk(KERN_ERR "%s: I2O controller has no memory regions"
129 " defined.\n", c->name);
130 i2o_pci_free(c);
131 return -EINVAL;
132 }
133
134 /* Map the I2O controller */
135 if (c->raptor) {
136 printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
137 printk(KERN_INFO " BAR0 at 0x%08lX size=%ld\n",
138 (unsigned long)c->base.phys, (unsigned long)c->base.len);
139 printk(KERN_INFO " BAR1 at 0x%08lX size=%ld\n",
140 (unsigned long)c->in_queue.phys,
141 (unsigned long)c->in_queue.len);
142 } else
143 printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
144 c->name, (unsigned long)c->base.phys,
145 (unsigned long)c->base.len);
146
Markus Lidel61fbfa82005-06-23 22:02:11 -0700147 c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!c->base.virt) {
149 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
Markus Lideldc9352a2005-08-09 14:30:57 -0700150 i2o_pci_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -ENOMEM;
152 }
153
154 if (c->raptor) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700155 c->in_queue.virt =
156 ioremap_nocache(c->in_queue.phys, c->in_queue.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!c->in_queue.virt) {
158 printk(KERN_ERR "%s: Unable to map controller.\n",
159 c->name);
160 i2o_pci_free(c);
161 return -ENOMEM;
162 }
163 } else
164 c->in_queue = c->base;
165
Markus Lidelf10378f2005-06-23 22:02:16 -0700166 c->irq_status = c->base.virt + I2O_IRQ_STATUS;
Markus Lidelf88e1192005-06-23 22:02:14 -0700167 c->irq_mask = c->base.virt + I2O_IRQ_MASK;
168 c->in_port = c->base.virt + I2O_IN_PORT;
169 c->out_port = c->base.virt + I2O_OUT_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
172 i2o_pci_free(c);
173 return -ENOMEM;
174 }
175
176 if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
177 i2o_pci_free(c);
178 return -ENOMEM;
179 }
180
181 if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
182 i2o_pci_free(c);
183 return -ENOMEM;
184 }
185
186 if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
187 GFP_KERNEL)) {
188 i2o_pci_free(c);
189 return -ENOMEM;
190 }
191
Markus Lidelf33213e2005-06-23 22:02:23 -0700192 if (i2o_dma_alloc
193 (dev, &c->out_queue,
194 I2O_MAX_OUTBOUND_MSG_FRAMES * I2O_OUTBOUND_MSG_FRAME_SIZE *
195 sizeof(u32), GFP_KERNEL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 i2o_pci_free(c);
197 return -ENOMEM;
198 }
199
200 pci_set_drvdata(pdev, c);
201
202 return 0;
203}
204
205/**
206 * i2o_pci_interrupt - Interrupt handler for I2O controller
207 * @irq: interrupt line
208 * @dev_id: pointer to the I2O controller
209 * @r: pointer to registers
210 *
211 * Handle an interrupt from a PCI based I2O controller. This turns out
212 * to be rather simple. We keep the controller pointer in the cookie.
213 */
214static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
215{
216 struct i2o_controller *c = dev_id;
Markus Lidelf10378f2005-06-23 22:02:16 -0700217 u32 m;
218 irqreturn_t rc = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Markus Lidelf10378f2005-06-23 22:02:16 -0700220 while (readl(c->irq_status) & I2O_IRQ_OUTBOUND_POST) {
221 m = readl(c->out_port);
222 if (m == I2O_QUEUE_EMPTY) {
223 /*
224 * Old 960 steppings had a bug in the I2O unit that
225 * caused the queue to appear empty when it wasn't.
226 */
227 m = readl(c->out_port);
228 if (unlikely(m == I2O_QUEUE_EMPTY))
229 break;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* dispatch it */
Markus Lidelf10378f2005-06-23 22:02:16 -0700233 if (i2o_driver_dispatch(c, m))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* flush it if result != 0 */
Markus Lidelf10378f2005-06-23 22:02:16 -0700235 i2o_flush_reply(c, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Markus Lidelf10378f2005-06-23 22:02:16 -0700237 rc = IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Markus Lidelf88e1192005-06-23 22:02:14 -0700239
Markus Lidelf10378f2005-06-23 22:02:16 -0700240 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/**
244 * i2o_pci_irq_enable - Allocate interrupt for I2O controller
245 *
246 * Allocate an interrupt for the I2O controller, and activate interrupts
247 * on the I2O controller.
248 *
249 * Returns 0 on success or negative error code on failure.
250 */
251static int i2o_pci_irq_enable(struct i2o_controller *c)
252{
253 struct pci_dev *pdev = c->pdev;
254 int rc;
255
Markus Lidelf88e1192005-06-23 22:02:14 -0700256 writel(0xffffffff, c->irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (pdev->irq) {
259 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
260 c->name, c);
261 if (rc < 0) {
262 printk(KERN_ERR "%s: unable to allocate interrupt %d."
263 "\n", c->name, pdev->irq);
264 return rc;
265 }
266 }
267
Markus Lidelf88e1192005-06-23 22:02:14 -0700268 writel(0x00000000, c->irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
271
272 return 0;
273}
274
275/**
276 * i2o_pci_irq_disable - Free interrupt for I2O controller
277 * @c: I2O controller
278 *
279 * Disable interrupts in I2O controller and then free interrupt.
280 */
281static void i2o_pci_irq_disable(struct i2o_controller *c)
282{
Markus Lidelf88e1192005-06-23 22:02:14 -0700283 writel(0xffffffff, c->irq_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (c->pdev->irq > 0)
286 free_irq(c->pdev->irq, c);
287}
288
289/**
290 * i2o_pci_probe - Probe the PCI device for an I2O controller
291 * @dev: PCI device to test
292 * @id: id which matched with the PCI device id table
293 *
294 * Probe the PCI device for any device which is a memory of the
295 * Intelligent, I2O class or an Adaptec Zero Channel Controller. We
296 * attempt to set up each such device and register it with the core.
297 *
298 * Returns 0 on success or negative error code on failure.
299 */
300static int __devinit i2o_pci_probe(struct pci_dev *pdev,
301 const struct pci_device_id *id)
302{
303 struct i2o_controller *c;
304 int rc;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700305 struct pci_dev *i960 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
308
309 if ((pdev->class & 0xff) > 1) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700310 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
311 "(skipping).\n", pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return -ENODEV;
313 }
314
315 if ((rc = pci_enable_device(pdev))) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700316 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
317 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return rc;
319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700322 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
323 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 rc = -ENODEV;
325 goto disable;
326 }
327
328 pci_set_master(pdev);
329
330 c = i2o_iop_alloc();
331 if (IS_ERR(c)) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700332 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
333 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rc = PTR_ERR(c);
335 goto disable;
Markus Lidel61fbfa82005-06-23 22:02:11 -0700336 } else
337 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
338 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 c->pdev = pdev;
Markus Lidelf88e1192005-06-23 22:02:14 -0700341 c->device.parent = get_device(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Cards that fall apart if you hit them with large I/O loads... */
344 if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
345 c->short_req = 1;
346 printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
347 c->name);
348 }
349
350 if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
Markus Lidel61fbfa82005-06-23 22:02:11 -0700351 /*
352 * Expose the ship behind i960 for initialization, or it will
353 * failed
354 */
355 i960 =
356 pci_find_slot(c->pdev->bus->number,
357 PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
358
359 if (i960)
360 pci_write_config_word(i960, 0x42, 0);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 c->promise = 1;
Markus Lidel718c3182005-06-23 22:02:24 -0700363 c->limit_sectors = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Markus Lidelb2aaee32005-06-23 22:02:19 -0700366 if (pdev->subsystem_vendor == PCI_VENDOR_ID_DPT)
367 c->adaptec = 1;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Cards that go bananas if you quiesce them before you reset them. */
370 if (pdev->vendor == PCI_VENDOR_ID_DPT) {
371 c->no_quiesce = 1;
372 if (pdev->device == 0xa511)
373 c->raptor = 1;
Markus Lidelb2aaee32005-06-23 22:02:19 -0700374
375 if (pdev->subsystem_device == 0xc05a) {
376 c->limit_sectors = 1;
377 printk(KERN_INFO
378 "%s: limit sectors per request to %d\n", c->name,
379 I2O_MAX_SECTORS_LIMITED);
380 }
381#ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64
382 if (sizeof(dma_addr_t) > 4) {
383 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK))
384 printk(KERN_INFO "%s: 64-bit DMA unavailable\n",
385 c->name);
386 else {
387 c->pae_support = 1;
388 printk(KERN_INFO "%s: using 64-bit DMA\n",
389 c->name);
390 }
391 }
392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 if ((rc = i2o_pci_alloc(c))) {
396 printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
397 " failed\n", c->name);
398 goto free_controller;
399 }
400
401 if (i2o_pci_irq_enable(c)) {
402 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
403 "controller\n", c->name);
404 goto free_pci;
405 }
406
407 if ((rc = i2o_iop_add(c)))
408 goto uninstall;
409
Markus Lidel9e875452005-06-23 22:02:21 -0700410 get_device(&c->device);
411
Markus Lidel61fbfa82005-06-23 22:02:11 -0700412 if (i960)
413 pci_write_config_word(i960, 0x42, 0x03ff);
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416
417 uninstall:
418 i2o_pci_irq_disable(c);
419
420 free_pci:
421 i2o_pci_free(c);
422
423 free_controller:
424 i2o_iop_free(c);
Markus Lidelf88e1192005-06-23 22:02:14 -0700425 put_device(c->device.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 disable:
428 pci_disable_device(pdev);
429
430 return rc;
431}
432
433/**
434 * i2o_pci_remove - Removes a I2O controller from the system
435 * pdev: I2O controller which should be removed
436 *
437 * Reset the I2O controller, disable interrupts and remove all allocated
438 * resources.
439 */
440static void __devexit i2o_pci_remove(struct pci_dev *pdev)
441{
442 struct i2o_controller *c;
443 c = pci_get_drvdata(pdev);
444
445 i2o_iop_remove(c);
446 i2o_pci_irq_disable(c);
447 i2o_pci_free(c);
448
Markus Lidelf88e1192005-06-23 22:02:14 -0700449 pci_disable_device(pdev);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 printk(KERN_INFO "%s: Controller removed.\n", c->name);
452
Markus Lidelf88e1192005-06-23 22:02:14 -0700453 put_device(c->device.parent);
454 put_device(&c->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
457/* PCI driver for I2O controller */
458static struct pci_driver i2o_pci_driver = {
Markus Lidelf88e1192005-06-23 22:02:14 -0700459 .name = "PCI_I2O",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 .id_table = i2o_pci_ids,
461 .probe = i2o_pci_probe,
462 .remove = __devexit_p(i2o_pci_remove),
463};
464
465/**
466 * i2o_pci_init - registers I2O PCI driver in PCI subsystem
467 *
468 * Returns > 0 on success or negative error code on failure.
469 */
470int __init i2o_pci_init(void)
471{
472 return pci_register_driver(&i2o_pci_driver);
473};
474
475/**
476 * i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
477 */
478void __exit i2o_pci_exit(void)
479{
480 pci_unregister_driver(&i2o_pci_driver);
481};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482MODULE_DEVICE_TABLE(pci, i2o_pci_ids);