blob: 09bf37721842863a28f370733d49bb12f0db1067 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Prarit Bhargava6af8bef2011-09-28 19:40:53 -040051#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "acpiphp.h"
55
56static LIST_HEAD(bridge_list);
57
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070061static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060062static void acpiphp_set_hpp_values(struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040063static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070064
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090065/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040066static acpi_status
67is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68{
69 int *count = (int *)context;
70
71 if (is_dock_device(handle)) {
72 (*count)++;
73 return AE_CTRL_TERMINATE;
74 } else {
75 return AE_OK;
76 }
77}
78
Kristen Accardi4e8662b2006-06-28 03:08:06 -040079/*
80 * the _DCK method can do funny things... and sometimes not
81 * hah-hah funny.
82 *
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
85 */
86static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 void *v)
88{
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
91 u32 buses;
92
93 if (!bus->self)
94 return NOTIFY_OK;
95
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
98 */
99 pci_read_config_dword(bus->self,
100 PCI_PRIMARY_BUS,
101 &buses);
102
103 if (((buses >> 8) & 0xff) != bus->secondary) {
104 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700105 | ((unsigned int)(bus->primary) << 0)
106 | ((unsigned int)(bus->secondary) << 8)
107 | ((unsigned int)(bus->subordinate) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 }
110 return NOTIFY_OK;
111}
112
113
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400114static const struct acpi_dock_ops acpiphp_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800115 .handler = handle_hotplug_event_func,
116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* callback routine to register each ACPI PCI slot object */
119static acpi_status
120register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
121{
122 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
123 struct acpiphp_slot *slot;
124 struct acpiphp_func *newfunc;
125 acpi_handle tmp;
126 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400127 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800128 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900129 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600130 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900132 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return AE_OK;
134
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600135 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
136 if (ACPI_FAILURE(status)) {
137 warn("can't evaluate _ADR (%#x)\n", status);
138 return AE_OK;
139 }
140
141 device = (adr >> 16) & 0xffff;
142 function = adr & 0xffff;
143
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100144 pdev = pbus->self;
145 if (pdev && pci_is_pcie(pdev)) {
146 tmp = acpi_find_root_bridge_handle(pdev);
147 if (tmp) {
148 struct acpi_pci_root *root = acpi_pci_find_root(tmp);
149
150 if (root && (root->osc_control_set &
151 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
152 return AE_OK;
153 }
154 }
155
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100156 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!newfunc)
158 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 INIT_LIST_HEAD(&newfunc->sibling);
161 newfunc->handle = handle;
162 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000163
164 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800165 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
168 newfunc->flags |= FUNC_HAS_STA;
169
170 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
171 newfunc->flags |= FUNC_HAS_PS0;
172
173 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
174 newfunc->flags |= FUNC_HAS_PS3;
175
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400176 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800177 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400180 if (ACPI_FAILURE(status)) {
181 /*
182 * use the count of the number of slots we've found
183 * for the number of the slot
184 */
185 sun = bridge->nr_slots+1;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* search for objects that share the same slot */
189 for (slot = bridge->slots; slot; slot = slot->next)
190 if (slot->device == device) {
191 if (slot->sun != sun)
192 warn("sibling found, but _SUN doesn't match!\n");
193 break;
194 }
195
196 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100197 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!slot) {
199 kfree(newfunc);
200 return AE_NO_MEMORY;
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 slot->device = device;
205 slot->sun = sun;
206 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100207 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 slot->next = bridge->slots;
210 bridge->slots = slot;
211
212 bridge->nr_slots++;
213
Justin Chenb6adc192008-12-11 11:16:44 -0700214 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900215 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800216 retval = acpiphp_register_hotplug_slot(slot);
217 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600218 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700219 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600220 "hotplug driver\n", slot->sun);
221 else
222 warn("acpiphp_register_hotplug_slot failed "
223 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800224 goto err_exit;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
228 newfunc->slot = slot;
229 list_add_tail(&newfunc->sibling, &slot->funcs);
230
Alex Chiang9d911d72009-05-21 16:21:15 -0600231 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
232 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600234 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400237 if (is_dock_device(handle)) {
238 /* we don't want to call this device's _EJ0
239 * because we want the dock notify handler
240 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800241 */
242 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400243 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800244 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400245 dbg("failed to register dock device\n");
246
247 /* we need to be notified when dock events happen
248 * outside of the hotplug operation, since we may
249 * need to do fixups before we can hotplug.
250 */
251 newfunc->nb.notifier_call = post_dock_fixups;
252 if (register_dock_notifier(&newfunc->nb))
253 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800254 }
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800257 if (!(newfunc->flags & FUNC_HAS_DCK)) {
258 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ACPI_SYSTEM_NOTIFY,
260 handle_hotplug_event_func,
261 newfunc);
262
Kristen Accardi20416ea2006-02-23 17:56:03 -0800263 if (ACPI_FAILURE(status))
264 err("failed to register interrupt notify handler\n");
265 } else
266 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Kristen Accardi20416ea2006-02-23 17:56:03 -0800268 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800269
270 err_exit:
271 bridge->nr_slots--;
272 bridge->slots = slot->next;
273 kfree(slot);
274 kfree(newfunc);
275
276 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
280/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600281static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Alex Chiang7f538662009-09-10 12:34:09 -0600283 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900284 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600285 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800286 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900287 }
288 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
292static void init_bridge_misc(struct acpiphp_bridge *bridge)
293{
294 acpi_status status;
295
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800296 /* must be added to the list prior to calling register_slot */
297 list_add(&bridge->list, &bridge_list);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* register all slot objects under this bridge */
300 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800301 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800302 if (ACPI_FAILURE(status)) {
303 list_del(&bridge->list);
304 return;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700308 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900309 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
310 status = acpi_remove_notify_handler(bridge->func->handle,
311 ACPI_SYSTEM_NOTIFY,
312 handle_hotplug_event_func);
313 if (ACPI_FAILURE(status))
314 err("failed to remove notify handler\n");
315 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700316 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ACPI_SYSTEM_NOTIFY,
318 handle_hotplug_event_bridge,
319 bridge);
320
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700321 if (ACPI_FAILURE(status)) {
322 err("failed to register interrupt notify handler\n");
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900328/* find acpiphp_func from acpiphp_bridge */
329static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
330{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900331 struct acpiphp_bridge *bridge;
332 struct acpiphp_slot *slot;
333 struct acpiphp_func *func;
334
Alex Chiang58c08622009-10-26 21:25:27 -0600335 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900336 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600337 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900338 if (func->handle == handle)
339 return func;
340 }
341 }
342 }
343
344 return NULL;
345}
346
347
348static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
349{
350 acpi_handle dummy_handle;
351
352 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
353 "_STA", &dummy_handle)))
354 bridge->flags |= BRIDGE_HAS_STA;
355
356 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
357 "_EJ0", &dummy_handle)))
358 bridge->flags |= BRIDGE_HAS_EJ0;
359
360 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
361 "_PS0", &dummy_handle)))
362 bridge->flags |= BRIDGE_HAS_PS0;
363
364 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
365 "_PS3", &dummy_handle)))
366 bridge->flags |= BRIDGE_HAS_PS3;
367
368 /* is this ejectable p2p bridge? */
369 if (bridge->flags & BRIDGE_HAS_EJ0) {
370 struct acpiphp_func *func;
371
372 dbg("found ejectable p2p bridge\n");
373
374 /* make link between PCI bridge and PCI function */
375 func = acpiphp_bridge_handle_to_function(bridge->handle);
376 if (!func)
377 return;
378 bridge->func = func;
379 func->bridge = bridge;
380 }
381}
382
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* allocate and initialize host bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600385static void add_host_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct acpiphp_bridge *bridge;
Alex Chiang6edd7672009-09-10 12:34:04 -0600388 struct acpi_pci_root *root = acpi_pci_find_root(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100390 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (bridge == NULL)
392 return;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 bridge->type = BRIDGE_TYPE_HOST;
395 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Alex Chiang6edd7672009-09-10 12:34:04 -0600397 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 spin_lock_init(&bridge->res_lock);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 init_bridge_misc(bridge);
402}
403
404
405/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600406static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100410 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (bridge == NULL) {
412 err("out of memory\n");
413 return;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 bridge->type = BRIDGE_TYPE_P2P;
417 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900418 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Alex Chiang6edd7672009-09-10 12:34:04 -0600420 bridge->pci_dev = acpi_get_pci_dev(handle);
421 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!bridge->pci_bus) {
423 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700424 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600427 /*
428 * Grab a ref to the subordinate PCI bus in case the bus is
429 * removed via PCI core logical hotplug. The ref pins the bus
430 * (which we access during module unload).
431 */
432 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_lock_init(&bridge->res_lock);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 return;
437 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600438 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700439 kfree(bridge);
440 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443
444/* callback routine to find P2P bridges */
445static acpi_status
446find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
447{
448 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Alex Chiang6edd7672009-09-10 12:34:04 -0600451 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700452 if (!dev || !dev->subordinate)
453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600456 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600458 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900461 /* search P2P bridges under this p2p bridge */
462 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800463 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900464 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900465 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900466
Rajesh Shah42f49a62005-04-28 00:25:53 -0700467 out:
468 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return AE_OK;
470}
471
472
473/* find hot-pluggable slots, and then find P2P bridge */
474static int add_bridge(acpi_handle handle)
475{
476 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400477 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 acpi_handle dummy_handle;
479
480 /* if the bridge doesn't have _STA, we assume it is always there */
481 status = acpi_get_handle(handle, "_STA", &dummy_handle);
482 if (ACPI_SUCCESS(status)) {
483 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
484 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800485 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487 }
488 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
489 /* don't register this object */
490 return 0;
491 }
492
Rajesh Shah42f49a62005-04-28 00:25:53 -0700493 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600494 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700495 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Alex Chiang6edd7672009-09-10 12:34:04 -0600496 add_host_bridge(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* search P2P bridges under this host bridge */
500 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800501 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900504 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 return 0;
507}
508
Rajesh Shah42f49a62005-04-28 00:25:53 -0700509static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
510{
Alex Chiang58c08622009-10-26 21:25:27 -0600511 struct acpiphp_bridge *bridge;
512
513 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700514 if (bridge->handle == handle)
515 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700516
517 return NULL;
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Rajesh Shah364d5092005-04-28 00:25:54 -0700520static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Alex Chiang58c08622009-10-26 21:25:27 -0600522 struct acpiphp_slot *slot, *next;
523 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700524 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700525 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700526
527 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
528 handle_hotplug_event_bridge);
529 if (ACPI_FAILURE(status))
530 err("failed to remove notify handler\n");
531
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900532 if ((bridge->type != BRIDGE_TYPE_HOST) &&
533 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
534 status = acpi_install_notify_handler(bridge->func->handle,
535 ACPI_SYSTEM_NOTIFY,
536 handle_hotplug_event_func,
537 bridge->func);
538 if (ACPI_FAILURE(status))
539 err("failed to install interrupt notify handler\n");
540 }
541
Rajesh Shah42f49a62005-04-28 00:25:53 -0700542 slot = bridge->slots;
543 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600544 next = slot->next;
545 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400546 if (is_dock_device(func->handle)) {
547 unregister_hotplug_dock_device(func->handle);
548 unregister_dock_notifier(&func->nb);
549 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800550 if (!(func->flags & FUNC_HAS_DCK)) {
551 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700552 ACPI_SYSTEM_NOTIFY,
553 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800554 if (ACPI_FAILURE(status))
555 err("failed to remove notify handler\n");
556 }
Alex Chiang58c08622009-10-26 21:25:27 -0600557 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700558 kfree(func);
559 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800560 acpiphp_unregister_hotplug_slot(slot);
561 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700562 kfree(slot);
563 slot = next;
564 }
565
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600566 /*
567 * Only P2P bridges have a pci_dev
568 */
569 if (bridge->pci_dev)
570 put_device(&bridge->pci_bus->dev);
571
Rajesh Shah42f49a62005-04-28 00:25:53 -0700572 pci_dev_put(bridge->pci_dev);
573 list_del(&bridge->list);
574 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Rajesh Shah364d5092005-04-28 00:25:54 -0700577static acpi_status
578cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
579{
580 struct acpiphp_bridge *bridge;
581
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900582 /* cleanup p2p bridges under this P2P bridge
583 in a depth-first manner */
584 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800585 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900586
Alex Chianga13307c2008-07-01 20:02:23 -0600587 bridge = acpiphp_handle_to_bridge(handle);
588 if (bridge)
589 cleanup_bridge(bridge);
590
Rajesh Shah364d5092005-04-28 00:25:54 -0700591 return AE_OK;
592}
593
594static void remove_bridge(acpi_handle handle)
595{
596 struct acpiphp_bridge *bridge;
597
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900598 /* cleanup p2p bridges under this host bridge
599 in a depth-first manner */
600 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800601 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900602
Alex Chianga13307c2008-07-01 20:02:23 -0600603 /*
604 * On root bridges with hotplug slots directly underneath (ie,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300605 * no p2p bridge between), we call cleanup_bridge().
Alex Chianga13307c2008-07-01 20:02:23 -0600606 *
607 * The else clause cleans up root bridges that either had no
608 * hotplug slots at all, or had a p2p bridge underneath.
609 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700610 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900611 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700612 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600613 else
614 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
615 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618static int power_on_slot(struct acpiphp_slot *slot)
619{
620 acpi_status status;
621 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int retval = 0;
623
624 /* if already enabled, just skip */
625 if (slot->flags & SLOT_POWEREDON)
626 goto err_exit;
627
Alex Chiang58c08622009-10-26 21:25:27 -0600628 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800630 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
632 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800633 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 retval = -1;
635 goto err_exit;
636 } else
637 break;
638 }
639 }
640
641 /* TBD: evaluate _STA to check if the slot is enabled */
642
643 slot->flags |= SLOT_POWEREDON;
644
645 err_exit:
646 return retval;
647}
648
649
650static int power_off_slot(struct acpiphp_slot *slot)
651{
652 acpi_status status;
653 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 int retval = 0;
656
657 /* if already disabled, just skip */
658 if ((slot->flags & SLOT_POWEREDON) == 0)
659 goto err_exit;
660
Alex Chiang58c08622009-10-26 21:25:27 -0600661 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700662 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
664 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800665 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 retval = -1;
667 goto err_exit;
668 } else
669 break;
670 }
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* TBD: evaluate _STA to check if the slot is disabled */
674
675 slot->flags &= (~SLOT_POWEREDON);
676
677 err_exit:
678 return retval;
679}
680
681
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800682
683/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800684 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800685 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800686 */
687static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
688{
689 struct list_head *tmp;
690 unsigned char max, n;
691
692 /*
693 * pci_bus_max_busnr will return the highest
694 * reserved busnr for all these children.
695 * that is equivalent to the bus->subordinate
696 * value. We don't want to use the parent's
697 * bus->subordinate value because it could have
698 * padding in it.
699 */
700 max = bus->secondary;
701
702 list_for_each(tmp, &bus->children) {
703 n = pci_bus_max_busnr(pci_bus_b(tmp));
704 if (n > max)
705 max = n;
706 }
707 return max;
708}
709
710
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800711/**
712 * acpiphp_bus_add - add a new bus to acpi subsystem
713 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800714 */
715static int acpiphp_bus_add(struct acpiphp_func *func)
716{
717 acpi_handle phandle;
718 struct acpi_device *device, *pdevice;
719 int ret_val;
720
721 acpi_get_parent(func->handle, &phandle);
722 if (acpi_bus_get_device(phandle, &pdevice)) {
723 dbg("no parent device, assuming NULL\n");
724 pdevice = NULL;
725 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800726 if (!acpi_bus_get_device(func->handle, &device)) {
727 dbg("bus exists... trim\n");
728 /* this shouldn't be in here, so remove
729 * the bus then re-add it...
730 */
731 ret_val = acpi_bus_trim(device, 1);
732 dbg("acpi_bus_trim return %x\n", ret_val);
733 }
734
735 ret_val = acpi_bus_add(&device, pdevice, func->handle,
736 ACPI_BUS_TYPE_DEVICE);
737 if (ret_val) {
738 dbg("error adding bus, %x\n",
739 -ret_val);
740 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800741 }
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800742 ret_val = acpi_bus_start(device);
743
744acpiphp_bus_add_out:
745 return ret_val;
746}
747
748
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900749/**
750 * acpiphp_bus_trim - trim a bus from acpi subsystem
751 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900752 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700753static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900754{
755 struct acpi_device *device;
756 int retval;
757
758 retval = acpi_bus_get_device(handle, &device);
759 if (retval) {
760 dbg("acpi_device not found\n");
761 return retval;
762 }
763
764 retval = acpi_bus_trim(device, 1);
765 if (retval)
766 err("cannot remove from acpi list\n");
767
768 return retval;
769}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800770
Shaohua Lid0607052010-02-25 10:59:34 +0800771static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
772{
773 struct acpiphp_func *func;
774 union acpi_object params[2];
775 struct acpi_object_list arg_list;
776
777 list_for_each_entry(func, &slot->funcs, sibling) {
778 arg_list.count = 2;
779 arg_list.pointer = params;
780 params[0].type = ACPI_TYPE_INTEGER;
781 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
782 params[1].type = ACPI_TYPE_INTEGER;
783 params[1].integer.value = 1;
784 /* _REG is optional, we don't care about if there is failure */
785 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
786 }
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/**
790 * enable_device - enable, configure a slot
791 * @slot: slot to be enabled
792 *
793 * This function should be called per *physical slot*,
794 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100796static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700799 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct acpiphp_func *func;
801 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700802 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900803 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (slot->flags & SLOT_ENABLED)
806 goto err_exit;
807
Rajesh Shah42f49a62005-04-28 00:25:53 -0700808 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
809 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800810 /* Maybe only part of funcs are added. */
811 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto err_exit;
813 }
814
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800815 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700816 for (pass = 0; pass < 2; pass++) {
817 list_for_each_entry(dev, &bus->devices, bus_list) {
818 if (PCI_SLOT(dev->devfn) != slot->device)
819 continue;
820 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800821 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700822 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900823 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800824 pci_bus_size_bridges(dev->subordinate);
825 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Alex Chiang58c08622009-10-26 21:25:27 -0600829 list_for_each_entry(func, &slot->funcs, sibling)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900830 acpiphp_bus_add(func);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900831
Rajesh Shah42f49a62005-04-28 00:25:53 -0700832 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700833 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600834 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800835 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700836 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100837
838 list_for_each_entry(dev, &bus->devices, bus_list) {
839 /* Assume that newly added devices are powered on already. */
840 if (!dev->is_added)
841 dev->current_state = PCI_D0;
842 }
843
Rajesh Shah42f49a62005-04-28 00:25:53 -0700844 pci_bus_add_devices(bus);
845
Amos Kongf382a082011-11-25 15:03:07 +0800846 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600847 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600848 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
849 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800850 if (!dev) {
851 /* Do not set SLOT_ENABLED flag if some funcs
852 are not added. */
853 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900854 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800855 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900856
Alex Chiang9d911d72009-05-21 16:21:15 -0600857 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
858 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
859 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900860 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600861 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900862
863 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
864 if (ACPI_FAILURE(status))
865 warn("find_p2p_bridge failed (error code = 0x%x)\n",
866 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600867 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 err_exit:
872 return retval;
873}
874
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700875static void disable_bridges(struct pci_bus *bus)
876{
877 struct pci_dev *dev;
878 list_for_each_entry(dev, &bus->devices, bus_list) {
879 if (dev->subordinate) {
880 disable_bridges(dev->subordinate);
881 pci_disable_device(dev);
882 }
883 }
884}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886/**
887 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800888 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 */
890static int disable_device(struct acpiphp_slot *slot)
891{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600893 struct pci_dev *pdev;
Amos Kongf382a082011-11-25 15:03:07 +0800894 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Amos Kongf382a082011-11-25 15:03:07 +0800896 /* The slot will be enabled when func 0 is added, so check
897 func 0 before disable the slot. */
898 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
899 if (!pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto err_exit;
901
Alex Chiang9d911d72009-05-21 16:21:15 -0600902 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900903 if (func->bridge) {
904 /* cleanup p2p bridges under this P2P bridge */
905 cleanup_p2p_bridge(func->bridge->handle,
906 (u32)1, NULL, NULL);
907 func->bridge = NULL;
908 }
909
Alex Chiang9d911d72009-05-21 16:21:15 -0600910 pdev = pci_get_slot(slot->bridge->pci_bus,
911 PCI_DEVFN(slot->device, func->function));
912 if (pdev) {
913 pci_stop_bus_device(pdev);
914 if (pdev->subordinate) {
915 disable_bridges(pdev->subordinate);
916 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700917 }
Yinghai Luf6330c32012-02-25 13:54:23 -0800918 __pci_remove_bus_device(pdev);
Alex Chiang9d911d72009-05-21 16:21:15 -0600919 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700920 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700921 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700922
Alex Chiang9d911d72009-05-21 16:21:15 -0600923 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900924 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 slot->flags &= (~SLOT_ENABLED);
928
Alex Chiang9d911d72009-05-21 16:21:15 -0600929err_exit:
930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933
934/**
935 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800936 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800938 * If a slot has _STA for each function and if any one of them
939 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800941 * If a slot doesn't have _STA and if any one of its functions'
942 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800944 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946static unsigned int get_slot_status(struct acpiphp_slot *slot)
947{
948 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400949 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct acpiphp_func *func;
952
Alex Chiang58c08622009-10-26 21:25:27 -0600953 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (func->flags & FUNC_HAS_STA) {
955 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
956 if (ACPI_SUCCESS(status) && sta)
957 break;
958 } else {
959 pci_bus_read_config_dword(slot->bridge->pci_bus,
960 PCI_DEVFN(slot->device,
961 func->function),
962 PCI_VENDOR_ID, &dvid);
963 if (dvid != 0xffffffff) {
964 sta = ACPI_STA_ALL;
965 break;
966 }
967 }
968 }
969
970 return (unsigned int)sta;
971}
972
973/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700974 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800975 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700976 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700977int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700978{
979 acpi_status status;
980 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700981 struct acpi_object_list arg_list;
982 union acpi_object arg;
983
Alex Chiang58c08622009-10-26 21:25:27 -0600984 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700985 /* We don't want to call _EJ0 on non-existing functions. */
986 if ((func->flags & FUNC_HAS_EJ0)) {
987 /* _EJ0 method take one argument */
988 arg_list.count = 1;
989 arg_list.pointer = &arg;
990 arg.type = ACPI_TYPE_INTEGER;
991 arg.integer.value = 1;
992
993 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
994 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800995 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -0700996 return -1;
997 } else
998 break;
999 }
1000 }
1001 return 0;
1002}
1003
1004/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001006 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
1008 * Iterate over all slots under this bridge and make sure that if a
1009 * card is present they are enabled, and if not they are disabled.
1010 */
1011static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1012{
1013 struct acpiphp_slot *slot;
1014 int retval = 0;
1015 int enabled, disabled;
1016
1017 enabled = disabled = 0;
1018
1019 for (slot = bridge->slots; slot; slot = slot->next) {
1020 unsigned int status = get_slot_status(slot);
1021 if (slot->flags & SLOT_ENABLED) {
1022 if (status == ACPI_STA_ALL)
1023 continue;
1024 retval = acpiphp_disable_slot(slot);
1025 if (retval) {
1026 err("Error occurred in disabling\n");
1027 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001028 } else {
1029 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 disabled++;
1032 } else {
1033 if (status != ACPI_STA_ALL)
1034 continue;
1035 retval = acpiphp_enable_slot(slot);
1036 if (retval) {
1037 err("Error occurred in enabling\n");
1038 goto err_exit;
1039 }
1040 enabled++;
1041 }
1042 }
1043
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001044 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 err_exit:
1047 return retval;
1048}
1049
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001050static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001051{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001052 struct pci_dev *dev;
1053
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001054 list_for_each_entry(dev, &bus->devices, bus_list)
1055 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001056}
1057
1058/*
1059 * Remove devices for which we could not assign resources, call
1060 * arch specific code to fix-up the bus
1061 */
1062static void acpiphp_sanitize_bus(struct pci_bus *bus)
1063{
1064 struct pci_dev *dev;
1065 int i;
1066 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1067
1068 list_for_each_entry(dev, &bus->devices, bus_list) {
1069 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1070 struct resource *res = &dev->resource[i];
1071 if ((res->flags & type_mask) && !res->start &&
1072 res->end) {
1073 /* Could not assign a required resources
1074 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -08001075 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001076 break;
1077 }
1078 }
1079 }
1080}
1081
1082/* Program resources in newly inserted bridge */
1083static int acpiphp_configure_bridge (acpi_handle handle)
1084{
Alex Chiang7f538662009-09-10 12:34:09 -06001085 struct pci_bus *bus;
1086
1087 if (acpi_is_root_bridge(handle)) {
1088 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1089 bus = root->bus;
1090 } else {
1091 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1092 bus = pdev->subordinate;
1093 pci_dev_put(pdev);
1094 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001095
1096 pci_bus_size_bridges(bus);
1097 pci_bus_assign_resources(bus);
1098 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001099 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001100 pci_enable_bridges(bus);
1101 return 0;
1102}
1103
1104static void handle_bridge_insertion(acpi_handle handle, u32 type)
1105{
1106 struct acpi_device *device, *pdevice;
1107 acpi_handle phandle;
1108
1109 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1110 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1111 err("unexpected notification type %d\n", type);
1112 return;
1113 }
1114
1115 acpi_get_parent(handle, &phandle);
1116 if (acpi_bus_get_device(phandle, &pdevice)) {
1117 dbg("no parent device, assuming NULL\n");
1118 pdevice = NULL;
1119 }
1120 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1121 err("cannot add bridge to acpi list\n");
1122 return;
1123 }
1124 if (!acpiphp_configure_bridge(handle) &&
1125 !acpi_bus_start(device))
1126 add_bridge(handle);
1127 else
1128 err("cannot configure and start bridge\n");
1129
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
1133 * ACPI event handlers
1134 */
1135
Gary Hade0bbd6422007-07-05 11:10:48 -07001136static acpi_status
1137count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1138{
1139 int *count = (int *)context;
1140 struct acpiphp_bridge *bridge;
1141
1142 bridge = acpiphp_handle_to_bridge(handle);
1143 if (bridge)
1144 (*count)++;
1145 return AE_OK ;
1146}
1147
1148static acpi_status
1149check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1150{
1151 struct acpiphp_bridge *bridge;
1152 char objname[64];
1153 struct acpi_buffer buffer = { .length = sizeof(objname),
1154 .pointer = objname };
1155
1156 bridge = acpiphp_handle_to_bridge(handle);
1157 if (bridge) {
1158 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1159 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001160 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001161 acpiphp_check_bridge(bridge);
1162 }
1163 return AE_OK ;
1164}
1165
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001166struct acpiphp_hp_work {
1167 struct work_struct work;
1168 acpi_handle handle;
1169 u32 type;
1170 void *context;
1171};
1172
1173static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1174 void *context,
1175 void (*func)(struct work_struct *work))
1176{
1177 struct acpiphp_hp_work *hp_work;
1178 int ret;
1179
1180 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1181 if (!hp_work)
1182 return;
1183
1184 hp_work->handle = handle;
1185 hp_work->type = type;
1186 hp_work->context = context;
1187
1188 INIT_WORK(&hp_work->work, func);
1189 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1190 if (!ret)
1191 kfree(hp_work);
1192}
1193
1194static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct acpiphp_bridge *bridge;
1197 char objname[64];
1198 struct acpi_buffer buffer = { .length = sizeof(objname),
1199 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001200 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001201 int num_sub_bridges = 0;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001202 struct acpiphp_hp_work *hp_work;
1203 acpi_handle handle;
1204 u32 type;
1205
1206 hp_work = container_of(work, struct acpiphp_hp_work, work);
1207 handle = hp_work->handle;
1208 type = hp_work->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001210 if (acpi_bus_get_device(handle, &device)) {
1211 /* This bridge must have just been physically inserted */
1212 handle_bridge_insertion(handle, type);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001213 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001214 }
1215
1216 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001217 if (type == ACPI_NOTIFY_BUS_CHECK) {
1218 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001219 count_sub_bridges, NULL, &num_sub_bridges, NULL);
Gary Hade0bbd6422007-07-05 11:10:48 -07001220 }
1221
1222 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001223 err("cannot get bridge info\n");
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001224 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1228
1229 switch (type) {
1230 case ACPI_NOTIFY_BUS_CHECK:
1231 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001232 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001233 if (bridge) {
1234 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001235 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001236 acpiphp_check_bridge(bridge);
1237 }
1238 if (num_sub_bridges)
1239 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +08001240 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242
1243 case ACPI_NOTIFY_DEVICE_CHECK:
1244 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001245 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 acpiphp_check_bridge(bridge);
1247 break;
1248
1249 case ACPI_NOTIFY_DEVICE_WAKE:
1250 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001251 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 break;
1253
1254 case ACPI_NOTIFY_EJECT_REQUEST:
1255 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001256 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001257 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1258 (bridge->flags & BRIDGE_HAS_EJ0)) {
1259 struct acpiphp_slot *slot;
1260 slot = bridge->func->slot;
1261 if (!acpiphp_disable_slot(slot))
1262 acpiphp_eject_slot(slot);
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
1265
1266 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1267 printk(KERN_ERR "Device %s cannot be configured due"
1268 " to a frequency mismatch\n", objname);
1269 break;
1270
1271 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1272 printk(KERN_ERR "Device %s cannot be configured due"
1273 " to a bus mode mismatch\n", objname);
1274 break;
1275
1276 case ACPI_NOTIFY_POWER_FAULT:
1277 printk(KERN_ERR "Device %s has suffered a power fault\n",
1278 objname);
1279 break;
1280
1281 default:
1282 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1283 break;
1284 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001285
1286out:
1287 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001291 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 * @handle: Notify()'ed acpi_handle
1293 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001294 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001296 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001298static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1299 void *context)
1300{
1301 /*
1302 * Currently the code adds all hotplug events to the kacpid_wq
1303 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1304 * The proper way to fix this is to reorganize the code so that
1305 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1306 * For now just re-add this work to the kacpi_hotplug_wq so we
1307 * don't deadlock on hotplug actions.
1308 */
1309 alloc_acpiphp_hp_work(handle, type, context,
1310 _handle_hotplug_event_bridge);
1311}
1312
1313static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 struct acpiphp_func *func;
1316 char objname[64];
1317 struct acpi_buffer buffer = { .length = sizeof(objname),
1318 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001319 struct acpiphp_hp_work *hp_work;
1320 acpi_handle handle;
1321 u32 type;
1322 void *context;
1323
1324 hp_work = container_of(work, struct acpiphp_hp_work, work);
1325 handle = hp_work->handle;
1326 type = hp_work->type;
1327 context = hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1330
1331 func = (struct acpiphp_func *)context;
1332
1333 switch (type) {
1334 case ACPI_NOTIFY_BUS_CHECK:
1335 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001336 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 acpiphp_enable_slot(func->slot);
1338 break;
1339
1340 case ACPI_NOTIFY_DEVICE_CHECK:
1341 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001342 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 acpiphp_check_bridge(func->slot->bridge);
1344 break;
1345
1346 case ACPI_NOTIFY_DEVICE_WAKE:
1347 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001348 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 break;
1350
1351 case ACPI_NOTIFY_EJECT_REQUEST:
1352 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001353 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001354 if (!(acpiphp_disable_slot(func->slot)))
1355 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 break;
1357
1358 default:
1359 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1360 break;
1361 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001362
1363 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001366/**
1367 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1368 * @handle: Notify()'ed acpi_handle
1369 * @type: Notify code
1370 * @context: pointer to acpiphp_func structure
1371 *
1372 * Handles ACPI event notification on slots.
1373 */
1374static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1375 void *context)
1376{
1377 /*
1378 * Currently the code adds all hotplug events to the kacpid_wq
1379 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1380 * The proper way to fix this is to reorganize the code so that
1381 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1382 * For now just re-add this work to the kacpi_hotplug_wq so we
1383 * don't deadlock on hotplug actions.
1384 */
1385 alloc_acpiphp_hp_work(handle, type, context,
1386 _handle_hotplug_event_func);
1387}
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001388
1389static acpi_status
1390find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1391{
1392 int *count = (int *)context;
1393
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001394 if (!acpi_is_root_bridge(handle))
1395 return AE_OK;
1396
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001397 (*count)++;
1398 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1399 handle_hotplug_event_bridge, NULL);
1400
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001401 return AE_OK ;
1402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404static struct acpi_pci_driver acpi_pci_hp_driver = {
1405 .add = add_bridge,
1406 .remove = remove_bridge,
1407};
1408
1409/**
1410 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
1412int __init acpiphp_glue_init(void)
1413{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001414 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001416 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001417 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (num <= 0)
1420 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001421 else
1422 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 return 0;
1425}
1426
1427
1428/**
1429 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1430 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001431 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001433void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1436}
1437
1438
1439/**
1440 * acpiphp_get_num_slots - count number of slots in a system
1441 */
1442int __init acpiphp_get_num_slots(void)
1443{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001445 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Alex Chiang58c08622009-10-26 21:25:27 -06001447 list_for_each_entry(bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001448 dbg("Bus %04x:%02x has %d slot%s\n",
1449 pci_domain_nr(bridge->pci_bus),
1450 bridge->pci_bus->number, bridge->nr_slots,
1451 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 num_slots += bridge->nr_slots;
1453 }
1454
Rajesh Shah42f49a62005-04-28 00:25:53 -07001455 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return num_slots;
1457}
1458
1459
1460#if 0
1461/**
1462 * acpiphp_for_each_slot - call function for each slot
1463 * @fn: callback function
1464 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 */
1466static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1467{
1468 struct list_head *node;
1469 struct acpiphp_bridge *bridge;
1470 struct acpiphp_slot *slot;
1471 int retval = 0;
1472
1473 list_for_each (node, &bridge_list) {
1474 bridge = (struct acpiphp_bridge *)node;
1475 for (slot = bridge->slots; slot; slot = slot->next) {
1476 retval = fn(slot, data);
1477 if (!retval)
1478 goto err_exit;
1479 }
1480 }
1481
1482 err_exit:
1483 return retval;
1484}
1485#endif
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488/**
1489 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001490 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
1492int acpiphp_enable_slot(struct acpiphp_slot *slot)
1493{
1494 int retval;
1495
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001496 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /* wake up all functions */
1499 retval = power_on_slot(slot);
1500 if (retval)
1501 goto err_exit;
1502
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001503 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /* configure all functions */
1505 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001506 if (retval)
1507 power_off_slot(slot);
1508 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001509 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001510 power_off_slot(slot);
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001514 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return retval;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/**
1519 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001520 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 */
1522int acpiphp_disable_slot(struct acpiphp_slot *slot)
1523{
1524 int retval = 0;
1525
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001526 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /* unconfigure all functions */
1529 retval = disable_device(slot);
1530 if (retval)
1531 goto err_exit;
1532
1533 /* power off all functions */
1534 retval = power_off_slot(slot);
1535 if (retval)
1536 goto err_exit;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001539 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return retval;
1541}
1542
1543
1544/*
1545 * slot enabled: 1
1546 * slot disabled: 0
1547 */
1548u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1549{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001550 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553
1554/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001555 * latch open: 1
1556 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
1558u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1559{
1560 unsigned int sta;
1561
1562 sta = get_slot_status(slot);
1563
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001564 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567
1568/*
1569 * adapter presence : 1
1570 * absence : 0
1571 */
1572u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1573{
1574 unsigned int sta;
1575
1576 sta = get_slot_status(slot);
1577
1578 return (sta == 0) ? 0 : 1;
1579}