]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/message/i2o/pci.c
579a8b7a2120b0d02cb793d741f4ec6ba0dfd2eb
[linux-2.6.git] / 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                                struct i2o_message *);
43
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},
50         {0}
51 };
52
53 /**
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
58  *      @gfp_mask: GFP mask
59  *
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.
63  *
64  *      Returns the 0 on success or negative error code on failure.
65  */
66 int i2o_dma_realloc(struct device *dev, struct i2o_dma *addr, size_t len,
67                     unsigned int gfp_mask)
68 {
69         i2o_dma_free(dev, addr);
70
71         if (len)
72                 return i2o_dma_alloc(dev, addr, len, gfp_mask);
73
74         return 0;
75 };
76
77 /**
78  *      i2o_pci_free - Frees the DMA memory for the I2O controller
79  *      @c: I2O controller to free
80  *
81  *      Remove all allocated DMA memory and unmap memory IO regions. If MTRR
82  *      is enabled, also remove it again.
83  */
84 static void i2o_pci_free(struct i2o_controller *c)
85 {
86         struct device *dev;
87
88         dev = &c->pdev->dev;
89
90         i2o_dma_free(dev, &c->out_queue);
91         i2o_dma_free(dev, &c->status_block);
92         if (c->lct)
93                 kfree(c->lct);
94         i2o_dma_free(dev, &c->dlct);
95         i2o_dma_free(dev, &c->hrt);
96         i2o_dma_free(dev, &c->status);
97
98         if (c->raptor && c->in_queue.virt)
99                 iounmap(c->in_queue.virt);
100
101         if (c->base.virt)
102                 iounmap(c->base.virt);
103 }
104
105 /**
106  *      i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
107  *      @c: I2O controller
108  *
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
111  *      regions here.
112  *
113  *      Returns 0 on success or negative error code on failure.
114  */
115 static int __devinit i2o_pci_alloc(struct i2o_controller *c)
116 {
117         struct pci_dev *pdev = c->pdev;
118         struct device *dev = &pdev->dev;
119         int i;
120
121         for (i = 0; i < 6; i++) {
122                 /* Skip I/O spaces */
123                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
124                         if (!c->base.phys) {
125                                 c->base.phys = pci_resource_start(pdev, i);
126                                 c->base.len = pci_resource_len(pdev, i);
127
128                                 /*
129                                  * If we know what card it is, set the size
130                                  * correctly. Code is taken from dpt_i2o.c
131                                  */
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;
137                                         } else {
138                                                 if (c->base.len > 0x100000)
139                                                         c->base.len = 0x100000;
140                                         }
141                                 }
142                                 if (!c->raptor)
143                                         break;
144                         } else {
145                                 c->in_queue.phys = pci_resource_start(pdev, i);
146                                 c->in_queue.len = pci_resource_len(pdev, i);
147                                 break;
148                         }
149                 }
150         }
151
152         if (i == 6) {
153                 printk(KERN_ERR "%s: I2O controller has no memory regions"
154                        " defined.\n", c->name);
155                 i2o_pci_free(c);
156                 return -EINVAL;
157         }
158
159         /* Map the I2O controller */
160         if (c->raptor) {
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);
167         } else
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);
171
172         c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
173         if (!c->base.virt) {
174                 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
175                 return -ENOMEM;
176         }
177
178         if (c->raptor) {
179                 c->in_queue.virt =
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",
183                                c->name);
184                         i2o_pci_free(c);
185                         return -ENOMEM;
186                 }
187         } else
188                 c->in_queue = c->base;
189
190         c->irq_mask = c->base.virt + 0x34;
191         c->post_port = c->base.virt + 0x40;
192         c->reply_port = c->base.virt + 0x44;
193
194         if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
195                 i2o_pci_free(c);
196                 return -ENOMEM;
197         }
198
199         if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
200                 i2o_pci_free(c);
201                 return -ENOMEM;
202         }
203
204         if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
205                 i2o_pci_free(c);
206                 return -ENOMEM;
207         }
208
209         if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
210                           GFP_KERNEL)) {
211                 i2o_pci_free(c);
212                 return -ENOMEM;
213         }
214
215         if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
216                 i2o_pci_free(c);
217                 return -ENOMEM;
218         }
219
220         pci_set_drvdata(pdev, c);
221
222         return 0;
223 }
224
225 /**
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
230  *
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.
233  */
234 static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
235 {
236         struct i2o_controller *c = dev_id;
237         struct device *dev = &c->pdev->dev;
238         struct i2o_message *m;
239         u32 mv;
240
241         /*
242          * Old 960 steppings had a bug in the I2O unit that caused
243          * the queue to appear empty when it wasn't.
244          */
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)) {
249                         return IRQ_NONE;
250                 } else
251                         pr_debug("%s: 960 bug detected\n", c->name);
252         }
253
254         while (mv != I2O_QUEUE_EMPTY) {
255                 /*
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!
259                  */
260                 m = i2o_msg_out_to_virt(c, mv);
261
262                 /*
263                  *      Ensure this message is seen coherently but cachably by
264                  *      the processor
265                  */
266                 dma_sync_single_for_cpu(dev, mv, MSG_FRAME_SIZE * 4,
267                                         PCI_DMA_FROMDEVICE);
268
269                 /* dispatch it */
270                 if (i2o_driver_dispatch(c, mv, m))
271                         /* flush it if result != 0 */
272                         i2o_flush_reply(c, mv);
273
274                 /*
275                  * That 960 bug again...
276                  */
277                 mv = I2O_REPLY_READ32(c);
278                 if (mv == I2O_QUEUE_EMPTY)
279                         mv = I2O_REPLY_READ32(c);
280         }
281         return IRQ_HANDLED;
282 }
283
284 /**
285  *      i2o_pci_irq_enable - Allocate interrupt for I2O controller
286  *
287  *      Allocate an interrupt for the I2O controller, and activate interrupts
288  *      on the I2O controller.
289  *
290  *      Returns 0 on success or negative error code on failure.
291  */
292 static int i2o_pci_irq_enable(struct i2o_controller *c)
293 {
294         struct pci_dev *pdev = c->pdev;
295         int rc;
296
297         I2O_IRQ_WRITE32(c, 0xffffffff);
298
299         if (pdev->irq) {
300                 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
301                                  c->name, c);
302                 if (rc < 0) {
303                         printk(KERN_ERR "%s: unable to allocate interrupt %d."
304                                "\n", c->name, pdev->irq);
305                         return rc;
306                 }
307         }
308
309         I2O_IRQ_WRITE32(c, 0x00000000);
310
311         printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
312
313         return 0;
314 }
315
316 /**
317  *      i2o_pci_irq_disable - Free interrupt for I2O controller
318  *      @c: I2O controller
319  *
320  *      Disable interrupts in I2O controller and then free interrupt.
321  */
322 static void i2o_pci_irq_disable(struct i2o_controller *c)
323 {
324         I2O_IRQ_WRITE32(c, 0xffffffff);
325
326         if (c->pdev->irq > 0)
327                 free_irq(c->pdev->irq, c);
328 }
329
330 /**
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
334  *
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.
338  *
339  *      Returns 0 on success or negative error code on failure.
340  */
341 static int __devinit i2o_pci_probe(struct pci_dev *pdev,
342                                    const struct pci_device_id *id)
343 {
344         struct i2o_controller *c;
345         int rc;
346         struct pci_dev *i960 = NULL;
347
348         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
349
350         if ((pdev->class & 0xff) > 1) {
351                 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
352                        "(skipping).\n", pci_name(pdev));
353                 return -ENODEV;
354         }
355
356         if ((rc = pci_enable_device(pdev))) {
357                 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
358                        pci_name(pdev));
359                 return rc;
360         }
361
362         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
363                 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
364                        pci_name(pdev));
365                 rc = -ENODEV;
366                 goto disable;
367         }
368
369         pci_set_master(pdev);
370
371         c = i2o_iop_alloc();
372         if (IS_ERR(c)) {
373                 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
374                        pci_name(pdev));
375                 rc = PTR_ERR(c);
376                 goto disable;
377         } else
378                 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
379                        pci_name(pdev));
380
381         c->pdev = pdev;
382         c->device = pdev->dev;
383
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) {
386                 c->short_req = 1;
387                 printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
388                        c->name);
389         }
390
391         if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
392                 /*
393                  * Expose the ship behind i960 for initialization, or it will
394                  * failed
395                  */
396                 i960 =
397                     pci_find_slot(c->pdev->bus->number,
398                                   PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
399
400                 if (i960)
401                         pci_write_config_word(i960, 0x42, 0);
402
403                 c->promise = 1;
404         }
405
406         /* Cards that go bananas if you quiesce them before you reset them. */
407         if (pdev->vendor == PCI_VENDOR_ID_DPT) {
408                 c->no_quiesce = 1;
409                 if (pdev->device == 0xa511)
410                         c->raptor = 1;
411         }
412
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;
417         }
418
419         if (i2o_pci_irq_enable(c)) {
420                 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
421                        "controller\n", c->name);
422                 goto free_pci;
423         }
424
425         if ((rc = i2o_iop_add(c)))
426                 goto uninstall;
427
428         if (i960)
429                 pci_write_config_word(i960, 0x42, 0x03ff);
430
431         return 0;
432
433       uninstall:
434         i2o_pci_irq_disable(c);
435
436       free_pci:
437         i2o_pci_free(c);
438
439       free_controller:
440         i2o_iop_free(c);
441
442       disable:
443         pci_disable_device(pdev);
444
445         return rc;
446 }
447
448 /**
449  *      i2o_pci_remove - Removes a I2O controller from the system
450  *      pdev: I2O controller which should be removed
451  *
452  *      Reset the I2O controller, disable interrupts and remove all allocated
453  *      resources.
454  */
455 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
456 {
457         struct i2o_controller *c;
458         c = pci_get_drvdata(pdev);
459
460         i2o_iop_remove(c);
461         i2o_pci_irq_disable(c);
462         i2o_pci_free(c);
463
464         printk(KERN_INFO "%s: Controller removed.\n", c->name);
465
466         i2o_iop_free(c);
467         pci_disable_device(pdev);
468 };
469
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),
476 };
477
478 /**
479  *      i2o_pci_init - registers I2O PCI driver in PCI subsystem
480  *
481  *      Returns > 0 on success or negative error code on failure.
482  */
483 int __init i2o_pci_init(void)
484 {
485         return pci_register_driver(&i2o_pci_driver);
486 };
487
488 /**
489  *      i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
490  */
491 void __exit i2o_pci_exit(void)
492 {
493         pci_unregister_driver(&i2o_pci_driver);
494 };
495
496 EXPORT_SYMBOL(i2o_dma_realloc);
497 MODULE_DEVICE_TABLE(pci, i2o_pci_ids);