blob: 4681d2c9b1dd96ba708d3654e2ecd828fe53e7aa [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
Jiang Liu5ba113f2012-08-22 23:16:45 +0800118/* Check whether the PCI device is managed by native PCIe hotplug driver */
119static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
120{
121 u32 reg32;
122 acpi_handle tmp;
123 struct acpi_pci_root *root;
124
125 /* Check whether the PCIe port supports native PCIe hotplug */
126 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
127 return false;
128 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
129 return false;
130
131 /*
132 * Check whether native PCIe hotplug has been enabled for
133 * this PCIe hierarchy.
134 */
135 tmp = acpi_find_root_bridge_handle(pdev);
136 if (!tmp)
137 return false;
138 root = acpi_pci_find_root(tmp);
139 if (!root)
140 return false;
141 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
142 return false;
143
144 return true;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* callback routine to register each ACPI PCI slot object */
148static acpi_status
149register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
150{
151 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
152 struct acpiphp_slot *slot;
153 struct acpiphp_func *newfunc;
154 acpi_handle tmp;
155 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400156 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800157 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900158 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600159 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900161 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return AE_OK;
163
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600164 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
165 if (ACPI_FAILURE(status)) {
166 warn("can't evaluate _ADR (%#x)\n", status);
167 return AE_OK;
168 }
169
170 device = (adr >> 16) & 0xffff;
171 function = adr & 0xffff;
172
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100173 pdev = pbus->self;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800174 if (pdev && device_is_managed_by_native_pciehp(pdev))
175 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100176
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100177 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!newfunc)
179 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 INIT_LIST_HEAD(&newfunc->sibling);
182 newfunc->handle = handle;
183 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000184
185 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800186 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
189 newfunc->flags |= FUNC_HAS_STA;
190
191 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
192 newfunc->flags |= FUNC_HAS_PS0;
193
194 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
195 newfunc->flags |= FUNC_HAS_PS3;
196
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400197 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800198 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400201 if (ACPI_FAILURE(status)) {
202 /*
203 * use the count of the number of slots we've found
204 * for the number of the slot
205 */
206 sun = bridge->nr_slots+1;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* search for objects that share the same slot */
210 for (slot = bridge->slots; slot; slot = slot->next)
211 if (slot->device == device) {
212 if (slot->sun != sun)
213 warn("sibling found, but _SUN doesn't match!\n");
214 break;
215 }
216
217 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100218 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!slot) {
220 kfree(newfunc);
221 return AE_NO_MEMORY;
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 slot->device = device;
226 slot->sun = sun;
227 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100228 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 slot->next = bridge->slots;
231 bridge->slots = slot;
232
233 bridge->nr_slots++;
234
Justin Chenb6adc192008-12-11 11:16:44 -0700235 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900236 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800237 retval = acpiphp_register_hotplug_slot(slot);
238 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600239 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700240 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600241 "hotplug driver\n", slot->sun);
242 else
243 warn("acpiphp_register_hotplug_slot failed "
244 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800245 goto err_exit;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 newfunc->slot = slot;
250 list_add_tail(&newfunc->sibling, &slot->funcs);
251
Alex Chiang9d911d72009-05-21 16:21:15 -0600252 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
253 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600255 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400258 if (is_dock_device(handle)) {
259 /* we don't want to call this device's _EJ0
260 * because we want the dock notify handler
261 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800262 */
263 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400264 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800265 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400266 dbg("failed to register dock device\n");
267
268 /* we need to be notified when dock events happen
269 * outside of the hotplug operation, since we may
270 * need to do fixups before we can hotplug.
271 */
272 newfunc->nb.notifier_call = post_dock_fixups;
273 if (register_dock_notifier(&newfunc->nb))
274 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800278 if (!(newfunc->flags & FUNC_HAS_DCK)) {
279 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ACPI_SYSTEM_NOTIFY,
281 handle_hotplug_event_func,
282 newfunc);
283
Kristen Accardi20416ea2006-02-23 17:56:03 -0800284 if (ACPI_FAILURE(status))
285 err("failed to register interrupt notify handler\n");
286 } else
287 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kristen Accardi20416ea2006-02-23 17:56:03 -0800289 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800290
291 err_exit:
292 bridge->nr_slots--;
293 bridge->slots = slot->next;
294 kfree(slot);
295 kfree(newfunc);
296
297 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
301/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600302static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Alex Chiang7f538662009-09-10 12:34:09 -0600304 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900305 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600306 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800307 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900308 }
309 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313static void init_bridge_misc(struct acpiphp_bridge *bridge)
314{
315 acpi_status status;
316
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800317 /* must be added to the list prior to calling register_slot */
318 list_add(&bridge->list, &bridge_list);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* register all slot objects under this bridge */
321 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800322 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800323 if (ACPI_FAILURE(status)) {
324 list_del(&bridge->list);
325 return;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Jiang Liube6d2862013-01-31 00:10:10 +0800328 /* install notify handler for P2P bridges */
329 if (!pci_is_root_bus(bridge->pci_bus)) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900330 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
331 status = acpi_remove_notify_handler(bridge->func->handle,
332 ACPI_SYSTEM_NOTIFY,
333 handle_hotplug_event_func);
334 if (ACPI_FAILURE(status))
335 err("failed to remove notify handler\n");
336 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700337 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ACPI_SYSTEM_NOTIFY,
339 handle_hotplug_event_bridge,
340 bridge);
341
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700342 if (ACPI_FAILURE(status)) {
343 err("failed to register interrupt notify handler\n");
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900349/* find acpiphp_func from acpiphp_bridge */
350static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
351{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900352 struct acpiphp_bridge *bridge;
353 struct acpiphp_slot *slot;
354 struct acpiphp_func *func;
355
Alex Chiang58c08622009-10-26 21:25:27 -0600356 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900357 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600358 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900359 if (func->handle == handle)
360 return func;
361 }
362 }
363 }
364
365 return NULL;
366}
367
368
369static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
370{
371 acpi_handle dummy_handle;
Jiang Liube6d2862013-01-31 00:10:10 +0800372 struct acpiphp_func *func;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900373
374 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
Jiang Liube6d2862013-01-31 00:10:10 +0800375 "_EJ0", &dummy_handle))) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900376 bridge->flags |= BRIDGE_HAS_EJ0;
377
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900378 dbg("found ejectable p2p bridge\n");
379
380 /* make link between PCI bridge and PCI function */
381 func = acpiphp_bridge_handle_to_function(bridge->handle);
382 if (!func)
383 return;
384 bridge->func = func;
385 func->bridge = bridge;
386 }
387}
388
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* allocate and initialize host bridge data structure */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900391static void add_host_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900394 acpi_handle handle = root->device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100396 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (bridge == NULL)
398 return;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Alex Chiang6edd7672009-09-10 12:34:04 -0600402 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 init_bridge_misc(bridge);
405}
406
407
408/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600409static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100413 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (bridge == NULL) {
415 err("out of memory\n");
416 return;
417 }
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900420 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Alex Chiang6edd7672009-09-10 12:34:04 -0600422 bridge->pci_dev = acpi_get_pci_dev(handle);
423 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!bridge->pci_bus) {
425 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700426 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600429 /*
430 * Grab a ref to the subordinate PCI bus in case the bus is
431 * removed via PCI core logical hotplug. The ref pins the bus
432 * (which we access during module unload).
433 */
434 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700437 return;
438 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600439 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 kfree(bridge);
441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444
445/* callback routine to find P2P bridges */
446static acpi_status
447find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
448{
449 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alex Chiang6edd7672009-09-10 12:34:04 -0600452 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700453 if (!dev || !dev->subordinate)
454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600457 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600459 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900462 /* search P2P bridges under this p2p bridge */
463 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800464 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900465 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900466 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900467
Rajesh Shah42f49a62005-04-28 00:25:53 -0700468 out:
469 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return AE_OK;
471}
472
473
474/* find hot-pluggable slots, and then find P2P bridge */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900475static int add_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400478 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 acpi_handle dummy_handle;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900480 acpi_handle handle = root->device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* if the bridge doesn't have _STA, we assume it is always there */
483 status = acpi_get_handle(handle, "_STA", &dummy_handle);
484 if (ACPI_SUCCESS(status)) {
485 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
486 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800487 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 0;
489 }
490 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
491 /* don't register this object */
492 return 0;
493 }
494
Rajesh Shah42f49a62005-04-28 00:25:53 -0700495 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600496 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700497 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900498 add_host_bridge(root);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* search P2P bridges under this host bridge */
502 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800503 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900506 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 return 0;
509}
510
Rajesh Shah42f49a62005-04-28 00:25:53 -0700511static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
512{
Alex Chiang58c08622009-10-26 21:25:27 -0600513 struct acpiphp_bridge *bridge;
514
515 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700516 if (bridge->handle == handle)
517 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700518
519 return NULL;
520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Rajesh Shah364d5092005-04-28 00:25:54 -0700522static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Alex Chiang58c08622009-10-26 21:25:27 -0600524 struct acpiphp_slot *slot, *next;
525 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700526 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700527 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700528
Jiang Liube6d2862013-01-31 00:10:10 +0800529 if (!pci_is_root_bus(bridge->pci_bus)) {
Yinghai Lu668192b2013-01-21 13:20:48 -0800530 status = acpi_remove_notify_handler(handle,
531 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700532 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800533 if (ACPI_FAILURE(status))
534 err("failed to remove notify handler\n");
535 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700536
Jiang Liube6d2862013-01-31 00:10:10 +0800537 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900538 status = acpi_install_notify_handler(bridge->func->handle,
539 ACPI_SYSTEM_NOTIFY,
540 handle_hotplug_event_func,
541 bridge->func);
542 if (ACPI_FAILURE(status))
543 err("failed to install interrupt notify handler\n");
544 }
545
Rajesh Shah42f49a62005-04-28 00:25:53 -0700546 slot = bridge->slots;
547 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600548 next = slot->next;
549 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400550 if (is_dock_device(func->handle)) {
551 unregister_hotplug_dock_device(func->handle);
552 unregister_dock_notifier(&func->nb);
553 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800554 if (!(func->flags & FUNC_HAS_DCK)) {
555 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700556 ACPI_SYSTEM_NOTIFY,
557 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800558 if (ACPI_FAILURE(status))
559 err("failed to remove notify handler\n");
560 }
Alex Chiang58c08622009-10-26 21:25:27 -0600561 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700562 kfree(func);
563 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800564 acpiphp_unregister_hotplug_slot(slot);
565 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700566 kfree(slot);
567 slot = next;
568 }
569
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600570 /*
571 * Only P2P bridges have a pci_dev
572 */
573 if (bridge->pci_dev)
574 put_device(&bridge->pci_bus->dev);
575
Rajesh Shah42f49a62005-04-28 00:25:53 -0700576 pci_dev_put(bridge->pci_dev);
577 list_del(&bridge->list);
578 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Rajesh Shah364d5092005-04-28 00:25:54 -0700581static acpi_status
582cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
583{
584 struct acpiphp_bridge *bridge;
585
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900586 /* cleanup p2p bridges under this P2P bridge
587 in a depth-first manner */
588 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800589 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900590
Alex Chianga13307c2008-07-01 20:02:23 -0600591 bridge = acpiphp_handle_to_bridge(handle);
592 if (bridge)
593 cleanup_bridge(bridge);
594
Rajesh Shah364d5092005-04-28 00:25:54 -0700595 return AE_OK;
596}
597
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900598static void remove_bridge(struct acpi_pci_root *root)
Rajesh Shah364d5092005-04-28 00:25:54 -0700599{
600 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900601 acpi_handle handle = root->device->handle;
Rajesh Shah364d5092005-04-28 00:25:54 -0700602
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900603 /* cleanup p2p bridges under this host bridge
604 in a depth-first manner */
605 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800606 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900607
Alex Chianga13307c2008-07-01 20:02:23 -0600608 /*
609 * On root bridges with hotplug slots directly underneath (ie,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300610 * no p2p bridge between), we call cleanup_bridge().
Alex Chianga13307c2008-07-01 20:02:23 -0600611 *
612 * The else clause cleans up root bridges that either had no
613 * hotplug slots at all, or had a p2p bridge underneath.
614 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700615 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900616 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700617 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620static int power_on_slot(struct acpiphp_slot *slot)
621{
622 acpi_status status;
623 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int retval = 0;
625
626 /* if already enabled, just skip */
627 if (slot->flags & SLOT_POWEREDON)
628 goto err_exit;
629
Alex Chiang58c08622009-10-26 21:25:27 -0600630 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800632 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
634 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800635 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 retval = -1;
637 goto err_exit;
638 } else
639 break;
640 }
641 }
642
643 /* TBD: evaluate _STA to check if the slot is enabled */
644
645 slot->flags |= SLOT_POWEREDON;
646
647 err_exit:
648 return retval;
649}
650
651
652static int power_off_slot(struct acpiphp_slot *slot)
653{
654 acpi_status status;
655 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 int retval = 0;
658
659 /* if already disabled, just skip */
660 if ((slot->flags & SLOT_POWEREDON) == 0)
661 goto err_exit;
662
Alex Chiang58c08622009-10-26 21:25:27 -0600663 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700664 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
666 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800667 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 retval = -1;
669 goto err_exit;
670 } else
671 break;
672 }
673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* TBD: evaluate _STA to check if the slot is disabled */
676
677 slot->flags &= (~SLOT_POWEREDON);
678
679 err_exit:
680 return retval;
681}
682
683
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800684
685/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800686 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800687 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800688 */
689static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
690{
691 struct list_head *tmp;
692 unsigned char max, n;
693
694 /*
695 * pci_bus_max_busnr will return the highest
696 * reserved busnr for all these children.
697 * that is equivalent to the bus->subordinate
698 * value. We don't want to use the parent's
699 * bus->subordinate value because it could have
700 * padding in it.
701 */
Yinghai Lub918c622012-05-17 18:51:11 -0700702 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800703
704 list_for_each(tmp, &bus->children) {
705 n = pci_bus_max_busnr(pci_bus_b(tmp));
706 if (n > max)
707 max = n;
708 }
709 return max;
710}
711
712
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800713/**
714 * acpiphp_bus_add - add a new bus to acpi subsystem
715 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800716 */
717static int acpiphp_bus_add(struct acpiphp_func *func)
718{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100719 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800720 int ret_val;
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 */
Rafael J. Wysockiae281792013-01-15 13:23:53 +0100727 ret_val = acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800728 dbg("acpi_bus_trim return %x\n", ret_val);
729 }
730
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100731 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100732 if (!ret_val)
733 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800734
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100735 if (ret_val)
736 dbg("error adding bus, %x\n", -ret_val);
737
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800738 return ret_val;
739}
740
741
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900742/**
743 * acpiphp_bus_trim - trim a bus from acpi subsystem
744 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900745 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700746static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900747{
748 struct acpi_device *device;
749 int retval;
750
751 retval = acpi_bus_get_device(handle, &device);
752 if (retval) {
753 dbg("acpi_device not found\n");
754 return retval;
755 }
756
Rafael J. Wysockiae281792013-01-15 13:23:53 +0100757 retval = acpi_bus_trim(device);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900758 if (retval)
759 err("cannot remove from acpi list\n");
760
761 return retval;
762}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800763
Shaohua Lid0607052010-02-25 10:59:34 +0800764static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
765{
766 struct acpiphp_func *func;
767 union acpi_object params[2];
768 struct acpi_object_list arg_list;
769
770 list_for_each_entry(func, &slot->funcs, sibling) {
771 arg_list.count = 2;
772 arg_list.pointer = params;
773 params[0].type = ACPI_TYPE_INTEGER;
774 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
775 params[1].type = ACPI_TYPE_INTEGER;
776 params[1].integer.value = 1;
777 /* _REG is optional, we don't care about if there is failure */
778 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
779 }
780}
781
Yinghai Lu1f96a962013-01-21 13:20:42 -0800782static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
783{
784 struct acpiphp_func *func;
785
786 if (!dev->subordinate)
787 return;
788
789 /* quirk, or pcie could set it already */
790 if (dev->is_hotplug_bridge)
791 return;
792
793 if (PCI_SLOT(dev->devfn) != slot->device)
794 return;
795
796 list_for_each_entry(func, &slot->funcs, sibling) {
797 if (PCI_FUNC(dev->devfn) == func->function) {
798 /* check if this bridge has ejectable slots */
799 if ((detect_ejectable_slots(func->handle) > 0))
800 dev->is_hotplug_bridge = 1;
801 break;
802 }
803 }
804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/**
806 * enable_device - enable, configure a slot
807 * @slot: slot to be enabled
808 *
809 * This function should be called per *physical slot*,
810 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100812static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700815 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct acpiphp_func *func;
817 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700818 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900819 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (slot->flags & SLOT_ENABLED)
822 goto err_exit;
823
Jiang Liu2ca344e2013-01-31 00:10:09 +0800824 list_for_each_entry(func, &slot->funcs, sibling)
825 acpiphp_bus_add(func);
826
Rajesh Shah42f49a62005-04-28 00:25:53 -0700827 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
828 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800829 /* Maybe only part of funcs are added. */
830 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto err_exit;
832 }
833
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800834 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700835 for (pass = 0; pass < 2; pass++) {
836 list_for_each_entry(dev, &bus->devices, bus_list) {
837 if (PCI_SLOT(dev->devfn) != slot->device)
838 continue;
839 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800840 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700841 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800842 if (pass && dev->subordinate) {
843 check_hotplug_bridge(slot, dev);
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800844 pci_bus_size_bridges(dev->subordinate);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800845 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800846 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Rajesh Shah42f49a62005-04-28 00:25:53 -0700850 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700851 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600852 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800853 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700854 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100855
856 list_for_each_entry(dev, &bus->devices, bus_list) {
857 /* Assume that newly added devices are powered on already. */
858 if (!dev->is_added)
859 dev->current_state = PCI_D0;
860 }
861
Rajesh Shah42f49a62005-04-28 00:25:53 -0700862 pci_bus_add_devices(bus);
863
Amos Kongf382a082011-11-25 15:03:07 +0800864 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600865 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600866 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
867 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800868 if (!dev) {
869 /* Do not set SLOT_ENABLED flag if some funcs
870 are not added. */
871 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900872 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800873 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900874
Alex Chiang9d911d72009-05-21 16:21:15 -0600875 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
876 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
877 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900878 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600879 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900880
881 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
882 if (ACPI_FAILURE(status))
883 warn("find_p2p_bridge failed (error code = 0x%x)\n",
884 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600885 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 err_exit:
890 return retval;
891}
892
Amos Kongce29ca32012-05-23 10:20:35 -0600893/* return first device in slot, acquiring a reference on it */
894static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
895{
896 struct pci_bus *bus = slot->bridge->pci_bus;
897 struct pci_dev *dev;
898 struct pci_dev *ret = NULL;
899
900 down_read(&pci_bus_sem);
901 list_for_each_entry(dev, &bus->devices, bus_list)
902 if (PCI_SLOT(dev->devfn) == slot->device) {
903 ret = pci_dev_get(dev);
904 break;
905 }
906 up_read(&pci_bus_sem);
907
908 return ret;
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/**
912 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800913 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 */
915static int disable_device(struct acpiphp_slot *slot)
916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600918 struct pci_dev *pdev;
Amos Kongf382a082011-11-25 15:03:07 +0800919 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Amos Kongf382a082011-11-25 15:03:07 +0800921 /* The slot will be enabled when func 0 is added, so check
922 func 0 before disable the slot. */
923 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
924 if (!pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto err_exit;
Amos Kong638f2932012-05-22 21:58:40 -0600926 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Alex Chiang9d911d72009-05-21 16:21:15 -0600928 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900929 if (func->bridge) {
930 /* cleanup p2p bridges under this P2P bridge */
931 cleanup_p2p_bridge(func->bridge->handle,
932 (u32)1, NULL, NULL);
933 func->bridge = NULL;
934 }
Amos Kongce29ca32012-05-23 10:20:35 -0600935 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900936
Amos Kongce29ca32012-05-23 10:20:35 -0600937 /*
938 * enable_device() enumerates all functions in this device via
939 * pci_scan_slot(), whether they have associated ACPI hotplug
940 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
941 * here.
942 */
943 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600944 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600945 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700946 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700947
Alex Chiang9d911d72009-05-21 16:21:15 -0600948 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900949 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
952 slot->flags &= (~SLOT_ENABLED);
953
Alex Chiang9d911d72009-05-21 16:21:15 -0600954err_exit:
955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958
959/**
960 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800961 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800963 * If a slot has _STA for each function and if any one of them
964 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800966 * If a slot doesn't have _STA and if any one of its functions'
967 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800969 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
971static unsigned int get_slot_status(struct acpiphp_slot *slot)
972{
973 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400974 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct acpiphp_func *func;
977
Alex Chiang58c08622009-10-26 21:25:27 -0600978 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (func->flags & FUNC_HAS_STA) {
980 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
981 if (ACPI_SUCCESS(status) && sta)
982 break;
983 } else {
984 pci_bus_read_config_dword(slot->bridge->pci_bus,
985 PCI_DEVFN(slot->device,
986 func->function),
987 PCI_VENDOR_ID, &dvid);
988 if (dvid != 0xffffffff) {
989 sta = ACPI_STA_ALL;
990 break;
991 }
992 }
993 }
994
995 return (unsigned int)sta;
996}
997
998/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700999 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001000 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001001 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001002int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001003{
1004 acpi_status status;
1005 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001006 struct acpi_object_list arg_list;
1007 union acpi_object arg;
1008
Alex Chiang58c08622009-10-26 21:25:27 -06001009 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -07001010 /* We don't want to call _EJ0 on non-existing functions. */
1011 if ((func->flags & FUNC_HAS_EJ0)) {
1012 /* _EJ0 method take one argument */
1013 arg_list.count = 1;
1014 arg_list.pointer = &arg;
1015 arg.type = ACPI_TYPE_INTEGER;
1016 arg.integer.value = 1;
1017
1018 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1019 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001020 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001021 return -1;
1022 } else
1023 break;
1024 }
1025 }
1026 return 0;
1027}
1028
1029/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001031 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 *
1033 * Iterate over all slots under this bridge and make sure that if a
1034 * card is present they are enabled, and if not they are disabled.
1035 */
1036static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1037{
1038 struct acpiphp_slot *slot;
1039 int retval = 0;
1040 int enabled, disabled;
1041
1042 enabled = disabled = 0;
1043
1044 for (slot = bridge->slots; slot; slot = slot->next) {
1045 unsigned int status = get_slot_status(slot);
1046 if (slot->flags & SLOT_ENABLED) {
1047 if (status == ACPI_STA_ALL)
1048 continue;
1049 retval = acpiphp_disable_slot(slot);
1050 if (retval) {
1051 err("Error occurred in disabling\n");
1052 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001053 } else {
1054 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 disabled++;
1057 } else {
1058 if (status != ACPI_STA_ALL)
1059 continue;
1060 retval = acpiphp_enable_slot(slot);
1061 if (retval) {
1062 err("Error occurred in enabling\n");
1063 goto err_exit;
1064 }
1065 enabled++;
1066 }
1067 }
1068
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001069 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 err_exit:
1072 return retval;
1073}
1074
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001075static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001076{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001077 struct pci_dev *dev;
1078
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001079 list_for_each_entry(dev, &bus->devices, bus_list)
1080 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001081}
1082
1083/*
1084 * Remove devices for which we could not assign resources, call
1085 * arch specific code to fix-up the bus
1086 */
1087static void acpiphp_sanitize_bus(struct pci_bus *bus)
1088{
1089 struct pci_dev *dev;
1090 int i;
1091 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1092
1093 list_for_each_entry(dev, &bus->devices, bus_list) {
1094 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1095 struct resource *res = &dev->resource[i];
1096 if ((res->flags & type_mask) && !res->start &&
1097 res->end) {
1098 /* Could not assign a required resources
1099 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -08001100 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001101 break;
1102 }
1103 }
1104 }
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/*
1108 * ACPI event handlers
1109 */
1110
Gary Hade0bbd6422007-07-05 11:10:48 -07001111static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -07001112check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1113{
1114 struct acpiphp_bridge *bridge;
1115 char objname[64];
1116 struct acpi_buffer buffer = { .length = sizeof(objname),
1117 .pointer = objname };
1118
1119 bridge = acpiphp_handle_to_bridge(handle);
1120 if (bridge) {
1121 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1122 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001123 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001124 acpiphp_check_bridge(bridge);
1125 }
1126 return AE_OK ;
1127}
1128
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001129static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct acpiphp_bridge *bridge;
1132 char objname[64];
1133 struct acpi_buffer buffer = { .length = sizeof(objname),
1134 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001135 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001136 acpi_handle handle;
1137 u32 type;
1138
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001139 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001140 handle = hp_work->handle;
1141 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001142 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1145
1146 switch (type) {
1147 case ACPI_NOTIFY_BUS_CHECK:
1148 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001149 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001150 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1151 acpiphp_check_bridge(bridge);
1152 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1153 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155
1156 case ACPI_NOTIFY_DEVICE_CHECK:
1157 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001158 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 acpiphp_check_bridge(bridge);
1160 break;
1161
1162 case ACPI_NOTIFY_DEVICE_WAKE:
1163 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001164 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 break;
1166
1167 case ACPI_NOTIFY_EJECT_REQUEST:
1168 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001169 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001170 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001171 struct acpiphp_slot *slot;
1172 slot = bridge->func->slot;
1173 if (!acpiphp_disable_slot(slot))
1174 acpiphp_eject_slot(slot);
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177
1178 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1179 printk(KERN_ERR "Device %s cannot be configured due"
1180 " to a frequency mismatch\n", objname);
1181 break;
1182
1183 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1184 printk(KERN_ERR "Device %s cannot be configured due"
1185 " to a bus mode mismatch\n", objname);
1186 break;
1187
1188 case ACPI_NOTIFY_POWER_FAULT:
1189 printk(KERN_ERR "Device %s has suffered a power fault\n",
1190 objname);
1191 break;
1192
1193 default:
1194 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1195 break;
1196 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001197
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001198 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001202 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 * @handle: Notify()'ed acpi_handle
1204 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001205 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001207 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001209static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1210 void *context)
1211{
1212 /*
1213 * Currently the code adds all hotplug events to the kacpid_wq
1214 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1215 * The proper way to fix this is to reorganize the code so that
1216 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1217 * For now just re-add this work to the kacpi_hotplug_wq so we
1218 * don't deadlock on hotplug actions.
1219 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001220 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001221}
1222
1223static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct acpiphp_func *func;
1226 char objname[64];
1227 struct acpi_buffer buffer = { .length = sizeof(objname),
1228 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001229 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001230 acpi_handle handle;
1231 u32 type;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001232
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001233 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001234 handle = hp_work->handle;
1235 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001236 func = (struct acpiphp_func *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 switch (type) {
1241 case ACPI_NOTIFY_BUS_CHECK:
1242 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001243 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 acpiphp_enable_slot(func->slot);
1245 break;
1246
1247 case ACPI_NOTIFY_DEVICE_CHECK:
1248 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001249 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 acpiphp_check_bridge(func->slot->bridge);
1251 break;
1252
1253 case ACPI_NOTIFY_DEVICE_WAKE:
1254 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001255 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257
1258 case ACPI_NOTIFY_EJECT_REQUEST:
1259 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001260 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001261 if (!(acpiphp_disable_slot(func->slot)))
1262 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
1264
1265 default:
1266 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1267 break;
1268 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001269
1270 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001273/**
1274 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1275 * @handle: Notify()'ed acpi_handle
1276 * @type: Notify code
1277 * @context: pointer to acpiphp_func structure
1278 *
1279 * Handles ACPI event notification on slots.
1280 */
1281static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1282 void *context)
1283{
1284 /*
1285 * Currently the code adds all hotplug events to the kacpid_wq
1286 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1287 * The proper way to fix this is to reorganize the code so that
1288 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1289 * For now just re-add this work to the kacpi_hotplug_wq so we
1290 * don't deadlock on hotplug actions.
1291 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001292 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001293}
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295static struct acpi_pci_driver acpi_pci_hp_driver = {
1296 .add = add_bridge,
1297 .remove = remove_bridge,
1298};
1299
1300/**
1301 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
1303int __init acpiphp_glue_init(void)
1304{
Yinghai Lu668192b2013-01-21 13:20:48 -08001305 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 return 0;
1308}
1309
1310
1311/**
1312 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1313 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001314 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001316void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1319}
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321/**
1322 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001323 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 */
1325int acpiphp_enable_slot(struct acpiphp_slot *slot)
1326{
1327 int retval;
1328
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001329 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* wake up all functions */
1332 retval = power_on_slot(slot);
1333 if (retval)
1334 goto err_exit;
1335
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001336 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* configure all functions */
1338 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001339 if (retval)
1340 power_off_slot(slot);
1341 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001342 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001343 power_off_slot(slot);
1344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001347 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return retval;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/**
1352 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001353 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355int acpiphp_disable_slot(struct acpiphp_slot *slot)
1356{
1357 int retval = 0;
1358
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001359 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 /* unconfigure all functions */
1362 retval = disable_device(slot);
1363 if (retval)
1364 goto err_exit;
1365
1366 /* power off all functions */
1367 retval = power_off_slot(slot);
1368 if (retval)
1369 goto err_exit;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001372 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return retval;
1374}
1375
1376
1377/*
1378 * slot enabled: 1
1379 * slot disabled: 0
1380 */
1381u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1382{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001383 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386
1387/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001388 * latch open: 1
1389 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1392{
1393 unsigned int sta;
1394
1395 sta = get_slot_status(slot);
1396
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001397 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400
1401/*
1402 * adapter presence : 1
1403 * absence : 0
1404 */
1405u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1406{
1407 unsigned int sta;
1408
1409 sta = get_slot_status(slot);
1410
1411 return (sta == 0) ? 0 : 1;
1412}