blob: b306e993ad0854e26e6bee5e81395e26769f4a57 [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);
Jiang Liu3d54a312013-04-12 05:44:28 +000057static DEFINE_MUTEX(bridge_mutex);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +020058static DEFINE_MUTEX(acpiphp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define MY_NAME "acpiphp_glue"
61
Rafael J. Wysocki87831272013-07-13 23:27:24 +020062static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070063static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060064static void acpiphp_set_hpp_values(struct pci_bus *bus);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +020065static void hotplug_event(acpi_handle handle, u32 type, void *data);
Jiang Liu3d54a312013-04-12 05:44:28 +000066static void free_bridge(struct kref *kref);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070067
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +020068static void acpiphp_context_handler(acpi_handle handle, void *context)
69{
70 /* Intentionally empty. */
71}
72
73/**
74 * acpiphp_init_context - Create hotplug context and grab a reference to it.
75 * @handle: ACPI object handle to create the context for.
76 *
77 * Call under acpiphp_context_lock.
78 */
79static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
80{
81 struct acpiphp_context *context;
82 acpi_status status;
83
84 context = kzalloc(sizeof(*context), GFP_KERNEL);
85 if (!context)
86 return NULL;
87
88 context->handle = handle;
89 context->refcount = 1;
90 status = acpi_attach_data(handle, acpiphp_context_handler, context);
91 if (ACPI_FAILURE(status)) {
92 kfree(context);
93 return NULL;
94 }
95 return context;
96}
97
98/**
99 * acpiphp_get_context - Get hotplug context and grab a reference to it.
100 * @handle: ACPI object handle to get the context for.
101 *
102 * Call under acpiphp_context_lock.
103 */
104static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
105{
106 struct acpiphp_context *context = NULL;
107 acpi_status status;
108 void *data;
109
110 status = acpi_get_data(handle, acpiphp_context_handler, &data);
111 if (ACPI_SUCCESS(status)) {
112 context = data;
113 context->refcount++;
114 }
115 return context;
116}
117
118/**
119 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
120 * @handle: ACPI object handle to put the context for.
121 *
122 * The context object is removed if there are no more references to it.
123 *
124 * Call under acpiphp_context_lock.
125 */
126static void acpiphp_put_context(struct acpiphp_context *context)
127{
128 if (--context->refcount)
129 return;
130
131 WARN_ON(context->func || context->bridge);
132 acpi_detach_data(context->handle, acpiphp_context_handler);
133 kfree(context);
134}
135
Jiang Liu3d54a312013-04-12 05:44:28 +0000136static inline void get_bridge(struct acpiphp_bridge *bridge)
137{
138 kref_get(&bridge->ref);
139}
140
141static inline void put_bridge(struct acpiphp_bridge *bridge)
142{
143 kref_put(&bridge->ref, free_bridge);
144}
145
146static void free_bridge(struct kref *kref)
147{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200148 struct acpiphp_context *context;
Jiang Liu3d54a312013-04-12 05:44:28 +0000149 struct acpiphp_bridge *bridge;
150 struct acpiphp_slot *slot, *next;
151 struct acpiphp_func *func, *tmp;
152
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200153 mutex_lock(&acpiphp_context_lock);
154
Jiang Liu3d54a312013-04-12 05:44:28 +0000155 bridge = container_of(kref, struct acpiphp_bridge, ref);
156
157 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
158 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200159 context = func->context;
160 context->func = NULL;
161 acpiphp_put_context(context);
Jiang Liu3d54a312013-04-12 05:44:28 +0000162 kfree(func);
163 }
164 kfree(slot);
165 }
166
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200167 context = bridge->context;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200168 /* Root bridges will not have hotplug context. */
169 if (context) {
170 /* Release the reference taken by acpiphp_enumerate_slots(). */
Rafael J. Wysockif2818112013-07-13 23:27:24 +0200171 put_bridge(context->func->slot->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200172 context->bridge = NULL;
173 acpiphp_put_context(context);
174 }
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200175
Jiang Liu3d54a312013-04-12 05:44:28 +0000176 put_device(&bridge->pci_bus->dev);
177 pci_dev_put(bridge->pci_dev);
178 kfree(bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200179
180 mutex_unlock(&acpiphp_context_lock);
Jiang Liu3d54a312013-04-12 05:44:28 +0000181}
182
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400183/*
184 * the _DCK method can do funny things... and sometimes not
185 * hah-hah funny.
186 *
187 * TBD - figure out a way to only call fixups for
188 * systems that require them.
189 */
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200190static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400191{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200192 struct acpiphp_context *context = data;
193 struct pci_bus *bus = context->func->slot->bridge->pci_bus;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400194 u32 buses;
195
196 if (!bus->self)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200197 return;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400198
199 /* fixup bad _DCK function that rewrites
200 * secondary bridge on slot
201 */
202 pci_read_config_dword(bus->self,
203 PCI_PRIMARY_BUS,
204 &buses);
205
Yinghai Lub918c622012-05-17 18:51:11 -0700206 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400207 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700208 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700209 | ((unsigned int)(bus->busn_res.start) << 8)
210 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400211 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
212 }
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400213}
214
215
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400216static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200217 .fixup = post_dock_fixups,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200218 .handler = hotplug_event,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800219};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jiang Liu5ba113f2012-08-22 23:16:45 +0800221/* Check whether the PCI device is managed by native PCIe hotplug driver */
222static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
223{
224 u32 reg32;
225 acpi_handle tmp;
226 struct acpi_pci_root *root;
227
228 /* Check whether the PCIe port supports native PCIe hotplug */
229 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
230 return false;
231 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
232 return false;
233
234 /*
235 * Check whether native PCIe hotplug has been enabled for
236 * this PCIe hierarchy.
237 */
238 tmp = acpi_find_root_bridge_handle(pdev);
239 if (!tmp)
240 return false;
241 root = acpi_pci_find_root(tmp);
242 if (!root)
243 return false;
244 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
245 return false;
246
247 return true;
248}
249
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200250static void acpiphp_dock_init(void *data)
251{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200252 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200253
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200254 get_bridge(context->func->slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200255}
256
257static void acpiphp_dock_release(void *data)
258{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200259 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200260
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200261 put_bridge(context->func->slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/* callback routine to register each ACPI PCI slot object */
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200265static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
266 void **rv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200268 struct acpiphp_bridge *bridge = data;
269 struct acpiphp_context *context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct acpiphp_slot *slot;
271 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 acpi_status status = AE_OK;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200273 unsigned long long adr;
274 int device, function;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900275 struct pci_bus *pbus = bridge->pci_bus;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200276 struct pci_dev *pdev = bridge->pci_dev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000277 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200279 if (pdev && device_is_managed_by_native_pciehp(pdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return AE_OK;
281
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600282 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
283 if (ACPI_FAILURE(status)) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200284 acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600285 return AE_OK;
286 }
287
288 device = (adr >> 16) & 0xffff;
289 function = adr & 0xffff;
290
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100291 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!newfunc)
293 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 newfunc->handle = handle;
296 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000297
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200298 mutex_lock(&acpiphp_context_lock);
299 context = acpiphp_init_context(handle);
300 if (!context) {
301 mutex_unlock(&acpiphp_context_lock);
302 acpi_handle_err(handle, "No hotplug context\n");
303 kfree(newfunc);
304 return AE_NOT_EXIST;
305 }
306 newfunc->context = context;
307 context->func = newfunc;
308 mutex_unlock(&acpiphp_context_lock);
309
Jiang Liuecd046d2013-06-29 00:24:43 +0800310 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800311 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Jiang Liuecd046d2013-06-29 00:24:43 +0800313 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 newfunc->flags |= FUNC_HAS_STA;
315
Jiang Liuecd046d2013-06-29 00:24:43 +0800316 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 newfunc->flags |= FUNC_HAS_PS0;
318
Jiang Liuecd046d2013-06-29 00:24:43 +0800319 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 newfunc->flags |= FUNC_HAS_PS3;
321
Jiang Liuecd046d2013-06-29 00:24:43 +0800322 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800323 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000326 list_for_each_entry(slot, &bridge->slots, node)
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200327 if (slot->device == device)
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200328 goto slot_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200330 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
331 if (!slot) {
332 status = AE_NO_MEMORY;
333 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200336 slot->bridge = bridge;
337 slot->device = device;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200338 INIT_LIST_HEAD(&slot->funcs);
339 mutex_init(&slot->crit_sect);
340
341 mutex_lock(&bridge_mutex);
342 list_add_tail(&slot->node, &bridge->slots);
343 mutex_unlock(&bridge_mutex);
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200344
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200345 /* Register slots for ejectable funtions only. */
346 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
347 unsigned long long sun;
348 int retval;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200349
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200350 bridge->nr_slots++;
351 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
352 if (ACPI_FAILURE(status))
353 sun = bridge->nr_slots;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200354
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200355 slot->sun = sun;
356 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
357 slot->sun, pci_domain_nr(pbus), pbus->number, device);
358
359 retval = acpiphp_register_hotplug_slot(slot);
360 if (retval) {
361 bridge->nr_slots--;
362 if (retval == -EBUSY)
363 warn("Slot %llu already registered by another "
364 "hotplug driver\n", slot->sun);
365 else
366 warn("acpiphp_register_hotplug_slot failed "
367 "(err code = 0x%x)\n", retval);
368 }
369 /* Even if the slot registration fails, we can still use it. */
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200370 }
371
372 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000374 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000376 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000378 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
379 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400382 if (is_dock_device(handle)) {
383 /* we don't want to call this device's _EJ0
384 * because we want the dock notify handler
385 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800386 */
387 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400388 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200389 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200390 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400391 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800395 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200396 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event,
398 context);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200399 if (ACPI_FAILURE(status))
400 acpi_handle_err(handle,
401 "failed to install notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200404 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800405
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200406 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200407 mutex_lock(&acpiphp_context_lock);
408 context->func = NULL;
409 acpiphp_put_context(context);
410 mutex_unlock(&acpiphp_context_lock);
411 kfree(newfunc);
412 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Rajesh Shah42f49a62005-04-28 00:25:53 -0700415static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
416{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200417 struct acpiphp_context *context;
418 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600419
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200420 mutex_lock(&acpiphp_context_lock);
421 context = acpiphp_get_context(handle);
422 if (context) {
423 bridge = context->bridge;
424 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000425 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700426
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200427 acpiphp_put_context(context);
428 }
429 mutex_unlock(&acpiphp_context_lock);
430 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700431}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Rajesh Shah364d5092005-04-28 00:25:54 -0700433static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jiang Liu3d54a312013-04-12 05:44:28 +0000435 struct acpiphp_slot *slot;
436 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700437 acpi_status status;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700438
Jiang Liu3d54a312013-04-12 05:44:28 +0000439 list_for_each_entry(slot, &bridge->slots, node) {
440 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400441 if (is_dock_device(func->handle)) {
442 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400443 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800444 if (!(func->flags & FUNC_HAS_DCK)) {
445 status = acpi_remove_notify_handler(func->handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200446 ACPI_SYSTEM_NOTIFY,
447 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800448 if (ACPI_FAILURE(status))
449 err("failed to remove notify handler\n");
450 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700451 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800452 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700453 }
454
Jiang Liu3d54a312013-04-12 05:44:28 +0000455 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700456 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000457 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static int power_on_slot(struct acpiphp_slot *slot)
461{
462 acpi_status status;
463 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int retval = 0;
465
466 /* if already enabled, just skip */
467 if (slot->flags & SLOT_POWEREDON)
468 goto err_exit;
469
Alex Chiang58c08622009-10-26 21:25:27 -0600470 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800472 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
474 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800475 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 retval = -1;
477 goto err_exit;
478 } else
479 break;
480 }
481 }
482
483 /* TBD: evaluate _STA to check if the slot is enabled */
484
485 slot->flags |= SLOT_POWEREDON;
486
487 err_exit:
488 return retval;
489}
490
491
492static int power_off_slot(struct acpiphp_slot *slot)
493{
494 acpi_status status;
495 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 int retval = 0;
498
499 /* if already disabled, just skip */
500 if ((slot->flags & SLOT_POWEREDON) == 0)
501 goto err_exit;
502
Alex Chiang58c08622009-10-26 21:25:27 -0600503 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700504 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
506 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800507 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 retval = -1;
509 goto err_exit;
510 } else
511 break;
512 }
513 }
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* TBD: evaluate _STA to check if the slot is disabled */
516
517 slot->flags &= (~SLOT_POWEREDON);
518
519 err_exit:
520 return retval;
521}
522
523
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800524
525/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800526 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800527 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800528 */
529static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
530{
531 struct list_head *tmp;
532 unsigned char max, n;
533
534 /*
535 * pci_bus_max_busnr will return the highest
536 * reserved busnr for all these children.
537 * that is equivalent to the bus->subordinate
538 * value. We don't want to use the parent's
539 * bus->subordinate value because it could have
540 * padding in it.
541 */
Yinghai Lub918c622012-05-17 18:51:11 -0700542 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800543
544 list_for_each(tmp, &bus->children) {
545 n = pci_bus_max_busnr(pci_bus_b(tmp));
546 if (n > max)
547 max = n;
548 }
549 return max;
550}
551
552
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800553/**
554 * acpiphp_bus_add - add a new bus to acpi subsystem
555 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800556 */
557static int acpiphp_bus_add(struct acpiphp_func *func)
558{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100559 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800560 int ret_val;
561
Kristen Accardi20416ea2006-02-23 17:56:03 -0800562 if (!acpi_bus_get_device(func->handle, &device)) {
563 dbg("bus exists... trim\n");
564 /* this shouldn't be in here, so remove
565 * the bus then re-add it...
566 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100567 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800568 }
569
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100570 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100571 if (!ret_val)
572 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800573
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100574 if (ret_val)
575 dbg("error adding bus, %x\n", -ret_val);
576
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800577 return ret_val;
578}
579
580
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900581/**
582 * acpiphp_bus_trim - trim a bus from acpi subsystem
583 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900584 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700585static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900586{
587 struct acpi_device *device;
588 int retval;
589
590 retval = acpi_bus_get_device(handle, &device);
591 if (retval) {
592 dbg("acpi_device not found\n");
593 return retval;
594 }
595
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100596 acpi_bus_trim(device);
597 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900598}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800599
Shaohua Lid0607052010-02-25 10:59:34 +0800600static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
601{
602 struct acpiphp_func *func;
603 union acpi_object params[2];
604 struct acpi_object_list arg_list;
605
606 list_for_each_entry(func, &slot->funcs, sibling) {
607 arg_list.count = 2;
608 arg_list.pointer = params;
609 params[0].type = ACPI_TYPE_INTEGER;
610 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
611 params[1].type = ACPI_TYPE_INTEGER;
612 params[1].integer.value = 1;
613 /* _REG is optional, we don't care about if there is failure */
614 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
615 }
616}
617
Yinghai Lu1f96a962013-01-21 13:20:42 -0800618static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
619{
620 struct acpiphp_func *func;
621
622 if (!dev->subordinate)
623 return;
624
625 /* quirk, or pcie could set it already */
626 if (dev->is_hotplug_bridge)
627 return;
628
629 if (PCI_SLOT(dev->devfn) != slot->device)
630 return;
631
632 list_for_each_entry(func, &slot->funcs, sibling) {
633 if (PCI_FUNC(dev->devfn) == func->function) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200634 dev->is_hotplug_bridge = 1;
Yinghai Lu1f96a962013-01-21 13:20:42 -0800635 break;
636 }
637 }
638}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/**
641 * enable_device - enable, configure a slot
642 * @slot: slot to be enabled
643 *
644 * This function should be called per *physical slot*,
645 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100647static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700650 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700652 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200653 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (slot->flags & SLOT_ENABLED)
656 goto err_exit;
657
Jiang Liu2ca344e2013-01-31 00:10:09 +0800658 list_for_each_entry(func, &slot->funcs, sibling)
659 acpiphp_bus_add(func);
660
Rajesh Shah42f49a62005-04-28 00:25:53 -0700661 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
662 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800663 /* Maybe only part of funcs are added. */
664 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto err_exit;
666 }
667
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800668 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700669 for (pass = 0; pass < 2; pass++) {
670 list_for_each_entry(dev, &bus->devices, bus_list) {
671 if (PCI_SLOT(dev->devfn) != slot->device)
672 continue;
673 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800674 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700675 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800676 if (pass && dev->subordinate) {
677 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200678 pcibios_resource_survey_bus(dev->subordinate);
679 __pci_bus_size_bridges(dev->subordinate,
680 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800681 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800682 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Jiang Liud66ecb72013-06-23 01:01:35 +0200686 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700687 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600688 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800689 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700690 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100691
692 list_for_each_entry(dev, &bus->devices, bus_list) {
693 /* Assume that newly added devices are powered on already. */
694 if (!dev->is_added)
695 dev->current_state = PCI_D0;
696 }
697
Rajesh Shah42f49a62005-04-28 00:25:53 -0700698 pci_bus_add_devices(bus);
699
Amos Kongf382a082011-11-25 15:03:07 +0800700 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600701 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600702 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
703 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800704 if (!dev) {
705 /* Do not set SLOT_ENABLED flag if some funcs
706 are not added. */
707 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900708 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Amos Kongce29ca32012-05-23 10:20:35 -0600717/* return first device in slot, acquiring a reference on it */
718static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
719{
720 struct pci_bus *bus = slot->bridge->pci_bus;
721 struct pci_dev *dev;
722 struct pci_dev *ret = NULL;
723
724 down_read(&pci_bus_sem);
725 list_for_each_entry(dev, &bus->devices, bus_list)
726 if (PCI_SLOT(dev->devfn) == slot->device) {
727 ret = pci_dev_get(dev);
728 break;
729 }
730 up_read(&pci_bus_sem);
731
732 return ret;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/**
736 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800737 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
739static int disable_device(struct acpiphp_slot *slot)
740{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600742 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900743
Amos Kongce29ca32012-05-23 10:20:35 -0600744 /*
745 * enable_device() enumerates all functions in this device via
746 * pci_scan_slot(), whether they have associated ACPI hotplug
747 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
748 * here.
749 */
750 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600751 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600752 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700753 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700754
Alex Chiang9d911d72009-05-21 16:21:15 -0600755 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900756 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
759 slot->flags &= (~SLOT_ENABLED);
760
Alex Chiang9d911d72009-05-21 16:21:15 -0600761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764
765/**
766 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800767 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800769 * If a slot has _STA for each function and if any one of them
770 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800772 * If a slot doesn't have _STA and if any one of its functions'
773 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800775 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777static unsigned int get_slot_status(struct acpiphp_slot *slot)
778{
779 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400780 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct acpiphp_func *func;
783
Alex Chiang58c08622009-10-26 21:25:27 -0600784 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (func->flags & FUNC_HAS_STA) {
786 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
787 if (ACPI_SUCCESS(status) && sta)
788 break;
789 } else {
790 pci_bus_read_config_dword(slot->bridge->pci_bus,
791 PCI_DEVFN(slot->device,
792 func->function),
793 PCI_VENDOR_ID, &dvid);
794 if (dvid != 0xffffffff) {
795 sta = ACPI_STA_ALL;
796 break;
797 }
798 }
799 }
800
801 return (unsigned int)sta;
802}
803
804/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700805 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800806 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700807 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700808int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700809{
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700810 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700811
Alex Chiang58c08622009-10-26 21:25:27 -0600812 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700813 /* We don't want to call _EJ0 on non-existing functions. */
814 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800815 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700816 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800817 else
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700818 break;
819 }
820 }
821 return 0;
822}
823
824/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800826 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
828 * Iterate over all slots under this bridge and make sure that if a
829 * card is present they are enabled, and if not they are disabled.
830 */
831static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
832{
833 struct acpiphp_slot *slot;
834 int retval = 0;
835 int enabled, disabled;
836
837 enabled = disabled = 0;
838
Yijing Wangad41dd92013-04-12 05:44:27 +0000839 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned int status = get_slot_status(slot);
841 if (slot->flags & SLOT_ENABLED) {
842 if (status == ACPI_STA_ALL)
843 continue;
844 retval = acpiphp_disable_slot(slot);
845 if (retval) {
846 err("Error occurred in disabling\n");
847 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700848 } else {
849 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 disabled++;
852 } else {
853 if (status != ACPI_STA_ALL)
854 continue;
855 retval = acpiphp_enable_slot(slot);
856 if (retval) {
857 err("Error occurred in enabling\n");
858 goto err_exit;
859 }
860 enabled++;
861 }
862 }
863
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800864 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 err_exit:
867 return retval;
868}
869
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600870static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700871{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700872 struct pci_dev *dev;
873
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600874 list_for_each_entry(dev, &bus->devices, bus_list)
875 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700876}
877
878/*
879 * Remove devices for which we could not assign resources, call
880 * arch specific code to fix-up the bus
881 */
882static void acpiphp_sanitize_bus(struct pci_bus *bus)
883{
Yijing Wangd65eba62013-04-12 05:44:17 +0000884 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700885 int i;
886 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
887
Yijing Wangd65eba62013-04-12 05:44:17 +0000888 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700889 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
890 struct resource *res = &dev->resource[i];
891 if ((res->flags & type_mask) && !res->start &&
892 res->end) {
893 /* Could not assign a required resources
894 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800895 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700896 break;
897 }
898 }
899 }
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/*
903 * ACPI event handlers
904 */
905
Gary Hade0bbd6422007-07-05 11:10:48 -0700906static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700907check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
908{
909 struct acpiphp_bridge *bridge;
910 char objname[64];
911 struct acpi_buffer buffer = { .length = sizeof(objname),
912 .pointer = objname };
913
914 bridge = acpiphp_handle_to_bridge(handle);
915 if (bridge) {
916 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
917 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800918 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700919 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000920 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700921 }
922 return AE_OK ;
923}
924
Yinghai Lu3f327e32013-05-07 11:06:03 -0600925void acpiphp_check_host_bridge(acpi_handle handle)
926{
927 struct acpiphp_bridge *bridge;
928
929 bridge = acpiphp_handle_to_bridge(handle);
930 if (bridge) {
931 acpiphp_check_bridge(bridge);
932 put_bridge(bridge);
933 }
934
935 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
936 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
937}
938
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200939static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200941 struct acpiphp_context *context = data;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200942 struct acpiphp_func *func = context->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 struct acpiphp_bridge *bridge;
944 char objname[64];
945 struct acpi_buffer buffer = { .length = sizeof(objname),
946 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400947
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200948 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200949 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200950 if (bridge)
951 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200953 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
956
957 switch (type) {
958 case ACPI_NOTIFY_BUS_CHECK:
959 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800960 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800961 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200962 if (bridge) {
963 acpiphp_check_bridge(bridge);
964 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
965 ACPI_UINT32_MAX, check_sub_bridges,
966 NULL, NULL, NULL);
967 } else {
968 acpiphp_enable_slot(func->slot);
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 break;
971
972 case ACPI_NOTIFY_DEVICE_CHECK:
973 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800974 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200975 if (bridge)
976 acpiphp_check_bridge(bridge);
977 else
978 acpiphp_check_bridge(func->slot->bridge);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981
982 case ACPI_NOTIFY_DEVICE_WAKE:
983 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800984 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986
987 case ACPI_NOTIFY_EJECT_REQUEST:
988 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800989 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200990 if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
991 break;
992
993 if (!(acpiphp_disable_slot(func->slot)))
994 acpiphp_eject_slot(func->slot);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 break;
997
998 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
999 printk(KERN_ERR "Device %s cannot be configured due"
1000 " to a frequency mismatch\n", objname);
1001 break;
1002
1003 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1004 printk(KERN_ERR "Device %s cannot be configured due"
1005 " to a bus mode mismatch\n", objname);
1006 break;
1007
1008 case ACPI_NOTIFY_POWER_FAULT:
1009 printk(KERN_ERR "Device %s has suffered a power fault\n",
1010 objname);
1011 break;
1012
1013 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001014 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1015 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001018
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001019 if (bridge)
1020 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001023static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001024{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001025 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001026 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001027
1028 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001029 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001030 acpi_scan_lock_acquire();
1031
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001032 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001033
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001034 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001035 kfree(hp_work); /* allocated in handle_hotplug_event() */
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001036 put_bridge(context->func->slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001039/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001040 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001041 * @handle: Notify()'ed acpi_handle
1042 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001043 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001044 *
1045 * Handles ACPI event notification on slots.
1046 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001047static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001048{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001049 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001050
1051 mutex_lock(&acpiphp_context_lock);
1052 context = acpiphp_get_context(handle);
1053 if (context) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001054 get_bridge(context->func->slot->bridge);
1055 acpiphp_put_context(context);
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001056 }
1057 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001058 /*
1059 * Currently the code adds all hotplug events to the kacpid_wq
1060 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1061 * The proper way to fix this is to reorganize the code so that
1062 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1063 * For now just re-add this work to the kacpi_hotplug_wq so we
1064 * don't deadlock on hotplug actions.
1065 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001066 if (context)
1067 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001070/*
1071 * Create hotplug slots for the PCI bus.
1072 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001074void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001076 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001077 acpi_handle handle;
1078 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001080 if (acpiphp_disabled)
1081 return;
1082
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001083 handle = ACPI_HANDLE(bus->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001084 if (!handle)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001085 return;
1086
1087 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001088 if (!bridge) {
1089 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001090 return;
1091 }
1092
Yijing Wangad41dd92013-04-12 05:44:27 +00001093 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001094 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001095 bridge->handle = handle;
1096 bridge->pci_dev = pci_dev_get(bus->self);
1097 bridge->pci_bus = bus;
1098
1099 /*
1100 * Grab a ref to the subordinate PCI bus in case the bus is
1101 * removed via PCI core logical hotplug. The ref pins the bus
1102 * (which we access during module unload).
1103 */
1104 get_device(&bus->dev);
1105
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001106 if (!pci_is_root_bus(bridge->pci_bus)) {
1107 struct acpiphp_context *context;
1108
1109 /*
1110 * This bridge should have been registered as a hotplug function
1111 * under its parent, so the context has to be there. If not, we
1112 * are in deep goo.
1113 */
1114 mutex_lock(&acpiphp_context_lock);
1115 context = acpiphp_get_context(handle);
1116 if (WARN_ON(!context || !context->func)) {
1117 mutex_unlock(&acpiphp_context_lock);
1118 put_device(&bus->dev);
1119 kfree(bridge);
1120 return;
1121 }
1122 bridge->context = context;
1123 context->bridge = bridge;
1124 /* Get a reference to the parent bridge. */
1125 get_bridge(context->func->slot->bridge);
1126 mutex_unlock(&acpiphp_context_lock);
1127 }
1128
1129 status = acpi_get_handle(bridge->handle, "_EJ0", &handle);
1130 if (ACPI_SUCCESS(status)) {
1131 dbg("found ejectable p2p bridge\n");
1132 bridge->flags |= BRIDGE_HAS_EJ0;
1133 }
1134
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001135 /* must be added to the list prior to calling register_slot */
1136 mutex_lock(&bridge_mutex);
1137 list_add(&bridge->list, &bridge_list);
1138 mutex_unlock(&bridge_mutex);
1139
1140 /* register all slot objects under this bridge */
1141 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1142 register_slot, NULL, bridge, NULL);
1143 if (ACPI_FAILURE(status)) {
1144 acpi_handle_err(bridge->handle, "failed to register slots\n");
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001145 cleanup_bridge(bridge);
1146 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001150/* Destroy hotplug slots associated with the PCI bus */
1151void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001153 struct acpiphp_bridge *bridge, *tmp;
1154
1155 if (acpiphp_disabled)
1156 return;
1157
1158 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1159 if (bridge->pci_bus == bus) {
1160 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001161 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001162 break;
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/**
1167 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001168 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170int acpiphp_enable_slot(struct acpiphp_slot *slot)
1171{
1172 int retval;
1173
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001174 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* wake up all functions */
1177 retval = power_on_slot(slot);
1178 if (retval)
1179 goto err_exit;
1180
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001181 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /* configure all functions */
1183 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001184 if (retval)
1185 power_off_slot(slot);
1186 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001187 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001188 power_off_slot(slot);
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001192 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return retval;
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/**
1197 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001198 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
1200int acpiphp_disable_slot(struct acpiphp_slot *slot)
1201{
1202 int retval = 0;
1203
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001204 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* unconfigure all functions */
1207 retval = disable_device(slot);
1208 if (retval)
1209 goto err_exit;
1210
1211 /* power off all functions */
1212 retval = power_off_slot(slot);
1213 if (retval)
1214 goto err_exit;
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001217 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return retval;
1219}
1220
1221
1222/*
1223 * slot enabled: 1
1224 * slot disabled: 0
1225 */
1226u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1227{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001228 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231
1232/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001233 * latch open: 1
1234 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1237{
1238 unsigned int sta;
1239
1240 sta = get_slot_status(slot);
1241
Jiang Liuce15d872013-04-12 05:44:19 +00001242 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245
1246/*
1247 * adapter presence : 1
1248 * absence : 0
1249 */
1250u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1251{
1252 unsigned int sta;
1253
1254 sta = get_slot_status(slot);
1255
1256 return (sta == 0) ? 0 : 1;
1257}