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