PCI: Add SPDX GPL-2.0 to replace COPYING boilerplate
[linux-2.6-block.git] / drivers / pci / hotplug / acpiphp_glue.c
CommitLineData
1da177e4
LT
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
42f49a6a
RS
7 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
8e7561cf
RS
9 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
1da177e4
LT
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 *
998be20f 29 * Send feedback to <kristen.c.accardi@intel.com>
1da177e4
LT
30 *
31 */
32
42f49a6a
RS
33/*
34 * Lifetime rules for pci_dev:
42f49a6a
RS
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.
5d4a4b25
AC
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.
42f49a6a
RS
40 */
41
bd950799
LT
42#define pr_fmt(fmt) "acpiphp_glue: " fmt
43
1da177e4
LT
44#include <linux/module.h>
45
46#include <linux/kernel.h>
47#include <linux/pci.h>
7a54f25c 48#include <linux/pci_hotplug.h>
e8c331e9 49#include <linux/pci-acpi.h>
4ebe3450 50#include <linux/pm_runtime.h>
6aa4cdd0 51#include <linux/mutex.h>
5a0e3ad6 52#include <linux/slab.h>
6af8bef1 53#include <linux/acpi.h>
1da177e4
LT
54
55#include "../pci.h"
1da177e4
LT
56#include "acpiphp.h"
57
58static LIST_HEAD(bridge_list);
3d54a316 59static DEFINE_MUTEX(bridge_mutex);
1da177e4 60
be27b3dc 61static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
edf5bf34 62static void acpiphp_post_dock_fixup(struct acpi_device *adev);
8e5dce35 63static void acpiphp_sanitize_bus(struct pci_bus *bus);
d3a1ebb0 64static void hotplug_event(u32 type, struct acpiphp_context *context);
3d54a316 65static void free_bridge(struct kref *kref);
8e5dce35 66
cb7b8ced
RW
67/**
68 * acpiphp_init_context - Create hotplug context and grab a reference to it.
bbcbfc0e 69 * @adev: ACPI device object to create the context for.
cb7b8ced 70 *
e525506f 71 * Call under acpi_hp_context_lock.
cb7b8ced 72 */
bbcbfc0e 73static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
cb7b8ced
RW
74{
75 struct acpiphp_context *context;
cb7b8ced
RW
76
77 context = kzalloc(sizeof(*context), GFP_KERNEL);
78 if (!context)
79 return NULL;
80
cb7b8ced 81 context->refcount = 1;
ba574dc8
RW
82 context->hp.notify = acpiphp_hotplug_notify;
83 context->hp.fixup = acpiphp_post_dock_fixup;
84 acpi_set_hp_context(adev, &context->hp);
cb7b8ced
RW
85 return context;
86}
87
88/**
89 * acpiphp_get_context - Get hotplug context and grab a reference to it.
3c2cc7ff 90 * @adev: ACPI device object to get the context for.
cb7b8ced 91 *
e525506f 92 * Call under acpi_hp_context_lock.
cb7b8ced 93 */
3c2cc7ff 94static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
cb7b8ced 95{
3c2cc7ff 96 struct acpiphp_context *context;
cb7b8ced 97
3c2cc7ff
RW
98 if (!adev->hp)
99 return NULL;
100
101 context = to_acpiphp_context(adev->hp);
102 context->refcount++;
cb7b8ced
RW
103 return context;
104}
105
106/**
107 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
bbcbfc0e 108 * @context: ACPI hotplug context to drop a reference to.
cb7b8ced
RW
109 *
110 * The context object is removed if there are no more references to it.
111 *
e525506f 112 * Call under acpi_hp_context_lock.
cb7b8ced
RW
113 */
114static void acpiphp_put_context(struct acpiphp_context *context)
115{
116 if (--context->refcount)
117 return;
118
bd4674df 119 WARN_ON(context->bridge);
3c2cc7ff 120 context->hp.self->hp = NULL;
cb7b8ced
RW
121 kfree(context);
122}
123
3d54a316
JL
124static inline void get_bridge(struct acpiphp_bridge *bridge)
125{
126 kref_get(&bridge->ref);
127}
128
129static inline void put_bridge(struct acpiphp_bridge *bridge)
130{
131 kref_put(&bridge->ref, free_bridge);
132}
133
edf5bf34
RW
134static 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
150static void acpiphp_let_context_go(struct acpiphp_context *context)
151{
152 put_bridge(context->func.parent);
153}
154
3d54a316
JL
155static void free_bridge(struct kref *kref)
156{
cb7b8ced 157 struct acpiphp_context *context;
3d54a316
JL
158 struct acpiphp_bridge *bridge;
159 struct acpiphp_slot *slot, *next;
160 struct acpiphp_func *func, *tmp;
161
e525506f 162 acpi_lock_hp_context();
cb7b8ced 163
3d54a316
JL
164 bridge = container_of(kref, struct acpiphp_bridge, ref);
165
166 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
bd4674df
RW
167 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
168 acpiphp_put_context(func_to_context(func));
169
3d54a316
JL
170 kfree(slot);
171 }
172
87831273 173 context = bridge->context;
bbd34fcd
RW
174 /* Root bridges will not have hotplug context. */
175 if (context) {
176 /* Release the reference taken by acpiphp_enumerate_slots(). */
bda46dbb 177 put_bridge(context->func.parent);
bbd34fcd
RW
178 context->bridge = NULL;
179 acpiphp_put_context(context);
180 }
cb7b8ced 181
3d54a316
JL
182 put_device(&bridge->pci_bus->dev);
183 pci_dev_put(bridge->pci_dev);
184 kfree(bridge);
cb7b8ced 185
e525506f 186 acpi_unlock_hp_context();
3d54a316
JL
187}
188
edf5bf34
RW
189/**
190 * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices.
191 * @adev: ACPI device object corresponding to a PCI device.
4e8662bb 192 *
edf5bf34 193 * TBD - figure out a way to only call fixups for systems that require them.
4e8662bb 194 */
edf5bf34 195static void acpiphp_post_dock_fixup(struct acpi_device *adev)
4e8662bb 196{
edf5bf34
RW
197 struct acpiphp_context *context = acpiphp_grab_context(adev);
198 struct pci_bus *bus;
4e8662bb
KA
199 u32 buses;
200
edf5bf34 201 if (!context)
f09ce741 202 return;
4e8662bb 203
edf5bf34
RW
204 bus = context->func.slot->bus;
205 if (!bus->self)
206 goto out;
207
4e8662bb
KA
208 /* fixup bad _DCK function that rewrites
209 * secondary bridge on slot
210 */
edf5bf34 211 pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
4e8662bb 212
b918c62e 213 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
4e8662bb 214 buses = (buses & 0xff000000)
2a9d3521 215 | ((unsigned int)(bus->primary) << 0)
b918c62e
YL
216 | ((unsigned int)(bus->busn_res.start) << 8)
217 | ((unsigned int)(bus->busn_res.end) << 16);
4e8662bb
KA
218 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
219 }
4e8662bb 220
edf5bf34
RW
221 out:
222 acpiphp_let_context_go(context);
af9d8adc 223}
4e8662bb 224
3799c5a0
RW
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 */
232static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
233 void **rv)
1da177e4 234{
cb7b8ced
RW
235 struct acpiphp_bridge *bridge = data;
236 struct acpiphp_context *context;
bbcbfc0e 237 struct acpi_device *adev;
1da177e4
LT
238 struct acpiphp_slot *slot;
239 struct acpiphp_func *newfunc;
1da177e4 240 acpi_status status = AE_OK;
bbd34fcd
RW
241 unsigned long long adr;
242 int device, function;
e8c331e9 243 struct pci_bus *pbus = bridge->pci_bus;
bbd34fcd 244 struct pci_dev *pdev = bridge->pci_dev;
3b63aaa7 245 u32 val;
1da177e4 246
dfb117b3
BH
247 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
248 if (ACPI_FAILURE(status)) {
f26ca1d6
TK
249 if (status != AE_NOT_FOUND)
250 acpi_handle_warn(handle,
251 "can't evaluate _ADR (%#x)\n", status);
dfb117b3
BH
252 return AE_OK;
253 }
bbcbfc0e
RW
254 if (acpi_bus_get_device(handle, &adev))
255 return AE_OK;
dfb117b3
BH
256
257 device = (adr >> 16) & 0xffff;
258 function = adr & 0xffff;
259
e525506f 260 acpi_lock_hp_context();
bbcbfc0e 261 context = acpiphp_init_context(adev);
cb7b8ced 262 if (!context) {
e525506f 263 acpi_unlock_hp_context();
cb7b8ced 264 acpi_handle_err(handle, "No hotplug context\n");
cb7b8ced
RW
265 return AE_NOT_EXIST;
266 }
bd4674df 267 newfunc = &context->func;
bd4674df 268 newfunc->function = function;
bda46dbb 269 newfunc->parent = bridge;
edf5bf34 270 acpi_unlock_hp_context();
cb7b8ced 271
edf5bf34
RW
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"))
20416ea5 277 newfunc->flags = FUNC_HAS_EJ0;
1da177e4 278
ecd046da 279 if (acpi_has_method(handle, "_STA"))
1da177e4
LT
280 newfunc->flags |= FUNC_HAS_STA;
281
1da177e4 282 /* search for objects that share the same slot */
ad41dd9d 283 list_for_each_entry(slot, &bridge->slots, node)
bbd34fcd 284 if (slot->device == device)
ac372338 285 goto slot_found;
1da177e4 286
ac372338
RW
287 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
288 if (!slot) {
e525506f 289 acpi_lock_hp_context();
146fc68a 290 acpiphp_put_context(context);
e525506f 291 acpi_unlock_hp_context();
146fc68a 292 return AE_NO_MEMORY;
ac372338
RW
293 }
294
bda46dbb 295 slot->bus = bridge->pci_bus;
ac372338 296 slot->device = device;
ac372338 297 INIT_LIST_HEAD(&slot->funcs);
ac372338 298
ac372338 299 list_add_tail(&slot->node, &bridge->slots);
1da177e4 300
cc6254e0
RW
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 */
3b52b21f 307 if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
437eb7bf 308 && !(pdev && pdev->is_hotplug_bridge && pciehp_is_native(pdev))) {
bbd34fcd
RW
309 unsigned long long sun;
310 int retval;
ac372338 311
bbd34fcd
RW
312 bridge->nr_slots++;
313 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
314 if (ACPI_FAILURE(status))
315 sun = bridge->nr_slots;
316
bd950799 317 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
7342798d 318 sun, pci_domain_nr(pbus), pbus->number, device);
bbd34fcd 319
7342798d 320 retval = acpiphp_register_hotplug_slot(slot, sun);
bbd34fcd 321 if (retval) {
1aaac071 322 slot->slot = NULL;
bbd34fcd
RW
323 bridge->nr_slots--;
324 if (retval == -EBUSY)
227f0647 325 pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
bbd34fcd 326 else
227f0647 327 pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
bbd34fcd
RW
328 }
329 /* Even if the slot registration fails, we can still use it. */
1da177e4
LT
330 }
331
ac372338 332 slot_found:
1da177e4
LT
333 newfunc->slot = slot;
334 list_add_tail(&newfunc->sibling, &slot->funcs);
335
3b63aaa7
JL
336 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
337 &val, 60*1000))
bc805a55 338 slot->flags |= SLOT_ENABLED;
1da177e4 339
2e862c51 340 return AE_OK;
1da177e4
LT
341}
342
364d5094 343static void cleanup_bridge(struct acpiphp_bridge *bridge)
1da177e4 344{
3d54a316
JL
345 struct acpiphp_slot *slot;
346 struct acpiphp_func *func;
42f49a6a 347
3d54a316
JL
348 list_for_each_entry(slot, &bridge->slots, node) {
349 list_for_each_entry(func, &slot->funcs, sibling) {
1a699476 350 struct acpi_device *adev = func_to_acpi_device(func);
5a3bc573 351
1a699476 352 acpi_lock_hp_context();
be27b3dc 353 adev->hp->notify = NULL;
edf5bf34 354 adev->hp->fixup = NULL;
1a699476 355 acpi_unlock_hp_context();
42f49a6a 356 }
9217a984 357 slot->flags |= SLOT_IS_GOING_AWAY;
1aaac071
RW
358 if (slot->slot)
359 acpiphp_unregister_hotplug_slot(slot);
42f49a6a
RS
360 }
361
3d54a316 362 mutex_lock(&bridge_mutex);
42f49a6a 363 list_del(&bridge->list);
3d54a316 364 mutex_unlock(&bridge_mutex);
9217a984 365
e525506f 366 acpi_lock_hp_context();
9217a984 367 bridge->is_going_away = true;
e525506f 368 acpi_unlock_hp_context();
1da177e4
LT
369}
370
15a1ae74 371/**
26e6c66e 372 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
15a1ae74 373 * @bus: bus to start search with
15a1ae74
KA
374 */
375static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
376{
c6f0d5ad 377 struct pci_bus *tmp;
15a1ae74
KA
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 */
b918c62e 388 max = bus->busn_res.start;
15a1ae74 389
c6f0d5ad
YW
390 list_for_each_entry(tmp, &bus->children, node) {
391 n = pci_bus_max_busnr(tmp);
15a1ae74
KA
392 if (n > max)
393 max = n;
394 }
395 return max;
396}
397
d0607050
SL
398static 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 */
5a3bc573
RW
412 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
413 NULL);
d0607050
SL
414 }
415}
416
1f96a965
YL
417static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
418{
419 struct acpiphp_func *func;
420
1f96a965
YL
421 /* quirk, or pcie could set it already */
422 if (dev->is_hotplug_bridge)
423 return;
424
1f96a965
YL
425 list_for_each_entry(func, &slot->funcs, sibling) {
426 if (PCI_FUNC(dev->devfn) == func->function) {
bbd34fcd 427 dev->is_hotplug_bridge = 1;
1f96a965
YL
428 break;
429 }
430 }
431}
3b63aaa7 432
a47d8c8e
RW
433static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
434{
435 struct acpiphp_func *func;
436
b6708fbf
RW
437 list_for_each_entry(func, &slot->funcs, sibling) {
438 struct acpi_device *adev = func_to_acpi_device(func);
a47d8c8e 439
b6708fbf
RW
440 acpi_bus_scan(adev->handle);
441 if (acpi_device_enumerated(adev))
442 acpi_device_set_power(adev, ACPI_STATE_D0);
443 }
a47d8c8e
RW
444 return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
445}
446
1da177e4 447/**
a1d0abce 448 * enable_slot - enable, configure a slot
1da177e4
LT
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.
1da177e4 453 */
10874f5a 454static void enable_slot(struct acpiphp_slot *slot)
1da177e4 455{
1da177e4 456 struct pci_dev *dev;
bda46dbb 457 struct pci_bus *bus = slot->bus;
1da177e4 458 struct acpiphp_func *func;
b91182a6 459 int max, pass;
d66ecb72 460 LIST_HEAD(add_list);
1da177e4 461
ab122590 462 acpiphp_rescan_slot(slot);
15a1ae74 463 max = acpiphp_max_busnr(bus);
42f49a6a 464 for (pass = 0; pass < 2; pass++) {
24a0c654 465 for_each_pci_bridge(dev, bus) {
42f49a6a
RS
466 if (PCI_SLOT(dev->devfn) != slot->device)
467 continue;
a1d0abce 468
24a0c654
AS
469 max = pci_scan_bridge(bus, dev, max, pass);
470 if (pass && dev->subordinate) {
471 check_hotplug_bridge(slot, dev);
472 pcibios_resource_survey_bus(dev->subordinate);
473 __pci_bus_size_bridges(dev->subordinate, &add_list);
c64b5eea 474 }
42f49a6a 475 }
1da177e4 476 }
d66ecb72 477 __pci_bus_assign_resources(bus, &add_list, NULL);
2dc41281 478
8e5dce35 479 acpiphp_sanitize_bus(bus);
81ee5732 480 pcie_bus_configure_settings(bus);
d0607050 481 acpiphp_set_acpi_region(slot);
69643e48
IC
482
483 list_for_each_entry(dev, &bus->devices, bus_list) {
484 /* Assume that newly added devices are powered on already. */
485 if (!dev->is_added)
486 dev->current_state = PCI_D0;
487 }
488
42f49a6a
RS
489 pci_bus_add_devices(bus);
490
f382a086 491 slot->flags |= SLOT_ENABLED;
58c08628 492 list_for_each_entry(func, &slot->funcs, sibling) {
9d911d79
AC
493 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
494 func->function));
f382a086
AK
495 if (!dev) {
496 /* Do not set SLOT_ENABLED flag if some funcs
497 are not added. */
498 slot->flags &= (~SLOT_ENABLED);
551bcb75 499 continue;
f382a086 500 }
1da177e4 501 }
1da177e4
LT
502}
503
1da177e4 504/**
a1d0abce 505 * disable_slot - disable a slot
26e6c66e 506 * @slot: ACPI PHP slot
1da177e4 507 */
a1d0abce 508static void disable_slot(struct acpiphp_slot *slot)
1da177e4 509{
1c0c5443
RW
510 struct pci_bus *bus = slot->bus;
511 struct pci_dev *dev, *prev;
1da177e4 512 struct acpiphp_func *func;
551bcb75 513
ce29ca3e 514 /*
a1d0abce 515 * enable_slot() enumerates all functions in this device via
ce29ca3e
AK
516 * pci_scan_slot(), whether they have associated ACPI hotplug
517 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
518 * here.
519 */
1c0c5443
RW
520 list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
521 if (PCI_SLOT(dev->devfn) == slot->device)
522 pci_stop_and_remove_bus_device(dev);
600812ec 523
5a3bc573 524 list_for_each_entry(func, &slot->funcs, sibling)
bbcbfc0e 525 acpi_bus_trim(func_to_acpi_device(func));
1da177e4
LT
526
527 slot->flags &= (~SLOT_ENABLED);
1da177e4
LT
528}
529
f244d8b6
RW
530static bool slot_no_hotplug(struct acpiphp_slot *slot)
531{
b440bde7
BH
532 struct pci_bus *bus = slot->bus;
533 struct pci_dev *dev;
f244d8b6 534
b440bde7
BH
535 list_for_each_entry(dev, &bus->devices, bus_list) {
536 if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
f244d8b6 537 return true;
b440bde7 538 }
f244d8b6
RW
539 return false;
540}
1da177e4
LT
541
542/**
543 * get_slot_status - get ACPI slot status
26e6c66e 544 * @slot: ACPI PHP slot
1da177e4 545 *
26e6c66e
RD
546 * If a slot has _STA for each function and if any one of them
547 * returned non-zero status, return it.
1da177e4 548 *
26e6c66e
RD
549 * If a slot doesn't have _STA and if any one of its functions'
550 * configuration space is configured, return 0x0f as a _STA.
1da177e4 551 *
26e6c66e 552 * Otherwise return 0.
1da177e4
LT
553 */
554static unsigned int get_slot_status(struct acpiphp_slot *slot)
555{
27663c58 556 unsigned long long sta = 0;
1da177e4
LT
557 struct acpiphp_func *func;
558
58c08628 559 list_for_each_entry(func, &slot->funcs, sibling) {
1da177e4 560 if (func->flags & FUNC_HAS_STA) {
5a3bc573
RW
561 acpi_status status;
562
563 status = acpi_evaluate_integer(func_to_handle(func),
564 "_STA", NULL, &sta);
1da177e4
LT
565 if (ACPI_SUCCESS(status) && sta)
566 break;
567 } else {
5a3bc573
RW
568 u32 dvid;
569
bda46dbb 570 pci_bus_read_config_dword(slot->bus,
1da177e4
LT
571 PCI_DEVFN(slot->device,
572 func->function),
573 PCI_VENDOR_ID, &dvid);
574 if (dvid != 0xffffffff) {
575 sta = ACPI_STA_ALL;
576 break;
577 }
578 }
579 }
580
581 return (unsigned int)sta;
582}
583
72820594
MW
584static inline bool device_status_valid(unsigned int sta)
585{
586 /*
587 * ACPI spec says that _STA may return bit 0 clear with bit 3 set
588 * if the device is valid but does not require a device driver to be
589 * loaded (Section 6.3.7 of ACPI 5.0A).
590 */
591 unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
592 return (sta & mask) == mask;
593}
594
4ebe3450
RW
595/**
596 * trim_stale_devices - remove PCI devices that are not responding.
597 * @dev: PCI device to start walking the hierarchy from.
598 */
599static void trim_stale_devices(struct pci_dev *dev)
600{
4dc3082d 601 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
4ebe3450 602 struct pci_bus *bus = dev->subordinate;
d41be346 603 bool alive = dev->ignore_hotplug;
4ebe3450 604
4dc3082d 605 if (adev) {
4ebe3450
RW
606 acpi_status status;
607 unsigned long long sta;
608
b2118d6a 609 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
d41be346 610 alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
4ebe3450 611 }
b8a62d54
RW
612 if (!alive)
613 alive = pci_device_is_present(dev);
4ebe3450 614
4ebe3450
RW
615 if (!alive) {
616 pci_stop_and_remove_bus_device(dev);
4dc3082d
RW
617 if (adev)
618 acpi_bus_trim(adev);
4ebe3450
RW
619 } else if (bus) {
620 struct pci_dev *child, *tmp;
621
622 /* The device is a bridge. so check the bus below it. */
623 pm_runtime_get_sync(&dev->dev);
2d7c1b77 624 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
4ebe3450
RW
625 trim_stale_devices(child);
626
627 pm_runtime_put(&dev->dev);
628 }
629}
630
1da177e4
LT
631/**
632 * acpiphp_check_bridge - re-enumerate devices
26e6c66e 633 * @bridge: where to begin re-enumeration
1da177e4
LT
634 *
635 * Iterate over all slots under this bridge and make sure that if a
636 * card is present they are enabled, and if not they are disabled.
637 */
4ebe3450 638static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1da177e4
LT
639{
640 struct acpiphp_slot *slot;
1da177e4 641
9217a984
RW
642 /* Bail out if the bridge is going away. */
643 if (bridge->is_going_away)
644 return;
645
dd7fd3a8
RW
646 if (bridge->pci_dev)
647 pm_runtime_get_sync(&bridge->pci_dev->dev);
648
ad41dd9d 649 list_for_each_entry(slot, &bridge->slots, node) {
4ebe3450
RW
650 struct pci_bus *bus = slot->bus;
651 struct pci_dev *dev, *tmp;
652
f244d8b6
RW
653 if (slot_no_hotplug(slot)) {
654 ; /* do nothing */
72820594 655 } else if (device_status_valid(get_slot_status(slot))) {
4ebe3450 656 /* remove stale devices if any */
2d7c1b77
RW
657 list_for_each_entry_safe_reverse(dev, tmp,
658 &bus->devices, bus_list)
4ebe3450
RW
659 if (PCI_SLOT(dev->devfn) == slot->device)
660 trim_stale_devices(dev);
661
662 /* configure all functions */
a1d0abce 663 enable_slot(slot);
1da177e4 664 } else {
a1d0abce 665 disable_slot(slot);
1da177e4
LT
666 }
667 }
dd7fd3a8
RW
668
669 if (bridge->pci_dev)
670 pm_runtime_put(&bridge->pci_dev->dev);
1da177e4
LT
671}
672
8e7561cf
RS
673/*
674 * Remove devices for which we could not assign resources, call
675 * arch specific code to fix-up the bus
676 */
677static void acpiphp_sanitize_bus(struct pci_bus *bus)
678{
d65eba6a 679 struct pci_dev *dev, *tmp;
8e7561cf
RS
680 int i;
681 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
682
2d7c1b77 683 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
ff3ce480 684 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
8e7561cf
RS
685 struct resource *res = &dev->resource[i];
686 if ((res->flags & type_mask) && !res->start &&
687 res->end) {
688 /* Could not assign a required resources
689 * for this device, remove it */
210647af 690 pci_stop_and_remove_bus_device(dev);
8e7561cf
RS
691 break;
692 }
693 }
694 }
695}
696
1da177e4
LT
697/*
698 * ACPI event handlers
699 */
700
1f7c164b 701void acpiphp_check_host_bridge(struct acpi_device *adev)
3f327e39 702{
86f5f3ca 703 struct acpiphp_bridge *bridge = NULL;
3f327e39 704
86f5f3ca
RW
705 acpi_lock_hp_context();
706 if (adev->hp) {
707 bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
708 if (bridge)
709 get_bridge(bridge);
710 }
711 acpi_unlock_hp_context();
3f327e39 712 if (bridge) {
d42f5da2
RW
713 pci_lock_rescan_remove();
714
3f327e39 715 acpiphp_check_bridge(bridge);
d42f5da2
RW
716
717 pci_unlock_rescan_remove();
3f327e39
YL
718 put_bridge(bridge);
719 }
3f327e39
YL
720}
721
9217a984
RW
722static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
723
d3a1ebb0 724static void hotplug_event(u32 type, struct acpiphp_context *context)
1da177e4 725{
3c2cc7ff 726 acpi_handle handle = context->hp.self->handle;
bd4674df 727 struct acpiphp_func *func = &context->func;
b75cece1 728 struct acpiphp_slot *slot = func->slot;
1da177e4 729 struct acpiphp_bridge *bridge;
6af8bef1 730
e525506f 731 acpi_lock_hp_context();
c8ebcf1f 732 bridge = context->bridge;
43e5c091
RW
733 if (bridge)
734 get_bridge(bridge);
1da177e4 735
e525506f 736 acpi_unlock_hp_context();
3757b948 737
f41b3261 738 pci_lock_rescan_remove();
1da177e4
LT
739
740 switch (type) {
741 case ACPI_NOTIFY_BUS_CHECK:
742 /* bus re-enumerate */
1d4a5b61 743 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
b75cece1 744 if (bridge)
43e5c091 745 acpiphp_check_bridge(bridge);
b75cece1 746 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
a1d0abce 747 enable_slot(slot);
4ebe3450 748
1da177e4
LT
749 break;
750
751 case ACPI_NOTIFY_DEVICE_CHECK:
752 /* device check */
1d4a5b61 753 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
a47d8c8e 754 if (bridge) {
43e5c091 755 acpiphp_check_bridge(bridge);
b75cece1 756 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
a47d8c8e
RW
757 /*
758 * Check if anything has changed in the slot and rescan
759 * from the parent if that's the case.
760 */
661b4064 761 if (acpiphp_rescan_slot(slot))
a47d8c8e
RW
762 acpiphp_check_bridge(func->parent);
763 }
1da177e4
LT
764 break;
765
1da177e4
LT
766 case ACPI_NOTIFY_EJECT_REQUEST:
767 /* request device eject */
1d4a5b61 768 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
b75cece1 769 acpiphp_disable_and_eject_slot(slot);
1da177e4 770 break;
1da177e4 771 }
6af8bef1 772
f41b3261 773 pci_unlock_rescan_remove();
43e5c091
RW
774 if (bridge)
775 put_bridge(bridge);
21a31013
RW
776}
777
be27b3dc 778static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
6af8bef1 779{
87831273 780 struct acpiphp_context *context;
87831273 781
edf5bf34
RW
782 context = acpiphp_grab_context(adev);
783 if (!context)
3c2cc7ff 784 return -ENODATA;
1b360f44 785
3c2cc7ff 786 hotplug_event(type, context);
edf5bf34 787 acpiphp_let_context_go(context);
3c2cc7ff 788 return 0;
8e7561cf 789}
1da177e4 790
454481ad
RW
791/**
792 * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
793 * @bus: PCI bus to enumerate the slots for.
794 *
795 * A "slot" is an object associated with a PCI device number. All functions
796 * (PCI devices) with the same bus and device number belong to the same slot.
1da177e4 797 */
be1c9de9 798void acpiphp_enumerate_slots(struct pci_bus *bus)
1da177e4 799{
3b63aaa7 800 struct acpiphp_bridge *bridge;
bbcbfc0e 801 struct acpi_device *adev;
2552002a
RW
802 acpi_handle handle;
803 acpi_status status;
1da177e4 804
3b63aaa7
JL
805 if (acpiphp_disabled)
806 return;
1da177e4 807
bbcbfc0e
RW
808 adev = ACPI_COMPANION(bus->bridge);
809 if (!adev)
3b63aaa7 810 return;
1da177e4 811
bbcbfc0e 812 handle = adev->handle;
3b63aaa7 813 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
cb7b8ced
RW
814 if (!bridge) {
815 acpi_handle_err(handle, "No memory for bridge object\n");
3b63aaa7
JL
816 return;
817 }
818
ad41dd9d 819 INIT_LIST_HEAD(&bridge->slots);
3d54a316 820 kref_init(&bridge->ref);
3b63aaa7
JL
821 bridge->pci_dev = pci_dev_get(bus->self);
822 bridge->pci_bus = bus;
823
824 /*
825 * Grab a ref to the subordinate PCI bus in case the bus is
826 * removed via PCI core logical hotplug. The ref pins the bus
827 * (which we access during module unload).
828 */
829 get_device(&bus->dev);
830
882d18a7
RW
831 acpi_lock_hp_context();
832 if (pci_is_root_bus(bridge->pci_bus)) {
833 struct acpiphp_root_context *root_context;
834
835 root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
836 if (!root_context)
837 goto err;
838
839 root_context->root_bridge = bridge;
ba574dc8 840 acpi_set_hp_context(adev, &root_context->hp);
882d18a7 841 } else {
bbd34fcd
RW
842 struct acpiphp_context *context;
843
844 /*
845 * This bridge should have been registered as a hotplug function
fd3cfebe
RW
846 * under its parent, so the context should be there, unless the
847 * parent is going to be handled by pciehp, in which case this
848 * bridge is not interesting to us either.
bbd34fcd 849 */
3c2cc7ff 850 context = acpiphp_get_context(adev);
882d18a7
RW
851 if (!context)
852 goto err;
853
bbd34fcd
RW
854 bridge->context = context;
855 context->bridge = bridge;
856 /* Get a reference to the parent bridge. */
bda46dbb 857 get_bridge(context->func.parent);
bbd34fcd 858 }
882d18a7 859 acpi_unlock_hp_context();
bbd34fcd 860
3799c5a0 861 /* Must be added to the list prior to calling acpiphp_add_context(). */
2552002a
RW
862 mutex_lock(&bridge_mutex);
863 list_add(&bridge->list, &bridge_list);
864 mutex_unlock(&bridge_mutex);
865
866 /* register all slot objects under this bridge */
89373a55 867 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
3799c5a0 868 acpiphp_add_context, NULL, bridge, NULL);
2552002a 869 if (ACPI_FAILURE(status)) {
89373a55 870 acpi_handle_err(handle, "failed to register slots\n");
bbd34fcd
RW
871 cleanup_bridge(bridge);
872 put_bridge(bridge);
2552002a 873 }
882d18a7
RW
874 return;
875
876 err:
877 acpi_unlock_hp_context();
878 put_device(&bus->dev);
879 pci_dev_put(bridge->pci_dev);
880 kfree(bridge);
881}
882
902ee490 883static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
882d18a7
RW
884{
885 if (pci_is_root_bus(bridge->pci_bus)) {
886 struct acpiphp_root_context *root_context;
887 struct acpi_device *adev;
888
889 acpi_lock_hp_context();
890 adev = ACPI_COMPANION(bridge->pci_bus->bridge);
891 root_context = to_acpiphp_root_context(adev->hp);
892 adev->hp = NULL;
893 acpi_unlock_hp_context();
894 kfree(root_context);
895 }
896 cleanup_bridge(bridge);
897 put_bridge(bridge);
3b63aaa7
JL
898}
899
454481ad
RW
900/**
901 * acpiphp_remove_slots - Remove slot objects associated with a given bus.
902 * @bus: PCI bus to remove the slot objects for.
903 */
3b63aaa7 904void acpiphp_remove_slots(struct pci_bus *bus)
1da177e4 905{
ff181e5a 906 struct acpiphp_bridge *bridge;
3b63aaa7
JL
907
908 if (acpiphp_disabled)
909 return;
910
ff181e5a
RW
911 mutex_lock(&bridge_mutex);
912 list_for_each_entry(bridge, &bridge_list, list)
3b63aaa7 913 if (bridge->pci_bus == bus) {
ff181e5a 914 mutex_unlock(&bridge_mutex);
882d18a7 915 acpiphp_drop_bridge(bridge);
ff181e5a 916 return;
3b63aaa7 917 }
ff181e5a
RW
918
919 mutex_unlock(&bridge_mutex);
1da177e4
LT
920}
921
1da177e4
LT
922/**
923 * acpiphp_enable_slot - power on slot
26e6c66e 924 * @slot: ACPI PHP slot
1da177e4
LT
925 */
926int acpiphp_enable_slot(struct acpiphp_slot *slot)
927{
9217a984
RW
928 pci_lock_rescan_remove();
929
2c3033a0
IY
930 if (slot->flags & SLOT_IS_GOING_AWAY) {
931 pci_unlock_rescan_remove();
9217a984 932 return -ENODEV;
2c3033a0 933 }
9217a984 934
bc805a55 935 /* configure all functions */
55502ddb 936 if (!(slot->flags & SLOT_ENABLED))
a1d0abce 937 enable_slot(slot);
55502ddb 938
9217a984 939 pci_unlock_rescan_remove();
a1d0abce 940 return 0;
1da177e4
LT
941}
942
1da177e4 943/**
ad21d2d0 944 * acpiphp_disable_and_eject_slot - power off and eject slot
26e6c66e 945 * @slot: ACPI PHP slot
1da177e4 946 */
9217a984 947static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1da177e4 948{
ad21d2d0 949 struct acpiphp_func *func;
9217a984
RW
950
951 if (slot->flags & SLOT_IS_GOING_AWAY)
952 return -ENODEV;
1da177e4 953
1da177e4 954 /* unconfigure all functions */
a1d0abce 955 disable_slot(slot);
1da177e4 956
ad21d2d0
MW
957 list_for_each_entry(func, &slot->funcs, sibling)
958 if (func->flags & FUNC_HAS_EJ0) {
959 acpi_handle handle = func_to_handle(func);
960
961 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
962 acpi_handle_err(handle, "_EJ0 failed\n");
963
964 break;
965 }
966
9217a984 967 return 0;
1da177e4
LT
968}
969
9217a984
RW
970int acpiphp_disable_slot(struct acpiphp_slot *slot)
971{
972 int ret;
973
21369c77
RW
974 /*
975 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
976 * acpiphp_disable_and_eject_slot() will be synchronized properly.
977 */
978 acpi_scan_lock_acquire();
9217a984
RW
979 pci_lock_rescan_remove();
980 ret = acpiphp_disable_and_eject_slot(slot);
981 pci_unlock_rescan_remove();
21369c77 982 acpi_scan_lock_release();
9217a984
RW
983 return ret;
984}
1da177e4
LT
985
986/*
987 * slot enabled: 1
988 * slot disabled: 0
989 */
990u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
991{
bc805a55 992 return (slot->flags & SLOT_ENABLED);
1da177e4
LT
993}
994
1da177e4 995/*
35ae61a0
MT
996 * latch open: 1
997 * latch closed: 0
1da177e4
LT
998 */
999u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1000{
1ad3790a 1001 return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1da177e4
LT
1002}
1003
1da177e4
LT
1004/*
1005 * adapter presence : 1
1006 * absence : 0
1007 */
1008u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1009{
1ad3790a 1010 return !!get_slot_status(slot);
1da177e4 1011}