blob: 718464f8fd40885b1bc06c56eec168c31f71e0cf [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 }
Jiang Liuce15d872013-04-12 05:44:19 +0000490 if ((tmp & ACPI_STA_DEVICE_FUNCTIONING) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* 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. Wysockibfee26d2013-01-26 00:27:44 +0100727 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800728 }
729
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100730 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100731 if (!ret_val)
732 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800733
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100734 if (ret_val)
735 dbg("error adding bus, %x\n", -ret_val);
736
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800737 return ret_val;
738}
739
740
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900741/**
742 * acpiphp_bus_trim - trim a bus from acpi subsystem
743 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900744 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700745static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900746{
747 struct acpi_device *device;
748 int retval;
749
750 retval = acpi_bus_get_device(handle, &device);
751 if (retval) {
752 dbg("acpi_device not found\n");
753 return retval;
754 }
755
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100756 acpi_bus_trim(device);
757 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900758}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800759
Shaohua Lid0607052010-02-25 10:59:34 +0800760static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
761{
762 struct acpiphp_func *func;
763 union acpi_object params[2];
764 struct acpi_object_list arg_list;
765
766 list_for_each_entry(func, &slot->funcs, sibling) {
767 arg_list.count = 2;
768 arg_list.pointer = params;
769 params[0].type = ACPI_TYPE_INTEGER;
770 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
771 params[1].type = ACPI_TYPE_INTEGER;
772 params[1].integer.value = 1;
773 /* _REG is optional, we don't care about if there is failure */
774 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
775 }
776}
777
Yinghai Lu1f96a962013-01-21 13:20:42 -0800778static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
779{
780 struct acpiphp_func *func;
781
782 if (!dev->subordinate)
783 return;
784
785 /* quirk, or pcie could set it already */
786 if (dev->is_hotplug_bridge)
787 return;
788
789 if (PCI_SLOT(dev->devfn) != slot->device)
790 return;
791
792 list_for_each_entry(func, &slot->funcs, sibling) {
793 if (PCI_FUNC(dev->devfn) == func->function) {
794 /* check if this bridge has ejectable slots */
795 if ((detect_ejectable_slots(func->handle) > 0))
796 dev->is_hotplug_bridge = 1;
797 break;
798 }
799 }
800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/**
802 * enable_device - enable, configure a slot
803 * @slot: slot to be enabled
804 *
805 * This function should be called per *physical slot*,
806 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100808static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700811 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct acpiphp_func *func;
813 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700814 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900815 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (slot->flags & SLOT_ENABLED)
818 goto err_exit;
819
Jiang Liu2ca344e2013-01-31 00:10:09 +0800820 list_for_each_entry(func, &slot->funcs, sibling)
821 acpiphp_bus_add(func);
822
Rajesh Shah42f49a62005-04-28 00:25:53 -0700823 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
824 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800825 /* Maybe only part of funcs are added. */
826 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto err_exit;
828 }
829
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800830 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700831 for (pass = 0; pass < 2; pass++) {
832 list_for_each_entry(dev, &bus->devices, bus_list) {
833 if (PCI_SLOT(dev->devfn) != slot->device)
834 continue;
835 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800836 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700837 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800838 if (pass && dev->subordinate) {
839 check_hotplug_bridge(slot, dev);
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800840 pci_bus_size_bridges(dev->subordinate);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800841 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800842 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Rajesh Shah42f49a62005-04-28 00:25:53 -0700846 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700847 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600848 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800849 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700850 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100851
852 list_for_each_entry(dev, &bus->devices, bus_list) {
853 /* Assume that newly added devices are powered on already. */
854 if (!dev->is_added)
855 dev->current_state = PCI_D0;
856 }
857
Rajesh Shah42f49a62005-04-28 00:25:53 -0700858 pci_bus_add_devices(bus);
859
Amos Kongf382a082011-11-25 15:03:07 +0800860 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600861 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600862 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
863 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800864 if (!dev) {
865 /* Do not set SLOT_ENABLED flag if some funcs
866 are not added. */
867 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900868 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800869 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900870
Alex Chiang9d911d72009-05-21 16:21:15 -0600871 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
872 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
873 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900874 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600875 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900876
877 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
878 if (ACPI_FAILURE(status))
879 warn("find_p2p_bridge failed (error code = 0x%x)\n",
880 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600881 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 err_exit:
886 return retval;
887}
888
Amos Kongce29ca32012-05-23 10:20:35 -0600889/* return first device in slot, acquiring a reference on it */
890static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
891{
892 struct pci_bus *bus = slot->bridge->pci_bus;
893 struct pci_dev *dev;
894 struct pci_dev *ret = NULL;
895
896 down_read(&pci_bus_sem);
897 list_for_each_entry(dev, &bus->devices, bus_list)
898 if (PCI_SLOT(dev->devfn) == slot->device) {
899 ret = pci_dev_get(dev);
900 break;
901 }
902 up_read(&pci_bus_sem);
903
904 return ret;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907/**
908 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800909 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
911static int disable_device(struct acpiphp_slot *slot)
912{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600914 struct pci_dev *pdev;
Amos Kongf382a082011-11-25 15:03:07 +0800915 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Alex Chiang9d911d72009-05-21 16:21:15 -0600917 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900918 if (func->bridge) {
919 /* cleanup p2p bridges under this P2P bridge */
920 cleanup_p2p_bridge(func->bridge->handle,
921 (u32)1, NULL, NULL);
922 func->bridge = NULL;
923 }
Amos Kongce29ca32012-05-23 10:20:35 -0600924 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900925
Amos Kongce29ca32012-05-23 10:20:35 -0600926 /*
927 * enable_device() enumerates all functions in this device via
928 * pci_scan_slot(), whether they have associated ACPI hotplug
929 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
930 * here.
931 */
932 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600933 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600934 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700935 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700936
Alex Chiang9d911d72009-05-21 16:21:15 -0600937 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900938 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 slot->flags &= (~SLOT_ENABLED);
942
Alex Chiang9d911d72009-05-21 16:21:15 -0600943err_exit:
944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947
948/**
949 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800950 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800952 * If a slot has _STA for each function and if any one of them
953 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800955 * If a slot doesn't have _STA and if any one of its functions'
956 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800958 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 */
960static unsigned int get_slot_status(struct acpiphp_slot *slot)
961{
962 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400963 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct acpiphp_func *func;
966
Alex Chiang58c08622009-10-26 21:25:27 -0600967 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (func->flags & FUNC_HAS_STA) {
969 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
970 if (ACPI_SUCCESS(status) && sta)
971 break;
972 } else {
973 pci_bus_read_config_dword(slot->bridge->pci_bus,
974 PCI_DEVFN(slot->device,
975 func->function),
976 PCI_VENDOR_ID, &dvid);
977 if (dvid != 0xffffffff) {
978 sta = ACPI_STA_ALL;
979 break;
980 }
981 }
982 }
983
984 return (unsigned int)sta;
985}
986
987/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700988 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800989 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700990 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700991int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700992{
993 acpi_status status;
994 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700995 struct acpi_object_list arg_list;
996 union acpi_object arg;
997
Alex Chiang58c08622009-10-26 21:25:27 -0600998 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700999 /* We don't want to call _EJ0 on non-existing functions. */
1000 if ((func->flags & FUNC_HAS_EJ0)) {
1001 /* _EJ0 method take one argument */
1002 arg_list.count = 1;
1003 arg_list.pointer = &arg;
1004 arg.type = ACPI_TYPE_INTEGER;
1005 arg.integer.value = 1;
1006
1007 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1008 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001009 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001010 return -1;
1011 } else
1012 break;
1013 }
1014 }
1015 return 0;
1016}
1017
1018/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001020 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 *
1022 * Iterate over all slots under this bridge and make sure that if a
1023 * card is present they are enabled, and if not they are disabled.
1024 */
1025static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1026{
1027 struct acpiphp_slot *slot;
1028 int retval = 0;
1029 int enabled, disabled;
1030
1031 enabled = disabled = 0;
1032
1033 for (slot = bridge->slots; slot; slot = slot->next) {
1034 unsigned int status = get_slot_status(slot);
1035 if (slot->flags & SLOT_ENABLED) {
1036 if (status == ACPI_STA_ALL)
1037 continue;
1038 retval = acpiphp_disable_slot(slot);
1039 if (retval) {
1040 err("Error occurred in disabling\n");
1041 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001042 } else {
1043 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 disabled++;
1046 } else {
1047 if (status != ACPI_STA_ALL)
1048 continue;
1049 retval = acpiphp_enable_slot(slot);
1050 if (retval) {
1051 err("Error occurred in enabling\n");
1052 goto err_exit;
1053 }
1054 enabled++;
1055 }
1056 }
1057
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001058 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 err_exit:
1061 return retval;
1062}
1063
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001064static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001065{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001066 struct pci_dev *dev;
1067
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001068 list_for_each_entry(dev, &bus->devices, bus_list)
1069 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001070}
1071
1072/*
1073 * Remove devices for which we could not assign resources, call
1074 * arch specific code to fix-up the bus
1075 */
1076static void acpiphp_sanitize_bus(struct pci_bus *bus)
1077{
Yijing Wangd65eba62013-04-12 05:44:17 +00001078 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001079 int i;
1080 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1081
Yijing Wangd65eba62013-04-12 05:44:17 +00001082 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001083 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1084 struct resource *res = &dev->resource[i];
1085 if ((res->flags & type_mask) && !res->start &&
1086 res->end) {
1087 /* Could not assign a required resources
1088 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -08001089 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001090 break;
1091 }
1092 }
1093 }
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/*
1097 * ACPI event handlers
1098 */
1099
Gary Hade0bbd6422007-07-05 11:10:48 -07001100static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -07001101check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1102{
1103 struct acpiphp_bridge *bridge;
1104 char objname[64];
1105 struct acpi_buffer buffer = { .length = sizeof(objname),
1106 .pointer = objname };
1107
1108 bridge = acpiphp_handle_to_bridge(handle);
1109 if (bridge) {
1110 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1111 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001112 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001113 acpiphp_check_bridge(bridge);
1114 }
1115 return AE_OK ;
1116}
1117
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001118static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct acpiphp_bridge *bridge;
1121 char objname[64];
1122 struct acpi_buffer buffer = { .length = sizeof(objname),
1123 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001124 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001125 acpi_handle handle;
1126 u32 type;
1127
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001128 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001129 handle = hp_work->handle;
1130 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001131 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001133 acpi_scan_lock_acquire();
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1136
1137 switch (type) {
1138 case ACPI_NOTIFY_BUS_CHECK:
1139 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001140 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001141 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1142 acpiphp_check_bridge(bridge);
1143 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1144 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146
1147 case ACPI_NOTIFY_DEVICE_CHECK:
1148 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001149 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 acpiphp_check_bridge(bridge);
1151 break;
1152
1153 case ACPI_NOTIFY_DEVICE_WAKE:
1154 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001155 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157
1158 case ACPI_NOTIFY_EJECT_REQUEST:
1159 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001160 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001161 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001162 struct acpiphp_slot *slot;
1163 slot = bridge->func->slot;
1164 if (!acpiphp_disable_slot(slot))
1165 acpiphp_eject_slot(slot);
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168
1169 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1170 printk(KERN_ERR "Device %s cannot be configured due"
1171 " to a frequency mismatch\n", objname);
1172 break;
1173
1174 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1175 printk(KERN_ERR "Device %s cannot be configured due"
1176 " to a bus mode mismatch\n", objname);
1177 break;
1178
1179 case ACPI_NOTIFY_POWER_FAULT:
1180 printk(KERN_ERR "Device %s has suffered a power fault\n",
1181 objname);
1182 break;
1183
1184 default:
1185 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1186 break;
1187 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001188
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001189 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001190 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001194 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * @handle: Notify()'ed acpi_handle
1196 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001197 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001199 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001201static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1202 void *context)
1203{
1204 /*
1205 * Currently the code adds all hotplug events to the kacpid_wq
1206 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1207 * The proper way to fix this is to reorganize the code so that
1208 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1209 * For now just re-add this work to the kacpi_hotplug_wq so we
1210 * don't deadlock on hotplug actions.
1211 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001212 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001213}
1214
1215static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct acpiphp_func *func;
1218 char objname[64];
1219 struct acpi_buffer buffer = { .length = sizeof(objname),
1220 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001221 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001222 acpi_handle handle;
1223 u32 type;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001224
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001225 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001226 handle = hp_work->handle;
1227 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001228 func = (struct acpiphp_func *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001230 acpi_scan_lock_acquire();
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 switch (type) {
1235 case ACPI_NOTIFY_BUS_CHECK:
1236 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001237 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 acpiphp_enable_slot(func->slot);
1239 break;
1240
1241 case ACPI_NOTIFY_DEVICE_CHECK:
1242 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001243 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 acpiphp_check_bridge(func->slot->bridge);
1245 break;
1246
1247 case ACPI_NOTIFY_DEVICE_WAKE:
1248 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001249 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251
1252 case ACPI_NOTIFY_EJECT_REQUEST:
1253 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001254 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001255 if (!(acpiphp_disable_slot(func->slot)))
1256 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
1258
1259 default:
1260 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1261 break;
1262 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001263
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001264 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001265 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001268/**
1269 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1270 * @handle: Notify()'ed acpi_handle
1271 * @type: Notify code
1272 * @context: pointer to acpiphp_func structure
1273 *
1274 * Handles ACPI event notification on slots.
1275 */
1276static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1277 void *context)
1278{
1279 /*
1280 * Currently the code adds all hotplug events to the kacpid_wq
1281 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1282 * The proper way to fix this is to reorganize the code so that
1283 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1284 * For now just re-add this work to the kacpi_hotplug_wq so we
1285 * don't deadlock on hotplug actions.
1286 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001287 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290static struct acpi_pci_driver acpi_pci_hp_driver = {
1291 .add = add_bridge,
1292 .remove = remove_bridge,
1293};
1294
1295/**
1296 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
1298int __init acpiphp_glue_init(void)
1299{
Yinghai Lu668192b2013-01-21 13:20:48 -08001300 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 return 0;
1303}
1304
1305
1306/**
1307 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1308 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001309 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001311void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/**
1317 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001318 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
1320int acpiphp_enable_slot(struct acpiphp_slot *slot)
1321{
1322 int retval;
1323
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001324 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* wake up all functions */
1327 retval = power_on_slot(slot);
1328 if (retval)
1329 goto err_exit;
1330
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001331 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* configure all functions */
1333 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001334 if (retval)
1335 power_off_slot(slot);
1336 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001337 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001338 power_off_slot(slot);
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001342 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return retval;
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/**
1347 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001348 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
1350int acpiphp_disable_slot(struct acpiphp_slot *slot)
1351{
1352 int retval = 0;
1353
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001354 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* unconfigure all functions */
1357 retval = disable_device(slot);
1358 if (retval)
1359 goto err_exit;
1360
1361 /* power off all functions */
1362 retval = power_off_slot(slot);
1363 if (retval)
1364 goto err_exit;
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001367 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return retval;
1369}
1370
1371
1372/*
1373 * slot enabled: 1
1374 * slot disabled: 0
1375 */
1376u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1377{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001378 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
1381
1382/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001383 * latch open: 1
1384 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 */
1386u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1387{
1388 unsigned int sta;
1389
1390 sta = get_slot_status(slot);
1391
Jiang Liuce15d872013-04-12 05:44:19 +00001392 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395
1396/*
1397 * adapter presence : 1
1398 * absence : 0
1399 */
1400u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1401{
1402 unsigned int sta;
1403
1404 sta = get_slot_status(slot);
1405
1406 return (sta == 0) ? 0 : 1;
1407}