This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 __devexit 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, 4, 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         u32 *msg;
281
282         /*
283          * Old 960 steppings had a bug in the I2O unit that caused
284          * the queue to appear empty when it wasn't.
285          */
286         mv = I2O_REPLY_READ32(c);
287         if (mv == I2O_QUEUE_EMPTY) {
288                 mv = I2O_REPLY_READ32(c);
289                 if (unlikely(mv == I2O_QUEUE_EMPTY)) {
290                         return IRQ_NONE;
291                 } else
292                         pr_debug("960 bug detected\n");
293         }
294
295         while (mv != I2O_QUEUE_EMPTY) {
296                 /*
297                  * Map the message from the page frame map to kernel virtual.
298                  * Because bus_to_virt is deprecated, we have calculate the
299                  * location by ourself!
300                  */
301                 m = (struct i2o_message *)(mv -
302                                            (unsigned long)c->out_queue.phys +
303                                            (unsigned long)c->out_queue.virt);
304
305                 msg = (u32 *) m;
306
307                 /*
308                  *      Ensure this message is seen coherently but cachably by
309                  *      the processor
310                  */
311                 dma_sync_single_for_cpu(dev, c->out_queue.phys, MSG_FRAME_SIZE,
312                                         PCI_DMA_FROMDEVICE);
313
314                 /* dispatch it */
315                 if (i2o_driver_dispatch(c, mv, m))
316                         /* flush it if result != 0 */
317                         i2o_flush_reply(c, mv);
318
319                 /*
320                  * That 960 bug again...
321                  */
322                 mv = I2O_REPLY_READ32(c);
323                 if (mv == I2O_QUEUE_EMPTY)
324                         mv = I2O_REPLY_READ32(c);
325         }
326         return IRQ_HANDLED;
327 }
328
329 /**
330  *      i2o_pci_irq_enable - Allocate interrupt for I2O controller
331  *
332  *      Allocate an interrupt for the I2O controller, and activate interrupts
333  *      on the I2O controller.
334  *
335  *      Returns 0 on success or negative error code on failure.
336  */
337 static int i2o_pci_irq_enable(struct i2o_controller *c)
338 {
339         struct pci_dev *pdev = c->pdev;
340         int rc;
341
342         I2O_IRQ_WRITE32(c, 0xffffffff);
343
344         if (pdev->irq) {
345                 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
346                                  c->name, c);
347                 if (rc < 0) {
348                         printk(KERN_ERR "%s: unable to allocate interrupt %d."
349                                "\n", c->name, pdev->irq);
350                         return rc;
351                 }
352         }
353
354         I2O_IRQ_WRITE32(c, 0x00000000);
355
356         printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
357
358         return 0;
359 }
360
361 /**
362  *      i2o_pci_irq_disable - Free interrupt for I2O controller
363  *      @c: I2O controller
364  *
365  *      Disable interrupts in I2O controller and then free interrupt.
366  */
367 static void i2o_pci_irq_disable(struct i2o_controller *c)
368 {
369         I2O_IRQ_WRITE32(c, 0xffffffff);
370
371         if (c->pdev->irq > 0)
372                 free_irq(c->pdev->irq, c);
373 }
374
375 /**
376  *      i2o_pci_probe - Probe the PCI device for an I2O controller
377  *      @dev: PCI device to test
378  *      @id: id which matched with the PCI device id table
379  *
380  *      Probe the PCI device for any device which is a memory of the
381  *      Intelligent, I2O class or an Adaptec Zero Channel Controller. We
382  *      attempt to set up each such device and register it with the core.
383  *
384  *      Returns 0 on success or negative error code on failure.
385  */
386 static int __devinit i2o_pci_probe(struct pci_dev *pdev,
387                                    const struct pci_device_id *id)
388 {
389         struct i2o_controller *c;
390         int rc;
391
392         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
393
394         if ((pdev->class & 0xff) > 1) {
395                 printk(KERN_WARNING "i2o: I2O controller found but does not "
396                        "support I2O 1.5 (skipping).\n");
397                 return -ENODEV;
398         }
399
400         if ((rc = pci_enable_device(pdev))) {
401                 printk(KERN_WARNING "i2o: I2O controller found but could not be"
402                        " enabled.\n");
403                 return rc;
404         }
405
406         printk(KERN_INFO "i2o: I2O controller found on bus %d at %d.\n",
407                pdev->bus->number, pdev->devfn);
408
409         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
410                 printk(KERN_WARNING "i2o: I2O controller on bus %d at %d: No "
411                        "suitable DMA available!\n", pdev->bus->number,
412                        pdev->devfn);
413                 rc = -ENODEV;
414                 goto disable;
415         }
416
417         pci_set_master(pdev);
418
419         c = i2o_iop_alloc();
420         if (IS_ERR(c)) {
421                 printk(KERN_ERR "i2o: memory for I2O controller could not be "
422                        "allocated\n");
423                 rc = PTR_ERR(c);
424                 goto disable;
425         }
426
427         c->pdev = pdev;
428         c->device = pdev->dev;
429
430         /* Cards that fall apart if you hit them with large I/O loads... */
431         if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
432                 c->short_req = 1;
433                 printk(KERN_INFO "i2o: Symbios FC920 workarounds activated.\n");
434         }
435
436         if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
437                 c->promise = 1;
438                 printk(KERN_INFO "i2o: Promise workarounds activated.\n");
439         }
440
441         /* Cards that go bananas if you quiesce them before you reset them. */
442         if (pdev->vendor == PCI_VENDOR_ID_DPT) {
443                 c->no_quiesce = 1;
444                 if (pdev->device == 0xa511)
445                         c->raptor = 1;
446         }
447
448         if ((rc = i2o_pci_alloc(c))) {
449                 printk(KERN_ERR "i2o: DMA / IO allocation for I2O controller "
450                        " failed\n");
451                 goto free_controller;
452         }
453
454         if (i2o_pci_irq_enable(c)) {
455                 printk(KERN_ERR "i2o: unable to enable interrupts for I2O "
456                        "controller\n");
457                 goto free_pci;
458         }
459
460         if ((rc = i2o_iop_add(c)))
461                 goto uninstall;
462
463         return 0;
464
465       uninstall:
466         i2o_pci_irq_disable(c);
467
468       free_pci:
469         i2o_pci_free(c);
470
471       free_controller:
472         i2o_iop_free(c);
473
474       disable:
475         pci_disable_device(pdev);
476
477         return rc;
478 }
479
480 /**
481  *      i2o_pci_remove - Removes a I2O controller from the system
482  *      pdev: I2O controller which should be removed
483  *
484  *      Reset the I2O controller, disable interrupts and remove all allocated
485  *      resources.
486  */
487 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
488 {
489         struct i2o_controller *c;
490         c = pci_get_drvdata(pdev);
491
492         i2o_iop_remove(c);
493         i2o_pci_irq_disable(c);
494         i2o_pci_free(c);
495
496         printk(KERN_INFO "%s: Controller removed.\n", c->name);
497
498         i2o_iop_free(c);
499         pci_disable_device(pdev);
500 };
501
502 /* PCI driver for I2O controller */
503 static struct pci_driver i2o_pci_driver = {
504         .name = "I2O controller",
505         .id_table = i2o_pci_ids,
506         .probe = i2o_pci_probe,
507         .remove = __devexit_p(i2o_pci_remove),
508 };
509
510 /**
511  *      i2o_pci_init - registers I2O PCI driver in PCI subsystem
512  *
513  *      Returns > 0 on success or negative error code on failure.
514  */
515 int __init i2o_pci_init(void)
516 {
517         return pci_register_driver(&i2o_pci_driver);
518 };
519
520 /**
521  *      i2o_pci_exit - unregisters I2O PCI driver from PCI subsystem
522  */
523 void __exit i2o_pci_exit(void)
524 {
525         pci_unregister_driver(&i2o_pci_driver);
526 };
527
528 EXPORT_SYMBOL(i2o_dma_realloc);