[ACPI] merge 3549 4320 4485 4588 4980 5483 5651 acpica asus fops pnpacpi branches...
[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 #include "core.h"
34
35 #define OSM_DESCRIPTION "I2O-subsystem"
36
37 /* PCI device id table for all I2O controllers */
38 static 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)},
41         {.vendor = PCI_VENDOR_ID_INTEL,.device = 0x1962,
42          .subvendor = PCI_VENDOR_ID_PROMISE,.subdevice = PCI_ANY_ID},
43         {0}
44 };
45
46 /**
47  *      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  */
53 static 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);
61         kfree(c->lct);
62         i2o_dma_free(dev, &c->dlct);
63         i2o_dma_free(dev, &c->hrt);
64         i2o_dma_free(dev, &c->status);
65
66         if (c->raptor && c->in_queue.virt)
67                 iounmap(c->in_queue.virt);
68
69         if (c->base.virt)
70                 iounmap(c->base.virt);
71
72         pci_release_regions(c->pdev);
73 }
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  */
85 static 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
91         for (i = 0; i < 6; i++) {
92                 /* Skip I/O spaces */
93                 if (!(pci_resource_flags(pdev, i) & IORESOURCE_IO)) {
94                         if (!c->base.phys) {
95                                 c->base.phys = pci_resource_start(pdev, i);
96                                 c->base.len = pci_resource_len(pdev, i);
97
98                                 /*
99                                  * If we know what card it is, set the size
100                                  * correctly. Code is taken from dpt_i2o.c
101                                  */
102                                 if (pdev->device == 0xa501) {
103                                         if (pdev->subsystem_device >= 0xc032 &&
104                                             pdev->subsystem_device <= 0xc03b) {
105                                                 if (c->base.len > 0x400000)
106                                                         c->base.len = 0x400000;
107                                         } else {
108                                                 if (c->base.len > 0x100000)
109                                                         c->base.len = 0x100000;
110                                         }
111                                 }
112                                 if (!c->raptor)
113                                         break;
114                         } else {
115                                 c->in_queue.phys = pci_resource_start(pdev, i);
116                                 c->in_queue.len = pci_resource_len(pdev, i);
117                                 break;
118                         }
119                 }
120         }
121
122         if (i == 6) {
123                 printk(KERN_ERR "%s: I2O controller has no memory regions"
124                        " defined.\n", c->name);
125                 i2o_pci_free(c);
126                 return -EINVAL;
127         }
128
129         /* Map the I2O controller */
130         if (c->raptor) {
131                 printk(KERN_INFO "%s: PCI I2O controller\n", c->name);
132                 printk(KERN_INFO "     BAR0 at 0x%08lX size=%ld\n",
133                        (unsigned long)c->base.phys, (unsigned long)c->base.len);
134                 printk(KERN_INFO "     BAR1 at 0x%08lX size=%ld\n",
135                        (unsigned long)c->in_queue.phys,
136                        (unsigned long)c->in_queue.len);
137         } else
138                 printk(KERN_INFO "%s: PCI I2O controller at %08lX size=%ld\n",
139                        c->name, (unsigned long)c->base.phys,
140                        (unsigned long)c->base.len);
141
142         c->base.virt = ioremap_nocache(c->base.phys, c->base.len);
143         if (!c->base.virt) {
144                 printk(KERN_ERR "%s: Unable to map controller.\n", c->name);
145                 i2o_pci_free(c);
146                 return -ENOMEM;
147         }
148
149         if (c->raptor) {
150                 c->in_queue.virt =
151                     ioremap_nocache(c->in_queue.phys, c->in_queue.len);
152                 if (!c->in_queue.virt) {
153                         printk(KERN_ERR "%s: Unable to map controller.\n",
154                                c->name);
155                         i2o_pci_free(c);
156                         return -ENOMEM;
157                 }
158         } else
159                 c->in_queue = c->base;
160
161         c->irq_status = c->base.virt + I2O_IRQ_STATUS;
162         c->irq_mask = c->base.virt + I2O_IRQ_MASK;
163         c->in_port = c->base.virt + I2O_IN_PORT;
164         c->out_port = c->base.virt + I2O_OUT_PORT;
165
166         if (i2o_dma_alloc(dev, &c->status, 8, GFP_KERNEL)) {
167                 i2o_pci_free(c);
168                 return -ENOMEM;
169         }
170
171         if (i2o_dma_alloc(dev, &c->hrt, sizeof(i2o_hrt), GFP_KERNEL)) {
172                 i2o_pci_free(c);
173                 return -ENOMEM;
174         }
175
176         if (i2o_dma_alloc(dev, &c->dlct, 8192, GFP_KERNEL)) {
177                 i2o_pci_free(c);
178                 return -ENOMEM;
179         }
180
181         if (i2o_dma_alloc(dev, &c->status_block, sizeof(i2o_status_block),
182                           GFP_KERNEL)) {
183                 i2o_pci_free(c);
184                 return -ENOMEM;
185         }
186
187         if (i2o_dma_alloc
188             (dev, &c->out_queue,
189              I2O_MAX_OUTBOUND_MSG_FRAMES * I2O_OUTBOUND_MSG_FRAME_SIZE *
190              sizeof(u32), 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         writel(0xffffffff, c->irq_mask);
252
253         if (pdev->irq) {
254                 rc = request_irq(pdev->irq, i2o_pci_interrupt, SA_SHIRQ,
255                                  c->name, c);
256                 if (rc < 0) {
257                         printk(KERN_ERR "%s: unable to allocate interrupt %d."
258                                "\n", c->name, pdev->irq);
259                         return rc;
260                 }
261         }
262
263         writel(0x00000000, c->irq_mask);
264
265         printk(KERN_INFO "%s: Installed at IRQ %d\n", c->name, pdev->irq);
266
267         return 0;
268 }
269
270 /**
271  *      i2o_pci_irq_disable - Free interrupt for I2O controller
272  *      @c: I2O controller
273  *
274  *      Disable interrupts in I2O controller and then free interrupt.
275  */
276 static void i2o_pci_irq_disable(struct i2o_controller *c)
277 {
278         writel(0xffffffff, c->irq_mask);
279
280         if (c->pdev->irq > 0)
281                 free_irq(c->pdev->irq, c);
282 }
283
284 /**
285  *      i2o_pci_probe - Probe the PCI device for an I2O controller
286  *      @dev: PCI device to test
287  *      @id: id which matched with the PCI device id table
288  *
289  *      Probe the PCI device for any device which is a memory of the
290  *      Intelligent, I2O class or an Adaptec Zero Channel Controller. We
291  *      attempt to set up each such device and register it with the core.
292  *
293  *      Returns 0 on success or negative error code on failure.
294  */
295 static int __devinit i2o_pci_probe(struct pci_dev *pdev,
296                                    const struct pci_device_id *id)
297 {
298         struct i2o_controller *c;
299         int rc;
300         struct pci_dev *i960 = NULL;
301         int pci_dev_busy = 0;
302
303         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
304
305         if ((pdev->class & 0xff) > 1) {
306                 printk(KERN_WARNING "i2o: %s does not support I2O 1.5 "
307                        "(skipping).\n", pci_name(pdev));
308                 return -ENODEV;
309         }
310
311         if ((rc = pci_enable_device(pdev))) {
312                 printk(KERN_WARNING "i2o: couldn't enable device %s\n",
313                        pci_name(pdev));
314                 return rc;
315         }
316
317         if (pci_request_regions(pdev, OSM_DESCRIPTION)) {
318                 printk(KERN_ERR "i2o: device already claimed\n");
319                 return -ENODEV;
320         }
321
322         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
323                 printk(KERN_WARNING "i2o: no suitable DMA found for %s\n",
324                        pci_name(pdev));
325                 rc = -ENODEV;
326                 goto disable;
327         }
328
329         pci_set_master(pdev);
330
331         c = i2o_iop_alloc();
332         if (IS_ERR(c)) {
333                 printk(KERN_ERR "i2o: couldn't allocate memory for %s\n",
334                        pci_name(pdev));
335                 rc = PTR_ERR(c);
336                 goto disable;
337         } else
338                 printk(KERN_INFO "%s: controller found (%s)\n", c->name,
339                        pci_name(pdev));
340
341         c->pdev = pdev;
342         c->device.parent = &pdev->dev;
343
344         /* Cards that fall apart if you hit them with large I/O loads... */
345         if (pdev->vendor == PCI_VENDOR_ID_NCR && pdev->device == 0x0630) {
346                 c->short_req = 1;
347                 printk(KERN_INFO "%s: Symbios FC920 workarounds activated.\n",
348                        c->name);
349         }
350
351         if (pdev->subsystem_vendor == PCI_VENDOR_ID_PROMISE) {
352                 /*
353                  * Expose the ship behind i960 for initialization, or it will
354                  * failed
355                  */
356                 i960 =
357                     pci_find_slot(c->pdev->bus->number,
358                                   PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 0));
359
360                 if (i960)
361                         pci_write_config_word(i960, 0x42, 0);
362
363                 c->promise = 1;
364                 c->limit_sectors = 1;
365         }
366
367         if (pdev->subsystem_vendor == PCI_VENDOR_ID_DPT)
368                 c->adaptec = 1;
369
370         /* Cards that go bananas if you quiesce them before you reset them. */
371         if (pdev->vendor == PCI_VENDOR_ID_DPT) {
372                 c->no_quiesce = 1;
373                 if (pdev->device == 0xa511)
374                         c->raptor = 1;
375
376                 if (pdev->subsystem_device == 0xc05a) {
377                         c->limit_sectors = 1;
378                         printk(KERN_INFO
379                                "%s: limit sectors per request to %d\n", c->name,
380                                I2O_MAX_SECTORS_LIMITED);
381                 }
382 #ifdef CONFIG_I2O_EXT_ADAPTEC_DMA64
383                 if (sizeof(dma_addr_t) > 4) {
384                         if (pci_set_dma_mask(pdev, DMA_64BIT_MASK))
385                                 printk(KERN_INFO "%s: 64-bit DMA unavailable\n",
386                                        c->name);
387                         else {
388                                 c->pae_support = 1;
389                                 printk(KERN_INFO "%s: using 64-bit DMA\n",
390                                        c->name);
391                         }
392                 }
393 #endif
394         }
395
396         if ((rc = i2o_pci_alloc(c))) {
397                 printk(KERN_ERR "%s: DMA / IO allocation for I2O controller "
398                        " failed\n", c->name);
399                 if (rc == -ENODEV)
400                         pci_dev_busy = 1;
401                 goto free_controller;
402         }
403
404         if (i2o_pci_irq_enable(c)) {
405                 printk(KERN_ERR "%s: unable to enable interrupts for I2O "
406                        "controller\n", c->name);
407                 goto free_pci;
408         }
409
410         if ((rc = i2o_iop_add(c)))
411                 goto uninstall;
412
413         if (i960)
414                 pci_write_config_word(i960, 0x42, 0x03ff);
415
416         return 0;
417
418       uninstall:
419         i2o_pci_irq_disable(c);
420
421       free_pci:
422         i2o_pci_free(c);
423
424       free_controller:
425         i2o_iop_free(c);
426
427       disable:
428         if (!pci_dev_busy)
429                 pci_disable_device(pdev);
430
431         return rc;
432 }
433
434 /**
435  *      i2o_pci_remove - Removes a I2O controller from the system
436  *      pdev: I2O controller which should be removed
437  *
438  *      Reset the I2O controller, disable interrupts and remove all allocated
439  *      resources.
440  */
441 static void __devexit i2o_pci_remove(struct pci_dev *pdev)
442 {
443         struct i2o_controller *c;
444         c = pci_get_drvdata(pdev);
445
446         i2o_iop_remove(c);
447         i2o_pci_irq_disable(c);
448         i2o_pci_free(c);
449
450         pci_disable_device(pdev);
451
452         printk(KERN_INFO "%s: Controller removed.\n", c->name);
453
454         put_device(&c->device);
455 };
456
457 /* PCI driver for I2O controller */
458 static struct pci_driver i2o_pci_driver = {
459         .name = "PCI_I2O",
460         .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  */
470 int __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  */
478 void __exit i2o_pci_exit(void)
479 {
480         pci_unregister_driver(&i2o_pci_driver);
481 };
482
483 MODULE_DEVICE_TABLE(pci, i2o_pci_ids);