blob: a0c518c2185e6295bc1cefe87e88500a7a013d6b [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
62static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
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. Wysocki21a31012013-06-24 11:22:53 +020065static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
Len Brown2b85e132006-06-27 01:50:14 -040066static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Jiang Liu3d54a312013-04-12 05:44:28 +000067static void free_bridge(struct kref *kref);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070068
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090069/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040070static acpi_status
71is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
72{
73 int *count = (int *)context;
74
75 if (is_dock_device(handle)) {
76 (*count)++;
77 return AE_CTRL_TERMINATE;
78 } else {
79 return AE_OK;
80 }
81}
82
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +020083static void acpiphp_context_handler(acpi_handle handle, void *context)
84{
85 /* Intentionally empty. */
86}
87
88/**
89 * acpiphp_init_context - Create hotplug context and grab a reference to it.
90 * @handle: ACPI object handle to create the context for.
91 *
92 * Call under acpiphp_context_lock.
93 */
94static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
95{
96 struct acpiphp_context *context;
97 acpi_status status;
98
99 context = kzalloc(sizeof(*context), GFP_KERNEL);
100 if (!context)
101 return NULL;
102
103 context->handle = handle;
104 context->refcount = 1;
105 status = acpi_attach_data(handle, acpiphp_context_handler, context);
106 if (ACPI_FAILURE(status)) {
107 kfree(context);
108 return NULL;
109 }
110 return context;
111}
112
113/**
114 * acpiphp_get_context - Get hotplug context and grab a reference to it.
115 * @handle: ACPI object handle to get the context for.
116 *
117 * Call under acpiphp_context_lock.
118 */
119static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
120{
121 struct acpiphp_context *context = NULL;
122 acpi_status status;
123 void *data;
124
125 status = acpi_get_data(handle, acpiphp_context_handler, &data);
126 if (ACPI_SUCCESS(status)) {
127 context = data;
128 context->refcount++;
129 }
130 return context;
131}
132
133/**
134 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
135 * @handle: ACPI object handle to put the context for.
136 *
137 * The context object is removed if there are no more references to it.
138 *
139 * Call under acpiphp_context_lock.
140 */
141static void acpiphp_put_context(struct acpiphp_context *context)
142{
143 if (--context->refcount)
144 return;
145
146 WARN_ON(context->func || context->bridge);
147 acpi_detach_data(context->handle, acpiphp_context_handler);
148 kfree(context);
149}
150
Jiang Liu3d54a312013-04-12 05:44:28 +0000151static inline void get_bridge(struct acpiphp_bridge *bridge)
152{
153 kref_get(&bridge->ref);
154}
155
156static inline void put_bridge(struct acpiphp_bridge *bridge)
157{
158 kref_put(&bridge->ref, free_bridge);
159}
160
161static void free_bridge(struct kref *kref)
162{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200163 struct acpiphp_context *context;
Jiang Liu3d54a312013-04-12 05:44:28 +0000164 struct acpiphp_bridge *bridge;
165 struct acpiphp_slot *slot, *next;
166 struct acpiphp_func *func, *tmp;
167
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200168 mutex_lock(&acpiphp_context_lock);
169
Jiang Liu3d54a312013-04-12 05:44:28 +0000170 bridge = container_of(kref, struct acpiphp_bridge, ref);
171
172 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
173 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200174 context = func->context;
175 context->func = NULL;
176 acpiphp_put_context(context);
Jiang Liu3d54a312013-04-12 05:44:28 +0000177 kfree(func);
178 }
179 kfree(slot);
180 }
181
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200182 /* Release the reference acquired by acpiphp_enumerate_slots(). */
Jiang Liu3d54a312013-04-12 05:44:28 +0000183 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
184 put_bridge(bridge->func->slot->bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200185
Jiang Liu3d54a312013-04-12 05:44:28 +0000186 put_device(&bridge->pci_bus->dev);
187 pci_dev_put(bridge->pci_dev);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200188 context = bridge->context;
189 context->bridge = NULL;
190 acpiphp_put_context(context);
Jiang Liu3d54a312013-04-12 05:44:28 +0000191 kfree(bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200192
193 mutex_unlock(&acpiphp_context_lock);
Jiang Liu3d54a312013-04-12 05:44:28 +0000194}
195
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400196/*
197 * the _DCK method can do funny things... and sometimes not
198 * hah-hah funny.
199 *
200 * TBD - figure out a way to only call fixups for
201 * systems that require them.
202 */
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200203static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400204{
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200205 struct acpiphp_func *func = data;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400206 struct pci_bus *bus = func->slot->bridge->pci_bus;
207 u32 buses;
208
209 if (!bus->self)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200210 return;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400211
212 /* fixup bad _DCK function that rewrites
213 * secondary bridge on slot
214 */
215 pci_read_config_dword(bus->self,
216 PCI_PRIMARY_BUS,
217 &buses);
218
Yinghai Lub918c622012-05-17 18:51:11 -0700219 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400220 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700221 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700222 | ((unsigned int)(bus->busn_res.start) << 8)
223 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400224 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
225 }
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400226}
227
228
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400229static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200230 .fixup = post_dock_fixups,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200231 .handler = hotplug_event_func,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800232};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jiang Liu5ba113f2012-08-22 23:16:45 +0800234/* Check whether the PCI device is managed by native PCIe hotplug driver */
235static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
236{
237 u32 reg32;
238 acpi_handle tmp;
239 struct acpi_pci_root *root;
240
241 /* Check whether the PCIe port supports native PCIe hotplug */
242 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
243 return false;
244 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
245 return false;
246
247 /*
248 * Check whether native PCIe hotplug has been enabled for
249 * this PCIe hierarchy.
250 */
251 tmp = acpi_find_root_bridge_handle(pdev);
252 if (!tmp)
253 return false;
254 root = acpi_pci_find_root(tmp);
255 if (!root)
256 return false;
257 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
258 return false;
259
260 return true;
261}
262
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200263static void acpiphp_dock_init(void *data)
264{
265 struct acpiphp_func *func = data;
266
267 get_bridge(func->slot->bridge);
268}
269
270static void acpiphp_dock_release(void *data)
271{
272 struct acpiphp_func *func = data;
273
274 put_bridge(func->slot->bridge);
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* callback routine to register each ACPI PCI slot object */
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200278static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
279 void **rv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200281 struct acpiphp_bridge *bridge = data;
282 struct acpiphp_context *context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct acpiphp_slot *slot;
284 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400286 unsigned long long adr, sun;
Yijing Wangad41dd92013-04-12 05:44:27 +0000287 int device, function, retval, found = 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900288 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600289 struct pci_dev *pdev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000290 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900292 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return AE_OK;
294
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600295 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
296 if (ACPI_FAILURE(status)) {
297 warn("can't evaluate _ADR (%#x)\n", status);
298 return AE_OK;
299 }
300
301 device = (adr >> 16) & 0xffff;
302 function = adr & 0xffff;
303
Jiang Liu3d54a312013-04-12 05:44:28 +0000304 pdev = bridge->pci_dev;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800305 if (pdev && device_is_managed_by_native_pciehp(pdev))
306 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100307
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100308 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!newfunc)
310 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 newfunc->handle = handle;
313 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000314
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200315 mutex_lock(&acpiphp_context_lock);
316 context = acpiphp_init_context(handle);
317 if (!context) {
318 mutex_unlock(&acpiphp_context_lock);
319 acpi_handle_err(handle, "No hotplug context\n");
320 kfree(newfunc);
321 return AE_NOT_EXIST;
322 }
323 newfunc->context = context;
324 context->func = newfunc;
325 mutex_unlock(&acpiphp_context_lock);
326
Jiang Liuecd046d2013-06-29 00:24:43 +0800327 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800328 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Jiang Liuecd046d2013-06-29 00:24:43 +0800330 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 newfunc->flags |= FUNC_HAS_STA;
332
Jiang Liuecd046d2013-06-29 00:24:43 +0800333 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 newfunc->flags |= FUNC_HAS_PS0;
335
Jiang Liuecd046d2013-06-29 00:24:43 +0800336 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 newfunc->flags |= FUNC_HAS_PS3;
338
Jiang Liuecd046d2013-06-29 00:24:43 +0800339 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800340 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400343 if (ACPI_FAILURE(status)) {
344 /*
345 * use the count of the number of slots we've found
346 * for the number of the slot
347 */
348 sun = bridge->nr_slots+1;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000352 list_for_each_entry(slot, &bridge->slots, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (slot->device == device) {
354 if (slot->sun != sun)
355 warn("sibling found, but _SUN doesn't match!\n");
Yijing Wangad41dd92013-04-12 05:44:27 +0000356 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
359
Yijing Wangad41dd92013-04-12 05:44:27 +0000360 if (!found) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100361 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!slot) {
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200363 status = AE_NO_MEMORY;
364 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 slot->device = device;
369 slot->sun = sun;
370 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100371 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jiang Liu3d54a312013-04-12 05:44:28 +0000373 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000374 list_add_tail(&slot->node, &bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +0000375 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 bridge->nr_slots++;
377
Justin Chenb6adc192008-12-11 11:16:44 -0700378 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900379 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800380 retval = acpiphp_register_hotplug_slot(slot);
381 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600382 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700383 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600384 "hotplug driver\n", slot->sun);
385 else
386 warn("acpiphp_register_hotplug_slot failed "
387 "(err code = 0x%x)\n", retval);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200388
389 status = AE_OK;
390 goto err;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000395 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000397 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000399 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
400 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400403 if (is_dock_device(handle)) {
404 /* we don't want to call this device's _EJ0
405 * because we want the dock notify handler
406 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800407 */
408 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400409 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200410 &acpiphp_dock_ops, newfunc,
411 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400412 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800416 if (!(newfunc->flags & FUNC_HAS_DCK)) {
417 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ACPI_SYSTEM_NOTIFY,
419 handle_hotplug_event_func,
420 newfunc);
421
Kristen Accardi20416ea2006-02-23 17:56:03 -0800422 if (ACPI_FAILURE(status))
423 err("failed to register interrupt notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200426 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800427
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200428 err:
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800429 bridge->nr_slots--;
Jiang Liu3d54a312013-04-12 05:44:28 +0000430 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000431 list_del(&slot->node);
Jiang Liu3d54a312013-04-12 05:44:28 +0000432 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800433 kfree(slot);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800434
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200435 err_out:
436 mutex_lock(&acpiphp_context_lock);
437 context->func = NULL;
438 acpiphp_put_context(context);
439 mutex_unlock(&acpiphp_context_lock);
440 kfree(newfunc);
441 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444
445/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600446static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Alex Chiang7f538662009-09-10 12:34:09 -0600448 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900449 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600450 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800451 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900452 }
453 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Rajesh Shah42f49a62005-04-28 00:25:53 -0700456static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
457{
Alex Chiang58c08622009-10-26 21:25:27 -0600458 struct acpiphp_bridge *bridge;
459
Jiang Liu3d54a312013-04-12 05:44:28 +0000460 mutex_lock(&bridge_mutex);
Alex Chiang58c08622009-10-26 21:25:27 -0600461 list_for_each_entry(bridge, &bridge_list, list)
Jiang Liu3d54a312013-04-12 05:44:28 +0000462 if (bridge->handle == handle) {
463 get_bridge(bridge);
464 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700465 return bridge;
Jiang Liu3d54a312013-04-12 05:44:28 +0000466 }
467 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700468
469 return NULL;
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Rajesh Shah364d5092005-04-28 00:25:54 -0700472static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Jiang Liu3d54a312013-04-12 05:44:28 +0000474 struct acpiphp_slot *slot;
475 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700476 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700477 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700478
Jiang Liube6d2862013-01-31 00:10:10 +0800479 if (!pci_is_root_bus(bridge->pci_bus)) {
Yinghai Lu668192b2013-01-21 13:20:48 -0800480 status = acpi_remove_notify_handler(handle,
481 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700482 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800483 if (ACPI_FAILURE(status))
484 err("failed to remove notify handler\n");
485 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486
Jiang Liube6d2862013-01-31 00:10:10 +0800487 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900488 status = acpi_install_notify_handler(bridge->func->handle,
489 ACPI_SYSTEM_NOTIFY,
490 handle_hotplug_event_func,
491 bridge->func);
492 if (ACPI_FAILURE(status))
493 err("failed to install interrupt notify handler\n");
494 }
495
Jiang Liu3d54a312013-04-12 05:44:28 +0000496 list_for_each_entry(slot, &bridge->slots, node) {
497 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400498 if (is_dock_device(func->handle)) {
499 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400500 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800501 if (!(func->flags & FUNC_HAS_DCK)) {
502 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700503 ACPI_SYSTEM_NOTIFY,
504 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800505 if (ACPI_FAILURE(status))
506 err("failed to remove notify handler\n");
507 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700508 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800509 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700510 }
511
Jiang Liu3d54a312013-04-12 05:44:28 +0000512 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700513 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000514 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static int power_on_slot(struct acpiphp_slot *slot)
518{
519 acpi_status status;
520 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int retval = 0;
522
523 /* if already enabled, just skip */
524 if (slot->flags & SLOT_POWEREDON)
525 goto err_exit;
526
Alex Chiang58c08622009-10-26 21:25:27 -0600527 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800529 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
531 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800532 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 retval = -1;
534 goto err_exit;
535 } else
536 break;
537 }
538 }
539
540 /* TBD: evaluate _STA to check if the slot is enabled */
541
542 slot->flags |= SLOT_POWEREDON;
543
544 err_exit:
545 return retval;
546}
547
548
549static int power_off_slot(struct acpiphp_slot *slot)
550{
551 acpi_status status;
552 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 int retval = 0;
555
556 /* if already disabled, just skip */
557 if ((slot->flags & SLOT_POWEREDON) == 0)
558 goto err_exit;
559
Alex Chiang58c08622009-10-26 21:25:27 -0600560 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700561 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
563 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800564 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 retval = -1;
566 goto err_exit;
567 } else
568 break;
569 }
570 }
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* TBD: evaluate _STA to check if the slot is disabled */
573
574 slot->flags &= (~SLOT_POWEREDON);
575
576 err_exit:
577 return retval;
578}
579
580
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800581
582/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800583 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800584 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800585 */
586static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
587{
588 struct list_head *tmp;
589 unsigned char max, n;
590
591 /*
592 * pci_bus_max_busnr will return the highest
593 * reserved busnr for all these children.
594 * that is equivalent to the bus->subordinate
595 * value. We don't want to use the parent's
596 * bus->subordinate value because it could have
597 * padding in it.
598 */
Yinghai Lub918c622012-05-17 18:51:11 -0700599 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800600
601 list_for_each(tmp, &bus->children) {
602 n = pci_bus_max_busnr(pci_bus_b(tmp));
603 if (n > max)
604 max = n;
605 }
606 return max;
607}
608
609
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800610/**
611 * acpiphp_bus_add - add a new bus to acpi subsystem
612 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800613 */
614static int acpiphp_bus_add(struct acpiphp_func *func)
615{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100616 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800617 int ret_val;
618
Kristen Accardi20416ea2006-02-23 17:56:03 -0800619 if (!acpi_bus_get_device(func->handle, &device)) {
620 dbg("bus exists... trim\n");
621 /* this shouldn't be in here, so remove
622 * the bus then re-add it...
623 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100624 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800625 }
626
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100627 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100628 if (!ret_val)
629 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800630
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100631 if (ret_val)
632 dbg("error adding bus, %x\n", -ret_val);
633
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800634 return ret_val;
635}
636
637
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900638/**
639 * acpiphp_bus_trim - trim a bus from acpi subsystem
640 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900641 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700642static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900643{
644 struct acpi_device *device;
645 int retval;
646
647 retval = acpi_bus_get_device(handle, &device);
648 if (retval) {
649 dbg("acpi_device not found\n");
650 return retval;
651 }
652
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100653 acpi_bus_trim(device);
654 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900655}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800656
Shaohua Lid0607052010-02-25 10:59:34 +0800657static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
658{
659 struct acpiphp_func *func;
660 union acpi_object params[2];
661 struct acpi_object_list arg_list;
662
663 list_for_each_entry(func, &slot->funcs, sibling) {
664 arg_list.count = 2;
665 arg_list.pointer = params;
666 params[0].type = ACPI_TYPE_INTEGER;
667 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
668 params[1].type = ACPI_TYPE_INTEGER;
669 params[1].integer.value = 1;
670 /* _REG is optional, we don't care about if there is failure */
671 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
672 }
673}
674
Yinghai Lu1f96a962013-01-21 13:20:42 -0800675static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
676{
677 struct acpiphp_func *func;
678
679 if (!dev->subordinate)
680 return;
681
682 /* quirk, or pcie could set it already */
683 if (dev->is_hotplug_bridge)
684 return;
685
686 if (PCI_SLOT(dev->devfn) != slot->device)
687 return;
688
689 list_for_each_entry(func, &slot->funcs, sibling) {
690 if (PCI_FUNC(dev->devfn) == func->function) {
691 /* check if this bridge has ejectable slots */
692 if ((detect_ejectable_slots(func->handle) > 0))
693 dev->is_hotplug_bridge = 1;
694 break;
695 }
696 }
697}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/**
700 * enable_device - enable, configure a slot
701 * @slot: slot to be enabled
702 *
703 * This function should be called per *physical slot*,
704 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100706static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700709 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700711 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200712 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (slot->flags & SLOT_ENABLED)
715 goto err_exit;
716
Jiang Liu2ca344e2013-01-31 00:10:09 +0800717 list_for_each_entry(func, &slot->funcs, sibling)
718 acpiphp_bus_add(func);
719
Rajesh Shah42f49a62005-04-28 00:25:53 -0700720 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
721 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800722 /* Maybe only part of funcs are added. */
723 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto err_exit;
725 }
726
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800727 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700728 for (pass = 0; pass < 2; pass++) {
729 list_for_each_entry(dev, &bus->devices, bus_list) {
730 if (PCI_SLOT(dev->devfn) != slot->device)
731 continue;
732 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800733 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700734 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800735 if (pass && dev->subordinate) {
736 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200737 pcibios_resource_survey_bus(dev->subordinate);
738 __pci_bus_size_bridges(dev->subordinate,
739 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800740 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800741 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
Jiang Liud66ecb72013-06-23 01:01:35 +0200745 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700746 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600747 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800748 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700749 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100750
751 list_for_each_entry(dev, &bus->devices, bus_list) {
752 /* Assume that newly added devices are powered on already. */
753 if (!dev->is_added)
754 dev->current_state = PCI_D0;
755 }
756
Rajesh Shah42f49a62005-04-28 00:25:53 -0700757 pci_bus_add_devices(bus);
758
Amos Kongf382a082011-11-25 15:03:07 +0800759 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600760 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600761 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
762 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800763 if (!dev) {
764 /* Do not set SLOT_ENABLED flag if some funcs
765 are not added. */
766 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900767 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000773 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Amos Kongce29ca32012-05-23 10:20:35 -0600776/* return first device in slot, acquiring a reference on it */
777static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
778{
779 struct pci_bus *bus = slot->bridge->pci_bus;
780 struct pci_dev *dev;
781 struct pci_dev *ret = NULL;
782
783 down_read(&pci_bus_sem);
784 list_for_each_entry(dev, &bus->devices, bus_list)
785 if (PCI_SLOT(dev->devfn) == slot->device) {
786 ret = pci_dev_get(dev);
787 break;
788 }
789 up_read(&pci_bus_sem);
790
791 return ret;
792}
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/**
795 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800796 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
798static int disable_device(struct acpiphp_slot *slot)
799{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600801 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900802
Amos Kongce29ca32012-05-23 10:20:35 -0600803 /*
804 * enable_device() enumerates all functions in this device via
805 * pci_scan_slot(), whether they have associated ACPI hotplug
806 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
807 * here.
808 */
809 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600810 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600811 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700812 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700813
Alex Chiang9d911d72009-05-21 16:21:15 -0600814 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900815 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
818 slot->flags &= (~SLOT_ENABLED);
819
Alex Chiang9d911d72009-05-21 16:21:15 -0600820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823
824/**
825 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800826 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800828 * If a slot has _STA for each function and if any one of them
829 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800831 * If a slot doesn't have _STA and if any one of its functions'
832 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800834 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
836static unsigned int get_slot_status(struct acpiphp_slot *slot)
837{
838 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400839 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct acpiphp_func *func;
842
Alex Chiang58c08622009-10-26 21:25:27 -0600843 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (func->flags & FUNC_HAS_STA) {
845 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
846 if (ACPI_SUCCESS(status) && sta)
847 break;
848 } else {
849 pci_bus_read_config_dword(slot->bridge->pci_bus,
850 PCI_DEVFN(slot->device,
851 func->function),
852 PCI_VENDOR_ID, &dvid);
853 if (dvid != 0xffffffff) {
854 sta = ACPI_STA_ALL;
855 break;
856 }
857 }
858 }
859
860 return (unsigned int)sta;
861}
862
863/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700864 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800865 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700866 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700867int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700868{
Rajesh Shah8d50e332005-04-28 00:25:57 -0700869 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700870
Alex Chiang58c08622009-10-26 21:25:27 -0600871 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700872 /* We don't want to call _EJ0 on non-existing functions. */
873 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800874 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e332005-04-28 00:25:57 -0700875 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800876 else
Rajesh Shah8d50e332005-04-28 00:25:57 -0700877 break;
878 }
879 }
880 return 0;
881}
882
883/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800885 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 *
887 * Iterate over all slots under this bridge and make sure that if a
888 * card is present they are enabled, and if not they are disabled.
889 */
890static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
891{
892 struct acpiphp_slot *slot;
893 int retval = 0;
894 int enabled, disabled;
895
896 enabled = disabled = 0;
897
Yijing Wangad41dd92013-04-12 05:44:27 +0000898 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned int status = get_slot_status(slot);
900 if (slot->flags & SLOT_ENABLED) {
901 if (status == ACPI_STA_ALL)
902 continue;
903 retval = acpiphp_disable_slot(slot);
904 if (retval) {
905 err("Error occurred in disabling\n");
906 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700907 } else {
908 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910 disabled++;
911 } else {
912 if (status != ACPI_STA_ALL)
913 continue;
914 retval = acpiphp_enable_slot(slot);
915 if (retval) {
916 err("Error occurred in enabling\n");
917 goto err_exit;
918 }
919 enabled++;
920 }
921 }
922
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800923 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 err_exit:
926 return retval;
927}
928
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600929static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700930{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700931 struct pci_dev *dev;
932
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600933 list_for_each_entry(dev, &bus->devices, bus_list)
934 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700935}
936
937/*
938 * Remove devices for which we could not assign resources, call
939 * arch specific code to fix-up the bus
940 */
941static void acpiphp_sanitize_bus(struct pci_bus *bus)
942{
Yijing Wangd65eba62013-04-12 05:44:17 +0000943 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700944 int i;
945 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
946
Yijing Wangd65eba62013-04-12 05:44:17 +0000947 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700948 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
949 struct resource *res = &dev->resource[i];
950 if ((res->flags & type_mask) && !res->start &&
951 res->end) {
952 /* Could not assign a required resources
953 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800954 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700955 break;
956 }
957 }
958 }
959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/*
962 * ACPI event handlers
963 */
964
Gary Hade0bbd6422007-07-05 11:10:48 -0700965static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700966check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
967{
968 struct acpiphp_bridge *bridge;
969 char objname[64];
970 struct acpi_buffer buffer = { .length = sizeof(objname),
971 .pointer = objname };
972
973 bridge = acpiphp_handle_to_bridge(handle);
974 if (bridge) {
975 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
976 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800977 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700978 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000979 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700980 }
981 return AE_OK ;
982}
983
Yinghai Lu3f327e32013-05-07 11:06:03 -0600984void acpiphp_check_host_bridge(acpi_handle handle)
985{
986 struct acpiphp_bridge *bridge;
987
988 bridge = acpiphp_handle_to_bridge(handle);
989 if (bridge) {
990 acpiphp_check_bridge(bridge);
991 put_bridge(bridge);
992 }
993
994 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
995 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
996}
997
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400998static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct acpiphp_bridge *bridge;
1001 char objname[64];
1002 struct acpi_buffer buffer = { .length = sizeof(objname),
1003 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001004 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001005 acpi_handle handle;
1006 u32 type;
1007
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001008 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001009 handle = hp_work->handle;
1010 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001011 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001013 acpi_scan_lock_acquire();
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1016
1017 switch (type) {
1018 case ACPI_NOTIFY_BUS_CHECK:
1019 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001020 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001021 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1022 acpiphp_check_bridge(bridge);
1023 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1024 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 break;
1026
1027 case ACPI_NOTIFY_DEVICE_CHECK:
1028 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001029 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 acpiphp_check_bridge(bridge);
1031 break;
1032
1033 case ACPI_NOTIFY_DEVICE_WAKE:
1034 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001035 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037
1038 case ACPI_NOTIFY_EJECT_REQUEST:
1039 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001040 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001041 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001042 struct acpiphp_slot *slot;
1043 slot = bridge->func->slot;
1044 if (!acpiphp_disable_slot(slot))
1045 acpiphp_eject_slot(slot);
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
1048
1049 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1050 printk(KERN_ERR "Device %s cannot be configured due"
1051 " to a frequency mismatch\n", objname);
1052 break;
1053
1054 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1055 printk(KERN_ERR "Device %s cannot be configured due"
1056 " to a bus mode mismatch\n", objname);
1057 break;
1058
1059 case ACPI_NOTIFY_POWER_FAULT:
1060 printk(KERN_ERR "Device %s has suffered a power fault\n",
1061 objname);
1062 break;
1063
1064 default:
1065 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1066 break;
1067 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001068
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001069 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001070 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Jiang Liu3d54a312013-04-12 05:44:28 +00001071 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001075 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * @handle: Notify()'ed acpi_handle
1077 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001078 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001080 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001082static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1083 void *context)
1084{
Jiang Liu3d54a312013-04-12 05:44:28 +00001085 struct acpiphp_bridge *bridge = context;
1086
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001087 /*
1088 * Currently the code adds all hotplug events to the kacpid_wq
1089 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1090 * The proper way to fix this is to reorganize the code so that
1091 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1092 * For now just re-add this work to the kacpi_hotplug_wq so we
1093 * don't deadlock on hotplug actions.
1094 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001095 get_bridge(bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001096 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001097}
1098
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001099static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001101 struct acpiphp_func *func = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 char objname[64];
1103 struct acpi_buffer buffer = { .length = sizeof(objname),
1104 .pointer = objname };
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 switch (type) {
1109 case ACPI_NOTIFY_BUS_CHECK:
1110 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001111 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 acpiphp_enable_slot(func->slot);
1113 break;
1114
1115 case ACPI_NOTIFY_DEVICE_CHECK:
1116 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001117 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 acpiphp_check_bridge(func->slot->bridge);
1119 break;
1120
1121 case ACPI_NOTIFY_DEVICE_WAKE:
1122 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001123 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125
1126 case ACPI_NOTIFY_EJECT_REQUEST:
1127 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001128 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001129 if (!(acpiphp_disable_slot(func->slot)))
1130 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132
1133 default:
1134 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1135 break;
1136 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001137}
1138
1139static void _handle_hotplug_event_func(struct work_struct *work)
1140{
1141 struct acpi_hp_work *hp_work;
1142 struct acpiphp_func *func;
1143
1144 hp_work = container_of(work, struct acpi_hp_work, work);
1145 func = hp_work->context;
1146 acpi_scan_lock_acquire();
1147
1148 hotplug_event_func(hp_work->handle, hp_work->type, func);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001149
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001150 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001151 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Jiang Liu3d54a312013-04-12 05:44:28 +00001152 put_bridge(func->slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001155/**
1156 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1157 * @handle: Notify()'ed acpi_handle
1158 * @type: Notify code
1159 * @context: pointer to acpiphp_func structure
1160 *
1161 * Handles ACPI event notification on slots.
1162 */
1163static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1164 void *context)
1165{
Jiang Liu3d54a312013-04-12 05:44:28 +00001166 struct acpiphp_func *func = context;
1167
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001168 /*
1169 * Currently the code adds all hotplug events to the kacpid_wq
1170 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1171 * The proper way to fix this is to reorganize the code so that
1172 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1173 * For now just re-add this work to the kacpi_hotplug_wq so we
1174 * don't deadlock on hotplug actions.
1175 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001176 get_bridge(func->slot->bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001177 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001178}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001180/*
1181 * Create hotplug slots for the PCI bus.
1182 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001184void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001186 struct acpiphp_context *context;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001187 struct acpiphp_bridge *bridge;
Rafael J. Wysocki25520022013-07-13 23:27:23 +02001188 acpi_handle handle;
1189 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001191 if (acpiphp_disabled)
1192 return;
1193
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001194 handle = ACPI_HANDLE(bus->bridge);
1195 if (!handle || detect_ejectable_slots(handle) <= 0)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001196 return;
1197
1198 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001199 if (!bridge) {
1200 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001201 return;
1202 }
1203
Yijing Wangad41dd92013-04-12 05:44:27 +00001204 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001205 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001206 bridge->handle = handle;
1207 bridge->pci_dev = pci_dev_get(bus->self);
1208 bridge->pci_bus = bus;
1209
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001210 mutex_lock(&acpiphp_context_lock);
1211 context = acpiphp_get_context(handle);
1212 if (!context) {
1213 context = acpiphp_init_context(handle);
1214 if (!context) {
1215 mutex_unlock(&acpiphp_context_lock);
1216 acpi_handle_err(handle, "No hotplug context\n");
1217 kfree(bridge);
1218 return;
1219 }
1220 }
1221 bridge->context = context;
1222 context->bridge = bridge;
1223 mutex_unlock(&acpiphp_context_lock);
1224
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001225 /*
1226 * Grab a ref to the subordinate PCI bus in case the bus is
1227 * removed via PCI core logical hotplug. The ref pins the bus
1228 * (which we access during module unload).
1229 */
1230 get_device(&bus->dev);
1231
Rafael J. Wysocki25520022013-07-13 23:27:23 +02001232 /* must be added to the list prior to calling register_slot */
1233 mutex_lock(&bridge_mutex);
1234 list_add(&bridge->list, &bridge_list);
1235 mutex_unlock(&bridge_mutex);
1236
1237 /* register all slot objects under this bridge */
1238 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1239 register_slot, NULL, bridge, NULL);
1240 if (ACPI_FAILURE(status)) {
1241 acpi_handle_err(bridge->handle, "failed to register slots\n");
1242 goto err;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001243 }
1244
Rafael J. Wysocki25520022013-07-13 23:27:23 +02001245 if (pci_is_root_bus(bridge->pci_bus))
1246 return;
1247
1248 /* install notify handler for P2P bridges */
1249 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
1250 handle_hotplug_event_bridge,
1251 bridge);
1252 if (ACPI_FAILURE(status)) {
1253 acpi_handle_err(bridge->handle,
1254 "failed to register notify handler\n");
1255 goto err;
1256 }
1257
1258 if (!acpi_has_method(bridge->handle, "_EJ0"))
1259 return;
1260
1261 dbg("found ejectable p2p bridge\n");
1262 bridge->flags |= BRIDGE_HAS_EJ0;
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001263 if (context->func) {
1264 get_bridge(context->func->slot->bridge);
1265 bridge->func = context->func;
1266 handle = context->handle;
1267 WARN_ON(bridge->handle != handle);
1268 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
Rafael J. Wysocki25520022013-07-13 23:27:23 +02001269 handle_hotplug_event_func);
1270 if (ACPI_FAILURE(status))
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001271 acpi_handle_err(handle,
Rafael J. Wysocki25520022013-07-13 23:27:23 +02001272 "failed to remove notify handler\n");
1273 }
1274 return;
1275
1276 err:
1277 cleanup_bridge(bridge);
1278 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001281/* Destroy hotplug slots associated with the PCI bus */
1282void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001284 struct acpiphp_bridge *bridge, *tmp;
1285
1286 if (acpiphp_disabled)
1287 return;
1288
1289 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1290 if (bridge->pci_bus == bus) {
1291 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001292 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001293 break;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297/**
1298 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001299 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 */
1301int acpiphp_enable_slot(struct acpiphp_slot *slot)
1302{
1303 int retval;
1304
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001305 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* wake up all functions */
1308 retval = power_on_slot(slot);
1309 if (retval)
1310 goto err_exit;
1311
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001312 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* configure all functions */
1314 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001315 if (retval)
1316 power_off_slot(slot);
1317 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001318 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001319 power_off_slot(slot);
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001323 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return retval;
1325}
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/**
1328 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001329 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
1331int acpiphp_disable_slot(struct acpiphp_slot *slot)
1332{
1333 int retval = 0;
1334
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001335 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* unconfigure all functions */
1338 retval = disable_device(slot);
1339 if (retval)
1340 goto err_exit;
1341
1342 /* power off all functions */
1343 retval = power_off_slot(slot);
1344 if (retval)
1345 goto err_exit;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001348 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return retval;
1350}
1351
1352
1353/*
1354 * slot enabled: 1
1355 * slot disabled: 0
1356 */
1357u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1358{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001359 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362
1363/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001364 * latch open: 1
1365 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
1367u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1368{
1369 unsigned int sta;
1370
1371 sta = get_slot_status(slot);
1372
Jiang Liuce15d872013-04-12 05:44:19 +00001373 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376
1377/*
1378 * adapter presence : 1
1379 * absence : 0
1380 */
1381u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1382{
1383 unsigned int sta;
1384
1385 sta = get_slot_status(slot);
1386
1387 return (sta == 0) ? 0 : 1;
1388}