blob: a251071b7d7984ef588cd8ab16d6b26b4e8c0817 [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 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200356 sun, pci_domain_nr(pbus), pbus->number, device);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200357
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200358 retval = acpiphp_register_hotplug_slot(slot, sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200359 if (retval) {
360 bridge->nr_slots--;
361 if (retval == -EBUSY)
362 warn("Slot %llu already registered by another "
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200363 "hotplug driver\n", sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200364 else
365 warn("acpiphp_register_hotplug_slot failed "
366 "(err code = 0x%x)\n", retval);
367 }
368 /* Even if the slot registration fails, we can still use it. */
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200369 }
370
371 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000373 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000375 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000377 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
378 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400381 if (is_dock_device(handle)) {
382 /* we don't want to call this device's _EJ0
383 * because we want the dock notify handler
384 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800385 */
386 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400387 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200388 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200389 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400390 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800394 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200395 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
396 handle_hotplug_event,
397 context);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200398 if (ACPI_FAILURE(status))
399 acpi_handle_err(handle,
400 "failed to install notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200403 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800404
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200405 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200406 mutex_lock(&acpiphp_context_lock);
407 context->func = NULL;
408 acpiphp_put_context(context);
409 mutex_unlock(&acpiphp_context_lock);
410 kfree(newfunc);
411 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Rajesh Shah42f49a62005-04-28 00:25:53 -0700414static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
415{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200416 struct acpiphp_context *context;
417 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600418
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200419 mutex_lock(&acpiphp_context_lock);
420 context = acpiphp_get_context(handle);
421 if (context) {
422 bridge = context->bridge;
423 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000424 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700425
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200426 acpiphp_put_context(context);
427 }
428 mutex_unlock(&acpiphp_context_lock);
429 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700430}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Rajesh Shah364d5092005-04-28 00:25:54 -0700432static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Jiang Liu3d54a312013-04-12 05:44:28 +0000434 struct acpiphp_slot *slot;
435 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 acpi_status status;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700437
Jiang Liu3d54a312013-04-12 05:44:28 +0000438 list_for_each_entry(slot, &bridge->slots, node) {
439 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400440 if (is_dock_device(func->handle)) {
441 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400442 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800443 if (!(func->flags & FUNC_HAS_DCK)) {
444 status = acpi_remove_notify_handler(func->handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200445 ACPI_SYSTEM_NOTIFY,
446 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800447 if (ACPI_FAILURE(status))
448 err("failed to remove notify handler\n");
449 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700450 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800451 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700452 }
453
Jiang Liu3d54a312013-04-12 05:44:28 +0000454 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700455 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000456 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static int power_on_slot(struct acpiphp_slot *slot)
460{
461 acpi_status status;
462 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int retval = 0;
464
465 /* if already enabled, just skip */
466 if (slot->flags & SLOT_POWEREDON)
467 goto err_exit;
468
Alex Chiang58c08622009-10-26 21:25:27 -0600469 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800471 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
473 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800474 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 retval = -1;
476 goto err_exit;
477 } else
478 break;
479 }
480 }
481
482 /* TBD: evaluate _STA to check if the slot is enabled */
483
484 slot->flags |= SLOT_POWEREDON;
485
486 err_exit:
487 return retval;
488}
489
490
491static int power_off_slot(struct acpiphp_slot *slot)
492{
493 acpi_status status;
494 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 int retval = 0;
497
498 /* if already disabled, just skip */
499 if ((slot->flags & SLOT_POWEREDON) == 0)
500 goto err_exit;
501
Alex Chiang58c08622009-10-26 21:25:27 -0600502 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700503 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
505 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800506 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 retval = -1;
508 goto err_exit;
509 } else
510 break;
511 }
512 }
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* TBD: evaluate _STA to check if the slot is disabled */
515
516 slot->flags &= (~SLOT_POWEREDON);
517
518 err_exit:
519 return retval;
520}
521
522
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800523
524/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800525 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800526 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800527 */
528static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
529{
530 struct list_head *tmp;
531 unsigned char max, n;
532
533 /*
534 * pci_bus_max_busnr will return the highest
535 * reserved busnr for all these children.
536 * that is equivalent to the bus->subordinate
537 * value. We don't want to use the parent's
538 * bus->subordinate value because it could have
539 * padding in it.
540 */
Yinghai Lub918c622012-05-17 18:51:11 -0700541 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800542
543 list_for_each(tmp, &bus->children) {
544 n = pci_bus_max_busnr(pci_bus_b(tmp));
545 if (n > max)
546 max = n;
547 }
548 return max;
549}
550
551
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800552/**
553 * acpiphp_bus_add - add a new bus to acpi subsystem
554 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800555 */
556static int acpiphp_bus_add(struct acpiphp_func *func)
557{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100558 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800559 int ret_val;
560
Kristen Accardi20416ea2006-02-23 17:56:03 -0800561 if (!acpi_bus_get_device(func->handle, &device)) {
562 dbg("bus exists... trim\n");
563 /* this shouldn't be in here, so remove
564 * the bus then re-add it...
565 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100566 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800567 }
568
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100569 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100570 if (!ret_val)
571 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800572
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100573 if (ret_val)
574 dbg("error adding bus, %x\n", -ret_val);
575
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800576 return ret_val;
577}
578
579
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900580/**
581 * acpiphp_bus_trim - trim a bus from acpi subsystem
582 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900583 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700584static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900585{
586 struct acpi_device *device;
587 int retval;
588
589 retval = acpi_bus_get_device(handle, &device);
590 if (retval) {
591 dbg("acpi_device not found\n");
592 return retval;
593 }
594
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100595 acpi_bus_trim(device);
596 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900597}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800598
Shaohua Lid0607052010-02-25 10:59:34 +0800599static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
600{
601 struct acpiphp_func *func;
602 union acpi_object params[2];
603 struct acpi_object_list arg_list;
604
605 list_for_each_entry(func, &slot->funcs, sibling) {
606 arg_list.count = 2;
607 arg_list.pointer = params;
608 params[0].type = ACPI_TYPE_INTEGER;
609 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
610 params[1].type = ACPI_TYPE_INTEGER;
611 params[1].integer.value = 1;
612 /* _REG is optional, we don't care about if there is failure */
613 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
614 }
615}
616
Yinghai Lu1f96a962013-01-21 13:20:42 -0800617static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
618{
619 struct acpiphp_func *func;
620
621 if (!dev->subordinate)
622 return;
623
624 /* quirk, or pcie could set it already */
625 if (dev->is_hotplug_bridge)
626 return;
627
628 if (PCI_SLOT(dev->devfn) != slot->device)
629 return;
630
631 list_for_each_entry(func, &slot->funcs, sibling) {
632 if (PCI_FUNC(dev->devfn) == func->function) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200633 dev->is_hotplug_bridge = 1;
Yinghai Lu1f96a962013-01-21 13:20:42 -0800634 break;
635 }
636 }
637}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/**
640 * enable_device - enable, configure a slot
641 * @slot: slot to be enabled
642 *
643 * This function should be called per *physical slot*,
644 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100646static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700649 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700651 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200652 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (slot->flags & SLOT_ENABLED)
655 goto err_exit;
656
Jiang Liu2ca344e2013-01-31 00:10:09 +0800657 list_for_each_entry(func, &slot->funcs, sibling)
658 acpiphp_bus_add(func);
659
Rajesh Shah42f49a62005-04-28 00:25:53 -0700660 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
661 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800662 /* Maybe only part of funcs are added. */
663 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto err_exit;
665 }
666
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800667 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700668 for (pass = 0; pass < 2; pass++) {
669 list_for_each_entry(dev, &bus->devices, bus_list) {
670 if (PCI_SLOT(dev->devfn) != slot->device)
671 continue;
672 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800673 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700674 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800675 if (pass && dev->subordinate) {
676 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200677 pcibios_resource_survey_bus(dev->subordinate);
678 __pci_bus_size_bridges(dev->subordinate,
679 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800680 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800681 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Jiang Liud66ecb72013-06-23 01:01:35 +0200685 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700686 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600687 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800688 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700689 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100690
691 list_for_each_entry(dev, &bus->devices, bus_list) {
692 /* Assume that newly added devices are powered on already. */
693 if (!dev->is_added)
694 dev->current_state = PCI_D0;
695 }
696
Rajesh Shah42f49a62005-04-28 00:25:53 -0700697 pci_bus_add_devices(bus);
698
Amos Kongf382a082011-11-25 15:03:07 +0800699 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600700 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600701 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
702 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800703 if (!dev) {
704 /* Do not set SLOT_ENABLED flag if some funcs
705 are not added. */
706 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900707 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
Amos Kongce29ca32012-05-23 10:20:35 -0600716/* return first device in slot, acquiring a reference on it */
717static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
718{
719 struct pci_bus *bus = slot->bridge->pci_bus;
720 struct pci_dev *dev;
721 struct pci_dev *ret = NULL;
722
723 down_read(&pci_bus_sem);
724 list_for_each_entry(dev, &bus->devices, bus_list)
725 if (PCI_SLOT(dev->devfn) == slot->device) {
726 ret = pci_dev_get(dev);
727 break;
728 }
729 up_read(&pci_bus_sem);
730
731 return ret;
732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800736 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
738static int disable_device(struct acpiphp_slot *slot)
739{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600741 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900742
Amos Kongce29ca32012-05-23 10:20:35 -0600743 /*
744 * enable_device() enumerates all functions in this device via
745 * pci_scan_slot(), whether they have associated ACPI hotplug
746 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
747 * here.
748 */
749 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600750 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600751 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700752 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700753
Alex Chiang9d911d72009-05-21 16:21:15 -0600754 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900755 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757
758 slot->flags &= (~SLOT_ENABLED);
759
Alex Chiang9d911d72009-05-21 16:21:15 -0600760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763
764/**
765 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800766 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800768 * If a slot has _STA for each function and if any one of them
769 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800771 * If a slot doesn't have _STA and if any one of its functions'
772 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800774 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
776static unsigned int get_slot_status(struct acpiphp_slot *slot)
777{
778 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400779 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct acpiphp_func *func;
782
Alex Chiang58c08622009-10-26 21:25:27 -0600783 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (func->flags & FUNC_HAS_STA) {
785 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
786 if (ACPI_SUCCESS(status) && sta)
787 break;
788 } else {
789 pci_bus_read_config_dword(slot->bridge->pci_bus,
790 PCI_DEVFN(slot->device,
791 func->function),
792 PCI_VENDOR_ID, &dvid);
793 if (dvid != 0xffffffff) {
794 sta = ACPI_STA_ALL;
795 break;
796 }
797 }
798 }
799
800 return (unsigned int)sta;
801}
802
803/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700804 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800805 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700806 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700807int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700808{
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700809 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700810
Alex Chiang58c08622009-10-26 21:25:27 -0600811 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700812 /* We don't want to call _EJ0 on non-existing functions. */
813 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800814 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700815 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800816 else
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700817 break;
818 }
819 }
820 return 0;
821}
822
823/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800825 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
827 * Iterate over all slots under this bridge and make sure that if a
828 * card is present they are enabled, and if not they are disabled.
829 */
830static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
831{
832 struct acpiphp_slot *slot;
833 int retval = 0;
834 int enabled, disabled;
835
836 enabled = disabled = 0;
837
Yijing Wangad41dd92013-04-12 05:44:27 +0000838 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 unsigned int status = get_slot_status(slot);
840 if (slot->flags & SLOT_ENABLED) {
841 if (status == ACPI_STA_ALL)
842 continue;
843 retval = acpiphp_disable_slot(slot);
844 if (retval) {
845 err("Error occurred in disabling\n");
846 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700847 } else {
848 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 disabled++;
851 } else {
852 if (status != ACPI_STA_ALL)
853 continue;
854 retval = acpiphp_enable_slot(slot);
855 if (retval) {
856 err("Error occurred in enabling\n");
857 goto err_exit;
858 }
859 enabled++;
860 }
861 }
862
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800863 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 err_exit:
866 return retval;
867}
868
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600869static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700870{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700871 struct pci_dev *dev;
872
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600873 list_for_each_entry(dev, &bus->devices, bus_list)
874 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700875}
876
877/*
878 * Remove devices for which we could not assign resources, call
879 * arch specific code to fix-up the bus
880 */
881static void acpiphp_sanitize_bus(struct pci_bus *bus)
882{
Yijing Wangd65eba62013-04-12 05:44:17 +0000883 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700884 int i;
885 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
886
Yijing Wangd65eba62013-04-12 05:44:17 +0000887 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700888 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
889 struct resource *res = &dev->resource[i];
890 if ((res->flags & type_mask) && !res->start &&
891 res->end) {
892 /* Could not assign a required resources
893 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800894 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700895 break;
896 }
897 }
898 }
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * ACPI event handlers
903 */
904
Gary Hade0bbd6422007-07-05 11:10:48 -0700905static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700906check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
907{
908 struct acpiphp_bridge *bridge;
909 char objname[64];
910 struct acpi_buffer buffer = { .length = sizeof(objname),
911 .pointer = objname };
912
913 bridge = acpiphp_handle_to_bridge(handle);
914 if (bridge) {
915 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
916 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800917 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700918 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000919 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700920 }
921 return AE_OK ;
922}
923
Yinghai Lu3f327e32013-05-07 11:06:03 -0600924void acpiphp_check_host_bridge(acpi_handle handle)
925{
926 struct acpiphp_bridge *bridge;
927
928 bridge = acpiphp_handle_to_bridge(handle);
929 if (bridge) {
930 acpiphp_check_bridge(bridge);
931 put_bridge(bridge);
932 }
933
934 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
935 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
936}
937
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200938static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200940 struct acpiphp_context *context = data;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200941 struct acpiphp_func *func = context->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct acpiphp_bridge *bridge;
943 char objname[64];
944 struct acpi_buffer buffer = { .length = sizeof(objname),
945 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400946
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200947 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200948 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200949 if (bridge)
950 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200952 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
955
956 switch (type) {
957 case ACPI_NOTIFY_BUS_CHECK:
958 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800959 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800960 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200961 if (bridge) {
962 acpiphp_check_bridge(bridge);
963 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
964 ACPI_UINT32_MAX, check_sub_bridges,
965 NULL, NULL, NULL);
966 } else {
967 acpiphp_enable_slot(func->slot);
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 break;
970
971 case ACPI_NOTIFY_DEVICE_CHECK:
972 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800973 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200974 if (bridge)
975 acpiphp_check_bridge(bridge);
976 else
977 acpiphp_check_bridge(func->slot->bridge);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980
981 case ACPI_NOTIFY_DEVICE_WAKE:
982 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800983 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985
986 case ACPI_NOTIFY_EJECT_REQUEST:
987 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800988 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200989 if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
990 break;
991
992 if (!(acpiphp_disable_slot(func->slot)))
993 acpiphp_eject_slot(func->slot);
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996
997 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
998 printk(KERN_ERR "Device %s cannot be configured due"
999 " to a frequency mismatch\n", objname);
1000 break;
1001
1002 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1003 printk(KERN_ERR "Device %s cannot be configured due"
1004 " to a bus mode mismatch\n", objname);
1005 break;
1006
1007 case ACPI_NOTIFY_POWER_FAULT:
1008 printk(KERN_ERR "Device %s has suffered a power fault\n",
1009 objname);
1010 break;
1011
1012 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001013 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1014 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001017
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001018 if (bridge)
1019 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001022static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001023{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001024 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001025 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001026
1027 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001028 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001029 acpi_scan_lock_acquire();
1030
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001031 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001032
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001033 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001034 kfree(hp_work); /* allocated in handle_hotplug_event() */
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001035 put_bridge(context->func->slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001038/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001039 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001040 * @handle: Notify()'ed acpi_handle
1041 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001042 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001043 *
1044 * Handles ACPI event notification on slots.
1045 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001046static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001047{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001048 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001049
1050 mutex_lock(&acpiphp_context_lock);
1051 context = acpiphp_get_context(handle);
1052 if (context) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001053 get_bridge(context->func->slot->bridge);
1054 acpiphp_put_context(context);
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001055 }
1056 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001057 /*
1058 * Currently the code adds all hotplug events to the kacpid_wq
1059 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1060 * The proper way to fix this is to reorganize the code so that
1061 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1062 * For now just re-add this work to the kacpi_hotplug_wq so we
1063 * don't deadlock on hotplug actions.
1064 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001065 if (context)
1066 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001069/*
1070 * Create hotplug slots for the PCI bus.
1071 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001073void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001075 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001076 acpi_handle handle;
1077 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001079 if (acpiphp_disabled)
1080 return;
1081
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001082 handle = ACPI_HANDLE(bus->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001083 if (!handle)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001084 return;
1085
1086 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001087 if (!bridge) {
1088 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001089 return;
1090 }
1091
Yijing Wangad41dd92013-04-12 05:44:27 +00001092 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001093 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001094 bridge->handle = handle;
1095 bridge->pci_dev = pci_dev_get(bus->self);
1096 bridge->pci_bus = bus;
1097
1098 /*
1099 * Grab a ref to the subordinate PCI bus in case the bus is
1100 * removed via PCI core logical hotplug. The ref pins the bus
1101 * (which we access during module unload).
1102 */
1103 get_device(&bus->dev);
1104
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001105 if (!pci_is_root_bus(bridge->pci_bus)) {
1106 struct acpiphp_context *context;
1107
1108 /*
1109 * This bridge should have been registered as a hotplug function
1110 * under its parent, so the context has to be there. If not, we
1111 * are in deep goo.
1112 */
1113 mutex_lock(&acpiphp_context_lock);
1114 context = acpiphp_get_context(handle);
1115 if (WARN_ON(!context || !context->func)) {
1116 mutex_unlock(&acpiphp_context_lock);
1117 put_device(&bus->dev);
1118 kfree(bridge);
1119 return;
1120 }
1121 bridge->context = context;
1122 context->bridge = bridge;
1123 /* Get a reference to the parent bridge. */
1124 get_bridge(context->func->slot->bridge);
1125 mutex_unlock(&acpiphp_context_lock);
1126 }
1127
1128 status = acpi_get_handle(bridge->handle, "_EJ0", &handle);
1129 if (ACPI_SUCCESS(status)) {
1130 dbg("found ejectable p2p bridge\n");
1131 bridge->flags |= BRIDGE_HAS_EJ0;
1132 }
1133
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001134 /* must be added to the list prior to calling register_slot */
1135 mutex_lock(&bridge_mutex);
1136 list_add(&bridge->list, &bridge_list);
1137 mutex_unlock(&bridge_mutex);
1138
1139 /* register all slot objects under this bridge */
1140 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1141 register_slot, NULL, bridge, NULL);
1142 if (ACPI_FAILURE(status)) {
1143 acpi_handle_err(bridge->handle, "failed to register slots\n");
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001144 cleanup_bridge(bridge);
1145 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001149/* Destroy hotplug slots associated with the PCI bus */
1150void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001152 struct acpiphp_bridge *bridge, *tmp;
1153
1154 if (acpiphp_disabled)
1155 return;
1156
1157 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1158 if (bridge->pci_bus == bus) {
1159 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001160 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001161 break;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165/**
1166 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001167 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
1169int acpiphp_enable_slot(struct acpiphp_slot *slot)
1170{
1171 int retval;
1172
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001173 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* wake up all functions */
1176 retval = power_on_slot(slot);
1177 if (retval)
1178 goto err_exit;
1179
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001180 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* configure all functions */
1182 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001183 if (retval)
1184 power_off_slot(slot);
1185 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001186 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001187 power_off_slot(slot);
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001191 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return retval;
1193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/**
1196 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001197 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
1199int acpiphp_disable_slot(struct acpiphp_slot *slot)
1200{
1201 int retval = 0;
1202
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001203 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* unconfigure all functions */
1206 retval = disable_device(slot);
1207 if (retval)
1208 goto err_exit;
1209
1210 /* power off all functions */
1211 retval = power_off_slot(slot);
1212 if (retval)
1213 goto err_exit;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001216 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return retval;
1218}
1219
1220
1221/*
1222 * slot enabled: 1
1223 * slot disabled: 0
1224 */
1225u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1226{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001227 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230
1231/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001232 * latch open: 1
1233 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 */
1235u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1236{
1237 unsigned int sta;
1238
1239 sta = get_slot_status(slot);
1240
Jiang Liuce15d872013-04-12 05:44:19 +00001241 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244
1245/*
1246 * adapter presence : 1
1247 * absence : 0
1248 */
1249u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1250{
1251 unsigned int sta;
1252
1253 sta = get_slot_status(slot);
1254
1255 return (sta == 0) ? 0 : 1;
1256}