[SCSI] zfcp: remove union zfcp_req_data, use unit refcount for FCP commands
[linux-2.6] / drivers / acpi / pci_link.c
1 /*
2  *  pci_link.c - ACPI PCI Interrupt Link Device Driver ($Revision: 34 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *  Copyright (C) 2002       Dominik Brodowski <devel@brodo.de>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *
26  * TBD: 
27  *      1. Support more than one IRQ resource entry per link device (index).
28  *      2. Implement start/stop mechanism and use ACPI Bus Driver facilities
29  *         for IRQ management (e.g. start()->_SRS).
30  */
31
32 #include <linux/sysdev.h>
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/proc_fs.h>
38 #include <linux/spinlock.h>
39 #include <linux/pm.h>
40 #include <linux/pci.h>
41
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44
45 #define _COMPONENT              ACPI_PCI_COMPONENT
46 ACPI_MODULE_NAME("pci_link")
47 #define ACPI_PCI_LINK_CLASS             "pci_irq_routing"
48 #define ACPI_PCI_LINK_HID               "PNP0C0F"
49 #define ACPI_PCI_LINK_DRIVER_NAME       "ACPI PCI Interrupt Link Driver"
50 #define ACPI_PCI_LINK_DEVICE_NAME       "PCI Interrupt Link"
51 #define ACPI_PCI_LINK_FILE_INFO         "info"
52 #define ACPI_PCI_LINK_FILE_STATUS       "state"
53 #define ACPI_PCI_LINK_MAX_POSSIBLE 16
54 static int acpi_pci_link_add(struct acpi_device *device);
55 static int acpi_pci_link_remove(struct acpi_device *device, int type);
56
57 static struct acpi_driver acpi_pci_link_driver = {
58         .name = ACPI_PCI_LINK_DRIVER_NAME,
59         .class = ACPI_PCI_LINK_CLASS,
60         .ids = ACPI_PCI_LINK_HID,
61         .ops = {
62                 .add = acpi_pci_link_add,
63                 .remove = acpi_pci_link_remove,
64                 },
65 };
66
67 /*
68  * If a link is initialized, we never change its active and initialized
69  * later even the link is disable. Instead, we just repick the active irq
70  */
71 struct acpi_pci_link_irq {
72         u8 active;              /* Current IRQ */
73         u8 edge_level;          /* All IRQs */
74         u8 active_high_low;     /* All IRQs */
75         u8 resource_type;
76         u8 possible_count;
77         u8 possible[ACPI_PCI_LINK_MAX_POSSIBLE];
78         u8 initialized:1;
79         u8 reserved:7;
80 };
81
82 struct acpi_pci_link {
83         struct list_head node;
84         struct acpi_device *device;
85         acpi_handle handle;
86         struct acpi_pci_link_irq irq;
87         int refcnt;
88 };
89
90 static struct {
91         int count;
92         struct list_head entries;
93 } acpi_link;
94 DECLARE_MUTEX(acpi_link_lock);
95
96 /* --------------------------------------------------------------------------
97                             PCI Link Device Management
98    -------------------------------------------------------------------------- */
99
100 /*
101  * set context (link) possible list from resource list
102  */
103 static acpi_status
104 acpi_pci_link_check_possible(struct acpi_resource *resource, void *context)
105 {
106         struct acpi_pci_link *link = (struct acpi_pci_link *)context;
107         u32 i = 0;
108
109         ACPI_FUNCTION_TRACE("acpi_pci_link_check_possible");
110
111         switch (resource->id) {
112         case ACPI_RSTYPE_START_DPF:
113                 return_ACPI_STATUS(AE_OK);
114         case ACPI_RSTYPE_IRQ:
115                 {
116                         struct acpi_resource_irq *p = &resource->data.irq;
117                         if (!p || !p->number_of_interrupts) {
118                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
119                                                   "Blank IRQ resource\n"));
120                                 return_ACPI_STATUS(AE_OK);
121                         }
122                         for (i = 0;
123                              (i < p->number_of_interrupts
124                               && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
125                                 if (!p->interrupts[i]) {
126                                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
127                                                           "Invalid IRQ %d\n",
128                                                           p->interrupts[i]));
129                                         continue;
130                                 }
131                                 link->irq.possible[i] = p->interrupts[i];
132                                 link->irq.possible_count++;
133                         }
134                         link->irq.edge_level = p->edge_level;
135                         link->irq.active_high_low = p->active_high_low;
136                         link->irq.resource_type = ACPI_RSTYPE_IRQ;
137                         break;
138                 }
139         case ACPI_RSTYPE_EXT_IRQ:
140                 {
141                         struct acpi_resource_ext_irq *p =
142                             &resource->data.extended_irq;
143                         if (!p || !p->number_of_interrupts) {
144                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
145                                                   "Blank EXT IRQ resource\n"));
146                                 return_ACPI_STATUS(AE_OK);
147                         }
148                         for (i = 0;
149                              (i < p->number_of_interrupts
150                               && i < ACPI_PCI_LINK_MAX_POSSIBLE); i++) {
151                                 if (!p->interrupts[i]) {
152                                         ACPI_DEBUG_PRINT((ACPI_DB_WARN,
153                                                           "Invalid IRQ %d\n",
154                                                           p->interrupts[i]));
155                                         continue;
156                                 }
157                                 link->irq.possible[i] = p->interrupts[i];
158                                 link->irq.possible_count++;
159                         }
160                         link->irq.edge_level = p->edge_level;
161                         link->irq.active_high_low = p->active_high_low;
162                         link->irq.resource_type = ACPI_RSTYPE_EXT_IRQ;
163                         break;
164                 }
165         default:
166                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
167                                   "Resource is not an IRQ entry\n"));
168                 return_ACPI_STATUS(AE_OK);
169         }
170
171         return_ACPI_STATUS(AE_CTRL_TERMINATE);
172 }
173
174 static int acpi_pci_link_get_possible(struct acpi_pci_link *link)
175 {
176         acpi_status status;
177
178         ACPI_FUNCTION_TRACE("acpi_pci_link_get_possible");
179
180         if (!link)
181                 return_VALUE(-EINVAL);
182
183         status = acpi_walk_resources(link->handle, METHOD_NAME__PRS,
184                                      acpi_pci_link_check_possible, link);
185         if (ACPI_FAILURE(status)) {
186                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRS\n"));
187                 return_VALUE(-ENODEV);
188         }
189
190         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
191                           "Found %d possible IRQs\n",
192                           link->irq.possible_count));
193
194         return_VALUE(0);
195 }
196
197 static acpi_status
198 acpi_pci_link_check_current(struct acpi_resource *resource, void *context)
199 {
200         int *irq = (int *)context;
201
202         ACPI_FUNCTION_TRACE("acpi_pci_link_check_current");
203
204         switch (resource->id) {
205         case ACPI_RSTYPE_IRQ:
206                 {
207                         struct acpi_resource_irq *p = &resource->data.irq;
208                         if (!p || !p->number_of_interrupts) {
209                                 /*
210                                  * IRQ descriptors may have no IRQ# bits set,
211                                  * particularly those those w/ _STA disabled
212                                  */
213                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
214                                                   "Blank IRQ resource\n"));
215                                 return_ACPI_STATUS(AE_OK);
216                         }
217                         *irq = p->interrupts[0];
218                         break;
219                 }
220         case ACPI_RSTYPE_EXT_IRQ:
221                 {
222                         struct acpi_resource_ext_irq *p =
223                             &resource->data.extended_irq;
224                         if (!p || !p->number_of_interrupts) {
225                                 /*
226                                  * extended IRQ descriptors must
227                                  * return at least 1 IRQ
228                                  */
229                                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
230                                                   "Blank EXT IRQ resource\n"));
231                                 return_ACPI_STATUS(AE_OK);
232                         }
233                         *irq = p->interrupts[0];
234                         break;
235                 }
236         default:
237                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Resource isn't an IRQ\n"));
238                 return_ACPI_STATUS(AE_OK);
239         }
240         return_ACPI_STATUS(AE_CTRL_TERMINATE);
241 }
242
243 /*
244  * Run _CRS and set link->irq.active
245  *
246  * return value:
247  * 0 - success
248  * !0 - failure
249  */
250 static int acpi_pci_link_get_current(struct acpi_pci_link *link)
251 {
252         int result = 0;
253         acpi_status status = AE_OK;
254         int irq = 0;
255
256         ACPI_FUNCTION_TRACE("acpi_pci_link_get_current");
257
258         if (!link || !link->handle)
259                 return_VALUE(-EINVAL);
260
261         link->irq.active = 0;
262
263         /* in practice, status disabled is meaningless, ignore it */
264         if (acpi_strict) {
265                 /* Query _STA, set link->device->status */
266                 result = acpi_bus_get_status(link->device);
267                 if (result) {
268                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
269                                           "Unable to read status\n"));
270                         goto end;
271                 }
272
273                 if (!link->device->status.enabled) {
274                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link disabled\n"));
275                         return_VALUE(0);
276                 }
277         }
278
279         /* 
280          * Query and parse _CRS to get the current IRQ assignment. 
281          */
282
283         status = acpi_walk_resources(link->handle, METHOD_NAME__CRS,
284                                      acpi_pci_link_check_current, &irq);
285         if (ACPI_FAILURE(status)) {
286                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _CRS\n"));
287                 result = -ENODEV;
288                 goto end;
289         }
290
291         if (acpi_strict && !irq) {
292                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_CRS returned 0\n"));
293                 result = -ENODEV;
294         }
295
296         link->irq.active = irq;
297
298         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Link at IRQ %d \n", link->irq.active));
299
300       end:
301         return_VALUE(result);
302 }
303
304 static int acpi_pci_link_set(struct acpi_pci_link *link, int irq)
305 {
306         int result = 0;
307         acpi_status status = AE_OK;
308         struct {
309                 struct acpi_resource res;
310                 struct acpi_resource end;
311         } *resource;
312         struct acpi_buffer buffer = { 0, NULL };
313
314         ACPI_FUNCTION_TRACE("acpi_pci_link_set");
315
316         if (!link || !irq)
317                 return_VALUE(-EINVAL);
318
319         resource = kmalloc(sizeof(*resource) + 1, GFP_KERNEL);
320         if (!resource)
321                 return_VALUE(-ENOMEM);
322
323         memset(resource, 0, sizeof(*resource) + 1);
324         buffer.length = sizeof(*resource) + 1;
325         buffer.pointer = resource;
326
327         switch (link->irq.resource_type) {
328         case ACPI_RSTYPE_IRQ:
329                 resource->res.id = ACPI_RSTYPE_IRQ;
330                 resource->res.length = sizeof(struct acpi_resource);
331                 resource->res.data.irq.edge_level = link->irq.edge_level;
332                 resource->res.data.irq.active_high_low =
333                     link->irq.active_high_low;
334                 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
335                         resource->res.data.irq.shared_exclusive =
336                             ACPI_EXCLUSIVE;
337                 else
338                         resource->res.data.irq.shared_exclusive = ACPI_SHARED;
339                 resource->res.data.irq.number_of_interrupts = 1;
340                 resource->res.data.irq.interrupts[0] = irq;
341                 break;
342
343         case ACPI_RSTYPE_EXT_IRQ:
344                 resource->res.id = ACPI_RSTYPE_EXT_IRQ;
345                 resource->res.length = sizeof(struct acpi_resource);
346                 resource->res.data.extended_irq.producer_consumer =
347                     ACPI_CONSUMER;
348                 resource->res.data.extended_irq.edge_level =
349                     link->irq.edge_level;
350                 resource->res.data.extended_irq.active_high_low =
351                     link->irq.active_high_low;
352                 if (link->irq.edge_level == ACPI_EDGE_SENSITIVE)
353                         resource->res.data.irq.shared_exclusive =
354                             ACPI_EXCLUSIVE;
355                 else
356                         resource->res.data.irq.shared_exclusive = ACPI_SHARED;
357                 resource->res.data.extended_irq.number_of_interrupts = 1;
358                 resource->res.data.extended_irq.interrupts[0] = irq;
359                 /* ignore resource_source, it's optional */
360                 break;
361         default:
362                 printk("ACPI BUG: resource_type %d\n", link->irq.resource_type);
363                 result = -EINVAL;
364                 goto end;
365
366         }
367         resource->end.id = ACPI_RSTYPE_END_TAG;
368
369         /* Attempt to set the resource */
370         status = acpi_set_current_resources(link->handle, &buffer);
371
372         /* check for total failure */
373         if (ACPI_FAILURE(status)) {
374                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SRS\n"));
375                 result = -ENODEV;
376                 goto end;
377         }
378
379         /* Query _STA, set device->status */
380         result = acpi_bus_get_status(link->device);
381         if (result) {
382                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to read status\n"));
383                 goto end;
384         }
385         if (!link->device->status.enabled) {
386                 printk(KERN_WARNING PREFIX
387                        "%s [%s] disabled and referenced, BIOS bug.\n",
388                        acpi_device_name(link->device),
389                        acpi_device_bid(link->device));
390         }
391
392         /* Query _CRS, set link->irq.active */
393         result = acpi_pci_link_get_current(link);
394         if (result) {
395                 goto end;
396         }
397
398         /*
399          * Is current setting not what we set?
400          * set link->irq.active
401          */
402         if (link->irq.active != irq) {
403                 /*
404                  * policy: when _CRS doesn't return what we just _SRS
405                  * assume _SRS worked and override _CRS value.
406                  */
407                 printk(KERN_WARNING PREFIX
408                        "%s [%s] BIOS reported IRQ %d, using IRQ %d\n",
409                        acpi_device_name(link->device),
410                        acpi_device_bid(link->device), link->irq.active, irq);
411                 link->irq.active = irq;
412         }
413
414         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Set IRQ %d\n", link->irq.active));
415
416       end:
417         kfree(resource);
418         return_VALUE(result);
419 }
420
421 /* --------------------------------------------------------------------------
422                             PCI Link IRQ Management
423    -------------------------------------------------------------------------- */
424
425 /*
426  * "acpi_irq_balance" (default in APIC mode) enables ACPI to use PIC Interrupt
427  * Link Devices to move the PIRQs around to minimize sharing.
428  * 
429  * "acpi_irq_nobalance" (default in PIC mode) tells ACPI not to move any PIC IRQs
430  * that the BIOS has already set to active.  This is necessary because
431  * ACPI has no automatic means of knowing what ISA IRQs are used.  Note that
432  * if the BIOS doesn't set a Link Device active, ACPI needs to program it
433  * even if acpi_irq_nobalance is set.
434  *
435  * A tables of penalties avoids directing PCI interrupts to well known
436  * ISA IRQs. Boot params are available to over-ride the default table:
437  *
438  * List interrupts that are free for PCI use.
439  * acpi_irq_pci=n[,m]
440  *
441  * List interrupts that should not be used for PCI:
442  * acpi_irq_isa=n[,m]
443  *
444  * Note that PCI IRQ routers have a list of possible IRQs,
445  * which may not include the IRQs this table says are available.
446  * 
447  * Since this heuristic can't tell the difference between a link
448  * that no device will attach to, vs. a link which may be shared
449  * by multiple active devices -- it is not optimal.
450  *
451  * If interrupt performance is that important, get an IO-APIC system
452  * with a pin dedicated to each device.  Or for that matter, an MSI
453  * enabled system.
454  */
455
456 #define ACPI_MAX_IRQS           256
457 #define ACPI_MAX_ISA_IRQ        16
458
459 #define PIRQ_PENALTY_PCI_AVAILABLE      (0)
460 #define PIRQ_PENALTY_PCI_POSSIBLE       (16*16)
461 #define PIRQ_PENALTY_PCI_USING          (16*16*16)
462 #define PIRQ_PENALTY_ISA_TYPICAL        (16*16*16*16)
463 #define PIRQ_PENALTY_ISA_USED           (16*16*16*16*16)
464 #define PIRQ_PENALTY_ISA_ALWAYS         (16*16*16*16*16*16)
465
466 static int acpi_irq_penalty[ACPI_MAX_IRQS] = {
467         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ0 timer */
468         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ1 keyboard */
469         PIRQ_PENALTY_ISA_ALWAYS,        /* IRQ2 cascade */
470         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ3 serial */
471         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ4 serial */
472         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ5 sometimes SoundBlaster */
473         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ6 */
474         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ7 parallel, spurious */
475         PIRQ_PENALTY_ISA_TYPICAL,       /* IRQ8 rtc, sometimes */
476         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ9  PCI, often acpi */
477         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ10 PCI */
478         PIRQ_PENALTY_PCI_AVAILABLE,     /* IRQ11 PCI */
479         PIRQ_PENALTY_ISA_USED,  /* IRQ12 mouse */
480         PIRQ_PENALTY_ISA_USED,  /* IRQ13 fpe, sometimes */
481         PIRQ_PENALTY_ISA_USED,  /* IRQ14 ide0 */
482         PIRQ_PENALTY_ISA_USED,  /* IRQ15 ide1 */
483         /* >IRQ15 */
484 };
485
486 int __init acpi_irq_penalty_init(void)
487 {
488         struct list_head *node = NULL;
489         struct acpi_pci_link *link = NULL;
490         int i = 0;
491
492         ACPI_FUNCTION_TRACE("acpi_irq_penalty_init");
493
494         /*
495          * Update penalties to facilitate IRQ balancing.
496          */
497         list_for_each(node, &acpi_link.entries) {
498
499                 link = list_entry(node, struct acpi_pci_link, node);
500                 if (!link) {
501                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
502                                           "Invalid link context\n"));
503                         continue;
504                 }
505
506                 /*
507                  * reflect the possible and active irqs in the penalty table --
508                  * useful for breaking ties.
509                  */
510                 if (link->irq.possible_count) {
511                         int penalty =
512                             PIRQ_PENALTY_PCI_POSSIBLE /
513                             link->irq.possible_count;
514
515                         for (i = 0; i < link->irq.possible_count; i++) {
516                                 if (link->irq.possible[i] < ACPI_MAX_ISA_IRQ)
517                                         acpi_irq_penalty[link->irq.
518                                                          possible[i]] +=
519                                             penalty;
520                         }
521
522                 } else if (link->irq.active) {
523                         acpi_irq_penalty[link->irq.active] +=
524                             PIRQ_PENALTY_PCI_POSSIBLE;
525                 }
526         }
527         /* Add a penalty for the SCI */
528         acpi_irq_penalty[acpi_fadt.sci_int] += PIRQ_PENALTY_PCI_USING;
529
530         return_VALUE(0);
531 }
532
533 static int acpi_irq_balance;    /* 0: static, 1: balance */
534
535 static int acpi_pci_link_allocate(struct acpi_pci_link *link)
536 {
537         int irq;
538         int i;
539
540         ACPI_FUNCTION_TRACE("acpi_pci_link_allocate");
541
542         if (link->irq.initialized) {
543                 if (link->refcnt == 0)
544                         /* This means the link is disabled but initialized */
545                         acpi_pci_link_set(link, link->irq.active);
546                 return_VALUE(0);
547         }
548
549         /*
550          * search for active IRQ in list of possible IRQs.
551          */
552         for (i = 0; i < link->irq.possible_count; ++i) {
553                 if (link->irq.active == link->irq.possible[i])
554                         break;
555         }
556         /*
557          * forget active IRQ that is not in possible list
558          */
559         if (i == link->irq.possible_count) {
560                 if (acpi_strict)
561                         printk(KERN_WARNING PREFIX "_CRS %d not found"
562                                " in _PRS\n", link->irq.active);
563                 link->irq.active = 0;
564         }
565
566         /*
567          * if active found, use it; else pick entry from end of possible list.
568          */
569         if (link->irq.active) {
570                 irq = link->irq.active;
571         } else {
572                 irq = link->irq.possible[link->irq.possible_count - 1];
573         }
574
575         if (acpi_irq_balance || !link->irq.active) {
576                 /*
577                  * Select the best IRQ.  This is done in reverse to promote
578                  * the use of IRQs 9, 10, 11, and >15.
579                  */
580                 for (i = (link->irq.possible_count - 1); i >= 0; i--) {
581                         if (acpi_irq_penalty[irq] >
582                             acpi_irq_penalty[link->irq.possible[i]])
583                                 irq = link->irq.possible[i];
584                 }
585         }
586
587         /* Attempt to enable the link device at this IRQ. */
588         if (acpi_pci_link_set(link, irq)) {
589                 printk(PREFIX
590                        "Unable to set IRQ for %s [%s] (likely buggy ACPI BIOS).\n"
591                        "Try pci=noacpi or acpi=off\n",
592                        acpi_device_name(link->device),
593                        acpi_device_bid(link->device));
594                 return_VALUE(-ENODEV);
595         } else {
596                 acpi_irq_penalty[link->irq.active] += PIRQ_PENALTY_PCI_USING;
597                 printk(PREFIX "%s [%s] enabled at IRQ %d\n",
598                        acpi_device_name(link->device),
599                        acpi_device_bid(link->device), link->irq.active);
600         }
601
602         link->irq.initialized = 1;
603
604         return_VALUE(0);
605 }
606
607 /*
608  * acpi_pci_link_allocate_irq
609  * success: return IRQ >= 0
610  * failure: return -1
611  */
612
613 int
614 acpi_pci_link_allocate_irq(acpi_handle handle,
615                            int index,
616                            int *edge_level, int *active_high_low, char **name)
617 {
618         int result = 0;
619         struct acpi_device *device = NULL;
620         struct acpi_pci_link *link = NULL;
621
622         ACPI_FUNCTION_TRACE("acpi_pci_link_allocate_irq");
623
624         result = acpi_bus_get_device(handle, &device);
625         if (result) {
626                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
627                 return_VALUE(-1);
628         }
629
630         link = (struct acpi_pci_link *)acpi_driver_data(device);
631         if (!link) {
632                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
633                 return_VALUE(-1);
634         }
635
636         /* TBD: Support multiple index (IRQ) entries per Link Device */
637         if (index) {
638                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid index %d\n", index));
639                 return_VALUE(-1);
640         }
641
642         down(&acpi_link_lock);
643         if (acpi_pci_link_allocate(link)) {
644                 up(&acpi_link_lock);
645                 return_VALUE(-1);
646         }
647
648         if (!link->irq.active) {
649                 up(&acpi_link_lock);
650                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link active IRQ is 0!\n"));
651                 return_VALUE(-1);
652         }
653         link->refcnt++;
654         up(&acpi_link_lock);
655
656         if (edge_level)
657                 *edge_level = link->irq.edge_level;
658         if (active_high_low)
659                 *active_high_low = link->irq.active_high_low;
660         if (name)
661                 *name = acpi_device_bid(link->device);
662         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
663                           "Link %s is referenced\n",
664                           acpi_device_bid(link->device)));
665         return_VALUE(link->irq.active);
666 }
667
668 /*
669  * We don't change link's irq information here.  After it is reenabled, we
670  * continue use the info
671  */
672 int acpi_pci_link_free_irq(acpi_handle handle)
673 {
674         struct acpi_device *device = NULL;
675         struct acpi_pci_link *link = NULL;
676         acpi_status result;
677
678         ACPI_FUNCTION_TRACE("acpi_pci_link_free_irq");
679
680         result = acpi_bus_get_device(handle, &device);
681         if (result) {
682                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link device\n"));
683                 return_VALUE(-1);
684         }
685
686         link = (struct acpi_pci_link *)acpi_driver_data(device);
687         if (!link) {
688                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid link context\n"));
689                 return_VALUE(-1);
690         }
691
692         down(&acpi_link_lock);
693         if (!link->irq.initialized) {
694                 up(&acpi_link_lock);
695                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Link isn't initialized\n"));
696                 return_VALUE(-1);
697         }
698 #ifdef  FUTURE_USE
699         /*
700          * The Link reference count allows us to _DISable an unused link
701          * and suspend time, and set it again  on resume.
702          * However, 2.6.12 still has irq_router.resume
703          * which blindly restores the link state.
704          * So we disable the reference count method
705          * to prevent duplicate acpi_pci_link_set()
706          * which would harm some systems
707          */
708         link->refcnt--;
709 #endif
710         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
711                           "Link %s is dereferenced\n",
712                           acpi_device_bid(link->device)));
713
714         if (link->refcnt == 0) {
715                 acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
716         }
717         up(&acpi_link_lock);
718         return_VALUE(link->irq.active);
719 }
720
721 /* --------------------------------------------------------------------------
722                                  Driver Interface
723    -------------------------------------------------------------------------- */
724
725 static int acpi_pci_link_add(struct acpi_device *device)
726 {
727         int result = 0;
728         struct acpi_pci_link *link = NULL;
729         int i = 0;
730         int found = 0;
731
732         ACPI_FUNCTION_TRACE("acpi_pci_link_add");
733
734         if (!device)
735                 return_VALUE(-EINVAL);
736
737         link = kmalloc(sizeof(struct acpi_pci_link), GFP_KERNEL);
738         if (!link)
739                 return_VALUE(-ENOMEM);
740         memset(link, 0, sizeof(struct acpi_pci_link));
741
742         link->device = device;
743         link->handle = device->handle;
744         strcpy(acpi_device_name(device), ACPI_PCI_LINK_DEVICE_NAME);
745         strcpy(acpi_device_class(device), ACPI_PCI_LINK_CLASS);
746         acpi_driver_data(device) = link;
747
748         down(&acpi_link_lock);
749         result = acpi_pci_link_get_possible(link);
750         if (result)
751                 goto end;
752
753         /* query and set link->irq.active */
754         acpi_pci_link_get_current(link);
755
756         printk(PREFIX "%s [%s] (IRQs", acpi_device_name(device),
757                acpi_device_bid(device));
758         for (i = 0; i < link->irq.possible_count; i++) {
759                 if (link->irq.active == link->irq.possible[i]) {
760                         printk(" *%d", link->irq.possible[i]);
761                         found = 1;
762                 } else
763                         printk(" %d", link->irq.possible[i]);
764         }
765
766         printk(")");
767
768         if (!found)
769                 printk(" *%d", link->irq.active);
770
771         if (!link->device->status.enabled)
772                 printk(", disabled.");
773
774         printk("\n");
775
776         /* TBD: Acquire/release lock */
777         list_add_tail(&link->node, &acpi_link.entries);
778         acpi_link.count++;
779
780       end:
781         /* disable all links -- to be activated on use */
782         acpi_ut_evaluate_object(link->handle, "_DIS", 0, NULL);
783         up(&acpi_link_lock);
784
785         if (result)
786                 kfree(link);
787
788         return_VALUE(result);
789 }
790
791 static int acpi_pci_link_resume(struct acpi_pci_link *link)
792 {
793         ACPI_FUNCTION_TRACE("acpi_pci_link_resume");
794
795         if (link->refcnt && link->irq.active && link->irq.initialized)
796                 return_VALUE(acpi_pci_link_set(link, link->irq.active));
797         else
798                 return_VALUE(0);
799 }
800
801 /*
802  * FIXME: this is a workaround to avoid nasty warning.  It will be removed
803  * after every device calls pci_disable_device in .resume.
804  */
805 int acpi_in_resume;
806 static int irqrouter_resume(struct sys_device *dev)
807 {
808         struct list_head *node = NULL;
809         struct acpi_pci_link *link = NULL;
810
811         ACPI_FUNCTION_TRACE("irqrouter_resume");
812
813         acpi_in_resume = 1;
814         list_for_each(node, &acpi_link.entries) {
815                 link = list_entry(node, struct acpi_pci_link, node);
816                 if (!link) {
817                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
818                                           "Invalid link context\n"));
819                         continue;
820                 }
821                 acpi_pci_link_resume(link);
822         }
823         acpi_in_resume = 0;
824         return_VALUE(0);
825 }
826
827 static int acpi_pci_link_remove(struct acpi_device *device, int type)
828 {
829         struct acpi_pci_link *link = NULL;
830
831         ACPI_FUNCTION_TRACE("acpi_pci_link_remove");
832
833         if (!device || !acpi_driver_data(device))
834                 return_VALUE(-EINVAL);
835
836         link = (struct acpi_pci_link *)acpi_driver_data(device);
837
838         down(&acpi_link_lock);
839         list_del(&link->node);
840         up(&acpi_link_lock);
841
842         kfree(link);
843
844         return_VALUE(0);
845 }
846
847 /*
848  * modify acpi_irq_penalty[] from cmdline
849  */
850 static int __init acpi_irq_penalty_update(char *str, int used)
851 {
852         int i;
853
854         for (i = 0; i < 16; i++) {
855                 int retval;
856                 int irq;
857
858                 retval = get_option(&str, &irq);
859
860                 if (!retval)
861                         break;  /* no number found */
862
863                 if (irq < 0)
864                         continue;
865
866                 if (irq >= ACPI_MAX_IRQS)
867                         continue;
868
869                 if (used)
870                         acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
871                 else
872                         acpi_irq_penalty[irq] = PIRQ_PENALTY_PCI_AVAILABLE;
873
874                 if (retval != 2)        /* no next number */
875                         break;
876         }
877         return 1;
878 }
879
880 /*
881  * We'd like PNP to call this routine for the
882  * single ISA_USED value for each legacy device.
883  * But instead it calls us with each POSSIBLE setting.
884  * There is no ISA_POSSIBLE weight, so we simply use
885  * the (small) PCI_USING penalty.
886  */
887 void acpi_penalize_isa_irq(int irq, int active)
888 {
889         if (active)
890                 acpi_irq_penalty[irq] += PIRQ_PENALTY_ISA_USED;
891         else
892                 acpi_irq_penalty[irq] += PIRQ_PENALTY_PCI_USING;
893 }
894
895 /*
896  * Over-ride default table to reserve additional IRQs for use by ISA
897  * e.g. acpi_irq_isa=5
898  * Useful for telling ACPI how not to interfere with your ISA sound card.
899  */
900 static int __init acpi_irq_isa(char *str)
901 {
902         return acpi_irq_penalty_update(str, 1);
903 }
904
905 __setup("acpi_irq_isa=", acpi_irq_isa);
906
907 /*
908  * Over-ride default table to free additional IRQs for use by PCI
909  * e.g. acpi_irq_pci=7,15
910  * Used for acpi_irq_balance to free up IRQs to reduce PCI IRQ sharing.
911  */
912 static int __init acpi_irq_pci(char *str)
913 {
914         return acpi_irq_penalty_update(str, 0);
915 }
916
917 __setup("acpi_irq_pci=", acpi_irq_pci);
918
919 static int __init acpi_irq_nobalance_set(char *str)
920 {
921         acpi_irq_balance = 0;
922         return 1;
923 }
924
925 __setup("acpi_irq_nobalance", acpi_irq_nobalance_set);
926
927 int __init acpi_irq_balance_set(char *str)
928 {
929         acpi_irq_balance = 1;
930         return 1;
931 }
932
933 __setup("acpi_irq_balance", acpi_irq_balance_set);
934
935 /* FIXME: we will remove this interface after all drivers call pci_disable_device */
936 static struct sysdev_class irqrouter_sysdev_class = {
937         set_kset_name("irqrouter"),
938         .resume = irqrouter_resume,
939 };
940
941 static struct sys_device device_irqrouter = {
942         .id = 0,
943         .cls = &irqrouter_sysdev_class,
944 };
945
946 static int __init irqrouter_init_sysfs(void)
947 {
948         int error;
949
950         ACPI_FUNCTION_TRACE("irqrouter_init_sysfs");
951
952         if (acpi_disabled || acpi_noirq)
953                 return_VALUE(0);
954
955         error = sysdev_class_register(&irqrouter_sysdev_class);
956         if (!error)
957                 error = sysdev_register(&device_irqrouter);
958
959         return_VALUE(error);
960 }
961
962 device_initcall(irqrouter_init_sysfs);
963
964 static int __init acpi_pci_link_init(void)
965 {
966         ACPI_FUNCTION_TRACE("acpi_pci_link_init");
967
968         if (acpi_noirq)
969                 return_VALUE(0);
970
971         acpi_link.count = 0;
972         INIT_LIST_HEAD(&acpi_link.entries);
973
974         if (acpi_bus_register_driver(&acpi_pci_link_driver) < 0)
975                 return_VALUE(-ENODEV);
976
977         return_VALUE(0);
978 }
979
980 subsys_initcall(acpi_pci_link_init);