]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/acpiphp_glue.c
Merge git://git.infradead.org/mtd-2.6
[mirror_ubuntu-bionic-kernel.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
1da177e4
LT
42#include <linux/init.h>
43#include <linux/module.h>
44
45#include <linux/kernel.h>
46#include <linux/pci.h>
7a54f25c 47#include <linux/pci_hotplug.h>
e8c331e9 48#include <linux/pci-acpi.h>
6aa4cdd0 49#include <linux/mutex.h>
1da177e4
LT
50
51#include "../pci.h"
1da177e4
LT
52#include "acpiphp.h"
53
54static LIST_HEAD(bridge_list);
600812ec
ST
55static LIST_HEAD(ioapic_list);
56static DEFINE_SPINLOCK(ioapic_list_lock);
1da177e4
LT
57
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
8e5dce35
KA
61static void acpiphp_sanitize_bus(struct pci_bus *bus);
62static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
2b85e130 63static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
8e5dce35 64
5a340ed8 65/* callback routine to check for the existence of a pci dock device */
4e8662bb
KA
66static acpi_status
67is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68{
69 int *count = (int *)context;
70
71 if (is_dock_device(handle)) {
72 (*count)++;
73 return AE_CTRL_TERMINATE;
74 } else {
75 return AE_OK;
76 }
77}
78
4e8662bb
KA
79/*
80 * the _DCK method can do funny things... and sometimes not
81 * hah-hah funny.
82 *
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
85 */
86static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 void *v)
88{
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
91 u32 buses;
92
93 if (!bus->self)
94 return NOTIFY_OK;
95
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
98 */
99 pci_read_config_dword(bus->self,
100 PCI_PRIMARY_BUS,
101 &buses);
102
103 if (((buses >> 8) & 0xff) != bus->secondary) {
104 buses = (buses & 0xff000000)
2a9d3521
AC
105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->secondary) << 8)
107 | ((unsigned int)(bus->subordinate) << 16);
4e8662bb
KA
108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 }
110 return NOTIFY_OK;
111}
112
113
1253f7aa
SL
114static struct acpi_dock_ops acpiphp_dock_ops = {
115 .handler = handle_hotplug_event_func,
116};
1da177e4
LT
117
118/* callback routine to register each ACPI PCI slot object */
119static acpi_status
120register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
121{
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
125 acpi_handle tmp;
126 acpi_status status = AE_OK;
27663c58 127 unsigned long long adr, sun;
e27da381 128 int device, function, retval;
e8c331e9 129 struct pci_bus *pbus = bridge->pci_bus;
9d911d79 130 struct pci_dev *pdev;
1da177e4 131
e8c331e9 132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
1da177e4
LT
133 return AE_OK;
134
56ee325e 135 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
1da177e4
LT
136 device = (adr >> 16) & 0xffff;
137 function = adr & 0xffff;
138
f5afe806 139 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
1da177e4
LT
140 if (!newfunc)
141 return AE_NO_MEMORY;
1da177e4
LT
142
143 INIT_LIST_HEAD(&newfunc->sibling);
144 newfunc->handle = handle;
145 newfunc->function = function;
56ee325e
MG
146
147 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
20416ea5 148 newfunc->flags = FUNC_HAS_EJ0;
1da177e4
LT
149
150 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
151 newfunc->flags |= FUNC_HAS_STA;
152
153 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
154 newfunc->flags |= FUNC_HAS_PS0;
155
156 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
157 newfunc->flags |= FUNC_HAS_PS3;
158
4e8662bb 159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
20416ea5 160 newfunc->flags |= FUNC_HAS_DCK;
20416ea5 161
1da177e4 162 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
95b38b3f
KA
163 if (ACPI_FAILURE(status)) {
164 /*
165 * use the count of the number of slots we've found
166 * for the number of the slot
167 */
168 sun = bridge->nr_slots+1;
169 }
1da177e4
LT
170
171 /* search for objects that share the same slot */
172 for (slot = bridge->slots; slot; slot = slot->next)
173 if (slot->device == device) {
174 if (slot->sun != sun)
175 warn("sibling found, but _SUN doesn't match!\n");
176 break;
177 }
178
179 if (!slot) {
f5afe806 180 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
1da177e4
LT
181 if (!slot) {
182 kfree(newfunc);
183 return AE_NO_MEMORY;
184 }
185
1da177e4 186 slot->bridge = bridge;
1da177e4
LT
187 slot->device = device;
188 slot->sun = sun;
189 INIT_LIST_HEAD(&slot->funcs);
6aa4cdd0 190 mutex_init(&slot->crit_sect);
1da177e4
LT
191
192 slot->next = bridge->slots;
193 bridge->slots = slot;
194
195 bridge->nr_slots++;
196
b6adc195 197 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
e8c331e9 198 slot->sun, pci_domain_nr(pbus), pbus->number, device);
e27da381
MT
199 retval = acpiphp_register_hotplug_slot(slot);
200 if (retval) {
f46753c5 201 if (retval == -EBUSY)
b6adc195 202 warn("Slot %llu already registered by another "
f46753c5
AC
203 "hotplug driver\n", slot->sun);
204 else
205 warn("acpiphp_register_hotplug_slot failed "
206 "(err code = 0x%x)\n", retval);
e27da381
MT
207 goto err_exit;
208 }
1da177e4
LT
209 }
210
211 newfunc->slot = slot;
212 list_add_tail(&newfunc->sibling, &slot->funcs);
213
9d911d79
AC
214 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
215 if (pdev) {
1da177e4 216 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
9d911d79 217 pci_dev_put(pdev);
1da177e4
LT
218 }
219
4e8662bb
KA
220 if (is_dock_device(handle)) {
221 /* we don't want to call this device's _EJ0
222 * because we want the dock notify handler
223 * to call it after it calls _DCK
20416ea5
KA
224 */
225 newfunc->flags &= ~FUNC_HAS_EJ0;
4e8662bb 226 if (register_hotplug_dock_device(handle,
1253f7aa 227 &acpiphp_dock_ops, newfunc))
4e8662bb
KA
228 dbg("failed to register dock device\n");
229
230 /* we need to be notified when dock events happen
231 * outside of the hotplug operation, since we may
232 * need to do fixups before we can hotplug.
233 */
234 newfunc->nb.notifier_call = post_dock_fixups;
235 if (register_dock_notifier(&newfunc->nb))
236 dbg("failed to register a dock notifier");
20416ea5
KA
237 }
238
1da177e4 239 /* install notify handler */
20416ea5
KA
240 if (!(newfunc->flags & FUNC_HAS_DCK)) {
241 status = acpi_install_notify_handler(handle,
1da177e4
LT
242 ACPI_SYSTEM_NOTIFY,
243 handle_hotplug_event_func,
244 newfunc);
245
20416ea5
KA
246 if (ACPI_FAILURE(status))
247 err("failed to register interrupt notify handler\n");
248 } else
249 status = AE_OK;
1da177e4 250
20416ea5 251 return status;
e27da381
MT
252
253 err_exit:
254 bridge->nr_slots--;
255 bridge->slots = slot->next;
256 kfree(slot);
257 kfree(newfunc);
258
259 return AE_OK;
1da177e4
LT
260}
261
262
263/* see if it's worth looking at this bridge */
e8c331e9 264static int detect_ejectable_slots(struct pci_bus *pbus)
1da177e4 265{
e8c331e9
KK
266 int found = acpi_pci_detect_ejectable(pbus);
267 if (!found) {
268 acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
d45e0855
JB
269 if (!bridge_handle)
270 return 0;
e8c331e9
KK
271 acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
272 is_pci_dock_device, (void *)&found, NULL);
273 }
274 return found;
1da177e4
LT
275}
276
277
1da177e4
LT
278/* decode ACPI 2.0 _HPP hot plug parameters */
279static void decode_hpp(struct acpiphp_bridge *bridge)
280{
281 acpi_status status;
1da177e4 282
7430e34c 283 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
e22b7350
KK
284 if (ACPI_FAILURE(status) ||
285 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
783c49fc 286 /* use default numbers */
e22b7350
KK
287 printk(KERN_WARNING
288 "%s: Could not get hotplug parameters. Use defaults\n",
66bef8c0 289 __func__);
e22b7350
KK
290 bridge->hpp.t0 = &bridge->hpp.type0_data;
291 bridge->hpp.t0->revision = 0;
292 bridge->hpp.t0->cache_line_size = 0x10;
293 bridge->hpp.t0->latency_timer = 0x40;
294 bridge->hpp.t0->enable_serr = 0;
295 bridge->hpp.t0->enable_perr = 0;
1da177e4 296 }
1da177e4
LT
297}
298
299
783c49fc 300
1da177e4
LT
301/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
302static void init_bridge_misc(struct acpiphp_bridge *bridge)
303{
304 acpi_status status;
305
306 /* decode ACPI 2.0 _HPP (hot plug parameters) */
307 decode_hpp(bridge);
308
e27da381
MT
309 /* must be added to the list prior to calling register_slot */
310 list_add(&bridge->list, &bridge_list);
311
1da177e4
LT
312 /* register all slot objects under this bridge */
313 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
314 register_slot, bridge, NULL);
e27da381
MT
315 if (ACPI_FAILURE(status)) {
316 list_del(&bridge->list);
317 return;
318 }
1da177e4
LT
319
320 /* install notify handler */
8e7561cf 321 if (bridge->type != BRIDGE_TYPE_HOST) {
551bcb75
MT
322 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
323 status = acpi_remove_notify_handler(bridge->func->handle,
324 ACPI_SYSTEM_NOTIFY,
325 handle_hotplug_event_func);
326 if (ACPI_FAILURE(status))
327 err("failed to remove notify handler\n");
328 }
8e7561cf 329 status = acpi_install_notify_handler(bridge->handle,
1da177e4
LT
330 ACPI_SYSTEM_NOTIFY,
331 handle_hotplug_event_bridge,
332 bridge);
333
8e7561cf
RS
334 if (ACPI_FAILURE(status)) {
335 err("failed to register interrupt notify handler\n");
336 }
1da177e4 337 }
1da177e4
LT
338}
339
340
551bcb75
MT
341/* find acpiphp_func from acpiphp_bridge */
342static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
343{
344 struct list_head *node, *l;
345 struct acpiphp_bridge *bridge;
346 struct acpiphp_slot *slot;
347 struct acpiphp_func *func;
348
349 list_for_each(node, &bridge_list) {
350 bridge = list_entry(node, struct acpiphp_bridge, list);
351 for (slot = bridge->slots; slot; slot = slot->next) {
352 list_for_each(l, &slot->funcs) {
353 func = list_entry(l, struct acpiphp_func,
354 sibling);
355 if (func->handle == handle)
356 return func;
357 }
358 }
359 }
360
361 return NULL;
362}
363
364
365static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
366{
367 acpi_handle dummy_handle;
368
369 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
370 "_STA", &dummy_handle)))
371 bridge->flags |= BRIDGE_HAS_STA;
372
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_EJ0", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_EJ0;
376
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_PS0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_PS0;
380
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS3", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS3;
384
385 /* is this ejectable p2p bridge? */
386 if (bridge->flags & BRIDGE_HAS_EJ0) {
387 struct acpiphp_func *func;
388
389 dbg("found ejectable p2p bridge\n");
390
391 /* make link between PCI bridge and PCI function */
392 func = acpiphp_bridge_handle_to_function(bridge->handle);
393 if (!func)
394 return;
395 bridge->func = func;
396 func->bridge = bridge;
397 }
398}
399
400
1da177e4 401/* allocate and initialize host bridge data structure */
42f49a6a 402static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
1da177e4 403{
1da177e4
LT
404 struct acpiphp_bridge *bridge;
405
f5afe806 406 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1da177e4
LT
407 if (bridge == NULL)
408 return;
409
1da177e4
LT
410 bridge->type = BRIDGE_TYPE_HOST;
411 bridge->handle = handle;
1da177e4 412
42f49a6a 413 bridge->pci_bus = pci_bus;
1da177e4
LT
414
415 spin_lock_init(&bridge->res_lock);
416
1da177e4
LT
417 init_bridge_misc(bridge);
418}
419
420
421/* allocate and initialize PCI-to-PCI bridge data structure */
42f49a6a 422static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
1da177e4
LT
423{
424 struct acpiphp_bridge *bridge;
1da177e4 425
f5afe806 426 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1da177e4
LT
427 if (bridge == NULL) {
428 err("out of memory\n");
429 return;
430 }
431
1da177e4
LT
432 bridge->type = BRIDGE_TYPE_P2P;
433 bridge->handle = handle;
551bcb75 434 config_p2p_bridge_flags(bridge);
1da177e4 435
42f49a6a
RS
436 bridge->pci_dev = pci_dev_get(pci_dev);
437 bridge->pci_bus = pci_dev->subordinate;
1da177e4
LT
438 if (!bridge->pci_bus) {
439 err("This is not a PCI-to-PCI bridge!\n");
42f49a6a 440 goto err;
1da177e4
LT
441 }
442
5d4a4b25
AC
443 /*
444 * Grab a ref to the subordinate PCI bus in case the bus is
445 * removed via PCI core logical hotplug. The ref pins the bus
446 * (which we access during module unload).
447 */
448 get_device(&bridge->pci_bus->dev);
1da177e4
LT
449 spin_lock_init(&bridge->res_lock);
450
1da177e4 451 init_bridge_misc(bridge);
42f49a6a
RS
452 return;
453 err:
454 pci_dev_put(pci_dev);
455 kfree(bridge);
456 return;
1da177e4
LT
457}
458
459
460/* callback routine to find P2P bridges */
461static acpi_status
462find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
463{
464 acpi_status status;
465 acpi_handle dummy_handle;
27663c58 466 unsigned long long tmp;
42f49a6a 467 int device, function;
1da177e4 468 struct pci_dev *dev;
42f49a6a 469 struct pci_bus *pci_bus = context;
1da177e4
LT
470
471 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
472 if (ACPI_FAILURE(status))
473 return AE_OK; /* continue */
474
475 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
476 if (ACPI_FAILURE(status)) {
66bef8c0 477 dbg("%s: _ADR evaluation failure\n", __func__);
1da177e4
LT
478 return AE_OK;
479 }
480
481 device = (tmp >> 16) & 0xffff;
482 function = tmp & 0xffff;
483
42f49a6a 484 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
1da177e4 485
42f49a6a
RS
486 if (!dev || !dev->subordinate)
487 goto out;
1da177e4
LT
488
489 /* check if this bridge has ejectable slots */
e8c331e9 490 if ((detect_ejectable_slots(dev->subordinate) > 0)) {
1da177e4 491 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
42f49a6a 492 add_p2p_bridge(handle, dev);
1da177e4
LT
493 }
494
7c8f25da
KK
495 /* search P2P bridges under this p2p bridge */
496 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
497 find_p2p_bridge, dev->subordinate, NULL);
498 if (ACPI_FAILURE(status))
551bcb75 499 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
7c8f25da 500
42f49a6a
RS
501 out:
502 pci_dev_put(dev);
1da177e4
LT
503 return AE_OK;
504}
505
506
507/* find hot-pluggable slots, and then find P2P bridge */
508static int add_bridge(acpi_handle handle)
509{
510 acpi_status status;
27663c58 511 unsigned long long tmp;
1da177e4
LT
512 int seg, bus;
513 acpi_handle dummy_handle;
42f49a6a 514 struct pci_bus *pci_bus;
1da177e4
LT
515
516 /* if the bridge doesn't have _STA, we assume it is always there */
517 status = acpi_get_handle(handle, "_STA", &dummy_handle);
518 if (ACPI_SUCCESS(status)) {
519 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
520 if (ACPI_FAILURE(status)) {
66bef8c0 521 dbg("%s: _STA evaluation failure\n", __func__);
1da177e4
LT
522 return 0;
523 }
524 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
525 /* don't register this object */
526 return 0;
527 }
528
529 /* get PCI segment number */
530 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
531
532 seg = ACPI_SUCCESS(status) ? tmp : 0;
533
534 /* get PCI bus number */
535 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
536
537 if (ACPI_SUCCESS(status)) {
538 bus = tmp;
539 } else {
540 warn("can't get bus number, assuming 0\n");
541 bus = 0;
542 }
543
42f49a6a
RS
544 pci_bus = pci_find_bus(seg, bus);
545 if (!pci_bus) {
546 err("Can't find bus %04x:%02x\n", seg, bus);
547 return 0;
548 }
549
1da177e4 550 /* check if this bridge has ejectable slots */
e8c331e9 551 if (detect_ejectable_slots(pci_bus) > 0) {
1da177e4 552 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
42f49a6a 553 add_host_bridge(handle, pci_bus);
1da177e4
LT
554 }
555
1da177e4
LT
556 /* search P2P bridges under this host bridge */
557 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
42f49a6a 558 find_p2p_bridge, pci_bus, NULL);
1da177e4
LT
559
560 if (ACPI_FAILURE(status))
551bcb75 561 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
1da177e4
LT
562
563 return 0;
564}
565
42f49a6a
RS
566static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
567{
568 struct list_head *head;
569 list_for_each(head, &bridge_list) {
570 struct acpiphp_bridge *bridge = list_entry(head,
571 struct acpiphp_bridge, list);
572 if (bridge->handle == handle)
573 return bridge;
574 }
575
576 return NULL;
577}
1da177e4 578
364d5094 579static void cleanup_bridge(struct acpiphp_bridge *bridge)
1da177e4 580{
42f49a6a 581 struct list_head *list, *tmp;
42f49a6a
RS
582 struct acpiphp_slot *slot;
583 acpi_status status;
364d5094 584 acpi_handle handle = bridge->handle;
42f49a6a
RS
585
586 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
587 handle_hotplug_event_bridge);
588 if (ACPI_FAILURE(status))
589 err("failed to remove notify handler\n");
590
551bcb75
MT
591 if ((bridge->type != BRIDGE_TYPE_HOST) &&
592 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
593 status = acpi_install_notify_handler(bridge->func->handle,
594 ACPI_SYSTEM_NOTIFY,
595 handle_hotplug_event_func,
596 bridge->func);
597 if (ACPI_FAILURE(status))
598 err("failed to install interrupt notify handler\n");
599 }
600
42f49a6a
RS
601 slot = bridge->slots;
602 while (slot) {
603 struct acpiphp_slot *next = slot->next;
604 list_for_each_safe (list, tmp, &slot->funcs) {
605 struct acpiphp_func *func;
606 func = list_entry(list, struct acpiphp_func, sibling);
4e8662bb
KA
607 if (is_dock_device(func->handle)) {
608 unregister_hotplug_dock_device(func->handle);
609 unregister_dock_notifier(&func->nb);
610 }
20416ea5
KA
611 if (!(func->flags & FUNC_HAS_DCK)) {
612 status = acpi_remove_notify_handler(func->handle,
42f49a6a
RS
613 ACPI_SYSTEM_NOTIFY,
614 handle_hotplug_event_func);
20416ea5
KA
615 if (ACPI_FAILURE(status))
616 err("failed to remove notify handler\n");
617 }
42f49a6a
RS
618 list_del(list);
619 kfree(func);
620 }
e27da381
MT
621 acpiphp_unregister_hotplug_slot(slot);
622 list_del(&slot->funcs);
42f49a6a
RS
623 kfree(slot);
624 slot = next;
625 }
626
5d4a4b25
AC
627 /*
628 * Only P2P bridges have a pci_dev
629 */
630 if (bridge->pci_dev)
631 put_device(&bridge->pci_bus->dev);
632
42f49a6a
RS
633 pci_dev_put(bridge->pci_dev);
634 list_del(&bridge->list);
635 kfree(bridge);
1da177e4
LT
636}
637
364d5094
RS
638static acpi_status
639cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
640{
641 struct acpiphp_bridge *bridge;
642
551bcb75
MT
643 /* cleanup p2p bridges under this P2P bridge
644 in a depth-first manner */
645 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
646 cleanup_p2p_bridge, NULL, NULL);
647
a13307ce
AC
648 bridge = acpiphp_handle_to_bridge(handle);
649 if (bridge)
650 cleanup_bridge(bridge);
651
364d5094
RS
652 return AE_OK;
653}
654
655static void remove_bridge(acpi_handle handle)
656{
657 struct acpiphp_bridge *bridge;
658
551bcb75
MT
659 /* cleanup p2p bridges under this host bridge
660 in a depth-first manner */
661 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
662 (u32)1, cleanup_p2p_bridge, NULL, NULL);
663
a13307ce
AC
664 /*
665 * On root bridges with hotplug slots directly underneath (ie,
666 * no p2p bridge inbetween), we call cleanup_bridge().
667 *
668 * The else clause cleans up root bridges that either had no
669 * hotplug slots at all, or had a p2p bridge underneath.
670 */
364d5094 671 bridge = acpiphp_handle_to_bridge(handle);
551bcb75 672 if (bridge)
364d5094 673 cleanup_bridge(bridge);
a13307ce
AC
674 else
675 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
676 handle_hotplug_event_bridge);
364d5094 677}
1da177e4 678
a0d399a8
KK
679static struct pci_dev * get_apic_pci_info(acpi_handle handle)
680{
a0d399a8
KK
681 struct pci_dev *dev;
682
d6aa484c 683 dev = acpi_get_pci_dev(handle);
a0d399a8
KK
684 if (!dev)
685 return NULL;
686
687 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
688 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
689 {
690 pci_dev_put(dev);
691 return NULL;
692 }
693
694 return dev;
695}
696
697static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
698{
699 acpi_status status;
700 int result = -1;
27663c58 701 unsigned long long gsb;
a0d399a8
KK
702 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
703 union acpi_object *obj;
704 void *table;
705
706 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
707 if (ACPI_SUCCESS(status)) {
708 *gsi_base = (u32)gsb;
709 return 0;
710 }
711
712 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
713 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
714 return -1;
715
716 obj = buffer.pointer;
717 if (obj->type != ACPI_TYPE_BUFFER)
718 goto out;
719
720 table = obj->buffer.pointer;
15a58ed1
AS
721 switch (((struct acpi_subtable_header *)table)->type) {
722 case ACPI_MADT_TYPE_IO_SAPIC:
723 *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
a0d399a8
KK
724 result = 0;
725 break;
15a58ed1
AS
726 case ACPI_MADT_TYPE_IO_APIC:
727 *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
a0d399a8
KK
728 result = 0;
729 break;
730 default:
731 break;
732 }
733 out:
81b26bca 734 kfree(buffer.pointer);
a0d399a8
KK
735 return result;
736}
737
738static acpi_status
739ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
740{
741 acpi_status status;
27663c58 742 unsigned long long sta;
a0d399a8
KK
743 acpi_handle tmp;
744 struct pci_dev *pdev;
745 u32 gsi_base;
746 u64 phys_addr;
600812ec 747 struct acpiphp_ioapic *ioapic;
a0d399a8
KK
748
749 /* Evaluate _STA if present */
750 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
751 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
752 return AE_CTRL_DEPTH;
753
754 /* Scan only PCI bus scope */
755 status = acpi_get_handle(handle, "_HID", &tmp);
756 if (ACPI_SUCCESS(status))
757 return AE_CTRL_DEPTH;
758
759 if (get_gsi_base(handle, &gsi_base))
760 return AE_OK;
761
600812ec
ST
762 ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
763 if (!ioapic)
764 return AE_NO_MEMORY;
765
a0d399a8
KK
766 pdev = get_apic_pci_info(handle);
767 if (!pdev)
600812ec 768 goto exit_kfree;
a0d399a8 769
600812ec
ST
770 if (pci_enable_device(pdev))
771 goto exit_pci_dev_put;
a0d399a8
KK
772
773 pci_set_master(pdev);
774
600812ec
ST
775 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
776 goto exit_pci_disable_device;
a0d399a8
KK
777
778 phys_addr = pci_resource_start(pdev, 0);
600812ec
ST
779 if (acpi_register_ioapic(handle, phys_addr, gsi_base))
780 goto exit_pci_release_region;
781
782 ioapic->gsi_base = gsi_base;
783 ioapic->dev = pdev;
784 spin_lock(&ioapic_list_lock);
785 list_add_tail(&ioapic->list, &ioapic_list);
786 spin_unlock(&ioapic_list_lock);
787
788 return AE_OK;
789
790 exit_pci_release_region:
791 pci_release_region(pdev, 0);
792 exit_pci_disable_device:
793 pci_disable_device(pdev);
794 exit_pci_dev_put:
795 pci_dev_put(pdev);
796 exit_kfree:
797 kfree(ioapic);
798
799 return AE_OK;
800}
801
802static acpi_status
803ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
804{
805 acpi_status status;
27663c58 806 unsigned long long sta;
600812ec
ST
807 acpi_handle tmp;
808 u32 gsi_base;
809 struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
810
811 /* Evaluate _STA if present */
812 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
813 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
814 return AE_CTRL_DEPTH;
815
816 /* Scan only PCI bus scope */
817 status = acpi_get_handle(handle, "_HID", &tmp);
818 if (ACPI_SUCCESS(status))
819 return AE_CTRL_DEPTH;
820
821 if (get_gsi_base(handle, &gsi_base))
a0d399a8 822 return AE_OK;
600812ec
ST
823
824 acpi_unregister_ioapic(handle, gsi_base);
825
826 spin_lock(&ioapic_list_lock);
827 list_for_each_entry_safe(pos, n, &ioapic_list, list) {
828 if (pos->gsi_base != gsi_base)
829 continue;
830 ioapic = pos;
831 list_del(&ioapic->list);
832 break;
a0d399a8 833 }
600812ec
ST
834 spin_unlock(&ioapic_list_lock);
835
836 if (!ioapic)
837 return AE_OK;
838
839 pci_release_region(ioapic->dev, 0);
840 pci_disable_device(ioapic->dev);
841 pci_dev_put(ioapic->dev);
842 kfree(ioapic);
a0d399a8
KK
843
844 return AE_OK;
845}
846
847static int acpiphp_configure_ioapics(acpi_handle handle)
848{
9b1d19ee 849 ioapic_add(handle, 0, NULL, NULL);
a0d399a8
KK
850 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
851 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
852 return 0;
853}
854
600812ec
ST
855static int acpiphp_unconfigure_ioapics(acpi_handle handle)
856{
857 ioapic_remove(handle, 0, NULL, NULL);
858 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
859 ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
860 return 0;
861}
862
1da177e4
LT
863static int power_on_slot(struct acpiphp_slot *slot)
864{
865 acpi_status status;
866 struct acpiphp_func *func;
867 struct list_head *l;
868 int retval = 0;
869
870 /* if already enabled, just skip */
871 if (slot->flags & SLOT_POWEREDON)
872 goto err_exit;
873
874 list_for_each (l, &slot->funcs) {
875 func = list_entry(l, struct acpiphp_func, sibling);
876
877 if (func->flags & FUNC_HAS_PS0) {
66bef8c0 878 dbg("%s: executing _PS0\n", __func__);
1da177e4
LT
879 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
880 if (ACPI_FAILURE(status)) {
66bef8c0 881 warn("%s: _PS0 failed\n", __func__);
1da177e4
LT
882 retval = -1;
883 goto err_exit;
884 } else
885 break;
886 }
887 }
888
889 /* TBD: evaluate _STA to check if the slot is enabled */
890
891 slot->flags |= SLOT_POWEREDON;
892
893 err_exit:
894 return retval;
895}
896
897
898static int power_off_slot(struct acpiphp_slot *slot)
899{
900 acpi_status status;
901 struct acpiphp_func *func;
902 struct list_head *l;
1da177e4
LT
903
904 int retval = 0;
905
906 /* if already disabled, just skip */
907 if ((slot->flags & SLOT_POWEREDON) == 0)
908 goto err_exit;
909
910 list_for_each (l, &slot->funcs) {
911 func = list_entry(l, struct acpiphp_func, sibling);
912
2f523b15 913 if (func->flags & FUNC_HAS_PS3) {
1da177e4
LT
914 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
915 if (ACPI_FAILURE(status)) {
66bef8c0 916 warn("%s: _PS3 failed\n", __func__);
1da177e4
LT
917 retval = -1;
918 goto err_exit;
919 } else
920 break;
921 }
922 }
923
1da177e4
LT
924 /* TBD: evaluate _STA to check if the slot is disabled */
925
926 slot->flags &= (~SLOT_POWEREDON);
927
928 err_exit:
929 return retval;
930}
931
932
15a1ae74
KA
933
934/**
26e6c66e 935 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
15a1ae74 936 * @bus: bus to start search with
15a1ae74
KA
937 */
938static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
939{
940 struct list_head *tmp;
941 unsigned char max, n;
942
943 /*
944 * pci_bus_max_busnr will return the highest
945 * reserved busnr for all these children.
946 * that is equivalent to the bus->subordinate
947 * value. We don't want to use the parent's
948 * bus->subordinate value because it could have
949 * padding in it.
950 */
951 max = bus->secondary;
952
953 list_for_each(tmp, &bus->children) {
954 n = pci_bus_max_busnr(pci_bus_b(tmp));
955 if (n > max)
956 max = n;
957 }
958 return max;
959}
960
961
15a1ae74
KA
962/**
963 * acpiphp_bus_add - add a new bus to acpi subsystem
964 * @func: acpiphp_func of the bridge
15a1ae74
KA
965 */
966static int acpiphp_bus_add(struct acpiphp_func *func)
967{
968 acpi_handle phandle;
969 struct acpi_device *device, *pdevice;
970 int ret_val;
971
972 acpi_get_parent(func->handle, &phandle);
973 if (acpi_bus_get_device(phandle, &pdevice)) {
974 dbg("no parent device, assuming NULL\n");
975 pdevice = NULL;
976 }
20416ea5
KA
977 if (!acpi_bus_get_device(func->handle, &device)) {
978 dbg("bus exists... trim\n");
979 /* this shouldn't be in here, so remove
980 * the bus then re-add it...
981 */
982 ret_val = acpi_bus_trim(device, 1);
983 dbg("acpi_bus_trim return %x\n", ret_val);
984 }
985
986 ret_val = acpi_bus_add(&device, pdevice, func->handle,
987 ACPI_BUS_TYPE_DEVICE);
988 if (ret_val) {
989 dbg("error adding bus, %x\n",
990 -ret_val);
991 goto acpiphp_bus_add_out;
15a1ae74
KA
992 }
993 /*
994 * try to start anyway. We could have failed to add
995 * simply because this bus had previously been added
996 * on another add. Don't bother with the return value
997 * we just keep going.
998 */
999 ret_val = acpi_bus_start(device);
1000
1001acpiphp_bus_add_out:
1002 return ret_val;
1003}
1004
1005
92c9be95
MT
1006/**
1007 * acpiphp_bus_trim - trim a bus from acpi subsystem
1008 * @handle: handle to acpi namespace
92c9be95 1009 */
6d47a5e4 1010static int acpiphp_bus_trim(acpi_handle handle)
92c9be95
MT
1011{
1012 struct acpi_device *device;
1013 int retval;
1014
1015 retval = acpi_bus_get_device(handle, &device);
1016 if (retval) {
1017 dbg("acpi_device not found\n");
1018 return retval;
1019 }
1020
1021 retval = acpi_bus_trim(device, 1);
1022 if (retval)
1023 err("cannot remove from acpi list\n");
1024
1025 return retval;
1026}
15a1ae74 1027
1da177e4
LT
1028/**
1029 * enable_device - enable, configure a slot
1030 * @slot: slot to be enabled
1031 *
1032 * This function should be called per *physical slot*,
1033 * not per each slot object in ACPI namespace.
1da177e4 1034 */
0ab2b57f 1035static int __ref enable_device(struct acpiphp_slot *slot)
1da177e4 1036{
1da177e4 1037 struct pci_dev *dev;
42f49a6a 1038 struct pci_bus *bus = slot->bridge->pci_bus;
1da177e4
LT
1039 struct list_head *l;
1040 struct acpiphp_func *func;
1041 int retval = 0;
42f49a6a 1042 int num, max, pass;
551bcb75 1043 acpi_status status;
1da177e4
LT
1044
1045 if (slot->flags & SLOT_ENABLED)
1046 goto err_exit;
1047
1048 /* sanity check: dev should be NULL when hot-plugged in */
42f49a6a 1049 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
1da177e4
LT
1050 if (dev) {
1051 /* This case shouldn't happen */
1052 err("pci_dev structure already exists.\n");
42f49a6a 1053 pci_dev_put(dev);
1da177e4
LT
1054 retval = -1;
1055 goto err_exit;
1056 }
1057
42f49a6a
RS
1058 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1059 if (num == 0) {
1da177e4
LT
1060 err("No new device found\n");
1061 retval = -1;
1062 goto err_exit;
1063 }
1064
15a1ae74 1065 max = acpiphp_max_busnr(bus);
42f49a6a
RS
1066 for (pass = 0; pass < 2; pass++) {
1067 list_for_each_entry(dev, &bus->devices, bus_list) {
1068 if (PCI_SLOT(dev->devfn) != slot->device)
1069 continue;
1070 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
c64b5eea 1071 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
42f49a6a 1072 max = pci_scan_bridge(bus, dev, max, pass);
92c9be95 1073 if (pass && dev->subordinate)
c64b5eea
KA
1074 pci_bus_size_bridges(dev->subordinate);
1075 }
42f49a6a 1076 }
1da177e4
LT
1077 }
1078
92c9be95
MT
1079 list_for_each (l, &slot->funcs) {
1080 func = list_entry(l, struct acpiphp_func, sibling);
1081 acpiphp_bus_add(func);
1082 }
1083
42f49a6a 1084 pci_bus_assign_resources(bus);
8e5dce35 1085 acpiphp_sanitize_bus(bus);
287af2fb 1086 acpiphp_set_hpp_values(slot->bridge->handle, bus);
9b1d19ee
ST
1087 list_for_each_entry(func, &slot->funcs, sibling)
1088 acpiphp_configure_ioapics(func->handle);
8e5dce35 1089 pci_enable_bridges(bus);
42f49a6a
RS
1090 pci_bus_add_devices(bus);
1091
1da177e4
LT
1092 list_for_each (l, &slot->funcs) {
1093 func = list_entry(l, struct acpiphp_func, sibling);
9d911d79
AC
1094 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
1095 func->function));
1096 if (!dev)
551bcb75
MT
1097 continue;
1098
9d911d79
AC
1099 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1100 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
1101 pci_dev_put(dev);
551bcb75 1102 continue;
9d911d79 1103 }
551bcb75
MT
1104
1105 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1106 if (ACPI_FAILURE(status))
1107 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1108 status);
9d911d79 1109 pci_dev_put(dev);
1da177e4
LT
1110 }
1111
1112 slot->flags |= SLOT_ENABLED;
1113
1da177e4
LT
1114 err_exit:
1115 return retval;
1116}
1117
d5cdb672
ST
1118static void disable_bridges(struct pci_bus *bus)
1119{
1120 struct pci_dev *dev;
1121 list_for_each_entry(dev, &bus->devices, bus_list) {
1122 if (dev->subordinate) {
1123 disable_bridges(dev->subordinate);
1124 pci_disable_device(dev);
1125 }
1126 }
1127}
1da177e4
LT
1128
1129/**
1130 * disable_device - disable a slot
26e6c66e 1131 * @slot: ACPI PHP slot
1da177e4
LT
1132 */
1133static int disable_device(struct acpiphp_slot *slot)
1134{
1da177e4 1135 struct acpiphp_func *func;
9d911d79 1136 struct pci_dev *pdev;
1da177e4
LT
1137
1138 /* is this slot already disabled? */
1139 if (!(slot->flags & SLOT_ENABLED))
1140 goto err_exit;
1141
9d911d79 1142 list_for_each_entry(func, &slot->funcs, sibling) {
551bcb75
MT
1143 if (func->bridge) {
1144 /* cleanup p2p bridges under this P2P bridge */
1145 cleanup_p2p_bridge(func->bridge->handle,
1146 (u32)1, NULL, NULL);
1147 func->bridge = NULL;
1148 }
1149
9d911d79
AC
1150 pdev = pci_get_slot(slot->bridge->pci_bus,
1151 PCI_DEVFN(slot->device, func->function));
1152 if (pdev) {
1153 pci_stop_bus_device(pdev);
1154 if (pdev->subordinate) {
1155 disable_bridges(pdev->subordinate);
1156 pci_disable_device(pdev);
d5cdb672 1157 }
9d911d79
AC
1158 pci_remove_bus_device(pdev);
1159 pci_dev_put(pdev);
d5cdb672 1160 }
600812ec
ST
1161 }
1162
9d911d79 1163 list_for_each_entry(func, &slot->funcs, sibling) {
600812ec 1164 acpiphp_unconfigure_ioapics(func->handle);
92c9be95 1165 acpiphp_bus_trim(func->handle);
1da177e4
LT
1166 }
1167
1168 slot->flags &= (~SLOT_ENABLED);
1169
9d911d79
AC
1170err_exit:
1171 return 0;
1da177e4
LT
1172}
1173
1174
1175/**
1176 * get_slot_status - get ACPI slot status
26e6c66e 1177 * @slot: ACPI PHP slot
1da177e4 1178 *
26e6c66e
RD
1179 * If a slot has _STA for each function and if any one of them
1180 * returned non-zero status, return it.
1da177e4 1181 *
26e6c66e
RD
1182 * If a slot doesn't have _STA and if any one of its functions'
1183 * configuration space is configured, return 0x0f as a _STA.
1da177e4 1184 *
26e6c66e 1185 * Otherwise return 0.
1da177e4
LT
1186 */
1187static unsigned int get_slot_status(struct acpiphp_slot *slot)
1188{
1189 acpi_status status;
27663c58 1190 unsigned long long sta = 0;
1da177e4
LT
1191 u32 dvid;
1192 struct list_head *l;
1193 struct acpiphp_func *func;
1194
1195 list_for_each (l, &slot->funcs) {
1196 func = list_entry(l, struct acpiphp_func, sibling);
1197
1198 if (func->flags & FUNC_HAS_STA) {
1199 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1200 if (ACPI_SUCCESS(status) && sta)
1201 break;
1202 } else {
1203 pci_bus_read_config_dword(slot->bridge->pci_bus,
1204 PCI_DEVFN(slot->device,
1205 func->function),
1206 PCI_VENDOR_ID, &dvid);
1207 if (dvid != 0xffffffff) {
1208 sta = ACPI_STA_ALL;
1209 break;
1210 }
1211 }
1212 }
1213
1214 return (unsigned int)sta;
1215}
1216
8d50e332
RS
1217/**
1218 * acpiphp_eject_slot - physically eject the slot
26e6c66e 1219 * @slot: ACPI PHP slot
8d50e332 1220 */
bfceafc5 1221int acpiphp_eject_slot(struct acpiphp_slot *slot)
8d50e332
RS
1222{
1223 acpi_status status;
1224 struct acpiphp_func *func;
1225 struct list_head *l;
1226 struct acpi_object_list arg_list;
1227 union acpi_object arg;
1228
1229 list_for_each (l, &slot->funcs) {
1230 func = list_entry(l, struct acpiphp_func, sibling);
1231
1232 /* We don't want to call _EJ0 on non-existing functions. */
1233 if ((func->flags & FUNC_HAS_EJ0)) {
1234 /* _EJ0 method take one argument */
1235 arg_list.count = 1;
1236 arg_list.pointer = &arg;
1237 arg.type = ACPI_TYPE_INTEGER;
1238 arg.integer.value = 1;
1239
1240 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1241 if (ACPI_FAILURE(status)) {
66bef8c0 1242 warn("%s: _EJ0 failed\n", __func__);
8d50e332
RS
1243 return -1;
1244 } else
1245 break;
1246 }
1247 }
1248 return 0;
1249}
1250
1da177e4
LT
1251/**
1252 * acpiphp_check_bridge - re-enumerate devices
26e6c66e 1253 * @bridge: where to begin re-enumeration
1da177e4
LT
1254 *
1255 * Iterate over all slots under this bridge and make sure that if a
1256 * card is present they are enabled, and if not they are disabled.
1257 */
1258static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1259{
1260 struct acpiphp_slot *slot;
1261 int retval = 0;
1262 int enabled, disabled;
1263
1264 enabled = disabled = 0;
1265
1266 for (slot = bridge->slots; slot; slot = slot->next) {
1267 unsigned int status = get_slot_status(slot);
1268 if (slot->flags & SLOT_ENABLED) {
1269 if (status == ACPI_STA_ALL)
1270 continue;
1271 retval = acpiphp_disable_slot(slot);
1272 if (retval) {
1273 err("Error occurred in disabling\n");
1274 goto err_exit;
8d50e332
RS
1275 } else {
1276 acpiphp_eject_slot(slot);
1da177e4
LT
1277 }
1278 disabled++;
1279 } else {
1280 if (status != ACPI_STA_ALL)
1281 continue;
1282 retval = acpiphp_enable_slot(slot);
1283 if (retval) {
1284 err("Error occurred in enabling\n");
1285 goto err_exit;
1286 }
1287 enabled++;
1288 }
1289 }
1290
66bef8c0 1291 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
1da177e4
LT
1292
1293 err_exit:
1294 return retval;
1295}
1296
8e7561cf
RS
1297static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1298{
1299 u16 pci_cmd, pci_bctl;
1300 struct pci_dev *cdev;
1301
1302 /* Program hpp values for this device */
1303 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1304 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1305 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1306 return;
e22b7350 1307
9ef2241b
GH
1308 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
1309 return;
1310
8e7561cf 1311 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
e22b7350 1312 bridge->hpp.t0->cache_line_size);
8e7561cf 1313 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
e22b7350 1314 bridge->hpp.t0->latency_timer);
8e7561cf 1315 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
e22b7350 1316 if (bridge->hpp.t0->enable_serr)
8e7561cf
RS
1317 pci_cmd |= PCI_COMMAND_SERR;
1318 else
1319 pci_cmd &= ~PCI_COMMAND_SERR;
e22b7350 1320 if (bridge->hpp.t0->enable_perr)
8e7561cf
RS
1321 pci_cmd |= PCI_COMMAND_PARITY;
1322 else
1323 pci_cmd &= ~PCI_COMMAND_PARITY;
1324 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1325
1326 /* Program bridge control value and child devices */
1327 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1328 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
e22b7350 1329 bridge->hpp.t0->latency_timer);
8e7561cf 1330 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
e22b7350 1331 if (bridge->hpp.t0->enable_serr)
8e7561cf
RS
1332 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1333 else
1334 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
e22b7350 1335 if (bridge->hpp.t0->enable_perr)
8e7561cf
RS
1336 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1337 else
1338 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1339 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1340 if (dev->subordinate) {
1341 list_for_each_entry(cdev, &dev->subordinate->devices,
1342 bus_list)
1343 program_hpp(cdev, bridge);
1344 }
1345 }
1346}
1347
1348static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1349{
1350 struct acpiphp_bridge bridge;
1351 struct pci_dev *dev;
1352
1353 memset(&bridge, 0, sizeof(bridge));
1354 bridge.handle = handle;
7430e34c 1355 bridge.pci_bus = bus;
783c49fc 1356 bridge.pci_dev = bus->self;
8e7561cf
RS
1357 decode_hpp(&bridge);
1358 list_for_each_entry(dev, &bus->devices, bus_list)
1359 program_hpp(dev, &bridge);
1360
1361}
1362
1363/*
1364 * Remove devices for which we could not assign resources, call
1365 * arch specific code to fix-up the bus
1366 */
1367static void acpiphp_sanitize_bus(struct pci_bus *bus)
1368{
1369 struct pci_dev *dev;
1370 int i;
1371 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1372
1373 list_for_each_entry(dev, &bus->devices, bus_list) {
1374 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1375 struct resource *res = &dev->resource[i];
1376 if ((res->flags & type_mask) && !res->start &&
1377 res->end) {
1378 /* Could not assign a required resources
1379 * for this device, remove it */
1380 pci_remove_bus_device(dev);
1381 break;
1382 }
1383 }
1384 }
1385}
1386
1387/* Program resources in newly inserted bridge */
1388static int acpiphp_configure_bridge (acpi_handle handle)
1389{
d6aa484c 1390 struct pci_dev *dev;
8e7561cf
RS
1391 struct pci_bus *bus;
1392
d6aa484c
AC
1393 dev = acpi_get_pci_dev(handle);
1394 if (!dev) {
8e7561cf
RS
1395 err("cannot get PCI domain and bus number for bridge\n");
1396 return -EINVAL;
1397 }
d6aa484c
AC
1398
1399 bus = dev->bus;
8e7561cf
RS
1400
1401 pci_bus_size_bridges(bus);
1402 pci_bus_assign_resources(bus);
1403 acpiphp_sanitize_bus(bus);
1404 acpiphp_set_hpp_values(handle, bus);
1405 pci_enable_bridges(bus);
a0d399a8 1406 acpiphp_configure_ioapics(handle);
d6aa484c 1407 pci_dev_put(dev);
8e7561cf
RS
1408 return 0;
1409}
1410
1411static void handle_bridge_insertion(acpi_handle handle, u32 type)
1412{
1413 struct acpi_device *device, *pdevice;
1414 acpi_handle phandle;
1415
1416 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1417 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1418 err("unexpected notification type %d\n", type);
1419 return;
1420 }
1421
1422 acpi_get_parent(handle, &phandle);
1423 if (acpi_bus_get_device(phandle, &pdevice)) {
1424 dbg("no parent device, assuming NULL\n");
1425 pdevice = NULL;
1426 }
1427 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1428 err("cannot add bridge to acpi list\n");
1429 return;
1430 }
1431 if (!acpiphp_configure_bridge(handle) &&
1432 !acpi_bus_start(device))
1433 add_bridge(handle);
1434 else
1435 err("cannot configure and start bridge\n");
1436
1437}
1438
1da177e4
LT
1439/*
1440 * ACPI event handlers
1441 */
1442
0bbd6424
GH
1443static acpi_status
1444count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1445{
1446 int *count = (int *)context;
1447 struct acpiphp_bridge *bridge;
1448
1449 bridge = acpiphp_handle_to_bridge(handle);
1450 if (bridge)
1451 (*count)++;
1452 return AE_OK ;
1453}
1454
1455static acpi_status
1456check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1457{
1458 struct acpiphp_bridge *bridge;
1459 char objname[64];
1460 struct acpi_buffer buffer = { .length = sizeof(objname),
1461 .pointer = objname };
1462
1463 bridge = acpiphp_handle_to_bridge(handle);
1464 if (bridge) {
1465 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1466 dbg("%s: re-enumerating slots under %s\n",
66bef8c0 1467 __func__, objname);
0bbd6424
GH
1468 acpiphp_check_bridge(bridge);
1469 }
1470 return AE_OK ;
1471}
1472
1da177e4
LT
1473/**
1474 * handle_hotplug_event_bridge - handle ACPI event on bridges
1da177e4
LT
1475 * @handle: Notify()'ed acpi_handle
1476 * @type: Notify code
1477 * @context: pointer to acpiphp_bridge structure
1478 *
26e6c66e 1479 * Handles ACPI event notification on {host,p2p} bridges.
1da177e4
LT
1480 */
1481static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1482{
1483 struct acpiphp_bridge *bridge;
1484 char objname[64];
1485 struct acpi_buffer buffer = { .length = sizeof(objname),
1486 .pointer = objname };
8e7561cf 1487 struct acpi_device *device;
0bbd6424 1488 int num_sub_bridges = 0;
1da177e4 1489
8e7561cf
RS
1490 if (acpi_bus_get_device(handle, &device)) {
1491 /* This bridge must have just been physically inserted */
1492 handle_bridge_insertion(handle, type);
1493 return;
1494 }
1495
1496 bridge = acpiphp_handle_to_bridge(handle);
0bbd6424
GH
1497 if (type == ACPI_NOTIFY_BUS_CHECK) {
1498 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
1499 count_sub_bridges, &num_sub_bridges, NULL);
1500 }
1501
1502 if (!bridge && !num_sub_bridges) {
8e7561cf
RS
1503 err("cannot get bridge info\n");
1504 return;
1505 }
1da177e4
LT
1506
1507 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1508
1509 switch (type) {
1510 case ACPI_NOTIFY_BUS_CHECK:
1511 /* bus re-enumerate */
66bef8c0 1512 dbg("%s: Bus check notify on %s\n", __func__, objname);
0bbd6424
GH
1513 if (bridge) {
1514 dbg("%s: re-enumerating slots under %s\n",
66bef8c0 1515 __func__, objname);
0bbd6424
GH
1516 acpiphp_check_bridge(bridge);
1517 }
1518 if (num_sub_bridges)
1519 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1520 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
1da177e4
LT
1521 break;
1522
1523 case ACPI_NOTIFY_DEVICE_CHECK:
1524 /* device check */
66bef8c0 1525 dbg("%s: Device check notify on %s\n", __func__, objname);
1da177e4
LT
1526 acpiphp_check_bridge(bridge);
1527 break;
1528
1529 case ACPI_NOTIFY_DEVICE_WAKE:
1530 /* wake event */
66bef8c0 1531 dbg("%s: Device wake notify on %s\n", __func__, objname);
1da177e4
LT
1532 break;
1533
1534 case ACPI_NOTIFY_EJECT_REQUEST:
1535 /* request device eject */
66bef8c0 1536 dbg("%s: Device eject notify on %s\n", __func__, objname);
551bcb75
MT
1537 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1538 (bridge->flags & BRIDGE_HAS_EJ0)) {
1539 struct acpiphp_slot *slot;
1540 slot = bridge->func->slot;
1541 if (!acpiphp_disable_slot(slot))
1542 acpiphp_eject_slot(slot);
1543 }
1da177e4
LT
1544 break;
1545
1546 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1547 printk(KERN_ERR "Device %s cannot be configured due"
1548 " to a frequency mismatch\n", objname);
1549 break;
1550
1551 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1552 printk(KERN_ERR "Device %s cannot be configured due"
1553 " to a bus mode mismatch\n", objname);
1554 break;
1555
1556 case ACPI_NOTIFY_POWER_FAULT:
1557 printk(KERN_ERR "Device %s has suffered a power fault\n",
1558 objname);
1559 break;
1560
1561 default:
1562 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1563 break;
1564 }
1565}
1566
1da177e4
LT
1567/**
1568 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1da177e4
LT
1569 * @handle: Notify()'ed acpi_handle
1570 * @type: Notify code
1571 * @context: pointer to acpiphp_func structure
1572 *
26e6c66e 1573 * Handles ACPI event notification on slots.
1da177e4 1574 */
2b85e130 1575static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
1da177e4
LT
1576{
1577 struct acpiphp_func *func;
1578 char objname[64];
1579 struct acpi_buffer buffer = { .length = sizeof(objname),
1580 .pointer = objname };
1581
1582 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1583
1584 func = (struct acpiphp_func *)context;
1585
1586 switch (type) {
1587 case ACPI_NOTIFY_BUS_CHECK:
1588 /* bus re-enumerate */
66bef8c0 1589 dbg("%s: Bus check notify on %s\n", __func__, objname);
1da177e4
LT
1590 acpiphp_enable_slot(func->slot);
1591 break;
1592
1593 case ACPI_NOTIFY_DEVICE_CHECK:
1594 /* device check : re-enumerate from parent bus */
66bef8c0 1595 dbg("%s: Device check notify on %s\n", __func__, objname);
1da177e4
LT
1596 acpiphp_check_bridge(func->slot->bridge);
1597 break;
1598
1599 case ACPI_NOTIFY_DEVICE_WAKE:
1600 /* wake event */
66bef8c0 1601 dbg("%s: Device wake notify on %s\n", __func__, objname);
1da177e4
LT
1602 break;
1603
1604 case ACPI_NOTIFY_EJECT_REQUEST:
1605 /* request device eject */
66bef8c0 1606 dbg("%s: Device eject notify on %s\n", __func__, objname);
8d50e332
RS
1607 if (!(acpiphp_disable_slot(func->slot)))
1608 acpiphp_eject_slot(func->slot);
1da177e4
LT
1609 break;
1610
1611 default:
1612 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1613 break;
1614 }
1615}
1616
8e7561cf
RS
1617
1618static acpi_status
1619find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1620{
1621 int *count = (int *)context;
1622
27558203 1623 if (acpi_is_root_bridge(handle)) {
8e7561cf
RS
1624 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1625 handle_hotplug_event_bridge, NULL);
1626 (*count)++;
1627 }
1628 return AE_OK ;
1629}
1da177e4
LT
1630
1631static struct acpi_pci_driver acpi_pci_hp_driver = {
1632 .add = add_bridge,
1633 .remove = remove_bridge,
1634};
1635
1636/**
1637 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1da177e4
LT
1638 */
1639int __init acpiphp_glue_init(void)
1640{
8e7561cf 1641 int num = 0;
1da177e4 1642
8e7561cf
RS
1643 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1644 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
1da177e4
LT
1645
1646 if (num <= 0)
1647 return -1;
8e7561cf
RS
1648 else
1649 acpi_pci_register_driver(&acpi_pci_hp_driver);
1da177e4
LT
1650
1651 return 0;
1652}
1653
1654
1655/**
1656 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1657 *
26e6c66e 1658 * This function frees all data allocated in acpiphp_glue_init().
1da177e4 1659 */
031f30d2 1660void acpiphp_glue_exit(void)
1da177e4 1661{
1da177e4
LT
1662 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1663}
1664
1665
1666/**
1667 * acpiphp_get_num_slots - count number of slots in a system
1668 */
1669int __init acpiphp_get_num_slots(void)
1670{
1da177e4 1671 struct acpiphp_bridge *bridge;
467c442f 1672 int num_slots = 0;
1da177e4 1673
467c442f 1674 list_for_each_entry (bridge, &bridge_list, list) {
42f49a6a
RS
1675 dbg("Bus %04x:%02x has %d slot%s\n",
1676 pci_domain_nr(bridge->pci_bus),
1677 bridge->pci_bus->number, bridge->nr_slots,
1678 bridge->nr_slots == 1 ? "" : "s");
1da177e4
LT
1679 num_slots += bridge->nr_slots;
1680 }
1681
42f49a6a 1682 dbg("Total %d slots\n", num_slots);
1da177e4
LT
1683 return num_slots;
1684}
1685
1686
1687#if 0
1688/**
1689 * acpiphp_for_each_slot - call function for each slot
1690 * @fn: callback function
1691 * @data: context to be passed to callback function
1da177e4
LT
1692 */
1693static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1694{
1695 struct list_head *node;
1696 struct acpiphp_bridge *bridge;
1697 struct acpiphp_slot *slot;
1698 int retval = 0;
1699
1700 list_for_each (node, &bridge_list) {
1701 bridge = (struct acpiphp_bridge *)node;
1702 for (slot = bridge->slots; slot; slot = slot->next) {
1703 retval = fn(slot, data);
1704 if (!retval)
1705 goto err_exit;
1706 }
1707 }
1708
1709 err_exit:
1710 return retval;
1711}
1712#endif
1713
1da177e4
LT
1714
1715/**
1716 * acpiphp_enable_slot - power on slot
26e6c66e 1717 * @slot: ACPI PHP slot
1da177e4
LT
1718 */
1719int acpiphp_enable_slot(struct acpiphp_slot *slot)
1720{
1721 int retval;
1722
6aa4cdd0 1723 mutex_lock(&slot->crit_sect);
1da177e4
LT
1724
1725 /* wake up all functions */
1726 retval = power_on_slot(slot);
1727 if (retval)
1728 goto err_exit;
1729
cde0e5d7 1730 if (get_slot_status(slot) == ACPI_STA_ALL) {
1da177e4
LT
1731 /* configure all functions */
1732 retval = enable_device(slot);
cde0e5d7
MT
1733 if (retval)
1734 power_off_slot(slot);
1735 } else {
66bef8c0 1736 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
cde0e5d7
MT
1737 power_off_slot(slot);
1738 }
1da177e4
LT
1739
1740 err_exit:
6aa4cdd0 1741 mutex_unlock(&slot->crit_sect);
1da177e4
LT
1742 return retval;
1743}
1744
1da177e4
LT
1745/**
1746 * acpiphp_disable_slot - power off slot
26e6c66e 1747 * @slot: ACPI PHP slot
1da177e4
LT
1748 */
1749int acpiphp_disable_slot(struct acpiphp_slot *slot)
1750{
1751 int retval = 0;
1752
6aa4cdd0 1753 mutex_lock(&slot->crit_sect);
1da177e4
LT
1754
1755 /* unconfigure all functions */
1756 retval = disable_device(slot);
1757 if (retval)
1758 goto err_exit;
1759
1760 /* power off all functions */
1761 retval = power_off_slot(slot);
1762 if (retval)
1763 goto err_exit;
1764
1da177e4 1765 err_exit:
6aa4cdd0 1766 mutex_unlock(&slot->crit_sect);
1da177e4
LT
1767 return retval;
1768}
1769
1770
1771/*
1772 * slot enabled: 1
1773 * slot disabled: 0
1774 */
1775u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1776{
8d50e332 1777 return (slot->flags & SLOT_POWEREDON);
1da177e4
LT
1778}
1779
1780
1781/*
35ae61a0
MT
1782 * latch open: 1
1783 * latch closed: 0
1da177e4
LT
1784 */
1785u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1786{
1787 unsigned int sta;
1788
1789 sta = get_slot_status(slot);
1790
35ae61a0 1791 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
1da177e4
LT
1792}
1793
1794
1795/*
1796 * adapter presence : 1
1797 * absence : 0
1798 */
1799u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1800{
1801 unsigned int sta;
1802
1803 sta = get_slot_status(slot);
1804
1805 return (sta == 0) ? 0 : 1;
1806}