]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/hotplug/acpiphp_glue.c
PCI hotplug: clean up acpi_get_hp_params_from_firmware() interface
[karo-tx-linux.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  * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8  * Copyright (C) 2003-2005 Hewlett Packard
9  * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10  * Copyright (C) 2005 Intel Corporation
11  *
12  * All rights reserved.
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or (at
17  * your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22  * NON INFRINGEMENT.  See the GNU General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  * Send feedback to <kristen.c.accardi@intel.com>
30  *
31  */
32
33 /*
34  * Lifetime rules for pci_dev:
35  *  - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36  *    when the bridge is scanned and it loses a refcount when the bridge
37  *    is removed.
38  *  - When a P2P bridge is present, we elevate the refcount on the subordinate
39  *    bus. It loses the refcount when the the driver unloads.
40  */
41
42 #include <linux/init.h>
43 #include <linux/module.h>
44
45 #include <linux/kernel.h>
46 #include <linux/pci.h>
47 #include <linux/pci_hotplug.h>
48 #include <linux/pci-acpi.h>
49 #include <linux/mutex.h>
50
51 #include "../pci.h"
52 #include "acpiphp.h"
53
54 static LIST_HEAD(bridge_list);
55 static LIST_HEAD(ioapic_list);
56 static DEFINE_SPINLOCK(ioapic_list_lock);
57
58 #define MY_NAME "acpiphp_glue"
59
60 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
61 static void acpiphp_sanitize_bus(struct pci_bus *bus);
62 static void acpiphp_set_hpp_values(struct pci_bus *bus);
63 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
64
65 /* callback routine to check for the existence of a pci dock device */
66 static acpi_status
67 is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68 {
69         int *count = (int *)context;
70
71         if (is_dock_device(handle)) {
72                 (*count)++;
73                 return AE_CTRL_TERMINATE;
74         } else {
75                 return AE_OK;
76         }
77 }
78
79 /*
80  * the _DCK method can do funny things... and sometimes not
81  * hah-hah funny.
82  *
83  * TBD - figure out a way to only call fixups for
84  * systems that require them.
85  */
86 static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87         void *v)
88 {
89         struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90         struct pci_bus *bus = func->slot->bridge->pci_bus;
91         u32 buses;
92
93         if (!bus->self)
94                 return  NOTIFY_OK;
95
96         /* fixup bad _DCK function that rewrites
97          * secondary bridge on slot
98          */
99         pci_read_config_dword(bus->self,
100                         PCI_PRIMARY_BUS,
101                         &buses);
102
103         if (((buses >> 8) & 0xff) != bus->secondary) {
104                 buses = (buses & 0xff000000)
105                         | ((unsigned int)(bus->primary)     <<  0)
106                         | ((unsigned int)(bus->secondary)   <<  8)
107                         | ((unsigned int)(bus->subordinate) << 16);
108                 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109         }
110         return NOTIFY_OK;
111 }
112
113
114 static struct acpi_dock_ops acpiphp_dock_ops = {
115         .handler = handle_hotplug_event_func,
116 };
117
118 /* callback routine to register each ACPI PCI slot object */
119 static acpi_status
120 register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
121 {
122         struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123         struct acpiphp_slot *slot;
124         struct acpiphp_func *newfunc;
125         acpi_handle tmp;
126         acpi_status status = AE_OK;
127         unsigned long long adr, sun;
128         int device, function, retval;
129         struct pci_bus *pbus = bridge->pci_bus;
130         struct pci_dev *pdev;
131
132         if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
133                 return AE_OK;
134
135         acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136         device = (adr >> 16) & 0xffff;
137         function = adr & 0xffff;
138
139         newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
140         if (!newfunc)
141                 return AE_NO_MEMORY;
142
143         INIT_LIST_HEAD(&newfunc->sibling);
144         newfunc->handle = handle;
145         newfunc->function = function;
146
147         if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
148                 newfunc->flags = FUNC_HAS_EJ0;
149
150         if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
151                 newfunc->flags |= FUNC_HAS_STA;
152
153         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
154                 newfunc->flags |= FUNC_HAS_PS0;
155
156         if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
157                 newfunc->flags |= FUNC_HAS_PS3;
158
159         if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
160                 newfunc->flags |= FUNC_HAS_DCK;
161
162         status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
163         if (ACPI_FAILURE(status)) {
164                 /*
165                  * use the count of the number of slots we've found
166                  * for the number of the slot
167                  */
168                 sun = bridge->nr_slots+1;
169         }
170
171         /* search for objects that share the same slot */
172         for (slot = bridge->slots; slot; slot = slot->next)
173                 if (slot->device == device) {
174                         if (slot->sun != sun)
175                                 warn("sibling found, but _SUN doesn't match!\n");
176                         break;
177                 }
178
179         if (!slot) {
180                 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
181                 if (!slot) {
182                         kfree(newfunc);
183                         return AE_NO_MEMORY;
184                 }
185
186                 slot->bridge = bridge;
187                 slot->device = device;
188                 slot->sun = sun;
189                 INIT_LIST_HEAD(&slot->funcs);
190                 mutex_init(&slot->crit_sect);
191
192                 slot->next = bridge->slots;
193                 bridge->slots = slot;
194
195                 bridge->nr_slots++;
196
197                 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
198                     slot->sun, pci_domain_nr(pbus), pbus->number, device);
199                 retval = acpiphp_register_hotplug_slot(slot);
200                 if (retval) {
201                         if (retval == -EBUSY)
202                                 warn("Slot %llu already registered by another "
203                                         "hotplug driver\n", slot->sun);
204                         else
205                                 warn("acpiphp_register_hotplug_slot failed "
206                                         "(err code = 0x%x)\n", retval);
207                         goto err_exit;
208                 }
209         }
210
211         newfunc->slot = slot;
212         list_add_tail(&newfunc->sibling, &slot->funcs);
213
214         pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
215         if (pdev) {
216                 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
217                 pci_dev_put(pdev);
218         }
219
220         if (is_dock_device(handle)) {
221                 /* we don't want to call this device's _EJ0
222                  * because we want the dock notify handler
223                  * to call it after it calls _DCK
224                  */
225                 newfunc->flags &= ~FUNC_HAS_EJ0;
226                 if (register_hotplug_dock_device(handle,
227                         &acpiphp_dock_ops, newfunc))
228                         dbg("failed to register dock device\n");
229
230                 /* we need to be notified when dock events happen
231                  * outside of the hotplug operation, since we may
232                  * need to do fixups before we can hotplug.
233                  */
234                 newfunc->nb.notifier_call = post_dock_fixups;
235                 if (register_dock_notifier(&newfunc->nb))
236                         dbg("failed to register a dock notifier");
237         }
238
239         /* install notify handler */
240         if (!(newfunc->flags & FUNC_HAS_DCK)) {
241                 status = acpi_install_notify_handler(handle,
242                                              ACPI_SYSTEM_NOTIFY,
243                                              handle_hotplug_event_func,
244                                              newfunc);
245
246                 if (ACPI_FAILURE(status))
247                         err("failed to register interrupt notify handler\n");
248         } else
249                 status = AE_OK;
250
251         return status;
252
253  err_exit:
254         bridge->nr_slots--;
255         bridge->slots = slot->next;
256         kfree(slot);
257         kfree(newfunc);
258
259         return AE_OK;
260 }
261
262
263 /* see if it's worth looking at this bridge */
264 static int detect_ejectable_slots(acpi_handle handle)
265 {
266         int found = acpi_pci_detect_ejectable(handle);
267         if (!found) {
268                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
269                                     is_pci_dock_device, (void *)&found, NULL);
270         }
271         return found;
272 }
273
274
275 /* decode ACPI 2.0 _HPP hot plug parameters */
276 static void decode_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
277 {
278         int ret;
279
280         ret = acpi_get_hp_params_from_firmware(dev, hpp);
281         if (ret || !hpp->t0 || (hpp->t0->revision > 1)) {
282                 /* use default numbers */
283                 printk(KERN_WARNING
284                        "%s: Could not get hotplug parameters. Use defaults\n",
285                        __func__);
286                 hpp->t0 = &hpp->type0_data;
287                 hpp->t0->revision = 0;
288                 hpp->t0->cache_line_size = 0x10;
289                 hpp->t0->latency_timer = 0x40;
290                 hpp->t0->enable_serr = 0;
291                 hpp->t0->enable_perr = 0;
292         }
293 }
294
295
296
297 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
298 static void init_bridge_misc(struct acpiphp_bridge *bridge)
299 {
300         acpi_status status;
301
302         /* must be added to the list prior to calling register_slot */
303         list_add(&bridge->list, &bridge_list);
304
305         /* register all slot objects under this bridge */
306         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
307                                      register_slot, bridge, NULL);
308         if (ACPI_FAILURE(status)) {
309                 list_del(&bridge->list);
310                 return;
311         }
312
313         /* install notify handler */
314         if (bridge->type != BRIDGE_TYPE_HOST) {
315                 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
316                         status = acpi_remove_notify_handler(bridge->func->handle,
317                                                 ACPI_SYSTEM_NOTIFY,
318                                                 handle_hotplug_event_func);
319                         if (ACPI_FAILURE(status))
320                                 err("failed to remove notify handler\n");
321                 }
322                 status = acpi_install_notify_handler(bridge->handle,
323                                              ACPI_SYSTEM_NOTIFY,
324                                              handle_hotplug_event_bridge,
325                                              bridge);
326
327                 if (ACPI_FAILURE(status)) {
328                         err("failed to register interrupt notify handler\n");
329                 }
330         }
331 }
332
333
334 /* find acpiphp_func from acpiphp_bridge */
335 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
336 {
337         struct list_head *node, *l;
338         struct acpiphp_bridge *bridge;
339         struct acpiphp_slot *slot;
340         struct acpiphp_func *func;
341
342         list_for_each(node, &bridge_list) {
343                 bridge = list_entry(node, struct acpiphp_bridge, list);
344                 for (slot = bridge->slots; slot; slot = slot->next) {
345                         list_for_each(l, &slot->funcs) {
346                                 func = list_entry(l, struct acpiphp_func,
347                                                         sibling);
348                                 if (func->handle == handle)
349                                         return func;
350                         }
351                 }
352         }
353
354         return NULL;
355 }
356
357
358 static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
359 {
360         acpi_handle dummy_handle;
361
362         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
363                                         "_STA", &dummy_handle)))
364                 bridge->flags |= BRIDGE_HAS_STA;
365
366         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
367                                         "_EJ0", &dummy_handle)))
368                 bridge->flags |= BRIDGE_HAS_EJ0;
369
370         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
371                                         "_PS0", &dummy_handle)))
372                 bridge->flags |= BRIDGE_HAS_PS0;
373
374         if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
375                                         "_PS3", &dummy_handle)))
376                 bridge->flags |= BRIDGE_HAS_PS3;
377
378         /* is this ejectable p2p bridge? */
379         if (bridge->flags & BRIDGE_HAS_EJ0) {
380                 struct acpiphp_func *func;
381
382                 dbg("found ejectable p2p bridge\n");
383
384                 /* make link between PCI bridge and PCI function */
385                 func = acpiphp_bridge_handle_to_function(bridge->handle);
386                 if (!func)
387                         return;
388                 bridge->func = func;
389                 func->bridge = bridge;
390         }
391 }
392
393
394 /* allocate and initialize host bridge data structure */
395 static void add_host_bridge(acpi_handle *handle)
396 {
397         struct acpiphp_bridge *bridge;
398         struct acpi_pci_root *root = acpi_pci_find_root(handle);
399
400         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
401         if (bridge == NULL)
402                 return;
403
404         bridge->type = BRIDGE_TYPE_HOST;
405         bridge->handle = handle;
406
407         bridge->pci_bus = root->bus;
408
409         spin_lock_init(&bridge->res_lock);
410
411         init_bridge_misc(bridge);
412 }
413
414
415 /* allocate and initialize PCI-to-PCI bridge data structure */
416 static void add_p2p_bridge(acpi_handle *handle)
417 {
418         struct acpiphp_bridge *bridge;
419
420         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
421         if (bridge == NULL) {
422                 err("out of memory\n");
423                 return;
424         }
425
426         bridge->type = BRIDGE_TYPE_P2P;
427         bridge->handle = handle;
428         config_p2p_bridge_flags(bridge);
429
430         bridge->pci_dev = acpi_get_pci_dev(handle);
431         bridge->pci_bus = bridge->pci_dev->subordinate;
432         if (!bridge->pci_bus) {
433                 err("This is not a PCI-to-PCI bridge!\n");
434                 goto err;
435         }
436
437         /*
438          * Grab a ref to the subordinate PCI bus in case the bus is
439          * removed via PCI core logical hotplug. The ref pins the bus
440          * (which we access during module unload).
441          */
442         get_device(&bridge->pci_bus->dev);
443         spin_lock_init(&bridge->res_lock);
444
445         init_bridge_misc(bridge);
446         return;
447  err:
448         pci_dev_put(bridge->pci_dev);
449         kfree(bridge);
450         return;
451 }
452
453
454 /* callback routine to find P2P bridges */
455 static acpi_status
456 find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
457 {
458         acpi_status status;
459         struct pci_dev *dev;
460
461         dev = acpi_get_pci_dev(handle);
462         if (!dev || !dev->subordinate)
463                 goto out;
464
465         /* check if this bridge has ejectable slots */
466         if ((detect_ejectable_slots(handle) > 0)) {
467                 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
468                 add_p2p_bridge(handle);
469         }
470
471         /* search P2P bridges under this p2p bridge */
472         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
473                                      find_p2p_bridge, NULL, NULL);
474         if (ACPI_FAILURE(status))
475                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
476
477  out:
478         pci_dev_put(dev);
479         return AE_OK;
480 }
481
482
483 /* find hot-pluggable slots, and then find P2P bridge */
484 static int add_bridge(acpi_handle handle)
485 {
486         acpi_status status;
487         unsigned long long tmp;
488         acpi_handle dummy_handle;
489
490         /* if the bridge doesn't have _STA, we assume it is always there */
491         status = acpi_get_handle(handle, "_STA", &dummy_handle);
492         if (ACPI_SUCCESS(status)) {
493                 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
494                 if (ACPI_FAILURE(status)) {
495                         dbg("%s: _STA evaluation failure\n", __func__);
496                         return 0;
497                 }
498                 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
499                         /* don't register this object */
500                         return 0;
501         }
502
503         /* check if this bridge has ejectable slots */
504         if (detect_ejectable_slots(handle) > 0) {
505                 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
506                 add_host_bridge(handle);
507         }
508
509         /* search P2P bridges under this host bridge */
510         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
511                                      find_p2p_bridge, NULL, NULL);
512
513         if (ACPI_FAILURE(status))
514                 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
515
516         return 0;
517 }
518
519 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
520 {
521         struct list_head *head;
522         list_for_each(head, &bridge_list) {
523                 struct acpiphp_bridge *bridge = list_entry(head,
524                                                 struct acpiphp_bridge, list);
525                 if (bridge->handle == handle)
526                         return bridge;
527         }
528
529         return NULL;
530 }
531
532 static void cleanup_bridge(struct acpiphp_bridge *bridge)
533 {
534         struct list_head *list, *tmp;
535         struct acpiphp_slot *slot;
536         acpi_status status;
537         acpi_handle handle = bridge->handle;
538
539         status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
540                                             handle_hotplug_event_bridge);
541         if (ACPI_FAILURE(status))
542                 err("failed to remove notify handler\n");
543
544         if ((bridge->type != BRIDGE_TYPE_HOST) &&
545             ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
546                 status = acpi_install_notify_handler(bridge->func->handle,
547                                                 ACPI_SYSTEM_NOTIFY,
548                                                 handle_hotplug_event_func,
549                                                 bridge->func);
550                 if (ACPI_FAILURE(status))
551                         err("failed to install interrupt notify handler\n");
552         }
553
554         slot = bridge->slots;
555         while (slot) {
556                 struct acpiphp_slot *next = slot->next;
557                 list_for_each_safe (list, tmp, &slot->funcs) {
558                         struct acpiphp_func *func;
559                         func = list_entry(list, struct acpiphp_func, sibling);
560                         if (is_dock_device(func->handle)) {
561                                 unregister_hotplug_dock_device(func->handle);
562                                 unregister_dock_notifier(&func->nb);
563                         }
564                         if (!(func->flags & FUNC_HAS_DCK)) {
565                                 status = acpi_remove_notify_handler(func->handle,
566                                                 ACPI_SYSTEM_NOTIFY,
567                                                 handle_hotplug_event_func);
568                                 if (ACPI_FAILURE(status))
569                                         err("failed to remove notify handler\n");
570                         }
571                         list_del(list);
572                         kfree(func);
573                 }
574                 acpiphp_unregister_hotplug_slot(slot);
575                 list_del(&slot->funcs);
576                 kfree(slot);
577                 slot = next;
578         }
579
580         /*
581          * Only P2P bridges have a pci_dev
582          */
583         if (bridge->pci_dev)
584                 put_device(&bridge->pci_bus->dev);
585
586         pci_dev_put(bridge->pci_dev);
587         list_del(&bridge->list);
588         kfree(bridge);
589 }
590
591 static acpi_status
592 cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
593 {
594         struct acpiphp_bridge *bridge;
595
596         /* cleanup p2p bridges under this P2P bridge
597            in a depth-first manner */
598         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
599                                 cleanup_p2p_bridge, NULL, NULL);
600
601         bridge = acpiphp_handle_to_bridge(handle);
602         if (bridge)
603                 cleanup_bridge(bridge);
604
605         return AE_OK;
606 }
607
608 static void remove_bridge(acpi_handle handle)
609 {
610         struct acpiphp_bridge *bridge;
611
612         /* cleanup p2p bridges under this host bridge
613            in a depth-first manner */
614         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
615                                 (u32)1, cleanup_p2p_bridge, NULL, NULL);
616
617         /*
618          * On root bridges with hotplug slots directly underneath (ie,
619          * no p2p bridge inbetween), we call cleanup_bridge(). 
620          *
621          * The else clause cleans up root bridges that either had no
622          * hotplug slots at all, or had a p2p bridge underneath.
623          */
624         bridge = acpiphp_handle_to_bridge(handle);
625         if (bridge)
626                 cleanup_bridge(bridge);
627         else
628                 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
629                                            handle_hotplug_event_bridge);
630 }
631
632 static struct pci_dev * get_apic_pci_info(acpi_handle handle)
633 {
634         struct pci_dev *dev;
635
636         dev = acpi_get_pci_dev(handle);
637         if (!dev)
638                 return NULL;
639
640         if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
641             (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
642         {
643                 pci_dev_put(dev);
644                 return NULL;
645         }
646
647         return dev;
648 }
649
650 static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
651 {
652         acpi_status status;
653         int result = -1;
654         unsigned long long gsb;
655         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
656         union acpi_object *obj;
657         void *table;
658
659         status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
660         if (ACPI_SUCCESS(status)) {
661                 *gsi_base = (u32)gsb;
662                 return 0;
663         }
664
665         status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
666         if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
667                 return -1;
668
669         obj = buffer.pointer;
670         if (obj->type != ACPI_TYPE_BUFFER)
671                 goto out;
672
673         table = obj->buffer.pointer;
674         switch (((struct acpi_subtable_header *)table)->type) {
675         case ACPI_MADT_TYPE_IO_SAPIC:
676                 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
677                 result = 0;
678                 break;
679         case ACPI_MADT_TYPE_IO_APIC:
680                 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
681                 result = 0;
682                 break;
683         default:
684                 break;
685         }
686  out:
687         kfree(buffer.pointer);
688         return result;
689 }
690
691 static acpi_status
692 ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
693 {
694         acpi_status status;
695         unsigned long long sta;
696         acpi_handle tmp;
697         struct pci_dev *pdev;
698         u32 gsi_base;
699         u64 phys_addr;
700         struct acpiphp_ioapic *ioapic;
701
702         /* Evaluate _STA if present */
703         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
704         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
705                 return AE_CTRL_DEPTH;
706
707         /* Scan only PCI bus scope */
708         status = acpi_get_handle(handle, "_HID", &tmp);
709         if (ACPI_SUCCESS(status))
710                 return AE_CTRL_DEPTH;
711
712         if (get_gsi_base(handle, &gsi_base))
713                 return AE_OK;
714
715         ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
716         if (!ioapic)
717                 return AE_NO_MEMORY;
718
719         pdev = get_apic_pci_info(handle);
720         if (!pdev)
721                 goto exit_kfree;
722
723         if (pci_enable_device(pdev))
724                 goto exit_pci_dev_put;
725
726         pci_set_master(pdev);
727
728         if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
729                 goto exit_pci_disable_device;
730
731         phys_addr = pci_resource_start(pdev, 0);
732         if (acpi_register_ioapic(handle, phys_addr, gsi_base))
733                 goto exit_pci_release_region;
734
735         ioapic->gsi_base = gsi_base;
736         ioapic->dev = pdev;
737         spin_lock(&ioapic_list_lock);
738         list_add_tail(&ioapic->list, &ioapic_list);
739         spin_unlock(&ioapic_list_lock);
740
741         return AE_OK;
742
743  exit_pci_release_region:
744         pci_release_region(pdev, 0);
745  exit_pci_disable_device:
746         pci_disable_device(pdev);
747  exit_pci_dev_put:
748         pci_dev_put(pdev);
749  exit_kfree:
750         kfree(ioapic);
751
752         return AE_OK;
753 }
754
755 static acpi_status
756 ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
757 {
758         acpi_status status;
759         unsigned long long sta;
760         acpi_handle tmp;
761         u32 gsi_base;
762         struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
763
764         /* Evaluate _STA if present */
765         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
766         if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
767                 return AE_CTRL_DEPTH;
768
769         /* Scan only PCI bus scope */
770         status = acpi_get_handle(handle, "_HID", &tmp);
771         if (ACPI_SUCCESS(status))
772                 return AE_CTRL_DEPTH;
773
774         if (get_gsi_base(handle, &gsi_base))
775                 return AE_OK;
776
777         acpi_unregister_ioapic(handle, gsi_base);
778
779         spin_lock(&ioapic_list_lock);
780         list_for_each_entry_safe(pos, n, &ioapic_list, list) {
781                 if (pos->gsi_base != gsi_base)
782                         continue;
783                 ioapic = pos;
784                 list_del(&ioapic->list);
785                 break;
786         }
787         spin_unlock(&ioapic_list_lock);
788
789         if (!ioapic)
790                 return AE_OK;
791
792         pci_release_region(ioapic->dev, 0);
793         pci_disable_device(ioapic->dev);
794         pci_dev_put(ioapic->dev);
795         kfree(ioapic);
796
797         return AE_OK;
798 }
799
800 static int acpiphp_configure_ioapics(acpi_handle handle)
801 {
802         ioapic_add(handle, 0, NULL, NULL);
803         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
804                             ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
805         return 0;
806 }
807
808 static int acpiphp_unconfigure_ioapics(acpi_handle handle)
809 {
810         ioapic_remove(handle, 0, NULL, NULL);
811         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
812                             ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
813         return 0;
814 }
815
816 static int power_on_slot(struct acpiphp_slot *slot)
817 {
818         acpi_status status;
819         struct acpiphp_func *func;
820         struct list_head *l;
821         int retval = 0;
822
823         /* if already enabled, just skip */
824         if (slot->flags & SLOT_POWEREDON)
825                 goto err_exit;
826
827         list_for_each (l, &slot->funcs) {
828                 func = list_entry(l, struct acpiphp_func, sibling);
829
830                 if (func->flags & FUNC_HAS_PS0) {
831                         dbg("%s: executing _PS0\n", __func__);
832                         status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
833                         if (ACPI_FAILURE(status)) {
834                                 warn("%s: _PS0 failed\n", __func__);
835                                 retval = -1;
836                                 goto err_exit;
837                         } else
838                                 break;
839                 }
840         }
841
842         /* TBD: evaluate _STA to check if the slot is enabled */
843
844         slot->flags |= SLOT_POWEREDON;
845
846  err_exit:
847         return retval;
848 }
849
850
851 static int power_off_slot(struct acpiphp_slot *slot)
852 {
853         acpi_status status;
854         struct acpiphp_func *func;
855         struct list_head *l;
856
857         int retval = 0;
858
859         /* if already disabled, just skip */
860         if ((slot->flags & SLOT_POWEREDON) == 0)
861                 goto err_exit;
862
863         list_for_each (l, &slot->funcs) {
864                 func = list_entry(l, struct acpiphp_func, sibling);
865
866                 if (func->flags & FUNC_HAS_PS3) {
867                         status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
868                         if (ACPI_FAILURE(status)) {
869                                 warn("%s: _PS3 failed\n", __func__);
870                                 retval = -1;
871                                 goto err_exit;
872                         } else
873                                 break;
874                 }
875         }
876
877         /* TBD: evaluate _STA to check if the slot is disabled */
878
879         slot->flags &= (~SLOT_POWEREDON);
880
881  err_exit:
882         return retval;
883 }
884
885
886
887 /**
888  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
889  * @bus: bus to start search with
890  */
891 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
892 {
893         struct list_head *tmp;
894         unsigned char max, n;
895
896         /*
897          * pci_bus_max_busnr will return the highest
898          * reserved busnr for all these children.
899          * that is equivalent to the bus->subordinate
900          * value.  We don't want to use the parent's
901          * bus->subordinate value because it could have
902          * padding in it.
903          */
904         max = bus->secondary;
905
906         list_for_each(tmp, &bus->children) {
907                 n = pci_bus_max_busnr(pci_bus_b(tmp));
908                 if (n > max)
909                         max = n;
910         }
911         return max;
912 }
913
914
915 /**
916  * acpiphp_bus_add - add a new bus to acpi subsystem
917  * @func: acpiphp_func of the bridge
918  */
919 static int acpiphp_bus_add(struct acpiphp_func *func)
920 {
921         acpi_handle phandle;
922         struct acpi_device *device, *pdevice;
923         int ret_val;
924
925         acpi_get_parent(func->handle, &phandle);
926         if (acpi_bus_get_device(phandle, &pdevice)) {
927                 dbg("no parent device, assuming NULL\n");
928                 pdevice = NULL;
929         }
930         if (!acpi_bus_get_device(func->handle, &device)) {
931                 dbg("bus exists... trim\n");
932                 /* this shouldn't be in here, so remove
933                  * the bus then re-add it...
934                  */
935                 ret_val = acpi_bus_trim(device, 1);
936                 dbg("acpi_bus_trim return %x\n", ret_val);
937         }
938
939         ret_val = acpi_bus_add(&device, pdevice, func->handle,
940                 ACPI_BUS_TYPE_DEVICE);
941         if (ret_val) {
942                 dbg("error adding bus, %x\n",
943                         -ret_val);
944                 goto acpiphp_bus_add_out;
945         }
946         /*
947          * try to start anyway.  We could have failed to add
948          * simply because this bus had previously been added
949          * on another add.  Don't bother with the return value
950          * we just keep going.
951          */
952         ret_val = acpi_bus_start(device);
953
954 acpiphp_bus_add_out:
955         return ret_val;
956 }
957
958
959 /**
960  * acpiphp_bus_trim - trim a bus from acpi subsystem
961  * @handle: handle to acpi namespace
962  */
963 static int acpiphp_bus_trim(acpi_handle handle)
964 {
965         struct acpi_device *device;
966         int retval;
967
968         retval = acpi_bus_get_device(handle, &device);
969         if (retval) {
970                 dbg("acpi_device not found\n");
971                 return retval;
972         }
973
974         retval = acpi_bus_trim(device, 1);
975         if (retval)
976                 err("cannot remove from acpi list\n");
977
978         return retval;
979 }
980
981 /**
982  * enable_device - enable, configure a slot
983  * @slot: slot to be enabled
984  *
985  * This function should be called per *physical slot*,
986  * not per each slot object in ACPI namespace.
987  */
988 static int __ref enable_device(struct acpiphp_slot *slot)
989 {
990         struct pci_dev *dev;
991         struct pci_bus *bus = slot->bridge->pci_bus;
992         struct list_head *l;
993         struct acpiphp_func *func;
994         int retval = 0;
995         int num, max, pass;
996         acpi_status status;
997
998         if (slot->flags & SLOT_ENABLED)
999                 goto err_exit;
1000
1001         /* sanity check: dev should be NULL when hot-plugged in */
1002         dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1003         if (dev) {
1004                 /* This case shouldn't happen */
1005                 err("pci_dev structure already exists.\n");
1006                 pci_dev_put(dev);
1007                 retval = -1;
1008                 goto err_exit;
1009         }
1010
1011         num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1012         if (num == 0) {
1013                 err("No new device found\n");
1014                 retval = -1;
1015                 goto err_exit;
1016         }
1017
1018         max = acpiphp_max_busnr(bus);
1019         for (pass = 0; pass < 2; pass++) {
1020                 list_for_each_entry(dev, &bus->devices, bus_list) {
1021                         if (PCI_SLOT(dev->devfn) != slot->device)
1022                                 continue;
1023                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1024                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
1025                                 max = pci_scan_bridge(bus, dev, max, pass);
1026                                 if (pass && dev->subordinate)
1027                                         pci_bus_size_bridges(dev->subordinate);
1028                         }
1029                 }
1030         }
1031
1032         list_for_each (l, &slot->funcs) {
1033                 func = list_entry(l, struct acpiphp_func, sibling);
1034                 acpiphp_bus_add(func);
1035         }
1036
1037         pci_bus_assign_resources(bus);
1038         acpiphp_sanitize_bus(bus);
1039         acpiphp_set_hpp_values(bus);
1040         list_for_each_entry(func, &slot->funcs, sibling)
1041                 acpiphp_configure_ioapics(func->handle);
1042         pci_enable_bridges(bus);
1043         pci_bus_add_devices(bus);
1044
1045         list_for_each (l, &slot->funcs) {
1046                 func = list_entry(l, struct acpiphp_func, sibling);
1047                 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1048                                                   func->function));
1049                 if (!dev)
1050                         continue;
1051
1052                 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1053                     dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1054                         pci_dev_put(dev);
1055                         continue;
1056                 }
1057
1058                 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1059                 if (ACPI_FAILURE(status))
1060                         warn("find_p2p_bridge failed (error code = 0x%x)\n",
1061                                 status);
1062                 pci_dev_put(dev);
1063         }
1064
1065         slot->flags |= SLOT_ENABLED;
1066
1067  err_exit:
1068         return retval;
1069 }
1070
1071 static void disable_bridges(struct pci_bus *bus)
1072 {
1073         struct pci_dev *dev;
1074         list_for_each_entry(dev, &bus->devices, bus_list) {
1075                 if (dev->subordinate) {
1076                         disable_bridges(dev->subordinate);
1077                         pci_disable_device(dev);
1078                 }
1079         }
1080 }
1081
1082 /**
1083  * disable_device - disable a slot
1084  * @slot: ACPI PHP slot
1085  */
1086 static int disable_device(struct acpiphp_slot *slot)
1087 {
1088         struct acpiphp_func *func;
1089         struct pci_dev *pdev;
1090
1091         /* is this slot already disabled? */
1092         if (!(slot->flags & SLOT_ENABLED))
1093                 goto err_exit;
1094
1095         list_for_each_entry(func, &slot->funcs, sibling) {
1096                 if (func->bridge) {
1097                         /* cleanup p2p bridges under this P2P bridge */
1098                         cleanup_p2p_bridge(func->bridge->handle,
1099                                                 (u32)1, NULL, NULL);
1100                         func->bridge = NULL;
1101                 }
1102
1103                 pdev = pci_get_slot(slot->bridge->pci_bus,
1104                                     PCI_DEVFN(slot->device, func->function));
1105                 if (pdev) {
1106                         pci_stop_bus_device(pdev);
1107                         if (pdev->subordinate) {
1108                                 disable_bridges(pdev->subordinate);
1109                                 pci_disable_device(pdev);
1110                         }
1111                         pci_remove_bus_device(pdev);
1112                         pci_dev_put(pdev);
1113                 }
1114         }
1115
1116         list_for_each_entry(func, &slot->funcs, sibling) {
1117                 acpiphp_unconfigure_ioapics(func->handle);
1118                 acpiphp_bus_trim(func->handle);
1119         }
1120
1121         slot->flags &= (~SLOT_ENABLED);
1122
1123 err_exit:
1124         return 0;
1125 }
1126
1127
1128 /**
1129  * get_slot_status - get ACPI slot status
1130  * @slot: ACPI PHP slot
1131  *
1132  * If a slot has _STA for each function and if any one of them
1133  * returned non-zero status, return it.
1134  *
1135  * If a slot doesn't have _STA and if any one of its functions'
1136  * configuration space is configured, return 0x0f as a _STA.
1137  *
1138  * Otherwise return 0.
1139  */
1140 static unsigned int get_slot_status(struct acpiphp_slot *slot)
1141 {
1142         acpi_status status;
1143         unsigned long long sta = 0;
1144         u32 dvid;
1145         struct list_head *l;
1146         struct acpiphp_func *func;
1147
1148         list_for_each (l, &slot->funcs) {
1149                 func = list_entry(l, struct acpiphp_func, sibling);
1150
1151                 if (func->flags & FUNC_HAS_STA) {
1152                         status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1153                         if (ACPI_SUCCESS(status) && sta)
1154                                 break;
1155                 } else {
1156                         pci_bus_read_config_dword(slot->bridge->pci_bus,
1157                                                   PCI_DEVFN(slot->device,
1158                                                             func->function),
1159                                                   PCI_VENDOR_ID, &dvid);
1160                         if (dvid != 0xffffffff) {
1161                                 sta = ACPI_STA_ALL;
1162                                 break;
1163                         }
1164                 }
1165         }
1166
1167         return (unsigned int)sta;
1168 }
1169
1170 /**
1171  * acpiphp_eject_slot - physically eject the slot
1172  * @slot: ACPI PHP slot
1173  */
1174 int acpiphp_eject_slot(struct acpiphp_slot *slot)
1175 {
1176         acpi_status status;
1177         struct acpiphp_func *func;
1178         struct list_head *l;
1179         struct acpi_object_list arg_list;
1180         union acpi_object arg;
1181
1182         list_for_each (l, &slot->funcs) {
1183                 func = list_entry(l, struct acpiphp_func, sibling);
1184
1185                 /* We don't want to call _EJ0 on non-existing functions. */
1186                 if ((func->flags & FUNC_HAS_EJ0)) {
1187                         /* _EJ0 method take one argument */
1188                         arg_list.count = 1;
1189                         arg_list.pointer = &arg;
1190                         arg.type = ACPI_TYPE_INTEGER;
1191                         arg.integer.value = 1;
1192
1193                         status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1194                         if (ACPI_FAILURE(status)) {
1195                                 warn("%s: _EJ0 failed\n", __func__);
1196                                 return -1;
1197                         } else
1198                                 break;
1199                 }
1200         }
1201         return 0;
1202 }
1203
1204 /**
1205  * acpiphp_check_bridge - re-enumerate devices
1206  * @bridge: where to begin re-enumeration
1207  *
1208  * Iterate over all slots under this bridge and make sure that if a
1209  * card is present they are enabled, and if not they are disabled.
1210  */
1211 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1212 {
1213         struct acpiphp_slot *slot;
1214         int retval = 0;
1215         int enabled, disabled;
1216
1217         enabled = disabled = 0;
1218
1219         for (slot = bridge->slots; slot; slot = slot->next) {
1220                 unsigned int status = get_slot_status(slot);
1221                 if (slot->flags & SLOT_ENABLED) {
1222                         if (status == ACPI_STA_ALL)
1223                                 continue;
1224                         retval = acpiphp_disable_slot(slot);
1225                         if (retval) {
1226                                 err("Error occurred in disabling\n");
1227                                 goto err_exit;
1228                         } else {
1229                                 acpiphp_eject_slot(slot);
1230                         }
1231                         disabled++;
1232                 } else {
1233                         if (status != ACPI_STA_ALL)
1234                                 continue;
1235                         retval = acpiphp_enable_slot(slot);
1236                         if (retval) {
1237                                 err("Error occurred in enabling\n");
1238                                 goto err_exit;
1239                         }
1240                         enabled++;
1241                 }
1242         }
1243
1244         dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1245
1246  err_exit:
1247         return retval;
1248 }
1249
1250 static void program_hpp(struct pci_dev *dev, struct hotplug_params *hpp)
1251 {
1252         u16 pci_cmd, pci_bctl;
1253         struct pci_dev *cdev;
1254
1255         /* Program hpp values for this device */
1256         if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1257                         (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1258                         (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1259                 return;
1260
1261         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1262                 return;
1263
1264         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
1265                         hpp->t0->cache_line_size);
1266         pci_write_config_byte(dev, PCI_LATENCY_TIMER,
1267                         hpp->t0->latency_timer);
1268         pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
1269         if (hpp->t0->enable_serr)
1270                 pci_cmd |= PCI_COMMAND_SERR;
1271         else
1272                 pci_cmd &= ~PCI_COMMAND_SERR;
1273         if (hpp->t0->enable_perr)
1274                 pci_cmd |= PCI_COMMAND_PARITY;
1275         else
1276                 pci_cmd &= ~PCI_COMMAND_PARITY;
1277         pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1278
1279         /* Program bridge control value and child devices */
1280         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1281                 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
1282                                 hpp->t0->latency_timer);
1283                 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
1284                 if (hpp->t0->enable_serr)
1285                         pci_bctl |= PCI_BRIDGE_CTL_SERR;
1286                 else
1287                         pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
1288                 if (hpp->t0->enable_perr)
1289                         pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1290                 else
1291                         pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1292                 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1293                 if (dev->subordinate) {
1294                         list_for_each_entry(cdev, &dev->subordinate->devices,
1295                                         bus_list)
1296                                 program_hpp(cdev, hpp);
1297                 }
1298         }
1299 }
1300
1301 static void acpiphp_set_hpp_values(struct pci_bus *bus)
1302 {
1303         struct pci_dev *dev;
1304         struct hotplug_params hpp;
1305
1306         list_for_each_entry(dev, &bus->devices, bus_list) {
1307                 decode_hpp(dev, &hpp);
1308                 program_hpp(dev, &hpp);
1309         }
1310 }
1311
1312 /*
1313  * Remove devices for which we could not assign resources, call
1314  * arch specific code to fix-up the bus
1315  */
1316 static void acpiphp_sanitize_bus(struct pci_bus *bus)
1317 {
1318         struct pci_dev *dev;
1319         int i;
1320         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1321
1322         list_for_each_entry(dev, &bus->devices, bus_list) {
1323                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1324                         struct resource *res = &dev->resource[i];
1325                         if ((res->flags & type_mask) && !res->start &&
1326                                         res->end) {
1327                                 /* Could not assign a required resources
1328                                  * for this device, remove it */
1329                                 pci_remove_bus_device(dev);
1330                                 break;
1331                         }
1332                 }
1333         }
1334 }
1335
1336 /* Program resources in newly inserted bridge */
1337 static int acpiphp_configure_bridge (acpi_handle handle)
1338 {
1339         struct pci_bus *bus;
1340
1341         if (acpi_is_root_bridge(handle)) {
1342                 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1343                 bus = root->bus;
1344         } else {
1345                 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1346                 bus = pdev->subordinate;
1347                 pci_dev_put(pdev);
1348         }
1349
1350         pci_bus_size_bridges(bus);
1351         pci_bus_assign_resources(bus);
1352         acpiphp_sanitize_bus(bus);
1353         acpiphp_set_hpp_values(bus);
1354         pci_enable_bridges(bus);
1355         acpiphp_configure_ioapics(handle);
1356         return 0;
1357 }
1358
1359 static void handle_bridge_insertion(acpi_handle handle, u32 type)
1360 {
1361         struct acpi_device *device, *pdevice;
1362         acpi_handle phandle;
1363
1364         if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1365                         (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1366                 err("unexpected notification type %d\n", type);
1367                 return;
1368         }
1369
1370         acpi_get_parent(handle, &phandle);
1371         if (acpi_bus_get_device(phandle, &pdevice)) {
1372                 dbg("no parent device, assuming NULL\n");
1373                 pdevice = NULL;
1374         }
1375         if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1376                 err("cannot add bridge to acpi list\n");
1377                 return;
1378         }
1379         if (!acpiphp_configure_bridge(handle) &&
1380                 !acpi_bus_start(device))
1381                 add_bridge(handle);
1382         else
1383                 err("cannot configure and start bridge\n");
1384
1385 }
1386
1387 /*
1388  * ACPI event handlers
1389  */
1390
1391 static acpi_status
1392 count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1393 {
1394         int *count = (int *)context;
1395         struct acpiphp_bridge *bridge;
1396
1397         bridge = acpiphp_handle_to_bridge(handle);
1398         if (bridge)
1399                 (*count)++;
1400         return AE_OK ;
1401 }
1402
1403 static acpi_status
1404 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1405 {
1406         struct acpiphp_bridge *bridge;
1407         char objname[64];
1408         struct acpi_buffer buffer = { .length = sizeof(objname),
1409                                       .pointer = objname };
1410
1411         bridge = acpiphp_handle_to_bridge(handle);
1412         if (bridge) {
1413                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1414                 dbg("%s: re-enumerating slots under %s\n",
1415                         __func__, objname);
1416                 acpiphp_check_bridge(bridge);
1417         }
1418         return AE_OK ;
1419 }
1420
1421 /**
1422  * handle_hotplug_event_bridge - handle ACPI event on bridges
1423  * @handle: Notify()'ed acpi_handle
1424  * @type: Notify code
1425  * @context: pointer to acpiphp_bridge structure
1426  *
1427  * Handles ACPI event notification on {host,p2p} bridges.
1428  */
1429 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1430 {
1431         struct acpiphp_bridge *bridge;
1432         char objname[64];
1433         struct acpi_buffer buffer = { .length = sizeof(objname),
1434                                       .pointer = objname };
1435         struct acpi_device *device;
1436         int num_sub_bridges = 0;
1437
1438         if (acpi_bus_get_device(handle, &device)) {
1439                 /* This bridge must have just been physically inserted */
1440                 handle_bridge_insertion(handle, type);
1441                 return;
1442         }
1443
1444         bridge = acpiphp_handle_to_bridge(handle);
1445         if (type == ACPI_NOTIFY_BUS_CHECK) {
1446                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1447                         count_sub_bridges, &num_sub_bridges, NULL);
1448         }
1449
1450         if (!bridge && !num_sub_bridges) {
1451                 err("cannot get bridge info\n");
1452                 return;
1453         }
1454
1455         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1456
1457         switch (type) {
1458         case ACPI_NOTIFY_BUS_CHECK:
1459                 /* bus re-enumerate */
1460                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1461                 if (bridge) {
1462                         dbg("%s: re-enumerating slots under %s\n",
1463                                 __func__, objname);
1464                         acpiphp_check_bridge(bridge);
1465                 }
1466                 if (num_sub_bridges)
1467                         acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1468                                 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1469                 break;
1470
1471         case ACPI_NOTIFY_DEVICE_CHECK:
1472                 /* device check */
1473                 dbg("%s: Device check notify on %s\n", __func__, objname);
1474                 acpiphp_check_bridge(bridge);
1475                 break;
1476
1477         case ACPI_NOTIFY_DEVICE_WAKE:
1478                 /* wake event */
1479                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1480                 break;
1481
1482         case ACPI_NOTIFY_EJECT_REQUEST:
1483                 /* request device eject */
1484                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1485                 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1486                     (bridge->flags & BRIDGE_HAS_EJ0)) {
1487                         struct acpiphp_slot *slot;
1488                         slot = bridge->func->slot;
1489                         if (!acpiphp_disable_slot(slot))
1490                                 acpiphp_eject_slot(slot);
1491                 }
1492                 break;
1493
1494         case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1495                 printk(KERN_ERR "Device %s cannot be configured due"
1496                                 " to a frequency mismatch\n", objname);
1497                 break;
1498
1499         case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1500                 printk(KERN_ERR "Device %s cannot be configured due"
1501                                 " to a bus mode mismatch\n", objname);
1502                 break;
1503
1504         case ACPI_NOTIFY_POWER_FAULT:
1505                 printk(KERN_ERR "Device %s has suffered a power fault\n",
1506                                 objname);
1507                 break;
1508
1509         default:
1510                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1511                 break;
1512         }
1513 }
1514
1515 /**
1516  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1517  * @handle: Notify()'ed acpi_handle
1518  * @type: Notify code
1519  * @context: pointer to acpiphp_func structure
1520  *
1521  * Handles ACPI event notification on slots.
1522  */
1523 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1524 {
1525         struct acpiphp_func *func;
1526         char objname[64];
1527         struct acpi_buffer buffer = { .length = sizeof(objname),
1528                                       .pointer = objname };
1529
1530         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1531
1532         func = (struct acpiphp_func *)context;
1533
1534         switch (type) {
1535         case ACPI_NOTIFY_BUS_CHECK:
1536                 /* bus re-enumerate */
1537                 dbg("%s: Bus check notify on %s\n", __func__, objname);
1538                 acpiphp_enable_slot(func->slot);
1539                 break;
1540
1541         case ACPI_NOTIFY_DEVICE_CHECK:
1542                 /* device check : re-enumerate from parent bus */
1543                 dbg("%s: Device check notify on %s\n", __func__, objname);
1544                 acpiphp_check_bridge(func->slot->bridge);
1545                 break;
1546
1547         case ACPI_NOTIFY_DEVICE_WAKE:
1548                 /* wake event */
1549                 dbg("%s: Device wake notify on %s\n", __func__, objname);
1550                 break;
1551
1552         case ACPI_NOTIFY_EJECT_REQUEST:
1553                 /* request device eject */
1554                 dbg("%s: Device eject notify on %s\n", __func__, objname);
1555                 if (!(acpiphp_disable_slot(func->slot)))
1556                         acpiphp_eject_slot(func->slot);
1557                 break;
1558
1559         default:
1560                 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1561                 break;
1562         }
1563 }
1564
1565
1566 static acpi_status
1567 find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1568 {
1569         int *count = (int *)context;
1570
1571         if (acpi_is_root_bridge(handle)) {
1572                 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1573                                 handle_hotplug_event_bridge, NULL);
1574                         (*count)++;
1575         }
1576         return AE_OK ;
1577 }
1578
1579 static struct acpi_pci_driver acpi_pci_hp_driver = {
1580         .add =          add_bridge,
1581         .remove =       remove_bridge,
1582 };
1583
1584 /**
1585  * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1586  */
1587 int __init acpiphp_glue_init(void)
1588 {
1589         int num = 0;
1590
1591         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1592                         ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1593
1594         if (num <= 0)
1595                 return -1;
1596         else
1597                 acpi_pci_register_driver(&acpi_pci_hp_driver);
1598
1599         return 0;
1600 }
1601
1602
1603 /**
1604  * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1605  *
1606  * This function frees all data allocated in acpiphp_glue_init().
1607  */
1608 void  acpiphp_glue_exit(void)
1609 {
1610         acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1611 }
1612
1613
1614 /**
1615  * acpiphp_get_num_slots - count number of slots in a system
1616  */
1617 int __init acpiphp_get_num_slots(void)
1618 {
1619         struct acpiphp_bridge *bridge;
1620         int num_slots = 0;
1621
1622         list_for_each_entry (bridge, &bridge_list, list) {
1623                 dbg("Bus %04x:%02x has %d slot%s\n",
1624                                 pci_domain_nr(bridge->pci_bus),
1625                                 bridge->pci_bus->number, bridge->nr_slots,
1626                                 bridge->nr_slots == 1 ? "" : "s");
1627                 num_slots += bridge->nr_slots;
1628         }
1629
1630         dbg("Total %d slots\n", num_slots);
1631         return num_slots;
1632 }
1633
1634
1635 #if 0
1636 /**
1637  * acpiphp_for_each_slot - call function for each slot
1638  * @fn: callback function
1639  * @data: context to be passed to callback function
1640  */
1641 static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1642 {
1643         struct list_head *node;
1644         struct acpiphp_bridge *bridge;
1645         struct acpiphp_slot *slot;
1646         int retval = 0;
1647
1648         list_for_each (node, &bridge_list) {
1649                 bridge = (struct acpiphp_bridge *)node;
1650                 for (slot = bridge->slots; slot; slot = slot->next) {
1651                         retval = fn(slot, data);
1652                         if (!retval)
1653                                 goto err_exit;
1654                 }
1655         }
1656
1657  err_exit:
1658         return retval;
1659 }
1660 #endif
1661
1662
1663 /**
1664  * acpiphp_enable_slot - power on slot
1665  * @slot: ACPI PHP slot
1666  */
1667 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1668 {
1669         int retval;
1670
1671         mutex_lock(&slot->crit_sect);
1672
1673         /* wake up all functions */
1674         retval = power_on_slot(slot);
1675         if (retval)
1676                 goto err_exit;
1677
1678         if (get_slot_status(slot) == ACPI_STA_ALL) {
1679                 /* configure all functions */
1680                 retval = enable_device(slot);
1681                 if (retval)
1682                         power_off_slot(slot);
1683         } else {
1684                 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1685                 power_off_slot(slot);
1686         }
1687
1688  err_exit:
1689         mutex_unlock(&slot->crit_sect);
1690         return retval;
1691 }
1692
1693 /**
1694  * acpiphp_disable_slot - power off slot
1695  * @slot: ACPI PHP slot
1696  */
1697 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1698 {
1699         int retval = 0;
1700
1701         mutex_lock(&slot->crit_sect);
1702
1703         /* unconfigure all functions */
1704         retval = disable_device(slot);
1705         if (retval)
1706                 goto err_exit;
1707
1708         /* power off all functions */
1709         retval = power_off_slot(slot);
1710         if (retval)
1711                 goto err_exit;
1712
1713  err_exit:
1714         mutex_unlock(&slot->crit_sect);
1715         return retval;
1716 }
1717
1718
1719 /*
1720  * slot enabled:  1
1721  * slot disabled: 0
1722  */
1723 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1724 {
1725         return (slot->flags & SLOT_POWEREDON);
1726 }
1727
1728
1729 /*
1730  * latch   open:  1
1731  * latch closed:  0
1732  */
1733 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1734 {
1735         unsigned int sta;
1736
1737         sta = get_slot_status(slot);
1738
1739         return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1740 }
1741
1742
1743 /*
1744  * adapter presence : 1
1745  *          absence : 0
1746  */
1747 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1748 {
1749         unsigned int sta;
1750
1751         sta = get_slot_status(slot);
1752
1753         return (sta == 0) ? 0 : 1;
1754 }