blob: b6691cc8230ba988a8ac95418c36c6bad7ee8520 [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, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800307 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000310 list_for_each_entry(slot, &bridge->slots, node)
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200311 if (slot->device == device)
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200312 goto slot_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200314 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
315 if (!slot) {
316 status = AE_NO_MEMORY;
317 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200320 slot->bus = bridge->pci_bus;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200321 slot->device = device;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200322 INIT_LIST_HEAD(&slot->funcs);
323 mutex_init(&slot->crit_sect);
324
325 mutex_lock(&bridge_mutex);
326 list_add_tail(&slot->node, &bridge->slots);
327 mutex_unlock(&bridge_mutex);
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200328
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200329 /* Register slots for ejectable funtions only. */
330 if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
331 unsigned long long sun;
332 int retval;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200333
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200334 bridge->nr_slots++;
335 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
336 if (ACPI_FAILURE(status))
337 sun = bridge->nr_slots;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200338
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200339 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200340 sun, pci_domain_nr(pbus), pbus->number, device);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200341
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200342 retval = acpiphp_register_hotplug_slot(slot, sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200343 if (retval) {
344 bridge->nr_slots--;
345 if (retval == -EBUSY)
346 warn("Slot %llu already registered by another "
Rafael J. Wysocki73427982013-07-13 23:27:25 +0200347 "hotplug driver\n", sun);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200348 else
349 warn("acpiphp_register_hotplug_slot failed "
350 "(err code = 0x%x)\n", retval);
351 }
352 /* Even if the slot registration fails, we can still use it. */
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200353 }
354
355 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000357 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000359 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000361 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
362 &val, 60*1000))
Rafael J. Wysockibc805a52013-07-13 23:27:26 +0200363 slot->flags |= SLOT_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400365 if (is_dock_device(handle)) {
366 /* we don't want to call this device's _EJ0
367 * because we want the dock notify handler
368 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800369 */
370 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400371 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200372 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200373 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400374 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800378 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200379 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
380 handle_hotplug_event,
381 context);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200382 if (ACPI_FAILURE(status))
383 acpi_handle_err(handle,
384 "failed to install notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200387 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800388
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200389 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200390 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200391 acpiphp_put_context(context);
392 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200393 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Rajesh Shah42f49a62005-04-28 00:25:53 -0700396static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
397{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200398 struct acpiphp_context *context;
399 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600400
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200401 mutex_lock(&acpiphp_context_lock);
402 context = acpiphp_get_context(handle);
403 if (context) {
404 bridge = context->bridge;
405 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000406 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700407
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200408 acpiphp_put_context(context);
409 }
410 mutex_unlock(&acpiphp_context_lock);
411 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Rajesh Shah364d5092005-04-28 00:25:54 -0700414static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Jiang Liu3d54a312013-04-12 05:44:28 +0000416 struct acpiphp_slot *slot;
417 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700418 acpi_status status;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700419
Jiang Liu3d54a312013-04-12 05:44:28 +0000420 list_for_each_entry(slot, &bridge->slots, node) {
421 list_for_each_entry(func, &slot->funcs, sibling) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200422 acpi_handle handle = func_to_handle(func);
423
424 if (is_dock_device(handle))
425 unregister_hotplug_dock_device(handle);
426
Kristen Accardi20416ea2006-02-23 17:56:03 -0800427 if (!(func->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200428 status = acpi_remove_notify_handler(handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200429 ACPI_SYSTEM_NOTIFY,
430 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800431 if (ACPI_FAILURE(status))
432 err("failed to remove notify handler\n");
433 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700434 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800435 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700436 }
437
Jiang Liu3d54a312013-04-12 05:44:28 +0000438 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700439 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000440 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800443/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800444 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800445 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800446 */
447static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
448{
449 struct list_head *tmp;
450 unsigned char max, n;
451
452 /*
453 * pci_bus_max_busnr will return the highest
454 * reserved busnr for all these children.
455 * that is equivalent to the bus->subordinate
456 * value. We don't want to use the parent's
457 * bus->subordinate value because it could have
458 * padding in it.
459 */
Yinghai Lub918c622012-05-17 18:51:11 -0700460 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800461
462 list_for_each(tmp, &bus->children) {
463 n = pci_bus_max_busnr(pci_bus_b(tmp));
464 if (n > max)
465 max = n;
466 }
467 return max;
468}
469
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800470/**
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200471 * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
472 * @handle: ACPI device object handle to start from.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800473 */
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200474static void acpiphp_bus_trim(acpi_handle handle)
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800475{
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200476 struct acpi_device *adev = NULL;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800477
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200478 acpi_bus_get_device(handle, &adev);
479 if (adev)
480 acpi_bus_trim(adev);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800481}
482
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900483/**
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200484 * acpiphp_bus_add - Scan ACPI namespace subtree.
485 * @handle: ACPI object handle to start the scan from.
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900486 */
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200487static void acpiphp_bus_add(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900488{
Rafael J. Wysockibc805a52013-07-13 23:27:26 +0200489 struct acpi_device *adev = NULL;
490
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200491 acpiphp_bus_trim(handle);
492 acpi_bus_scan(handle);
Rafael J. Wysockibc805a52013-07-13 23:27:26 +0200493 acpi_bus_get_device(handle, &adev);
494 if (adev)
495 acpi_device_set_power(adev, ACPI_STATE_D0);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900496}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800497
Shaohua Lid0607052010-02-25 10:59:34 +0800498static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
499{
500 struct acpiphp_func *func;
501 union acpi_object params[2];
502 struct acpi_object_list arg_list;
503
504 list_for_each_entry(func, &slot->funcs, sibling) {
505 arg_list.count = 2;
506 arg_list.pointer = params;
507 params[0].type = ACPI_TYPE_INTEGER;
508 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
509 params[1].type = ACPI_TYPE_INTEGER;
510 params[1].integer.value = 1;
511 /* _REG is optional, we don't care about if there is failure */
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200512 acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
513 NULL);
Shaohua Lid0607052010-02-25 10:59:34 +0800514 }
515}
516
Yinghai Lu1f96a962013-01-21 13:20:42 -0800517static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
518{
519 struct acpiphp_func *func;
520
Yinghai Lu1f96a962013-01-21 13:20:42 -0800521 /* quirk, or pcie could set it already */
522 if (dev->is_hotplug_bridge)
523 return;
524
Yinghai Lu1f96a962013-01-21 13:20:42 -0800525 list_for_each_entry(func, &slot->funcs, sibling) {
526 if (PCI_FUNC(dev->devfn) == func->function) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200527 dev->is_hotplug_bridge = 1;
Yinghai Lu1f96a962013-01-21 13:20:42 -0800528 break;
529 }
530 }
531}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/**
534 * enable_device - enable, configure a slot
535 * @slot: slot to be enabled
536 *
537 * This function should be called per *physical slot*,
538 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100540static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct pci_dev *dev;
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200543 struct pci_bus *bus = slot->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700545 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200546 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (slot->flags & SLOT_ENABLED)
549 goto err_exit;
550
Jiang Liu2ca344e2013-01-31 00:10:09 +0800551 list_for_each_entry(func, &slot->funcs, sibling)
Rafael J. Wysocki236e2622013-07-13 23:27:25 +0200552 acpiphp_bus_add(func_to_handle(func));
Jiang Liu2ca344e2013-01-31 00:10:09 +0800553
Rajesh Shah42f49a62005-04-28 00:25:53 -0700554 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
555 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800556 /* Maybe only part of funcs are added. */
557 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto err_exit;
559 }
560
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800561 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700562 for (pass = 0; pass < 2; pass++) {
563 list_for_each_entry(dev, &bus->devices, bus_list) {
564 if (PCI_SLOT(dev->devfn) != slot->device)
565 continue;
566 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800567 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700568 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800569 if (pass && dev->subordinate) {
570 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200571 pcibios_resource_survey_bus(dev->subordinate);
572 __pci_bus_size_bridges(dev->subordinate,
573 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800574 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800575 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Jiang Liud66ecb72013-06-23 01:01:35 +0200579 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700580 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600581 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800582 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700583 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100584
585 list_for_each_entry(dev, &bus->devices, bus_list) {
586 /* Assume that newly added devices are powered on already. */
587 if (!dev->is_added)
588 dev->current_state = PCI_D0;
589 }
590
Rajesh Shah42f49a62005-04-28 00:25:53 -0700591 pci_bus_add_devices(bus);
592
Amos Kongf382a082011-11-25 15:03:07 +0800593 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600594 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600595 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
596 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800597 if (!dev) {
598 /* Do not set SLOT_ENABLED flag if some funcs
599 are not added. */
600 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900601 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Amos Kongce29ca32012-05-23 10:20:35 -0600610/* return first device in slot, acquiring a reference on it */
611static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
612{
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200613 struct pci_bus *bus = slot->bus;
Amos Kongce29ca32012-05-23 10:20:35 -0600614 struct pci_dev *dev;
615 struct pci_dev *ret = NULL;
616
617 down_read(&pci_bus_sem);
618 list_for_each_entry(dev, &bus->devices, bus_list)
619 if (PCI_SLOT(dev->devfn) == slot->device) {
620 ret = pci_dev_get(dev);
621 break;
622 }
623 up_read(&pci_bus_sem);
624
625 return ret;
626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
629 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800630 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632static int disable_device(struct acpiphp_slot *slot)
633{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600635 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900636
Amos Kongce29ca32012-05-23 10:20:35 -0600637 /*
638 * enable_device() enumerates all functions in this device via
639 * pci_scan_slot(), whether they have associated ACPI hotplug
640 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
641 * here.
642 */
643 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600644 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600645 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700646 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700647
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200648 list_for_each_entry(func, &slot->funcs, sibling)
649 acpiphp_bus_trim(func_to_handle(func));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 slot->flags &= (~SLOT_ENABLED);
652
Alex Chiang9d911d72009-05-21 16:21:15 -0600653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656
657/**
658 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800659 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800661 * If a slot has _STA for each function and if any one of them
662 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800664 * If a slot doesn't have _STA and if any one of its functions'
665 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800667 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
669static unsigned int get_slot_status(struct acpiphp_slot *slot)
670{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400671 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct acpiphp_func *func;
673
Alex Chiang58c08622009-10-26 21:25:27 -0600674 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (func->flags & FUNC_HAS_STA) {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200676 acpi_status status;
677
678 status = acpi_evaluate_integer(func_to_handle(func),
679 "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ACPI_SUCCESS(status) && sta)
681 break;
682 } else {
Rafael J. Wysocki5a3bc572013-07-13 23:27:25 +0200683 u32 dvid;
684
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200685 pci_bus_read_config_dword(slot->bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 PCI_DEVFN(slot->device,
687 func->function),
688 PCI_VENDOR_ID, &dvid);
689 if (dvid != 0xffffffff) {
690 sta = ACPI_STA_ALL;
691 break;
692 }
693 }
694 }
695
696 return (unsigned int)sta;
697}
698
699/**
700 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800701 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
703 * Iterate over all slots under this bridge and make sure that if a
704 * card is present they are enabled, and if not they are disabled.
705 */
706static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
707{
708 struct acpiphp_slot *slot;
709 int retval = 0;
710 int enabled, disabled;
711
712 enabled = disabled = 0;
713
Yijing Wangad41dd92013-04-12 05:44:27 +0000714 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 unsigned int status = get_slot_status(slot);
716 if (slot->flags & SLOT_ENABLED) {
717 if (status == ACPI_STA_ALL)
718 continue;
Mika Westerbergad21d2d2013-07-13 23:27:26 +0200719
720 retval = acpiphp_disable_and_eject_slot(slot);
721 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto err_exit;
Mika Westerbergad21d2d2013-07-13 23:27:26 +0200723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 disabled++;
725 } else {
726 if (status != ACPI_STA_ALL)
727 continue;
728 retval = acpiphp_enable_slot(slot);
729 if (retval) {
730 err("Error occurred in enabling\n");
731 goto err_exit;
732 }
733 enabled++;
734 }
735 }
736
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800737 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 err_exit:
740 return retval;
741}
742
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600743static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700744{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700745 struct pci_dev *dev;
746
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600747 list_for_each_entry(dev, &bus->devices, bus_list)
748 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700749}
750
751/*
752 * Remove devices for which we could not assign resources, call
753 * arch specific code to fix-up the bus
754 */
755static void acpiphp_sanitize_bus(struct pci_bus *bus)
756{
Yijing Wangd65eba62013-04-12 05:44:17 +0000757 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700758 int i;
759 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
760
Yijing Wangd65eba62013-04-12 05:44:17 +0000761 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700762 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
763 struct resource *res = &dev->resource[i];
764 if ((res->flags & type_mask) && !res->start &&
765 res->end) {
766 /* Could not assign a required resources
767 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800768 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700769 break;
770 }
771 }
772 }
773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
776 * ACPI event handlers
777 */
778
Gary Hade0bbd6422007-07-05 11:10:48 -0700779static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700780check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
781{
782 struct acpiphp_bridge *bridge;
783 char objname[64];
784 struct acpi_buffer buffer = { .length = sizeof(objname),
785 .pointer = objname };
786
787 bridge = acpiphp_handle_to_bridge(handle);
788 if (bridge) {
789 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
790 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800791 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700792 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000793 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700794 }
795 return AE_OK ;
796}
797
Yinghai Lu3f327e32013-05-07 11:06:03 -0600798void acpiphp_check_host_bridge(acpi_handle handle)
799{
800 struct acpiphp_bridge *bridge;
801
802 bridge = acpiphp_handle_to_bridge(handle);
803 if (bridge) {
804 acpiphp_check_bridge(bridge);
805 put_bridge(bridge);
806 }
807
808 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
809 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
810}
811
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200812static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200814 struct acpiphp_context *context = data;
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200815 struct acpiphp_func *func = &context->func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct acpiphp_bridge *bridge;
817 char objname[64];
818 struct acpi_buffer buffer = { .length = sizeof(objname),
819 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400820
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200821 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200822 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200823 if (bridge)
824 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200826 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
829
830 switch (type) {
831 case ACPI_NOTIFY_BUS_CHECK:
832 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800833 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800834 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200835 if (bridge) {
836 acpiphp_check_bridge(bridge);
837 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
838 ACPI_UINT32_MAX, check_sub_bridges,
839 NULL, NULL, NULL);
840 } else {
841 acpiphp_enable_slot(func->slot);
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 break;
844
845 case ACPI_NOTIFY_DEVICE_CHECK:
846 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800847 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200848 if (bridge)
849 acpiphp_check_bridge(bridge);
850 else
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200851 acpiphp_check_bridge(func->parent);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 break;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 case ACPI_NOTIFY_EJECT_REQUEST:
856 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800857 dbg("%s: Device eject notify on %s\n", __func__, objname);
Mika Westerbergad21d2d2013-07-13 23:27:26 +0200858 acpiphp_disable_and_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400861
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200862 if (bridge)
863 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200866static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200867{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200868 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200869 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200870
871 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200872 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200873 acpi_scan_lock_acquire();
874
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200875 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400876
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100877 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200878 kfree(hp_work); /* allocated in handle_hotplug_event() */
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200879 put_bridge(context->func.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400882/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200883 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400884 * @handle: Notify()'ed acpi_handle
885 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200886 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400887 *
888 * Handles ACPI event notification on slots.
889 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200890static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400891{
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200892 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200893
Rafael J. Wysocki5c8d0e12013-07-13 23:27:26 +0200894 switch (type) {
895 case ACPI_NOTIFY_BUS_CHECK:
896 case ACPI_NOTIFY_DEVICE_CHECK:
897 case ACPI_NOTIFY_EJECT_REQUEST:
898 break;
899
900 case ACPI_NOTIFY_DEVICE_WAKE:
901 return;
902
903 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
904 acpi_handle_err(handle, "Device cannot be configured due "
905 "to a frequency mismatch\n");
906 return;
907
908 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
909 acpi_handle_err(handle, "Device cannot be configured due "
910 "to a bus mode mismatch\n");
911 return;
912
913 case ACPI_NOTIFY_POWER_FAULT:
914 acpi_handle_err(handle, "Device has suffered a power fault\n");
915 return;
916
917 default:
918 acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
919 return;
920 }
921
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200922 mutex_lock(&acpiphp_context_lock);
923 context = acpiphp_get_context(handle);
924 if (context) {
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200925 get_bridge(context->func.parent);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200926 acpiphp_put_context(context);
Rafael J. Wysocki5c8d0e12013-07-13 23:27:26 +0200927 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200928 }
929 mutex_unlock(&acpiphp_context_lock);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000932/*
933 * Create hotplug slots for the PCI bus.
934 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +0200936void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000938 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +0200939 acpi_handle handle;
940 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000942 if (acpiphp_disabled)
943 return;
944
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +0200945 handle = ACPI_HANDLE(bus->bridge);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200946 if (!handle)
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000947 return;
948
949 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200950 if (!bridge) {
951 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000952 return;
953 }
954
Yijing Wangad41dd92013-04-12 05:44:27 +0000955 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +0000956 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000957 bridge->pci_dev = pci_dev_get(bus->self);
958 bridge->pci_bus = bus;
959
960 /*
961 * Grab a ref to the subordinate PCI bus in case the bus is
962 * removed via PCI core logical hotplug. The ref pins the bus
963 * (which we access during module unload).
964 */
965 get_device(&bus->dev);
966
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200967 if (!pci_is_root_bus(bridge->pci_bus)) {
968 struct acpiphp_context *context;
969
970 /*
971 * This bridge should have been registered as a hotplug function
972 * under its parent, so the context has to be there. If not, we
973 * are in deep goo.
974 */
975 mutex_lock(&acpiphp_context_lock);
976 context = acpiphp_get_context(handle);
Rafael J. Wysockibd4674d2013-07-13 23:27:25 +0200977 if (WARN_ON(!context)) {
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200978 mutex_unlock(&acpiphp_context_lock);
979 put_device(&bus->dev);
980 kfree(bridge);
981 return;
982 }
983 bridge->context = context;
984 context->bridge = bridge;
985 /* Get a reference to the parent bridge. */
Rafael J. Wysockibda46db2013-07-13 23:27:25 +0200986 get_bridge(context->func.parent);
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +0200987 mutex_unlock(&acpiphp_context_lock);
988 }
989
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +0200990 /* must be added to the list prior to calling register_slot */
991 mutex_lock(&bridge_mutex);
992 list_add(&bridge->list, &bridge_list);
993 mutex_unlock(&bridge_mutex);
994
995 /* register all slot objects under this bridge */
Rafael J. Wysocki89373a52013-07-13 23:27:25 +0200996 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +0200997 register_slot, NULL, bridge, NULL);
998 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki89373a52013-07-13 23:27:25 +0200999 acpi_handle_err(handle, "failed to register slots\n");
Rafael J. Wysockibbd34fc2013-07-13 23:27:24 +02001000 cleanup_bridge(bridge);
1001 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001005/* Destroy hotplug slots associated with the PCI bus */
1006void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001008 struct acpiphp_bridge *bridge, *tmp;
1009
1010 if (acpiphp_disabled)
1011 return;
1012
1013 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1014 if (bridge->pci_bus == bus) {
1015 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001016 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001017 break;
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021/**
1022 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001023 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025int acpiphp_enable_slot(struct acpiphp_slot *slot)
1026{
1027 int retval;
1028
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001029 mutex_lock(&slot->crit_sect);
Rafael J. Wysockibc805a52013-07-13 23:27:26 +02001030 /* configure all functions */
1031 retval = enable_device(slot);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001032 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return retval;
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/**
Mika Westerbergad21d2d2013-07-13 23:27:26 +02001037 * acpiphp_disable_and_eject_slot - power off and eject slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001038 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Mika Westerbergad21d2d2013-07-13 23:27:26 +02001040int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Mika Westerbergad21d2d2013-07-13 23:27:26 +02001042 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int retval = 0;
1044
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001045 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* unconfigure all functions */
1048 retval = disable_device(slot);
1049 if (retval)
1050 goto err_exit;
1051
Mika Westerbergad21d2d2013-07-13 23:27:26 +02001052 list_for_each_entry(func, &slot->funcs, sibling)
1053 if (func->flags & FUNC_HAS_EJ0) {
1054 acpi_handle handle = func_to_handle(func);
1055
1056 if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
1057 acpi_handle_err(handle, "_EJ0 failed\n");
1058
1059 break;
1060 }
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001063 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return retval;
1065}
1066
1067
1068/*
1069 * slot enabled: 1
1070 * slot disabled: 0
1071 */
1072u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1073{
Rafael J. Wysockibc805a52013-07-13 23:27:26 +02001074 return (slot->flags & SLOT_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077
1078/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001079 * latch open: 1
1080 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
1082u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1083{
1084 unsigned int sta;
1085
1086 sta = get_slot_status(slot);
1087
Jiang Liuce15d872013-04-12 05:44:19 +00001088 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091
1092/*
1093 * adapter presence : 1
1094 * absence : 0
1095 */
1096u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1097{
1098 unsigned int sta;
1099
1100 sta = get_slot_status(slot);
1101
1102 return (sta == 0) ? 0 : 1;
1103}