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 acpiphp_no_hotplug(struct acpi_device *adev)
564 {
565 	return adev && adev->flags.no_hotplug;
566 }
567 
568 static bool slot_no_hotplug(struct acpiphp_slot *slot)
569 {
570 	struct acpiphp_func *func;
571 
572 	list_for_each_entry(func, &slot->funcs, sibling)
573 		if (acpiphp_no_hotplug(func_to_acpi_device(func)))
574 			return true;
575 
576 	return false;
577 }
578 
579 /**
580  * get_slot_status - get ACPI slot status
581  * @slot: ACPI PHP slot
582  *
583  * If a slot has _STA for each function and if any one of them
584  * returned non-zero status, return it.
585  *
586  * If a slot doesn't have _STA and if any one of its functions'
587  * configuration space is configured, return 0x0f as a _STA.
588  *
589  * Otherwise return 0.
590  */
591 static unsigned int get_slot_status(struct acpiphp_slot *slot)
592 {
593 	unsigned long long sta = 0;
594 	struct acpiphp_func *func;
595 
596 	list_for_each_entry(func, &slot->funcs, sibling) {
597 		if (func->flags & FUNC_HAS_STA) {
598 			acpi_status status;
599 
600 			status = acpi_evaluate_integer(func_to_handle(func),
601 						       "_STA", NULL, &sta);
602 			if (ACPI_SUCCESS(status) && sta)
603 				break;
604 		} else {
605 			u32 dvid;
606 
607 			pci_bus_read_config_dword(slot->bus,
608 						  PCI_DEVFN(slot->device,
609 							    func->function),
610 						  PCI_VENDOR_ID, &dvid);
611 			if (dvid != 0xffffffff) {
612 				sta = ACPI_STA_ALL;
613 				break;
614 			}
615 		}
616 	}
617 
618 	return (unsigned int)sta;
619 }
620 
621 static inline bool device_status_valid(unsigned int sta)
622 {
623 	/*
624 	 * ACPI spec says that _STA may return bit 0 clear with bit 3 set
625 	 * if the device is valid but does not require a device driver to be
626 	 * loaded (Section 6.3.7 of ACPI 5.0A).
627 	 */
628 	unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
629 	return (sta & mask) == mask;
630 }
631 
632 /**
633  * trim_stale_devices - remove PCI devices that are not responding.
634  * @dev: PCI device to start walking the hierarchy from.
635  */
636 static void trim_stale_devices(struct pci_dev *dev)
637 {
638 	struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
639 	struct pci_bus *bus = dev->subordinate;
640 	bool alive = false;
641 
642 	if (adev) {
643 		acpi_status status;
644 		unsigned long long sta;
645 
646 		status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
647 		alive = (ACPI_SUCCESS(status) && device_status_valid(sta))
648 			|| acpiphp_no_hotplug(adev);
649 	}
650 	if (!alive)
651 		alive = pci_device_is_present(dev);
652 
653 	if (!alive) {
654 		pci_stop_and_remove_bus_device(dev);
655 		if (adev)
656 			acpi_bus_trim(adev);
657 	} else if (bus) {
658 		struct pci_dev *child, *tmp;
659 
660 		/* The device is a bridge. so check the bus below it. */
661 		pm_runtime_get_sync(&dev->dev);
662 		list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
663 			trim_stale_devices(child);
664 
665 		pm_runtime_put(&dev->dev);
666 	}
667 }
668 
669 /**
670  * acpiphp_check_bridge - re-enumerate devices
671  * @bridge: where to begin re-enumeration
672  *
673  * Iterate over all slots under this bridge and make sure that if a
674  * card is present they are enabled, and if not they are disabled.
675  */
676 static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
677 {
678 	struct acpiphp_slot *slot;
679 
680 	/* Bail out if the bridge is going away. */
681 	if (bridge->is_going_away)
682 		return;
683 
684 	list_for_each_entry(slot, &bridge->slots, node) {
685 		struct pci_bus *bus = slot->bus;
686 		struct pci_dev *dev, *tmp;
687 
688 		if (slot_no_hotplug(slot)) {
689 			; /* do nothing */
690 		} else if (device_status_valid(get_slot_status(slot))) {
691 			/* remove stale devices if any */
692 			list_for_each_entry_safe_reverse(dev, tmp,
693 							 &bus->devices, bus_list)
694 				if (PCI_SLOT(dev->devfn) == slot->device)
695 					trim_stale_devices(dev);
696 
697 			/* configure all functions */
698 			enable_slot(slot);
699 		} else {
700 			disable_slot(slot);
701 		}
702 	}
703 }
704 
705 static void acpiphp_set_hpp_values(struct pci_bus *bus)
706 {
707 	struct pci_dev *dev;
708 
709 	list_for_each_entry(dev, &bus->devices, bus_list)
710 		pci_configure_slot(dev);
711 }
712 
713 /*
714  * Remove devices for which we could not assign resources, call
715  * arch specific code to fix-up the bus
716  */
717 static void acpiphp_sanitize_bus(struct pci_bus *bus)
718 {
719 	struct pci_dev *dev, *tmp;
720 	int i;
721 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
722 
723 	list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
724 		for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
725 			struct resource *res = &dev->resource[i];
726 			if ((res->flags & type_mask) && !res->start &&
727 					res->end) {
728 				/* Could not assign a required resources
729 				 * for this device, remove it */
730 				pci_stop_and_remove_bus_device(dev);
731 				break;
732 			}
733 		}
734 	}
735 }
736 
737 /*
738  * ACPI event handlers
739  */
740 
741 void acpiphp_check_host_bridge(struct acpi_device *adev)
742 {
743 	struct acpiphp_bridge *bridge = NULL;
744 
745 	acpi_lock_hp_context();
746 	if (adev->hp) {
747 		bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
748 		if (bridge)
749 			get_bridge(bridge);
750 	}
751 	acpi_unlock_hp_context();
752 	if (bridge) {
753 		pci_lock_rescan_remove();
754 
755 		acpiphp_check_bridge(bridge);
756 
757 		pci_unlock_rescan_remove();
758 		put_bridge(bridge);
759 	}
760 }
761 
762 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
763 
764 static void hotplug_event(u32 type, struct acpiphp_context *context)
765 {
766 	acpi_handle handle = context->hp.self->handle;
767 	struct acpiphp_func *func = &context->func;
768 	struct acpiphp_slot *slot = func->slot;
769 	struct acpiphp_bridge *bridge;
770 
771 	acpi_lock_hp_context();
772 	bridge = context->bridge;
773 	if (bridge)
774 		get_bridge(bridge);
775 
776 	acpi_unlock_hp_context();
777 
778 	pci_lock_rescan_remove();
779 
780 	switch (type) {
781 	case ACPI_NOTIFY_BUS_CHECK:
782 		/* bus re-enumerate */
783 		acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
784 		if (bridge)
785 			acpiphp_check_bridge(bridge);
786 		else if (!(slot->flags & SLOT_IS_GOING_AWAY))
787 			enable_slot(slot);
788 
789 		break;
790 
791 	case ACPI_NOTIFY_DEVICE_CHECK:
792 		/* device check */
793 		acpi_handle_debug(handle, "Device check in %s()\n", __func__);
794 		if (bridge) {
795 			acpiphp_check_bridge(bridge);
796 		} else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
797 			/*
798 			 * Check if anything has changed in the slot and rescan
799 			 * from the parent if that's the case.
800 			 */
801 			if (acpiphp_rescan_slot(slot))
802 				acpiphp_check_bridge(func->parent);
803 		}
804 		break;
805 
806 	case ACPI_NOTIFY_EJECT_REQUEST:
807 		/* request device eject */
808 		acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
809 		acpiphp_disable_and_eject_slot(slot);
810 		break;
811 	}
812 
813 	pci_unlock_rescan_remove();
814 	if (bridge)
815 		put_bridge(bridge);
816 }
817 
818 static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
819 {
820 	struct acpiphp_context *context;
821 
822 	context = acpiphp_grab_context(adev);
823 	if (!context)
824 		return -ENODATA;
825 
826 	hotplug_event(type, context);
827 	acpiphp_let_context_go(context);
828 	return 0;
829 }
830 
831 /**
832  * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
833  * @bus: PCI bus to enumerate the slots for.
834  *
835  * A "slot" is an object associated with a PCI device number.  All functions
836  * (PCI devices) with the same bus and device number belong to the same slot.
837  */
838 void acpiphp_enumerate_slots(struct pci_bus *bus)
839 {
840 	struct acpiphp_bridge *bridge;
841 	struct acpi_device *adev;
842 	acpi_handle handle;
843 	acpi_status status;
844 
845 	if (acpiphp_disabled)
846 		return;
847 
848 	adev = ACPI_COMPANION(bus->bridge);
849 	if (!adev)
850 		return;
851 
852 	handle = adev->handle;
853 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
854 	if (!bridge) {
855 		acpi_handle_err(handle, "No memory for bridge object\n");
856 		return;
857 	}
858 
859 	INIT_LIST_HEAD(&bridge->slots);
860 	kref_init(&bridge->ref);
861 	bridge->pci_dev = pci_dev_get(bus->self);
862 	bridge->pci_bus = bus;
863 
864 	/*
865 	 * Grab a ref to the subordinate PCI bus in case the bus is
866 	 * removed via PCI core logical hotplug. The ref pins the bus
867 	 * (which we access during module unload).
868 	 */
869 	get_device(&bus->dev);
870 
871 	acpi_lock_hp_context();
872 	if (pci_is_root_bus(bridge->pci_bus)) {
873 		struct acpiphp_root_context *root_context;
874 
875 		root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
876 		if (!root_context)
877 			goto err;
878 
879 		root_context->root_bridge = bridge;
880 		acpi_set_hp_context(adev, &root_context->hp);
881 	} else {
882 		struct acpiphp_context *context;
883 
884 		/*
885 		 * This bridge should have been registered as a hotplug function
886 		 * under its parent, so the context should be there, unless the
887 		 * parent is going to be handled by pciehp, in which case this
888 		 * bridge is not interesting to us either.
889 		 */
890 		context = acpiphp_get_context(adev);
891 		if (!context)
892 			goto err;
893 
894 		bridge->context = context;
895 		context->bridge = bridge;
896 		/* Get a reference to the parent bridge. */
897 		get_bridge(context->func.parent);
898 	}
899 	acpi_unlock_hp_context();
900 
901 	/* Must be added to the list prior to calling acpiphp_add_context(). */
902 	mutex_lock(&bridge_mutex);
903 	list_add(&bridge->list, &bridge_list);
904 	mutex_unlock(&bridge_mutex);
905 
906 	/* register all slot objects under this bridge */
907 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
908 				     acpiphp_add_context, NULL, bridge, NULL);
909 	if (ACPI_FAILURE(status)) {
910 		acpi_handle_err(handle, "failed to register slots\n");
911 		cleanup_bridge(bridge);
912 		put_bridge(bridge);
913 	}
914 	return;
915 
916  err:
917 	acpi_unlock_hp_context();
918 	put_device(&bus->dev);
919 	pci_dev_put(bridge->pci_dev);
920 	kfree(bridge);
921 }
922 
923 static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
924 {
925 	if (pci_is_root_bus(bridge->pci_bus)) {
926 		struct acpiphp_root_context *root_context;
927 		struct acpi_device *adev;
928 
929 		acpi_lock_hp_context();
930 		adev = ACPI_COMPANION(bridge->pci_bus->bridge);
931 		root_context = to_acpiphp_root_context(adev->hp);
932 		adev->hp = NULL;
933 		acpi_unlock_hp_context();
934 		kfree(root_context);
935 	}
936 	cleanup_bridge(bridge);
937 	put_bridge(bridge);
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 			acpiphp_drop_bridge(bridge);
956 			return;
957 		}
958 
959 	mutex_unlock(&bridge_mutex);
960 }
961 
962 /**
963  * acpiphp_enable_slot - power on slot
964  * @slot: ACPI PHP slot
965  */
966 int acpiphp_enable_slot(struct acpiphp_slot *slot)
967 {
968 	pci_lock_rescan_remove();
969 
970 	if (slot->flags & SLOT_IS_GOING_AWAY)
971 		return -ENODEV;
972 
973 	/* configure all functions */
974 	if (!(slot->flags & SLOT_ENABLED))
975 		enable_slot(slot);
976 
977 	pci_unlock_rescan_remove();
978 	return 0;
979 }
980 
981 /**
982  * acpiphp_disable_and_eject_slot - power off and eject slot
983  * @slot: ACPI PHP slot
984  */
985 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
986 {
987 	struct acpiphp_func *func;
988 
989 	if (slot->flags & SLOT_IS_GOING_AWAY)
990 		return -ENODEV;
991 
992 	/* unconfigure all functions */
993 	disable_slot(slot);
994 
995 	list_for_each_entry(func, &slot->funcs, sibling)
996 		if (func->flags & FUNC_HAS_EJ0) {
997 			acpi_handle handle = func_to_handle(func);
998 
999 			if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1000 				acpi_handle_err(handle, "_EJ0 failed\n");
1001 
1002 			break;
1003 		}
1004 
1005 	return 0;
1006 }
1007 
1008 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1009 {
1010 	int ret;
1011 
1012 	/*
1013 	 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
1014 	 * acpiphp_disable_and_eject_slot() will be synchronized properly.
1015 	 */
1016 	acpi_scan_lock_acquire();
1017 	pci_lock_rescan_remove();
1018 	ret = acpiphp_disable_and_eject_slot(slot);
1019 	pci_unlock_rescan_remove();
1020 	acpi_scan_lock_release();
1021 	return ret;
1022 }
1023 
1024 /*
1025  * slot enabled:  1
1026  * slot disabled: 0
1027  */
1028 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1029 {
1030 	return (slot->flags & SLOT_ENABLED);
1031 }
1032 
1033 /*
1034  * latch   open:  1
1035  * latch closed:  0
1036  */
1037 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1038 {
1039 	return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1040 }
1041 
1042 /*
1043  * adapter presence : 1
1044  *          absence : 0
1045  */
1046 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1047 {
1048 	return !!get_slot_status(slot);
1049 }
1050