upgrade to linux 2.6.10-1.12_FC2
[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 #ifdef CONFIG_MTRR
35 #include <asm/mtrr.h>
36 #endif                          // CONFIG_MTRR
37
38 /* Module internal functions from other sources */
39 extern struct i2o_controller *i2o_iop_alloc(void);
40 extern void i2o_iop_free(struct i2o_controller *);
41
42 extern int i2o_iop_add(struct i2o_controller *);
43 extern void i2o_iop_remove(struct i2o_controller *);
44
45 extern int i2o_driver_dispatch(struct i2o_controller *, u32,
46                                struct i2o_message *);
47
48 /* PCI device id table for all I2O controllers */
49 static struct pci_device_id __devinitdata i2o_pci_ids[] = {
50         {PCI_DEVICE_CLASS(PCI_CLASS_INTELLIGENT_I2O << 8, 0xffff00)},
51         {PCI_DEVICE(PCI_VENDOR_ID_DPT, 0xa511)},
52         {0}
53 };
54
55 /**
56  *      i2o_dma_realloc - Realloc DMA memory
57  *      @dev: struct device pointer to the PCI device of the I2O controller
58  *      @addr: pointer to a i2o_dma struct DMA buffer
59  *      @len: new length of memory
60  *      @gfp_mask: GFP mask
61  *
62  *      If there was something allocated in the addr, free it first. If len > 0
63  *      than try to allocate it and write the addresses back to the addr
64  *      structure. If len == 0 set the virtual address to NULL.
65  *
66  *      Returns the 0 on success or negative error code on failure.
67  */
68 int i2o_dma_realloc(struct device *dev, struct i2o_dma *addr, size_t len,
69                     unsigned int gfp_mask)
70 {
71         i2o_dma_free(dev, addr);
72
73         if (len)
74                 return i2o_dma_alloc(dev, addr, len, gfp_mask);
75
76         return 0;
77 };
78
79 /**
80  *      i2o_pci_free - Frees the DMA memory for the I2O controller
81  *      @c: I2O controller to free
82  *
83  *      Remove all allocated DMA memory and unmap memory IO regions. If MTRR
84  *      is enabled, also remove it again.
85  */
86 static void i2o_pci_free(struct i2o_controller *c)
87 {
88         struct device *dev;
89
90         dev = &c->pdev->dev;
91
92         i2o_dma_free(dev, &c->out_queue);
93         i2o_dma_free(dev, &c->status_block);
94         if (c->lct)
95                 kfree(c->lct);
96         i2o_dma_free(dev, &c->dlct);
97         i2o_dma_free(dev, &c->hrt);
98         i2o_dma_free(dev, &c->status);
99
100 #ifdef CONFIG_MTRR
101         if (c->mtrr_reg0 >= 0)
102                 mtrr_del(c->mtrr_reg0, 0, 0);
103         if (c->mtrr_reg1 >= 0)
104                 mtrr_del(c->mtrr_reg1, 0, 0);
105 #endif
106
107         if (c->raptor && c->in_queue.virt)
108                 iounmap(c->in_queue.virt);
109
110         if (c->base.virt)
111                 iounmap(c->base.virt);
112 }
113
114 /**
115  *      i2o_pci_alloc - Allocate DMA memory, map IO memory for I2O controller
116  *      @c: I2O controller
117  *
118  *      Allocate DMA memory for a PCI (or in theory AGP) I2O controller. All
119  *      IO mappings are also done here. If MTRR is enabled, also do add memory
120  *      regions here.
121  *
122  *      Returns 0 on success or negative error code on failure.
123  */
124 static int __devinit i2o_pci_alloc(struct i2o_controller *c)
125 {
126         struct pci_dev *pdev = c->pdev;
127         struct device *dev = &pdev->dev;
128         int i;
129
130         for (i = 0; i < 6; i++) {
131                 /* Skip I/O spaces */
132                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
133                         if (!c->base.phys) {
134                                 c->base.phys = pci_resource_start(pdev, i);
135                                 c->base.len = pci_resource_len(pdev, i);
136
137                                 /*
138                                  * If we know what card it is, set the size
139                                  * correctly. Code is taken from dpt_i2o.c
140                                  */
141                                 if (pdev->device == 0xa501) {
142                                         if (pdev->subsystem_device >= 0xc032 &&
143                                             pdev->subsystem_device <= 0xc03b) {
144                                                 if (c->base.len > 0x400000)
145                                                         c->base.len = 0x400000;
146                                         } else {
147                                                 if (c->base.len > 0x100000)
148                                                         c->base.len = 0x100000;
149                                         }
150                                 }
151                                 if (!c->raptor)
152                                         break;
153                         } else {
154                                 c->in_queue.phys = pci_resource_start(pdev, i);
155                                 c->in_queue.len = pci_resource_len(pdev, i);
156                                 break;
157                         }
158                 }
159         }
160
161         if (i == 6) {
162                 printk(KERN_ERR "i2o: I2O controller has no memory regions"
163                        " defined.\n");
164                 i2o_pci_free(c);
165                 return -EINVAL;
166         }
167
168         /* Map the I2O controller */
169         if (c->raptor) {
170                 printk(KERN_INFO "i2o: PCI I2O controller\n");
171                 printk(KERN_INFO "     BAR0 at 0x%08lX size=%ld\n",
172                        (unsigned long)c->base.phys, (unsigned long)c->base.len);
173                 printk(KERN_INFO "     BAR1 at 0x%08lX size=%ld\n",
174                        (unsigned long)c->in_queue.phys,
175                        (unsigned long)c->in_queue.len);
176         } else
177                 printk(KERN_INFO "i2o: PCI I2O controller at %08lX size=%ld\n",
178                        (unsigned long)c->base.phys, (unsigned long)c->base.len);
179
180         c->base.virt = ioremap(c->base.phys, c->base.len);
181         if (!c->base.virt) {
182                 printk(KERN_ERR "i2o: Unable to map controller.\n");
183                 return -ENOMEM;
184         }
185
186         if (c->raptor) {
187                 c->in_queue.virt = ioremap(c->in_queue.phys, c->in_queue.len);
188                 if (!c->in_queue.virt) {
189                         printk(KERN_ERR "i2o: Unable to map controller.\n");
190                         i2o_pci_free(c);
191                         return -ENOMEM;
192                 }
193         } else
194                 c->in_queue = c->base;
195
196         c->irq_mask = c->base.virt + 0x34;
197         c->post_port = c->base.virt + 0x40;
198         c->reply_port = c->base.virt + 0x44;
199
200 #ifdef CONFIG_MTRR
201         /* Enable Write Combining MTRR for IOP's memory region */
202         c->mtrr_reg0 = mtrr_add(c->in_queue.phys, c->in_queue.len,
203                                 MTRR_TYPE_WRCOMB, 1);
204         c->mtrr_reg1 = -1;
205
206         if (c->mtrr_reg0 < 0)
207                 printk(KERN_WARNING "i2o: could not enable write combining "
208                        "MTRR\n");
209         else
210                 printk(KERN_INFO "i2o: using write combining MTRR\n");
211
212         /*
213          * If it is an INTEL i960 I/O processor then set the first 64K to
214          * Uncacheable since the region contains the messaging unit which
215          * shouldn't be cached.
216          */
217         if ((pdev->vendor == PCI_VENDOR_ID_INTEL ||
218              pdev->vendor == PCI_VENDOR_ID_DPT) && !c->raptor) {
219                 printk(KERN_INFO "i2o: MTRR workaround for Intel i960 processor"
220                        "\n");
221                 c->mtrr_reg1 = mtrr_add(c->base.phys, 0x10000,
222                                         MTRR_TYPE_UNCACHABLE, 1);
223
224                 if (c->mtrr_reg1 < 0) {
225                         printk(KERN_WARNING "i2o_pci: Error in setting "
226                                "MTRR_TYPE_UNCACHABLE\n");
227                         mtrr_del(c->mtrr_reg0, c->in_queue.phys,
228                                  c->in_queue.len);
229                         c->mtrr_reg0 = -1;
230                 }
231         }
232 #endif
233
234         if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
235                 i2o_pci_free(c);
236                 return -ENOMEM;
237         }
238
239         if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
240                 i2o_pci_free(c);
241                 return -ENOMEM;
242         }
243
244         if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
245                 i2o_pci_free(c);
246                 return -ENOMEM;
247         }
248
249         if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
250                           GFP_KERNEL)) {
251                 i2o_pci_free(c);
252                 return -ENOMEM;
253         }
254
255         if (i2o_dma_alloc(dev, &c->out_queue, MSG_POOL_SIZE, GFP_KERNEL)) {
256                 i2o_pci_free(c);
257                 return -ENOMEM;
258         }
259
260         pci_set_drvdata(pdev, c);
261
262         return 0;
263 }
264
265 /**
266  *      i2o_pci_interrupt - Interrupt handler for I2O controller
267  *      @irq: interrupt line
268  *      @dev_id: pointer to the I2O controller
269  *      @r: pointer to registers
270  *
271  *      Handle an interrupt from a PCI based I2O controller. This turns out
272  *      to be rather simple. We keep the controller pointer in the cookie.
273  */
274 static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
275 {
276         struct i2o_controller *c = dev_id;
277         struct device *dev = &c->pdev->dev;
278         struct i2o_message *m;
279         u32 mv;
280
281         /*
282          * Old 960 steppings had a bug in the I2O unit that caused
283          * the queue to appear empty when it wasn't.
284          */
285         mv = I2O_REPLY_READ32(c);
286         if (mv == I2O_QUEUE_EMPTY) {
287                 mv = I2O_REPLY_READ32(c);
288                 if (unlikely(mv == I2O_QUEUE_EMPTY)) {
289                         return IRQ_NONE;
290                 } else
291                         pr_debug("960 bug detected\n");
292         }
293
294         while (mv != I2O_QUEUE_EMPTY) {
295                 /*
296                  * Map the message from the page frame map to kernel virtual.
297                  * Because bus_to_virt is deprecated, we have calculate the
298                  * location by ourself!
299                  */
300                 m = i2o_msg_out_to_virt(c, mv);
301
302                 /*
303                  *      Ensure this message is seen coherently but cachably by
304                  *      the processor
305                  */
306                 dma_sync_single_for_cpu(dev, c->out_queue.phys, MSG_FRAME_SIZE,
307                                         PCI_DMA_FROMDEVICE);
308
309                 /* dispatch it */
310                 if (i2o_driver_dispatch(c, mv, m))
311                         /* flush it if result != 0 */
312                         i2o_flush_reply(c, mv);
313
314                 /*
315                  * That 960 bug again...
316                  */
317                 mv = I2O_REPLY_READ32(c);
318                 if (mv == I2O_QUEUE_EMPTY)
319                         mv = I2O_REPLY_READ32(c);
320         }
321         return IRQ_HANDLED;
322 }
323
324 /**
325  *      i2o_pci_irq_enable - Allocate interrupt for I2O controller
326  *
327  *      Allocate an interrupt for the I2O controller, and activate interrupts
328  *      on the I2O controller.
329  *
330  *      Returns 0 on success or negative error code on failure.
331  */
332 static int i2o_pci_irq_enable(struct i2o_controller *c)
333 {
334         struct pci_dev *pdev = c->pdev;
335         int rc;
336
337         I2O_IRQ_WRITE32(c, 0xffffffff);
338
339         if (pdev->irq) {
340                 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
341                                  c->name, c);
342                 if (rc < 0) {
343                         printk(KERN_ERR "%s: unable to allocate interrupt %d."
344                                "\n", c->name, pdev->irq);
345                         return rc;
346                 }
347         }
348
349         I2O_IRQ_WRITE32(c, 0x00000000);
350
351         printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
352
353         return 0;
354 }
355
356 /**
357  *      i2o_pci_irq_disable - Free interrupt for I2O controller
358  *      @c: I2O controller
359  *
360  *      Disable interrupts in I2O controller and then free interrupt.
361  */
362 static void i2o_pci_irq_disable(struct i2o_controller *c)
363 {
364         I2O_IRQ_WRITE32(c, 0xffffffff);
365
366         if (c->pdev->irq > 0)
367                 free_irq(c->pdev->irq, c);
368 }
369
370 /**
371  *      i2o_pci_probe - Probe the PCI device for an I2O controller
372  *      @dev: PCI device to test
373  *      @id: id which matched with the PCI device id table
374  *
375  *      Probe the PCI device for any device which is a memory of the
376  *      Intelligent, I2O class or an Adaptec Zero Channel Controller. We
377  *      attempt to set up each such device and register it with the core.
378  *
379  *      Returns 0 on success or negative error code on failure.
380  */
381 static int __devinit i2o_pci_probe(struct pci_dev *pdev,
382                                    const struct pci_device_id *id)
383 {
384         struct i2o_controller *c;
385         int rc;
386
387         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
388
389         if ((pdev->class & 0xff) > 1) {
390                 printk(KERN_WARNING "i2o: I2O controller found but does not "
391                        "support I2O 1.5 (skipping).\n");
392                 return -ENODEV;
393         }
394
395         if ((rc = pci_enable_device(pdev))) {
396                 printk(KERN_WARNING "i2o: I2O controller found but could not be"
397                        " enabled.\n");
398                 return rc;
399         }
400
401         printk(KERN_INFO "i2o: I2O controller found on bus %d at %d.\n",
402                pdev->bus->number, pdev->devfn);
403
404         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
405                 printk(KERN_WARNING "i2o: I2O controller on bus %d at %d: No "
406                        "suitable DMA available!\n", pdev->bus->number,
407                        pdev->devfn);
408                 rc = -ENODEV;
409                 goto disable;
410         }
411
412         pci_set_master(pdev);
413
414         c = i2o_iop_alloc();
415         if (IS_ERR(c)) {
416                 printk(KERN_ERR "i2o: memory for I2O controller could not be "
417                        "allocated\n");
418                 rc = PTR_ERR(c);
419                 goto disable;
420         }
421
422         c->pdev = pdev;
423         c->device = pdev->dev;
424
425         /* Cards that fall apart if you hit them with large I/O loads... */
426         if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
427                 c->short_req = 1;
428                 printk(KERN_INFO "i2o: Symbios FC920 workarounds activated.\n");
429         }
430
431         if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
432                 c->promise = 1;
433                 printk(KERN_INFO "i2o: Promise workarounds activated.\n");
434         }
435
436         /* Cards that go bananas if you quiesce them before you reset them. */
437         if (pdev->vendor == PCI_VENDOR_ID_DPT) {
438                 c->no_quiesce = 1;
439                 if (pdev->device == 0xa511)
440                         c->raptor = 1;
441         }
442
443         if ((rc = i2o_pci_alloc(c))) {
444                 printk(KERN_ERR "i2o: DMA / IO allocation for I2O controller "
445                        " failed\n");
446                 goto free_controller;
447         }
448
449         if (i2o_pci_irq_enable(c)) {
450                 printk(KERN_ERR "i2o: unable to enable interrupts for I2O "
451                        "controller\n");
452                 goto free_pci;
453         }
454
455         if ((rc = i2o_iop_add(c)))
456                 goto uninstall;
457
458         return 0;
459
460       uninstall:
461         i2o_pci_irq_disable(c);
462
463       free_pci:
464         i2o_pci_free(c);
465
466       free_controller:
467         i2o_iop_free(c);
468
469       disable:
470         pci_disable_device(pdev);
471
472         return rc;
473 }
474
475 /**
476  *      i2o_pci_remove - Removes a I2O controller from the system
477  *      pdev: I2O controller which should be removed
478  *
479  *      Reset the I2O controller, disable interrupts and remove all allocated
480  *      resources.
481  */
482 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
483 {
484         struct i2o_controller *c;
485         c = pci_get_drvdata(pdev);
486
487         i2o_iop_remove(c);
488         i2o_pci_irq_disable(c);
489         i2o_pci_free(c);
490
491         printk(KERN_INFO "%s: Controller removed.\n", c->name);
492
493         i2o_iop_free(c);
494         pci_disable_device(pdev);
495 };
496
497 /* PCI driver for I2O controller */
498 static struct pci_driver i2o_pci_driver = {
499         .name = "I2O controller",
500         .id_table = i2o_pci_ids,
501         .probe = i2o_pci_probe,
502         .remove = __devexit_p(i2o_pci_remove),
503 };
504
505 /**
506  *      i2o_pci_init - registers I2O PCI driver in PCI subsystem
507  *
508  *      Returns > 0 on success or negative error code on failure.
509  */
510 int __init i2o_pci_init(void)
511 {
512         return pci_register_driver(&i2o_pci_driver);
513 };
514
515 /**
516  *      i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
517  */
518 void __exit i2o_pci_exit(void)
519 {
520         pci_unregister_driver(&i2o_pci_driver);
521 };
522
523 EXPORT_SYMBOL(i2o_dma_realloc);