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