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