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