PCI: Merge multi-line quoted strings
[deliverable/linux.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);
fca6825a 64static void acpiphp_set_hpp_values(struct pci_bus *bus);
d3a1ebb0 65static void hotplug_event(u32 type, struct acpiphp_context *context);
3d54a316 66static void free_bridge(struct kref *kref);
8e5dce35 67
cb7b8ced
RW
68/**
69 * acpiphp_init_context - Create hotplug context and grab a reference to it.
bbcbfc0e 70 * @adev: ACPI device object to create the context for.
cb7b8ced 71 *
e525506f 72 * Call under acpi_hp_context_lock.
cb7b8ced 73 */
bbcbfc0e 74static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
cb7b8ced
RW
75{
76 struct acpiphp_context *context;
cb7b8ced
RW
77
78 context = kzalloc(sizeof(*context), GFP_KERNEL);
79 if (!context)
80 return NULL;
81
cb7b8ced 82 context->refcount = 1;
be27b3dc 83 acpi_set_hp_context(adev, &context->hp, acpiphp_hotplug_notify, NULL,
edf5bf34 84 acpiphp_post_dock_fixup);
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
5ba113f7
JL
225/* Check whether the PCI device is managed by native PCIe hotplug driver */
226static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
227{
228 u32 reg32;
229 acpi_handle tmp;
230 struct acpi_pci_root *root;
231
232 /* Check whether the PCIe port supports native PCIe hotplug */
233 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
234 return false;
235 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
236 return false;
237
238 /*
239 * Check whether native PCIe hotplug has been enabled for
240 * this PCIe hierarchy.
241 */
242 tmp = acpi_find_root_bridge_handle(pdev);
243 if (!tmp)
244 return false;
245 root = acpi_pci_find_root(tmp);
246 if (!root)
247 return false;
248 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
249 return false;
250
251 return true;
252}
253
3799c5a0
RW
254/**
255 * acpiphp_add_context - Add ACPIPHP context to an ACPI device object.
256 * @handle: ACPI handle of the object to add a context to.
257 * @lvl: Not used.
258 * @data: The object's parent ACPIPHP bridge.
259 * @rv: Not used.
260 */
261static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
262 void **rv)
1da177e4 263{
cb7b8ced
RW
264 struct acpiphp_bridge *bridge = data;
265 struct acpiphp_context *context;
bbcbfc0e 266 struct acpi_device *adev;
1da177e4
LT
267 struct acpiphp_slot *slot;
268 struct acpiphp_func *newfunc;
1da177e4 269 acpi_status status = AE_OK;
bbd34fcd
RW
270 unsigned long long adr;
271 int device, function;
e8c331e9 272 struct pci_bus *pbus = bridge->pci_bus;
bbd34fcd 273 struct pci_dev *pdev = bridge->pci_dev;
3b63aaa7 274 u32 val;
1da177e4 275
dfb117b3
BH
276 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
277 if (ACPI_FAILURE(status)) {
f26ca1d6
TK
278 if (status != AE_NOT_FOUND)
279 acpi_handle_warn(handle,
280 "can't evaluate _ADR (%#x)\n", status);
dfb117b3
BH
281 return AE_OK;
282 }
bbcbfc0e
RW
283 if (acpi_bus_get_device(handle, &adev))
284 return AE_OK;
dfb117b3
BH
285
286 device = (adr >> 16) & 0xffff;
287 function = adr & 0xffff;
288
e525506f 289 acpi_lock_hp_context();
bbcbfc0e 290 context = acpiphp_init_context(adev);
cb7b8ced 291 if (!context) {
e525506f 292 acpi_unlock_hp_context();
cb7b8ced 293 acpi_handle_err(handle, "No hotplug context\n");
cb7b8ced
RW
294 return AE_NOT_EXIST;
295 }
bd4674df 296 newfunc = &context->func;
bd4674df 297 newfunc->function = function;
bda46dbb 298 newfunc->parent = bridge;
edf5bf34 299 acpi_unlock_hp_context();
cb7b8ced 300
edf5bf34
RW
301 /*
302 * If this is a dock device, its _EJ0 should be executed by the dock
303 * notify handler after calling _DCK.
304 */
305 if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
20416ea5 306 newfunc->flags = FUNC_HAS_EJ0;
1da177e4 307
ecd046da 308 if (acpi_has_method(handle, "_STA"))
1da177e4
LT
309 newfunc->flags |= FUNC_HAS_STA;
310
1da177e4 311 /* search for objects that share the same slot */
ad41dd9d 312 list_for_each_entry(slot, &bridge->slots, node)
bbd34fcd 313 if (slot->device == device)
ac372338 314 goto slot_found;
1da177e4 315
ac372338
RW
316 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
317 if (!slot) {
e525506f 318 acpi_lock_hp_context();
146fc68a 319 acpiphp_put_context(context);
e525506f 320 acpi_unlock_hp_context();
146fc68a 321 return AE_NO_MEMORY;
ac372338
RW
322 }
323
bda46dbb 324 slot->bus = bridge->pci_bus;
ac372338 325 slot->device = device;
ac372338 326 INIT_LIST_HEAD(&slot->funcs);
ac372338 327
ac372338 328 list_add_tail(&slot->node, &bridge->slots);
1da177e4 329
cc6254e0
RW
330 /*
331 * Expose slots to user space for functions that have _EJ0 or _RMV or
332 * are located in dock stations. Do not expose them for devices handled
333 * by the native PCIe hotplug (PCIeHP), becuase that code is supposed to
334 * expose slots to user space in those cases.
335 */
3b52b21f 336 if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
cc6254e0 337 && !(pdev && device_is_managed_by_native_pciehp(pdev))) {
bbd34fcd
RW
338 unsigned long long sun;
339 int retval;
ac372338 340
bbd34fcd
RW
341 bridge->nr_slots++;
342 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
343 if (ACPI_FAILURE(status))
344 sun = bridge->nr_slots;
345
bd950799 346 pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
7342798d 347 sun, pci_domain_nr(pbus), pbus->number, device);
bbd34fcd 348
7342798d 349 retval = acpiphp_register_hotplug_slot(slot, sun);
bbd34fcd 350 if (retval) {
1aaac071 351 slot->slot = NULL;
bbd34fcd
RW
352 bridge->nr_slots--;
353 if (retval == -EBUSY)
227f0647 354 pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
bbd34fcd 355 else
227f0647 356 pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
bbd34fcd
RW
357 }
358 /* Even if the slot registration fails, we can still use it. */
1da177e4
LT
359 }
360
ac372338 361 slot_found:
1da177e4
LT
362 newfunc->slot = slot;
363 list_add_tail(&newfunc->sibling, &slot->funcs);
364
3b63aaa7
JL
365 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
366 &val, 60*1000))
bc805a55 367 slot->flags |= SLOT_ENABLED;
1da177e4 368
2e862c51 369 return AE_OK;
1da177e4
LT
370}
371
1f7c164b 372static struct acpiphp_bridge *acpiphp_dev_to_bridge(struct acpi_device *adev)
42f49a6a 373{
ed13febf
RW
374 struct acpiphp_context *context;
375 struct acpiphp_bridge *bridge = NULL;
58c08628 376
e525506f 377 acpi_lock_hp_context();
3c2cc7ff 378 context = acpiphp_get_context(adev);
ed13febf
RW
379 if (context) {
380 bridge = context->bridge;
381 if (bridge)
3d54a316 382 get_bridge(bridge);
42f49a6a 383
ed13febf
RW
384 acpiphp_put_context(context);
385 }
e525506f 386 acpi_unlock_hp_context();
ed13febf 387 return bridge;
42f49a6a 388}
1da177e4 389
364d5094 390static void cleanup_bridge(struct acpiphp_bridge *bridge)
1da177e4 391{
3d54a316
JL
392 struct acpiphp_slot *slot;
393 struct acpiphp_func *func;
42f49a6a 394
3d54a316
JL
395 list_for_each_entry(slot, &bridge->slots, node) {
396 list_for_each_entry(func, &slot->funcs, sibling) {
1a699476 397 struct acpi_device *adev = func_to_acpi_device(func);
5a3bc573 398
1a699476 399 acpi_lock_hp_context();
be27b3dc 400 adev->hp->notify = NULL;
edf5bf34 401 adev->hp->fixup = NULL;
1a699476 402 acpi_unlock_hp_context();
42f49a6a 403 }
9217a984 404 slot->flags |= SLOT_IS_GOING_AWAY;
1aaac071
RW
405 if (slot->slot)
406 acpiphp_unregister_hotplug_slot(slot);
42f49a6a
RS
407 }
408
3d54a316 409 mutex_lock(&bridge_mutex);
42f49a6a 410 list_del(&bridge->list);
3d54a316 411 mutex_unlock(&bridge_mutex);
9217a984 412
e525506f 413 acpi_lock_hp_context();
9217a984 414 bridge->is_going_away = true;
e525506f 415 acpi_unlock_hp_context();
1da177e4
LT
416}
417
15a1ae74 418/**
26e6c66e 419 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
15a1ae74 420 * @bus: bus to start search with
15a1ae74
KA
421 */
422static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
423{
c6f0d5ad 424 struct pci_bus *tmp;
15a1ae74
KA
425 unsigned char max, n;
426
427 /*
428 * pci_bus_max_busnr will return the highest
429 * reserved busnr for all these children.
430 * that is equivalent to the bus->subordinate
431 * value. We don't want to use the parent's
432 * bus->subordinate value because it could have
433 * padding in it.
434 */
b918c62e 435 max = bus->busn_res.start;
15a1ae74 436
c6f0d5ad
YW
437 list_for_each_entry(tmp, &bus->children, node) {
438 n = pci_bus_max_busnr(tmp);
15a1ae74
KA
439 if (n > max)
440 max = n;
441 }
442 return max;
443}
444
d0607050
SL
445static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
446{
447 struct acpiphp_func *func;
448 union acpi_object params[2];
449 struct acpi_object_list arg_list;
450
451 list_for_each_entry(func, &slot->funcs, sibling) {
452 arg_list.count = 2;
453 arg_list.pointer = params;
454 params[0].type = ACPI_TYPE_INTEGER;
455 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
456 params[1].type = ACPI_TYPE_INTEGER;
457 params[1].integer.value = 1;
458 /* _REG is optional, we don't care about if there is failure */
5a3bc573
RW
459 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
460 NULL);
d0607050
SL
461 }
462}
463
1f96a965
YL
464static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
465{
466 struct acpiphp_func *func;
467
1f96a965
YL
468 /* quirk, or pcie could set it already */
469 if (dev->is_hotplug_bridge)
470 return;
471
1f96a965
YL
472 list_for_each_entry(func, &slot->funcs, sibling) {
473 if (PCI_FUNC(dev->devfn) == func->function) {
bbd34fcd 474 dev->is_hotplug_bridge = 1;
1f96a965
YL
475 break;
476 }
477 }
478}
3b63aaa7 479
a47d8c8e
RW
480static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
481{
482 struct acpiphp_func *func;
483
b6708fbf
RW
484 list_for_each_entry(func, &slot->funcs, sibling) {
485 struct acpi_device *adev = func_to_acpi_device(func);
a47d8c8e 486
b6708fbf
RW
487 acpi_bus_scan(adev->handle);
488 if (acpi_device_enumerated(adev))
489 acpi_device_set_power(adev, ACPI_STATE_D0);
490 }
a47d8c8e
RW
491 return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
492}
493
1da177e4 494/**
a1d0abce 495 * enable_slot - enable, configure a slot
1da177e4
LT
496 * @slot: slot to be enabled
497 *
498 * This function should be called per *physical slot*,
499 * not per each slot object in ACPI namespace.
1da177e4 500 */
10874f5a 501static void enable_slot(struct acpiphp_slot *slot)
1da177e4 502{
1da177e4 503 struct pci_dev *dev;
bda46dbb 504 struct pci_bus *bus = slot->bus;
1da177e4 505 struct acpiphp_func *func;
b91182a6 506 int max, pass;
d66ecb72 507 LIST_HEAD(add_list);
1da177e4 508
ab122590 509 acpiphp_rescan_slot(slot);
15a1ae74 510 max = acpiphp_max_busnr(bus);
42f49a6a
RS
511 for (pass = 0; pass < 2; pass++) {
512 list_for_each_entry(dev, &bus->devices, bus_list) {
513 if (PCI_SLOT(dev->devfn) != slot->device)
514 continue;
a1d0abce 515
c7a071f6 516 if (pci_is_bridge(dev)) {
42f49a6a 517 max = pci_scan_bridge(bus, dev, max, pass);
1f96a965
YL
518 if (pass && dev->subordinate) {
519 check_hotplug_bridge(slot, dev);
d66ecb72
JL
520 pcibios_resource_survey_bus(dev->subordinate);
521 __pci_bus_size_bridges(dev->subordinate,
522 &add_list);
1f96a965 523 }
c64b5eea 524 }
42f49a6a 525 }
1da177e4 526 }
d66ecb72 527 __pci_bus_assign_resources(bus, &add_list, NULL);
2dc41281 528
8e5dce35 529 acpiphp_sanitize_bus(bus);
fca6825a 530 acpiphp_set_hpp_values(bus);
d0607050 531 acpiphp_set_acpi_region(slot);
69643e48
IC
532
533 list_for_each_entry(dev, &bus->devices, bus_list) {
534 /* Assume that newly added devices are powered on already. */
535 if (!dev->is_added)
536 dev->current_state = PCI_D0;
537 }
538
42f49a6a
RS
539 pci_bus_add_devices(bus);
540
f382a086 541 slot->flags |= SLOT_ENABLED;
58c08628 542 list_for_each_entry(func, &slot->funcs, sibling) {
9d911d79
AC
543 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
544 func->function));
f382a086
AK
545 if (!dev) {
546 /* Do not set SLOT_ENABLED flag if some funcs
547 are not added. */
548 slot->flags &= (~SLOT_ENABLED);
551bcb75 549 continue;
f382a086 550 }
1da177e4 551 }
1da177e4
LT
552}
553
1da177e4 554/**
a1d0abce 555 * disable_slot - disable a slot
26e6c66e 556 * @slot: ACPI PHP slot
1da177e4 557 */
a1d0abce 558static void disable_slot(struct acpiphp_slot *slot)
1da177e4 559{
1c0c5443
RW
560 struct pci_bus *bus = slot->bus;
561 struct pci_dev *dev, *prev;
1da177e4 562 struct acpiphp_func *func;
551bcb75 563
ce29ca3e 564 /*
a1d0abce 565 * enable_slot() enumerates all functions in this device via
ce29ca3e
AK
566 * pci_scan_slot(), whether they have associated ACPI hotplug
567 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
568 * here.
569 */
1c0c5443
RW
570 list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
571 if (PCI_SLOT(dev->devfn) == slot->device)
572 pci_stop_and_remove_bus_device(dev);
600812ec 573
5a3bc573 574 list_for_each_entry(func, &slot->funcs, sibling)
bbcbfc0e 575 acpi_bus_trim(func_to_acpi_device(func));
1da177e4
LT
576
577 slot->flags &= (~SLOT_ENABLED);
1da177e4
LT
578}
579
b2118d6a 580static bool acpiphp_no_hotplug(struct acpi_device *adev)
f244d8b6 581{
f244d8b6
RW
582 return adev && adev->flags.no_hotplug;
583}
584
585static bool slot_no_hotplug(struct acpiphp_slot *slot)
586{
587 struct acpiphp_func *func;
588
589 list_for_each_entry(func, &slot->funcs, sibling)
bbcbfc0e 590 if (acpiphp_no_hotplug(func_to_acpi_device(func)))
f244d8b6
RW
591 return true;
592
593 return false;
594}
1da177e4
LT
595
596/**
597 * get_slot_status - get ACPI slot status
26e6c66e 598 * @slot: ACPI PHP slot
1da177e4 599 *
26e6c66e
RD
600 * If a slot has _STA for each function and if any one of them
601 * returned non-zero status, return it.
1da177e4 602 *
26e6c66e
RD
603 * If a slot doesn't have _STA and if any one of its functions'
604 * configuration space is configured, return 0x0f as a _STA.
1da177e4 605 *
26e6c66e 606 * Otherwise return 0.
1da177e4
LT
607 */
608static unsigned int get_slot_status(struct acpiphp_slot *slot)
609{
27663c58 610 unsigned long long sta = 0;
1da177e4
LT
611 struct acpiphp_func *func;
612
58c08628 613 list_for_each_entry(func, &slot->funcs, sibling) {
1da177e4 614 if (func->flags & FUNC_HAS_STA) {
5a3bc573
RW
615 acpi_status status;
616
617 status = acpi_evaluate_integer(func_to_handle(func),
618 "_STA", NULL, &sta);
1da177e4
LT
619 if (ACPI_SUCCESS(status) && sta)
620 break;
621 } else {
5a3bc573
RW
622 u32 dvid;
623
bda46dbb 624 pci_bus_read_config_dword(slot->bus,
1da177e4
LT
625 PCI_DEVFN(slot->device,
626 func->function),
627 PCI_VENDOR_ID, &dvid);
628 if (dvid != 0xffffffff) {
629 sta = ACPI_STA_ALL;
630 break;
631 }
632 }
633 }
634
635 return (unsigned int)sta;
636}
637
72820594
MW
638static inline bool device_status_valid(unsigned int sta)
639{
640 /*
641 * ACPI spec says that _STA may return bit 0 clear with bit 3 set
642 * if the device is valid but does not require a device driver to be
643 * loaded (Section 6.3.7 of ACPI 5.0A).
644 */
645 unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
646 return (sta & mask) == mask;
647}
648
4ebe3450
RW
649/**
650 * trim_stale_devices - remove PCI devices that are not responding.
651 * @dev: PCI device to start walking the hierarchy from.
652 */
653static void trim_stale_devices(struct pci_dev *dev)
654{
4dc3082d 655 struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
4ebe3450
RW
656 struct pci_bus *bus = dev->subordinate;
657 bool alive = false;
658
4dc3082d 659 if (adev) {
4ebe3450
RW
660 acpi_status status;
661 unsigned long long sta;
662
b2118d6a 663 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
72820594 664 alive = (ACPI_SUCCESS(status) && device_status_valid(sta))
b2118d6a 665 || acpiphp_no_hotplug(adev);
4ebe3450 666 }
b8a62d54
RW
667 if (!alive)
668 alive = pci_device_is_present(dev);
4ebe3450 669
4ebe3450
RW
670 if (!alive) {
671 pci_stop_and_remove_bus_device(dev);
4dc3082d
RW
672 if (adev)
673 acpi_bus_trim(adev);
4ebe3450
RW
674 } else if (bus) {
675 struct pci_dev *child, *tmp;
676
677 /* The device is a bridge. so check the bus below it. */
678 pm_runtime_get_sync(&dev->dev);
2d7c1b77 679 list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
4ebe3450
RW
680 trim_stale_devices(child);
681
682 pm_runtime_put(&dev->dev);
683 }
684}
685
1da177e4
LT
686/**
687 * acpiphp_check_bridge - re-enumerate devices
26e6c66e 688 * @bridge: where to begin re-enumeration
1da177e4
LT
689 *
690 * Iterate over all slots under this bridge and make sure that if a
691 * card is present they are enabled, and if not they are disabled.
692 */
4ebe3450 693static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1da177e4
LT
694{
695 struct acpiphp_slot *slot;
1da177e4 696
9217a984
RW
697 /* Bail out if the bridge is going away. */
698 if (bridge->is_going_away)
699 return;
700
ad41dd9d 701 list_for_each_entry(slot, &bridge->slots, node) {
4ebe3450
RW
702 struct pci_bus *bus = slot->bus;
703 struct pci_dev *dev, *tmp;
704
f244d8b6
RW
705 if (slot_no_hotplug(slot)) {
706 ; /* do nothing */
72820594 707 } else if (device_status_valid(get_slot_status(slot))) {
4ebe3450 708 /* remove stale devices if any */
2d7c1b77
RW
709 list_for_each_entry_safe_reverse(dev, tmp,
710 &bus->devices, bus_list)
4ebe3450
RW
711 if (PCI_SLOT(dev->devfn) == slot->device)
712 trim_stale_devices(dev);
713
714 /* configure all functions */
a1d0abce 715 enable_slot(slot);
1da177e4 716 } else {
a1d0abce 717 disable_slot(slot);
1da177e4
LT
718 }
719 }
1da177e4
LT
720}
721
fca6825a 722static void acpiphp_set_hpp_values(struct pci_bus *bus)
8e7561cf 723{
8e7561cf
RS
724 struct pci_dev *dev;
725
e81995bb
BH
726 list_for_each_entry(dev, &bus->devices, bus_list)
727 pci_configure_slot(dev);
8e7561cf
RS
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{
d65eba6a 736 struct pci_dev *dev, *tmp;
8e7561cf
RS
737 int i;
738 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
739
2d7c1b77 740 list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
8e7561cf
RS
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 */
210647af 747 pci_stop_and_remove_bus_device(dev);
8e7561cf
RS
748 break;
749 }
750 }
751 }
752}
753
1da177e4
LT
754/*
755 * ACPI event handlers
756 */
757
1f7c164b 758void acpiphp_check_host_bridge(struct acpi_device *adev)
3f327e39
YL
759{
760 struct acpiphp_bridge *bridge;
761
1f7c164b 762 bridge = acpiphp_dev_to_bridge(adev);
3f327e39 763 if (bridge) {
d42f5da2
RW
764 pci_lock_rescan_remove();
765
3f327e39 766 acpiphp_check_bridge(bridge);
d42f5da2
RW
767
768 pci_unlock_rescan_remove();
3f327e39
YL
769 put_bridge(bridge);
770 }
3f327e39
YL
771}
772
9217a984
RW
773static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
774
d3a1ebb0 775static void hotplug_event(u32 type, struct acpiphp_context *context)
1da177e4 776{
3c2cc7ff 777 acpi_handle handle = context->hp.self->handle;
bd4674df 778 struct acpiphp_func *func = &context->func;
b75cece1 779 struct acpiphp_slot *slot = func->slot;
1da177e4 780 struct acpiphp_bridge *bridge;
6af8bef1 781
e525506f 782 acpi_lock_hp_context();
c8ebcf1f 783 bridge = context->bridge;
43e5c091
RW
784 if (bridge)
785 get_bridge(bridge);
1da177e4 786
e525506f 787 acpi_unlock_hp_context();
3757b948 788
f41b3261 789 pci_lock_rescan_remove();
1da177e4
LT
790
791 switch (type) {
792 case ACPI_NOTIFY_BUS_CHECK:
793 /* bus re-enumerate */
1d4a5b61 794 acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
b75cece1 795 if (bridge)
43e5c091 796 acpiphp_check_bridge(bridge);
b75cece1 797 else if (!(slot->flags & SLOT_IS_GOING_AWAY))
a1d0abce 798 enable_slot(slot);
4ebe3450 799
1da177e4
LT
800 break;
801
802 case ACPI_NOTIFY_DEVICE_CHECK:
803 /* device check */
1d4a5b61 804 acpi_handle_debug(handle, "Device check in %s()\n", __func__);
a47d8c8e 805 if (bridge) {
43e5c091 806 acpiphp_check_bridge(bridge);
b75cece1 807 } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
a47d8c8e
RW
808 /*
809 * Check if anything has changed in the slot and rescan
810 * from the parent if that's the case.
811 */
661b4064 812 if (acpiphp_rescan_slot(slot))
a47d8c8e
RW
813 acpiphp_check_bridge(func->parent);
814 }
1da177e4
LT
815 break;
816
1da177e4
LT
817 case ACPI_NOTIFY_EJECT_REQUEST:
818 /* request device eject */
1d4a5b61 819 acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
b75cece1 820 acpiphp_disable_and_eject_slot(slot);
1da177e4 821 break;
1da177e4 822 }
6af8bef1 823
f41b3261 824 pci_unlock_rescan_remove();
43e5c091
RW
825 if (bridge)
826 put_bridge(bridge);
21a31013
RW
827}
828
be27b3dc 829static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
6af8bef1 830{
87831273 831 struct acpiphp_context *context;
87831273 832
edf5bf34
RW
833 context = acpiphp_grab_context(adev);
834 if (!context)
3c2cc7ff 835 return -ENODATA;
1b360f44 836
3c2cc7ff 837 hotplug_event(type, context);
edf5bf34 838 acpiphp_let_context_go(context);
3c2cc7ff 839 return 0;
8e7561cf 840}
1da177e4 841
454481ad
RW
842/**
843 * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
844 * @bus: PCI bus to enumerate the slots for.
845 *
846 * A "slot" is an object associated with a PCI device number. All functions
847 * (PCI devices) with the same bus and device number belong to the same slot.
1da177e4 848 */
be1c9de9 849void acpiphp_enumerate_slots(struct pci_bus *bus)
1da177e4 850{
3b63aaa7 851 struct acpiphp_bridge *bridge;
bbcbfc0e 852 struct acpi_device *adev;
2552002a
RW
853 acpi_handle handle;
854 acpi_status status;
1da177e4 855
3b63aaa7
JL
856 if (acpiphp_disabled)
857 return;
1da177e4 858
bbcbfc0e
RW
859 adev = ACPI_COMPANION(bus->bridge);
860 if (!adev)
3b63aaa7 861 return;
1da177e4 862
bbcbfc0e 863 handle = adev->handle;
3b63aaa7 864 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
cb7b8ced
RW
865 if (!bridge) {
866 acpi_handle_err(handle, "No memory for bridge object\n");
3b63aaa7
JL
867 return;
868 }
869
ad41dd9d 870 INIT_LIST_HEAD(&bridge->slots);
3d54a316 871 kref_init(&bridge->ref);
3b63aaa7
JL
872 bridge->pci_dev = pci_dev_get(bus->self);
873 bridge->pci_bus = bus;
874
875 /*
876 * Grab a ref to the subordinate PCI bus in case the bus is
877 * removed via PCI core logical hotplug. The ref pins the bus
878 * (which we access during module unload).
879 */
880 get_device(&bus->dev);
881
bbd34fcd
RW
882 if (!pci_is_root_bus(bridge->pci_bus)) {
883 struct acpiphp_context *context;
884
885 /*
886 * This bridge should have been registered as a hotplug function
fd3cfebe
RW
887 * under its parent, so the context should be there, unless the
888 * parent is going to be handled by pciehp, in which case this
889 * bridge is not interesting to us either.
bbd34fcd 890 */
e525506f 891 acpi_lock_hp_context();
3c2cc7ff 892 context = acpiphp_get_context(adev);
fd3cfebe 893 if (!context) {
e525506f 894 acpi_unlock_hp_context();
bbd34fcd 895 put_device(&bus->dev);
5d449457 896 pci_dev_put(bridge->pci_dev);
bbd34fcd
RW
897 kfree(bridge);
898 return;
899 }
900 bridge->context = context;
901 context->bridge = bridge;
902 /* Get a reference to the parent bridge. */
bda46dbb 903 get_bridge(context->func.parent);
e525506f 904 acpi_unlock_hp_context();
bbd34fcd
RW
905 }
906
3799c5a0 907 /* Must be added to the list prior to calling acpiphp_add_context(). */
2552002a
RW
908 mutex_lock(&bridge_mutex);
909 list_add(&bridge->list, &bridge_list);
910 mutex_unlock(&bridge_mutex);
911
912 /* register all slot objects under this bridge */
89373a55 913 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
3799c5a0 914 acpiphp_add_context, NULL, bridge, NULL);
2552002a 915 if (ACPI_FAILURE(status)) {
89373a55 916 acpi_handle_err(handle, "failed to register slots\n");
bbd34fcd
RW
917 cleanup_bridge(bridge);
918 put_bridge(bridge);
2552002a 919 }
3b63aaa7
JL
920}
921
454481ad
RW
922/**
923 * acpiphp_remove_slots - Remove slot objects associated with a given bus.
924 * @bus: PCI bus to remove the slot objects for.
925 */
3b63aaa7 926void acpiphp_remove_slots(struct pci_bus *bus)
1da177e4 927{
ff181e5a 928 struct acpiphp_bridge *bridge;
3b63aaa7
JL
929
930 if (acpiphp_disabled)
931 return;
932
ff181e5a
RW
933 mutex_lock(&bridge_mutex);
934 list_for_each_entry(bridge, &bridge_list, list)
3b63aaa7 935 if (bridge->pci_bus == bus) {
ff181e5a 936 mutex_unlock(&bridge_mutex);
3b63aaa7 937 cleanup_bridge(bridge);
3d54a316 938 put_bridge(bridge);
ff181e5a 939 return;
3b63aaa7 940 }
ff181e5a
RW
941
942 mutex_unlock(&bridge_mutex);
1da177e4
LT
943}
944
1da177e4
LT
945/**
946 * acpiphp_enable_slot - power on slot
26e6c66e 947 * @slot: ACPI PHP slot
1da177e4
LT
948 */
949int acpiphp_enable_slot(struct acpiphp_slot *slot)
950{
9217a984
RW
951 pci_lock_rescan_remove();
952
953 if (slot->flags & SLOT_IS_GOING_AWAY)
954 return -ENODEV;
955
bc805a55 956 /* configure all functions */
55502ddb 957 if (!(slot->flags & SLOT_ENABLED))
a1d0abce 958 enable_slot(slot);
55502ddb 959
9217a984 960 pci_unlock_rescan_remove();
a1d0abce 961 return 0;
1da177e4
LT
962}
963
1da177e4 964/**
ad21d2d0 965 * acpiphp_disable_and_eject_slot - power off and eject slot
26e6c66e 966 * @slot: ACPI PHP slot
1da177e4 967 */
9217a984 968static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
1da177e4 969{
ad21d2d0 970 struct acpiphp_func *func;
9217a984
RW
971
972 if (slot->flags & SLOT_IS_GOING_AWAY)
973 return -ENODEV;
1da177e4 974
1da177e4 975 /* unconfigure all functions */
a1d0abce 976 disable_slot(slot);
1da177e4 977
ad21d2d0
MW
978 list_for_each_entry(func, &slot->funcs, sibling)
979 if (func->flags & FUNC_HAS_EJ0) {
980 acpi_handle handle = func_to_handle(func);
981
982 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
983 acpi_handle_err(handle, "_EJ0 failed\n");
984
985 break;
986 }
987
9217a984 988 return 0;
1da177e4
LT
989}
990
9217a984
RW
991int acpiphp_disable_slot(struct acpiphp_slot *slot)
992{
993 int ret;
994
21369c77
RW
995 /*
996 * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
997 * acpiphp_disable_and_eject_slot() will be synchronized properly.
998 */
999 acpi_scan_lock_acquire();
9217a984
RW
1000 pci_lock_rescan_remove();
1001 ret = acpiphp_disable_and_eject_slot(slot);
1002 pci_unlock_rescan_remove();
21369c77 1003 acpi_scan_lock_release();
9217a984
RW
1004 return ret;
1005}
1da177e4
LT
1006
1007/*
1008 * slot enabled: 1
1009 * slot disabled: 0
1010 */
1011u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1012{
bc805a55 1013 return (slot->flags & SLOT_ENABLED);
1da177e4
LT
1014}
1015
1da177e4 1016/*
35ae61a0
MT
1017 * latch open: 1
1018 * latch closed: 0
1da177e4
LT
1019 */
1020u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1021{
1ad3790a 1022 return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
1da177e4
LT
1023}
1024
1da177e4
LT
1025/*
1026 * adapter presence : 1
1027 * absence : 0
1028 */
1029u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1030{
1ad3790a 1031 return !!get_slot_status(slot);
1da177e4 1032}
This page took 0.845526 seconds and 5 git commands to generate.