blob: cb2fd01eddaed6fa23b679ecadf86d15171e8518 [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:
Rajesh Shah42f49a62005-04-28 00:25:53 -070035 * - 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.
Alex Chiang5d4a4b22009-03-30 10:50:14 -060038 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
Rajesh Shah42f49a62005-04-28 00:25:53 -070040 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/module.h>
44
45#include <linux/kernel.h>
46#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070047#include <linux/pci_hotplug.h>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090048#include <linux/pci-acpi.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "acpiphp.h"
53
54static LIST_HEAD(bridge_list);
55
56#define MY_NAME "acpiphp_glue"
57
58static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070059static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060060static void acpiphp_set_hpp_values(struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040061static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070062
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090063/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040064static acpi_status
65is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
66{
67 int *count = (int *)context;
68
69 if (is_dock_device(handle)) {
70 (*count)++;
71 return AE_CTRL_TERMINATE;
72 } else {
73 return AE_OK;
74 }
75}
76
Kristen Accardi4e8662b2006-06-28 03:08:06 -040077/*
78 * the _DCK method can do funny things... and sometimes not
79 * hah-hah funny.
80 *
81 * TBD - figure out a way to only call fixups for
82 * systems that require them.
83 */
84static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
85 void *v)
86{
87 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
88 struct pci_bus *bus = func->slot->bridge->pci_bus;
89 u32 buses;
90
91 if (!bus->self)
92 return NOTIFY_OK;
93
94 /* fixup bad _DCK function that rewrites
95 * secondary bridge on slot
96 */
97 pci_read_config_dword(bus->self,
98 PCI_PRIMARY_BUS,
99 &buses);
100
101 if (((buses >> 8) & 0xff) != bus->secondary) {
102 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700103 | ((unsigned int)(bus->primary) << 0)
104 | ((unsigned int)(bus->secondary) << 8)
105 | ((unsigned int)(bus->subordinate) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400106 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
107 }
108 return NOTIFY_OK;
109}
110
111
Shaohua Li1253f7a2008-08-28 10:06:16 +0800112static struct acpi_dock_ops acpiphp_dock_ops = {
113 .handler = handle_hotplug_event_func,
114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* callback routine to register each ACPI PCI slot object */
117static acpi_status
118register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
119{
120 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
121 struct acpiphp_slot *slot;
122 struct acpiphp_func *newfunc;
123 acpi_handle tmp;
124 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400125 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800126 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900127 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600128 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900130 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return AE_OK;
132
Matthew Garrett56ee3252008-11-25 21:48:14 +0000133 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 device = (adr >> 16) & 0xffff;
135 function = adr & 0xffff;
136
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100137 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (!newfunc)
139 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 INIT_LIST_HEAD(&newfunc->sibling);
142 newfunc->handle = handle;
143 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000144
145 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800146 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
149 newfunc->flags |= FUNC_HAS_STA;
150
151 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
152 newfunc->flags |= FUNC_HAS_PS0;
153
154 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
155 newfunc->flags |= FUNC_HAS_PS3;
156
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400157 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800158 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400161 if (ACPI_FAILURE(status)) {
162 /*
163 * use the count of the number of slots we've found
164 * for the number of the slot
165 */
166 sun = bridge->nr_slots+1;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /* search for objects that share the same slot */
170 for (slot = bridge->slots; slot; slot = slot->next)
171 if (slot->device == device) {
172 if (slot->sun != sun)
173 warn("sibling found, but _SUN doesn't match!\n");
174 break;
175 }
176
177 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100178 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!slot) {
180 kfree(newfunc);
181 return AE_NO_MEMORY;
182 }
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 slot->device = device;
186 slot->sun = sun;
187 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100188 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 slot->next = bridge->slots;
191 bridge->slots = slot;
192
193 bridge->nr_slots++;
194
Justin Chenb6adc192008-12-11 11:16:44 -0700195 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900196 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800197 retval = acpiphp_register_hotplug_slot(slot);
198 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600199 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700200 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600201 "hotplug driver\n", slot->sun);
202 else
203 warn("acpiphp_register_hotplug_slot failed "
204 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800205 goto err_exit;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 newfunc->slot = slot;
210 list_add_tail(&newfunc->sibling, &slot->funcs);
211
Alex Chiang9d911d72009-05-21 16:21:15 -0600212 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
213 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600215 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400218 if (is_dock_device(handle)) {
219 /* we don't want to call this device's _EJ0
220 * because we want the dock notify handler
221 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800222 */
223 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400224 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800225 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400226 dbg("failed to register dock device\n");
227
228 /* we need to be notified when dock events happen
229 * outside of the hotplug operation, since we may
230 * need to do fixups before we can hotplug.
231 */
232 newfunc->nb.notifier_call = post_dock_fixups;
233 if (register_dock_notifier(&newfunc->nb))
234 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800235 }
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800238 if (!(newfunc->flags & FUNC_HAS_DCK)) {
239 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 ACPI_SYSTEM_NOTIFY,
241 handle_hotplug_event_func,
242 newfunc);
243
Kristen Accardi20416ea2006-02-23 17:56:03 -0800244 if (ACPI_FAILURE(status))
245 err("failed to register interrupt notify handler\n");
246 } else
247 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Kristen Accardi20416ea2006-02-23 17:56:03 -0800249 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800250
251 err_exit:
252 bridge->nr_slots--;
253 bridge->slots = slot->next;
254 kfree(slot);
255 kfree(newfunc);
256
257 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260
261/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600262static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Alex Chiang7f538662009-09-10 12:34:09 -0600264 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900265 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600266 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800267 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900268 }
269 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
273static void init_bridge_misc(struct acpiphp_bridge *bridge)
274{
275 acpi_status status;
276
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800277 /* must be added to the list prior to calling register_slot */
278 list_add(&bridge->list, &bridge_list);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* register all slot objects under this bridge */
281 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800282 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800283 if (ACPI_FAILURE(status)) {
284 list_del(&bridge->list);
285 return;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700289 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900290 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
291 status = acpi_remove_notify_handler(bridge->func->handle,
292 ACPI_SYSTEM_NOTIFY,
293 handle_hotplug_event_func);
294 if (ACPI_FAILURE(status))
295 err("failed to remove notify handler\n");
296 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700297 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 ACPI_SYSTEM_NOTIFY,
299 handle_hotplug_event_bridge,
300 bridge);
301
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700302 if (ACPI_FAILURE(status)) {
303 err("failed to register interrupt notify handler\n");
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900309/* find acpiphp_func from acpiphp_bridge */
310static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
311{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900312 struct acpiphp_bridge *bridge;
313 struct acpiphp_slot *slot;
314 struct acpiphp_func *func;
315
Alex Chiang58c08622009-10-26 21:25:27 -0600316 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900317 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600318 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900319 if (func->handle == handle)
320 return func;
321 }
322 }
323 }
324
325 return NULL;
326}
327
328
329static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
330{
331 acpi_handle dummy_handle;
332
333 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
334 "_STA", &dummy_handle)))
335 bridge->flags |= BRIDGE_HAS_STA;
336
337 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
338 "_EJ0", &dummy_handle)))
339 bridge->flags |= BRIDGE_HAS_EJ0;
340
341 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
342 "_PS0", &dummy_handle)))
343 bridge->flags |= BRIDGE_HAS_PS0;
344
345 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
346 "_PS3", &dummy_handle)))
347 bridge->flags |= BRIDGE_HAS_PS3;
348
349 /* is this ejectable p2p bridge? */
350 if (bridge->flags & BRIDGE_HAS_EJ0) {
351 struct acpiphp_func *func;
352
353 dbg("found ejectable p2p bridge\n");
354
355 /* make link between PCI bridge and PCI function */
356 func = acpiphp_bridge_handle_to_function(bridge->handle);
357 if (!func)
358 return;
359 bridge->func = func;
360 func->bridge = bridge;
361 }
362}
363
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* allocate and initialize host bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600366static void add_host_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct acpiphp_bridge *bridge;
Alex Chiang6edd7672009-09-10 12:34:04 -0600369 struct acpi_pci_root *root = acpi_pci_find_root(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100371 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (bridge == NULL)
373 return;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 bridge->type = BRIDGE_TYPE_HOST;
376 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Alex Chiang6edd7672009-09-10 12:34:04 -0600378 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 spin_lock_init(&bridge->res_lock);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 init_bridge_misc(bridge);
383}
384
385
386/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600387static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100391 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (bridge == NULL) {
393 err("out of memory\n");
394 return;
395 }
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 bridge->type = BRIDGE_TYPE_P2P;
398 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900399 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alex Chiang6edd7672009-09-10 12:34:04 -0600401 bridge->pci_dev = acpi_get_pci_dev(handle);
402 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (!bridge->pci_bus) {
404 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700405 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600408 /*
409 * Grab a ref to the subordinate PCI bus in case the bus is
410 * removed via PCI core logical hotplug. The ref pins the bus
411 * (which we access during module unload).
412 */
413 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_lock_init(&bridge->res_lock);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700417 return;
418 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600419 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700420 kfree(bridge);
421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424
425/* callback routine to find P2P bridges */
426static acpi_status
427find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
428{
429 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Alex Chiang6edd7672009-09-10 12:34:04 -0600432 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700433 if (!dev || !dev->subordinate)
434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600437 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600439 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900442 /* search P2P bridges under this p2p bridge */
443 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800444 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900445 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900446 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900447
Rajesh Shah42f49a62005-04-28 00:25:53 -0700448 out:
449 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return AE_OK;
451}
452
453
454/* find hot-pluggable slots, and then find P2P bridge */
455static int add_bridge(acpi_handle handle)
456{
457 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400458 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 acpi_handle dummy_handle;
460
461 /* if the bridge doesn't have _STA, we assume it is always there */
462 status = acpi_get_handle(handle, "_STA", &dummy_handle);
463 if (ACPI_SUCCESS(status)) {
464 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
465 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800466 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468 }
469 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
470 /* don't register this object */
471 return 0;
472 }
473
Rajesh Shah42f49a62005-04-28 00:25:53 -0700474 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600475 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700476 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Alex Chiang6edd7672009-09-10 12:34:04 -0600477 add_host_bridge(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* search P2P bridges under this host bridge */
481 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800482 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900485 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 return 0;
488}
489
Rajesh Shah42f49a62005-04-28 00:25:53 -0700490static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
491{
Alex Chiang58c08622009-10-26 21:25:27 -0600492 struct acpiphp_bridge *bridge;
493
494 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700495 if (bridge->handle == handle)
496 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700497
498 return NULL;
499}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Rajesh Shah364d5092005-04-28 00:25:54 -0700501static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Alex Chiang58c08622009-10-26 21:25:27 -0600503 struct acpiphp_slot *slot, *next;
504 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700505 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700506 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700507
508 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
509 handle_hotplug_event_bridge);
510 if (ACPI_FAILURE(status))
511 err("failed to remove notify handler\n");
512
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900513 if ((bridge->type != BRIDGE_TYPE_HOST) &&
514 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
515 status = acpi_install_notify_handler(bridge->func->handle,
516 ACPI_SYSTEM_NOTIFY,
517 handle_hotplug_event_func,
518 bridge->func);
519 if (ACPI_FAILURE(status))
520 err("failed to install interrupt notify handler\n");
521 }
522
Rajesh Shah42f49a62005-04-28 00:25:53 -0700523 slot = bridge->slots;
524 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600525 next = slot->next;
526 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400527 if (is_dock_device(func->handle)) {
528 unregister_hotplug_dock_device(func->handle);
529 unregister_dock_notifier(&func->nb);
530 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800531 if (!(func->flags & FUNC_HAS_DCK)) {
532 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700533 ACPI_SYSTEM_NOTIFY,
534 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800535 if (ACPI_FAILURE(status))
536 err("failed to remove notify handler\n");
537 }
Alex Chiang58c08622009-10-26 21:25:27 -0600538 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700539 kfree(func);
540 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800541 acpiphp_unregister_hotplug_slot(slot);
542 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700543 kfree(slot);
544 slot = next;
545 }
546
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600547 /*
548 * Only P2P bridges have a pci_dev
549 */
550 if (bridge->pci_dev)
551 put_device(&bridge->pci_bus->dev);
552
Rajesh Shah42f49a62005-04-28 00:25:53 -0700553 pci_dev_put(bridge->pci_dev);
554 list_del(&bridge->list);
555 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Rajesh Shah364d5092005-04-28 00:25:54 -0700558static acpi_status
559cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
560{
561 struct acpiphp_bridge *bridge;
562
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900563 /* cleanup p2p bridges under this P2P bridge
564 in a depth-first manner */
565 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800566 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900567
Alex Chianga13307c2008-07-01 20:02:23 -0600568 bridge = acpiphp_handle_to_bridge(handle);
569 if (bridge)
570 cleanup_bridge(bridge);
571
Rajesh Shah364d5092005-04-28 00:25:54 -0700572 return AE_OK;
573}
574
575static void remove_bridge(acpi_handle handle)
576{
577 struct acpiphp_bridge *bridge;
578
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900579 /* cleanup p2p bridges under this host bridge
580 in a depth-first manner */
581 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800582 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900583
Alex Chianga13307c2008-07-01 20:02:23 -0600584 /*
585 * On root bridges with hotplug slots directly underneath (ie,
586 * no p2p bridge inbetween), we call cleanup_bridge().
587 *
588 * The else clause cleans up root bridges that either had no
589 * hotplug slots at all, or had a p2p bridge underneath.
590 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700591 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900592 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700593 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600594 else
595 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
596 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599static int power_on_slot(struct acpiphp_slot *slot)
600{
601 acpi_status status;
602 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int retval = 0;
604
605 /* if already enabled, just skip */
606 if (slot->flags & SLOT_POWEREDON)
607 goto err_exit;
608
Alex Chiang58c08622009-10-26 21:25:27 -0600609 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800611 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
613 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800614 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 retval = -1;
616 goto err_exit;
617 } else
618 break;
619 }
620 }
621
622 /* TBD: evaluate _STA to check if the slot is enabled */
623
624 slot->flags |= SLOT_POWEREDON;
625
626 err_exit:
627 return retval;
628}
629
630
631static int power_off_slot(struct acpiphp_slot *slot)
632{
633 acpi_status status;
634 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 int retval = 0;
637
638 /* if already disabled, just skip */
639 if ((slot->flags & SLOT_POWEREDON) == 0)
640 goto err_exit;
641
Alex Chiang58c08622009-10-26 21:25:27 -0600642 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700643 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
645 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800646 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 retval = -1;
648 goto err_exit;
649 } else
650 break;
651 }
652 }
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* TBD: evaluate _STA to check if the slot is disabled */
655
656 slot->flags &= (~SLOT_POWEREDON);
657
658 err_exit:
659 return retval;
660}
661
662
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800663
664/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800665 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800666 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800667 */
668static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
669{
670 struct list_head *tmp;
671 unsigned char max, n;
672
673 /*
674 * pci_bus_max_busnr will return the highest
675 * reserved busnr for all these children.
676 * that is equivalent to the bus->subordinate
677 * value. We don't want to use the parent's
678 * bus->subordinate value because it could have
679 * padding in it.
680 */
681 max = bus->secondary;
682
683 list_for_each(tmp, &bus->children) {
684 n = pci_bus_max_busnr(pci_bus_b(tmp));
685 if (n > max)
686 max = n;
687 }
688 return max;
689}
690
691
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800692/**
693 * acpiphp_bus_add - add a new bus to acpi subsystem
694 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800695 */
696static int acpiphp_bus_add(struct acpiphp_func *func)
697{
698 acpi_handle phandle;
699 struct acpi_device *device, *pdevice;
700 int ret_val;
701
702 acpi_get_parent(func->handle, &phandle);
703 if (acpi_bus_get_device(phandle, &pdevice)) {
704 dbg("no parent device, assuming NULL\n");
705 pdevice = NULL;
706 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800707 if (!acpi_bus_get_device(func->handle, &device)) {
708 dbg("bus exists... trim\n");
709 /* this shouldn't be in here, so remove
710 * the bus then re-add it...
711 */
712 ret_val = acpi_bus_trim(device, 1);
713 dbg("acpi_bus_trim return %x\n", ret_val);
714 }
715
716 ret_val = acpi_bus_add(&device, pdevice, func->handle,
717 ACPI_BUS_TYPE_DEVICE);
718 if (ret_val) {
719 dbg("error adding bus, %x\n",
720 -ret_val);
721 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800722 }
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800723 ret_val = acpi_bus_start(device);
724
725acpiphp_bus_add_out:
726 return ret_val;
727}
728
729
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900730/**
731 * acpiphp_bus_trim - trim a bus from acpi subsystem
732 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900733 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700734static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900735{
736 struct acpi_device *device;
737 int retval;
738
739 retval = acpi_bus_get_device(handle, &device);
740 if (retval) {
741 dbg("acpi_device not found\n");
742 return retval;
743 }
744
745 retval = acpi_bus_trim(device, 1);
746 if (retval)
747 err("cannot remove from acpi list\n");
748
749 return retval;
750}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/**
753 * enable_device - enable, configure a slot
754 * @slot: slot to be enabled
755 *
756 * This function should be called per *physical slot*,
757 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100759static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700762 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct acpiphp_func *func;
764 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700765 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900766 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (slot->flags & SLOT_ENABLED)
769 goto err_exit;
770
771 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700772 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (dev) {
774 /* This case shouldn't happen */
775 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700776 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 retval = -1;
778 goto err_exit;
779 }
780
Rajesh Shah42f49a62005-04-28 00:25:53 -0700781 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
782 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 err("No new device found\n");
784 retval = -1;
785 goto err_exit;
786 }
787
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800788 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700789 for (pass = 0; pass < 2; pass++) {
790 list_for_each_entry(dev, &bus->devices, bus_list) {
791 if (PCI_SLOT(dev->devfn) != slot->device)
792 continue;
793 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800794 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700795 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900796 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800797 pci_bus_size_bridges(dev->subordinate);
798 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Alex Chiang58c08622009-10-26 21:25:27 -0600802 list_for_each_entry(func, &slot->funcs, sibling)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900803 acpiphp_bus_add(func);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900804
Rajesh Shah42f49a62005-04-28 00:25:53 -0700805 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700806 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600807 acpiphp_set_hpp_values(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700808 pci_enable_bridges(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700809 pci_bus_add_devices(bus);
810
Alex Chiang58c08622009-10-26 21:25:27 -0600811 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600812 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
813 func->function));
814 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900815 continue;
816
Alex Chiang9d911d72009-05-21 16:21:15 -0600817 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
818 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
819 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900820 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600821 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900822
823 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
824 if (ACPI_FAILURE(status))
825 warn("find_p2p_bridge failed (error code = 0x%x)\n",
826 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600827 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 slot->flags |= SLOT_ENABLED;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err_exit:
833 return retval;
834}
835
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700836static void disable_bridges(struct pci_bus *bus)
837{
838 struct pci_dev *dev;
839 list_for_each_entry(dev, &bus->devices, bus_list) {
840 if (dev->subordinate) {
841 disable_bridges(dev->subordinate);
842 pci_disable_device(dev);
843 }
844 }
845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/**
848 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800849 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851static int disable_device(struct acpiphp_slot *slot)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600854 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* is this slot already disabled? */
857 if (!(slot->flags & SLOT_ENABLED))
858 goto err_exit;
859
Alex Chiang9d911d72009-05-21 16:21:15 -0600860 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900861 if (func->bridge) {
862 /* cleanup p2p bridges under this P2P bridge */
863 cleanup_p2p_bridge(func->bridge->handle,
864 (u32)1, NULL, NULL);
865 func->bridge = NULL;
866 }
867
Alex Chiang9d911d72009-05-21 16:21:15 -0600868 pdev = pci_get_slot(slot->bridge->pci_bus,
869 PCI_DEVFN(slot->device, func->function));
870 if (pdev) {
871 pci_stop_bus_device(pdev);
872 if (pdev->subordinate) {
873 disable_bridges(pdev->subordinate);
874 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700875 }
Alex Chiang9d911d72009-05-21 16:21:15 -0600876 pci_remove_bus_device(pdev);
877 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700878 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700879 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700880
Alex Chiang9d911d72009-05-21 16:21:15 -0600881 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900882 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
885 slot->flags &= (~SLOT_ENABLED);
886
Alex Chiang9d911d72009-05-21 16:21:15 -0600887err_exit:
888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891
892/**
893 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800894 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800896 * If a slot has _STA for each function and if any one of them
897 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800899 * If a slot doesn't have _STA and if any one of its functions'
900 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800902 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
904static unsigned int get_slot_status(struct acpiphp_slot *slot)
905{
906 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400907 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct acpiphp_func *func;
910
Alex Chiang58c08622009-10-26 21:25:27 -0600911 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (func->flags & FUNC_HAS_STA) {
913 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
914 if (ACPI_SUCCESS(status) && sta)
915 break;
916 } else {
917 pci_bus_read_config_dword(slot->bridge->pci_bus,
918 PCI_DEVFN(slot->device,
919 func->function),
920 PCI_VENDOR_ID, &dvid);
921 if (dvid != 0xffffffff) {
922 sta = ACPI_STA_ALL;
923 break;
924 }
925 }
926 }
927
928 return (unsigned int)sta;
929}
930
931/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700932 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800933 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700934 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700935int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700936{
937 acpi_status status;
938 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700939 struct acpi_object_list arg_list;
940 union acpi_object arg;
941
Alex Chiang58c08622009-10-26 21:25:27 -0600942 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700943 /* We don't want to call _EJ0 on non-existing functions. */
944 if ((func->flags & FUNC_HAS_EJ0)) {
945 /* _EJ0 method take one argument */
946 arg_list.count = 1;
947 arg_list.pointer = &arg;
948 arg.type = ACPI_TYPE_INTEGER;
949 arg.integer.value = 1;
950
951 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
952 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800953 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700954 return -1;
955 } else
956 break;
957 }
958 }
959 return 0;
960}
961
962/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800964 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
966 * Iterate over all slots under this bridge and make sure that if a
967 * card is present they are enabled, and if not they are disabled.
968 */
969static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
970{
971 struct acpiphp_slot *slot;
972 int retval = 0;
973 int enabled, disabled;
974
975 enabled = disabled = 0;
976
977 for (slot = bridge->slots; slot; slot = slot->next) {
978 unsigned int status = get_slot_status(slot);
979 if (slot->flags & SLOT_ENABLED) {
980 if (status == ACPI_STA_ALL)
981 continue;
982 retval = acpiphp_disable_slot(slot);
983 if (retval) {
984 err("Error occurred in disabling\n");
985 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700986 } else {
987 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989 disabled++;
990 } else {
991 if (status != ACPI_STA_ALL)
992 continue;
993 retval = acpiphp_enable_slot(slot);
994 if (retval) {
995 err("Error occurred in enabling\n");
996 goto err_exit;
997 }
998 enabled++;
999 }
1000 }
1001
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001002 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 err_exit:
1005 return retval;
1006}
1007
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001008static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001009{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001010 struct pci_dev *dev;
1011
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001012 list_for_each_entry(dev, &bus->devices, bus_list)
1013 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001014}
1015
1016/*
1017 * Remove devices for which we could not assign resources, call
1018 * arch specific code to fix-up the bus
1019 */
1020static void acpiphp_sanitize_bus(struct pci_bus *bus)
1021{
1022 struct pci_dev *dev;
1023 int i;
1024 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1025
1026 list_for_each_entry(dev, &bus->devices, bus_list) {
1027 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1028 struct resource *res = &dev->resource[i];
1029 if ((res->flags & type_mask) && !res->start &&
1030 res->end) {
1031 /* Could not assign a required resources
1032 * for this device, remove it */
1033 pci_remove_bus_device(dev);
1034 break;
1035 }
1036 }
1037 }
1038}
1039
1040/* Program resources in newly inserted bridge */
1041static int acpiphp_configure_bridge (acpi_handle handle)
1042{
Alex Chiang7f538662009-09-10 12:34:09 -06001043 struct pci_bus *bus;
1044
1045 if (acpi_is_root_bridge(handle)) {
1046 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1047 bus = root->bus;
1048 } else {
1049 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1050 bus = pdev->subordinate;
1051 pci_dev_put(pdev);
1052 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001053
1054 pci_bus_size_bridges(bus);
1055 pci_bus_assign_resources(bus);
1056 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001057 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001058 pci_enable_bridges(bus);
1059 return 0;
1060}
1061
1062static void handle_bridge_insertion(acpi_handle handle, u32 type)
1063{
1064 struct acpi_device *device, *pdevice;
1065 acpi_handle phandle;
1066
1067 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1068 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1069 err("unexpected notification type %d\n", type);
1070 return;
1071 }
1072
1073 acpi_get_parent(handle, &phandle);
1074 if (acpi_bus_get_device(phandle, &pdevice)) {
1075 dbg("no parent device, assuming NULL\n");
1076 pdevice = NULL;
1077 }
1078 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1079 err("cannot add bridge to acpi list\n");
1080 return;
1081 }
1082 if (!acpiphp_configure_bridge(handle) &&
1083 !acpi_bus_start(device))
1084 add_bridge(handle);
1085 else
1086 err("cannot configure and start bridge\n");
1087
1088}
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090/*
1091 * ACPI event handlers
1092 */
1093
Gary Hade0bbd6422007-07-05 11:10:48 -07001094static acpi_status
1095count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1096{
1097 int *count = (int *)context;
1098 struct acpiphp_bridge *bridge;
1099
1100 bridge = acpiphp_handle_to_bridge(handle);
1101 if (bridge)
1102 (*count)++;
1103 return AE_OK ;
1104}
1105
1106static acpi_status
1107check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1108{
1109 struct acpiphp_bridge *bridge;
1110 char objname[64];
1111 struct acpi_buffer buffer = { .length = sizeof(objname),
1112 .pointer = objname };
1113
1114 bridge = acpiphp_handle_to_bridge(handle);
1115 if (bridge) {
1116 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1117 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001118 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001119 acpiphp_check_bridge(bridge);
1120 }
1121 return AE_OK ;
1122}
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/**
1125 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 * @handle: Notify()'ed acpi_handle
1127 * @type: Notify code
1128 * @context: pointer to acpiphp_bridge structure
1129 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001130 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
1132static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
1133{
1134 struct acpiphp_bridge *bridge;
1135 char objname[64];
1136 struct acpi_buffer buffer = { .length = sizeof(objname),
1137 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001138 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001139 int num_sub_bridges = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001141 if (acpi_bus_get_device(handle, &device)) {
1142 /* This bridge must have just been physically inserted */
1143 handle_bridge_insertion(handle, type);
1144 return;
1145 }
1146
1147 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001148 if (type == ACPI_NOTIFY_BUS_CHECK) {
1149 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001150 count_sub_bridges, NULL, &num_sub_bridges, NULL);
Gary Hade0bbd6422007-07-05 11:10:48 -07001151 }
1152
1153 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001154 err("cannot get bridge info\n");
1155 return;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1159
1160 switch (type) {
1161 case ACPI_NOTIFY_BUS_CHECK:
1162 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001163 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001164 if (bridge) {
1165 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001166 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001167 acpiphp_check_bridge(bridge);
1168 }
1169 if (num_sub_bridges)
1170 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +08001171 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173
1174 case ACPI_NOTIFY_DEVICE_CHECK:
1175 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001176 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 acpiphp_check_bridge(bridge);
1178 break;
1179
1180 case ACPI_NOTIFY_DEVICE_WAKE:
1181 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001182 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184
1185 case ACPI_NOTIFY_EJECT_REQUEST:
1186 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001187 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001188 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1189 (bridge->flags & BRIDGE_HAS_EJ0)) {
1190 struct acpiphp_slot *slot;
1191 slot = bridge->func->slot;
1192 if (!acpiphp_disable_slot(slot))
1193 acpiphp_eject_slot(slot);
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196
1197 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1198 printk(KERN_ERR "Device %s cannot be configured due"
1199 " to a frequency mismatch\n", objname);
1200 break;
1201
1202 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1203 printk(KERN_ERR "Device %s cannot be configured due"
1204 " to a bus mode mismatch\n", objname);
1205 break;
1206
1207 case ACPI_NOTIFY_POWER_FAULT:
1208 printk(KERN_ERR "Device %s has suffered a power fault\n",
1209 objname);
1210 break;
1211
1212 default:
1213 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1214 break;
1215 }
1216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/**
1219 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * @handle: Notify()'ed acpi_handle
1221 * @type: Notify code
1222 * @context: pointer to acpiphp_func structure
1223 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001224 * Handles ACPI event notification on slots.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Len Brown2b85e132006-06-27 01:50:14 -04001226static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
1228 struct acpiphp_func *func;
1229 char objname[64];
1230 struct acpi_buffer buffer = { .length = sizeof(objname),
1231 .pointer = objname };
1232
1233 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1234
1235 func = (struct acpiphp_func *)context;
1236
1237 switch (type) {
1238 case ACPI_NOTIFY_BUS_CHECK:
1239 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001240 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 acpiphp_enable_slot(func->slot);
1242 break;
1243
1244 case ACPI_NOTIFY_DEVICE_CHECK:
1245 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001246 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 acpiphp_check_bridge(func->slot->bridge);
1248 break;
1249
1250 case ACPI_NOTIFY_DEVICE_WAKE:
1251 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001252 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254
1255 case ACPI_NOTIFY_EJECT_REQUEST:
1256 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001257 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001258 if (!(acpiphp_disable_slot(func->slot)))
1259 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261
1262 default:
1263 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1264 break;
1265 }
1266}
1267
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001268
1269static acpi_status
1270find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1271{
1272 int *count = (int *)context;
1273
Alexander Chiang27558202009-06-10 19:55:14 +00001274 if (acpi_is_root_bridge(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001275 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1276 handle_hotplug_event_bridge, NULL);
1277 (*count)++;
1278 }
1279 return AE_OK ;
1280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282static struct acpi_pci_driver acpi_pci_hp_driver = {
1283 .add = add_bridge,
1284 .remove = remove_bridge,
1285};
1286
1287/**
1288 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 */
1290int __init acpiphp_glue_init(void)
1291{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001292 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001294 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001295 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (num <= 0)
1298 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001299 else
1300 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 return 0;
1303}
1304
1305
1306/**
1307 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1308 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001309 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001311void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1314}
1315
1316
1317/**
1318 * acpiphp_get_num_slots - count number of slots in a system
1319 */
1320int __init acpiphp_get_num_slots(void)
1321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001323 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Alex Chiang58c08622009-10-26 21:25:27 -06001325 list_for_each_entry(bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001326 dbg("Bus %04x:%02x has %d slot%s\n",
1327 pci_domain_nr(bridge->pci_bus),
1328 bridge->pci_bus->number, bridge->nr_slots,
1329 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 num_slots += bridge->nr_slots;
1331 }
1332
Rajesh Shah42f49a62005-04-28 00:25:53 -07001333 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return num_slots;
1335}
1336
1337
1338#if 0
1339/**
1340 * acpiphp_for_each_slot - call function for each slot
1341 * @fn: callback function
1342 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1345{
1346 struct list_head *node;
1347 struct acpiphp_bridge *bridge;
1348 struct acpiphp_slot *slot;
1349 int retval = 0;
1350
1351 list_for_each (node, &bridge_list) {
1352 bridge = (struct acpiphp_bridge *)node;
1353 for (slot = bridge->slots; slot; slot = slot->next) {
1354 retval = fn(slot, data);
1355 if (!retval)
1356 goto err_exit;
1357 }
1358 }
1359
1360 err_exit:
1361 return retval;
1362}
1363#endif
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366/**
1367 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001368 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
1370int acpiphp_enable_slot(struct acpiphp_slot *slot)
1371{
1372 int retval;
1373
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001374 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* wake up all functions */
1377 retval = power_on_slot(slot);
1378 if (retval)
1379 goto err_exit;
1380
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001381 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* configure all functions */
1383 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001384 if (retval)
1385 power_off_slot(slot);
1386 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001387 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001388 power_off_slot(slot);
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001392 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return retval;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/**
1397 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001398 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400int acpiphp_disable_slot(struct acpiphp_slot *slot)
1401{
1402 int retval = 0;
1403
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001404 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* unconfigure all functions */
1407 retval = disable_device(slot);
1408 if (retval)
1409 goto err_exit;
1410
1411 /* power off all functions */
1412 retval = power_off_slot(slot);
1413 if (retval)
1414 goto err_exit;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001417 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return retval;
1419}
1420
1421
1422/*
1423 * slot enabled: 1
1424 * slot disabled: 0
1425 */
1426u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1427{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001428 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431
1432/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001433 * latch open: 1
1434 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
1436u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1437{
1438 unsigned int sta;
1439
1440 sta = get_slot_status(slot);
1441
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001442 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
1445
1446/*
1447 * adapter presence : 1
1448 * absence : 0
1449 */
1450u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1451{
1452 unsigned int sta;
1453
1454 sta = get_slot_status(slot);
1455
1456 return (sta == 0) ? 0 : 1;
1457}