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 static DEFINE_MUTEX(acpiphp_context_lock);
62 
63 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
64 static void acpiphp_sanitize_bus(struct pci_bus *bus);
65 static void acpiphp_set_hpp_values(struct pci_bus *bus);
66 static void hotplug_event(acpi_handle handle, u32 type, void *data);
67 static void free_bridge(struct kref *kref);
68 
69 static void acpiphp_context_handler(acpi_handle handle, void *context)
70 {
71 	/* Intentionally empty. */
72 }
73 
74 /**
75  * acpiphp_init_context - Create hotplug context and grab a reference to it.
76  * @adev: ACPI device object to create the context for.
77  *
78  * Call under acpiphp_context_lock.
79  */
80 static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
81 {
82 	struct acpiphp_context *context;
83 	acpi_status status;
84 
85 	context = kzalloc(sizeof(*context), GFP_KERNEL);
86 	if (!context)
87 		return NULL;
88 
89 	context->adev = adev;
90 	context->refcount = 1;
91 	status = acpi_attach_data(adev->handle, acpiphp_context_handler, context);
92 	if (ACPI_FAILURE(status)) {
93 		kfree(context);
94 		return NULL;
95 	}
96 	return context;
97 }
98 
99 /**
100  * acpiphp_get_context - Get hotplug context and grab a reference to it.
101  * @handle: ACPI object handle to get the context for.
102  *
103  * Call under acpiphp_context_lock.
104  */
105 static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
106 {
107 	struct acpiphp_context *context = NULL;
108 	acpi_status status;
109 	void *data;
110 
111 	status = acpi_get_data(handle, acpiphp_context_handler, &data);
112 	if (ACPI_SUCCESS(status)) {
113 		context = data;
114 		context->refcount++;
115 	}
116 	return context;
117 }
118 
119 /**
120  * acpiphp_put_context - Drop a reference to ACPI hotplug context.
121  * @context: ACPI hotplug context to drop a reference to.
122  *
123  * The context object is removed if there are no more references to it.
124  *
125  * Call under acpiphp_context_lock.
126  */
127 static void acpiphp_put_context(struct acpiphp_context *context)
128 {
129 	if (--context->refcount)
130 		return;
131 
132 	WARN_ON(context->bridge);
133 	acpi_detach_data(context->adev->handle, acpiphp_context_handler);
134 	kfree(context);
135 }
136 
137 static inline void get_bridge(struct acpiphp_bridge *bridge)
138 {
139 	kref_get(&bridge->ref);
140 }
141 
142 static inline void put_bridge(struct acpiphp_bridge *bridge)
143 {
144 	kref_put(&bridge->ref, free_bridge);
145 }
146 
147 static void free_bridge(struct kref *kref)
148 {
149 	struct acpiphp_context *context;
150 	struct acpiphp_bridge *bridge;
151 	struct acpiphp_slot *slot, *next;
152 	struct acpiphp_func *func, *tmp;
153 
154 	mutex_lock(&acpiphp_context_lock);
155 
156 	bridge = container_of(kref, struct acpiphp_bridge, ref);
157 
158 	list_for_each_entry_safe(slot, next, &bridge->slots, node) {
159 		list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
160 			acpiphp_put_context(func_to_context(func));
161 
162 		kfree(slot);
163 	}
164 
165 	context = bridge->context;
166 	/* Root bridges will not have hotplug context. */
167 	if (context) {
168 		/* Release the reference taken by acpiphp_enumerate_slots(). */
169 		put_bridge(context->func.parent);
170 		context->bridge = NULL;
171 		acpiphp_put_context(context);
172 	}
173 
174 	put_device(&bridge->pci_bus->dev);
175 	pci_dev_put(bridge->pci_dev);
176 	kfree(bridge);
177 
178 	mutex_unlock(&acpiphp_context_lock);
179 }
180 
181 /*
182  * the _DCK method can do funny things... and sometimes not
183  * hah-hah funny.
184  *
185  * TBD - figure out a way to only call fixups for
186  * systems that require them.
187  */
188 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
189 {
190 	struct acpiphp_context *context = data;
191 	struct pci_bus *bus = context->func.slot->bus;
192 	u32 buses;
193 
194 	if (!bus->self)
195 		return;
196 
197 	/* fixup bad _DCK function that rewrites
198 	 * secondary bridge on slot
199 	 */
200 	pci_read_config_dword(bus->self,
201 			PCI_PRIMARY_BUS,
202 			&buses);
203 
204 	if (((buses >> 8) & 0xff) != bus->busn_res.start) {
205 		buses = (buses & 0xff000000)
206 			| ((unsigned int)(bus->primary)     <<  0)
207 			| ((unsigned int)(bus->busn_res.start)   <<  8)
208 			| ((unsigned int)(bus->busn_res.end) << 16);
209 		pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
210 	}
211 }
212 
213 static void dock_event(acpi_handle handle, u32 type, void *data)
214 {
215 	struct acpiphp_context *context;
216 
217 	mutex_lock(&acpiphp_context_lock);
218 	context = acpiphp_get_context(handle);
219 	if (!context || WARN_ON(context->adev->handle != handle)
220 	    || context->func.parent->is_going_away) {
221 		mutex_unlock(&acpiphp_context_lock);
222 		return;
223 	}
224 	get_bridge(context->func.parent);
225 	acpiphp_put_context(context);
226 	mutex_unlock(&acpiphp_context_lock);
227 
228 	hotplug_event(handle, type, data);
229 
230 	put_bridge(context->func.parent);
231 }
232 
233 static const struct acpi_dock_ops acpiphp_dock_ops = {
234 	.fixup = post_dock_fixups,
235 	.handler = dock_event,
236 };
237 
238 /* Check whether the PCI device is managed by native PCIe hotplug driver */
239 static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
240 {
241 	u32 reg32;
242 	acpi_handle tmp;
243 	struct acpi_pci_root *root;
244 
245 	/* Check whether the PCIe port supports native PCIe hotplug */
246 	if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
247 		return false;
248 	if (!(reg32 & PCI_EXP_SLTCAP_HPC))
249 		return false;
250 
251 	/*
252 	 * Check whether native PCIe hotplug has been enabled for
253 	 * this PCIe hierarchy.
254 	 */
255 	tmp = acpi_find_root_bridge_handle(pdev);
256 	if (!tmp)
257 		return false;
258 	root = acpi_pci_find_root(tmp);
259 	if (!root)
260 		return false;
261 	if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
262 		return false;
263 
264 	return true;
265 }
266 
267 static void acpiphp_dock_init(void *data)
268 {
269 	struct acpiphp_context *context = data;
270 
271 	get_bridge(context->func.parent);
272 }
273 
274 static void acpiphp_dock_release(void *data)
275 {
276 	struct acpiphp_context *context = data;
277 
278 	put_bridge(context->func.parent);
279 }
280 
281 /* callback routine to register each ACPI PCI slot object */
282 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
283 				 void **rv)
284 {
285 	struct acpiphp_bridge *bridge = data;
286 	struct acpiphp_context *context;
287 	struct acpi_device *adev;
288 	struct acpiphp_slot *slot;
289 	struct acpiphp_func *newfunc;
290 	acpi_status status = AE_OK;
291 	unsigned long long adr;
292 	int device, function;
293 	struct pci_bus *pbus = bridge->pci_bus;
294 	struct pci_dev *pdev = bridge->pci_dev;
295 	u32 val;
296 
297 	if (pdev && device_is_managed_by_native_pciehp(pdev))
298 		return AE_OK;
299 
300 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
301 	if (ACPI_FAILURE(status)) {
302 		if (status != AE_NOT_FOUND)
303 			acpi_handle_warn(handle,
304 				"can't evaluate _ADR (%#x)\n", status);
305 		return AE_OK;
306 	}
307 	if (acpi_bus_get_device(handle, &adev))
308 		return AE_OK;
309 
310 	device = (adr >> 16) & 0xffff;
311 	function = adr & 0xffff;
312 
313 	mutex_lock(&acpiphp_context_lock);
314 	context = acpiphp_init_context(adev);
315 	if (!context) {
316 		mutex_unlock(&acpiphp_context_lock);
317 		acpi_handle_err(handle, "No hotplug context\n");
318 		return AE_NOT_EXIST;
319 	}
320 	newfunc = &context->func;
321 	newfunc->function = function;
322 	newfunc->parent = bridge;
323 	mutex_unlock(&acpiphp_context_lock);
324 
325 	if (acpi_has_method(handle, "_EJ0"))
326 		newfunc->flags = FUNC_HAS_EJ0;
327 
328 	if (acpi_has_method(handle, "_STA"))
329 		newfunc->flags |= FUNC_HAS_STA;
330 
331 	if (acpi_has_method(handle, "_DCK"))
332 		newfunc->flags |= FUNC_HAS_DCK;
333 
334 	/* search for objects that share the same slot */
335 	list_for_each_entry(slot, &bridge->slots, node)
336 		if (slot->device == device)
337 			goto slot_found;
338 
339 	slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
340 	if (!slot) {
341 		mutex_lock(&acpiphp_context_lock);
342 		acpiphp_put_context(context);
343 		mutex_unlock(&acpiphp_context_lock);
344 		return AE_NO_MEMORY;
345 	}
346 
347 	slot->bus = bridge->pci_bus;
348 	slot->device = device;
349 	INIT_LIST_HEAD(&slot->funcs);
350 	mutex_init(&slot->crit_sect);
351 
352 	list_add_tail(&slot->node, &bridge->slots);
353 
354 	/* Register slots for ejectable functions only. */
355 	if (acpi_pci_check_ejectable(pbus, handle)  || is_dock_device(handle)) {
356 		unsigned long long sun;
357 		int retval;
358 
359 		bridge->nr_slots++;
360 		status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
361 		if (ACPI_FAILURE(status))
362 			sun = bridge->nr_slots;
363 
364 		pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
365 		    sun, pci_domain_nr(pbus), pbus->number, device);
366 
367 		retval = acpiphp_register_hotplug_slot(slot, sun);
368 		if (retval) {
369 			slot->slot = NULL;
370 			bridge->nr_slots--;
371 			if (retval == -EBUSY)
372 				pr_warn("Slot %llu already registered by another "
373 					"hotplug driver\n", sun);
374 			else
375 				pr_warn("acpiphp_register_hotplug_slot failed "
376 					"(err code = 0x%x)\n", retval);
377 		}
378 		/* Even if the slot registration fails, we can still use it. */
379 	}
380 
381  slot_found:
382 	newfunc->slot = slot;
383 	list_add_tail(&newfunc->sibling, &slot->funcs);
384 
385 	if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
386 				       &val, 60*1000))
387 		slot->flags |= SLOT_ENABLED;
388 
389 	if (is_dock_device(handle)) {
390 		/* we don't want to call this device's _EJ0
391 		 * because we want the dock notify handler
392 		 * to call it after it calls _DCK
393 		 */
394 		newfunc->flags &= ~FUNC_HAS_EJ0;
395 		if (register_hotplug_dock_device(handle,
396 			&acpiphp_dock_ops, context,
397 			acpiphp_dock_init, acpiphp_dock_release))
398 			pr_debug("failed to register dock device\n");
399 	}
400 
401 	/* install notify handler */
402 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
403 		status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
404 						     handle_hotplug_event,
405 						     context);
406 		if (ACPI_FAILURE(status))
407 			acpi_handle_err(handle,
408 					"failed to install notify handler\n");
409 	}
410 
411 	return AE_OK;
412 }
413 
414 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
415 {
416 	struct acpiphp_context *context;
417 	struct acpiphp_bridge *bridge = NULL;
418 
419 	mutex_lock(&acpiphp_context_lock);
420 	context = acpiphp_get_context(handle);
421 	if (context) {
422 		bridge = context->bridge;
423 		if (bridge)
424 			get_bridge(bridge);
425 
426 		acpiphp_put_context(context);
427 	}
428 	mutex_unlock(&acpiphp_context_lock);
429 	return bridge;
430 }
431 
432 static void cleanup_bridge(struct acpiphp_bridge *bridge)
433 {
434 	struct acpiphp_slot *slot;
435 	struct acpiphp_func *func;
436 	acpi_status status;
437 
438 	list_for_each_entry(slot, &bridge->slots, node) {
439 		list_for_each_entry(func, &slot->funcs, sibling) {
440 			acpi_handle handle = func_to_handle(func);
441 
442 			if (is_dock_device(handle))
443 				unregister_hotplug_dock_device(handle);
444 
445 			if (!(func->flags & FUNC_HAS_DCK)) {
446 				status = acpi_remove_notify_handler(handle,
447 							ACPI_SYSTEM_NOTIFY,
448 							handle_hotplug_event);
449 				if (ACPI_FAILURE(status))
450 					pr_err("failed to remove notify handler\n");
451 			}
452 		}
453 		slot->flags |= SLOT_IS_GOING_AWAY;
454 		if (slot->slot)
455 			acpiphp_unregister_hotplug_slot(slot);
456 	}
457 
458 	mutex_lock(&bridge_mutex);
459 	list_del(&bridge->list);
460 	mutex_unlock(&bridge_mutex);
461 
462 	mutex_lock(&acpiphp_context_lock);
463 	bridge->is_going_away = true;
464 	mutex_unlock(&acpiphp_context_lock);
465 }
466 
467 /**
468  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
469  * @bus: bus to start search with
470  */
471 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
472 {
473 	struct list_head *tmp;
474 	unsigned char max, n;
475 
476 	/*
477 	 * pci_bus_max_busnr will return the highest
478 	 * reserved busnr for all these children.
479 	 * that is equivalent to the bus->subordinate
480 	 * value.  We don't want to use the parent's
481 	 * bus->subordinate value because it could have
482 	 * padding in it.
483 	 */
484 	max = bus->busn_res.start;
485 
486 	list_for_each(tmp, &bus->children) {
487 		n = pci_bus_max_busnr(pci_bus_b(tmp));
488 		if (n > max)
489 			max = n;
490 	}
491 	return max;
492 }
493 
494 /**
495  * acpiphp_bus_add - Scan ACPI namespace subtree.
496  * @handle: ACPI object handle to start the scan from.
497  */
498 static void acpiphp_bus_add(acpi_handle handle)
499 {
500 	struct acpi_device *adev = NULL;
501 
502 	acpi_bus_scan(handle);
503 	acpi_bus_get_device(handle, &adev);
504 	if (acpi_device_enumerated(adev))
505 		acpi_device_set_power(adev, ACPI_STATE_D0);
506 }
507 
508 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
509 {
510 	struct acpiphp_func *func;
511 	union acpi_object params[2];
512 	struct acpi_object_list arg_list;
513 
514 	list_for_each_entry(func, &slot->funcs, sibling) {
515 		arg_list.count = 2;
516 		arg_list.pointer = params;
517 		params[0].type = ACPI_TYPE_INTEGER;
518 		params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
519 		params[1].type = ACPI_TYPE_INTEGER;
520 		params[1].integer.value = 1;
521 		/* _REG is optional, we don't care about if there is failure */
522 		acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
523 				     NULL);
524 	}
525 }
526 
527 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
528 {
529 	struct acpiphp_func *func;
530 
531 	/* quirk, or pcie could set it already */
532 	if (dev->is_hotplug_bridge)
533 		return;
534 
535 	list_for_each_entry(func, &slot->funcs, sibling) {
536 		if (PCI_FUNC(dev->devfn) == func->function) {
537 			dev->is_hotplug_bridge = 1;
538 			break;
539 		}
540 	}
541 }
542 
543 static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
544 {
545 	struct acpiphp_func *func;
546 
547 	list_for_each_entry(func, &slot->funcs, sibling)
548 		acpiphp_bus_add(func_to_handle(func));
549 
550 	return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
551 }
552 
553 /**
554  * enable_slot - enable, configure a slot
555  * @slot: slot to be enabled
556  *
557  * This function should be called per *physical slot*,
558  * not per each slot object in ACPI namespace.
559  */
560 static void __ref enable_slot(struct acpiphp_slot *slot)
561 {
562 	struct pci_dev *dev;
563 	struct pci_bus *bus = slot->bus;
564 	struct acpiphp_func *func;
565 	int max, pass;
566 	LIST_HEAD(add_list);
567 
568 	acpiphp_rescan_slot(slot);
569 	max = acpiphp_max_busnr(bus);
570 	for (pass = 0; pass < 2; pass++) {
571 		list_for_each_entry(dev, &bus->devices, bus_list) {
572 			if (PCI_SLOT(dev->devfn) != slot->device)
573 				continue;
574 
575 			if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
576 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
577 				max = pci_scan_bridge(bus, dev, max, pass);
578 				if (pass && dev->subordinate) {
579 					check_hotplug_bridge(slot, dev);
580 					pcibios_resource_survey_bus(dev->subordinate);
581 					__pci_bus_size_bridges(dev->subordinate,
582 							       &add_list);
583 				}
584 			}
585 		}
586 	}
587 	__pci_bus_assign_resources(bus, &add_list, NULL);
588 
589 	acpiphp_sanitize_bus(bus);
590 	acpiphp_set_hpp_values(bus);
591 	acpiphp_set_acpi_region(slot);
592 
593 	list_for_each_entry(dev, &bus->devices, bus_list) {
594 		/* Assume that newly added devices are powered on already. */
595 		if (!dev->is_added)
596 			dev->current_state = PCI_D0;
597 	}
598 
599 	pci_bus_add_devices(bus);
600 
601 	slot->flags |= SLOT_ENABLED;
602 	list_for_each_entry(func, &slot->funcs, sibling) {
603 		dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
604 						  func->function));
605 		if (!dev) {
606 			/* Do not set SLOT_ENABLED flag if some funcs
607 			   are not added. */
608 			slot->flags &= (~SLOT_ENABLED);
609 			continue;
610 		}
611 	}
612 }
613 
614 /**
615  * disable_slot - disable a slot
616  * @slot: ACPI PHP slot
617  */
618 static void disable_slot(struct acpiphp_slot *slot)
619 {
620 	struct pci_bus *bus = slot->bus;
621 	struct pci_dev *dev, *prev;
622 	struct acpiphp_func *func;
623 
624 	/*
625 	 * enable_slot() enumerates all functions in this device via
626 	 * pci_scan_slot(), whether they have associated ACPI hotplug
627 	 * methods (_EJ0, etc.) or not.  Therefore, we remove all functions
628 	 * here.
629 	 */
630 	list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
631 		if (PCI_SLOT(dev->devfn) == slot->device)
632 			pci_stop_and_remove_bus_device(dev);
633 
634 	list_for_each_entry(func, &slot->funcs, sibling)
635 		acpi_bus_trim(func_to_acpi_device(func));
636 
637 	slot->flags &= (~SLOT_ENABLED);
638 }
639 
640 static bool acpiphp_no_hotplug(struct acpi_device *adev)
641 {
642 	return adev && adev->flags.no_hotplug;
643 }
644 
645 static bool slot_no_hotplug(struct acpiphp_slot *slot)
646 {
647 	struct acpiphp_func *func;
648 
649 	list_for_each_entry(func, &slot->funcs, sibling)
650 		if (acpiphp_no_hotplug(func_to_acpi_device(func)))
651 			return true;
652 
653 	return false;
654 }
655 
656 /**
657  * get_slot_status - get ACPI slot status
658  * @slot: ACPI PHP slot
659  *
660  * If a slot has _STA for each function and if any one of them
661  * returned non-zero status, return it.
662  *
663  * If a slot doesn't have _STA and if any one of its functions'
664  * configuration space is configured, return 0x0f as a _STA.
665  *
666  * Otherwise return 0.
667  */
668 static unsigned int get_slot_status(struct acpiphp_slot *slot)
669 {
670 	unsigned long long sta = 0;
671 	struct acpiphp_func *func;
672 
673 	list_for_each_entry(func, &slot->funcs, sibling) {
674 		if (func->flags & FUNC_HAS_STA) {
675 			acpi_status status;
676 
677 			status = acpi_evaluate_integer(func_to_handle(func),
678 						       "_STA", NULL, &sta);
679 			if (ACPI_SUCCESS(status) && sta)
680 				break;
681 		} else {
682 			u32 dvid;
683 
684 			pci_bus_read_config_dword(slot->bus,
685 						  PCI_DEVFN(slot->device,
686 							    func->function),
687 						  PCI_VENDOR_ID, &dvid);
688 			if (dvid != 0xffffffff) {
689 				sta = ACPI_STA_ALL;
690 				break;
691 			}
692 		}
693 	}
694 
695 	return (unsigned int)sta;
696 }
697 
698 /**
699  * trim_stale_devices - remove PCI devices that are not responding.
700  * @dev: PCI device to start walking the hierarchy from.
701  */
702 static void trim_stale_devices(struct pci_dev *dev)
703 {
704 	struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
705 	struct pci_bus *bus = dev->subordinate;
706 	bool alive = false;
707 
708 	if (adev) {
709 		acpi_status status;
710 		unsigned long long sta;
711 
712 		status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
713 		alive = (ACPI_SUCCESS(status) && sta == ACPI_STA_ALL)
714 			|| acpiphp_no_hotplug(adev);
715 	}
716 	if (!alive) {
717 		u32 v;
718 
719 		/* Check if the device responds. */
720 		alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
721 	}
722 	if (!alive) {
723 		pci_stop_and_remove_bus_device(dev);
724 		if (adev)
725 			acpi_bus_trim(adev);
726 	} else if (bus) {
727 		struct pci_dev *child, *tmp;
728 
729 		/* The device is a bridge. so check the bus below it. */
730 		pm_runtime_get_sync(&dev->dev);
731 		list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
732 			trim_stale_devices(child);
733 
734 		pm_runtime_put(&dev->dev);
735 	}
736 }
737 
738 /**
739  * acpiphp_check_bridge - re-enumerate devices
740  * @bridge: where to begin re-enumeration
741  *
742  * Iterate over all slots under this bridge and make sure that if a
743  * card is present they are enabled, and if not they are disabled.
744  */
745 static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
746 {
747 	struct acpiphp_slot *slot;
748 
749 	/* Bail out if the bridge is going away. */
750 	if (bridge->is_going_away)
751 		return;
752 
753 	list_for_each_entry(slot, &bridge->slots, node) {
754 		struct pci_bus *bus = slot->bus;
755 		struct pci_dev *dev, *tmp;
756 
757 		mutex_lock(&slot->crit_sect);
758 		if (slot_no_hotplug(slot)) {
759 			; /* do nothing */
760 		} else if (get_slot_status(slot) == ACPI_STA_ALL) {
761 			/* remove stale devices if any */
762 			list_for_each_entry_safe_reverse(dev, tmp,
763 							 &bus->devices, bus_list)
764 				if (PCI_SLOT(dev->devfn) == slot->device)
765 					trim_stale_devices(dev);
766 
767 			/* configure all functions */
768 			enable_slot(slot);
769 		} else {
770 			disable_slot(slot);
771 		}
772 		mutex_unlock(&slot->crit_sect);
773 	}
774 }
775 
776 static void acpiphp_set_hpp_values(struct pci_bus *bus)
777 {
778 	struct pci_dev *dev;
779 
780 	list_for_each_entry(dev, &bus->devices, bus_list)
781 		pci_configure_slot(dev);
782 }
783 
784 /*
785  * Remove devices for which we could not assign resources, call
786  * arch specific code to fix-up the bus
787  */
788 static void acpiphp_sanitize_bus(struct pci_bus *bus)
789 {
790 	struct pci_dev *dev, *tmp;
791 	int i;
792 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
793 
794 	list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
795 		for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
796 			struct resource *res = &dev->resource[i];
797 			if ((res->flags & type_mask) && !res->start &&
798 					res->end) {
799 				/* Could not assign a required resources
800 				 * for this device, remove it */
801 				pci_stop_and_remove_bus_device(dev);
802 				break;
803 			}
804 		}
805 	}
806 }
807 
808 /*
809  * ACPI event handlers
810  */
811 
812 void acpiphp_check_host_bridge(acpi_handle handle)
813 {
814 	struct acpiphp_bridge *bridge;
815 
816 	bridge = acpiphp_handle_to_bridge(handle);
817 	if (bridge) {
818 		pci_lock_rescan_remove();
819 
820 		acpiphp_check_bridge(bridge);
821 
822 		pci_unlock_rescan_remove();
823 		put_bridge(bridge);
824 	}
825 }
826 
827 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
828 
829 static void hotplug_event(acpi_handle handle, u32 type, void *data)
830 {
831 	struct acpiphp_context *context = data;
832 	struct acpiphp_func *func = &context->func;
833 	struct acpiphp_bridge *bridge;
834 	char objname[64];
835 	struct acpi_buffer buffer = { .length = sizeof(objname),
836 				      .pointer = objname };
837 
838 	mutex_lock(&acpiphp_context_lock);
839 	bridge = context->bridge;
840 	if (bridge)
841 		get_bridge(bridge);
842 
843 	mutex_unlock(&acpiphp_context_lock);
844 
845 	pci_lock_rescan_remove();
846 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
847 
848 	switch (type) {
849 	case ACPI_NOTIFY_BUS_CHECK:
850 		/* bus re-enumerate */
851 		pr_debug("%s: Bus check notify on %s\n", __func__, objname);
852 		pr_debug("%s: re-enumerating slots under %s\n",
853 			 __func__, objname);
854 		if (bridge) {
855 			acpiphp_check_bridge(bridge);
856 		} else {
857 			struct acpiphp_slot *slot = func->slot;
858 
859 			if (slot->flags & SLOT_IS_GOING_AWAY)
860 				break;
861 
862 			mutex_lock(&slot->crit_sect);
863 			enable_slot(slot);
864 			mutex_unlock(&slot->crit_sect);
865 		}
866 		break;
867 
868 	case ACPI_NOTIFY_DEVICE_CHECK:
869 		/* device check */
870 		pr_debug("%s: Device check notify on %s\n", __func__, objname);
871 		if (bridge) {
872 			acpiphp_check_bridge(bridge);
873 		} else {
874 			struct acpiphp_slot *slot = func->slot;
875 			int ret;
876 
877 			if (slot->flags & SLOT_IS_GOING_AWAY)
878 				break;
879 
880 			/*
881 			 * Check if anything has changed in the slot and rescan
882 			 * from the parent if that's the case.
883 			 */
884 			mutex_lock(&slot->crit_sect);
885 			ret = acpiphp_rescan_slot(slot);
886 			mutex_unlock(&slot->crit_sect);
887 			if (ret)
888 				acpiphp_check_bridge(func->parent);
889 		}
890 		break;
891 
892 	case ACPI_NOTIFY_EJECT_REQUEST:
893 		/* request device eject */
894 		pr_debug("%s: Device eject notify on %s\n", __func__, objname);
895 		acpiphp_disable_and_eject_slot(func->slot);
896 		break;
897 	}
898 
899 	pci_unlock_rescan_remove();
900 	if (bridge)
901 		put_bridge(bridge);
902 }
903 
904 static void hotplug_event_work(void *data, u32 type)
905 {
906 	struct acpiphp_context *context = data;
907 	acpi_handle handle = context->adev->handle;
908 
909 	acpi_scan_lock_acquire();
910 
911 	hotplug_event(handle, type, context);
912 
913 	acpi_scan_lock_release();
914 	acpi_evaluate_hotplug_ost(handle, type, ACPI_OST_SC_SUCCESS, NULL);
915 	put_bridge(context->func.parent);
916 }
917 
918 /**
919  * handle_hotplug_event - handle ACPI hotplug event
920  * @handle: Notify()'ed acpi_handle
921  * @type: Notify code
922  * @data: pointer to acpiphp_context structure
923  *
924  * Handles ACPI event notification on slots.
925  */
926 static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
927 {
928 	struct acpiphp_context *context;
929 	u32 ost_code = ACPI_OST_SC_SUCCESS;
930 	acpi_status status;
931 
932 	switch (type) {
933 	case ACPI_NOTIFY_BUS_CHECK:
934 	case ACPI_NOTIFY_DEVICE_CHECK:
935 		break;
936 	case ACPI_NOTIFY_EJECT_REQUEST:
937 		ost_code = ACPI_OST_SC_EJECT_IN_PROGRESS;
938 		acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
939 		break;
940 
941 	case ACPI_NOTIFY_DEVICE_WAKE:
942 		return;
943 
944 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
945 		acpi_handle_err(handle, "Device cannot be configured due "
946 				"to a frequency mismatch\n");
947 		goto out;
948 
949 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
950 		acpi_handle_err(handle, "Device cannot be configured due "
951 				"to a bus mode mismatch\n");
952 		goto out;
953 
954 	case ACPI_NOTIFY_POWER_FAULT:
955 		acpi_handle_err(handle, "Device has suffered a power fault\n");
956 		goto out;
957 
958 	default:
959 		acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
960 		ost_code = ACPI_OST_SC_UNRECOGNIZED_NOTIFY;
961 		goto out;
962 	}
963 
964 	mutex_lock(&acpiphp_context_lock);
965 	context = acpiphp_get_context(handle);
966 	if (!context || WARN_ON(context->adev->handle != handle)
967 	    || context->func.parent->is_going_away)
968 		goto err_out;
969 
970 	get_bridge(context->func.parent);
971 	acpiphp_put_context(context);
972 	status = acpi_hotplug_execute(hotplug_event_work, context, type);
973 	if (ACPI_SUCCESS(status)) {
974 		mutex_unlock(&acpiphp_context_lock);
975 		return;
976 	}
977 	put_bridge(context->func.parent);
978 
979  err_out:
980 	mutex_unlock(&acpiphp_context_lock);
981 	ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
982 
983  out:
984 	acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
985 }
986 
987 /**
988  * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
989  * @bus: PCI bus to enumerate the slots for.
990  *
991  * A "slot" is an object associated with a PCI device number.  All functions
992  * (PCI devices) with the same bus and device number belong to the same slot.
993  */
994 void acpiphp_enumerate_slots(struct pci_bus *bus)
995 {
996 	struct acpiphp_bridge *bridge;
997 	struct acpi_device *adev;
998 	acpi_handle handle;
999 	acpi_status status;
1000 
1001 	if (acpiphp_disabled)
1002 		return;
1003 
1004 	adev = ACPI_COMPANION(bus->bridge);
1005 	if (!adev)
1006 		return;
1007 
1008 	handle = adev->handle;
1009 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1010 	if (!bridge) {
1011 		acpi_handle_err(handle, "No memory for bridge object\n");
1012 		return;
1013 	}
1014 
1015 	INIT_LIST_HEAD(&bridge->slots);
1016 	kref_init(&bridge->ref);
1017 	bridge->pci_dev = pci_dev_get(bus->self);
1018 	bridge->pci_bus = bus;
1019 
1020 	/*
1021 	 * Grab a ref to the subordinate PCI bus in case the bus is
1022 	 * removed via PCI core logical hotplug. The ref pins the bus
1023 	 * (which we access during module unload).
1024 	 */
1025 	get_device(&bus->dev);
1026 
1027 	if (!pci_is_root_bus(bridge->pci_bus)) {
1028 		struct acpiphp_context *context;
1029 
1030 		/*
1031 		 * This bridge should have been registered as a hotplug function
1032 		 * under its parent, so the context should be there, unless the
1033 		 * parent is going to be handled by pciehp, in which case this
1034 		 * bridge is not interesting to us either.
1035 		 */
1036 		mutex_lock(&acpiphp_context_lock);
1037 		context = acpiphp_get_context(handle);
1038 		if (!context) {
1039 			mutex_unlock(&acpiphp_context_lock);
1040 			put_device(&bus->dev);
1041 			pci_dev_put(bridge->pci_dev);
1042 			kfree(bridge);
1043 			return;
1044 		}
1045 		bridge->context = context;
1046 		context->bridge = bridge;
1047 		/* Get a reference to the parent bridge. */
1048 		get_bridge(context->func.parent);
1049 		mutex_unlock(&acpiphp_context_lock);
1050 	}
1051 
1052 	/* must be added to the list prior to calling register_slot */
1053 	mutex_lock(&bridge_mutex);
1054 	list_add(&bridge->list, &bridge_list);
1055 	mutex_unlock(&bridge_mutex);
1056 
1057 	/* register all slot objects under this bridge */
1058 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1059 				     register_slot, NULL, bridge, NULL);
1060 	if (ACPI_FAILURE(status)) {
1061 		acpi_handle_err(handle, "failed to register slots\n");
1062 		cleanup_bridge(bridge);
1063 		put_bridge(bridge);
1064 	}
1065 }
1066 
1067 /**
1068  * acpiphp_remove_slots - Remove slot objects associated with a given bus.
1069  * @bus: PCI bus to remove the slot objects for.
1070  */
1071 void acpiphp_remove_slots(struct pci_bus *bus)
1072 {
1073 	struct acpiphp_bridge *bridge;
1074 
1075 	if (acpiphp_disabled)
1076 		return;
1077 
1078 	mutex_lock(&bridge_mutex);
1079 	list_for_each_entry(bridge, &bridge_list, list)
1080 		if (bridge->pci_bus == bus) {
1081 			mutex_unlock(&bridge_mutex);
1082 			cleanup_bridge(bridge);
1083 			put_bridge(bridge);
1084 			return;
1085 		}
1086 
1087 	mutex_unlock(&bridge_mutex);
1088 }
1089 
1090 /**
1091  * acpiphp_enable_slot - power on slot
1092  * @slot: ACPI PHP slot
1093  */
1094 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1095 {
1096 	pci_lock_rescan_remove();
1097 
1098 	if (slot->flags & SLOT_IS_GOING_AWAY)
1099 		return -ENODEV;
1100 
1101 	mutex_lock(&slot->crit_sect);
1102 	/* configure all functions */
1103 	if (!(slot->flags & SLOT_ENABLED))
1104 		enable_slot(slot);
1105 
1106 	mutex_unlock(&slot->crit_sect);
1107 
1108 	pci_unlock_rescan_remove();
1109 	return 0;
1110 }
1111 
1112 /**
1113  * acpiphp_disable_and_eject_slot - power off and eject slot
1114  * @slot: ACPI PHP slot
1115  */
1116 static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1117 {
1118 	struct acpiphp_func *func;
1119 
1120 	if (slot->flags & SLOT_IS_GOING_AWAY)
1121 		return -ENODEV;
1122 
1123 	mutex_lock(&slot->crit_sect);
1124 
1125 	/* unconfigure all functions */
1126 	disable_slot(slot);
1127 
1128 	list_for_each_entry(func, &slot->funcs, sibling)
1129 		if (func->flags & FUNC_HAS_EJ0) {
1130 			acpi_handle handle = func_to_handle(func);
1131 
1132 			if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1133 				acpi_handle_err(handle, "_EJ0 failed\n");
1134 
1135 			break;
1136 		}
1137 
1138 	mutex_unlock(&slot->crit_sect);
1139 	return 0;
1140 }
1141 
1142 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1143 {
1144 	int ret;
1145 
1146 	pci_lock_rescan_remove();
1147 	ret = acpiphp_disable_and_eject_slot(slot);
1148 	pci_unlock_rescan_remove();
1149 	return ret;
1150 }
1151 
1152 /*
1153  * slot enabled:  1
1154  * slot disabled: 0
1155  */
1156 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1157 {
1158 	return (slot->flags & SLOT_ENABLED);
1159 }
1160 
1161 /*
1162  * latch   open:  1
1163  * latch closed:  0
1164  */
1165 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1166 {
1167 	return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1168 }
1169 
1170 /*
1171  * adapter presence : 1
1172  *          absence : 0
1173  */
1174 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1175 {
1176 	return !!get_slot_status(slot);
1177 }
1178