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