blob: 98be06ac2af24af2ac2157c1e039386713fd89d3 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
David Brazdil0f672f62019-12-10 10:32:29 +00008 * Copyright (C) 2003-2005 Matthew Wilcox (willy@infradead.org)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 * 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
44static LIST_HEAD(bridge_list);
45static DEFINE_MUTEX(bridge_mutex);
46
47static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
48static void acpiphp_post_dock_fixup(struct acpi_device *adev);
49static void acpiphp_sanitize_bus(struct pci_bus *bus);
50static void hotplug_event(u32 type, struct acpiphp_context *context);
51static 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 */
59static 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 */
80static 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 */
100static 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
110static inline void get_bridge(struct acpiphp_bridge *bridge)
111{
112 kref_get(&bridge->ref);
113}
114
115static inline void put_bridge(struct acpiphp_bridge *bridge)
116{
117 kref_put(&bridge->ref, free_bridge);
118}
119
120static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
121{
122 struct acpiphp_context *context;
123
124 acpi_lock_hp_context();
Olivier Deprez0e641232021-09-23 10:07:05 +0200125
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126 context = acpiphp_get_context(adev);
Olivier Deprez0e641232021-09-23 10:07:05 +0200127 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200135
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 get_bridge(context->func.parent);
137 acpiphp_put_context(context);
Olivier Deprez0e641232021-09-23 10:07:05 +0200138
139unlock:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140 acpi_unlock_hp_context();
141 return context;
142}
143
144static void acpiphp_let_context_go(struct acpiphp_context *context)
145{
146 put_bridge(context->func.parent);
147}
148
149static 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 */
189static 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 */
226static 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
338static 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 */
370static 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
393static 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
412static 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
428static 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
442static 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) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200460 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469 }
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 */
480static 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498 } 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 __pci_bus_size_bridges(dev->subordinate,
514 &add_list);
515 }
516 }
517 }
518 __pci_bus_assign_resources(bus, &add_list, NULL);
519 }
520
521 acpiphp_sanitize_bus(bus);
522 pcie_bus_configure_settings(bus);
523 acpiphp_set_acpi_region(slot);
524
525 list_for_each_entry(dev, &bus->devices, bus_list) {
526 /* Assume that newly added devices are powered on already. */
527 if (!pci_dev_is_added(dev))
528 dev->current_state = PCI_D0;
529 }
530
531 pci_bus_add_devices(bus);
532
533 slot->flags |= SLOT_ENABLED;
534 list_for_each_entry(func, &slot->funcs, sibling) {
535 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
536 func->function));
537 if (!dev) {
538 /* Do not set SLOT_ENABLED flag if some funcs
539 are not added. */
540 slot->flags &= ~SLOT_ENABLED;
541 continue;
542 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200543 pci_dev_put(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544 }
545}
546
547/**
548 * disable_slot - disable a slot
549 * @slot: ACPI PHP slot
550 */
551static void disable_slot(struct acpiphp_slot *slot)
552{
553 struct pci_bus *bus = slot->bus;
554 struct pci_dev *dev, *prev;
555 struct acpiphp_func *func;
556
557 /*
558 * enable_slot() enumerates all functions in this device via
559 * pci_scan_slot(), whether they have associated ACPI hotplug
560 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
561 * here.
562 */
563 list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
564 if (PCI_SLOT(dev->devfn) == slot->device)
565 pci_stop_and_remove_bus_device(dev);
566
567 list_for_each_entry(func, &slot->funcs, sibling)
568 acpi_bus_trim(func_to_acpi_device(func));
569
570 slot->flags &= ~SLOT_ENABLED;
571}
572
573static bool slot_no_hotplug(struct acpiphp_slot *slot)
574{
575 struct pci_bus *bus = slot->bus;
576 struct pci_dev *dev;
577
578 list_for_each_entry(dev, &bus->devices, bus_list) {
579 if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
580 return true;
581 }
582 return false;
583}
584
585/**
586 * get_slot_status - get ACPI slot status
587 * @slot: ACPI PHP slot
588 *
589 * If a slot has _STA for each function and if any one of them
590 * returned non-zero status, return it.
591 *
592 * If a slot doesn't have _STA and if any one of its functions'
593 * configuration space is configured, return 0x0f as a _STA.
594 *
595 * Otherwise return 0.
596 */
597static unsigned int get_slot_status(struct acpiphp_slot *slot)
598{
599 unsigned long long sta = 0;
600 struct acpiphp_func *func;
601 u32 dvid;
602
603 list_for_each_entry(func, &slot->funcs, sibling) {
604 if (func->flags & FUNC_HAS_STA) {
605 acpi_status status;
606
607 status = acpi_evaluate_integer(func_to_handle(func),
608 "_STA", NULL, &sta);
609 if (ACPI_SUCCESS(status) && sta)
610 break;
611 } else {
612 if (pci_bus_read_dev_vendor_id(slot->bus,
613 PCI_DEVFN(slot->device, func->function),
614 &dvid, 0)) {
615 sta = ACPI_STA_ALL;
616 break;
617 }
618 }
619 }
620
621 if (!sta) {
622 /*
623 * Check for the slot itself since it may be that the
624 * ACPI slot is a device below PCIe upstream port so in
625 * that case it may not even be reachable yet.
626 */
627 if (pci_bus_read_dev_vendor_id(slot->bus,
628 PCI_DEVFN(slot->device, 0), &dvid, 0)) {
629 sta = ACPI_STA_ALL;
630 }
631 }
632
633 return (unsigned int)sta;
634}
635
636static inline bool device_status_valid(unsigned int sta)
637{
638 /*
639 * ACPI spec says that _STA may return bit 0 clear with bit 3 set
640 * if the device is valid but does not require a device driver to be
641 * loaded (Section 6.3.7 of ACPI 5.0A).
642 */
643 unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
644 return (sta & mask) == mask;
645}
646
647/**
648 * trim_stale_devices - remove PCI devices that are not responding.
649 * @dev: PCI device to start walking the hierarchy from.
650 */
651static void trim_stale_devices(struct pci_dev *dev)
652{
653 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
654 struct pci_bus *bus = dev->subordinate;
655 bool alive = dev->ignore_hotplug;
656
657 if (adev) {
658 acpi_status status;
659 unsigned long long sta;
660
661 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
662 alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
663 }
664 if (!alive)
665 alive = pci_device_is_present(dev);
666
667 if (!alive) {
668 pci_dev_set_disconnected(dev, NULL);
669 if (pci_has_subordinate(dev))
670 pci_walk_bus(dev->subordinate, pci_dev_set_disconnected,
671 NULL);
672
673 pci_stop_and_remove_bus_device(dev);
674 if (adev)
675 acpi_bus_trim(adev);
676 } else if (bus) {
677 struct pci_dev *child, *tmp;
678
679 /* The device is a bridge. so check the bus below it. */
680 pm_runtime_get_sync(&dev->dev);
681 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
682 trim_stale_devices(child);
683
684 pm_runtime_put(&dev->dev);
685 }
686}
687
688/**
689 * acpiphp_check_bridge - re-enumerate devices
690 * @bridge: where to begin re-enumeration
691 *
692 * Iterate over all slots under this bridge and make sure that if a
693 * card is present they are enabled, and if not they are disabled.
694 */
695static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
696{
697 struct acpiphp_slot *slot;
698
699 /* Bail out if the bridge is going away. */
700 if (bridge->is_going_away)
701 return;
702
703 if (bridge->pci_dev)
704 pm_runtime_get_sync(&bridge->pci_dev->dev);
705
706 list_for_each_entry(slot, &bridge->slots, node) {
707 struct pci_bus *bus = slot->bus;
708 struct pci_dev *dev, *tmp;
709
710 if (slot_no_hotplug(slot)) {
711 ; /* do nothing */
712 } else if (device_status_valid(get_slot_status(slot))) {
713 /* remove stale devices if any */
714 list_for_each_entry_safe_reverse(dev, tmp,
715 &bus->devices, bus_list)
716 if (PCI_SLOT(dev->devfn) == slot->device)
717 trim_stale_devices(dev);
718
719 /* configure all functions */
720 enable_slot(slot, true);
721 } else {
722 disable_slot(slot);
723 }
724 }
725
726 if (bridge->pci_dev)
727 pm_runtime_put(&bridge->pci_dev->dev);
728}
729
730/*
731 * Remove devices for which we could not assign resources, call
732 * arch specific code to fix-up the bus
733 */
734static void acpiphp_sanitize_bus(struct pci_bus *bus)
735{
736 struct pci_dev *dev, *tmp;
737 int i;
738 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
739
740 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
741 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
742 struct resource *res = &dev->resource[i];
743 if ((res->flags & type_mask) && !res->start &&
744 res->end) {
745 /* Could not assign a required resources
746 * for this device, remove it */
747 pci_stop_and_remove_bus_device(dev);
748 break;
749 }
750 }
751 }
752}
753
754/*
755 * ACPI event handlers
756 */
757
758void acpiphp_check_host_bridge(struct acpi_device *adev)
759{
760 struct acpiphp_bridge *bridge = NULL;
761
762 acpi_lock_hp_context();
763 if (adev->hp) {
764 bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
765 if (bridge)
766 get_bridge(bridge);
767 }
768 acpi_unlock_hp_context();
769 if (bridge) {
770 pci_lock_rescan_remove();
771
772 acpiphp_check_bridge(bridge);
773
774 pci_unlock_rescan_remove();
775 put_bridge(bridge);
776 }
777}
778
779static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
780
781static void hotplug_event(u32 type, struct acpiphp_context *context)
782{
783 acpi_handle handle = context->hp.self->handle;
784 struct acpiphp_func *func = &context->func;
785 struct acpiphp_slot *slot = func->slot;
786 struct acpiphp_bridge *bridge;
787
788 acpi_lock_hp_context();
789 bridge = context->bridge;
790 if (bridge)
791 get_bridge(bridge);
792
793 acpi_unlock_hp_context();
794
795 pci_lock_rescan_remove();
796
797 switch (type) {
798 case ACPI_NOTIFY_BUS_CHECK:
799 /* bus re-enumerate */
800 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
801 if (bridge)
802 acpiphp_check_bridge(bridge);
803 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
804 enable_slot(slot, false);
805
806 break;
807
808 case ACPI_NOTIFY_DEVICE_CHECK:
809 /* device check */
810 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
811 if (bridge) {
812 acpiphp_check_bridge(bridge);
813 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
814 /*
815 * Check if anything has changed in the slot and rescan
816 * from the parent if that's the case.
817 */
818 if (acpiphp_rescan_slot(slot))
819 acpiphp_check_bridge(func->parent);
820 }
821 break;
822
823 case ACPI_NOTIFY_EJECT_REQUEST:
824 /* request device eject */
825 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
826 acpiphp_disable_and_eject_slot(slot);
827 break;
828 }
829
830 pci_unlock_rescan_remove();
831 if (bridge)
832 put_bridge(bridge);
833}
834
835static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
836{
837 struct acpiphp_context *context;
838
839 context = acpiphp_grab_context(adev);
840 if (!context)
841 return -ENODATA;
842
843 hotplug_event(type, context);
844 acpiphp_let_context_go(context);
845 return 0;
846}
847
848/**
849 * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
850 * @bus: PCI bus to enumerate the slots for.
851 *
852 * A "slot" is an object associated with a PCI device number. All functions
853 * (PCI devices) with the same bus and device number belong to the same slot.
854 */
855void acpiphp_enumerate_slots(struct pci_bus *bus)
856{
857 struct acpiphp_bridge *bridge;
858 struct acpi_device *adev;
859 acpi_handle handle;
860 acpi_status status;
861
862 if (acpiphp_disabled)
863 return;
864
865 adev = ACPI_COMPANION(bus->bridge);
866 if (!adev)
867 return;
868
869 handle = adev->handle;
870 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
871 if (!bridge)
872 return;
873
874 INIT_LIST_HEAD(&bridge->slots);
875 kref_init(&bridge->ref);
876 bridge->pci_dev = pci_dev_get(bus->self);
877 bridge->pci_bus = bus;
878
879 /*
880 * Grab a ref to the subordinate PCI bus in case the bus is
881 * removed via PCI core logical hotplug. The ref pins the bus
882 * (which we access during module unload).
883 */
884 get_device(&bus->dev);
885
886 acpi_lock_hp_context();
887 if (pci_is_root_bus(bridge->pci_bus)) {
888 struct acpiphp_root_context *root_context;
889
890 root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
891 if (!root_context)
892 goto err;
893
894 root_context->root_bridge = bridge;
895 acpi_set_hp_context(adev, &root_context->hp);
896 } else {
897 struct acpiphp_context *context;
898
899 /*
900 * This bridge should have been registered as a hotplug function
901 * under its parent, so the context should be there, unless the
902 * parent is going to be handled by pciehp, in which case this
903 * bridge is not interesting to us either.
904 */
905 context = acpiphp_get_context(adev);
906 if (!context)
907 goto err;
908
909 bridge->context = context;
910 context->bridge = bridge;
911 /* Get a reference to the parent bridge. */
912 get_bridge(context->func.parent);
913 }
914 acpi_unlock_hp_context();
915
916 /* Must be added to the list prior to calling acpiphp_add_context(). */
917 mutex_lock(&bridge_mutex);
918 list_add(&bridge->list, &bridge_list);
919 mutex_unlock(&bridge_mutex);
920
921 /* register all slot objects under this bridge */
922 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
923 acpiphp_add_context, NULL, bridge, NULL);
924 if (ACPI_FAILURE(status)) {
925 acpi_handle_err(handle, "failed to register slots\n");
926 cleanup_bridge(bridge);
927 put_bridge(bridge);
928 }
929 return;
930
931 err:
932 acpi_unlock_hp_context();
933 put_device(&bus->dev);
934 pci_dev_put(bridge->pci_dev);
935 kfree(bridge);
936}
937
938static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
939{
940 if (pci_is_root_bus(bridge->pci_bus)) {
941 struct acpiphp_root_context *root_context;
942 struct acpi_device *adev;
943
944 acpi_lock_hp_context();
945 adev = ACPI_COMPANION(bridge->pci_bus->bridge);
946 root_context = to_acpiphp_root_context(adev->hp);
947 adev->hp = NULL;
948 acpi_unlock_hp_context();
949 kfree(root_context);
950 }
951 cleanup_bridge(bridge);
952 put_bridge(bridge);
953}
954
955/**
956 * acpiphp_remove_slots - Remove slot objects associated with a given bus.
957 * @bus: PCI bus to remove the slot objects for.
958 */
959void acpiphp_remove_slots(struct pci_bus *bus)
960{
961 struct acpiphp_bridge *bridge;
962
963 if (acpiphp_disabled)
964 return;
965
966 mutex_lock(&bridge_mutex);
967 list_for_each_entry(bridge, &bridge_list, list)
968 if (bridge->pci_bus == bus) {
969 mutex_unlock(&bridge_mutex);
970 acpiphp_drop_bridge(bridge);
971 return;
972 }
973
974 mutex_unlock(&bridge_mutex);
975}
976
977/**
978 * acpiphp_enable_slot - power on slot
979 * @slot: ACPI PHP slot
980 */
981int acpiphp_enable_slot(struct acpiphp_slot *slot)
982{
983 pci_lock_rescan_remove();
984
985 if (slot->flags & SLOT_IS_GOING_AWAY) {
986 pci_unlock_rescan_remove();
987 return -ENODEV;
988 }
989
990 /* configure all functions */
991 if (!(slot->flags & SLOT_ENABLED))
992 enable_slot(slot, false);
993
994 pci_unlock_rescan_remove();
995 return 0;
996}
997
998/**
999 * acpiphp_disable_and_eject_slot - power off and eject slot
1000 * @slot: ACPI PHP slot
1001 */
1002static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1003{
1004 struct acpiphp_func *func;
1005
1006 if (slot->flags & SLOT_IS_GOING_AWAY)
1007 return -ENODEV;
1008
1009 /* unconfigure all functions */
1010 disable_slot(slot);
1011
1012 list_for_each_entry(func, &slot->funcs, sibling)
1013 if (func->flags & FUNC_HAS_EJ0) {
1014 acpi_handle handle = func_to_handle(func);
1015
1016 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1017 acpi_handle_err(handle, "_EJ0 failed\n");
1018
1019 break;
1020 }
1021
1022 return 0;
1023}
1024
1025int acpiphp_disable_slot(struct acpiphp_slot *slot)
1026{
1027 int ret;
1028
1029 /*
1030 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
1031 * acpiphp_disable_and_eject_slot() will be synchronized properly.
1032 */
1033 acpi_scan_lock_acquire();
1034 pci_lock_rescan_remove();
1035 ret = acpiphp_disable_and_eject_slot(slot);
1036 pci_unlock_rescan_remove();
1037 acpi_scan_lock_release();
1038 return ret;
1039}
1040
1041/*
1042 * slot enabled: 1
1043 * slot disabled: 0
1044 */
1045u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1046{
1047 return (slot->flags & SLOT_ENABLED);
1048}
1049
1050/*
1051 * latch open: 1
1052 * latch closed: 0
1053 */
1054u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1055{
1056 return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1057}
1058
1059/*
1060 * adapter presence : 1
1061 * absence : 0
1062 */
1063u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1064{
1065 return !!get_slot_status(slot);
1066}