7c498d663eb3b30462fc97cfb543340711c8a5f8
[firefly-linux-kernel-4.4.55.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         acpi_unlock_hp_context();
301
302         if (acpi_has_method(handle, "_EJ0"))
303                 newfunc->flags = FUNC_HAS_EJ0;
304
305         if (acpi_has_method(handle, "_STA"))
306                 newfunc->flags |= FUNC_HAS_STA;
307
308         if (acpi_has_method(handle, "_DCK"))
309                 newfunc->flags |= FUNC_HAS_DCK;
310
311         /* search for objects that share the same slot */
312         list_for_each_entry(slot, &bridge->slots, node)
313                 if (slot->device == device)
314                         goto slot_found;
315
316         slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
317         if (!slot) {
318                 acpi_lock_hp_context();
319                 acpiphp_put_context(context);
320                 acpi_unlock_hp_context();
321                 return AE_NO_MEMORY;
322         }
323
324         slot->bus = bridge->pci_bus;
325         slot->device = device;
326         INIT_LIST_HEAD(&slot->funcs);
327
328         list_add_tail(&slot->node, &bridge->slots);
329
330         /* Register slots for ejectable functions only. */
331         if (acpi_pci_check_ejectable(pbus, handle)  || is_dock_device(handle)) {
332                 unsigned long long sun;
333                 int retval;
334
335                 bridge->nr_slots++;
336                 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
337                 if (ACPI_FAILURE(status))
338                         sun = bridge->nr_slots;
339
340                 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
341                     sun, pci_domain_nr(pbus), pbus->number, device);
342
343                 retval = acpiphp_register_hotplug_slot(slot, sun);
344                 if (retval) {
345                         slot->slot = NULL;
346                         bridge->nr_slots--;
347                         if (retval == -EBUSY)
348                                 pr_warn("Slot %llu already registered by another "
349                                         "hotplug driver\n", sun);
350                         else
351                                 pr_warn("acpiphp_register_hotplug_slot failed "
352                                         "(err code = 0x%x)\n", retval);
353                 }
354                 /* Even if the slot registration fails, we can still use it. */
355         }
356
357  slot_found:
358         newfunc->slot = slot;
359         list_add_tail(&newfunc->sibling, &slot->funcs);
360
361         if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
362                                        &val, 60*1000))
363                 slot->flags |= SLOT_ENABLED;
364
365         if (is_dock_device(handle)) {
366                 /* we don't want to call this device's _EJ0
367                  * because we want the dock notify handler
368                  * to call it after it calls _DCK
369                  */
370                 newfunc->flags &= ~FUNC_HAS_EJ0;
371                 if (register_hotplug_dock_device(handle,
372                         &acpiphp_dock_ops, context,
373                         acpiphp_dock_init, acpiphp_dock_release))
374                         pr_debug("failed to register dock device\n");
375         }
376
377         /* install notify handler */
378         if (!(newfunc->flags & FUNC_HAS_DCK))
379                 acpi_install_hotplug_notify_handler(handle, NULL);
380
381         return AE_OK;
382 }
383
384 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
385 {
386         struct acpi_device *adev = acpi_bus_get_acpi_device(handle);
387         struct acpiphp_context *context;
388         struct acpiphp_bridge *bridge = NULL;
389
390         if (!adev)
391                 return NULL;
392
393         acpi_lock_hp_context();
394         context = acpiphp_get_context(adev);
395         if (context) {
396                 bridge = context->bridge;
397                 if (bridge)
398                         get_bridge(bridge);
399
400                 acpiphp_put_context(context);
401         }
402         acpi_unlock_hp_context();
403         acpi_bus_put_acpi_device(adev);
404         return bridge;
405 }
406
407 static void cleanup_bridge(struct acpiphp_bridge *bridge)
408 {
409         struct acpiphp_slot *slot;
410         struct acpiphp_func *func;
411
412         list_for_each_entry(slot, &bridge->slots, node) {
413                 list_for_each_entry(func, &slot->funcs, sibling) {
414                         acpi_handle handle = func_to_handle(func);
415
416                         if (is_dock_device(handle))
417                                 unregister_hotplug_dock_device(handle);
418
419                         if (!(func->flags & FUNC_HAS_DCK))
420                                 acpi_remove_hotplug_notify_handler(handle);
421                 }
422                 slot->flags |= SLOT_IS_GOING_AWAY;
423                 if (slot->slot)
424                         acpiphp_unregister_hotplug_slot(slot);
425         }
426
427         mutex_lock(&bridge_mutex);
428         list_del(&bridge->list);
429         mutex_unlock(&bridge_mutex);
430
431         acpi_lock_hp_context();
432         bridge->is_going_away = true;
433         acpi_unlock_hp_context();
434 }
435
436 /**
437  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
438  * @bus: bus to start search with
439  */
440 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
441 {
442         struct list_head *tmp;
443         unsigned char max, n;
444
445         /*
446          * pci_bus_max_busnr will return the highest
447          * reserved busnr for all these children.
448          * that is equivalent to the bus->subordinate
449          * value.  We don't want to use the parent's
450          * bus->subordinate value because it could have
451          * padding in it.
452          */
453         max = bus->busn_res.start;
454
455         list_for_each(tmp, &bus->children) {
456                 n = pci_bus_max_busnr(pci_bus_b(tmp));
457                 if (n > max)
458                         max = n;
459         }
460         return max;
461 }
462
463 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
464 {
465         struct acpiphp_func *func;
466         union acpi_object params[2];
467         struct acpi_object_list arg_list;
468
469         list_for_each_entry(func, &slot->funcs, sibling) {
470                 arg_list.count = 2;
471                 arg_list.pointer = params;
472                 params[0].type = ACPI_TYPE_INTEGER;
473                 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
474                 params[1].type = ACPI_TYPE_INTEGER;
475                 params[1].integer.value = 1;
476                 /* _REG is optional, we don't care about if there is failure */
477                 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
478                                      NULL);
479         }
480 }
481
482 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
483 {
484         struct acpiphp_func *func;
485
486         /* quirk, or pcie could set it already */
487         if (dev->is_hotplug_bridge)
488                 return;
489
490         list_for_each_entry(func, &slot->funcs, sibling) {
491                 if (PCI_FUNC(dev->devfn) == func->function) {
492                         dev->is_hotplug_bridge = 1;
493                         break;
494                 }
495         }
496 }
497
498 static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
499 {
500         struct acpiphp_func *func;
501
502         list_for_each_entry(func, &slot->funcs, sibling) {
503                 struct acpi_device *adev = func_to_acpi_device(func);
504
505                 acpi_bus_scan(adev->handle);
506                 if (acpi_device_enumerated(adev))
507                         acpi_device_set_power(adev, ACPI_STATE_D0);
508         }
509         return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
510 }
511
512 /**
513  * enable_slot - enable, configure a slot
514  * @slot: slot to be enabled
515  *
516  * This function should be called per *physical slot*,
517  * not per each slot object in ACPI namespace.
518  */
519 static void __ref enable_slot(struct acpiphp_slot *slot)
520 {
521         struct pci_dev *dev;
522         struct pci_bus *bus = slot->bus;
523         struct acpiphp_func *func;
524         int max, pass;
525         LIST_HEAD(add_list);
526
527         acpiphp_rescan_slot(slot);
528         max = acpiphp_max_busnr(bus);
529         for (pass = 0; pass < 2; pass++) {
530                 list_for_each_entry(dev, &bus->devices, bus_list) {
531                         if (PCI_SLOT(dev->devfn) != slot->device)
532                                 continue;
533
534                         if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
535                             dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
536                                 max = pci_scan_bridge(bus, dev, max, pass);
537                                 if (pass && dev->subordinate) {
538                                         check_hotplug_bridge(slot, dev);
539                                         pcibios_resource_survey_bus(dev->subordinate);
540                                         __pci_bus_size_bridges(dev->subordinate,
541                                                                &add_list);
542                                 }
543                         }
544                 }
545         }
546         __pci_bus_assign_resources(bus, &add_list, NULL);
547
548         acpiphp_sanitize_bus(bus);
549         acpiphp_set_hpp_values(bus);
550         acpiphp_set_acpi_region(slot);
551
552         list_for_each_entry(dev, &bus->devices, bus_list) {
553                 /* Assume that newly added devices are powered on already. */
554                 if (!dev->is_added)
555                         dev->current_state = PCI_D0;
556         }
557
558         pci_bus_add_devices(bus);
559
560         slot->flags |= SLOT_ENABLED;
561         list_for_each_entry(func, &slot->funcs, sibling) {
562                 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
563                                                   func->function));
564                 if (!dev) {
565                         /* Do not set SLOT_ENABLED flag if some funcs
566                            are not added. */
567                         slot->flags &= (~SLOT_ENABLED);
568                         continue;
569                 }
570         }
571 }
572
573 /**
574  * disable_slot - disable a slot
575  * @slot: ACPI PHP slot
576  */
577 static void disable_slot(struct acpiphp_slot *slot)
578 {
579         struct pci_bus *bus = slot->bus;
580         struct pci_dev *dev, *prev;
581         struct acpiphp_func *func;
582
583         /*
584          * enable_slot() enumerates all functions in this device via
585          * pci_scan_slot(), whether they have associated ACPI hotplug
586          * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
587          * here.
588          */
589         list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
590                 if (PCI_SLOT(dev->devfn) == slot->device)
591                         pci_stop_and_remove_bus_device(dev);
592
593         list_for_each_entry(func, &slot->funcs, sibling)
594                 acpi_bus_trim(func_to_acpi_device(func));
595
596         slot->flags &= (~SLOT_ENABLED);
597 }
598
599 static bool acpiphp_no_hotplug(struct acpi_device *adev)
600 {
601         return adev && adev->flags.no_hotplug;
602 }
603
604 static bool slot_no_hotplug(struct acpiphp_slot *slot)
605 {
606         struct acpiphp_func *func;
607
608         list_for_each_entry(func, &slot->funcs, sibling)
609                 if (acpiphp_no_hotplug(func_to_acpi_device(func)))
610                         return true;
611
612         return false;
613 }
614
615 /**
616  * get_slot_status - get ACPI slot status
617  * @slot: ACPI PHP slot
618  *
619  * If a slot has _STA for each function and if any one of them
620  * returned non-zero status, return it.
621  *
622  * If a slot doesn't have _STA and if any one of its functions'
623  * configuration space is configured, return 0x0f as a _STA.
624  *
625  * Otherwise return 0.
626  */
627 static unsigned int get_slot_status(struct acpiphp_slot *slot)
628 {
629         unsigned long long sta = 0;
630         struct acpiphp_func *func;
631
632         list_for_each_entry(func, &slot->funcs, sibling) {
633                 if (func->flags & FUNC_HAS_STA) {
634                         acpi_status status;
635
636                         status = acpi_evaluate_integer(func_to_handle(func),
637                                                        "_STA", NULL, &sta);
638                         if (ACPI_SUCCESS(status) && sta)
639                                 break;
640                 } else {
641                         u32 dvid;
642
643                         pci_bus_read_config_dword(slot->bus,
644                                                   PCI_DEVFN(slot->device,
645                                                             func->function),
646                                                   PCI_VENDOR_ID, &dvid);
647                         if (dvid != 0xffffffff) {
648                                 sta = ACPI_STA_ALL;
649                                 break;
650                         }
651                 }
652         }
653
654         return (unsigned int)sta;
655 }
656
657 /**
658  * trim_stale_devices - remove PCI devices that are not responding.
659  * @dev: PCI device to start walking the hierarchy from.
660  */
661 static void trim_stale_devices(struct pci_dev *dev)
662 {
663         struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
664         struct pci_bus *bus = dev->subordinate;
665         bool alive = false;
666
667         if (adev) {
668                 acpi_status status;
669                 unsigned long long sta;
670
671                 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
672                 alive = (ACPI_SUCCESS(status) && sta == ACPI_STA_ALL)
673                         || acpiphp_no_hotplug(adev);
674         }
675         if (!alive) {
676                 u32 v;
677
678                 /* Check if the device responds. */
679                 alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
680         }
681         if (!alive) {
682                 pci_stop_and_remove_bus_device(dev);
683                 if (adev)
684                         acpi_bus_trim(adev);
685         } else if (bus) {
686                 struct pci_dev *child, *tmp;
687
688                 /* The device is a bridge. so check the bus below it. */
689                 pm_runtime_get_sync(&dev->dev);
690                 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
691                         trim_stale_devices(child);
692
693                 pm_runtime_put(&dev->dev);
694         }
695 }
696
697 /**
698  * acpiphp_check_bridge - re-enumerate devices
699  * @bridge: where to begin re-enumeration
700  *
701  * Iterate over all slots under this bridge and make sure that if a
702  * card is present they are enabled, and if not they are disabled.
703  */
704 static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
705 {
706         struct acpiphp_slot *slot;
707
708         /* Bail out if the bridge is going away. */
709         if (bridge->is_going_away)
710                 return;
711
712         list_for_each_entry(slot, &bridge->slots, node) {
713                 struct pci_bus *bus = slot->bus;
714                 struct pci_dev *dev, *tmp;
715
716                 if (slot_no_hotplug(slot)) {
717                         ; /* do nothing */
718                 } else if (get_slot_status(slot) == ACPI_STA_ALL) {
719                         /* remove stale devices if any */
720                         list_for_each_entry_safe_reverse(dev, tmp,
721                                                          &bus->devices, bus_list)
722                                 if (PCI_SLOT(dev->devfn) == slot->device)
723                                         trim_stale_devices(dev);
724
725                         /* configure all functions */
726                         enable_slot(slot);
727                 } else {
728                         disable_slot(slot);
729                 }
730         }
731 }
732
733 static void acpiphp_set_hpp_values(struct pci_bus *bus)
734 {
735         struct pci_dev *dev;
736
737         list_for_each_entry(dev, &bus->devices, bus_list)
738                 pci_configure_slot(dev);
739 }
740
741 /*
742  * Remove devices for which we could not assign resources, call
743  * arch specific code to fix-up the bus
744  */
745 static void acpiphp_sanitize_bus(struct pci_bus *bus)
746 {
747         struct pci_dev *dev, *tmp;
748         int i;
749         unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
750
751         list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
752                 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
753                         struct resource *res = &dev->resource[i];
754                         if ((res->flags & type_mask) && !res->start &&
755                                         res->end) {
756                                 /* Could not assign a required resources
757                                  * for this device, remove it */
758                                 pci_stop_and_remove_bus_device(dev);
759                                 break;
760                         }
761                 }
762         }
763 }
764
765 /*
766  * ACPI event handlers
767  */
768
769 void acpiphp_check_host_bridge(acpi_handle handle)
770 {
771         struct acpiphp_bridge *bridge;
772
773         bridge = acpiphp_handle_to_bridge(handle);
774         if (bridge) {
775                 pci_lock_rescan_remove();
776
777                 acpiphp_check_bridge(bridge);
778
779                 pci_unlock_rescan_remove();
780                 put_bridge(bridge);
781         }
782 }
783
784 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
785
786 static void hotplug_event(u32 type, struct acpiphp_context *context)
787 {
788         acpi_handle handle = context->hp.self->handle;
789         struct acpiphp_func *func = &context->func;
790         struct acpiphp_slot *slot = func->slot;
791         struct acpiphp_bridge *bridge;
792
793         acpi_lock_hp_context();
794         bridge = context->bridge;
795         if (bridge)
796                 get_bridge(bridge);
797
798         acpi_unlock_hp_context();
799
800         pci_lock_rescan_remove();
801
802         switch (type) {
803         case ACPI_NOTIFY_BUS_CHECK:
804                 /* bus re-enumerate */
805                 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
806                 if (bridge)
807                         acpiphp_check_bridge(bridge);
808                 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
809                         enable_slot(slot);
810
811                 break;
812
813         case ACPI_NOTIFY_DEVICE_CHECK:
814                 /* device check */
815                 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
816                 if (bridge) {
817                         acpiphp_check_bridge(bridge);
818                 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
819                         /*
820                          * Check if anything has changed in the slot and rescan
821                          * from the parent if that's the case.
822                          */
823                         if (acpiphp_rescan_slot(slot))
824                                 acpiphp_check_bridge(func->parent);
825                 }
826                 break;
827
828         case ACPI_NOTIFY_EJECT_REQUEST:
829                 /* request device eject */
830                 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
831                 acpiphp_disable_and_eject_slot(slot);
832                 break;
833         }
834
835         pci_unlock_rescan_remove();
836         if (bridge)
837                 put_bridge(bridge);
838 }
839
840 static int acpiphp_hotplug_event(struct acpi_device *adev, u32 type)
841 {
842         struct acpiphp_context *context;
843
844         acpi_lock_hp_context();
845         context = acpiphp_get_context(adev);
846         if (!context || context->func.parent->is_going_away) {
847                 acpi_unlock_hp_context();
848                 return -ENODATA;
849         }
850         get_bridge(context->func.parent);
851         acpiphp_put_context(context);
852         acpi_unlock_hp_context();
853
854         hotplug_event(type, context);
855
856         put_bridge(context->func.parent);
857         return 0;
858 }
859
860 /**
861  * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
862  * @bus: PCI bus to enumerate the slots for.
863  *
864  * A "slot" is an object associated with a PCI device number.  All functions
865  * (PCI devices) with the same bus and device number belong to the same slot.
866  */
867 void acpiphp_enumerate_slots(struct pci_bus *bus)
868 {
869         struct acpiphp_bridge *bridge;
870         struct acpi_device *adev;
871         acpi_handle handle;
872         acpi_status status;
873
874         if (acpiphp_disabled)
875                 return;
876
877         adev = ACPI_COMPANION(bus->bridge);
878         if (!adev)
879                 return;
880
881         handle = adev->handle;
882         bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
883         if (!bridge) {
884                 acpi_handle_err(handle, "No memory for bridge object\n");
885                 return;
886         }
887
888         INIT_LIST_HEAD(&bridge->slots);
889         kref_init(&bridge->ref);
890         bridge->pci_dev = pci_dev_get(bus->self);
891         bridge->pci_bus = bus;
892
893         /*
894          * Grab a ref to the subordinate PCI bus in case the bus is
895          * removed via PCI core logical hotplug. The ref pins the bus
896          * (which we access during module unload).
897          */
898         get_device(&bus->dev);
899
900         if (!pci_is_root_bus(bridge->pci_bus)) {
901                 struct acpiphp_context *context;
902
903                 /*
904                  * This bridge should have been registered as a hotplug function
905                  * under its parent, so the context should be there, unless the
906                  * parent is going to be handled by pciehp, in which case this
907                  * bridge is not interesting to us either.
908                  */
909                 acpi_lock_hp_context();
910                 context = acpiphp_get_context(adev);
911                 if (!context) {
912                         acpi_unlock_hp_context();
913                         put_device(&bus->dev);
914                         pci_dev_put(bridge->pci_dev);
915                         kfree(bridge);
916                         return;
917                 }
918                 bridge->context = context;
919                 context->bridge = bridge;
920                 /* Get a reference to the parent bridge. */
921                 get_bridge(context->func.parent);
922                 acpi_unlock_hp_context();
923         }
924
925         /* must be added to the list prior to calling register_slot */
926         mutex_lock(&bridge_mutex);
927         list_add(&bridge->list, &bridge_list);
928         mutex_unlock(&bridge_mutex);
929
930         /* register all slot objects under this bridge */
931         status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
932                                      register_slot, NULL, bridge, NULL);
933         if (ACPI_FAILURE(status)) {
934                 acpi_handle_err(handle, "failed to register slots\n");
935                 cleanup_bridge(bridge);
936                 put_bridge(bridge);
937         }
938 }
939
940 /**
941  * acpiphp_remove_slots - Remove slot objects associated with a given bus.
942  * @bus: PCI bus to remove the slot objects for.
943  */
944 void acpiphp_remove_slots(struct pci_bus *bus)
945 {
946         struct acpiphp_bridge *bridge;
947
948         if (acpiphp_disabled)
949                 return;
950
951         mutex_lock(&bridge_mutex);
952         list_for_each_entry(bridge, &bridge_list, list)
953                 if (bridge->pci_bus == bus) {
954                         mutex_unlock(&bridge_mutex);
955                         cleanup_bridge(bridge);
956                         put_bridge(bridge);
957                         return;
958                 }
959
960         mutex_unlock(&bridge_mutex);
961 }
962
963 /**
964  * acpiphp_enable_slot - power on slot
965  * @slot: ACPI PHP slot
966  */
967 int acpiphp_enable_slot(struct acpiphp_slot *slot)
968 {
969         pci_lock_rescan_remove();
970
971         if (slot->flags & SLOT_IS_GOING_AWAY)
972                 return -ENODEV;
973
974         /* configure all functions */
975         if (!(slot->flags & SLOT_ENABLED))
976                 enable_slot(slot);
977
978         pci_unlock_rescan_remove();
979         return 0;
980 }
981
982 /**
983  * acpiphp_disable_and_eject_slot - power off and eject slot
984  * @slot: ACPI PHP slot
985  */
986 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
987 {
988         struct acpiphp_func *func;
989
990         if (slot->flags & SLOT_IS_GOING_AWAY)
991                 return -ENODEV;
992
993         /* unconfigure all functions */
994         disable_slot(slot);
995
996         list_for_each_entry(func, &slot->funcs, sibling)
997                 if (func->flags & FUNC_HAS_EJ0) {
998                         acpi_handle handle = func_to_handle(func);
999
1000                         if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1001                                 acpi_handle_err(handle, "_EJ0 failed\n");
1002
1003                         break;
1004                 }
1005
1006         return 0;
1007 }
1008
1009 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1010 {
1011         int ret;
1012
1013         pci_lock_rescan_remove();
1014         ret = acpiphp_disable_and_eject_slot(slot);
1015         pci_unlock_rescan_remove();
1016         return ret;
1017 }
1018
1019 /*
1020  * slot enabled:  1
1021  * slot disabled: 0
1022  */
1023 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1024 {
1025         return (slot->flags & SLOT_ENABLED);
1026 }
1027
1028 /*
1029  * latch   open:  1
1030  * latch closed:  0
1031  */
1032 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1033 {
1034         return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1035 }
1036
1037 /*
1038  * adapter presence : 1
1039  *          absence : 0
1040  */
1041 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1042 {
1043         return !!get_slot_status(slot);
1044 }