964fe481849e500261dc99600ca6cd16e3867dbe
[safe/jmp/linux-2.6] / drivers / message / i2o / pci.c
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
30 #include <linux/pci.h>
31 #include <linux/interrupt.h>
32 #include <linux/i2o.h>
33
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 *);
37
38 extern int i2o_iop_add(struct i2o_controller *);
39 extern void i2o_iop_remove(struct i2o_controller *);
40
41 extern int i2o_driver_dispatch(struct i2o_controller *, u32);
42
43 /* PCI device id table for all I2O controllers */
44 static 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)},
47         {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
48          .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
49         {0}
50 };
51
52 /**
53  *      i2o_pci_free - Frees the DMA memory for the I2O controller
54  *      @c: I2O controller to free
55  *
56  *      Remove all allocated DMA memory and unmap memory IO regions. If MTRR
57  *      is enabled, also remove it again.
58  */
59 static void i2o_pci_free(struct i2o_controller *c)
60 {
61         struct device *dev;
62
63         dev = &c->pdev->dev;
64
65         i2o_dma_free(dev, &c->out_queue);
66         i2o_dma_free(dev, &c->status_block);
67         kfree(c->lct);
68         i2o_dma_free(dev, &c->dlct);
69         i2o_dma_free(dev, &c->hrt);
70         i2o_dma_free(dev, &c->status);
71
72         if (c->raptor && c->in_queue.virt)
73                 iounmap(c->in_queue.virt);
74
75         if (c->base.virt)
76                 iounmap(c->base.virt);
77 }
78
79 /**
80  *      i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
81  *      @c: I2O controller
82  *
83  *      Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
84  *      IO mappings are also done here. If MTRR is enabled, also do add memory
85  *      regions here.
86  *
87  *      Returns 0 on success or negative error code on failure.
88  */
89 static int __devinit i2o_pci_alloc(struct i2o_controller *c)
90 {
91         struct pci_dev *pdev = c->pdev;
92         struct device *dev = &pdev->dev;
93         int i;
94
95         for (i = 0; i < 6; i++) {
96                 /* Skip I/O spaces */
97                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
98                         if (!c->base.phys) {
99                                 c->base.phys = pci_resource_start(pdev, i);
100                                 c->base.len = pci_resource_len(pdev, i);
101
102                                 /*
103                                  * If we know what card it is, set the size
104                                  * correctly. Code is taken from dpt_i2o.c
105                                  */
106                                 if (pdev->device == 0xa501) {
107                                         if (pdev->subsystem_device >= 0xc032 &&
108                                             pdev->subsystem_device <= 0xc03b) {
109                                                 if (c->base.len > 0x400000)
110                                                         c->base.len = 0x400000;
111                                         } else {
112                                                 if (c->base.len > 0x100000)
113                                                         c->base.len = 0x100000;
114                                         }
115                                 }
116                                 if (!c->raptor)
117                                         break;
118                         } else {
119                                 c->in_queue.phys = pci_resource_start(pdev, i);
120                                 c->in_queue.len = pci_resource_len(pdev, i);
121                                 break;
122                         }
123                 }
124         }
125
126         if (i == 6) {
127                 printk(KERN_ERR "%s: I2O controller has no memory regions"
128                        " defined.\n", c->name);
129                 i2o_pci_free(c);
130                 return -EINVAL;
131         }
132
133         /* Map the I2O controller */
134         if (c->raptor) {
135                 printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
136                 printk(KERN_INFO "     BAR0 at 0x%08lX size=%ld\n",
137                        (unsigned long)c->base.phys, (unsigned long)c->base.len);
138                 printk(KERN_INFO "     BAR1 at 0x%08lX size=%ld\n",
139                        (unsigned long)c->in_queue.phys,
140                        (unsigned long)c->in_queue.len);
141         } else
142                 printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
143                        c->name, (unsigned long)c->base.phys,
144                        (unsigned long)c->base.len);
145
146         c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
147         if (!c->base.virt) {
148                 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
149                 return -ENOMEM;
150         }
151
152         if (c->raptor) {
153                 c->in_queue.virt =
154                     ioremap_nocache(c->in_queue.phys, c->in_queue.len);
155                 if (!c->in_queue.virt) {
156                         printk(KERN_ERR "%s: Unable to map controller.\n",
157                                c->name);
158                         i2o_pci_free(c);
159                         return -ENOMEM;
160                 }
161         } else
162                 c->in_queue = c->base;
163
164         c->irq_status = c->base.virt + I2O_IRQ_STATUS;
165         c->irq_mask = c->base.virt + I2O_IRQ_MASK;
166         c->in_port = c->base.virt + I2O_IN_PORT;
167         c->out_port = c->base.virt + I2O_OUT_PORT;
168
169         if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
170                 i2o_pci_free(c);
171                 return -ENOMEM;
172         }
173
174         if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
175                 i2o_pci_free(c);
176                 return -ENOMEM;
177         }
178
179         if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
180                 i2o_pci_free(c);
181                 return -ENOMEM;
182         }
183
184         if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
185                           GFP_KERNEL)) {
186                 i2o_pci_free(c);
187                 return -ENOMEM;
188         }
189
190         if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
191                 i2o_pci_free(c);
192                 return -ENOMEM;
193         }
194
195         pci_set_drvdata(pdev, c);
196
197         return 0;
198 }
199
200 /**
201  *      i2o_pci_interrupt - Interrupt handler for I2O controller
202  *      @irq: interrupt line
203  *      @dev_id: pointer to the I2O controller
204  *      @r: pointer to registers
205  *
206  *      Handle an interrupt from a PCI based I2O controller. This turns out
207  *      to be rather simple. We keep the controller pointer in the cookie.
208  */
209 static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
210 {
211         struct i2o_controller *c = dev_id;
212         u32 m;
213         irqreturn_t rc = IRQ_NONE;
214
215         while (readl(c->irq_status) & I2O_IRQ_OUTBOUND_POST) {
216                 m = readl(c->out_port);
217                 if (m == I2O_QUEUE_EMPTY) {
218                         /*
219                          * Old 960 steppings had a bug in the I2O unit that
220                          * caused the queue to appear empty when it wasn't.
221                          */
222                         m = readl(c->out_port);
223                         if (unlikely(m == I2O_QUEUE_EMPTY))
224                                 break;
225                 }
226
227                 /* dispatch it */
228                 if (i2o_driver_dispatch(c, m))
229                         /* flush it if result != 0 */
230                         i2o_flush_reply(c, m);
231
232                 rc = IRQ_HANDLED;
233         }
234
235         return rc;
236 }
237
238 /**
239  *      i2o_pci_irq_enable - Allocate interrupt for I2O controller
240  *
241  *      Allocate an interrupt for the I2O controller, and activate interrupts
242  *      on the I2O controller.
243  *
244  *      Returns 0 on success or negative error code on failure.
245  */
246 static int i2o_pci_irq_enable(struct i2o_controller *c)
247 {
248         struct pci_dev *pdev = c->pdev;
249         int rc;
250
251         wmb();
252         writel(0xffffffff, c->irq_mask);
253         wmb();
254
255         if (pdev->irq) {
256                 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
257                                  c->name, c);
258                 if (rc < 0) {
259                         printk(KERN_ERR "%s: unable to allocate interrupt %d."
260                                "\n", c->name, pdev->irq);
261                         return rc;
262                 }
263         }
264
265         writel(0x00000000, c->irq_mask);
266         wmb();
267
268         printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
269
270         return 0;
271 }
272
273 /**
274  *      i2o_pci_irq_disable - Free interrupt for I2O controller
275  *      @c: I2O controller
276  *
277  *      Disable interrupts in I2O controller and then free interrupt.
278  */
279 static void i2o_pci_irq_disable(struct i2o_controller *c)
280 {
281         wmb();
282         writel(0xffffffff, c->irq_mask);
283         wmb();
284
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  */
300 static 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;
305         struct pci_dev *i960 = NULL;
306
307         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
308
309         if ((pdev->class & 0xff) > 1) {
310                 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
311                        "(skipping).\n", pci_name(pdev));
312                 return -ENODEV;
313         }
314
315         if ((rc = pci_enable_device(pdev))) {
316                 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
317                        pci_name(pdev));
318                 return rc;
319         }
320
321         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
322                 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
323                        pci_name(pdev));
324                 rc = -ENODEV;
325                 goto disable;
326         }
327
328         pci_set_master(pdev);
329
330         c = i2o_iop_alloc();
331         if (IS_ERR(c)) {
332                 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
333                        pci_name(pdev));
334                 rc = PTR_ERR(c);
335                 goto disable;
336         } else
337                 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
338                        pci_name(pdev));
339
340         c->pdev = pdev;
341         c->device.parent = get_device(&pdev->dev);
342
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) {
351                 /*
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
362                 c->promise = 1;
363         }
364
365         if (pdev->subsystem_vendor == PCI_VENDOR_ID_DPT)
366                 c->adaptec = 1;
367
368         /* Cards that go bananas if you quiesce them before you reset them. */
369         if (pdev->vendor == PCI_VENDOR_ID_DPT) {
370                 c->no_quiesce = 1;
371                 if (pdev->device == 0xa511)
372                         c->raptor = 1;
373
374                 if (pdev->subsystem_device == 0xc05a) {
375                         c->limit_sectors = 1;
376                         printk(KERN_INFO
377                                "%s: limit sectors per request to %d\n", c->name,
378                                I2O_MAX_SECTORS_LIMITED);
379                 }
380 #ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64
381                 if (sizeof(dma_addr_t) > 4) {
382                         if (pci_set_dma_mask(pdev, DMA_64BIT_MASK))
383                                 printk(KERN_INFO "%s: 64-bit DMA unavailable\n",
384                                        c->name);
385                         else {
386                                 c->pae_support = 1;
387                                 printk(KERN_INFO "%s: using 64-bit DMA\n",
388                                        c->name);
389                         }
390                 }
391 #endif
392         }
393
394         if ((rc = i2o_pci_alloc(c))) {
395                 printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
396                        " failed\n", c->name);
397                 goto free_controller;
398         }
399
400         if (i2o_pci_irq_enable(c)) {
401                 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
402                        "controller\n", c->name);
403                 goto free_pci;
404         }
405
406         if ((rc = i2o_iop_add(c)))
407                 goto uninstall;
408
409         if (i960)
410                 pci_write_config_word(i960, 0x42, 0x03ff);
411
412         get_device(&c->device);
413
414         return 0;
415
416       uninstall:
417         i2o_pci_irq_disable(c);
418
419       free_pci:
420         i2o_pci_free(c);
421
422       free_controller:
423         i2o_iop_free(c);
424         put_device(c->device.parent);
425
426       disable:
427         pci_disable_device(pdev);
428
429         return rc;
430 }
431
432 /**
433  *      i2o_pci_remove - Removes a I2O controller from the system
434  *      pdev: I2O controller which should be removed
435  *
436  *      Reset the I2O controller, disable interrupts and remove all allocated
437  *      resources.
438  */
439 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
440 {
441         struct i2o_controller *c;
442         c = pci_get_drvdata(pdev);
443
444         i2o_iop_remove(c);
445         i2o_pci_irq_disable(c);
446         i2o_pci_free(c);
447
448         pci_disable_device(pdev);
449
450         printk(KERN_INFO "%s: Controller removed.\n", c->name);
451
452         put_device(c->device.parent);
453         put_device(&c->device);
454 };
455
456 /* PCI driver for I2O controller */
457 static struct pci_driver i2o_pci_driver = {
458         .name = "PCI_I2O",
459         .id_table = i2o_pci_ids,
460         .probe = i2o_pci_probe,
461         .remove = __devexit_p(i2o_pci_remove),
462 };
463
464 /**
465  *      i2o_pci_init - registers I2O PCI driver in PCI subsystem
466  *
467  *      Returns > 0 on success or negative error code on failure.
468  */
469 int __init i2o_pci_init(void)
470 {
471         return pci_register_driver(&i2o_pci_driver);
472 };
473
474 /**
475  *      i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
476  */
477 void __exit i2o_pci_exit(void)
478 {
479         pci_unregister_driver(&i2o_pci_driver);
480 };
481
482 EXPORT_SYMBOL(i2o_dma_realloc);
483 MODULE_DEVICE_TABLE(pci, i2o_pci_ids);