blob: 466233b481b1b0cac3afe25d15f93357e8d1347f [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
Yinghai Lub918c622012-05-17 18:51:11 -0700103 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400104 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700105 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700106 | ((unsigned int)(bus->busn_res.start) << 8)
107 | ((unsigned int)(bus->busn_res.end) << 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 */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900385static void add_host_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900388 acpi_handle handle = root->device->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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 init_bridge_misc(bridge);
400}
401
402
403/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600404static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100408 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (bridge == NULL) {
410 err("out of memory\n");
411 return;
412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 bridge->type = BRIDGE_TYPE_P2P;
415 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900416 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alex Chiang6edd7672009-09-10 12:34:04 -0600418 bridge->pci_dev = acpi_get_pci_dev(handle);
419 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!bridge->pci_bus) {
421 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700422 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600425 /*
426 * Grab a ref to the subordinate PCI bus in case the bus is
427 * removed via PCI core logical hotplug. The ref pins the bus
428 * (which we access during module unload).
429 */
430 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700433 return;
434 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600435 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 kfree(bridge);
437 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440
441/* callback routine to find P2P bridges */
442static acpi_status
443find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
444{
445 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alex Chiang6edd7672009-09-10 12:34:04 -0600448 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700449 if (!dev || !dev->subordinate)
450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600453 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600455 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900458 /* search P2P bridges under this p2p bridge */
459 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800460 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900461 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900462 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900463
Rajesh Shah42f49a62005-04-28 00:25:53 -0700464 out:
465 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return AE_OK;
467}
468
469
470/* find hot-pluggable slots, and then find P2P bridge */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900471static int add_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400474 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 acpi_handle dummy_handle;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900476 acpi_handle handle = root->device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* if the bridge doesn't have _STA, we assume it is always there */
479 status = acpi_get_handle(handle, "_STA", &dummy_handle);
480 if (ACPI_SUCCESS(status)) {
481 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
482 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800483 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485 }
486 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
487 /* don't register this object */
488 return 0;
489 }
490
Rajesh Shah42f49a62005-04-28 00:25:53 -0700491 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600492 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700493 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900494 add_host_bridge(root);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* search P2P bridges under this host bridge */
498 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800499 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900502 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return 0;
505}
506
Rajesh Shah42f49a62005-04-28 00:25:53 -0700507static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
508{
Alex Chiang58c08622009-10-26 21:25:27 -0600509 struct acpiphp_bridge *bridge;
510
511 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700512 if (bridge->handle == handle)
513 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700514
515 return NULL;
516}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Rajesh Shah364d5092005-04-28 00:25:54 -0700518static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Alex Chiang58c08622009-10-26 21:25:27 -0600520 struct acpiphp_slot *slot, *next;
521 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700522 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700523 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700524
525 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
526 handle_hotplug_event_bridge);
527 if (ACPI_FAILURE(status))
528 err("failed to remove notify handler\n");
529
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900530 if ((bridge->type != BRIDGE_TYPE_HOST) &&
531 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
532 status = acpi_install_notify_handler(bridge->func->handle,
533 ACPI_SYSTEM_NOTIFY,
534 handle_hotplug_event_func,
535 bridge->func);
536 if (ACPI_FAILURE(status))
537 err("failed to install interrupt notify handler\n");
538 }
539
Rajesh Shah42f49a62005-04-28 00:25:53 -0700540 slot = bridge->slots;
541 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600542 next = slot->next;
543 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400544 if (is_dock_device(func->handle)) {
545 unregister_hotplug_dock_device(func->handle);
546 unregister_dock_notifier(&func->nb);
547 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800548 if (!(func->flags & FUNC_HAS_DCK)) {
549 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700550 ACPI_SYSTEM_NOTIFY,
551 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800552 if (ACPI_FAILURE(status))
553 err("failed to remove notify handler\n");
554 }
Alex Chiang58c08622009-10-26 21:25:27 -0600555 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700556 kfree(func);
557 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800558 acpiphp_unregister_hotplug_slot(slot);
559 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700560 kfree(slot);
561 slot = next;
562 }
563
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600564 /*
565 * Only P2P bridges have a pci_dev
566 */
567 if (bridge->pci_dev)
568 put_device(&bridge->pci_bus->dev);
569
Rajesh Shah42f49a62005-04-28 00:25:53 -0700570 pci_dev_put(bridge->pci_dev);
571 list_del(&bridge->list);
572 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Rajesh Shah364d5092005-04-28 00:25:54 -0700575static acpi_status
576cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
577{
578 struct acpiphp_bridge *bridge;
579
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900580 /* cleanup p2p bridges under this P2P bridge
581 in a depth-first manner */
582 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800583 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900584
Alex Chianga13307c2008-07-01 20:02:23 -0600585 bridge = acpiphp_handle_to_bridge(handle);
586 if (bridge)
587 cleanup_bridge(bridge);
588
Rajesh Shah364d5092005-04-28 00:25:54 -0700589 return AE_OK;
590}
591
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900592static void remove_bridge(struct acpi_pci_root *root)
Rajesh Shah364d5092005-04-28 00:25:54 -0700593{
594 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900595 acpi_handle handle = root->device->handle;
Rajesh Shah364d5092005-04-28 00:25:54 -0700596
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900597 /* cleanup p2p bridges under this host bridge
598 in a depth-first manner */
599 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800600 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900601
Alex Chianga13307c2008-07-01 20:02:23 -0600602 /*
603 * On root bridges with hotplug slots directly underneath (ie,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300604 * no p2p bridge between), we call cleanup_bridge().
Alex Chianga13307c2008-07-01 20:02:23 -0600605 *
606 * The else clause cleans up root bridges that either had no
607 * hotplug slots at all, or had a p2p bridge underneath.
608 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700609 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900610 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700611 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600612 else
613 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
614 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617static int power_on_slot(struct acpiphp_slot *slot)
618{
619 acpi_status status;
620 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int retval = 0;
622
623 /* if already enabled, just skip */
624 if (slot->flags & SLOT_POWEREDON)
625 goto err_exit;
626
Alex Chiang58c08622009-10-26 21:25:27 -0600627 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800629 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
631 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800632 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 retval = -1;
634 goto err_exit;
635 } else
636 break;
637 }
638 }
639
640 /* TBD: evaluate _STA to check if the slot is enabled */
641
642 slot->flags |= SLOT_POWEREDON;
643
644 err_exit:
645 return retval;
646}
647
648
649static int power_off_slot(struct acpiphp_slot *slot)
650{
651 acpi_status status;
652 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 int retval = 0;
655
656 /* if already disabled, just skip */
657 if ((slot->flags & SLOT_POWEREDON) == 0)
658 goto err_exit;
659
Alex Chiang58c08622009-10-26 21:25:27 -0600660 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700661 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
663 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800664 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 retval = -1;
666 goto err_exit;
667 } else
668 break;
669 }
670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* TBD: evaluate _STA to check if the slot is disabled */
673
674 slot->flags &= (~SLOT_POWEREDON);
675
676 err_exit:
677 return retval;
678}
679
680
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800681
682/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800683 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800684 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800685 */
686static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
687{
688 struct list_head *tmp;
689 unsigned char max, n;
690
691 /*
692 * pci_bus_max_busnr will return the highest
693 * reserved busnr for all these children.
694 * that is equivalent to the bus->subordinate
695 * value. We don't want to use the parent's
696 * bus->subordinate value because it could have
697 * padding in it.
698 */
Yinghai Lub918c622012-05-17 18:51:11 -0700699 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800700
701 list_for_each(tmp, &bus->children) {
702 n = pci_bus_max_busnr(pci_bus_b(tmp));
703 if (n > max)
704 max = n;
705 }
706 return max;
707}
708
709
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800710/**
711 * acpiphp_bus_add - add a new bus to acpi subsystem
712 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800713 */
714static int acpiphp_bus_add(struct acpiphp_func *func)
715{
716 acpi_handle phandle;
717 struct acpi_device *device, *pdevice;
718 int ret_val;
719
720 acpi_get_parent(func->handle, &phandle);
721 if (acpi_bus_get_device(phandle, &pdevice)) {
722 dbg("no parent device, assuming NULL\n");
723 pdevice = NULL;
724 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800725 if (!acpi_bus_get_device(func->handle, &device)) {
726 dbg("bus exists... trim\n");
727 /* this shouldn't be in here, so remove
728 * the bus then re-add it...
729 */
730 ret_val = acpi_bus_trim(device, 1);
731 dbg("acpi_bus_trim return %x\n", ret_val);
732 }
733
734 ret_val = acpi_bus_add(&device, pdevice, func->handle,
735 ACPI_BUS_TYPE_DEVICE);
736 if (ret_val) {
737 dbg("error adding bus, %x\n",
738 -ret_val);
739 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800740 }
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800741 ret_val = acpi_bus_start(device);
742
743acpiphp_bus_add_out:
744 return ret_val;
745}
746
747
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900748/**
749 * acpiphp_bus_trim - trim a bus from acpi subsystem
750 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900751 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700752static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900753{
754 struct acpi_device *device;
755 int retval;
756
757 retval = acpi_bus_get_device(handle, &device);
758 if (retval) {
759 dbg("acpi_device not found\n");
760 return retval;
761 }
762
763 retval = acpi_bus_trim(device, 1);
764 if (retval)
765 err("cannot remove from acpi list\n");
766
767 return retval;
768}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800769
Shaohua Lid0607052010-02-25 10:59:34 +0800770static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
771{
772 struct acpiphp_func *func;
773 union acpi_object params[2];
774 struct acpi_object_list arg_list;
775
776 list_for_each_entry(func, &slot->funcs, sibling) {
777 arg_list.count = 2;
778 arg_list.pointer = params;
779 params[0].type = ACPI_TYPE_INTEGER;
780 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
781 params[1].type = ACPI_TYPE_INTEGER;
782 params[1].integer.value = 1;
783 /* _REG is optional, we don't care about if there is failure */
784 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
785 }
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/**
789 * enable_device - enable, configure a slot
790 * @slot: slot to be enabled
791 *
792 * This function should be called per *physical slot*,
793 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100795static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700798 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct acpiphp_func *func;
800 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700801 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900802 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (slot->flags & SLOT_ENABLED)
805 goto err_exit;
806
Rajesh Shah42f49a62005-04-28 00:25:53 -0700807 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
808 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800809 /* Maybe only part of funcs are added. */
810 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto err_exit;
812 }
813
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800814 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700815 for (pass = 0; pass < 2; pass++) {
816 list_for_each_entry(dev, &bus->devices, bus_list) {
817 if (PCI_SLOT(dev->devfn) != slot->device)
818 continue;
819 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800820 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700821 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900822 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800823 pci_bus_size_bridges(dev->subordinate);
824 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Alex Chiang58c08622009-10-26 21:25:27 -0600828 list_for_each_entry(func, &slot->funcs, sibling)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900829 acpiphp_bus_add(func);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900830
Rajesh Shah42f49a62005-04-28 00:25:53 -0700831 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700832 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600833 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800834 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700835 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100836
837 list_for_each_entry(dev, &bus->devices, bus_list) {
838 /* Assume that newly added devices are powered on already. */
839 if (!dev->is_added)
840 dev->current_state = PCI_D0;
841 }
842
Rajesh Shah42f49a62005-04-28 00:25:53 -0700843 pci_bus_add_devices(bus);
844
Amos Kongf382a082011-11-25 15:03:07 +0800845 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600846 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600847 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
848 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800849 if (!dev) {
850 /* Do not set SLOT_ENABLED flag if some funcs
851 are not added. */
852 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900853 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800854 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900855
Alex Chiang9d911d72009-05-21 16:21:15 -0600856 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
857 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
858 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900859 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600860 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900861
862 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
863 if (ACPI_FAILURE(status))
864 warn("find_p2p_bridge failed (error code = 0x%x)\n",
865 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600866 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 err_exit:
871 return retval;
872}
873
Amos Kongce29ca32012-05-23 10:20:35 -0600874/* return first device in slot, acquiring a reference on it */
875static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
876{
877 struct pci_bus *bus = slot->bridge->pci_bus;
878 struct pci_dev *dev;
879 struct pci_dev *ret = NULL;
880
881 down_read(&pci_bus_sem);
882 list_for_each_entry(dev, &bus->devices, bus_list)
883 if (PCI_SLOT(dev->devfn) == slot->device) {
884 ret = pci_dev_get(dev);
885 break;
886 }
887 up_read(&pci_bus_sem);
888
889 return ret;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/**
893 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800894 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
896static int disable_device(struct acpiphp_slot *slot)
897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600899 struct pci_dev *pdev;
Amos Kongf382a082011-11-25 15:03:07 +0800900 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Amos Kongf382a082011-11-25 15:03:07 +0800902 /* The slot will be enabled when func 0 is added, so check
903 func 0 before disable the slot. */
904 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
905 if (!pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto err_exit;
Amos Kong638f2932012-05-22 21:58:40 -0600907 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Alex Chiang9d911d72009-05-21 16:21:15 -0600909 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900910 if (func->bridge) {
911 /* cleanup p2p bridges under this P2P bridge */
912 cleanup_p2p_bridge(func->bridge->handle,
913 (u32)1, NULL, NULL);
914 func->bridge = NULL;
915 }
Amos Kongce29ca32012-05-23 10:20:35 -0600916 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900917
Amos Kongce29ca32012-05-23 10:20:35 -0600918 /*
919 * enable_device() enumerates all functions in this device via
920 * pci_scan_slot(), whether they have associated ACPI hotplug
921 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
922 * here.
923 */
924 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600925 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600926 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700927 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700928
Alex Chiang9d911d72009-05-21 16:21:15 -0600929 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900930 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 slot->flags &= (~SLOT_ENABLED);
934
Alex Chiang9d911d72009-05-21 16:21:15 -0600935err_exit:
936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939
940/**
941 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800942 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800944 * If a slot has _STA for each function and if any one of them
945 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800947 * If a slot doesn't have _STA and if any one of its functions'
948 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800950 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
952static unsigned int get_slot_status(struct acpiphp_slot *slot)
953{
954 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400955 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 struct acpiphp_func *func;
958
Alex Chiang58c08622009-10-26 21:25:27 -0600959 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (func->flags & FUNC_HAS_STA) {
961 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
962 if (ACPI_SUCCESS(status) && sta)
963 break;
964 } else {
965 pci_bus_read_config_dword(slot->bridge->pci_bus,
966 PCI_DEVFN(slot->device,
967 func->function),
968 PCI_VENDOR_ID, &dvid);
969 if (dvid != 0xffffffff) {
970 sta = ACPI_STA_ALL;
971 break;
972 }
973 }
974 }
975
976 return (unsigned int)sta;
977}
978
979/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700980 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800981 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700982 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700983int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700984{
985 acpi_status status;
986 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700987 struct acpi_object_list arg_list;
988 union acpi_object arg;
989
Alex Chiang58c08622009-10-26 21:25:27 -0600990 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700991 /* We don't want to call _EJ0 on non-existing functions. */
992 if ((func->flags & FUNC_HAS_EJ0)) {
993 /* _EJ0 method take one argument */
994 arg_list.count = 1;
995 arg_list.pointer = &arg;
996 arg.type = ACPI_TYPE_INTEGER;
997 arg.integer.value = 1;
998
999 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1000 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001001 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001002 return -1;
1003 } else
1004 break;
1005 }
1006 }
1007 return 0;
1008}
1009
1010/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001012 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
1014 * Iterate over all slots under this bridge and make sure that if a
1015 * card is present they are enabled, and if not they are disabled.
1016 */
1017static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1018{
1019 struct acpiphp_slot *slot;
1020 int retval = 0;
1021 int enabled, disabled;
1022
1023 enabled = disabled = 0;
1024
1025 for (slot = bridge->slots; slot; slot = slot->next) {
1026 unsigned int status = get_slot_status(slot);
1027 if (slot->flags & SLOT_ENABLED) {
1028 if (status == ACPI_STA_ALL)
1029 continue;
1030 retval = acpiphp_disable_slot(slot);
1031 if (retval) {
1032 err("Error occurred in disabling\n");
1033 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001034 } else {
1035 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 disabled++;
1038 } else {
1039 if (status != ACPI_STA_ALL)
1040 continue;
1041 retval = acpiphp_enable_slot(slot);
1042 if (retval) {
1043 err("Error occurred in enabling\n");
1044 goto err_exit;
1045 }
1046 enabled++;
1047 }
1048 }
1049
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001050 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 err_exit:
1053 return retval;
1054}
1055
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001056static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001057{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001058 struct pci_dev *dev;
1059
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001060 list_for_each_entry(dev, &bus->devices, bus_list)
1061 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001062}
1063
1064/*
1065 * Remove devices for which we could not assign resources, call
1066 * arch specific code to fix-up the bus
1067 */
1068static void acpiphp_sanitize_bus(struct pci_bus *bus)
1069{
1070 struct pci_dev *dev;
1071 int i;
1072 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1073
1074 list_for_each_entry(dev, &bus->devices, bus_list) {
1075 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1076 struct resource *res = &dev->resource[i];
1077 if ((res->flags & type_mask) && !res->start &&
1078 res->end) {
1079 /* Could not assign a required resources
1080 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -08001081 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001082 break;
1083 }
1084 }
1085 }
1086}
1087
1088/* Program resources in newly inserted bridge */
1089static int acpiphp_configure_bridge (acpi_handle handle)
1090{
Alex Chiang7f538662009-09-10 12:34:09 -06001091 struct pci_bus *bus;
1092
1093 if (acpi_is_root_bridge(handle)) {
1094 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1095 bus = root->bus;
1096 } else {
1097 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1098 bus = pdev->subordinate;
1099 pci_dev_put(pdev);
1100 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001101
1102 pci_bus_size_bridges(bus);
1103 pci_bus_assign_resources(bus);
1104 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001105 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001106 pci_enable_bridges(bus);
1107 return 0;
1108}
1109
1110static void handle_bridge_insertion(acpi_handle handle, u32 type)
1111{
1112 struct acpi_device *device, *pdevice;
1113 acpi_handle phandle;
1114
1115 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1116 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1117 err("unexpected notification type %d\n", type);
1118 return;
1119 }
1120
1121 acpi_get_parent(handle, &phandle);
1122 if (acpi_bus_get_device(phandle, &pdevice)) {
1123 dbg("no parent device, assuming NULL\n");
1124 pdevice = NULL;
1125 }
1126 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1127 err("cannot add bridge to acpi list\n");
1128 return;
1129 }
1130 if (!acpiphp_configure_bridge(handle) &&
1131 !acpi_bus_start(device))
1132 add_bridge(handle);
1133 else
1134 err("cannot configure and start bridge\n");
1135
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/*
1139 * ACPI event handlers
1140 */
1141
Gary Hade0bbd6422007-07-05 11:10:48 -07001142static acpi_status
1143count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1144{
1145 int *count = (int *)context;
1146 struct acpiphp_bridge *bridge;
1147
1148 bridge = acpiphp_handle_to_bridge(handle);
1149 if (bridge)
1150 (*count)++;
1151 return AE_OK ;
1152}
1153
1154static acpi_status
1155check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1156{
1157 struct acpiphp_bridge *bridge;
1158 char objname[64];
1159 struct acpi_buffer buffer = { .length = sizeof(objname),
1160 .pointer = objname };
1161
1162 bridge = acpiphp_handle_to_bridge(handle);
1163 if (bridge) {
1164 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
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 return AE_OK ;
1170}
1171
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001172struct acpiphp_hp_work {
1173 struct work_struct work;
1174 acpi_handle handle;
1175 u32 type;
1176 void *context;
1177};
1178
1179static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1180 void *context,
1181 void (*func)(struct work_struct *work))
1182{
1183 struct acpiphp_hp_work *hp_work;
1184 int ret;
1185
1186 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1187 if (!hp_work)
1188 return;
1189
1190 hp_work->handle = handle;
1191 hp_work->type = type;
1192 hp_work->context = context;
1193
1194 INIT_WORK(&hp_work->work, func);
1195 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1196 if (!ret)
1197 kfree(hp_work);
1198}
1199
1200static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct acpiphp_bridge *bridge;
1203 char objname[64];
1204 struct acpi_buffer buffer = { .length = sizeof(objname),
1205 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001206 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001207 int num_sub_bridges = 0;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001208 struct acpiphp_hp_work *hp_work;
1209 acpi_handle handle;
1210 u32 type;
1211
1212 hp_work = container_of(work, struct acpiphp_hp_work, work);
1213 handle = hp_work->handle;
1214 type = hp_work->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001216 if (acpi_bus_get_device(handle, &device)) {
1217 /* This bridge must have just been physically inserted */
1218 handle_bridge_insertion(handle, type);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001219 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001220 }
1221
1222 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001223 if (type == ACPI_NOTIFY_BUS_CHECK) {
1224 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001225 count_sub_bridges, NULL, &num_sub_bridges, NULL);
Gary Hade0bbd6422007-07-05 11:10:48 -07001226 }
1227
1228 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001229 err("cannot get bridge info\n");
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001230 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1234
1235 switch (type) {
1236 case ACPI_NOTIFY_BUS_CHECK:
1237 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001238 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001239 if (bridge) {
1240 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001241 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001242 acpiphp_check_bridge(bridge);
1243 }
1244 if (num_sub_bridges)
1245 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +08001246 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248
1249 case ACPI_NOTIFY_DEVICE_CHECK:
1250 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001251 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 acpiphp_check_bridge(bridge);
1253 break;
1254
1255 case ACPI_NOTIFY_DEVICE_WAKE:
1256 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001257 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
1259
1260 case ACPI_NOTIFY_EJECT_REQUEST:
1261 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001262 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001263 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1264 (bridge->flags & BRIDGE_HAS_EJ0)) {
1265 struct acpiphp_slot *slot;
1266 slot = bridge->func->slot;
1267 if (!acpiphp_disable_slot(slot))
1268 acpiphp_eject_slot(slot);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
1271
1272 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1273 printk(KERN_ERR "Device %s cannot be configured due"
1274 " to a frequency mismatch\n", objname);
1275 break;
1276
1277 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1278 printk(KERN_ERR "Device %s cannot be configured due"
1279 " to a bus mode mismatch\n", objname);
1280 break;
1281
1282 case ACPI_NOTIFY_POWER_FAULT:
1283 printk(KERN_ERR "Device %s has suffered a power fault\n",
1284 objname);
1285 break;
1286
1287 default:
1288 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1289 break;
1290 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001291
1292out:
1293 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001297 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * @handle: Notify()'ed acpi_handle
1299 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001300 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001302 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001304static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1305 void *context)
1306{
1307 /*
1308 * Currently the code adds all hotplug events to the kacpid_wq
1309 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1310 * The proper way to fix this is to reorganize the code so that
1311 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1312 * For now just re-add this work to the kacpi_hotplug_wq so we
1313 * don't deadlock on hotplug actions.
1314 */
1315 alloc_acpiphp_hp_work(handle, type, context,
1316 _handle_hotplug_event_bridge);
1317}
1318
1319static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct acpiphp_func *func;
1322 char objname[64];
1323 struct acpi_buffer buffer = { .length = sizeof(objname),
1324 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001325 struct acpiphp_hp_work *hp_work;
1326 acpi_handle handle;
1327 u32 type;
1328 void *context;
1329
1330 hp_work = container_of(work, struct acpiphp_hp_work, work);
1331 handle = hp_work->handle;
1332 type = hp_work->type;
1333 context = hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1336
1337 func = (struct acpiphp_func *)context;
1338
1339 switch (type) {
1340 case ACPI_NOTIFY_BUS_CHECK:
1341 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001342 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 acpiphp_enable_slot(func->slot);
1344 break;
1345
1346 case ACPI_NOTIFY_DEVICE_CHECK:
1347 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001348 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 acpiphp_check_bridge(func->slot->bridge);
1350 break;
1351
1352 case ACPI_NOTIFY_DEVICE_WAKE:
1353 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001354 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
1356
1357 case ACPI_NOTIFY_EJECT_REQUEST:
1358 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001359 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001360 if (!(acpiphp_disable_slot(func->slot)))
1361 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
1363
1364 default:
1365 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1366 break;
1367 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001368
1369 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001372/**
1373 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1374 * @handle: Notify()'ed acpi_handle
1375 * @type: Notify code
1376 * @context: pointer to acpiphp_func structure
1377 *
1378 * Handles ACPI event notification on slots.
1379 */
1380static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1381 void *context)
1382{
1383 /*
1384 * Currently the code adds all hotplug events to the kacpid_wq
1385 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1386 * The proper way to fix this is to reorganize the code so that
1387 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1388 * For now just re-add this work to the kacpi_hotplug_wq so we
1389 * don't deadlock on hotplug actions.
1390 */
1391 alloc_acpiphp_hp_work(handle, type, context,
1392 _handle_hotplug_event_func);
1393}
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001394
1395static acpi_status
1396find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1397{
1398 int *count = (int *)context;
1399
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001400 if (!acpi_is_root_bridge(handle))
1401 return AE_OK;
1402
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001403 (*count)++;
1404 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1405 handle_hotplug_event_bridge, NULL);
1406
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001407 return AE_OK ;
1408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410static struct acpi_pci_driver acpi_pci_hp_driver = {
1411 .add = add_bridge,
1412 .remove = remove_bridge,
1413};
1414
1415/**
1416 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418int __init acpiphp_glue_init(void)
1419{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001420 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001422 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001423 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (num <= 0)
1426 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001427 else
1428 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 return 0;
1431}
1432
1433
1434/**
1435 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1436 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001437 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001439void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1442}
1443
1444
1445/**
1446 * acpiphp_get_num_slots - count number of slots in a system
1447 */
1448int __init acpiphp_get_num_slots(void)
1449{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001451 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Alex Chiang58c08622009-10-26 21:25:27 -06001453 list_for_each_entry(bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001454 dbg("Bus %04x:%02x has %d slot%s\n",
1455 pci_domain_nr(bridge->pci_bus),
1456 bridge->pci_bus->number, bridge->nr_slots,
1457 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 num_slots += bridge->nr_slots;
1459 }
1460
Rajesh Shah42f49a62005-04-28 00:25:53 -07001461 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return num_slots;
1463}
1464
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/**
1467 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001468 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 */
1470int acpiphp_enable_slot(struct acpiphp_slot *slot)
1471{
1472 int retval;
1473
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001474 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 /* wake up all functions */
1477 retval = power_on_slot(slot);
1478 if (retval)
1479 goto err_exit;
1480
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001481 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /* configure all functions */
1483 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001484 if (retval)
1485 power_off_slot(slot);
1486 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001487 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001488 power_off_slot(slot);
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001492 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return retval;
1494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/**
1497 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001498 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
1500int acpiphp_disable_slot(struct acpiphp_slot *slot)
1501{
1502 int retval = 0;
1503
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001504 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /* unconfigure all functions */
1507 retval = disable_device(slot);
1508 if (retval)
1509 goto err_exit;
1510
1511 /* power off all functions */
1512 retval = power_off_slot(slot);
1513 if (retval)
1514 goto err_exit;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001517 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return retval;
1519}
1520
1521
1522/*
1523 * slot enabled: 1
1524 * slot disabled: 0
1525 */
1526u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1527{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001528 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531
1532/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001533 * latch open: 1
1534 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
1536u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1537{
1538 unsigned int sta;
1539
1540 sta = get_slot_status(slot);
1541
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001542 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545
1546/*
1547 * adapter presence : 1
1548 * absence : 0
1549 */
1550u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1551{
1552 unsigned int sta;
1553
1554 sta = get_slot_status(slot);
1555
1556 return (sta == 0) ? 0 : 1;
1557}