blob: 712f02fb1cbbb6f8acc3c62df676240a7a05c617 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Rajesh Shah42f49a62005-04-28 00:25:53 -07007 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
Rajesh Shah8e7561c2005-04-28 00:25:56 -07009 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 *
Kristen Carlson Accardi998be202006-07-26 10:52:33 -070029 * Send feedback to <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
Rajesh Shah42f49a62005-04-28 00:25:53 -070033/*
34 * Lifetime rules for pci_dev:
35 * - The one in acpiphp_func has its refcount elevated by pci_get_slot()
36 * when the driver is loaded or when an insertion event occurs. It loses
37 * a refcount when its ejected or the driver unloads.
38 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
39 * when the bridge is scanned and it loses a refcount when the bridge
40 * is removed.
41 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/init.h>
44#include <linux/module.h>
45
46#include <linux/kernel.h>
47#include <linux/pci.h>
48#include <linux/smp_lock.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010049#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "../pci.h"
52#include "pci_hotplug.h"
53#include "acpiphp.h"
54
55static LIST_HEAD(bridge_list);
56
57#define MY_NAME "acpiphp_glue"
58
59static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070060static void acpiphp_sanitize_bus(struct pci_bus *bus);
61static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040062static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * initialization & terminatation routines
67 */
68
69/**
70 * is_ejectable - determine if a slot is ejectable
71 * @handle: handle to acpi namespace
72 *
73 * Ejectable slot should satisfy at least these conditions:
74 *
75 * 1. has _ADR method
76 * 2. has _EJ0 method
77 *
78 * optionally
79 *
80 * 1. has _STA method
81 * 2. has _PS0 method
82 * 3. has _PS3 method
83 * 4. ..
84 *
85 */
86static int is_ejectable(acpi_handle handle)
87{
88 acpi_status status;
89 acpi_handle tmp;
90
91 status = acpi_get_handle(handle, "_ADR", &tmp);
92 if (ACPI_FAILURE(status)) {
93 return 0;
94 }
95
96 status = acpi_get_handle(handle, "_EJ0", &tmp);
97 if (ACPI_FAILURE(status)) {
98 return 0;
99 }
100
101 return 1;
102}
103
104
105/* callback routine to check the existence of ejectable slots */
106static acpi_status
107is_ejectable_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
108{
109 int *count = (int *)context;
110
111 if (is_ejectable(handle)) {
112 (*count)++;
113 /* only one ejectable slot is enough */
114 return AE_CTRL_TERMINATE;
115 } else {
116 return AE_OK;
117 }
118}
119
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400120/* callback routine to check for the existance of a pci dock device */
121static acpi_status
122is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
123{
124 int *count = (int *)context;
125
126 if (is_dock_device(handle)) {
127 (*count)++;
128 return AE_CTRL_TERMINATE;
129 } else {
130 return AE_OK;
131 }
132}
133
134
135
136
137/*
138 * the _DCK method can do funny things... and sometimes not
139 * hah-hah funny.
140 *
141 * TBD - figure out a way to only call fixups for
142 * systems that require them.
143 */
144static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
145 void *v)
146{
147 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
148 struct pci_bus *bus = func->slot->bridge->pci_bus;
149 u32 buses;
150
151 if (!bus->self)
152 return NOTIFY_OK;
153
154 /* fixup bad _DCK function that rewrites
155 * secondary bridge on slot
156 */
157 pci_read_config_dword(bus->self,
158 PCI_PRIMARY_BUS,
159 &buses);
160
161 if (((buses >> 8) & 0xff) != bus->secondary) {
162 buses = (buses & 0xff000000)
163 | ((unsigned int)(bus->primary) << 0)
164 | ((unsigned int)(bus->secondary) << 8)
165 | ((unsigned int)(bus->subordinate) << 16);
166 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
167 }
168 return NOTIFY_OK;
169}
170
171
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* callback routine to register each ACPI PCI slot object */
175static acpi_status
176register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
177{
178 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
179 struct acpiphp_slot *slot;
180 struct acpiphp_func *newfunc;
181 acpi_handle tmp;
182 acpi_status status = AE_OK;
183 unsigned long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800184 int device, function, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
187
188 if (ACPI_FAILURE(status))
189 return AE_OK;
190
191 status = acpi_get_handle(handle, "_EJ0", &tmp);
192
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400193 if (ACPI_FAILURE(status) && !(is_dock_device(handle)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return AE_OK;
195
196 device = (adr >> 16) & 0xffff;
197 function = adr & 0xffff;
198
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100199 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!newfunc)
201 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 INIT_LIST_HEAD(&newfunc->sibling);
204 newfunc->handle = handle;
205 newfunc->function = function;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800206 if (ACPI_SUCCESS(status))
207 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
210 newfunc->flags |= FUNC_HAS_STA;
211
212 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
213 newfunc->flags |= FUNC_HAS_PS0;
214
215 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
216 newfunc->flags |= FUNC_HAS_PS3;
217
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400218 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800219 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400222 if (ACPI_FAILURE(status)) {
223 /*
224 * use the count of the number of slots we've found
225 * for the number of the slot
226 */
227 sun = bridge->nr_slots+1;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* search for objects that share the same slot */
231 for (slot = bridge->slots; slot; slot = slot->next)
232 if (slot->device == device) {
233 if (slot->sun != sun)
234 warn("sibling found, but _SUN doesn't match!\n");
235 break;
236 }
237
238 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100239 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!slot) {
241 kfree(newfunc);
242 return AE_NO_MEMORY;
243 }
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 slot->device = device;
247 slot->sun = sun;
248 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100249 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 slot->next = bridge->slots;
252 bridge->slots = slot;
253
254 bridge->nr_slots++;
255
Rajesh Shah42f49a62005-04-28 00:25:53 -0700256 dbg("found ACPI PCI Hotplug slot %d at PCI %04x:%02x:%02x\n",
257 slot->sun, pci_domain_nr(bridge->pci_bus),
258 bridge->pci_bus->number, slot->device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800259 retval = acpiphp_register_hotplug_slot(slot);
260 if (retval) {
261 warn("acpiphp_register_hotplug_slot failed(err code = 0x%x)\n", retval);
262 goto err_exit;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 newfunc->slot = slot;
267 list_add_tail(&newfunc->sibling, &slot->funcs);
268
269 /* associate corresponding pci_dev */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700270 newfunc->pci_dev = pci_get_slot(bridge->pci_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 PCI_DEVFN(device, function));
272 if (newfunc->pci_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
274 }
275
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400276 if (is_dock_device(handle)) {
277 /* we don't want to call this device's _EJ0
278 * because we want the dock notify handler
279 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800280 */
281 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400282 if (register_hotplug_dock_device(handle,
283 handle_hotplug_event_func, newfunc))
284 dbg("failed to register dock device\n");
285
286 /* we need to be notified when dock events happen
287 * outside of the hotplug operation, since we may
288 * need to do fixups before we can hotplug.
289 */
290 newfunc->nb.notifier_call = post_dock_fixups;
291 if (register_dock_notifier(&newfunc->nb))
292 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800293 }
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800296 if (!(newfunc->flags & FUNC_HAS_DCK)) {
297 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ACPI_SYSTEM_NOTIFY,
299 handle_hotplug_event_func,
300 newfunc);
301
Kristen Accardi20416ea2006-02-23 17:56:03 -0800302 if (ACPI_FAILURE(status))
303 err("failed to register interrupt notify handler\n");
304 } else
305 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Kristen Accardi20416ea2006-02-23 17:56:03 -0800307 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800308
309 err_exit:
310 bridge->nr_slots--;
311 bridge->slots = slot->next;
312 kfree(slot);
313 kfree(newfunc);
314
315 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318
319/* see if it's worth looking at this bridge */
320static int detect_ejectable_slots(acpi_handle *bridge_handle)
321{
322 acpi_status status;
323 int count;
324
325 count = 0;
326
327 /* only check slots defined directly below bridge object */
328 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
329 is_ejectable_slot, (void *)&count, NULL);
330
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400331 /*
332 * we also need to add this bridge if there is a dock bridge or
333 * other pci device on a dock station (removable)
334 */
335 if (!count)
336 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle,
337 (u32)1, is_pci_dock_device, (void *)&count,
338 NULL);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return count;
341}
342
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* decode ACPI 2.0 _HPP hot plug parameters */
345static void decode_hpp(struct acpiphp_bridge *bridge)
346{
347 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Kenji Kaneshige7430e342006-05-02 10:54:50 +0900349 status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900350 if (ACPI_FAILURE(status) ||
351 !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
Kristen Accardi783c49f2006-03-03 10:16:05 -0800352 /* use default numbers */
Kenji Kaneshigee22b73502006-05-02 10:57:14 +0900353 printk(KERN_WARNING
354 "%s: Could not get hotplug parameters. Use defaults\n",
355 __FUNCTION__);
356 bridge->hpp.t0 = &bridge->hpp.type0_data;
357 bridge->hpp.t0->revision = 0;
358 bridge->hpp.t0->cache_line_size = 0x10;
359 bridge->hpp.t0->latency_timer = 0x40;
360 bridge->hpp.t0->enable_serr = 0;
361 bridge->hpp.t0->enable_perr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365
Kristen Accardi783c49f2006-03-03 10:16:05 -0800366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
368static void init_bridge_misc(struct acpiphp_bridge *bridge)
369{
370 acpi_status status;
371
372 /* decode ACPI 2.0 _HPP (hot plug parameters) */
373 decode_hpp(bridge);
374
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800375 /* must be added to the list prior to calling register_slot */
376 list_add(&bridge->list, &bridge_list);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* register all slot objects under this bridge */
379 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
380 register_slot, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800381 if (ACPI_FAILURE(status)) {
382 list_del(&bridge->list);
383 return;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700387 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900388 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 status = acpi_remove_notify_handler(bridge->func->handle,
390 ACPI_SYSTEM_NOTIFY,
391 handle_hotplug_event_func);
392 if (ACPI_FAILURE(status))
393 err("failed to remove notify handler\n");
394 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700395 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event_bridge,
398 bridge);
399
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700400 if (ACPI_FAILURE(status)) {
401 err("failed to register interrupt notify handler\n");
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900407/* find acpiphp_func from acpiphp_bridge */
408static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
409{
410 struct list_head *node, *l;
411 struct acpiphp_bridge *bridge;
412 struct acpiphp_slot *slot;
413 struct acpiphp_func *func;
414
415 list_for_each(node, &bridge_list) {
416 bridge = list_entry(node, struct acpiphp_bridge, list);
417 for (slot = bridge->slots; slot; slot = slot->next) {
418 list_for_each(l, &slot->funcs) {
419 func = list_entry(l, struct acpiphp_func,
420 sibling);
421 if (func->handle == handle)
422 return func;
423 }
424 }
425 }
426
427 return NULL;
428}
429
430
431static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
432{
433 acpi_handle dummy_handle;
434
435 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
436 "_STA", &dummy_handle)))
437 bridge->flags |= BRIDGE_HAS_STA;
438
439 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
440 "_EJ0", &dummy_handle)))
441 bridge->flags |= BRIDGE_HAS_EJ0;
442
443 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
444 "_PS0", &dummy_handle)))
445 bridge->flags |= BRIDGE_HAS_PS0;
446
447 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
448 "_PS3", &dummy_handle)))
449 bridge->flags |= BRIDGE_HAS_PS3;
450
451 /* is this ejectable p2p bridge? */
452 if (bridge->flags & BRIDGE_HAS_EJ0) {
453 struct acpiphp_func *func;
454
455 dbg("found ejectable p2p bridge\n");
456
457 /* make link between PCI bridge and PCI function */
458 func = acpiphp_bridge_handle_to_function(bridge->handle);
459 if (!func)
460 return;
461 bridge->func = func;
462 func->bridge = bridge;
463 }
464}
465
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/* allocate and initialize host bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700468static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct acpiphp_bridge *bridge;
471
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100472 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (bridge == NULL)
474 return;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 bridge->type = BRIDGE_TYPE_HOST;
477 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Rajesh Shah42f49a62005-04-28 00:25:53 -0700479 bridge->pci_bus = pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 spin_lock_init(&bridge->res_lock);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 init_bridge_misc(bridge);
484}
485
486
487/* allocate and initialize PCI-to-PCI bridge data structure */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700488static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100492 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (bridge == NULL) {
494 err("out of memory\n");
495 return;
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 bridge->type = BRIDGE_TYPE_P2P;
499 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900500 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rajesh Shah42f49a62005-04-28 00:25:53 -0700502 bridge->pci_dev = pci_dev_get(pci_dev);
503 bridge->pci_bus = pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!bridge->pci_bus) {
505 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700506 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 spin_lock_init(&bridge->res_lock);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700512 return;
513 err:
514 pci_dev_put(pci_dev);
515 kfree(bridge);
516 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519
520/* callback routine to find P2P bridges */
521static acpi_status
522find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
523{
524 acpi_status status;
525 acpi_handle dummy_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned long tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700527 int device, function;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700529 struct pci_bus *pci_bus = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 status = acpi_get_handle(handle, "_ADR", &dummy_handle);
532 if (ACPI_FAILURE(status))
533 return AE_OK; /* continue */
534
535 status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
536 if (ACPI_FAILURE(status)) {
537 dbg("%s: _ADR evaluation failure\n", __FUNCTION__);
538 return AE_OK;
539 }
540
541 device = (tmp >> 16) & 0xffff;
542 function = tmp & 0xffff;
543
Rajesh Shah42f49a62005-04-28 00:25:53 -0700544 dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Rajesh Shah42f49a62005-04-28 00:25:53 -0700546 if (!dev || !dev->subordinate)
547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* check if this bridge has ejectable slots */
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400550 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Rajesh Shah42f49a62005-04-28 00:25:53 -0700552 add_p2p_bridge(handle, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900555 /* search P2P bridges under this p2p bridge */
556 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
557 find_p2p_bridge, dev->subordinate, NULL);
558 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900559 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900560
Rajesh Shah42f49a62005-04-28 00:25:53 -0700561 out:
562 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return AE_OK;
564}
565
566
567/* find hot-pluggable slots, and then find P2P bridge */
568static int add_bridge(acpi_handle handle)
569{
570 acpi_status status;
571 unsigned long tmp;
572 int seg, bus;
573 acpi_handle dummy_handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700574 struct pci_bus *pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* if the bridge doesn't have _STA, we assume it is always there */
577 status = acpi_get_handle(handle, "_STA", &dummy_handle);
578 if (ACPI_SUCCESS(status)) {
579 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
580 if (ACPI_FAILURE(status)) {
581 dbg("%s: _STA evaluation failure\n", __FUNCTION__);
582 return 0;
583 }
584 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
585 /* don't register this object */
586 return 0;
587 }
588
589 /* get PCI segment number */
590 status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
591
592 seg = ACPI_SUCCESS(status) ? tmp : 0;
593
594 /* get PCI bus number */
595 status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
596
597 if (ACPI_SUCCESS(status)) {
598 bus = tmp;
599 } else {
600 warn("can't get bus number, assuming 0\n");
601 bus = 0;
602 }
603
Rajesh Shah42f49a62005-04-28 00:25:53 -0700604 pci_bus = pci_find_bus(seg, bus);
605 if (!pci_bus) {
606 err("Can't find bus %04x:%02x\n", seg, bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608 }
609
Rajesh Shah42f49a62005-04-28 00:25:53 -0700610 /* check if this bridge has ejectable slots */
611 if (detect_ejectable_slots(handle) > 0) {
612 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
613 add_host_bridge(handle, pci_bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* search P2P bridges under this host bridge */
617 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700618 find_p2p_bridge, pci_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900621 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 return 0;
624}
625
Rajesh Shah42f49a62005-04-28 00:25:53 -0700626static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
627{
628 struct list_head *head;
629 list_for_each(head, &bridge_list) {
630 struct acpiphp_bridge *bridge = list_entry(head,
631 struct acpiphp_bridge, list);
632 if (bridge->handle == handle)
633 return bridge;
634 }
635
636 return NULL;
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Rajesh Shah364d5092005-04-28 00:25:54 -0700639static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Rajesh Shah42f49a62005-04-28 00:25:53 -0700641 struct list_head *list, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700642 struct acpiphp_slot *slot;
643 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700644 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700645
646 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
647 handle_hotplug_event_bridge);
648 if (ACPI_FAILURE(status))
649 err("failed to remove notify handler\n");
650
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900651 if ((bridge->type != BRIDGE_TYPE_HOST) &&
652 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
653 status = acpi_install_notify_handler(bridge->func->handle,
654 ACPI_SYSTEM_NOTIFY,
655 handle_hotplug_event_func,
656 bridge->func);
657 if (ACPI_FAILURE(status))
658 err("failed to install interrupt notify handler\n");
659 }
660
Rajesh Shah42f49a62005-04-28 00:25:53 -0700661 slot = bridge->slots;
662 while (slot) {
663 struct acpiphp_slot *next = slot->next;
664 list_for_each_safe (list, tmp, &slot->funcs) {
665 struct acpiphp_func *func;
666 func = list_entry(list, struct acpiphp_func, sibling);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400667 if (is_dock_device(func->handle)) {
668 unregister_hotplug_dock_device(func->handle);
669 unregister_dock_notifier(&func->nb);
670 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800671 if (!(func->flags & FUNC_HAS_DCK)) {
672 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700673 ACPI_SYSTEM_NOTIFY,
674 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800675 if (ACPI_FAILURE(status))
676 err("failed to remove notify handler\n");
677 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700678 pci_dev_put(func->pci_dev);
679 list_del(list);
680 kfree(func);
681 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800682 acpiphp_unregister_hotplug_slot(slot);
683 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700684 kfree(slot);
685 slot = next;
686 }
687
688 pci_dev_put(bridge->pci_dev);
689 list_del(&bridge->list);
690 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Rajesh Shah364d5092005-04-28 00:25:54 -0700693static acpi_status
694cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
695{
696 struct acpiphp_bridge *bridge;
697
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900698 /* cleanup p2p bridges under this P2P bridge
699 in a depth-first manner */
700 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
701 cleanup_p2p_bridge, NULL, NULL);
702
Rajesh Shah364d5092005-04-28 00:25:54 -0700703 if (!(bridge = acpiphp_handle_to_bridge(handle)))
704 return AE_OK;
705 cleanup_bridge(bridge);
706 return AE_OK;
707}
708
709static void remove_bridge(acpi_handle handle)
710{
711 struct acpiphp_bridge *bridge;
712
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900713 /* cleanup p2p bridges under this host bridge
714 in a depth-first manner */
715 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
716 (u32)1, cleanup_p2p_bridge, NULL, NULL);
717
Rajesh Shah364d5092005-04-28 00:25:54 -0700718 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900719 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700720 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700721}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700723static struct pci_dev * get_apic_pci_info(acpi_handle handle)
724{
725 struct acpi_pci_id id;
726 struct pci_bus *bus;
727 struct pci_dev *dev;
728
729 if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
730 return NULL;
731
732 bus = pci_find_bus(id.segment, id.bus);
733 if (!bus)
734 return NULL;
735
736 dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
737 if (!dev)
738 return NULL;
739
740 if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
741 (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
742 {
743 pci_dev_put(dev);
744 return NULL;
745 }
746
747 return dev;
748}
749
750static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
751{
752 acpi_status status;
753 int result = -1;
754 unsigned long gsb;
755 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
756 union acpi_object *obj;
757 void *table;
758
759 status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
760 if (ACPI_SUCCESS(status)) {
761 *gsi_base = (u32)gsb;
762 return 0;
763 }
764
765 status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
766 if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
767 return -1;
768
769 obj = buffer.pointer;
770 if (obj->type != ACPI_TYPE_BUFFER)
771 goto out;
772
773 table = obj->buffer.pointer;
774 switch (((acpi_table_entry_header *)table)->type) {
775 case ACPI_MADT_IOSAPIC:
776 *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
777 result = 0;
778 break;
779 case ACPI_MADT_IOAPIC:
780 *gsi_base = ((struct acpi_table_ioapic *)table)->global_irq_base;
781 result = 0;
782 break;
783 default:
784 break;
785 }
786 out:
Kristen Accardi81b26bc2006-04-18 14:36:43 -0700787 kfree(buffer.pointer);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700788 return result;
789}
790
791static acpi_status
792ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
793{
794 acpi_status status;
795 unsigned long sta;
796 acpi_handle tmp;
797 struct pci_dev *pdev;
798 u32 gsi_base;
799 u64 phys_addr;
800
801 /* Evaluate _STA if present */
802 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
803 if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
804 return AE_CTRL_DEPTH;
805
806 /* Scan only PCI bus scope */
807 status = acpi_get_handle(handle, "_HID", &tmp);
808 if (ACPI_SUCCESS(status))
809 return AE_CTRL_DEPTH;
810
811 if (get_gsi_base(handle, &gsi_base))
812 return AE_OK;
813
814 pdev = get_apic_pci_info(handle);
815 if (!pdev)
816 return AE_OK;
817
818 if (pci_enable_device(pdev)) {
819 pci_dev_put(pdev);
820 return AE_OK;
821 }
822
823 pci_set_master(pdev);
824
825 if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
826 pci_disable_device(pdev);
827 pci_dev_put(pdev);
828 return AE_OK;
829 }
830
831 phys_addr = pci_resource_start(pdev, 0);
832 if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
833 pci_release_region(pdev, 0);
834 pci_disable_device(pdev);
835 pci_dev_put(pdev);
836 return AE_OK;
837 }
838
839 return AE_OK;
840}
841
842static int acpiphp_configure_ioapics(acpi_handle handle)
843{
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -0700844 ioapic_add(handle, 0, NULL, NULL);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -0700845 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
846 ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
847 return 0;
848}
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850static int power_on_slot(struct acpiphp_slot *slot)
851{
852 acpi_status status;
853 struct acpiphp_func *func;
854 struct list_head *l;
855 int retval = 0;
856
857 /* if already enabled, just skip */
858 if (slot->flags & SLOT_POWEREDON)
859 goto err_exit;
860
861 list_for_each (l, &slot->funcs) {
862 func = list_entry(l, struct acpiphp_func, sibling);
863
864 if (func->flags & FUNC_HAS_PS0) {
865 dbg("%s: executing _PS0\n", __FUNCTION__);
866 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
867 if (ACPI_FAILURE(status)) {
868 warn("%s: _PS0 failed\n", __FUNCTION__);
869 retval = -1;
870 goto err_exit;
871 } else
872 break;
873 }
874 }
875
876 /* TBD: evaluate _STA to check if the slot is enabled */
877
878 slot->flags |= SLOT_POWEREDON;
879
880 err_exit:
881 return retval;
882}
883
884
885static int power_off_slot(struct acpiphp_slot *slot)
886{
887 acpi_status status;
888 struct acpiphp_func *func;
889 struct list_head *l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 int retval = 0;
892
893 /* if already disabled, just skip */
894 if ((slot->flags & SLOT_POWEREDON) == 0)
895 goto err_exit;
896
897 list_for_each (l, &slot->funcs) {
898 func = list_entry(l, struct acpiphp_func, sibling);
899
Rajesh Shah2f523b12005-04-28 00:25:55 -0700900 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
902 if (ACPI_FAILURE(status)) {
903 warn("%s: _PS3 failed\n", __FUNCTION__);
904 retval = -1;
905 goto err_exit;
906 } else
907 break;
908 }
909 }
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /* TBD: evaluate _STA to check if the slot is disabled */
912
913 slot->flags &= (~SLOT_POWEREDON);
914
915 err_exit:
916 return retval;
917}
918
919
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800920
921/**
922 * acpiphp_max_busnr - return the highest reserved bus number under
923 * the given bus.
924 * @bus: bus to start search with
925 *
926 */
927static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
928{
929 struct list_head *tmp;
930 unsigned char max, n;
931
932 /*
933 * pci_bus_max_busnr will return the highest
934 * reserved busnr for all these children.
935 * that is equivalent to the bus->subordinate
936 * value. We don't want to use the parent's
937 * bus->subordinate value because it could have
938 * padding in it.
939 */
940 max = bus->secondary;
941
942 list_for_each(tmp, &bus->children) {
943 n = pci_bus_max_busnr(pci_bus_b(tmp));
944 if (n > max)
945 max = n;
946 }
947 return max;
948}
949
950
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800951/**
952 * acpiphp_bus_add - add a new bus to acpi subsystem
953 * @func: acpiphp_func of the bridge
954 *
955 */
956static int acpiphp_bus_add(struct acpiphp_func *func)
957{
958 acpi_handle phandle;
959 struct acpi_device *device, *pdevice;
960 int ret_val;
961
962 acpi_get_parent(func->handle, &phandle);
963 if (acpi_bus_get_device(phandle, &pdevice)) {
964 dbg("no parent device, assuming NULL\n");
965 pdevice = NULL;
966 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800967 if (!acpi_bus_get_device(func->handle, &device)) {
968 dbg("bus exists... trim\n");
969 /* this shouldn't be in here, so remove
970 * the bus then re-add it...
971 */
972 ret_val = acpi_bus_trim(device, 1);
973 dbg("acpi_bus_trim return %x\n", ret_val);
974 }
975
976 ret_val = acpi_bus_add(&device, pdevice, func->handle,
977 ACPI_BUS_TYPE_DEVICE);
978 if (ret_val) {
979 dbg("error adding bus, %x\n",
980 -ret_val);
981 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800982 }
983 /*
984 * try to start anyway. We could have failed to add
985 * simply because this bus had previously been added
986 * on another add. Don't bother with the return value
987 * we just keep going.
988 */
989 ret_val = acpi_bus_start(device);
990
991acpiphp_bus_add_out:
992 return ret_val;
993}
994
995
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900996/**
997 * acpiphp_bus_trim - trim a bus from acpi subsystem
998 * @handle: handle to acpi namespace
999 *
1000 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -07001001static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001002{
1003 struct acpi_device *device;
1004 int retval;
1005
1006 retval = acpi_bus_get_device(handle, &device);
1007 if (retval) {
1008 dbg("acpi_device not found\n");
1009 return retval;
1010 }
1011
1012 retval = acpi_bus_trim(device, 1);
1013 if (retval)
1014 err("cannot remove from acpi list\n");
1015
1016 return retval;
1017}
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/**
1020 * enable_device - enable, configure a slot
1021 * @slot: slot to be enabled
1022 *
1023 * This function should be called per *physical slot*,
1024 * not per each slot object in ACPI namespace.
1025 *
1026 */
1027static int enable_device(struct acpiphp_slot *slot)
1028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001030 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct list_head *l;
1032 struct acpiphp_func *func;
1033 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001034 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001035 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 if (slot->flags & SLOT_ENABLED)
1038 goto err_exit;
1039
1040 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -07001041 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (dev) {
1043 /* This case shouldn't happen */
1044 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -07001045 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 retval = -1;
1047 goto err_exit;
1048 }
1049
Rajesh Shah42f49a62005-04-28 00:25:53 -07001050 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
1051 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 err("No new device found\n");
1053 retval = -1;
1054 goto err_exit;
1055 }
1056
Kristen Accardi15a1ae72006-02-23 17:55:58 -08001057 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001058 for (pass = 0; pass < 2; pass++) {
1059 list_for_each_entry(dev, &bus->devices, bus_list) {
1060 if (PCI_SLOT(dev->devfn) != slot->device)
1061 continue;
1062 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001063 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001064 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001065 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -08001066 pci_bus_size_bridges(dev->subordinate);
1067 }
Rajesh Shah42f49a62005-04-28 00:25:53 -07001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001071 list_for_each (l, &slot->funcs) {
1072 func = list_entry(l, struct acpiphp_func, sibling);
1073 acpiphp_bus_add(func);
1074 }
1075
Rajesh Shah42f49a62005-04-28 00:25:53 -07001076 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001077 acpiphp_sanitize_bus(bus);
Satoru Takeuchi287af2f2006-09-12 10:12:16 -07001078 acpiphp_set_hpp_values(slot->bridge->handle, bus);
Satoru Takeuchi9b1d19e2006-09-12 10:15:10 -07001079 list_for_each_entry(func, &slot->funcs, sibling)
1080 acpiphp_configure_ioapics(func->handle);
Kristen Accardi8e5dce32005-10-18 17:21:40 -07001081 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001082 pci_bus_add_devices(bus);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* associate pci_dev to our representation */
1085 list_for_each (l, &slot->funcs) {
1086 func = list_entry(l, struct acpiphp_func, sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -07001087 func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 func->function));
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001089 if (!func->pci_dev)
1090 continue;
1091
1092 if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
1093 func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
1094 continue;
1095
1096 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
1097 if (ACPI_FAILURE(status))
1098 warn("find_p2p_bridge failed (error code = 0x%x)\n",
1099 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101
1102 slot->flags |= SLOT_ENABLED;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 err_exit:
1105 return retval;
1106}
1107
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001108static void disable_bridges(struct pci_bus *bus)
1109{
1110 struct pci_dev *dev;
1111 list_for_each_entry(dev, &bus->devices, bus_list) {
1112 if (dev->subordinate) {
1113 disable_bridges(dev->subordinate);
1114 pci_disable_device(dev);
1115 }
1116 }
1117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119/**
1120 * disable_device - disable a slot
1121 */
1122static int disable_device(struct acpiphp_slot *slot)
1123{
1124 int retval = 0;
1125 struct acpiphp_func *func;
1126 struct list_head *l;
1127
1128 /* is this slot already disabled? */
1129 if (!(slot->flags & SLOT_ENABLED))
1130 goto err_exit;
1131
1132 list_for_each (l, &slot->funcs) {
1133 func = list_entry(l, struct acpiphp_func, sibling);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001134
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001135 if (func->bridge) {
1136 /* cleanup p2p bridges under this P2P bridge */
1137 cleanup_p2p_bridge(func->bridge->handle,
1138 (u32)1, NULL, NULL);
1139 func->bridge = NULL;
1140 }
1141
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001142 if (func->pci_dev) {
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001143 pci_stop_bus_device(func->pci_dev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -07001144 if (func->pci_dev->subordinate) {
1145 disable_bridges(func->pci_dev->subordinate);
1146 pci_disable_device(func->pci_dev);
1147 }
1148 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -07001149
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +09001150 acpiphp_bus_trim(func->handle);
1151 /* try to remove anyway.
1152 * acpiphp_bus_add might have been failed */
1153
Rajesh Shah42f49a62005-04-28 00:25:53 -07001154 if (!func->pci_dev)
1155 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Rajesh Shah42f49a62005-04-28 00:25:53 -07001157 pci_remove_bus_device(func->pci_dev);
1158 pci_dev_put(func->pci_dev);
1159 func->pci_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
1161
1162 slot->flags &= (~SLOT_ENABLED);
1163
1164 err_exit:
1165 return retval;
1166}
1167
1168
1169/**
1170 * get_slot_status - get ACPI slot status
1171 *
1172 * if a slot has _STA for each function and if any one of them
1173 * returned non-zero status, return it
1174 *
1175 * if a slot doesn't have _STA and if any one of its functions'
1176 * configuration space is configured, return 0x0f as a _STA
1177 *
1178 * otherwise return 0
1179 */
1180static unsigned int get_slot_status(struct acpiphp_slot *slot)
1181{
1182 acpi_status status;
1183 unsigned long sta = 0;
1184 u32 dvid;
1185 struct list_head *l;
1186 struct acpiphp_func *func;
1187
1188 list_for_each (l, &slot->funcs) {
1189 func = list_entry(l, struct acpiphp_func, sibling);
1190
1191 if (func->flags & FUNC_HAS_STA) {
1192 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
1193 if (ACPI_SUCCESS(status) && sta)
1194 break;
1195 } else {
1196 pci_bus_read_config_dword(slot->bridge->pci_bus,
1197 PCI_DEVFN(slot->device,
1198 func->function),
1199 PCI_VENDOR_ID, &dvid);
1200 if (dvid != 0xffffffff) {
1201 sta = ACPI_STA_ALL;
1202 break;
1203 }
1204 }
1205 }
1206
1207 return (unsigned int)sta;
1208}
1209
1210/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001211 * acpiphp_eject_slot - physically eject the slot
1212 */
1213static int acpiphp_eject_slot(struct acpiphp_slot *slot)
1214{
1215 acpi_status status;
1216 struct acpiphp_func *func;
1217 struct list_head *l;
1218 struct acpi_object_list arg_list;
1219 union acpi_object arg;
1220
1221 list_for_each (l, &slot->funcs) {
1222 func = list_entry(l, struct acpiphp_func, sibling);
1223
1224 /* We don't want to call _EJ0 on non-existing functions. */
1225 if ((func->flags & FUNC_HAS_EJ0)) {
1226 /* _EJ0 method take one argument */
1227 arg_list.count = 1;
1228 arg_list.pointer = &arg;
1229 arg.type = ACPI_TYPE_INTEGER;
1230 arg.integer.value = 1;
1231
1232 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1233 if (ACPI_FAILURE(status)) {
1234 warn("%s: _EJ0 failed\n", __FUNCTION__);
1235 return -1;
1236 } else
1237 break;
1238 }
1239 }
1240 return 0;
1241}
1242
1243/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * acpiphp_check_bridge - re-enumerate devices
1245 *
1246 * Iterate over all slots under this bridge and make sure that if a
1247 * card is present they are enabled, and if not they are disabled.
1248 */
1249static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1250{
1251 struct acpiphp_slot *slot;
1252 int retval = 0;
1253 int enabled, disabled;
1254
1255 enabled = disabled = 0;
1256
1257 for (slot = bridge->slots; slot; slot = slot->next) {
1258 unsigned int status = get_slot_status(slot);
1259 if (slot->flags & SLOT_ENABLED) {
1260 if (status == ACPI_STA_ALL)
1261 continue;
1262 retval = acpiphp_disable_slot(slot);
1263 if (retval) {
1264 err("Error occurred in disabling\n");
1265 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001266 } else {
1267 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 disabled++;
1270 } else {
1271 if (status != ACPI_STA_ALL)
1272 continue;
1273 retval = acpiphp_enable_slot(slot);
1274 if (retval) {
1275 err("Error occurred in enabling\n");
1276 goto err_exit;
1277 }
1278 enabled++;
1279 }
1280 }
1281
1282 dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
1283
1284 err_exit:
1285 return retval;
1286}
1287
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001288static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
1289{
1290 u16 pci_cmd, pci_bctl;
1291 struct pci_dev *cdev;
1292
1293 /* Program hpp values for this device */
1294 if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
1295 (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
1296 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
1297 return;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001298
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001299 pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001300 bridge->hpp.t0->cache_line_size);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001301 pci_write_config_byte(dev, PCI_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001302 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001303 pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001304 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001305 pci_cmd |= PCI_COMMAND_SERR;
1306 else
1307 pci_cmd &= ~PCI_COMMAND_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001308 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001309 pci_cmd |= PCI_COMMAND_PARITY;
1310 else
1311 pci_cmd &= ~PCI_COMMAND_PARITY;
1312 pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
1313
1314 /* Program bridge control value and child devices */
1315 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
1316 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001317 bridge->hpp.t0->latency_timer);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001318 pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001319 if (bridge->hpp.t0->enable_serr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001320 pci_bctl |= PCI_BRIDGE_CTL_SERR;
1321 else
1322 pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
Kenji Kaneshigee22b73502006-05-02 10:57:14 +09001323 if (bridge->hpp.t0->enable_perr)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001324 pci_bctl |= PCI_BRIDGE_CTL_PARITY;
1325 else
1326 pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
1327 pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
1328 if (dev->subordinate) {
1329 list_for_each_entry(cdev, &dev->subordinate->devices,
1330 bus_list)
1331 program_hpp(cdev, bridge);
1332 }
1333 }
1334}
1335
1336static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
1337{
1338 struct acpiphp_bridge bridge;
1339 struct pci_dev *dev;
1340
1341 memset(&bridge, 0, sizeof(bridge));
1342 bridge.handle = handle;
Kenji Kaneshige7430e342006-05-02 10:54:50 +09001343 bridge.pci_bus = bus;
Kristen Accardi783c49f2006-03-03 10:16:05 -08001344 bridge.pci_dev = bus->self;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001345 decode_hpp(&bridge);
1346 list_for_each_entry(dev, &bus->devices, bus_list)
1347 program_hpp(dev, &bridge);
1348
1349}
1350
1351/*
1352 * Remove devices for which we could not assign resources, call
1353 * arch specific code to fix-up the bus
1354 */
1355static void acpiphp_sanitize_bus(struct pci_bus *bus)
1356{
1357 struct pci_dev *dev;
1358 int i;
1359 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1360
1361 list_for_each_entry(dev, &bus->devices, bus_list) {
1362 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1363 struct resource *res = &dev->resource[i];
1364 if ((res->flags & type_mask) && !res->start &&
1365 res->end) {
1366 /* Could not assign a required resources
1367 * for this device, remove it */
1368 pci_remove_bus_device(dev);
1369 break;
1370 }
1371 }
1372 }
1373}
1374
1375/* Program resources in newly inserted bridge */
1376static int acpiphp_configure_bridge (acpi_handle handle)
1377{
1378 struct acpi_pci_id pci_id;
1379 struct pci_bus *bus;
1380
1381 if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
1382 err("cannot get PCI domain and bus number for bridge\n");
1383 return -EINVAL;
1384 }
1385 bus = pci_find_bus(pci_id.segment, pci_id.bus);
1386 if (!bus) {
1387 err("cannot find bus %d:%d\n",
1388 pci_id.segment, pci_id.bus);
1389 return -EINVAL;
1390 }
1391
1392 pci_bus_size_bridges(bus);
1393 pci_bus_assign_resources(bus);
1394 acpiphp_sanitize_bus(bus);
1395 acpiphp_set_hpp_values(handle, bus);
1396 pci_enable_bridges(bus);
Kenji Kaneshigea0d399a2005-04-28 00:25:59 -07001397 acpiphp_configure_ioapics(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001398 return 0;
1399}
1400
1401static void handle_bridge_insertion(acpi_handle handle, u32 type)
1402{
1403 struct acpi_device *device, *pdevice;
1404 acpi_handle phandle;
1405
1406 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1407 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1408 err("unexpected notification type %d\n", type);
1409 return;
1410 }
1411
1412 acpi_get_parent(handle, &phandle);
1413 if (acpi_bus_get_device(phandle, &pdevice)) {
1414 dbg("no parent device, assuming NULL\n");
1415 pdevice = NULL;
1416 }
1417 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1418 err("cannot add bridge to acpi list\n");
1419 return;
1420 }
1421 if (!acpiphp_configure_bridge(handle) &&
1422 !acpi_bus_start(device))
1423 add_bridge(handle);
1424 else
1425 err("cannot configure and start bridge\n");
1426
1427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
1430 * ACPI event handlers
1431 */
1432
1433/**
1434 * handle_hotplug_event_bridge - handle ACPI event on bridges
1435 *
1436 * @handle: Notify()'ed acpi_handle
1437 * @type: Notify code
1438 * @context: pointer to acpiphp_bridge structure
1439 *
1440 * handles ACPI event notification on {host,p2p} bridges
1441 *
1442 */
1443static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1444{
1445 struct acpiphp_bridge *bridge;
1446 char objname[64];
1447 struct acpi_buffer buffer = { .length = sizeof(objname),
1448 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001449 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001451 if (acpi_bus_get_device(handle, &device)) {
1452 /* This bridge must have just been physically inserted */
1453 handle_bridge_insertion(handle, type);
1454 return;
1455 }
1456
1457 bridge = acpiphp_handle_to_bridge(handle);
1458 if (!bridge) {
1459 err("cannot get bridge info\n");
1460 return;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1464
1465 switch (type) {
1466 case ACPI_NOTIFY_BUS_CHECK:
1467 /* bus re-enumerate */
1468 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1469 acpiphp_check_bridge(bridge);
1470 break;
1471
1472 case ACPI_NOTIFY_DEVICE_CHECK:
1473 /* device check */
1474 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1475 acpiphp_check_bridge(bridge);
1476 break;
1477
1478 case ACPI_NOTIFY_DEVICE_WAKE:
1479 /* wake event */
1480 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1481 break;
1482
1483 case ACPI_NOTIFY_EJECT_REQUEST:
1484 /* request device eject */
1485 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001486 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1487 (bridge->flags & BRIDGE_HAS_EJ0)) {
1488 struct acpiphp_slot *slot;
1489 slot = bridge->func->slot;
1490 if (!acpiphp_disable_slot(slot))
1491 acpiphp_eject_slot(slot);
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494
1495 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1496 printk(KERN_ERR "Device %s cannot be configured due"
1497 " to a frequency mismatch\n", objname);
1498 break;
1499
1500 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1501 printk(KERN_ERR "Device %s cannot be configured due"
1502 " to a bus mode mismatch\n", objname);
1503 break;
1504
1505 case ACPI_NOTIFY_POWER_FAULT:
1506 printk(KERN_ERR "Device %s has suffered a power fault\n",
1507 objname);
1508 break;
1509
1510 default:
1511 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1512 break;
1513 }
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/**
1517 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1518 *
1519 * @handle: Notify()'ed acpi_handle
1520 * @type: Notify code
1521 * @context: pointer to acpiphp_func structure
1522 *
1523 * handles ACPI event notification on slots
1524 *
1525 */
Len Brown2b85e132006-06-27 01:50:14 -04001526static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 struct acpiphp_func *func;
1529 char objname[64];
1530 struct acpi_buffer buffer = { .length = sizeof(objname),
1531 .pointer = objname };
1532
1533 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1534
1535 func = (struct acpiphp_func *)context;
1536
1537 switch (type) {
1538 case ACPI_NOTIFY_BUS_CHECK:
1539 /* bus re-enumerate */
1540 dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
1541 acpiphp_enable_slot(func->slot);
1542 break;
1543
1544 case ACPI_NOTIFY_DEVICE_CHECK:
1545 /* device check : re-enumerate from parent bus */
1546 dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
1547 acpiphp_check_bridge(func->slot->bridge);
1548 break;
1549
1550 case ACPI_NOTIFY_DEVICE_WAKE:
1551 /* wake event */
1552 dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
1553 break;
1554
1555 case ACPI_NOTIFY_EJECT_REQUEST:
1556 /* request device eject */
1557 dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001558 if (!(acpiphp_disable_slot(func->slot)))
1559 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 break;
1561
1562 default:
1563 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1564 break;
1565 }
1566}
1567
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001568
1569static acpi_status
1570find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1571{
1572 int *count = (int *)context;
1573
Kristen Accardi783c49f2006-03-03 10:16:05 -08001574 if (acpi_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001575 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1576 handle_hotplug_event_bridge, NULL);
1577 (*count)++;
1578 }
1579 return AE_OK ;
1580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582static struct acpi_pci_driver acpi_pci_hp_driver = {
1583 .add = add_bridge,
1584 .remove = remove_bridge,
1585};
1586
1587/**
1588 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
1589 *
1590 */
1591int __init acpiphp_glue_init(void)
1592{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001593 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001595 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1596 ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 if (num <= 0)
1599 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001600 else
1601 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 return 0;
1604}
1605
1606
1607/**
1608 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1609 *
1610 * This function frees all data allocated in acpiphp_glue_init()
1611 */
1612void __exit acpiphp_glue_exit(void)
1613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1615}
1616
1617
1618/**
1619 * acpiphp_get_num_slots - count number of slots in a system
1620 */
1621int __init acpiphp_get_num_slots(void)
1622{
1623 struct list_head *node;
1624 struct acpiphp_bridge *bridge;
1625 int num_slots;
1626
1627 num_slots = 0;
1628
1629 list_for_each (node, &bridge_list) {
1630 bridge = (struct acpiphp_bridge *)node;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001631 dbg("Bus %04x:%02x has %d slot%s\n",
1632 pci_domain_nr(bridge->pci_bus),
1633 bridge->pci_bus->number, bridge->nr_slots,
1634 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 num_slots += bridge->nr_slots;
1636 }
1637
Rajesh Shah42f49a62005-04-28 00:25:53 -07001638 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return num_slots;
1640}
1641
1642
1643#if 0
1644/**
1645 * acpiphp_for_each_slot - call function for each slot
1646 * @fn: callback function
1647 * @data: context to be passed to callback function
1648 *
1649 */
1650static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1651{
1652 struct list_head *node;
1653 struct acpiphp_bridge *bridge;
1654 struct acpiphp_slot *slot;
1655 int retval = 0;
1656
1657 list_for_each (node, &bridge_list) {
1658 bridge = (struct acpiphp_bridge *)node;
1659 for (slot = bridge->slots; slot; slot = slot->next) {
1660 retval = fn(slot, data);
1661 if (!retval)
1662 goto err_exit;
1663 }
1664 }
1665
1666 err_exit:
1667 return retval;
1668}
1669#endif
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672/**
1673 * acpiphp_enable_slot - power on slot
1674 */
1675int acpiphp_enable_slot(struct acpiphp_slot *slot)
1676{
1677 int retval;
1678
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001679 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* wake up all functions */
1682 retval = power_on_slot(slot);
1683 if (retval)
1684 goto err_exit;
1685
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001686 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /* configure all functions */
1688 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001689 if (retval)
1690 power_off_slot(slot);
1691 } else {
1692 dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
1693 power_off_slot(slot);
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001697 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return retval;
1699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/**
1702 * acpiphp_disable_slot - power off slot
1703 */
1704int acpiphp_disable_slot(struct acpiphp_slot *slot)
1705{
1706 int retval = 0;
1707
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001708 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /* unconfigure all functions */
1711 retval = disable_device(slot);
1712 if (retval)
1713 goto err_exit;
1714
1715 /* power off all functions */
1716 retval = power_off_slot(slot);
1717 if (retval)
1718 goto err_exit;
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001721 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return retval;
1723}
1724
1725
1726/*
1727 * slot enabled: 1
1728 * slot disabled: 0
1729 */
1730u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1731{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001732 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
1735
1736/*
1737 * latch closed: 1
1738 * latch open: 0
1739 */
1740u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1741{
1742 unsigned int sta;
1743
1744 sta = get_slot_status(slot);
1745
1746 return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
1747}
1748
1749
1750/*
1751 * adapter presence : 1
1752 * absence : 0
1753 */
1754u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1755{
1756 unsigned int sta;
1757
1758 sta = get_slot_status(slot);
1759
1760 return (sta == 0) ? 0 : 1;
1761}
1762
1763
1764/*
1765 * pci address (seg/bus/dev)
1766 */
1767u32 acpiphp_get_address(struct acpiphp_slot *slot)
1768{
1769 u32 address;
Rajesh Shah42f49a62005-04-28 00:25:53 -07001770 struct pci_bus *pci_bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Rajesh Shah42f49a62005-04-28 00:25:53 -07001772 address = (pci_domain_nr(pci_bus) << 16) |
1773 (pci_bus->number << 8) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 slot->device;
1775
1776 return address;
1777}