bbf25f2b84ec5c75c123e1b8eb96e24f2b68e99f
[linux-flexiantxendom0-3.2.10.git] / drivers / pci / hotplug / acpiphp_glue.c
1 /*
2  * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3  *
4  * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5  * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6  * Copyright (C) 2002,2003 NEC Corporation
7  *
8  * All rights reserved.
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, GOOD TITLE or
18  * NON INFRINGEMENT.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  *
25  * Send feedback to <t-kochi@bq.jp.nec.com>
26  *
27  */
28
29 #include <linux/config.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/pci.h>
33 #include <linux/smp_lock.h>
34 #include <linux/init.h>
35 #include <asm/semaphore.h>
36
37 #include "../pci.h"
38 #include "pci_hotplug.h"
39 #include "acpiphp.h"
40
41 static LIST_HEAD(bridge_list);
42
43 #define MY_NAME "acpiphp_glue"
44
45 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
46 static void handle_hotplug_event_func (acpi_handle, u32, void *);
47
48 /*
49  * initialization & terminatation routines
50  */
51
52 /**
53  * is_ejectable - determine if a slot is ejectable
54  * @handle: handle to acpi namespace
55  *
56  * Ejectable slot should satisfy at least these conditions:
57  *
58  *  1. has _ADR method
59  *  2. has _EJ0 method
60  *
61  * optionally
62  *
63  *  1. has _STA method
64  *  2. has _PS0 method
65  *  3. has _PS3 method
66  *  4. ..
67  *
68  */
69 static int is_ejectable (acpi_handle handle)
70 {
71         acpi_status status;
72         acpi_handle tmp;
73
74         status = acpi_get_handle(handle, "_ADR", &tmp);
75         if (ACPI_FAILURE(status)) {
76                 return 0;
77         }
78
79         status = acpi_get_handle(handle, "_EJ0", &tmp);
80         if (ACPI_FAILURE(status)) {
81                 return 0;
82         }
83
84         return 1;
85 }
86
87
88 /* callback routine to check the existence of ejectable slots */
89 static acpi_status
90 is_ejectable_slot (acpi_handle handle, u32 lvl, void *context, void **rv)
91 {
92         int *count = (int *)context;
93
94         if (is_ejectable(handle)) {
95                 (*count)++;
96                 /* only one ejectable slot is enough */
97                 return AE_CTRL_TERMINATE;
98         } else {
99                 return AE_OK;
100         }
101 }
102
103
104 /* callback routine to register each ACPI PCI slot object */
105 static acpi_status
106 register_slot (acpi_handle handle, u32 lvl, void *context, void **rv)
107 {
108         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
109         struct acpiphp_slot *slot;
110         struct acpiphp_func *newfunc;
111         acpi_handle tmp;
112         acpi_status status = AE_OK;
113         unsigned long adr, sun;
114         int device, function;
115         static int num_slots = 0;       /* XXX if we support I/O node hotplug... */
116
117         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
118
119         if (ACPI_FAILURE(status))
120                 return AE_OK;
121
122         status = acpi_get_handle(handle, "_EJ0", &tmp);
123
124         if (ACPI_FAILURE(status))
125                 return AE_OK;
126
127         device = (adr >> 16) & 0xffff;
128         function = adr & 0xffff;
129
130         newfunc = kmalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
131         if (!newfunc)
132                 return AE_NO_MEMORY;
133         memset(newfunc, 0, sizeof(struct acpiphp_func));
134
135         INIT_LIST_HEAD(&newfunc->sibling);
136         newfunc->handle = handle;
137         newfunc->function = function;
138         newfunc->flags = FUNC_HAS_EJ0;
139
140         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
141                 newfunc->flags |= FUNC_HAS_STA;
142
143         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
144                 newfunc->flags |= FUNC_HAS_PS0;
145
146         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
147                 newfunc->flags |= FUNC_HAS_PS3;
148
149         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
150         if (ACPI_FAILURE(status))
151                 sun = -1;
152
153         /* search for objects that share the same slot */
154         for (slot = bridge->slots; slot; slot = slot->next)
155                 if (slot->device == device) {
156                         if (slot->sun != sun)
157                                 warn("sibling found, but _SUN doesn't match!\n");
158                         break;
159                 }
160
161         if (!slot) {
162                 slot = kmalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
163                 if (!slot) {
164                         kfree(newfunc);
165                         return AE_NO_MEMORY;
166                 }
167
168                 memset(slot, 0, sizeof(struct acpiphp_slot));
169                 slot->bridge = bridge;
170                 slot->id = num_slots++;
171                 slot->device = device;
172                 slot->sun = sun;
173                 INIT_LIST_HEAD(&slot->funcs);
174                 init_MUTEX(&slot->crit_sect);
175
176                 slot->next = bridge->slots;
177                 bridge->slots = slot;
178
179                 bridge->nr_slots++;
180
181                 dbg("found ACPI PCI Hotplug slot at PCI %02x:%02x Slot:%d\n",
182                     slot->bridge->bus, slot->device, slot->sun);
183         }
184
185         newfunc->slot = slot;
186         list_add_tail(&newfunc->sibling, &slot->funcs);
187
188         /* associate corresponding pci_dev */
189         newfunc->pci_dev = pci_find_slot(bridge->bus,
190                                          PCI_DEVFN(device, function));
191         if (newfunc->pci_dev) {
192                 if (acpiphp_init_func_resource(newfunc) < 0) {
193                         kfree(newfunc);
194                         return AE_ERROR;
195                 }
196                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
197         }
198
199         /* install notify handler */
200         status = acpi_install_notify_handler(handle,
201                                              ACPI_SYSTEM_NOTIFY,
202                                              handle_hotplug_event_func,
203                                              newfunc);
204
205         if (ACPI_FAILURE(status)) {
206                 err("failed to register interrupt notify handler\n");
207                 return status;
208         }
209
210         return AE_OK;
211 }
212
213
214 /* see if it's worth looking at this bridge */
215 static int detect_ejectable_slots (acpi_handle *bridge_handle)
216 {
217         acpi_status status;
218         int count;
219
220         count = 0;
221
222         /* only check slots defined directly below bridge object */
223         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
224                                      is_ejectable_slot, (void *)&count, NULL);
225
226         return count;
227 }
228
229
230 /* decode ACPI _CRS data and convert into our internal resource list
231  * TBD: _TRA, etc.
232  */
233 static acpi_status
234 decode_acpi_resource (struct acpi_resource *resource, void *context)
235 {
236         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *) context;
237         struct acpi_resource_address64 address;
238         struct pci_resource *res;
239
240         if (resource->id != ACPI_RSTYPE_ADDRESS16 &&
241             resource->id != ACPI_RSTYPE_ADDRESS32 &&
242             resource->id != ACPI_RSTYPE_ADDRESS64)
243                 return AE_OK;
244
245         acpi_resource_to_address64(resource, &address);
246
247         if (address.producer_consumer == ACPI_PRODUCER && address.address_length > 0) {
248                 dbg("resource type: %d: 0x%llx - 0x%llx\n", address.resource_type, address.min_address_range, address.max_address_range);
249                 res = acpiphp_make_resource(address.min_address_range,
250                                     address.address_length);
251                 if (!res) {
252                         err("out of memory\n");
253                         return AE_OK;
254                 }
255
256                 switch (address.resource_type) {
257                 case ACPI_MEMORY_RANGE:
258                         if (address.attribute.memory.cache_attribute == ACPI_PREFETCHABLE_MEMORY) {
259                                 res->next = bridge->p_mem_head;
260                                 bridge->p_mem_head = res;
261                         } else {
262                                 res->next = bridge->mem_head;
263                                 bridge->mem_head = res;
264                         }
265                         break;
266                 case ACPI_IO_RANGE:
267                         res->next = bridge->io_head;
268                         bridge->io_head = res;
269                         break;
270                 case ACPI_BUS_NUMBER_RANGE:
271                         res->next = bridge->bus_head;
272                         bridge->bus_head = res;
273                         break;
274                 default:
275                         /* invalid type */
276                         kfree(res);
277                         break;
278                 }
279         }
280
281         return AE_OK;
282 }
283
284 /* decode ACPI 2.0 _HPP hot plug parameters */
285 static void decode_hpp(struct acpiphp_bridge *bridge)
286 {
287         acpi_status status;
288         struct acpi_buffer buffer = { .length = ACPI_ALLOCATE_BUFFER,
289                                       .pointer = NULL};
290         union acpi_object *package;
291         int i;
292
293         /* default numbers */
294         bridge->hpp.cache_line_size = 0x10;
295         bridge->hpp.latency_timer = 0x40;
296         bridge->hpp.enable_SERR = 0;
297         bridge->hpp.enable_PERR = 0;
298
299         status = acpi_evaluate_object(bridge->handle, "_HPP", NULL, &buffer);
300
301         if (ACPI_FAILURE(status)) {
302                 dbg("_HPP evaluation failed\n");
303                 return;
304         }
305
306         package = (union acpi_object *) buffer.pointer;
307
308         if (!package || package->type != ACPI_TYPE_PACKAGE ||
309             package->package.count != 4 || !package->package.elements) {
310                 err("invalid _HPP object; ignoring\n");
311                 goto err_exit;
312         }
313
314         for (i = 0; i < 4; i++) {
315                 if (package->package.elements[i].type != ACPI_TYPE_INTEGER) {
316                         err("invalid _HPP parameter type; ignoring\n");
317                         goto err_exit;
318                 }
319         }
320
321         bridge->hpp.cache_line_size = package->package.elements[0].integer.value;
322         bridge->hpp.latency_timer = package->package.elements[1].integer.value;
323         bridge->hpp.enable_SERR = package->package.elements[2].integer.value;
324         bridge->hpp.enable_PERR = package->package.elements[3].integer.value;
325
326         dbg("_HPP parameter = (%02x, %02x, %02x, %02x)\n",
327             bridge->hpp.cache_line_size,
328             bridge->hpp.latency_timer,
329             bridge->hpp.enable_SERR,
330             bridge->hpp.enable_PERR);
331
332         bridge->flags |= BRIDGE_HAS_HPP;
333
334  err_exit:
335         kfree(buffer.pointer);
336 }
337
338
339 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
340 static void init_bridge_misc (struct acpiphp_bridge *bridge)
341 {
342         acpi_status status;
343
344         /* decode ACPI 2.0 _HPP (hot plug parameters) */
345         decode_hpp(bridge);
346
347         /* subtract all resources already allocated */
348         acpiphp_detect_pci_resource(bridge);
349
350         /* register all slot objects under this bridge */
351         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
352                                      register_slot, bridge, NULL);
353
354         /* install notify handler */
355         status = acpi_install_notify_handler(bridge->handle,
356                                              ACPI_SYSTEM_NOTIFY,
357                                              handle_hotplug_event_bridge,
358                                              bridge);
359
360         if (ACPI_FAILURE(status)) {
361                 err("failed to register interrupt notify handler\n");
362         }
363
364         list_add(&bridge->list, &bridge_list);
365
366         dbg("Bridge resource:\n");
367         acpiphp_dump_resource(bridge);
368 }
369
370
371 /* allocate and initialize host bridge data structure */
372 static void add_host_bridge (acpi_handle *handle, int seg, int bus)
373 {
374         acpi_status status;
375         struct acpiphp_bridge *bridge;
376
377         bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
378         if (bridge == NULL)
379                 return;
380
381         memset(bridge, 0, sizeof(struct acpiphp_bridge));
382
383         bridge->type = BRIDGE_TYPE_HOST;
384         bridge->handle = handle;
385         bridge->seg = seg;
386         bridge->bus = bus;
387
388         bridge->pci_bus = pci_find_bus(seg, bus);
389
390         bridge->res_lock = SPIN_LOCK_UNLOCKED;
391
392         /* to be overridden when we decode _CRS */
393         bridge->sub = bridge->bus;
394
395         /* decode resources */
396
397         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
398                 decode_acpi_resource, bridge);
399
400         if (ACPI_FAILURE(status)) {
401                 err("failed to decode bridge resources\n");
402                 kfree(bridge);
403                 return;
404         }
405
406         acpiphp_resource_sort_and_combine(&bridge->io_head);
407         acpiphp_resource_sort_and_combine(&bridge->mem_head);
408         acpiphp_resource_sort_and_combine(&bridge->p_mem_head);
409         acpiphp_resource_sort_and_combine(&bridge->bus_head);
410
411         dbg("ACPI _CRS resource:\n");
412         acpiphp_dump_resource(bridge);
413
414         if (bridge->bus_head) {
415                 bridge->bus = bridge->bus_head->base;
416                 bridge->sub = bridge->bus_head->base + bridge->bus_head->length - 1;
417         }
418
419         init_bridge_misc(bridge);
420 }
421
422
423 /* allocate and initialize PCI-to-PCI bridge data structure */
424 static void add_p2p_bridge (acpi_handle *handle, int seg, int bus, int dev, int fn)
425 {
426         struct acpiphp_bridge *bridge;
427         u8 tmp8;
428         u16 tmp16;
429         u64 base64, limit64;
430         u32 base, limit, base32u, limit32u;
431
432         bridge = kmalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
433         if (bridge == NULL) {
434                 err("out of memory\n");
435                 return;
436         }
437
438         memset(bridge, 0, sizeof(struct acpiphp_bridge));
439
440         bridge->type = BRIDGE_TYPE_P2P;
441         bridge->handle = handle;
442         bridge->seg = seg;
443
444         bridge->pci_dev = pci_find_slot(bus, PCI_DEVFN(dev, fn));
445         if (!bridge->pci_dev) {
446                 err("Can't get pci_dev\n");
447                 kfree(bridge);
448                 return;
449         }
450
451         bridge->pci_bus = bridge->pci_dev->subordinate;
452         if (!bridge->pci_bus) {
453                 err("This is not a PCI-to-PCI bridge!\n");
454                 kfree(bridge);
455                 return;
456         }
457
458         bridge->res_lock = SPIN_LOCK_UNLOCKED;
459
460         bridge->bus = bridge->pci_bus->number;
461         bridge->sub = bridge->pci_bus->subordinate;
462
463         /*
464          * decode resources under this P2P bridge
465          */
466
467         /* I/O resources */
468         pci_read_config_byte(bridge->pci_dev, PCI_IO_BASE, &tmp8);
469         base = tmp8;
470         pci_read_config_byte(bridge->pci_dev, PCI_IO_LIMIT, &tmp8);
471         limit = tmp8;
472
473         switch (base & PCI_IO_RANGE_TYPE_MASK) {
474         case PCI_IO_RANGE_TYPE_16:
475                 base = (base << 8) & 0xf000;
476                 limit = ((limit << 8) & 0xf000) + 0xfff;
477                 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
478                 if (!bridge->io_head) {
479                         err("out of memory\n");
480                         kfree(bridge);
481                         return;
482                 }
483                 dbg("16bit I/O range: %04x-%04x\n",
484                     (u32)bridge->io_head->base,
485                     (u32)(bridge->io_head->base + bridge->io_head->length - 1));
486                 break;
487         case PCI_IO_RANGE_TYPE_32:
488                 pci_read_config_word(bridge->pci_dev, PCI_IO_BASE_UPPER16, &tmp16);
489                 base = ((u32)tmp16 << 16) | ((base << 8) & 0xf000);
490                 pci_read_config_word(bridge->pci_dev, PCI_IO_LIMIT_UPPER16, &tmp16);
491                 limit = (((u32)tmp16 << 16) | ((limit << 8) & 0xf000)) + 0xfff;
492                 bridge->io_head = acpiphp_make_resource((u64)base, limit - base + 1);
493                 if (!bridge->io_head) {
494                         err("out of memory\n");
495                         kfree(bridge);
496                         return;
497                 }
498                 dbg("32bit I/O range: %08x-%08x\n",
499                     (u32)bridge->io_head->base,
500                     (u32)(bridge->io_head->base + bridge->io_head->length - 1));
501                 break;
502         case 0x0f:
503                 dbg("I/O space unsupported\n");
504                 break;
505         default:
506                 warn("Unknown I/O range type\n");
507         }
508
509         /* Memory resources (mandatory for P2P bridge) */
510         pci_read_config_word(bridge->pci_dev, PCI_MEMORY_BASE, &tmp16);
511         base = (tmp16 & 0xfff0) << 16;
512         pci_read_config_word(bridge->pci_dev, PCI_MEMORY_LIMIT, &tmp16);
513         limit = ((tmp16 & 0xfff0) << 16) | 0xfffff;
514         bridge->mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
515         if (!bridge->mem_head) {
516                 err("out of memory\n");
517                 kfree(bridge);
518                 return;
519         }
520         dbg("32bit Memory range: %08x-%08x\n",
521             (u32)bridge->mem_head->base,
522             (u32)(bridge->mem_head->base + bridge->mem_head->length-1));
523
524         /* Prefetchable Memory resources (optional) */
525         pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_BASE, &tmp16);
526         base = tmp16;
527         pci_read_config_word(bridge->pci_dev, PCI_PREF_MEMORY_LIMIT, &tmp16);
528         limit = tmp16;
529
530         switch (base & PCI_MEMORY_RANGE_TYPE_MASK) {
531         case PCI_PREF_RANGE_TYPE_32:
532                 base = (base & 0xfff0) << 16;
533                 limit = ((limit & 0xfff0) << 16) | 0xfffff;
534                 bridge->p_mem_head = acpiphp_make_resource((u64)base, limit - base + 1);
535                 if (!bridge->p_mem_head) {
536                         err("out of memory\n");
537                         kfree(bridge);
538                         return;
539                 }
540                 dbg("32bit Prefetchable memory range: %08x-%08x\n",
541                     (u32)bridge->p_mem_head->base,
542                     (u32)(bridge->p_mem_head->base + bridge->p_mem_head->length - 1));
543                 break;
544         case PCI_PREF_RANGE_TYPE_64:
545                 pci_read_config_dword(bridge->pci_dev, PCI_PREF_BASE_UPPER32, &base32u);
546                 pci_read_config_dword(bridge->pci_dev, PCI_PREF_LIMIT_UPPER32, &limit32u);
547                 base64 = ((u64)base32u << 32) | ((base & 0xfff0) << 16);
548                 limit64 = (((u64)limit32u << 32) | ((limit & 0xfff0) << 16)) + 0xfffff;
549
550                 bridge->p_mem_head = acpiphp_make_resource(base64, limit64 - base64 + 1);
551                 if (!bridge->p_mem_head) {
552                         err("out of memory\n");
553                         kfree(bridge);
554                         return;
555                 }
556                 dbg("64bit Prefetchable memory range: %08x%08x-%08x%08x\n",
557                     (u32)(bridge->p_mem_head->base >> 32),
558                     (u32)(bridge->p_mem_head->base & 0xffffffff),
559                     (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) >> 32),
560                     (u32)((bridge->p_mem_head->base + bridge->p_mem_head->length - 1) & 0xffffffff));
561                 break;
562         case 0x0f:
563                 break;
564         default:
565                 warn("Unknown prefetchale memory type\n");
566         }
567
568         init_bridge_misc(bridge);
569 }
570
571
572 /* callback routine to find P2P bridges */
573 static acpi_status
574 find_p2p_bridge (acpi_handle handle, u32 lvl, void *context, void **rv)
575 {
576         acpi_status status;
577         acpi_handle dummy_handle;
578         unsigned long *segbus = context;
579         unsigned long tmp;
580         int seg, bus, device, function;
581         struct pci_dev *dev;
582
583         /* get PCI address */
584         seg = (*segbus >> 8) & 0xff;
585         bus = *segbus & 0xff;
586
587         status = acpi_get_handle(handle, "_ADR", &dummy_handle);
588         if (ACPI_FAILURE(status))
589                 return AE_OK;           /* continue */
590
591         status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
592         if (ACPI_FAILURE(status)) {
593                 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
594                 return AE_OK;
595         }
596
597         device = (tmp >> 16) & 0xffff;
598         function = tmp & 0xffff;
599
600         dev = pci_find_slot(bus, PCI_DEVFN(device, function));
601
602         if (!dev)
603                 return AE_OK;
604
605         if (!dev->subordinate)
606                 return AE_OK;
607
608         /* check if this bridge has ejectable slots */
609         if (detect_ejectable_slots(handle) > 0) {
610                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
611                 add_p2p_bridge(handle, seg, bus, device, function);
612         }
613
614         return AE_OK;
615 }
616
617
618 /* find hot-pluggable slots, and then find P2P bridge */
619 static int add_bridge(acpi_handle handle)
620 {
621         acpi_status status;
622         unsigned long tmp;
623         int seg, bus;
624         acpi_handle dummy_handle;
625
626         /* if the bridge doesn't have _STA, we assume it is always there */
627         status = acpi_get_handle(handle, "_STA", &dummy_handle);
628         if (ACPI_SUCCESS(status)) {
629                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
630                 if (ACPI_FAILURE(status)) {
631                         dbg("%s: _STA evaluation failure\n", __FUNCTION__);
632                         return 0;
633                 }
634                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
635                         /* don't register this object */
636                         return 0;
637         }
638
639         /* get PCI segment number */
640         status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
641
642         seg = ACPI_SUCCESS(status) ? tmp : 0;
643
644         /* get PCI bus number */
645         status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
646
647         if (ACPI_SUCCESS(status)) {
648                 bus = tmp;
649         } else {
650                 warn("can't get bus number, assuming 0\n");
651                 bus = 0;
652         }
653
654         /* check if this bridge has ejectable slots */
655         if (detect_ejectable_slots(handle) > 0) {
656                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
657                 add_host_bridge(handle, seg, bus);
658                 return 0;
659         }
660
661         tmp = seg << 8 | bus;
662
663         /* search P2P bridges under this host bridge */
664         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
665                                      find_p2p_bridge, &tmp, NULL);
666
667         if (ACPI_FAILURE(status))
668                 warn("find_p2p_bridge faied (error code = 0x%x)\n",status);
669
670         return 0;
671 }
672
673
674 static void remove_bridge (acpi_handle handle)
675 {
676         /* No-op for now .. */
677 }
678
679
680 static int power_on_slot (struct acpiphp_slot *slot)
681 {
682         acpi_status status;
683         struct acpiphp_func *func;
684         struct list_head *l;
685         int retval = 0;
686
687         /* is this already enabled? */
688         if (slot->flags & SLOT_POWEREDON)
689                 goto err_exit;
690
691         list_for_each (l, &slot->funcs) {
692                 func = list_entry(l, struct acpiphp_func, sibling);
693
694                 if (func->flags & FUNC_HAS_PS0) {
695                         dbg("%s: executing _PS0 on %s\n", __FUNCTION__,
696                             pci_name(func->pci_dev));
697                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
698                         if (ACPI_FAILURE(status)) {
699                                 warn("%s: _PS0 failed\n", __FUNCTION__);
700                                 retval = -1;
701                                 goto err_exit;
702                         }
703                 }
704         }
705
706         /* TBD: evaluate _STA to check if the slot is enabled */
707
708         slot->flags |= SLOT_POWEREDON;
709
710  err_exit:
711         return retval;
712 }
713
714
715 static int power_off_slot (struct acpiphp_slot *slot)
716 {
717         acpi_status status;
718         struct acpiphp_func *func;
719         struct list_head *l;
720         struct acpi_object_list arg_list;
721         union acpi_object arg;
722
723         int retval = 0;
724
725         /* is this already enabled? */
726         if ((slot->flags & SLOT_POWEREDON) == 0)
727                 goto err_exit;
728
729         list_for_each (l, &slot->funcs) {
730                 func = list_entry(l, struct acpiphp_func, sibling);
731
732                 if (func->flags & (FUNC_HAS_PS3 | FUNC_EXISTS)) {
733                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
734                         if (ACPI_FAILURE(status)) {
735                                 warn("%s: _PS3 failed\n", __FUNCTION__);
736                                 retval = -1;
737                                 goto err_exit;
738                         }
739                 }
740         }
741
742         list_for_each (l, &slot->funcs) {
743                 func = list_entry(l, struct acpiphp_func, sibling);
744
745                 /* We don't want to call _EJ0 on non-existing functions. */
746                 if (func->flags & (FUNC_HAS_EJ0 | FUNC_EXISTS)) {
747                         /* _EJ0 method take one argument */
748                         arg_list.count = 1;
749                         arg_list.pointer = &arg;
750                         arg.type = ACPI_TYPE_INTEGER;
751                         arg.integer.value = 1;
752
753                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
754                         if (ACPI_FAILURE(status)) {
755                                 warn("%s: _EJ0 failed\n", __FUNCTION__);
756                                 retval = -1;
757                                 goto err_exit;
758                         }
759                         func->flags &= (~FUNC_EXISTS);
760                 }
761         }
762
763         /* TBD: evaluate _STA to check if the slot is disabled */
764
765         slot->flags &= (~SLOT_POWEREDON);
766
767  err_exit:
768         return retval;
769 }
770
771
772 /**
773  * enable_device - enable, configure a slot
774  * @slot: slot to be enabled
775  *
776  * This function should be called per *physical slot*,
777  * not per each slot object in ACPI namespace.
778  *
779  */
780 static int enable_device (struct acpiphp_slot *slot)
781 {
782         u8 bus;
783         struct pci_dev *dev;
784         struct pci_bus *child;
785         struct list_head *l;
786         struct acpiphp_func *func;
787         int retval = 0;
788         int num;
789
790         if (slot->flags & SLOT_ENABLED)
791                 goto err_exit;
792
793         /* sanity check: dev should be NULL when hot-plugged in */
794         dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
795         if (dev) {
796                 /* This case shouldn't happen */
797                 err("pci_dev structure already exists.\n");
798                 retval = -1;
799                 goto err_exit;
800         }
801
802         /* allocate resources to device */
803         retval = acpiphp_configure_slot(slot);
804         if (retval)
805                 goto err_exit;
806
807         /* returned `dev' is the *first function* only! */
808         num = pci_scan_slot(slot->bridge->pci_bus, PCI_DEVFN(slot->device, 0));
809         if (num)
810                 pci_bus_add_devices(slot->bridge->pci_bus);
811         dev = pci_find_slot(slot->bridge->bus, PCI_DEVFN(slot->device, 0));
812
813         if (!dev) {
814                 err("No new device found\n");
815                 retval = -1;
816                 goto err_exit;
817         }
818
819         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
820                 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &bus);
821                 child = (struct pci_bus*) pci_add_new_bus(dev->bus, dev, bus);
822                 pci_do_scan_bus(child);
823         }
824
825         /* associate pci_dev to our representation */
826         list_for_each (l, &slot->funcs) {
827                 func = list_entry(l, struct acpiphp_func, sibling);
828
829                 func->pci_dev = pci_find_slot(slot->bridge->bus,
830                                               PCI_DEVFN(slot->device,
831                                                         func->function));
832                 if (!func->pci_dev)
833                         continue;
834
835                 /* configure device */
836                 retval = acpiphp_configure_function(func);
837                 if (retval)
838                         goto err_exit;
839
840                 func->flags |= FUNC_EXISTS;
841         }
842
843         slot->flags |= SLOT_ENABLED;
844
845         dbg("Available resources:\n");
846         acpiphp_dump_resource(slot->bridge);
847
848  err_exit:
849         return retval;
850 }
851
852
853 /**
854  * disable_device - disable a slot
855  */
856 static int disable_device (struct acpiphp_slot *slot)
857 {
858         int retval = 0;
859         struct acpiphp_func *func;
860         struct list_head *l;
861
862         /* is this slot already disabled? */
863         if (!(slot->flags & SLOT_ENABLED))
864                 goto err_exit;
865
866         list_for_each (l, &slot->funcs) {
867                 func = list_entry(l, struct acpiphp_func, sibling);
868
869                 if (func->pci_dev) {
870                         if (acpiphp_unconfigure_function(func) == 0) {
871                                 func->pci_dev = NULL;
872                         } else {
873                                 err("failed to unconfigure device\n");
874                                 retval = -1;
875                                 goto err_exit;
876                         }
877                 }
878         }
879
880         slot->flags &= (~SLOT_ENABLED);
881
882  err_exit:
883         return retval;
884 }
885
886
887 /**
888  * get_slot_status - get ACPI slot status
889  *
890  * if a slot has _STA for each function and if any one of them
891  * returned non-zero status, return it
892  *
893  * if a slot doesn't have _STA and if any one of its functions'
894  * configuration space is configured, return 0x0f as a _STA
895  *
896  * otherwise return 0
897  */
898 static unsigned int get_slot_status (struct acpiphp_slot *slot)
899 {
900         acpi_status status;
901         unsigned long sta = 0;
902         u32 dvid;
903         struct list_head *l;
904         struct acpiphp_func *func;
905
906         list_for_each (l, &slot->funcs) {
907                 func = list_entry(l, struct acpiphp_func, sibling);
908
909                 if (func->flags & FUNC_HAS_STA) {
910                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
911                         if (ACPI_SUCCESS(status) && sta)
912                                 break;
913                 } else {
914                         pci_bus_read_config_dword(slot->bridge->pci_bus,
915                                                   PCI_DEVFN(slot->device,
916                                                             func->function),
917                                                   PCI_VENDOR_ID, &dvid);
918                         if (dvid != 0xffffffff) {
919                                 sta = ACPI_STA_ALL;
920                                 break;
921                         }
922                 }
923         }
924
925         return (unsigned int)sta;
926 }
927
928
929 /*
930  * ACPI event handlers
931  */
932
933 /**
934  * handle_hotplug_event_bridge - handle ACPI event on bridges
935  *
936  * @handle: Notify()'ed acpi_handle
937  * @type: Notify code
938  * @context: pointer to acpiphp_bridge structure
939  *
940  * handles ACPI event notification on {host,p2p} bridges
941  *
942  */
943 static void handle_hotplug_event_bridge (acpi_handle handle, u32 type, void *context)
944 {
945         struct acpiphp_bridge *bridge;
946         char objname[64];
947         struct acpi_buffer buffer = { .length = sizeof(objname),
948                                       .pointer = objname };
949
950         bridge = (struct acpiphp_bridge *)context;
951
952         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
953
954         switch (type) {
955         case ACPI_NOTIFY_BUS_CHECK:
956                 /* bus re-enumerate */
957                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
958                 acpiphp_check_bridge(bridge);
959                 break;
960
961         case ACPI_NOTIFY_DEVICE_CHECK:
962                 /* device check */
963                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
964                 acpiphp_check_bridge(bridge);
965                 break;
966
967         case ACPI_NOTIFY_DEVICE_WAKE:
968                 /* wake event */
969                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
970                 break;
971
972         case ACPI_NOTIFY_EJECT_REQUEST:
973                 /* request device eject */
974                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
975                 break;
976
977         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
978                 printk(KERN_ERR "Device %s cannot be configured due"
979                                 " to a frequency mismatch\n", objname);
980                 break;
981
982         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
983                 printk(KERN_ERR "Device %s cannot be configured due"
984                                 " to a bus mode mismatch\n", objname);
985                 break;
986
987         case ACPI_NOTIFY_POWER_FAULT:
988                 printk(KERN_ERR "Device %s has suffered a power fault\n",
989                                 objname);
990                 break;
991
992         default:
993                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
994                 break;
995         }
996 }
997
998
999 /**
1000  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1001  *
1002  * @handle: Notify()'ed acpi_handle
1003  * @type: Notify code
1004  * @context: pointer to acpiphp_func structure
1005  *
1006  * handles ACPI event notification on slots
1007  *
1008  */
1009 static void handle_hotplug_event_func (acpi_handle handle, u32 type, void *context)
1010 {
1011         struct acpiphp_func *func;
1012         char objname[64];
1013         struct acpi_buffer buffer = { .length = sizeof(objname),
1014                                       .pointer = objname };
1015
1016         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1017
1018         func = (struct acpiphp_func *)context;
1019
1020         switch (type) {
1021         case ACPI_NOTIFY_BUS_CHECK:
1022                 /* bus re-enumerate */
1023                 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1024                 acpiphp_enable_slot(func->slot);
1025                 break;
1026
1027         case ACPI_NOTIFY_DEVICE_CHECK:
1028                 /* device check : re-enumerate from parent bus */
1029                 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1030                 acpiphp_check_bridge(func->slot->bridge);
1031                 break;
1032
1033         case ACPI_NOTIFY_DEVICE_WAKE:
1034                 /* wake event */
1035                 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1036                 break;
1037
1038         case ACPI_NOTIFY_EJECT_REQUEST:
1039                 /* request device eject */
1040                 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
1041                 acpiphp_disable_slot(func->slot);
1042                 break;
1043
1044         default:
1045                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1046                 break;
1047         }
1048 }
1049
1050
1051 static struct acpi_pci_driver acpi_pci_hp_driver = {
1052         .add =          add_bridge,
1053         .remove =       remove_bridge,
1054 };
1055
1056 /**
1057  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1058  *
1059  */
1060 int acpiphp_glue_init (void)
1061 {
1062         int num;
1063
1064         if (list_empty(&pci_root_buses))
1065                 return -1;
1066
1067         num = acpi_pci_register_driver(&acpi_pci_hp_driver);
1068
1069         if (num <= 0)
1070                 return -1;
1071
1072         return 0;
1073 }
1074
1075
1076 /**
1077  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1078  *
1079  * This function frees all data allocated in acpiphp_glue_init()
1080  */
1081 void acpiphp_glue_exit (void)
1082 {
1083         struct list_head *l1, *l2, *n1, *n2;
1084         struct acpiphp_bridge *bridge;
1085         struct acpiphp_slot *slot, *next;
1086         struct acpiphp_func *func;
1087         acpi_status status;
1088
1089         list_for_each_safe (l1, n1, &bridge_list) {
1090                 bridge = (struct acpiphp_bridge *)l1;
1091                 slot = bridge->slots;
1092                 while (slot) {
1093                         next = slot->next;
1094                         list_for_each_safe (l2, n2, &slot->funcs) {
1095                                 func = list_entry(l2, struct acpiphp_func, sibling);
1096                                 acpiphp_free_resource(&func->io_head);
1097                                 acpiphp_free_resource(&func->mem_head);
1098                                 acpiphp_free_resource(&func->p_mem_head);
1099                                 acpiphp_free_resource(&func->bus_head);
1100                                 status = acpi_remove_notify_handler(func->handle,
1101                                                                     ACPI_SYSTEM_NOTIFY,
1102                                                                     handle_hotplug_event_func);
1103                                 if (ACPI_FAILURE(status))
1104                                         err("failed to remove notify handler\n");
1105                                 kfree(func);
1106                         }
1107                         kfree(slot);
1108                         slot = next;
1109                 }
1110                 status = acpi_remove_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1111                                                     handle_hotplug_event_bridge);
1112                 if (ACPI_FAILURE(status))
1113                         err("failed to remove notify handler\n");
1114
1115                 acpiphp_free_resource(&bridge->io_head);
1116                 acpiphp_free_resource(&bridge->mem_head);
1117                 acpiphp_free_resource(&bridge->p_mem_head);
1118                 acpiphp_free_resource(&bridge->bus_head);
1119
1120                 kfree(bridge);
1121         }
1122 }
1123
1124
1125 /**
1126  * acpiphp_get_num_slots - count number of slots in a system
1127  */
1128 int acpiphp_get_num_slots (void)
1129 {
1130         struct list_head *node;
1131         struct acpiphp_bridge *bridge;
1132         int num_slots;
1133
1134         num_slots = 0;
1135
1136         list_for_each (node, &bridge_list) {
1137                 bridge = (struct acpiphp_bridge *)node;
1138                 dbg("Bus%d %dslot(s)\n", bridge->bus, bridge->nr_slots);
1139                 num_slots += bridge->nr_slots;
1140         }
1141
1142         dbg("Total %dslots\n", num_slots);
1143         return num_slots;
1144 }
1145
1146
1147 /**
1148  * acpiphp_for_each_slot - call function for each slot
1149  * @fn: callback function
1150  * @data: context to be passed to callback function
1151  *
1152  */
1153 int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1154 {
1155         struct list_head *node;
1156         struct acpiphp_bridge *bridge;
1157         struct acpiphp_slot *slot;
1158         int retval = 0;
1159
1160         list_for_each (node, &bridge_list) {
1161                 bridge = (struct acpiphp_bridge *)node;
1162                 for (slot = bridge->slots; slot; slot = slot->next) {
1163                         retval = fn(slot, data);
1164                         if (!retval)
1165                                 goto err_exit;
1166                 }
1167         }
1168
1169  err_exit:
1170         return retval;
1171 }
1172
1173
1174 /* search matching slot from id  */
1175 struct acpiphp_slot *get_slot_from_id (int id)
1176 {
1177         struct list_head *node;
1178         struct acpiphp_bridge *bridge;
1179         struct acpiphp_slot *slot;
1180
1181         list_for_each (node, &bridge_list) {
1182                 bridge = (struct acpiphp_bridge *)node;
1183                 for (slot = bridge->slots; slot; slot = slot->next)
1184                         if (slot->id == id)
1185                                 return slot;
1186         }
1187
1188         /* should never happen! */
1189         err("%s: no object for id %d\n",__FUNCTION__, id);
1190         return 0;
1191 }
1192
1193
1194 /**
1195  * acpiphp_enable_slot - power on slot
1196  */
1197 int acpiphp_enable_slot (struct acpiphp_slot *slot)
1198 {
1199         int retval;
1200
1201         down(&slot->crit_sect);
1202
1203         /* wake up all functions */
1204         retval = power_on_slot(slot);
1205         if (retval)
1206                 goto err_exit;
1207
1208         if (get_slot_status(slot) == ACPI_STA_ALL)
1209                 /* configure all functions */
1210                 retval = enable_device(slot);
1211
1212  err_exit:
1213         up(&slot->crit_sect);
1214         return retval;
1215 }
1216
1217
1218 /**
1219  * acpiphp_disable_slot - power off slot
1220  */
1221 int acpiphp_disable_slot (struct acpiphp_slot *slot)
1222 {
1223         int retval = 0;
1224
1225         down(&slot->crit_sect);
1226
1227         /* unconfigure all functions */
1228         retval = disable_device(slot);
1229         if (retval)
1230                 goto err_exit;
1231
1232         /* power off all functions */
1233         retval = power_off_slot(slot);
1234         if (retval)
1235                 goto err_exit;
1236
1237         acpiphp_resource_sort_and_combine(&slot->bridge->io_head);
1238         acpiphp_resource_sort_and_combine(&slot->bridge->mem_head);
1239         acpiphp_resource_sort_and_combine(&slot->bridge->p_mem_head);
1240         acpiphp_resource_sort_and_combine(&slot->bridge->bus_head);
1241         dbg("Available resources:\n");
1242         acpiphp_dump_resource(slot->bridge);
1243
1244  err_exit:
1245         up(&slot->crit_sect);
1246         return retval;
1247 }
1248
1249
1250 /**
1251  * acpiphp_check_bridge - re-enumerate devices
1252  */
1253 int acpiphp_check_bridge (struct acpiphp_bridge *bridge)
1254 {
1255         struct acpiphp_slot *slot;
1256         unsigned int sta;
1257         int retval = 0;
1258         int enabled, disabled;
1259
1260         enabled = disabled = 0;
1261
1262         for (slot = bridge->slots; slot; slot = slot->next) {
1263                 sta = get_slot_status(slot);
1264                 if (slot->flags & SLOT_ENABLED) {
1265                         /* if enabled but not present, disable */
1266                         if (sta != ACPI_STA_ALL) {
1267                                 retval = acpiphp_disable_slot(slot);
1268                                 if (retval) {
1269                                         err("Error occurred in enabling\n");
1270                                         up(&slot->crit_sect);
1271                                         goto err_exit;
1272                                 }
1273                                 enabled++;
1274                         }
1275                 } else {
1276                         /* if disabled but present, enable */
1277                         if (sta == ACPI_STA_ALL) {
1278                                 retval = acpiphp_enable_slot(slot);
1279                                 if (retval) {
1280                                         err("Error occurred in enabling\n");
1281                                         up(&slot->crit_sect);
1282                                         goto err_exit;
1283                                 }
1284                                 disabled++;
1285                         }
1286                 }
1287         }
1288
1289         dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1290
1291  err_exit:
1292         return retval;
1293 }
1294
1295
1296 /*
1297  * slot enabled:  1
1298  * slot disabled: 0
1299  */
1300 u8 acpiphp_get_power_status (struct acpiphp_slot *slot)
1301 {
1302         unsigned int sta;
1303
1304         sta = get_slot_status(slot);
1305
1306         return (sta & ACPI_STA_ENABLED) ? 1 : 0;
1307 }
1308
1309
1310 /*
1311  * attention LED ON: 1
1312  *              OFF: 0
1313  *
1314  * TBD
1315  * no direct attention led status information via ACPI
1316  *
1317  */
1318 u8 acpiphp_get_attention_status (struct acpiphp_slot *slot)
1319 {
1320         return 0;
1321 }
1322
1323
1324 /*
1325  * latch closed:  1
1326  * latch   open:  0
1327  */
1328 u8 acpiphp_get_latch_status (struct acpiphp_slot *slot)
1329 {
1330         unsigned int sta;
1331
1332         sta = get_slot_status(slot);
1333
1334         return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1335 }
1336
1337
1338 /*
1339  * adapter presence : 1
1340  *          absence : 0
1341  */
1342 u8 acpiphp_get_adapter_status (struct acpiphp_slot *slot)
1343 {
1344         unsigned int sta;
1345
1346         sta = get_slot_status(slot);
1347
1348         return (sta == 0) ? 0 : 1;
1349 }