blob: 9e4ad6f37647ab7e9ec868a33f758ae8593a117c [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
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200131 WARN_ON(context->bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200132 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) {
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200158 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
159 acpiphp_put_context(func_to_context(func));
160
Jiang Liu3d54a312013-04-12 05:44:28 +0000161 kfree(slot);
162 }
163
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200164 context = bridge->context;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200165 /* Root bridges will not have hotplug context. */
166 if (context) {
167 /* Release the reference taken by acpiphp_enumerate_slots(). */
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200168 put_bridge(context->func.slot->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200169 context->bridge = NULL;
170 acpiphp_put_context(context);
171 }
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200172
Jiang Liu3d54a312013-04-12 05:44:28 +0000173 put_device(&bridge->pci_bus->dev);
174 pci_dev_put(bridge->pci_dev);
175 kfree(bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200176
177 mutex_unlock(&acpiphp_context_lock);
Jiang Liu3d54a312013-04-12 05:44:28 +0000178}
179
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400180/*
181 * the _DCK method can do funny things... and sometimes not
182 * hah-hah funny.
183 *
184 * TBD - figure out a way to only call fixups for
185 * systems that require them.
186 */
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200187static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400188{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200189 struct acpiphp_context *context = data;
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200190 struct pci_bus *bus = context->func.slot->bridge->pci_bus;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400191 u32 buses;
192
193 if (!bus->self)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200194 return;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400195
196 /* fixup bad _DCK function that rewrites
197 * secondary bridge on slot
198 */
199 pci_read_config_dword(bus->self,
200 PCI_PRIMARY_BUS,
201 &buses);
202
Yinghai Lub918c622012-05-17 18:51:11 -0700203 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400204 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700205 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700206 | ((unsigned int)(bus->busn_res.start) << 8)
207 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400208 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
209 }
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400210}
211
212
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400213static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200214 .fixup = post_dock_fixups,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200215 .handler = hotplug_event,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800216};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jiang Liu5ba113f2012-08-22 23:16:45 +0800218/* Check whether the PCI device is managed by native PCIe hotplug driver */
219static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
220{
221 u32 reg32;
222 acpi_handle tmp;
223 struct acpi_pci_root *root;
224
225 /* Check whether the PCIe port supports native PCIe hotplug */
226 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
227 return false;
228 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
229 return false;
230
231 /*
232 * Check whether native PCIe hotplug has been enabled for
233 * this PCIe hierarchy.
234 */
235 tmp = acpi_find_root_bridge_handle(pdev);
236 if (!tmp)
237 return false;
238 root = acpi_pci_find_root(tmp);
239 if (!root)
240 return false;
241 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
242 return false;
243
244 return true;
245}
246
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200247static void acpiphp_dock_init(void *data)
248{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200249 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200250
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200251 get_bridge(context->func.slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200252}
253
254static void acpiphp_dock_release(void *data)
255{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200256 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200257
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200258 put_bridge(context->func.slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/* callback routine to register each ACPI PCI slot object */
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200262static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
263 void **rv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200265 struct acpiphp_bridge *bridge = data;
266 struct acpiphp_context *context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct acpiphp_slot *slot;
268 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 acpi_status status = AE_OK;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200270 unsigned long long adr;
271 int device, function;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900272 struct pci_bus *pbus = bridge->pci_bus;
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200273 struct pci_dev *pdev = bridge->pci_dev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000274 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200276 if (pdev && device_is_managed_by_native_pciehp(pdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return AE_OK;
278
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600279 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
280 if (ACPI_FAILURE(status)) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200281 acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600282 return AE_OK;
283 }
284
285 device = (adr >> 16) & 0xffff;
286 function = adr & 0xffff;
287
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200288 mutex_lock(&acpiphp_context_lock);
289 context = acpiphp_init_context(handle);
290 if (!context) {
291 mutex_unlock(&acpiphp_context_lock);
292 acpi_handle_err(handle, "No hotplug context\n");
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200293 return AE_NOT_EXIST;
294 }
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200295 newfunc = &context->func;
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200296 newfunc->function = function;
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200297 mutex_unlock(&acpiphp_context_lock);
298
Jiang Liuecd046d2013-06-29 00:24:43 +0800299 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800300 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jiang Liuecd046d2013-06-29 00:24:43 +0800302 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 newfunc->flags |= FUNC_HAS_STA;
304
Jiang Liuecd046d2013-06-29 00:24:43 +0800305 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 newfunc->flags |= FUNC_HAS_PS0;
307
Jiang Liuecd046d2013-06-29 00:24:43 +0800308 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 newfunc->flags |= FUNC_HAS_PS3;
310
Jiang Liuecd046d2013-06-29 00:24:43 +0800311 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800312 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000315 list_for_each_entry(slot, &bridge->slots, node)
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200316 if (slot->device == device)
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200317 goto slot_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200319 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
320 if (!slot) {
321 status = AE_NO_MEMORY;
322 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200325 slot->bridge = bridge;
326 slot->device = device;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200327 INIT_LIST_HEAD(&slot->funcs);
328 mutex_init(&slot->crit_sect);
329
330 mutex_lock(&bridge_mutex);
331 list_add_tail(&slot->node, &bridge->slots);
332 mutex_unlock(&bridge_mutex);
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200333
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200334 /* Register slots for ejectable funtions only. */
335 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
336 unsigned long long sun;
337 int retval;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200338
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200339 bridge->nr_slots++;
340 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
341 if (ACPI_FAILURE(status))
342 sun = bridge->nr_slots;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200343
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200344 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200345 sun, pci_domain_nr(pbus), pbus->number, device);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200346
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200347 retval = acpiphp_register_hotplug_slot(slot, sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200348 if (retval) {
349 bridge->nr_slots--;
350 if (retval == -EBUSY)
351 warn("Slot %llu already registered by another "
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200352 "hotplug driver\n", sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200353 else
354 warn("acpiphp_register_hotplug_slot failed "
355 "(err code = 0x%x)\n", retval);
356 }
357 /* Even if the slot registration fails, we can still use it. */
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200358 }
359
360 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000362 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000364 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000366 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
367 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400370 if (is_dock_device(handle)) {
371 /* we don't want to call this device's _EJ0
372 * because we want the dock notify handler
373 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800374 */
375 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400376 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200377 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200378 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400379 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800380 }
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800383 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200384 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
385 handle_hotplug_event,
386 context);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200387 if (ACPI_FAILURE(status))
388 acpi_handle_err(handle,
389 "failed to install notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200392 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800393
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200394 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200395 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200396 acpiphp_put_context(context);
397 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200398 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Rajesh Shah42f49a62005-04-28 00:25:53 -0700401static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
402{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200403 struct acpiphp_context *context;
404 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600405
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200406 mutex_lock(&acpiphp_context_lock);
407 context = acpiphp_get_context(handle);
408 if (context) {
409 bridge = context->bridge;
410 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000411 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700412
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200413 acpiphp_put_context(context);
414 }
415 mutex_unlock(&acpiphp_context_lock);
416 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Rajesh Shah364d5092005-04-28 00:25:54 -0700419static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Jiang Liu3d54a312013-04-12 05:44:28 +0000421 struct acpiphp_slot *slot;
422 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700423 acpi_status status;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700424
Jiang Liu3d54a312013-04-12 05:44:28 +0000425 list_for_each_entry(slot, &bridge->slots, node) {
426 list_for_each_entry(func, &slot->funcs, sibling) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200427 acpi_handle handle = func_to_handle(func);
428
429 if (is_dock_device(handle))
430 unregister_hotplug_dock_device(handle);
431
Kristen Accardi20416ea2006-02-23 17:56:03 -0800432 if (!(func->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200433 status = acpi_remove_notify_handler(handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200434 ACPI_SYSTEM_NOTIFY,
435 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800436 if (ACPI_FAILURE(status))
437 err("failed to remove notify handler\n");
438 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700439 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800440 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700441 }
442
Jiang Liu3d54a312013-04-12 05:44:28 +0000443 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700444 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000445 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int power_on_slot(struct acpiphp_slot *slot)
449{
450 acpi_status status;
451 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int retval = 0;
453
454 /* if already enabled, just skip */
455 if (slot->flags & SLOT_POWEREDON)
456 goto err_exit;
457
Alex Chiang58c08622009-10-26 21:25:27 -0600458 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800460 dbg("%s: executing _PS0\n", __func__);
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200461 status = acpi_evaluate_object(func_to_handle(func),
462 "_PS0", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800464 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 retval = -1;
466 goto err_exit;
467 } else
468 break;
469 }
470 }
471
472 /* TBD: evaluate _STA to check if the slot is enabled */
473
474 slot->flags |= SLOT_POWEREDON;
475
476 err_exit:
477 return retval;
478}
479
480
481static int power_off_slot(struct acpiphp_slot *slot)
482{
483 acpi_status status;
484 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 int retval = 0;
487
488 /* if already disabled, just skip */
489 if ((slot->flags & SLOT_POWEREDON) == 0)
490 goto err_exit;
491
Alex Chiang58c08622009-10-26 21:25:27 -0600492 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700493 if (func->flags & FUNC_HAS_PS3) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200494 status = acpi_evaluate_object(func_to_handle(func),
495 "_PS3", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800497 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 retval = -1;
499 goto err_exit;
500 } else
501 break;
502 }
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* TBD: evaluate _STA to check if the slot is disabled */
506
507 slot->flags &= (~SLOT_POWEREDON);
508
509 err_exit:
510 return retval;
511}
512
513
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800514
515/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800516 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800517 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800518 */
519static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
520{
521 struct list_head *tmp;
522 unsigned char max, n;
523
524 /*
525 * pci_bus_max_busnr will return the highest
526 * reserved busnr for all these children.
527 * that is equivalent to the bus->subordinate
528 * value. We don't want to use the parent's
529 * bus->subordinate value because it could have
530 * padding in it.
531 */
Yinghai Lub918c622012-05-17 18:51:11 -0700532 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800533
534 list_for_each(tmp, &bus->children) {
535 n = pci_bus_max_busnr(pci_bus_b(tmp));
536 if (n > max)
537 max = n;
538 }
539 return max;
540}
541
542
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800543/**
544 * acpiphp_bus_add - add a new bus to acpi subsystem
545 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800546 */
547static int acpiphp_bus_add(struct acpiphp_func *func)
548{
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200549 acpi_handle handle = func_to_handle(func);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100550 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800551 int ret_val;
552
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200553 if (!acpi_bus_get_device(handle, &device)) {
Kristen Accardi20416ea2006-02-23 17:56:03 -0800554 dbg("bus exists... trim\n");
555 /* this shouldn't be in here, so remove
556 * the bus then re-add it...
557 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100558 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800559 }
560
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200561 ret_val = acpi_bus_scan(handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100562 if (!ret_val)
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200563 ret_val = acpi_bus_get_device(handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800564
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100565 if (ret_val)
566 dbg("error adding bus, %x\n", -ret_val);
567
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800568 return ret_val;
569}
570
571
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900572/**
573 * acpiphp_bus_trim - trim a bus from acpi subsystem
574 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900575 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700576static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900577{
578 struct acpi_device *device;
579 int retval;
580
581 retval = acpi_bus_get_device(handle, &device);
582 if (retval) {
583 dbg("acpi_device not found\n");
584 return retval;
585 }
586
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100587 acpi_bus_trim(device);
588 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900589}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800590
Shaohua Lid0607052010-02-25 10:59:34 +0800591static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
592{
593 struct acpiphp_func *func;
594 union acpi_object params[2];
595 struct acpi_object_list arg_list;
596
597 list_for_each_entry(func, &slot->funcs, sibling) {
598 arg_list.count = 2;
599 arg_list.pointer = params;
600 params[0].type = ACPI_TYPE_INTEGER;
601 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
602 params[1].type = ACPI_TYPE_INTEGER;
603 params[1].integer.value = 1;
604 /* _REG is optional, we don't care about if there is failure */
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200605 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
606 NULL);
Shaohua Lid0607052010-02-25 10:59:34 +0800607 }
608}
609
Yinghai Lu1f96a962013-01-21 13:20:42 -0800610static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
611{
612 struct acpiphp_func *func;
613
614 if (!dev->subordinate)
615 return;
616
617 /* quirk, or pcie could set it already */
618 if (dev->is_hotplug_bridge)
619 return;
620
621 if (PCI_SLOT(dev->devfn) != slot->device)
622 return;
623
624 list_for_each_entry(func, &slot->funcs, sibling) {
625 if (PCI_FUNC(dev->devfn) == func->function) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200626 dev->is_hotplug_bridge = 1;
Yinghai Lu1f96a962013-01-21 13:20:42 -0800627 break;
628 }
629 }
630}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/**
633 * enable_device - enable, configure a slot
634 * @slot: slot to be enabled
635 *
636 * This function should be called per *physical slot*,
637 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100639static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700642 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700644 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200645 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (slot->flags & SLOT_ENABLED)
648 goto err_exit;
649
Jiang Liu2ca344e2013-01-31 00:10:09 +0800650 list_for_each_entry(func, &slot->funcs, sibling)
651 acpiphp_bus_add(func);
652
Rajesh Shah42f49a62005-04-28 00:25:53 -0700653 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
654 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800655 /* Maybe only part of funcs are added. */
656 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto err_exit;
658 }
659
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800660 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700661 for (pass = 0; pass < 2; pass++) {
662 list_for_each_entry(dev, &bus->devices, bus_list) {
663 if (PCI_SLOT(dev->devfn) != slot->device)
664 continue;
665 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800666 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700667 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800668 if (pass && dev->subordinate) {
669 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200670 pcibios_resource_survey_bus(dev->subordinate);
671 __pci_bus_size_bridges(dev->subordinate,
672 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800673 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800674 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Jiang Liud66ecb72013-06-23 01:01:35 +0200678 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700679 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600680 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800681 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700682 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100683
684 list_for_each_entry(dev, &bus->devices, bus_list) {
685 /* Assume that newly added devices are powered on already. */
686 if (!dev->is_added)
687 dev->current_state = PCI_D0;
688 }
689
Rajesh Shah42f49a62005-04-28 00:25:53 -0700690 pci_bus_add_devices(bus);
691
Amos Kongf382a082011-11-25 15:03:07 +0800692 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600693 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600694 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
695 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800696 if (!dev) {
697 /* Do not set SLOT_ENABLED flag if some funcs
698 are not added. */
699 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900700 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Amos Kongce29ca32012-05-23 10:20:35 -0600709/* return first device in slot, acquiring a reference on it */
710static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
711{
712 struct pci_bus *bus = slot->bridge->pci_bus;
713 struct pci_dev *dev;
714 struct pci_dev *ret = NULL;
715
716 down_read(&pci_bus_sem);
717 list_for_each_entry(dev, &bus->devices, bus_list)
718 if (PCI_SLOT(dev->devfn) == slot->device) {
719 ret = pci_dev_get(dev);
720 break;
721 }
722 up_read(&pci_bus_sem);
723
724 return ret;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/**
728 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800729 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731static int disable_device(struct acpiphp_slot *slot)
732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600734 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900735
Amos Kongce29ca32012-05-23 10:20:35 -0600736 /*
737 * enable_device() enumerates all functions in this device via
738 * pci_scan_slot(), whether they have associated ACPI hotplug
739 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
740 * here.
741 */
742 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600743 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600744 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700745 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700746
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200747 list_for_each_entry(func, &slot->funcs, sibling)
748 acpiphp_bus_trim(func_to_handle(func));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 slot->flags &= (~SLOT_ENABLED);
751
Alex Chiang9d911d72009-05-21 16:21:15 -0600752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755
756/**
757 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800758 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800760 * If a slot has _STA for each function and if any one of them
761 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800763 * If a slot doesn't have _STA and if any one of its functions'
764 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800766 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
768static unsigned int get_slot_status(struct acpiphp_slot *slot)
769{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400770 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct acpiphp_func *func;
772
Alex Chiang58c08622009-10-26 21:25:27 -0600773 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (func->flags & FUNC_HAS_STA) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200775 acpi_status status;
776
777 status = acpi_evaluate_integer(func_to_handle(func),
778 "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (ACPI_SUCCESS(status) && sta)
780 break;
781 } else {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200782 u32 dvid;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 pci_bus_read_config_dword(slot->bridge->pci_bus,
785 PCI_DEVFN(slot->device,
786 func->function),
787 PCI_VENDOR_ID, &dvid);
788 if (dvid != 0xffffffff) {
789 sta = ACPI_STA_ALL;
790 break;
791 }
792 }
793 }
794
795 return (unsigned int)sta;
796}
797
798/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700799 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800800 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700801 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700802int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700803{
Rajesh Shah8d50e332005-04-28 00:25:57 -0700804 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700805
Alex Chiang58c08622009-10-26 21:25:27 -0600806 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700807 /* We don't want to call _EJ0 on non-existing functions. */
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200808 if (!(func->flags & FUNC_HAS_EJ0))
809 continue;
810
811 if (ACPI_FAILURE(acpi_evaluate_ej0(func_to_handle(func))))
812 return -1;
813 else
814 break;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700815 }
816 return 0;
817}
818
819/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800821 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
823 * Iterate over all slots under this bridge and make sure that if a
824 * card is present they are enabled, and if not they are disabled.
825 */
826static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
827{
828 struct acpiphp_slot *slot;
829 int retval = 0;
830 int enabled, disabled;
831
832 enabled = disabled = 0;
833
Yijing Wangad41dd92013-04-12 05:44:27 +0000834 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 unsigned int status = get_slot_status(slot);
836 if (slot->flags & SLOT_ENABLED) {
837 if (status == ACPI_STA_ALL)
838 continue;
839 retval = acpiphp_disable_slot(slot);
840 if (retval) {
841 err("Error occurred in disabling\n");
842 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700843 } else {
844 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 disabled++;
847 } else {
848 if (status != ACPI_STA_ALL)
849 continue;
850 retval = acpiphp_enable_slot(slot);
851 if (retval) {
852 err("Error occurred in enabling\n");
853 goto err_exit;
854 }
855 enabled++;
856 }
857 }
858
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800859 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 err_exit:
862 return retval;
863}
864
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600865static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700866{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700867 struct pci_dev *dev;
868
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600869 list_for_each_entry(dev, &bus->devices, bus_list)
870 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700871}
872
873/*
874 * Remove devices for which we could not assign resources, call
875 * arch specific code to fix-up the bus
876 */
877static void acpiphp_sanitize_bus(struct pci_bus *bus)
878{
Yijing Wangd65eba62013-04-12 05:44:17 +0000879 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700880 int i;
881 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
882
Yijing Wangd65eba62013-04-12 05:44:17 +0000883 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700884 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
885 struct resource *res = &dev->resource[i];
886 if ((res->flags & type_mask) && !res->start &&
887 res->end) {
888 /* Could not assign a required resources
889 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800890 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700891 break;
892 }
893 }
894 }
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/*
898 * ACPI event handlers
899 */
900
Gary Hade0bbd6422007-07-05 11:10:48 -0700901static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700902check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
903{
904 struct acpiphp_bridge *bridge;
905 char objname[64];
906 struct acpi_buffer buffer = { .length = sizeof(objname),
907 .pointer = objname };
908
909 bridge = acpiphp_handle_to_bridge(handle);
910 if (bridge) {
911 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
912 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800913 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700914 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000915 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700916 }
917 return AE_OK ;
918}
919
Yinghai Lu3f327e32013-05-07 11:06:03 -0600920void acpiphp_check_host_bridge(acpi_handle handle)
921{
922 struct acpiphp_bridge *bridge;
923
924 bridge = acpiphp_handle_to_bridge(handle);
925 if (bridge) {
926 acpiphp_check_bridge(bridge);
927 put_bridge(bridge);
928 }
929
930 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
931 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
932}
933
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200934static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200936 struct acpiphp_context *context = data;
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200937 struct acpiphp_func *func = &context->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct acpiphp_bridge *bridge;
939 char objname[64];
940 struct acpi_buffer buffer = { .length = sizeof(objname),
941 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400942
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200943 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200944 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200945 if (bridge)
946 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200948 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
951
952 switch (type) {
953 case ACPI_NOTIFY_BUS_CHECK:
954 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800955 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800956 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200957 if (bridge) {
958 acpiphp_check_bridge(bridge);
959 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
960 ACPI_UINT32_MAX, check_sub_bridges,
961 NULL, NULL, NULL);
962 } else {
963 acpiphp_enable_slot(func->slot);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 break;
966
967 case ACPI_NOTIFY_DEVICE_CHECK:
968 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800969 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200970 if (bridge)
971 acpiphp_check_bridge(bridge);
972 else
973 acpiphp_check_bridge(func->slot->bridge);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 break;
976
977 case ACPI_NOTIFY_DEVICE_WAKE:
978 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800979 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
981
982 case ACPI_NOTIFY_EJECT_REQUEST:
983 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800984 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200985 if (!(acpiphp_disable_slot(func->slot)))
986 acpiphp_eject_slot(func->slot);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989
990 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
991 printk(KERN_ERR "Device %s cannot be configured due"
992 " to a frequency mismatch\n", objname);
993 break;
994
995 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
996 printk(KERN_ERR "Device %s cannot be configured due"
997 " to a bus mode mismatch\n", objname);
998 break;
999
1000 case ACPI_NOTIFY_POWER_FAULT:
1001 printk(KERN_ERR "Device %s has suffered a power fault\n",
1002 objname);
1003 break;
1004
1005 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001006 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1007 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 break;
1009 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001010
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001011 if (bridge)
1012 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001015static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001016{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001017 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001018 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001019
1020 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001021 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001022 acpi_scan_lock_acquire();
1023
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001024 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001025
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001026 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001027 kfree(hp_work); /* allocated in handle_hotplug_event() */
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +02001028 put_bridge(context->func.slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001031/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001032 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001033 * @handle: Notify()'ed acpi_handle
1034 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001035 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001036 *
1037 * Handles ACPI event notification on slots.
1038 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001039static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001040{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001041 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001042
1043 mutex_lock(&acpiphp_context_lock);
1044 context = acpiphp_get_context(handle);
1045 if (context) {
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +02001046 get_bridge(context->func.slot->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001047 acpiphp_put_context(context);
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001048 }
1049 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001050 /*
1051 * Currently the code adds all hotplug events to the kacpid_wq
1052 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1053 * The proper way to fix this is to reorganize the code so that
1054 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1055 * For now just re-add this work to the kacpi_hotplug_wq so we
1056 * don't deadlock on hotplug actions.
1057 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001058 if (context)
1059 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001062/*
1063 * Create hotplug slots for the PCI bus.
1064 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001066void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001068 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001069 acpi_handle handle;
1070 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001072 if (acpiphp_disabled)
1073 return;
1074
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001075 handle = ACPI_HANDLE(bus->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001076 if (!handle)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001077 return;
1078
1079 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001080 if (!bridge) {
1081 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001082 return;
1083 }
1084
Yijing Wangad41dd92013-04-12 05:44:27 +00001085 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001086 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001087 bridge->handle = handle;
1088 bridge->pci_dev = pci_dev_get(bus->self);
1089 bridge->pci_bus = bus;
1090
1091 /*
1092 * Grab a ref to the subordinate PCI bus in case the bus is
1093 * removed via PCI core logical hotplug. The ref pins the bus
1094 * (which we access during module unload).
1095 */
1096 get_device(&bus->dev);
1097
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001098 if (!pci_is_root_bus(bridge->pci_bus)) {
1099 struct acpiphp_context *context;
1100
1101 /*
1102 * This bridge should have been registered as a hotplug function
1103 * under its parent, so the context has to be there. If not, we
1104 * are in deep goo.
1105 */
1106 mutex_lock(&acpiphp_context_lock);
1107 context = acpiphp_get_context(handle);
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +02001108 if (WARN_ON(!context)) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001109 mutex_unlock(&acpiphp_context_lock);
1110 put_device(&bus->dev);
1111 kfree(bridge);
1112 return;
1113 }
1114 bridge->context = context;
1115 context->bridge = bridge;
1116 /* Get a reference to the parent bridge. */
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +02001117 get_bridge(context->func.slot->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001118 mutex_unlock(&acpiphp_context_lock);
1119 }
1120
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001121 /* must be added to the list prior to calling register_slot */
1122 mutex_lock(&bridge_mutex);
1123 list_add(&bridge->list, &bridge_list);
1124 mutex_unlock(&bridge_mutex);
1125
1126 /* register all slot objects under this bridge */
1127 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1128 register_slot, NULL, bridge, NULL);
1129 if (ACPI_FAILURE(status)) {
1130 acpi_handle_err(bridge->handle, "failed to register slots\n");
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001131 cleanup_bridge(bridge);
1132 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001136/* Destroy hotplug slots associated with the PCI bus */
1137void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001139 struct acpiphp_bridge *bridge, *tmp;
1140
1141 if (acpiphp_disabled)
1142 return;
1143
1144 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1145 if (bridge->pci_bus == bus) {
1146 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001147 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001148 break;
1149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/**
1153 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001154 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156int acpiphp_enable_slot(struct acpiphp_slot *slot)
1157{
1158 int retval;
1159
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001160 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* wake up all functions */
1163 retval = power_on_slot(slot);
1164 if (retval)
1165 goto err_exit;
1166
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001167 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* configure all functions */
1169 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001170 if (retval)
1171 power_off_slot(slot);
1172 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001173 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001174 power_off_slot(slot);
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001178 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return retval;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/**
1183 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001184 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 */
1186int acpiphp_disable_slot(struct acpiphp_slot *slot)
1187{
1188 int retval = 0;
1189
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001190 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* unconfigure all functions */
1193 retval = disable_device(slot);
1194 if (retval)
1195 goto err_exit;
1196
1197 /* power off all functions */
1198 retval = power_off_slot(slot);
1199 if (retval)
1200 goto err_exit;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001203 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return retval;
1205}
1206
1207
1208/*
1209 * slot enabled: 1
1210 * slot disabled: 0
1211 */
1212u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1213{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001214 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217
1218/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001219 * latch open: 1
1220 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
1222u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1223{
1224 unsigned int sta;
1225
1226 sta = get_slot_status(slot);
1227
Jiang Liuce15d872013-04-12 05:44:19 +00001228 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231
1232/*
1233 * adapter presence : 1
1234 * absence : 0
1235 */
1236u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1237{
1238 unsigned int sta;
1239
1240 sta = get_slot_status(slot);
1241
1242 return (sta == 0) ? 0 : 1;
1243}