blob: f131512b7bba20e22d2053de4846bb88e886d042 [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. Wysockibda46db2013-07-13 23:27:25 +0200168 put_bridge(context->func.parent);
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. Wysockibda46db2013-07-13 23:27:25 +0200190 struct pci_bus *bus = context->func.slot->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. Wysockibda46db2013-07-13 23:27:25 +0200251 get_bridge(context->func.parent);
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. Wysockibda46db2013-07-13 23:27:25 +0200258 put_bridge(context->func.parent);
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. Wysockibda46db2013-07-13 23:27:25 +0200297 newfunc->parent = bridge;
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200298 mutex_unlock(&acpiphp_context_lock);
299
Jiang Liuecd046d2013-06-29 00:24:43 +0800300 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800301 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jiang Liuecd046d2013-06-29 00:24:43 +0800303 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 newfunc->flags |= FUNC_HAS_STA;
305
Jiang Liuecd046d2013-06-29 00:24:43 +0800306 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 newfunc->flags |= FUNC_HAS_PS0;
308
Jiang Liuecd046d2013-06-29 00:24:43 +0800309 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 newfunc->flags |= FUNC_HAS_PS3;
311
Jiang Liuecd046d2013-06-29 00:24:43 +0800312 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800313 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000316 list_for_each_entry(slot, &bridge->slots, node)
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200317 if (slot->device == device)
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200318 goto slot_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200320 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
321 if (!slot) {
322 status = AE_NO_MEMORY;
323 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200326 slot->bus = bridge->pci_bus;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200327 slot->device = device;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200328 INIT_LIST_HEAD(&slot->funcs);
329 mutex_init(&slot->crit_sect);
330
331 mutex_lock(&bridge_mutex);
332 list_add_tail(&slot->node, &bridge->slots);
333 mutex_unlock(&bridge_mutex);
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200334
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200335 /* Register slots for ejectable funtions only. */
336 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
337 unsigned long long sun;
338 int retval;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200339
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200340 bridge->nr_slots++;
341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
342 if (ACPI_FAILURE(status))
343 sun = bridge->nr_slots;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200344
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200345 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200346 sun, pci_domain_nr(pbus), pbus->number, device);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200347
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200348 retval = acpiphp_register_hotplug_slot(slot, sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200349 if (retval) {
350 bridge->nr_slots--;
351 if (retval == -EBUSY)
352 warn("Slot %llu already registered by another "
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200353 "hotplug driver\n", sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200354 else
355 warn("acpiphp_register_hotplug_slot failed "
356 "(err code = 0x%x)\n", retval);
357 }
358 /* Even if the slot registration fails, we can still use it. */
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200359 }
360
361 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000363 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000365 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000367 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
368 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400371 if (is_dock_device(handle)) {
372 /* we don't want to call this device's _EJ0
373 * because we want the dock notify handler
374 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800375 */
376 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400377 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200378 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200379 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400380 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800381 }
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800384 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200385 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
386 handle_hotplug_event,
387 context);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200388 if (ACPI_FAILURE(status))
389 acpi_handle_err(handle,
390 "failed to install notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200393 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800394
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200395 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200396 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200397 acpiphp_put_context(context);
398 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200399 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Rajesh Shah42f49a62005-04-28 00:25:53 -0700402static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
403{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200404 struct acpiphp_context *context;
405 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600406
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200407 mutex_lock(&acpiphp_context_lock);
408 context = acpiphp_get_context(handle);
409 if (context) {
410 bridge = context->bridge;
411 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000412 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700413
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200414 acpiphp_put_context(context);
415 }
416 mutex_unlock(&acpiphp_context_lock);
417 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Rajesh Shah364d5092005-04-28 00:25:54 -0700420static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jiang Liu3d54a312013-04-12 05:44:28 +0000422 struct acpiphp_slot *slot;
423 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700424 acpi_status status;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700425
Jiang Liu3d54a312013-04-12 05:44:28 +0000426 list_for_each_entry(slot, &bridge->slots, node) {
427 list_for_each_entry(func, &slot->funcs, sibling) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200428 acpi_handle handle = func_to_handle(func);
429
430 if (is_dock_device(handle))
431 unregister_hotplug_dock_device(handle);
432
Kristen Accardi20416ea2006-02-23 17:56:03 -0800433 if (!(func->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200434 status = acpi_remove_notify_handler(handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200435 ACPI_SYSTEM_NOTIFY,
436 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800437 if (ACPI_FAILURE(status))
438 err("failed to remove notify handler\n");
439 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800441 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700442 }
443
Jiang Liu3d54a312013-04-12 05:44:28 +0000444 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700445 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000446 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static int power_on_slot(struct acpiphp_slot *slot)
450{
451 acpi_status status;
452 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int retval = 0;
454
455 /* if already enabled, just skip */
456 if (slot->flags & SLOT_POWEREDON)
457 goto err_exit;
458
Alex Chiang58c08622009-10-26 21:25:27 -0600459 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800461 dbg("%s: executing _PS0\n", __func__);
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200462 status = acpi_evaluate_object(func_to_handle(func),
463 "_PS0", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800465 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 retval = -1;
467 goto err_exit;
468 } else
469 break;
470 }
471 }
472
473 /* TBD: evaluate _STA to check if the slot is enabled */
474
475 slot->flags |= SLOT_POWEREDON;
476
477 err_exit:
478 return retval;
479}
480
481
482static int power_off_slot(struct acpiphp_slot *slot)
483{
484 acpi_status status;
485 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 int retval = 0;
488
489 /* if already disabled, just skip */
490 if ((slot->flags & SLOT_POWEREDON) == 0)
491 goto err_exit;
492
Alex Chiang58c08622009-10-26 21:25:27 -0600493 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700494 if (func->flags & FUNC_HAS_PS3) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200495 status = acpi_evaluate_object(func_to_handle(func),
496 "_PS3", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800498 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 retval = -1;
500 goto err_exit;
501 } else
502 break;
503 }
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* TBD: evaluate _STA to check if the slot is disabled */
507
508 slot->flags &= (~SLOT_POWEREDON);
509
510 err_exit:
511 return retval;
512}
513
514
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800515
516/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800517 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800518 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800519 */
520static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
521{
522 struct list_head *tmp;
523 unsigned char max, n;
524
525 /*
526 * pci_bus_max_busnr will return the highest
527 * reserved busnr for all these children.
528 * that is equivalent to the bus->subordinate
529 * value. We don't want to use the parent's
530 * bus->subordinate value because it could have
531 * padding in it.
532 */
Yinghai Lub918c622012-05-17 18:51:11 -0700533 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800534
535 list_for_each(tmp, &bus->children) {
536 n = pci_bus_max_busnr(pci_bus_b(tmp));
537 if (n > max)
538 max = n;
539 }
540 return max;
541}
542
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800543/**
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200544 * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
545 * @handle: ACPI device object handle to start from.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800546 */
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200547static void acpiphp_bus_trim(acpi_handle handle)
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800548{
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200549 struct acpi_device *adev = NULL;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800550
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200551 acpi_bus_get_device(handle, &adev);
552 if (adev)
553 acpi_bus_trim(adev);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800554}
555
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900556/**
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200557 * acpiphp_bus_add - Scan ACPI namespace subtree.
558 * @handle: ACPI object handle to start the scan from.
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900559 */
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200560static void acpiphp_bus_add(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900561{
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200562 acpiphp_bus_trim(handle);
563 acpi_bus_scan(handle);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900564}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800565
Shaohua Lid0607052010-02-25 10:59:34 +0800566static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
567{
568 struct acpiphp_func *func;
569 union acpi_object params[2];
570 struct acpi_object_list arg_list;
571
572 list_for_each_entry(func, &slot->funcs, sibling) {
573 arg_list.count = 2;
574 arg_list.pointer = params;
575 params[0].type = ACPI_TYPE_INTEGER;
576 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
577 params[1].type = ACPI_TYPE_INTEGER;
578 params[1].integer.value = 1;
579 /* _REG is optional, we don't care about if there is failure */
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200580 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
581 NULL);
Shaohua Lid0607052010-02-25 10:59:34 +0800582 }
583}
584
Yinghai Lu1f96a962013-01-21 13:20:42 -0800585static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
586{
587 struct acpiphp_func *func;
588
Yinghai Lu1f96a962013-01-21 13:20:42 -0800589 /* quirk, or pcie could set it already */
590 if (dev->is_hotplug_bridge)
591 return;
592
Yinghai Lu1f96a962013-01-21 13:20:42 -0800593 list_for_each_entry(func, &slot->funcs, sibling) {
594 if (PCI_FUNC(dev->devfn) == func->function) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200595 dev->is_hotplug_bridge = 1;
Yinghai Lu1f96a962013-01-21 13:20:42 -0800596 break;
597 }
598 }
599}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
602 * enable_device - enable, configure a slot
603 * @slot: slot to be enabled
604 *
605 * This function should be called per *physical slot*,
606 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100608static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 struct pci_dev *dev;
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200611 struct pci_bus *bus = slot->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700613 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200614 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (slot->flags & SLOT_ENABLED)
617 goto err_exit;
618
Jiang Liu2ca344e2013-01-31 00:10:09 +0800619 list_for_each_entry(func, &slot->funcs, sibling)
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200620 acpiphp_bus_add(func_to_handle(func));
Jiang Liu2ca344e2013-01-31 00:10:09 +0800621
Rajesh Shah42f49a62005-04-28 00:25:53 -0700622 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
623 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800624 /* Maybe only part of funcs are added. */
625 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto err_exit;
627 }
628
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800629 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700630 for (pass = 0; pass < 2; pass++) {
631 list_for_each_entry(dev, &bus->devices, bus_list) {
632 if (PCI_SLOT(dev->devfn) != slot->device)
633 continue;
634 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800635 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700636 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800637 if (pass && dev->subordinate) {
638 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200639 pcibios_resource_survey_bus(dev->subordinate);
640 __pci_bus_size_bridges(dev->subordinate,
641 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800642 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800643 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Jiang Liud66ecb72013-06-23 01:01:35 +0200647 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700648 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600649 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800650 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700651 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100652
653 list_for_each_entry(dev, &bus->devices, bus_list) {
654 /* Assume that newly added devices are powered on already. */
655 if (!dev->is_added)
656 dev->current_state = PCI_D0;
657 }
658
Rajesh Shah42f49a62005-04-28 00:25:53 -0700659 pci_bus_add_devices(bus);
660
Amos Kongf382a082011-11-25 15:03:07 +0800661 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600662 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600663 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
664 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800665 if (!dev) {
666 /* Do not set SLOT_ENABLED flag if some funcs
667 are not added. */
668 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900669 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Amos Kongce29ca32012-05-23 10:20:35 -0600678/* return first device in slot, acquiring a reference on it */
679static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
680{
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200681 struct pci_bus *bus = slot->bus;
Amos Kongce29ca32012-05-23 10:20:35 -0600682 struct pci_dev *dev;
683 struct pci_dev *ret = NULL;
684
685 down_read(&pci_bus_sem);
686 list_for_each_entry(dev, &bus->devices, bus_list)
687 if (PCI_SLOT(dev->devfn) == slot->device) {
688 ret = pci_dev_get(dev);
689 break;
690 }
691 up_read(&pci_bus_sem);
692
693 return ret;
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/**
697 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800698 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
700static int disable_device(struct acpiphp_slot *slot)
701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600703 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900704
Amos Kongce29ca32012-05-23 10:20:35 -0600705 /*
706 * enable_device() enumerates all functions in this device via
707 * pci_scan_slot(), whether they have associated ACPI hotplug
708 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
709 * here.
710 */
711 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600712 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600713 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700714 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700715
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200716 list_for_each_entry(func, &slot->funcs, sibling)
717 acpiphp_bus_trim(func_to_handle(func));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 slot->flags &= (~SLOT_ENABLED);
720
Alex Chiang9d911d72009-05-21 16:21:15 -0600721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724
725/**
726 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800727 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800729 * If a slot has _STA for each function and if any one of them
730 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800732 * If a slot doesn't have _STA and if any one of its functions'
733 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800735 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 */
737static unsigned int get_slot_status(struct acpiphp_slot *slot)
738{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400739 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 struct acpiphp_func *func;
741
Alex Chiang58c08622009-10-26 21:25:27 -0600742 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (func->flags & FUNC_HAS_STA) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200744 acpi_status status;
745
746 status = acpi_evaluate_integer(func_to_handle(func),
747 "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (ACPI_SUCCESS(status) && sta)
749 break;
750 } else {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200751 u32 dvid;
752
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200753 pci_bus_read_config_dword(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 PCI_DEVFN(slot->device,
755 func->function),
756 PCI_VENDOR_ID, &dvid);
757 if (dvid != 0xffffffff) {
758 sta = ACPI_STA_ALL;
759 break;
760 }
761 }
762 }
763
764 return (unsigned int)sta;
765}
766
767/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700768 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800769 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700770 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700771int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700772{
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700773 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700774
Alex Chiang58c08622009-10-26 21:25:27 -0600775 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700776 /* We don't want to call _EJ0 on non-existing functions. */
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200777 if (!(func->flags & FUNC_HAS_EJ0))
778 continue;
779
780 if (ACPI_FAILURE(acpi_evaluate_ej0(func_to_handle(func))))
781 return -1;
782 else
783 break;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700784 }
785 return 0;
786}
787
788/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800790 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
792 * Iterate over all slots under this bridge and make sure that if a
793 * card is present they are enabled, and if not they are disabled.
794 */
795static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
796{
797 struct acpiphp_slot *slot;
798 int retval = 0;
799 int enabled, disabled;
800
801 enabled = disabled = 0;
802
Yijing Wangad41dd92013-04-12 05:44:27 +0000803 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 unsigned int status = get_slot_status(slot);
805 if (slot->flags & SLOT_ENABLED) {
806 if (status == ACPI_STA_ALL)
807 continue;
808 retval = acpiphp_disable_slot(slot);
809 if (retval) {
810 err("Error occurred in disabling\n");
811 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700812 } else {
813 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 disabled++;
816 } else {
817 if (status != ACPI_STA_ALL)
818 continue;
819 retval = acpiphp_enable_slot(slot);
820 if (retval) {
821 err("Error occurred in enabling\n");
822 goto err_exit;
823 }
824 enabled++;
825 }
826 }
827
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800828 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 err_exit:
831 return retval;
832}
833
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600834static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700835{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700836 struct pci_dev *dev;
837
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600838 list_for_each_entry(dev, &bus->devices, bus_list)
839 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700840}
841
842/*
843 * Remove devices for which we could not assign resources, call
844 * arch specific code to fix-up the bus
845 */
846static void acpiphp_sanitize_bus(struct pci_bus *bus)
847{
Yijing Wangd65eba62013-04-12 05:44:17 +0000848 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700849 int i;
850 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
851
Yijing Wangd65eba62013-04-12 05:44:17 +0000852 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700853 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
854 struct resource *res = &dev->resource[i];
855 if ((res->flags & type_mask) && !res->start &&
856 res->end) {
857 /* Could not assign a required resources
858 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800859 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700860 break;
861 }
862 }
863 }
864}
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866/*
867 * ACPI event handlers
868 */
869
Gary Hade0bbd6422007-07-05 11:10:48 -0700870static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700871check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
872{
873 struct acpiphp_bridge *bridge;
874 char objname[64];
875 struct acpi_buffer buffer = { .length = sizeof(objname),
876 .pointer = objname };
877
878 bridge = acpiphp_handle_to_bridge(handle);
879 if (bridge) {
880 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
881 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800882 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700883 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000884 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700885 }
886 return AE_OK ;
887}
888
Yinghai Lu3f327e32013-05-07 11:06:03 -0600889void acpiphp_check_host_bridge(acpi_handle handle)
890{
891 struct acpiphp_bridge *bridge;
892
893 bridge = acpiphp_handle_to_bridge(handle);
894 if (bridge) {
895 acpiphp_check_bridge(bridge);
896 put_bridge(bridge);
897 }
898
899 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
900 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
901}
902
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200903static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200905 struct acpiphp_context *context = data;
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200906 struct acpiphp_func *func = &context->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct acpiphp_bridge *bridge;
908 char objname[64];
909 struct acpi_buffer buffer = { .length = sizeof(objname),
910 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400911
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200912 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200913 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200914 if (bridge)
915 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200917 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
920
921 switch (type) {
922 case ACPI_NOTIFY_BUS_CHECK:
923 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800924 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800925 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200926 if (bridge) {
927 acpiphp_check_bridge(bridge);
928 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
929 ACPI_UINT32_MAX, check_sub_bridges,
930 NULL, NULL, NULL);
931 } else {
932 acpiphp_enable_slot(func->slot);
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935
936 case ACPI_NOTIFY_DEVICE_CHECK:
937 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800938 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200939 if (bridge)
940 acpiphp_check_bridge(bridge);
941 else
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200942 acpiphp_check_bridge(func->parent);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 break;
945
946 case ACPI_NOTIFY_DEVICE_WAKE:
947 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800948 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950
951 case ACPI_NOTIFY_EJECT_REQUEST:
952 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800953 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200954 if (!(acpiphp_disable_slot(func->slot)))
955 acpiphp_eject_slot(func->slot);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958
959 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
960 printk(KERN_ERR "Device %s cannot be configured due"
961 " to a frequency mismatch\n", objname);
962 break;
963
964 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
965 printk(KERN_ERR "Device %s cannot be configured due"
966 " to a bus mode mismatch\n", objname);
967 break;
968
969 case ACPI_NOTIFY_POWER_FAULT:
970 printk(KERN_ERR "Device %s has suffered a power fault\n",
971 objname);
972 break;
973
974 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200975 warn("notify_handler: unknown event type 0x%x for %s\n", type,
976 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 break;
978 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400979
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200980 if (bridge)
981 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200984static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200985{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200986 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200987 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200988
989 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200990 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200991 acpi_scan_lock_acquire();
992
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200993 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400994
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100995 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200996 kfree(hp_work); /* allocated in handle_hotplug_event() */
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200997 put_bridge(context->func.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001000/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001001 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001002 * @handle: Notify()'ed acpi_handle
1003 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001004 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001005 *
1006 * Handles ACPI event notification on slots.
1007 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001008static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001009{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001010 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001011
1012 mutex_lock(&acpiphp_context_lock);
1013 context = acpiphp_get_context(handle);
1014 if (context) {
Rafael J. Wysockibda46db2013-07-13 23:27:25 +02001015 get_bridge(context->func.parent);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001016 acpiphp_put_context(context);
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001017 }
1018 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001019 /*
1020 * Currently the code adds all hotplug events to the kacpid_wq
1021 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1022 * The proper way to fix this is to reorganize the code so that
1023 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1024 * For now just re-add this work to the kacpi_hotplug_wq so we
1025 * don't deadlock on hotplug actions.
1026 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001027 if (context)
1028 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001031/*
1032 * Create hotplug slots for the PCI bus.
1033 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001035void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001037 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001038 acpi_handle handle;
1039 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001041 if (acpiphp_disabled)
1042 return;
1043
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001044 handle = ACPI_HANDLE(bus->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001045 if (!handle)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001046 return;
1047
1048 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001049 if (!bridge) {
1050 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001051 return;
1052 }
1053
Yijing Wangad41dd92013-04-12 05:44:27 +00001054 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001055 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001056 bridge->pci_dev = pci_dev_get(bus->self);
1057 bridge->pci_bus = bus;
1058
1059 /*
1060 * Grab a ref to the subordinate PCI bus in case the bus is
1061 * removed via PCI core logical hotplug. The ref pins the bus
1062 * (which we access during module unload).
1063 */
1064 get_device(&bus->dev);
1065
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001066 if (!pci_is_root_bus(bridge->pci_bus)) {
1067 struct acpiphp_context *context;
1068
1069 /*
1070 * This bridge should have been registered as a hotplug function
1071 * under its parent, so the context has to be there. If not, we
1072 * are in deep goo.
1073 */
1074 mutex_lock(&acpiphp_context_lock);
1075 context = acpiphp_get_context(handle);
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +02001076 if (WARN_ON(!context)) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001077 mutex_unlock(&acpiphp_context_lock);
1078 put_device(&bus->dev);
1079 kfree(bridge);
1080 return;
1081 }
1082 bridge->context = context;
1083 context->bridge = bridge;
1084 /* Get a reference to the parent bridge. */
Rafael J. Wysockibda46db2013-07-13 23:27:25 +02001085 get_bridge(context->func.parent);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001086 mutex_unlock(&acpiphp_context_lock);
1087 }
1088
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001089 /* must be added to the list prior to calling register_slot */
1090 mutex_lock(&bridge_mutex);
1091 list_add(&bridge->list, &bridge_list);
1092 mutex_unlock(&bridge_mutex);
1093
1094 /* register all slot objects under this bridge */
Rafael J. Wysocki89373a52013-07-13 23:27:25 +02001095 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001096 register_slot, NULL, bridge, NULL);
1097 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki89373a52013-07-13 23:27:25 +02001098 acpi_handle_err(handle, "failed to register slots\n");
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001099 cleanup_bridge(bridge);
1100 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001104/* Destroy hotplug slots associated with the PCI bus */
1105void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001107 struct acpiphp_bridge *bridge, *tmp;
1108
1109 if (acpiphp_disabled)
1110 return;
1111
1112 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1113 if (bridge->pci_bus == bus) {
1114 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001115 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001116 break;
1117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120/**
1121 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001122 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
1124int acpiphp_enable_slot(struct acpiphp_slot *slot)
1125{
1126 int retval;
1127
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001128 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* wake up all functions */
1131 retval = power_on_slot(slot);
1132 if (retval)
1133 goto err_exit;
1134
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001135 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /* configure all functions */
1137 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001138 if (retval)
1139 power_off_slot(slot);
1140 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001141 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001142 power_off_slot(slot);
1143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001146 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return retval;
1148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/**
1151 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001152 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
1154int acpiphp_disable_slot(struct acpiphp_slot *slot)
1155{
1156 int retval = 0;
1157
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001158 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /* unconfigure all functions */
1161 retval = disable_device(slot);
1162 if (retval)
1163 goto err_exit;
1164
1165 /* power off all functions */
1166 retval = power_off_slot(slot);
1167 if (retval)
1168 goto err_exit;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001171 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return retval;
1173}
1174
1175
1176/*
1177 * slot enabled: 1
1178 * slot disabled: 0
1179 */
1180u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1181{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001182 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
1185
1186/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001187 * latch open: 1
1188 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 */
1190u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1191{
1192 unsigned int sta;
1193
1194 sta = get_slot_status(slot);
1195
Jiang Liuce15d872013-04-12 05:44:19 +00001196 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199
1200/*
1201 * adapter presence : 1
1202 * absence : 0
1203 */
1204u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1205{
1206 unsigned int sta;
1207
1208 sta = get_slot_status(slot);
1209
1210 return (sta == 0) ? 0 : 1;
1211}