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