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