blob: 68360d5b494a55014385d3362ecc0ad4d417b39b [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
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100135 pdev = pbus->self;
136 if (pdev && pci_is_pcie(pdev)) {
137 tmp = acpi_find_root_bridge_handle(pdev);
138 if (tmp) {
139 struct acpi_pci_root *root = acpi_pci_find_root(tmp);
140
141 if (root && (root->osc_control_set &
142 OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
143 return AE_OK;
144 }
145 }
146
Matthew Garrett56ee3252008-11-25 21:48:14 +0000147 acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 device = (adr >> 16) & 0xffff;
149 function = adr & 0xffff;
150
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100151 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!newfunc)
153 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 INIT_LIST_HEAD(&newfunc->sibling);
156 newfunc->handle = handle;
157 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000158
159 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800160 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
163 newfunc->flags |= FUNC_HAS_STA;
164
165 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
166 newfunc->flags |= FUNC_HAS_PS0;
167
168 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
169 newfunc->flags |= FUNC_HAS_PS3;
170
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400171 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800172 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400175 if (ACPI_FAILURE(status)) {
176 /*
177 * use the count of the number of slots we've found
178 * for the number of the slot
179 */
180 sun = bridge->nr_slots+1;
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* search for objects that share the same slot */
184 for (slot = bridge->slots; slot; slot = slot->next)
185 if (slot->device == device) {
186 if (slot->sun != sun)
187 warn("sibling found, but _SUN doesn't match!\n");
188 break;
189 }
190
191 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100192 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!slot) {
194 kfree(newfunc);
195 return AE_NO_MEMORY;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 slot->device = device;
200 slot->sun = sun;
201 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100202 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 slot->next = bridge->slots;
205 bridge->slots = slot;
206
207 bridge->nr_slots++;
208
Justin Chenb6adc192008-12-11 11:16:44 -0700209 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900210 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800211 retval = acpiphp_register_hotplug_slot(slot);
212 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600213 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700214 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600215 "hotplug driver\n", slot->sun);
216 else
217 warn("acpiphp_register_hotplug_slot failed "
218 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800219 goto err_exit;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 newfunc->slot = slot;
224 list_add_tail(&newfunc->sibling, &slot->funcs);
225
Alex Chiang9d911d72009-05-21 16:21:15 -0600226 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
227 if (pdev) {
Stefano Stabellini47e90372011-02-28 16:20:11 +0000228 pdev->current_state = PCI_D0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600230 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400233 if (is_dock_device(handle)) {
234 /* we don't want to call this device's _EJ0
235 * because we want the dock notify handler
236 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800237 */
238 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400239 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800240 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400241 dbg("failed to register dock device\n");
242
243 /* we need to be notified when dock events happen
244 * outside of the hotplug operation, since we may
245 * need to do fixups before we can hotplug.
246 */
247 newfunc->nb.notifier_call = post_dock_fixups;
248 if (register_dock_notifier(&newfunc->nb))
249 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800250 }
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800253 if (!(newfunc->flags & FUNC_HAS_DCK)) {
254 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ACPI_SYSTEM_NOTIFY,
256 handle_hotplug_event_func,
257 newfunc);
258
Kristen Accardi20416ea2006-02-23 17:56:03 -0800259 if (ACPI_FAILURE(status))
260 err("failed to register interrupt notify handler\n");
261 } else
262 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kristen Accardi20416ea2006-02-23 17:56:03 -0800264 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800265
266 err_exit:
267 bridge->nr_slots--;
268 bridge->slots = slot->next;
269 kfree(slot);
270 kfree(newfunc);
271
272 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275
276/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600277static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Alex Chiang7f538662009-09-10 12:34:09 -0600279 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900280 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600281 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800282 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900283 }
284 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
288static void init_bridge_misc(struct acpiphp_bridge *bridge)
289{
290 acpi_status status;
291
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800292 /* must be added to the list prior to calling register_slot */
293 list_add(&bridge->list, &bridge_list);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* register all slot objects under this bridge */
296 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800297 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800298 if (ACPI_FAILURE(status)) {
299 list_del(&bridge->list);
300 return;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700304 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900305 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
306 status = acpi_remove_notify_handler(bridge->func->handle,
307 ACPI_SYSTEM_NOTIFY,
308 handle_hotplug_event_func);
309 if (ACPI_FAILURE(status))
310 err("failed to remove notify handler\n");
311 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700312 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ACPI_SYSTEM_NOTIFY,
314 handle_hotplug_event_bridge,
315 bridge);
316
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700317 if (ACPI_FAILURE(status)) {
318 err("failed to register interrupt notify handler\n");
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900324/* find acpiphp_func from acpiphp_bridge */
325static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
326{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900327 struct acpiphp_bridge *bridge;
328 struct acpiphp_slot *slot;
329 struct acpiphp_func *func;
330
Alex Chiang58c08622009-10-26 21:25:27 -0600331 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900332 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600333 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900334 if (func->handle == handle)
335 return func;
336 }
337 }
338 }
339
340 return NULL;
341}
342
343
344static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
345{
346 acpi_handle dummy_handle;
347
348 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
349 "_STA", &dummy_handle)))
350 bridge->flags |= BRIDGE_HAS_STA;
351
352 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
353 "_EJ0", &dummy_handle)))
354 bridge->flags |= BRIDGE_HAS_EJ0;
355
356 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
357 "_PS0", &dummy_handle)))
358 bridge->flags |= BRIDGE_HAS_PS0;
359
360 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
361 "_PS3", &dummy_handle)))
362 bridge->flags |= BRIDGE_HAS_PS3;
363
364 /* is this ejectable p2p bridge? */
365 if (bridge->flags & BRIDGE_HAS_EJ0) {
366 struct acpiphp_func *func;
367
368 dbg("found ejectable p2p bridge\n");
369
370 /* make link between PCI bridge and PCI function */
371 func = acpiphp_bridge_handle_to_function(bridge->handle);
372 if (!func)
373 return;
374 bridge->func = func;
375 func->bridge = bridge;
376 }
377}
378
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* allocate and initialize host bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600381static void add_host_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct acpiphp_bridge *bridge;
Alex Chiang6edd7672009-09-10 12:34:04 -0600384 struct acpi_pci_root *root = acpi_pci_find_root(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100386 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (bridge == NULL)
388 return;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 bridge->type = BRIDGE_TYPE_HOST;
391 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Alex Chiang6edd7672009-09-10 12:34:04 -0600393 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 spin_lock_init(&bridge->res_lock);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 init_bridge_misc(bridge);
398}
399
400
401/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600402static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100406 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (bridge == NULL) {
408 err("out of memory\n");
409 return;
410 }
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 bridge->type = BRIDGE_TYPE_P2P;
413 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900414 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alex Chiang6edd7672009-09-10 12:34:04 -0600416 bridge->pci_dev = acpi_get_pci_dev(handle);
417 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!bridge->pci_bus) {
419 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700420 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600423 /*
424 * Grab a ref to the subordinate PCI bus in case the bus is
425 * removed via PCI core logical hotplug. The ref pins the bus
426 * (which we access during module unload).
427 */
428 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 spin_lock_init(&bridge->res_lock);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700432 return;
433 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600434 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700435 kfree(bridge);
436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439
440/* callback routine to find P2P bridges */
441static acpi_status
442find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
443{
444 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Alex Chiang6edd7672009-09-10 12:34:04 -0600447 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700448 if (!dev || !dev->subordinate)
449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600452 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600454 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900457 /* search P2P bridges under this p2p bridge */
458 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800459 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900460 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900461 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900462
Rajesh Shah42f49a62005-04-28 00:25:53 -0700463 out:
464 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return AE_OK;
466}
467
468
469/* find hot-pluggable slots, and then find P2P bridge */
470static int add_bridge(acpi_handle handle)
471{
472 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400473 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 acpi_handle dummy_handle;
475
476 /* if the bridge doesn't have _STA, we assume it is always there */
477 status = acpi_get_handle(handle, "_STA", &dummy_handle);
478 if (ACPI_SUCCESS(status)) {
479 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
480 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800481 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483 }
484 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
485 /* don't register this object */
486 return 0;
487 }
488
Rajesh Shah42f49a62005-04-28 00:25:53 -0700489 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600490 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700491 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Alex Chiang6edd7672009-09-10 12:34:04 -0600492 add_host_bridge(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* search P2P bridges under this host bridge */
496 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800497 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900500 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 return 0;
503}
504
Rajesh Shah42f49a62005-04-28 00:25:53 -0700505static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
506{
Alex Chiang58c08622009-10-26 21:25:27 -0600507 struct acpiphp_bridge *bridge;
508
509 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700510 if (bridge->handle == handle)
511 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700512
513 return NULL;
514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Rajesh Shah364d5092005-04-28 00:25:54 -0700516static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Alex Chiang58c08622009-10-26 21:25:27 -0600518 struct acpiphp_slot *slot, *next;
519 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700520 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700521 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700522
523 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
524 handle_hotplug_event_bridge);
525 if (ACPI_FAILURE(status))
526 err("failed to remove notify handler\n");
527
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900528 if ((bridge->type != BRIDGE_TYPE_HOST) &&
529 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
530 status = acpi_install_notify_handler(bridge->func->handle,
531 ACPI_SYSTEM_NOTIFY,
532 handle_hotplug_event_func,
533 bridge->func);
534 if (ACPI_FAILURE(status))
535 err("failed to install interrupt notify handler\n");
536 }
537
Rajesh Shah42f49a62005-04-28 00:25:53 -0700538 slot = bridge->slots;
539 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600540 next = slot->next;
541 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400542 if (is_dock_device(func->handle)) {
543 unregister_hotplug_dock_device(func->handle);
544 unregister_dock_notifier(&func->nb);
545 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800546 if (!(func->flags & FUNC_HAS_DCK)) {
547 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700548 ACPI_SYSTEM_NOTIFY,
549 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800550 if (ACPI_FAILURE(status))
551 err("failed to remove notify handler\n");
552 }
Alex Chiang58c08622009-10-26 21:25:27 -0600553 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 kfree(func);
555 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800556 acpiphp_unregister_hotplug_slot(slot);
557 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700558 kfree(slot);
559 slot = next;
560 }
561
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600562 /*
563 * Only P2P bridges have a pci_dev
564 */
565 if (bridge->pci_dev)
566 put_device(&bridge->pci_bus->dev);
567
Rajesh Shah42f49a62005-04-28 00:25:53 -0700568 pci_dev_put(bridge->pci_dev);
569 list_del(&bridge->list);
570 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Rajesh Shah364d5092005-04-28 00:25:54 -0700573static acpi_status
574cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
575{
576 struct acpiphp_bridge *bridge;
577
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900578 /* cleanup p2p bridges under this P2P bridge
579 in a depth-first manner */
580 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800581 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900582
Alex Chianga13307c2008-07-01 20:02:23 -0600583 bridge = acpiphp_handle_to_bridge(handle);
584 if (bridge)
585 cleanup_bridge(bridge);
586
Rajesh Shah364d5092005-04-28 00:25:54 -0700587 return AE_OK;
588}
589
590static void remove_bridge(acpi_handle handle)
591{
592 struct acpiphp_bridge *bridge;
593
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900594 /* cleanup p2p bridges under this host bridge
595 in a depth-first manner */
596 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800597 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900598
Alex Chianga13307c2008-07-01 20:02:23 -0600599 /*
600 * On root bridges with hotplug slots directly underneath (ie,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300601 * no p2p bridge between), we call cleanup_bridge().
Alex Chianga13307c2008-07-01 20:02:23 -0600602 *
603 * The else clause cleans up root bridges that either had no
604 * hotplug slots at all, or had a p2p bridge underneath.
605 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700606 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900607 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700608 cleanup_bridge(bridge);
Alex Chianga13307c2008-07-01 20:02:23 -0600609 else
610 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
611 handle_hotplug_event_bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614static int power_on_slot(struct acpiphp_slot *slot)
615{
616 acpi_status status;
617 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int retval = 0;
619
620 /* if already enabled, just skip */
621 if (slot->flags & SLOT_POWEREDON)
622 goto err_exit;
623
Alex Chiang58c08622009-10-26 21:25:27 -0600624 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800626 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
628 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800629 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 retval = -1;
631 goto err_exit;
632 } else
633 break;
634 }
635 }
636
637 /* TBD: evaluate _STA to check if the slot is enabled */
638
639 slot->flags |= SLOT_POWEREDON;
640
641 err_exit:
642 return retval;
643}
644
645
646static int power_off_slot(struct acpiphp_slot *slot)
647{
648 acpi_status status;
649 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 int retval = 0;
652
653 /* if already disabled, just skip */
654 if ((slot->flags & SLOT_POWEREDON) == 0)
655 goto err_exit;
656
Alex Chiang58c08622009-10-26 21:25:27 -0600657 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700658 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
660 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800661 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 retval = -1;
663 goto err_exit;
664 } else
665 break;
666 }
667 }
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /* TBD: evaluate _STA to check if the slot is disabled */
670
671 slot->flags &= (~SLOT_POWEREDON);
672
673 err_exit:
674 return retval;
675}
676
677
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800678
679/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800680 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800681 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800682 */
683static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
684{
685 struct list_head *tmp;
686 unsigned char max, n;
687
688 /*
689 * pci_bus_max_busnr will return the highest
690 * reserved busnr for all these children.
691 * that is equivalent to the bus->subordinate
692 * value. We don't want to use the parent's
693 * bus->subordinate value because it could have
694 * padding in it.
695 */
696 max = bus->secondary;
697
698 list_for_each(tmp, &bus->children) {
699 n = pci_bus_max_busnr(pci_bus_b(tmp));
700 if (n > max)
701 max = n;
702 }
703 return max;
704}
705
706
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800707/**
708 * acpiphp_bus_add - add a new bus to acpi subsystem
709 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800710 */
711static int acpiphp_bus_add(struct acpiphp_func *func)
712{
713 acpi_handle phandle;
714 struct acpi_device *device, *pdevice;
715 int ret_val;
716
717 acpi_get_parent(func->handle, &phandle);
718 if (acpi_bus_get_device(phandle, &pdevice)) {
719 dbg("no parent device, assuming NULL\n");
720 pdevice = NULL;
721 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800722 if (!acpi_bus_get_device(func->handle, &device)) {
723 dbg("bus exists... trim\n");
724 /* this shouldn't be in here, so remove
725 * the bus then re-add it...
726 */
727 ret_val = acpi_bus_trim(device, 1);
728 dbg("acpi_bus_trim return %x\n", ret_val);
729 }
730
731 ret_val = acpi_bus_add(&device, pdevice, func->handle,
732 ACPI_BUS_TYPE_DEVICE);
733 if (ret_val) {
734 dbg("error adding bus, %x\n",
735 -ret_val);
736 goto acpiphp_bus_add_out;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800737 }
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800738 ret_val = acpi_bus_start(device);
739
740acpiphp_bus_add_out:
741 return ret_val;
742}
743
744
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900745/**
746 * acpiphp_bus_trim - trim a bus from acpi subsystem
747 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900748 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700749static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900750{
751 struct acpi_device *device;
752 int retval;
753
754 retval = acpi_bus_get_device(handle, &device);
755 if (retval) {
756 dbg("acpi_device not found\n");
757 return retval;
758 }
759
760 retval = acpi_bus_trim(device, 1);
761 if (retval)
762 err("cannot remove from acpi list\n");
763
764 return retval;
765}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800766
Shaohua Lid0607052010-02-25 10:59:34 +0800767static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
768{
769 struct acpiphp_func *func;
770 union acpi_object params[2];
771 struct acpi_object_list arg_list;
772
773 list_for_each_entry(func, &slot->funcs, sibling) {
774 arg_list.count = 2;
775 arg_list.pointer = params;
776 params[0].type = ACPI_TYPE_INTEGER;
777 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
778 params[1].type = ACPI_TYPE_INTEGER;
779 params[1].integer.value = 1;
780 /* _REG is optional, we don't care about if there is failure */
781 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
782 }
783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785/**
786 * enable_device - enable, configure a slot
787 * @slot: slot to be enabled
788 *
789 * This function should be called per *physical slot*,
790 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100792static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700795 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct acpiphp_func *func;
797 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700798 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900799 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (slot->flags & SLOT_ENABLED)
802 goto err_exit;
803
804 /* sanity check: dev should be NULL when hot-plugged in */
Rajesh Shah42f49a62005-04-28 00:25:53 -0700805 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (dev) {
807 /* This case shouldn't happen */
808 err("pci_dev structure already exists.\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700809 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 retval = -1;
811 goto err_exit;
812 }
813
Rajesh Shah42f49a62005-04-28 00:25:53 -0700814 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
815 if (num == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 err("No new device found\n");
817 retval = -1;
818 goto err_exit;
819 }
820
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800821 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700822 for (pass = 0; pass < 2; pass++) {
823 list_for_each_entry(dev, &bus->devices, bus_list) {
824 if (PCI_SLOT(dev->devfn) != slot->device)
825 continue;
826 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800827 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700828 max = pci_scan_bridge(bus, dev, max, pass);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900829 if (pass && dev->subordinate)
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800830 pci_bus_size_bridges(dev->subordinate);
831 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Alex Chiang58c08622009-10-26 21:25:27 -0600835 list_for_each_entry(func, &slot->funcs, sibling)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900836 acpiphp_bus_add(func);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900837
Rajesh Shah42f49a62005-04-28 00:25:53 -0700838 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700839 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600840 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800841 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700842 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100843
844 list_for_each_entry(dev, &bus->devices, bus_list) {
845 /* Assume that newly added devices are powered on already. */
846 if (!dev->is_added)
847 dev->current_state = PCI_D0;
848 }
849
Rajesh Shah42f49a62005-04-28 00:25:53 -0700850 pci_bus_add_devices(bus);
851
Alex Chiang58c08622009-10-26 21:25:27 -0600852 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600853 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
854 func->function));
855 if (!dev)
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900856 continue;
857
Alex Chiang9d911d72009-05-21 16:21:15 -0600858 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
859 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
860 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900861 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600862 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900863
864 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
865 if (ACPI_FAILURE(status))
866 warn("find_p2p_bridge failed (error code = 0x%x)\n",
867 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600868 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 slot->flags |= SLOT_ENABLED;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 err_exit:
874 return retval;
875}
876
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700877static void disable_bridges(struct pci_bus *bus)
878{
879 struct pci_dev *dev;
880 list_for_each_entry(dev, &bus->devices, bus_list) {
881 if (dev->subordinate) {
882 disable_bridges(dev->subordinate);
883 pci_disable_device(dev);
884 }
885 }
886}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888/**
889 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800890 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
892static int disable_device(struct acpiphp_slot *slot)
893{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600895 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* is this slot already disabled? */
898 if (!(slot->flags & SLOT_ENABLED))
899 goto err_exit;
900
Alex Chiang9d911d72009-05-21 16:21:15 -0600901 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900902 if (func->bridge) {
903 /* cleanup p2p bridges under this P2P bridge */
904 cleanup_p2p_bridge(func->bridge->handle,
905 (u32)1, NULL, NULL);
906 func->bridge = NULL;
907 }
908
Alex Chiang9d911d72009-05-21 16:21:15 -0600909 pdev = pci_get_slot(slot->bridge->pci_bus,
910 PCI_DEVFN(slot->device, func->function));
911 if (pdev) {
912 pci_stop_bus_device(pdev);
913 if (pdev->subordinate) {
914 disable_bridges(pdev->subordinate);
915 pci_disable_device(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700916 }
Alex Chiang9d911d72009-05-21 16:21:15 -0600917 pci_remove_bus_device(pdev);
918 pci_dev_put(pdev);
Satoru Takeuchid5cdb672006-09-12 10:19:00 -0700919 }
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700920 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700921
Alex Chiang9d911d72009-05-21 16:21:15 -0600922 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900923 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925
926 slot->flags &= (~SLOT_ENABLED);
927
Alex Chiang9d911d72009-05-21 16:21:15 -0600928err_exit:
929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932
933/**
934 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800935 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800937 * If a slot has _STA for each function and if any one of them
938 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800940 * If a slot doesn't have _STA and if any one of its functions'
941 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800943 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
945static unsigned int get_slot_status(struct acpiphp_slot *slot)
946{
947 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400948 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct acpiphp_func *func;
951
Alex Chiang58c08622009-10-26 21:25:27 -0600952 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (func->flags & FUNC_HAS_STA) {
954 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
955 if (ACPI_SUCCESS(status) && sta)
956 break;
957 } else {
958 pci_bus_read_config_dword(slot->bridge->pci_bus,
959 PCI_DEVFN(slot->device,
960 func->function),
961 PCI_VENDOR_ID, &dvid);
962 if (dvid != 0xffffffff) {
963 sta = ACPI_STA_ALL;
964 break;
965 }
966 }
967 }
968
969 return (unsigned int)sta;
970}
971
972/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700973 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800974 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700975 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700976int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700977{
978 acpi_status status;
979 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700980 struct acpi_object_list arg_list;
981 union acpi_object arg;
982
Alex Chiang58c08622009-10-26 21:25:27 -0600983 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700984 /* We don't want to call _EJ0 on non-existing functions. */
985 if ((func->flags & FUNC_HAS_EJ0)) {
986 /* _EJ0 method take one argument */
987 arg_list.count = 1;
988 arg_list.pointer = &arg;
989 arg.type = ACPI_TYPE_INTEGER;
990 arg.integer.value = 1;
991
992 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
993 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800994 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -0700995 return -1;
996 } else
997 break;
998 }
999 }
1000 return 0;
1001}
1002
1003/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001005 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 *
1007 * Iterate over all slots under this bridge and make sure that if a
1008 * card is present they are enabled, and if not they are disabled.
1009 */
1010static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1011{
1012 struct acpiphp_slot *slot;
1013 int retval = 0;
1014 int enabled, disabled;
1015
1016 enabled = disabled = 0;
1017
1018 for (slot = bridge->slots; slot; slot = slot->next) {
1019 unsigned int status = get_slot_status(slot);
1020 if (slot->flags & SLOT_ENABLED) {
1021 if (status == ACPI_STA_ALL)
1022 continue;
1023 retval = acpiphp_disable_slot(slot);
1024 if (retval) {
1025 err("Error occurred in disabling\n");
1026 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001027 } else {
1028 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030 disabled++;
1031 } else {
1032 if (status != ACPI_STA_ALL)
1033 continue;
1034 retval = acpiphp_enable_slot(slot);
1035 if (retval) {
1036 err("Error occurred in enabling\n");
1037 goto err_exit;
1038 }
1039 enabled++;
1040 }
1041 }
1042
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001043 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 err_exit:
1046 return retval;
1047}
1048
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001049static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001050{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001051 struct pci_dev *dev;
1052
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001053 list_for_each_entry(dev, &bus->devices, bus_list)
1054 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001055}
1056
1057/*
1058 * Remove devices for which we could not assign resources, call
1059 * arch specific code to fix-up the bus
1060 */
1061static void acpiphp_sanitize_bus(struct pci_bus *bus)
1062{
1063 struct pci_dev *dev;
1064 int i;
1065 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1066
1067 list_for_each_entry(dev, &bus->devices, bus_list) {
1068 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1069 struct resource *res = &dev->resource[i];
1070 if ((res->flags & type_mask) && !res->start &&
1071 res->end) {
1072 /* Could not assign a required resources
1073 * for this device, remove it */
1074 pci_remove_bus_device(dev);
1075 break;
1076 }
1077 }
1078 }
1079}
1080
1081/* Program resources in newly inserted bridge */
1082static int acpiphp_configure_bridge (acpi_handle handle)
1083{
Alex Chiang7f538662009-09-10 12:34:09 -06001084 struct pci_bus *bus;
1085
1086 if (acpi_is_root_bridge(handle)) {
1087 struct acpi_pci_root *root = acpi_pci_find_root(handle);
1088 bus = root->bus;
1089 } else {
1090 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1091 bus = pdev->subordinate;
1092 pci_dev_put(pdev);
1093 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001094
1095 pci_bus_size_bridges(bus);
1096 pci_bus_assign_resources(bus);
1097 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001098 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001099 pci_enable_bridges(bus);
1100 return 0;
1101}
1102
1103static void handle_bridge_insertion(acpi_handle handle, u32 type)
1104{
1105 struct acpi_device *device, *pdevice;
1106 acpi_handle phandle;
1107
1108 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1109 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1110 err("unexpected notification type %d\n", type);
1111 return;
1112 }
1113
1114 acpi_get_parent(handle, &phandle);
1115 if (acpi_bus_get_device(phandle, &pdevice)) {
1116 dbg("no parent device, assuming NULL\n");
1117 pdevice = NULL;
1118 }
1119 if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
1120 err("cannot add bridge to acpi list\n");
1121 return;
1122 }
1123 if (!acpiphp_configure_bridge(handle) &&
1124 !acpi_bus_start(device))
1125 add_bridge(handle);
1126 else
1127 err("cannot configure and start bridge\n");
1128
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/*
1132 * ACPI event handlers
1133 */
1134
Gary Hade0bbd6422007-07-05 11:10:48 -07001135static acpi_status
1136count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1137{
1138 int *count = (int *)context;
1139 struct acpiphp_bridge *bridge;
1140
1141 bridge = acpiphp_handle_to_bridge(handle);
1142 if (bridge)
1143 (*count)++;
1144 return AE_OK ;
1145}
1146
1147static acpi_status
1148check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1149{
1150 struct acpiphp_bridge *bridge;
1151 char objname[64];
1152 struct acpi_buffer buffer = { .length = sizeof(objname),
1153 .pointer = objname };
1154
1155 bridge = acpiphp_handle_to_bridge(handle);
1156 if (bridge) {
1157 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1158 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001159 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001160 acpiphp_check_bridge(bridge);
1161 }
1162 return AE_OK ;
1163}
1164
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001165struct acpiphp_hp_work {
1166 struct work_struct work;
1167 acpi_handle handle;
1168 u32 type;
1169 void *context;
1170};
1171
1172static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type,
1173 void *context,
1174 void (*func)(struct work_struct *work))
1175{
1176 struct acpiphp_hp_work *hp_work;
1177 int ret;
1178
1179 hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL);
1180 if (!hp_work)
1181 return;
1182
1183 hp_work->handle = handle;
1184 hp_work->type = type;
1185 hp_work->context = context;
1186
1187 INIT_WORK(&hp_work->work, func);
1188 ret = queue_work(kacpi_hotplug_wq, &hp_work->work);
1189 if (!ret)
1190 kfree(hp_work);
1191}
1192
1193static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct acpiphp_bridge *bridge;
1196 char objname[64];
1197 struct acpi_buffer buffer = { .length = sizeof(objname),
1198 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001199 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001200 int num_sub_bridges = 0;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001201 struct acpiphp_hp_work *hp_work;
1202 acpi_handle handle;
1203 u32 type;
1204
1205 hp_work = container_of(work, struct acpiphp_hp_work, work);
1206 handle = hp_work->handle;
1207 type = hp_work->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001209 if (acpi_bus_get_device(handle, &device)) {
1210 /* This bridge must have just been physically inserted */
1211 handle_bridge_insertion(handle, type);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001212 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001213 }
1214
1215 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001216 if (type == ACPI_NOTIFY_BUS_CHECK) {
1217 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001218 count_sub_bridges, NULL, &num_sub_bridges, NULL);
Gary Hade0bbd6422007-07-05 11:10:48 -07001219 }
1220
1221 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001222 err("cannot get bridge info\n");
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001223 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1227
1228 switch (type) {
1229 case ACPI_NOTIFY_BUS_CHECK:
1230 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001231 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001232 if (bridge) {
1233 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001234 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001235 acpiphp_check_bridge(bridge);
1236 }
1237 if (num_sub_bridges)
1238 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +08001239 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241
1242 case ACPI_NOTIFY_DEVICE_CHECK:
1243 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001244 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 acpiphp_check_bridge(bridge);
1246 break;
1247
1248 case ACPI_NOTIFY_DEVICE_WAKE:
1249 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001250 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252
1253 case ACPI_NOTIFY_EJECT_REQUEST:
1254 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001255 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001256 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1257 (bridge->flags & BRIDGE_HAS_EJ0)) {
1258 struct acpiphp_slot *slot;
1259 slot = bridge->func->slot;
1260 if (!acpiphp_disable_slot(slot))
1261 acpiphp_eject_slot(slot);
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
1264
1265 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1266 printk(KERN_ERR "Device %s cannot be configured due"
1267 " to a frequency mismatch\n", objname);
1268 break;
1269
1270 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1271 printk(KERN_ERR "Device %s cannot be configured due"
1272 " to a bus mode mismatch\n", objname);
1273 break;
1274
1275 case ACPI_NOTIFY_POWER_FAULT:
1276 printk(KERN_ERR "Device %s has suffered a power fault\n",
1277 objname);
1278 break;
1279
1280 default:
1281 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1282 break;
1283 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001284
1285out:
1286 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001290 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * @handle: Notify()'ed acpi_handle
1292 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001293 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001295 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001297static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1298 void *context)
1299{
1300 /*
1301 * Currently the code adds all hotplug events to the kacpid_wq
1302 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1303 * The proper way to fix this is to reorganize the code so that
1304 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1305 * For now just re-add this work to the kacpi_hotplug_wq so we
1306 * don't deadlock on hotplug actions.
1307 */
1308 alloc_acpiphp_hp_work(handle, type, context,
1309 _handle_hotplug_event_bridge);
1310}
1311
1312static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct acpiphp_func *func;
1315 char objname[64];
1316 struct acpi_buffer buffer = { .length = sizeof(objname),
1317 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001318 struct acpiphp_hp_work *hp_work;
1319 acpi_handle handle;
1320 u32 type;
1321 void *context;
1322
1323 hp_work = container_of(work, struct acpiphp_hp_work, work);
1324 handle = hp_work->handle;
1325 type = hp_work->type;
1326 context = hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1329
1330 func = (struct acpiphp_func *)context;
1331
1332 switch (type) {
1333 case ACPI_NOTIFY_BUS_CHECK:
1334 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001335 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 acpiphp_enable_slot(func->slot);
1337 break;
1338
1339 case ACPI_NOTIFY_DEVICE_CHECK:
1340 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001341 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 acpiphp_check_bridge(func->slot->bridge);
1343 break;
1344
1345 case ACPI_NOTIFY_DEVICE_WAKE:
1346 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001347 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349
1350 case ACPI_NOTIFY_EJECT_REQUEST:
1351 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001352 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001353 if (!(acpiphp_disable_slot(func->slot)))
1354 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
1356
1357 default:
1358 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1359 break;
1360 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001361
1362 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001365/**
1366 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1367 * @handle: Notify()'ed acpi_handle
1368 * @type: Notify code
1369 * @context: pointer to acpiphp_func structure
1370 *
1371 * Handles ACPI event notification on slots.
1372 */
1373static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1374 void *context)
1375{
1376 /*
1377 * Currently the code adds all hotplug events to the kacpid_wq
1378 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1379 * The proper way to fix this is to reorganize the code so that
1380 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1381 * For now just re-add this work to the kacpi_hotplug_wq so we
1382 * don't deadlock on hotplug actions.
1383 */
1384 alloc_acpiphp_hp_work(handle, type, context,
1385 _handle_hotplug_event_func);
1386}
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001387
1388static acpi_status
1389find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1390{
1391 int *count = (int *)context;
1392
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001393 if (!acpi_is_root_bridge(handle))
1394 return AE_OK;
1395
Rafael J. Wysocki0d52f542011-10-22 00:43:38 +02001396 (*count)++;
1397 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1398 handle_hotplug_event_bridge, NULL);
1399
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001400 return AE_OK ;
1401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403static struct acpi_pci_driver acpi_pci_hp_driver = {
1404 .add = add_bridge,
1405 .remove = remove_bridge,
1406};
1407
1408/**
1409 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411int __init acpiphp_glue_init(void)
1412{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001413 int num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001415 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001416 ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 if (num <= 0)
1419 return -1;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001420 else
1421 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 return 0;
1424}
1425
1426
1427/**
1428 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1429 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001430 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001432void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1435}
1436
1437
1438/**
1439 * acpiphp_get_num_slots - count number of slots in a system
1440 */
1441int __init acpiphp_get_num_slots(void)
1442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct acpiphp_bridge *bridge;
Akinobu Mita467c4422006-10-30 13:07:58 -08001444 int num_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alex Chiang58c08622009-10-26 21:25:27 -06001446 list_for_each_entry(bridge, &bridge_list, list) {
Rajesh Shah42f49a62005-04-28 00:25:53 -07001447 dbg("Bus %04x:%02x has %d slot%s\n",
1448 pci_domain_nr(bridge->pci_bus),
1449 bridge->pci_bus->number, bridge->nr_slots,
1450 bridge->nr_slots == 1 ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 num_slots += bridge->nr_slots;
1452 }
1453
Rajesh Shah42f49a62005-04-28 00:25:53 -07001454 dbg("Total %d slots\n", num_slots);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return num_slots;
1456}
1457
1458
1459#if 0
1460/**
1461 * acpiphp_for_each_slot - call function for each slot
1462 * @fn: callback function
1463 * @data: context to be passed to callback function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
1465static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
1466{
1467 struct list_head *node;
1468 struct acpiphp_bridge *bridge;
1469 struct acpiphp_slot *slot;
1470 int retval = 0;
1471
1472 list_for_each (node, &bridge_list) {
1473 bridge = (struct acpiphp_bridge *)node;
1474 for (slot = bridge->slots; slot; slot = slot->next) {
1475 retval = fn(slot, data);
1476 if (!retval)
1477 goto err_exit;
1478 }
1479 }
1480
1481 err_exit:
1482 return retval;
1483}
1484#endif
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487/**
1488 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001489 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491int acpiphp_enable_slot(struct acpiphp_slot *slot)
1492{
1493 int retval;
1494
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001495 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /* wake up all functions */
1498 retval = power_on_slot(slot);
1499 if (retval)
1500 goto err_exit;
1501
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001502 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* configure all functions */
1504 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001505 if (retval)
1506 power_off_slot(slot);
1507 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001508 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001509 power_off_slot(slot);
1510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001513 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return retval;
1515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/**
1518 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001519 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
1521int acpiphp_disable_slot(struct acpiphp_slot *slot)
1522{
1523 int retval = 0;
1524
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001525 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* unconfigure all functions */
1528 retval = disable_device(slot);
1529 if (retval)
1530 goto err_exit;
1531
1532 /* power off all functions */
1533 retval = power_off_slot(slot);
1534 if (retval)
1535 goto err_exit;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001538 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return retval;
1540}
1541
1542
1543/*
1544 * slot enabled: 1
1545 * slot disabled: 0
1546 */
1547u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1548{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001549 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552
1553/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001554 * latch open: 1
1555 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
1557u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1558{
1559 unsigned int sta;
1560
1561 sta = get_slot_status(slot);
1562
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001563 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566
1567/*
1568 * adapter presence : 1
1569 * absence : 0
1570 */
1571u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1572{
1573 unsigned int sta;
1574
1575 sta = get_slot_status(slot);
1576
1577 return (sta == 0) ? 0 : 1;
1578}