ACPI / hotplug / PCI: Execute _EJ0 under the ACPI scan lock
[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 #define pr_fmt(fmt) "acpiphp_glue: " fmt
43
44 #include <linux/init.h>
45 #include <linux/module.h>
46
47 #include <linux/kernel.h>
48 #include <linux/pci.h>
49 #include <linux/pci_hotplug.h>
50 #include <linux/pci-acpi.h>
51 #include <linux/pm_runtime.h>
52 #include <linux/mutex.h>
53 #include <linux/slab.h>
54 #include <linux/acpi.h>
55
56 #include "../pci.h"
57 #include "acpiphp.h"
58
59 static LIST_HEAD(bridge_list);
60 static DEFINE_MUTEX(bridge_mutex);
61
62 static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type);
63 static void acpiphp_sanitize_bus(struct pci_bus *bus);
64 static void acpiphp_set_hpp_values(struct pci_bus *bus);
65 static void hotplug_event(u32 type, struct acpiphp_context *context);
66 static void free_bridge(struct kref *kref);
67
68 /**
69  * acpiphp_init_context - Create hotplug context and grab a reference to it.
70  * @adev: ACPI device object to create the context for.
71  *
72  * Call under acpi_hp_context_lock.
73  */
74 static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
75 {
76         struct acpiphp_context *context;
77
78         context = kzalloc(sizeof(*context), GFP_KERNEL);
79         if (!context)
80                 return NULL;
81
82         context->refcount = 1;
83         acpi_set_hp_context(adev, &context->hp, acpiphp_hotplug_event);
84         return context;
85 }
86
87 /**
88  * acpiphp_get_context - Get hotplug context and grab a reference to it.
89  * @adev: ACPI device object to get the context for.
90  *
91  * Call under acpi_hp_context_lock.
92  */
93 static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
94 {
95         struct acpiphp_context *context;
96
97         if (!adev->hp)
98                 return NULL;
99
100         context = to_acpiphp_context(adev->hp);
101         context->refcount++;
102         return context;
103 }
104
105 /**
106  * acpiphp_put_context - Drop a reference to ACPI hotplug context.
107  * @context: ACPI hotplug context to drop a reference to.
108  *
109  * The context object is removed if there are no more references to it.
110  *
111  * Call under acpi_hp_context_lock.
112  */
113 static void acpiphp_put_context(struct acpiphp_context *context)
114 {
115         if (--context->refcount)
116                 return;
117
118         WARN_ON(context->bridge);
119         context->hp.self->hp = NULL;
120         kfree(context);
121 }
122
123 static inline void get_bridge(struct acpiphp_bridge *bridge)
124 {
125         kref_get(&bridge->ref);
126 }
127
128 static inline void put_bridge(struct acpiphp_bridge *bridge)
129 {
130         kref_put(&bridge->ref, free_bridge);
131 }
132
133 static void free_bridge(struct kref *kref)
134 {
135         struct acpiphp_context *context;
136         struct acpiphp_bridge *bridge;
137         struct acpiphp_slot *slot, *next;
138         struct acpiphp_func *func, *tmp;
139
140         acpi_lock_hp_context();
141
142         bridge = container_of(kref, struct acpiphp_bridge, ref);
143
144         list_for_each_entry_safe(slot, next, &bridge->slots, node) {
145                 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
146                         acpiphp_put_context(func_to_context(func));
147
148                 kfree(slot);
149         }
150
151         context = bridge->context;
152         /* Root bridges will not have hotplug context. */
153         if (context) {
154                 /* Release the reference taken by acpiphp_enumerate_slots(). */
155                 put_bridge(context->func.parent);
156                 context->bridge = NULL;
157                 acpiphp_put_context(context);
158         }
159
160         put_device(&bridge->pci_bus->dev);
161         pci_dev_put(bridge->pci_dev);
162         kfree(bridge);
163
164         acpi_unlock_hp_context();
165 }
166
167 /*
168  * the _DCK method can do funny things... and sometimes not
169  * hah-hah funny.
170  *
171  * TBD - figure out a way to only call fixups for
172  * systems that require them.
173  */
174 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
175 {
176         struct acpiphp_context *context = data;
177         struct pci_bus *bus = context->func.slot->bus;
178         u32 buses;
179
180         if (!bus->self)
181                 return;
182
183         /* fixup bad _DCK function that rewrites
184          * secondary bridge on slot
185          */
186         pci_read_config_dword(bus->self,
187                         PCI_PRIMARY_BUS,
188                         &buses);
189
190         if (((buses >> 8) & 0xff) != bus->busn_res.start) {
191                 buses = (buses & 0xff000000)
192                         | ((unsigned int)(bus->primary)     <<  0)
193                         | ((unsigned int)(bus->busn_res.start)   <<  8)
194                         | ((unsigned int)(bus->busn_res.end) << 16);
195                 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
196         }
197 }
198
199 static void dock_event(acpi_handle handle, u32 type, void *data)
200 {
201         struct acpi_device *adev;
202
203         adev = acpi_bus_get_acpi_device(handle);
204         if (adev) {
205                 acpiphp_hotplug_event(adev, type);
206                 acpi_bus_put_acpi_device(adev);
207         }
208 }
209
210 static const struct acpi_dock_ops acpiphp_dock_ops = {
211         .fixup = post_dock_fixups,
212         .handler = dock_event,
213 };
214
215 /* Check whether the PCI device is managed by native PCIe hotplug driver */
216 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
217 {
218         u32 reg32;
219         acpi_handle tmp;
220         struct acpi_pci_root *root;
221
222         /* Check whether the PCIe port supports native PCIe hotplug */
223         if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
224                 return false;
225         if (!(reg32 & PCI_EXP_SLTCAP_HPC))
226                 return false;
227
228         /*
229          * Check whether native PCIe hotplug has been enabled for
230          * this PCIe hierarchy.
231          */
232         tmp = acpi_find_root_bridge_handle(pdev);
233         if (!tmp)
234                 return false;
235         root = acpi_pci_find_root(tmp);
236         if (!root)
237                 return false;
238         if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
239                 return false;
240
241         return true;
242 }
243
244 static void acpiphp_dock_init(void *data)
245 {
246         struct acpiphp_context *context = data;
247
248         get_bridge(context->func.parent);
249 }
250
251 static void acpiphp_dock_release(void *data)
252 {
253         struct acpiphp_context *context = data;
254
255         put_bridge(context->func.parent);
256 }
257
258 /* callback routine to register each ACPI PCI slot object */
259 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
260                                  void **rv)
261 {
262         struct acpiphp_bridge *bridge = data;
263         struct acpiphp_context *context;
264         struct acpi_device *adev;
265         struct acpiphp_slot *slot;
266         struct acpiphp_func *newfunc;
267         acpi_status status = AE_OK;
268         unsigned long long adr;
269         int device, function;
270         struct pci_bus *pbus = bridge->pci_bus;
271         struct pci_dev *pdev = bridge->pci_dev;
272         u32 val;
273
274         if (pdev && device_is_managed_by_native_pciehp(pdev))
275                 return AE_OK;
276
277         status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
278         if (ACPI_FAILURE(status)) {
279                 if (status != AE_NOT_FOUND)
280                         acpi_handle_warn(handle,
281                                 "can't evaluate _ADR (%#x)\n", status);
282                 return AE_OK;
283         }
284         if (acpi_bus_get_device(handle, &adev))
285                 return AE_OK;
286
287         device = (adr >> 16) & 0xffff;
288         function = adr & 0xffff;
289
290         acpi_lock_hp_context();
291         context = acpiphp_init_context(adev);
292         if (!context) {
293                 acpi_unlock_hp_context();
294                 acpi_handle_err(handle, "No hotplug context\n");
295                 return AE_NOT_EXIST;
296         }
297         newfunc = &context->func;
298         newfunc->function = function;
299         newfunc->parent = bridge;
300
301         if (acpi_has_method(handle, "_EJ0"))
302                 newfunc->flags = FUNC_HAS_EJ0;
303
304         if (acpi_has_method(handle, "_STA"))
305                 newfunc->flags |= FUNC_HAS_STA;
306
307         /*
308          * Dock stations' notify handler should be used for dock devices instead
309          * of the common one, so clear hp.event in their contexts.
310          */
311         if (acpi_has_method(handle, "_DCK"))
312                 context->hp.event = NULL;
313
314         acpi_unlock_hp_context();
315
316         /* search for objects that share the same slot */
317         list_for_each_entry(slot, &bridge->slots, node)
318                 if (slot->device == device)
319                         goto slot_found;
320
321         slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
322         if (!slot) {
323                 acpi_lock_hp_context();
324                 acpiphp_put_context(context);
325                 acpi_unlock_hp_context();
326                 return AE_NO_MEMORY;
327         }
328
329         slot->bus = bridge->pci_bus;
330         slot->device = device;
331         INIT_LIST_HEAD(&slot->funcs);
332
333         list_add_tail(&slot->node, &bridge->slots);
334
335         /* Register slots for ejectable functions only. */
336         if (acpi_pci_check_ejectable(pbus, handle)  || is_dock_device(handle)) {
337                 unsigned long long sun;
338                 int retval;
339
340                 bridge->nr_slots++;
341                 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
342                 if (ACPI_FAILURE(status))
343                         sun = bridge->nr_slots;
344
345                 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
346                     sun, pci_domain_nr(pbus), pbus->number, device);
347
348                 retval = acpiphp_register_hotplug_slot(slot, sun);
349                 if (retval) {
350                         slot->slot = NULL;
351                         bridge->nr_slots--;
352                         if (retval == -EBUSY)
353                                 pr_warn("Slot %llu already registered by another "
354                                         "hotplug driver\n", sun);
355                         else
356                                 pr_warn("acpiphp_register_hotplug_slot failed "
357                                         "(err code = 0x%x)\n", retval);
358                 }
359                 /* Even if the slot registration fails, we can still use it. */
360         }
361
362  slot_found:
363         newfunc->slot = slot;
364         list_add_tail(&newfunc->sibling, &slot->funcs);
365
366         if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
367                                        &val, 60*1000))
368                 slot->flags |= SLOT_ENABLED;
369
370         if (is_dock_device(handle)) {
371                 /* we don't want to call this device's _EJ0
372                  * because we want the dock notify handler
373                  * to call it after it calls _DCK
374                  */
375                 newfunc->flags &= ~FUNC_HAS_EJ0;
376                 if (register_hotplug_dock_device(handle,
377                         &acpiphp_dock_ops, context,
378                         acpiphp_dock_init, acpiphp_dock_release))
379                         pr_debug("failed to register dock device\n");
380         }
381
382         return AE_OK;
383 }
384
385 static struct acpiphp_bridge *acpiphp_dev_to_bridge(struct acpi_device *adev)
386 {
387         struct acpiphp_context *context;
388         struct acpiphp_bridge *bridge = NULL;
389
390         acpi_lock_hp_context();
391         context = acpiphp_get_context(adev);
392         if (context) {
393                 bridge = context->bridge;
394                 if (bridge)
395                         get_bridge(bridge);
396
397                 acpiphp_put_context(context);
398         }
399         acpi_unlock_hp_context();
400         return bridge;
401 }
402
403 static void cleanup_bridge(struct acpiphp_bridge *bridge)
404 {
405         struct acpiphp_slot *slot;
406         struct acpiphp_func *func;
407
408         list_for_each_entry(slot, &bridge->slots, node) {
409                 list_for_each_entry(func, &slot->funcs, sibling) {
410                         struct acpi_device *adev = func_to_acpi_device(func);
411
412                         if (is_dock_device(adev->handle))
413                                 unregister_hotplug_dock_device(adev->handle);
414
415                         acpi_lock_hp_context();
416                         adev->hp->event = NULL;
417                         acpi_unlock_hp_context();
418                 }
419                 slot->flags |= SLOT_IS_GOING_AWAY;
420                 if (slot->slot)
421                         acpiphp_unregister_hotplug_slot(slot);
422         }
423
424         mutex_lock(&bridge_mutex);
425         list_del(&bridge->list);
426         mutex_unlock(&bridge_mutex);
427
428         acpi_lock_hp_context();
429         bridge->is_going_away = true;
430         acpi_unlock_hp_context();
431 }
432
433 /**
434  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
435  * @bus: bus to start search with
436  */
437 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
438 {
439         struct list_head *tmp;
440         unsigned char max, n;
441
442         /*
443          * pci_bus_max_busnr will return the highest
444          * reserved busnr for all these children.
445          * that is equivalent to the bus->subordinate
446          * value.  We don't want to use the parent's
447          * bus->subordinate value because it could have
448          * padding in it.
449          */
450         max = bus->busn_res.start;
451
452         list_for_each(tmp, &bus->children) {
453                 n = pci_bus_max_busnr(pci_bus_b(tmp));
454                 if (n > max)
455                         max = n;
456         }
457         return max;
458 }
459
460 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
461 {
462         struct acpiphp_func *func;
463         union acpi_object params[2];
464         struct acpi_object_list arg_list;
465
466         list_for_each_entry(func, &slot->funcs, sibling) {
467                 arg_list.count = 2;
468                 arg_list.pointer = params;
469                 params[0].type = ACPI_TYPE_INTEGER;
470                 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
471                 params[1].type = ACPI_TYPE_INTEGER;
472                 params[1].integer.value = 1;
473                 /* _REG is optional, we don't care about if there is failure */
474                 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
475                                      NULL);
476         }
477 }
478
479 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
480 {
481         struct acpiphp_func *func;
482
483         /* quirk, or pcie could set it already */
484         if (dev->is_hotplug_bridge)
485                 return;
486
487         list_for_each_entry(func, &slot->funcs, sibling) {
488                 if (PCI_FUNC(dev->devfn) == func->function) {
489                         dev->is_hotplug_bridge = 1;
490                         break;
491                 }
492         }
493 }
494
495 static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
496 {
497         struct acpiphp_func *func;
498
499         list_for_each_entry(func, &slot->funcs, sibling) {
500                 struct acpi_device *adev = func_to_acpi_device(func);
501
502                 acpi_bus_scan(adev->handle);
503                 if (acpi_device_enumerated(adev))
504                         acpi_device_set_power(adev, ACPI_STATE_D0);
505         }
506         return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
507 }
508
509 /**
510  * enable_slot - enable, configure a slot
511  * @slot: slot to be enabled
512  *
513  * This function should be called per *physical slot*,
514  * not per each slot object in ACPI namespace.
515  */
516 static void __ref enable_slot(struct acpiphp_slot *slot)
517 {
518         struct pci_dev *dev;
519         struct pci_bus *bus = slot->bus;
520         struct acpiphp_func *func;
521         int max, pass;
522         LIST_HEAD(add_list);
523
524         acpiphp_rescan_slot(slot);
525         max = acpiphp_max_busnr(bus);
526         for (pass = 0; pass < 2; pass++) {
527                 list_for_each_entry(dev, &bus->devices, bus_list) {
528                         if (PCI_SLOT(dev->devfn) != slot->device)
529                                 continue;
530
531                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
532                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
533                                 max = pci_scan_bridge(bus, dev, max, pass);
534                                 if (pass && dev->subordinate) {
535                                         check_hotplug_bridge(slot, dev);
536                                         pcibios_resource_survey_bus(dev->subordinate);
537                                         __pci_bus_size_bridges(dev->subordinate,
538                                                                &add_list);
539                                 }
540                         }
541                 }
542         }
543         __pci_bus_assign_resources(bus, &add_list, NULL);
544
545         acpiphp_sanitize_bus(bus);
546         acpiphp_set_hpp_values(bus);
547         acpiphp_set_acpi_region(slot);
548
549         list_for_each_entry(dev, &bus->devices, bus_list) {
550                 /* Assume that newly added devices are powered on already. */
551                 if (!dev->is_added)
552                         dev->current_state = PCI_D0;
553         }
554
555         pci_bus_add_devices(bus);
556
557         slot->flags |= SLOT_ENABLED;
558         list_for_each_entry(func, &slot->funcs, sibling) {
559                 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
560                                                   func->function));
561                 if (!dev) {
562                         /* Do not set SLOT_ENABLED flag if some funcs
563                            are not added. */
564                         slot->flags &= (~SLOT_ENABLED);
565                         continue;
566                 }
567         }
568 }
569
570 /**
571  * disable_slot - disable a slot
572  * @slot: ACPI PHP slot
573  */
574 static void disable_slot(struct acpiphp_slot *slot)
575 {
576         struct pci_bus *bus = slot->bus;
577         struct pci_dev *dev, *prev;
578         struct acpiphp_func *func;
579
580         /*
581          * enable_slot() enumerates all functions in this device via
582          * pci_scan_slot(), whether they have associated ACPI hotplug
583          * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
584          * here.
585          */
586         list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
587                 if (PCI_SLOT(dev->devfn) == slot->device)
588                         pci_stop_and_remove_bus_device(dev);
589
590         list_for_each_entry(func, &slot->funcs, sibling)
591                 acpi_bus_trim(func_to_acpi_device(func));
592
593         slot->flags &= (~SLOT_ENABLED);
594 }
595
596 static bool acpiphp_no_hotplug(struct acpi_device *adev)
597 {
598         return adev && adev->flags.no_hotplug;
599 }
600
601 static bool slot_no_hotplug(struct acpiphp_slot *slot)
602 {
603         struct acpiphp_func *func;
604
605         list_for_each_entry(func, &slot->funcs, sibling)
606                 if (acpiphp_no_hotplug(func_to_acpi_device(func)))
607                         return true;
608
609         return false;
610 }
611
612 /**
613  * get_slot_status - get ACPI slot status
614  * @slot: ACPI PHP slot
615  *
616  * If a slot has _STA for each function and if any one of them
617  * returned non-zero status, return it.
618  *
619  * If a slot doesn't have _STA and if any one of its functions'
620  * configuration space is configured, return 0x0f as a _STA.
621  *
622  * Otherwise return 0.
623  */
624 static unsigned int get_slot_status(struct acpiphp_slot *slot)
625 {
626         unsigned long long sta = 0;
627         struct acpiphp_func *func;
628
629         list_for_each_entry(func, &slot->funcs, sibling) {
630                 if (func->flags & FUNC_HAS_STA) {
631                         acpi_status status;
632
633                         status = acpi_evaluate_integer(func_to_handle(func),
634                                                        "_STA", NULL, &sta);
635                         if (ACPI_SUCCESS(status) && sta)
636                                 break;
637                 } else {
638                         u32 dvid;
639
640                         pci_bus_read_config_dword(slot->bus,
641                                                   PCI_DEVFN(slot->device,
642                                                             func->function),
643                                                   PCI_VENDOR_ID, &dvid);
644                         if (dvid != 0xffffffff) {
645                                 sta = ACPI_STA_ALL;
646                                 break;
647                         }
648                 }
649         }
650
651         return (unsigned int)sta;
652 }
653
654 /**
655  * trim_stale_devices - remove PCI devices that are not responding.
656  * @dev: PCI device to start walking the hierarchy from.
657  */
658 static void trim_stale_devices(struct pci_dev *dev)
659 {
660         struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
661         struct pci_bus *bus = dev->subordinate;
662         bool alive = false;
663
664         if (adev) {
665                 acpi_status status;
666                 unsigned long long sta;
667
668                 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
669                 alive = (ACPI_SUCCESS(status) && sta == ACPI_STA_ALL)
670                         || acpiphp_no_hotplug(adev);
671         }
672         if (!alive) {
673                 u32 v;
674
675                 /* Check if the device responds. */
676                 alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
677         }
678         if (!alive) {
679                 pci_stop_and_remove_bus_device(dev);
680                 if (adev)
681                         acpi_bus_trim(adev);
682         } else if (bus) {
683                 struct pci_dev *child, *tmp;
684
685                 /* The device is a bridge. so check the bus below it. */
686                 pm_runtime_get_sync(&dev->dev);
687                 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
688                         trim_stale_devices(child);
689
690                 pm_runtime_put(&dev->dev);
691         }
692 }
693
694 /**
695  * acpiphp_check_bridge - re-enumerate devices
696  * @bridge: where to begin re-enumeration
697  *
698  * Iterate over all slots under this bridge and make sure that if a
699  * card is present they are enabled, and if not they are disabled.
700  */
701 static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
702 {
703         struct acpiphp_slot *slot;
704
705         /* Bail out if the bridge is going away. */
706         if (bridge->is_going_away)
707                 return;
708
709         list_for_each_entry(slot, &bridge->slots, node) {
710                 struct pci_bus *bus = slot->bus;
711                 struct pci_dev *dev, *tmp;
712
713                 if (slot_no_hotplug(slot)) {
714                         ; /* do nothing */
715                 } else if (get_slot_status(slot) == ACPI_STA_ALL) {
716                         /* remove stale devices if any */
717                         list_for_each_entry_safe_reverse(dev, tmp,
718                                                          &bus->devices, bus_list)
719                                 if (PCI_SLOT(dev->devfn) == slot->device)
720                                         trim_stale_devices(dev);
721
722                         /* configure all functions */
723                         enable_slot(slot);
724                 } else {
725                         disable_slot(slot);
726                 }
727         }
728 }
729
730 static void acpiphp_set_hpp_values(struct pci_bus *bus)
731 {
732         struct pci_dev *dev;
733
734         list_for_each_entry(dev, &bus->devices, bus_list)
735                 pci_configure_slot(dev);
736 }
737
738 /*
739  * Remove devices for which we could not assign resources, call
740  * arch specific code to fix-up the bus
741  */
742 static void acpiphp_sanitize_bus(struct pci_bus *bus)
743 {
744         struct pci_dev *dev, *tmp;
745         int i;
746         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
747
748         list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
749                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
750                         struct resource *res = &dev->resource[i];
751                         if ((res->flags & type_mask) && !res->start &&
752                                         res->end) {
753                                 /* Could not assign a required resources
754                                  * for this device, remove it */
755                                 pci_stop_and_remove_bus_device(dev);
756                                 break;
757                         }
758                 }
759         }
760 }
761
762 /*
763  * ACPI event handlers
764  */
765
766 void acpiphp_check_host_bridge(struct acpi_device *adev)
767 {
768         struct acpiphp_bridge *bridge;
769
770         bridge = acpiphp_dev_to_bridge(adev);
771         if (bridge) {
772                 pci_lock_rescan_remove();
773
774                 acpiphp_check_bridge(bridge);
775
776                 pci_unlock_rescan_remove();
777                 put_bridge(bridge);
778         }
779 }
780
781 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
782
783 static void hotplug_event(u32 type, struct acpiphp_context *context)
784 {
785         acpi_handle handle = context->hp.self->handle;
786         struct acpiphp_func *func = &context->func;
787         struct acpiphp_slot *slot = func->slot;
788         struct acpiphp_bridge *bridge;
789
790         acpi_lock_hp_context();
791         bridge = context->bridge;
792         if (bridge)
793                 get_bridge(bridge);
794
795         acpi_unlock_hp_context();
796
797         pci_lock_rescan_remove();
798
799         switch (type) {
800         case ACPI_NOTIFY_BUS_CHECK:
801                 /* bus re-enumerate */
802                 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
803                 if (bridge)
804                         acpiphp_check_bridge(bridge);
805                 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
806                         enable_slot(slot);
807
808                 break;
809
810         case ACPI_NOTIFY_DEVICE_CHECK:
811                 /* device check */
812                 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
813                 if (bridge) {
814                         acpiphp_check_bridge(bridge);
815                 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
816                         /*
817                          * Check if anything has changed in the slot and rescan
818                          * from the parent if that's the case.
819                          */
820                         if (acpiphp_rescan_slot(slot))
821                                 acpiphp_check_bridge(func->parent);
822                 }
823                 break;
824
825         case ACPI_NOTIFY_EJECT_REQUEST:
826                 /* request device eject */
827                 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
828                 acpiphp_disable_and_eject_slot(slot);
829                 break;
830         }
831
832         pci_unlock_rescan_remove();
833         if (bridge)
834                 put_bridge(bridge);
835 }
836
837 static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type)
838 {
839         struct acpiphp_context *context;
840
841         acpi_lock_hp_context();
842         context = acpiphp_get_context(adev);
843         if (!context || context->func.parent->is_going_away) {
844                 acpi_unlock_hp_context();
845                 return -ENODATA;
846         }
847         get_bridge(context->func.parent);
848         acpiphp_put_context(context);
849         acpi_unlock_hp_context();
850
851         hotplug_event(type, context);
852
853         put_bridge(context->func.parent);
854         return 0;
855 }
856
857 /**
858  * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
859  * @bus: PCI bus to enumerate the slots for.
860  *
861  * A "slot" is an object associated with a PCI device number.  All functions
862  * (PCI devices) with the same bus and device number belong to the same slot.
863  */
864 void acpiphp_enumerate_slots(struct pci_bus *bus)
865 {
866         struct acpiphp_bridge *bridge;
867         struct acpi_device *adev;
868         acpi_handle handle;
869         acpi_status status;
870
871         if (acpiphp_disabled)
872                 return;
873
874         adev = ACPI_COMPANION(bus->bridge);
875         if (!adev)
876                 return;
877
878         handle = adev->handle;
879         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
880         if (!bridge) {
881                 acpi_handle_err(handle, "No memory for bridge object\n");
882                 return;
883         }
884
885         INIT_LIST_HEAD(&bridge->slots);
886         kref_init(&bridge->ref);
887         bridge->pci_dev = pci_dev_get(bus->self);
888         bridge->pci_bus = bus;
889
890         /*
891          * Grab a ref to the subordinate PCI bus in case the bus is
892          * removed via PCI core logical hotplug. The ref pins the bus
893          * (which we access during module unload).
894          */
895         get_device(&bus->dev);
896
897         if (!pci_is_root_bus(bridge->pci_bus)) {
898                 struct acpiphp_context *context;
899
900                 /*
901                  * This bridge should have been registered as a hotplug function
902                  * under its parent, so the context should be there, unless the
903                  * parent is going to be handled by pciehp, in which case this
904                  * bridge is not interesting to us either.
905                  */
906                 acpi_lock_hp_context();
907                 context = acpiphp_get_context(adev);
908                 if (!context) {
909                         acpi_unlock_hp_context();
910                         put_device(&bus->dev);
911                         pci_dev_put(bridge->pci_dev);
912                         kfree(bridge);
913                         return;
914                 }
915                 bridge->context = context;
916                 context->bridge = bridge;
917                 /* Get a reference to the parent bridge. */
918                 get_bridge(context->func.parent);
919                 acpi_unlock_hp_context();
920         }
921
922         /* must be added to the list prior to calling register_slot */
923         mutex_lock(&bridge_mutex);
924         list_add(&bridge->list, &bridge_list);
925         mutex_unlock(&bridge_mutex);
926
927         /* register all slot objects under this bridge */
928         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
929                                      register_slot, NULL, bridge, NULL);
930         if (ACPI_FAILURE(status)) {
931                 acpi_handle_err(handle, "failed to register slots\n");
932                 cleanup_bridge(bridge);
933                 put_bridge(bridge);
934         }
935 }
936
937 /**
938  * acpiphp_remove_slots - Remove slot objects associated with a given bus.
939  * @bus: PCI bus to remove the slot objects for.
940  */
941 void acpiphp_remove_slots(struct pci_bus *bus)
942 {
943         struct acpiphp_bridge *bridge;
944
945         if (acpiphp_disabled)
946                 return;
947
948         mutex_lock(&bridge_mutex);
949         list_for_each_entry(bridge, &bridge_list, list)
950                 if (bridge->pci_bus == bus) {
951                         mutex_unlock(&bridge_mutex);
952                         cleanup_bridge(bridge);
953                         put_bridge(bridge);
954                         return;
955                 }
956
957         mutex_unlock(&bridge_mutex);
958 }
959
960 /**
961  * acpiphp_enable_slot - power on slot
962  * @slot: ACPI PHP slot
963  */
964 int acpiphp_enable_slot(struct acpiphp_slot *slot)
965 {
966         pci_lock_rescan_remove();
967
968         if (slot->flags & SLOT_IS_GOING_AWAY)
969                 return -ENODEV;
970
971         /* configure all functions */
972         if (!(slot->flags & SLOT_ENABLED))
973                 enable_slot(slot);
974
975         pci_unlock_rescan_remove();
976         return 0;
977 }
978
979 /**
980  * acpiphp_disable_and_eject_slot - power off and eject slot
981  * @slot: ACPI PHP slot
982  */
983 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
984 {
985         struct acpiphp_func *func;
986
987         if (slot->flags & SLOT_IS_GOING_AWAY)
988                 return -ENODEV;
989
990         /* unconfigure all functions */
991         disable_slot(slot);
992
993         list_for_each_entry(func, &slot->funcs, sibling)
994                 if (func->flags & FUNC_HAS_EJ0) {
995                         acpi_handle handle = func_to_handle(func);
996
997                         if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
998                                 acpi_handle_err(handle, "_EJ0 failed\n");
999
1000                         break;
1001                 }
1002
1003         return 0;
1004 }
1005
1006 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1007 {
1008         int ret;
1009
1010         /*
1011          * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
1012          * acpiphp_disable_and_eject_slot() will be synchronized properly.
1013          */
1014         acpi_scan_lock_acquire();
1015         pci_lock_rescan_remove();
1016         ret = acpiphp_disable_and_eject_slot(slot);
1017         pci_unlock_rescan_remove();
1018         acpi_scan_lock_release();
1019         return ret;
1020 }
1021
1022 /*
1023  * slot enabled:  1
1024  * slot disabled: 0
1025  */
1026 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1027 {
1028         return (slot->flags & SLOT_ENABLED);
1029 }
1030
1031 /*
1032  * latch   open:  1
1033  * latch closed:  0
1034  */
1035 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1036 {
1037         return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1038 }
1039
1040 /*
1041  * adapter presence : 1
1042  *          absence : 0
1043  */
1044 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1045 {
1046         return !!get_slot_status(slot);
1047 }