blob: a203ba529fef97ad024df1b6180bda258d49bd48 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define MY_NAME "acpiphp_glue"
60
61static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070062static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060063static void acpiphp_set_hpp_values(struct pci_bus *bus);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020064static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
Len Brown2b85e132006-06-27 01:50:14 -040065static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Jiang Liu3d54a312013-04-12 05:44:28 +000066static void free_bridge(struct kref *kref);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070067
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090068/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040069static acpi_status
70is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
71{
72 int *count = (int *)context;
73
74 if (is_dock_device(handle)) {
75 (*count)++;
76 return AE_CTRL_TERMINATE;
77 } else {
78 return AE_OK;
79 }
80}
81
Jiang Liu3d54a312013-04-12 05:44:28 +000082static inline void get_bridge(struct acpiphp_bridge *bridge)
83{
84 kref_get(&bridge->ref);
85}
86
87static inline void put_bridge(struct acpiphp_bridge *bridge)
88{
89 kref_put(&bridge->ref, free_bridge);
90}
91
92static void free_bridge(struct kref *kref)
93{
94 struct acpiphp_bridge *bridge;
95 struct acpiphp_slot *slot, *next;
96 struct acpiphp_func *func, *tmp;
97
98 bridge = container_of(kref, struct acpiphp_bridge, ref);
99
100 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
101 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
102 kfree(func);
103 }
104 kfree(slot);
105 }
106
107 /* Release reference acquired by acpiphp_bridge_handle_to_function() */
108 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
109 put_bridge(bridge->func->slot->bridge);
110 put_device(&bridge->pci_bus->dev);
111 pci_dev_put(bridge->pci_dev);
112 kfree(bridge);
113}
114
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400115/*
116 * the _DCK method can do funny things... and sometimes not
117 * hah-hah funny.
118 *
119 * TBD - figure out a way to only call fixups for
120 * systems that require them.
121 */
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200122static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400123{
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200124 struct acpiphp_func *func = data;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400125 struct pci_bus *bus = func->slot->bridge->pci_bus;
126 u32 buses;
127
128 if (!bus->self)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200129 return;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400130
131 /* fixup bad _DCK function that rewrites
132 * secondary bridge on slot
133 */
134 pci_read_config_dword(bus->self,
135 PCI_PRIMARY_BUS,
136 &buses);
137
Yinghai Lub918c622012-05-17 18:51:11 -0700138 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400139 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700140 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700141 | ((unsigned int)(bus->busn_res.start) << 8)
142 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400143 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
144 }
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400145}
146
147
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400148static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200149 .fixup = post_dock_fixups,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200150 .handler = hotplug_event_func,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Jiang Liu5ba113f2012-08-22 23:16:45 +0800153/* Check whether the PCI device is managed by native PCIe hotplug driver */
154static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
155{
156 u32 reg32;
157 acpi_handle tmp;
158 struct acpi_pci_root *root;
159
160 /* Check whether the PCIe port supports native PCIe hotplug */
161 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
162 return false;
163 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
164 return false;
165
166 /*
167 * Check whether native PCIe hotplug has been enabled for
168 * this PCIe hierarchy.
169 */
170 tmp = acpi_find_root_bridge_handle(pdev);
171 if (!tmp)
172 return false;
173 root = acpi_pci_find_root(tmp);
174 if (!root)
175 return false;
176 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
177 return false;
178
179 return true;
180}
181
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200182static void acpiphp_dock_init(void *data)
183{
184 struct acpiphp_func *func = data;
185
186 get_bridge(func->slot->bridge);
187}
188
189static void acpiphp_dock_release(void *data)
190{
191 struct acpiphp_func *func = data;
192
193 put_bridge(func->slot->bridge);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* callback routine to register each ACPI PCI slot object */
197static acpi_status
198register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
199{
200 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
201 struct acpiphp_slot *slot;
202 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400204 unsigned long long adr, sun;
Yijing Wangad41dd92013-04-12 05:44:27 +0000205 int device, function, retval, found = 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900206 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600207 struct pci_dev *pdev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000208 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900210 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return AE_OK;
212
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600213 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
214 if (ACPI_FAILURE(status)) {
215 warn("can't evaluate _ADR (%#x)\n", status);
216 return AE_OK;
217 }
218
219 device = (adr >> 16) & 0xffff;
220 function = adr & 0xffff;
221
Jiang Liu3d54a312013-04-12 05:44:28 +0000222 pdev = bridge->pci_dev;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800223 if (pdev && device_is_managed_by_native_pciehp(pdev))
224 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100225
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100226 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!newfunc)
228 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 newfunc->handle = handle;
231 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000232
Jiang Liuecd046d2013-06-29 00:24:43 +0800233 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800234 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Jiang Liuecd046d2013-06-29 00:24:43 +0800236 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 newfunc->flags |= FUNC_HAS_STA;
238
Jiang Liuecd046d2013-06-29 00:24:43 +0800239 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 newfunc->flags |= FUNC_HAS_PS0;
241
Jiang Liuecd046d2013-06-29 00:24:43 +0800242 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 newfunc->flags |= FUNC_HAS_PS3;
244
Jiang Liuecd046d2013-06-29 00:24:43 +0800245 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800246 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400249 if (ACPI_FAILURE(status)) {
250 /*
251 * use the count of the number of slots we've found
252 * for the number of the slot
253 */
254 sun = bridge->nr_slots+1;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000258 list_for_each_entry(slot, &bridge->slots, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (slot->device == device) {
260 if (slot->sun != sun)
261 warn("sibling found, but _SUN doesn't match!\n");
Yijing Wangad41dd92013-04-12 05:44:27 +0000262 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 break;
264 }
265
Yijing Wangad41dd92013-04-12 05:44:27 +0000266 if (!found) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100267 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!slot) {
269 kfree(newfunc);
270 return AE_NO_MEMORY;
271 }
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 slot->device = device;
275 slot->sun = sun;
276 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100277 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Jiang Liu3d54a312013-04-12 05:44:28 +0000279 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000280 list_add_tail(&slot->node, &bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +0000281 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 bridge->nr_slots++;
283
Justin Chenb6adc192008-12-11 11:16:44 -0700284 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900285 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800286 retval = acpiphp_register_hotplug_slot(slot);
287 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600288 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700289 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600290 "hotplug driver\n", slot->sun);
291 else
292 warn("acpiphp_register_hotplug_slot failed "
293 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800294 goto err_exit;
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
298 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000299 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000301 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000303 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
304 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400307 if (is_dock_device(handle)) {
308 /* we don't want to call this device's _EJ0
309 * because we want the dock notify handler
310 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800311 */
312 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400313 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200314 &acpiphp_dock_ops, newfunc,
315 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400316 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800320 if (!(newfunc->flags & FUNC_HAS_DCK)) {
321 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 ACPI_SYSTEM_NOTIFY,
323 handle_hotplug_event_func,
324 newfunc);
325
Kristen Accardi20416ea2006-02-23 17:56:03 -0800326 if (ACPI_FAILURE(status))
327 err("failed to register interrupt notify handler\n");
328 } else
329 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Kristen Accardi20416ea2006-02-23 17:56:03 -0800331 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800332
333 err_exit:
334 bridge->nr_slots--;
Jiang Liu3d54a312013-04-12 05:44:28 +0000335 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000336 list_del(&slot->node);
Jiang Liu3d54a312013-04-12 05:44:28 +0000337 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800338 kfree(slot);
339 kfree(newfunc);
340
341 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344
345/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600346static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Alex Chiang7f538662009-09-10 12:34:09 -0600348 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900349 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600350 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800351 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900352 }
353 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
357static void init_bridge_misc(struct acpiphp_bridge *bridge)
358{
359 acpi_status status;
360
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800361 /* must be added to the list prior to calling register_slot */
Jiang Liu3d54a312013-04-12 05:44:28 +0000362 mutex_lock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800363 list_add(&bridge->list, &bridge_list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000364 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* register all slot objects under this bridge */
367 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800368 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800369 if (ACPI_FAILURE(status)) {
Jiang Liu3d54a312013-04-12 05:44:28 +0000370 mutex_lock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800371 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000372 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800373 return;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Jiang Liube6d2862013-01-31 00:10:10 +0800376 /* install notify handler for P2P bridges */
377 if (!pci_is_root_bus(bridge->pci_bus)) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900378 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
379 status = acpi_remove_notify_handler(bridge->func->handle,
380 ACPI_SYSTEM_NOTIFY,
381 handle_hotplug_event_func);
382 if (ACPI_FAILURE(status))
383 err("failed to remove notify handler\n");
384 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700385 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ACPI_SYSTEM_NOTIFY,
387 handle_hotplug_event_bridge,
388 bridge);
389
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700390 if (ACPI_FAILURE(status)) {
391 err("failed to register interrupt notify handler\n");
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900397/* find acpiphp_func from acpiphp_bridge */
398static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
399{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900400 struct acpiphp_bridge *bridge;
401 struct acpiphp_slot *slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000402 struct acpiphp_func *func = NULL;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900403
Jiang Liu3d54a312013-04-12 05:44:28 +0000404 mutex_lock(&bridge_mutex);
Alex Chiang58c08622009-10-26 21:25:27 -0600405 list_for_each_entry(bridge, &bridge_list, list) {
Yijing Wangad41dd92013-04-12 05:44:27 +0000406 list_for_each_entry(slot, &bridge->slots, node) {
Alex Chiang58c08622009-10-26 21:25:27 -0600407 list_for_each_entry(func, &slot->funcs, sibling) {
Jiang Liu3d54a312013-04-12 05:44:28 +0000408 if (func->handle == handle) {
409 get_bridge(func->slot->bridge);
410 mutex_unlock(&bridge_mutex);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900411 return func;
Jiang Liu3d54a312013-04-12 05:44:28 +0000412 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900413 }
414 }
415 }
Jiang Liu3d54a312013-04-12 05:44:28 +0000416 mutex_unlock(&bridge_mutex);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900417
418 return NULL;
419}
420
421
Rajesh Shah42f49a62005-04-28 00:25:53 -0700422static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
423{
Alex Chiang58c08622009-10-26 21:25:27 -0600424 struct acpiphp_bridge *bridge;
425
Jiang Liu3d54a312013-04-12 05:44:28 +0000426 mutex_lock(&bridge_mutex);
Alex Chiang58c08622009-10-26 21:25:27 -0600427 list_for_each_entry(bridge, &bridge_list, list)
Jiang Liu3d54a312013-04-12 05:44:28 +0000428 if (bridge->handle == handle) {
429 get_bridge(bridge);
430 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700431 return bridge;
Jiang Liu3d54a312013-04-12 05:44:28 +0000432 }
433 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700434
435 return NULL;
436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Rajesh Shah364d5092005-04-28 00:25:54 -0700438static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Jiang Liu3d54a312013-04-12 05:44:28 +0000440 struct acpiphp_slot *slot;
441 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700442 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700443 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700444
Jiang Liube6d2862013-01-31 00:10:10 +0800445 if (!pci_is_root_bus(bridge->pci_bus)) {
Yinghai Lu668192b2013-01-21 13:20:48 -0800446 status = acpi_remove_notify_handler(handle,
447 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700448 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800449 if (ACPI_FAILURE(status))
450 err("failed to remove notify handler\n");
451 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700452
Jiang Liube6d2862013-01-31 00:10:10 +0800453 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900454 status = acpi_install_notify_handler(bridge->func->handle,
455 ACPI_SYSTEM_NOTIFY,
456 handle_hotplug_event_func,
457 bridge->func);
458 if (ACPI_FAILURE(status))
459 err("failed to install interrupt notify handler\n");
460 }
461
Jiang Liu3d54a312013-04-12 05:44:28 +0000462 list_for_each_entry(slot, &bridge->slots, node) {
463 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400464 if (is_dock_device(func->handle)) {
465 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400466 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800467 if (!(func->flags & FUNC_HAS_DCK)) {
468 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700469 ACPI_SYSTEM_NOTIFY,
470 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800471 if (ACPI_FAILURE(status))
472 err("failed to remove notify handler\n");
473 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700474 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800475 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700476 }
477
Jiang Liu3d54a312013-04-12 05:44:28 +0000478 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700479 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000480 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int power_on_slot(struct acpiphp_slot *slot)
484{
485 acpi_status status;
486 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int retval = 0;
488
489 /* if already enabled, just skip */
490 if (slot->flags & SLOT_POWEREDON)
491 goto err_exit;
492
Alex Chiang58c08622009-10-26 21:25:27 -0600493 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800495 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
497 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800498 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 retval = -1;
500 goto err_exit;
501 } else
502 break;
503 }
504 }
505
506 /* TBD: evaluate _STA to check if the slot is enabled */
507
508 slot->flags |= SLOT_POWEREDON;
509
510 err_exit:
511 return retval;
512}
513
514
515static int power_off_slot(struct acpiphp_slot *slot)
516{
517 acpi_status status;
518 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 int retval = 0;
521
522 /* if already disabled, just skip */
523 if ((slot->flags & SLOT_POWEREDON) == 0)
524 goto err_exit;
525
Alex Chiang58c08622009-10-26 21:25:27 -0600526 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700527 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
529 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800530 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 retval = -1;
532 goto err_exit;
533 } else
534 break;
535 }
536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* TBD: evaluate _STA to check if the slot is disabled */
539
540 slot->flags &= (~SLOT_POWEREDON);
541
542 err_exit:
543 return retval;
544}
545
546
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800547
548/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800549 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800550 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800551 */
552static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
553{
554 struct list_head *tmp;
555 unsigned char max, n;
556
557 /*
558 * pci_bus_max_busnr will return the highest
559 * reserved busnr for all these children.
560 * that is equivalent to the bus->subordinate
561 * value. We don't want to use the parent's
562 * bus->subordinate value because it could have
563 * padding in it.
564 */
Yinghai Lub918c622012-05-17 18:51:11 -0700565 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800566
567 list_for_each(tmp, &bus->children) {
568 n = pci_bus_max_busnr(pci_bus_b(tmp));
569 if (n > max)
570 max = n;
571 }
572 return max;
573}
574
575
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800576/**
577 * acpiphp_bus_add - add a new bus to acpi subsystem
578 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800579 */
580static int acpiphp_bus_add(struct acpiphp_func *func)
581{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100582 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800583 int ret_val;
584
Kristen Accardi20416ea2006-02-23 17:56:03 -0800585 if (!acpi_bus_get_device(func->handle, &device)) {
586 dbg("bus exists... trim\n");
587 /* this shouldn't be in here, so remove
588 * the bus then re-add it...
589 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100590 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800591 }
592
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100593 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100594 if (!ret_val)
595 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800596
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100597 if (ret_val)
598 dbg("error adding bus, %x\n", -ret_val);
599
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800600 return ret_val;
601}
602
603
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900604/**
605 * acpiphp_bus_trim - trim a bus from acpi subsystem
606 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900607 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700608static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900609{
610 struct acpi_device *device;
611 int retval;
612
613 retval = acpi_bus_get_device(handle, &device);
614 if (retval) {
615 dbg("acpi_device not found\n");
616 return retval;
617 }
618
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100619 acpi_bus_trim(device);
620 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900621}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800622
Shaohua Lid0607052010-02-25 10:59:34 +0800623static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
624{
625 struct acpiphp_func *func;
626 union acpi_object params[2];
627 struct acpi_object_list arg_list;
628
629 list_for_each_entry(func, &slot->funcs, sibling) {
630 arg_list.count = 2;
631 arg_list.pointer = params;
632 params[0].type = ACPI_TYPE_INTEGER;
633 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
634 params[1].type = ACPI_TYPE_INTEGER;
635 params[1].integer.value = 1;
636 /* _REG is optional, we don't care about if there is failure */
637 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
638 }
639}
640
Yinghai Lu1f96a962013-01-21 13:20:42 -0800641static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
642{
643 struct acpiphp_func *func;
644
645 if (!dev->subordinate)
646 return;
647
648 /* quirk, or pcie could set it already */
649 if (dev->is_hotplug_bridge)
650 return;
651
652 if (PCI_SLOT(dev->devfn) != slot->device)
653 return;
654
655 list_for_each_entry(func, &slot->funcs, sibling) {
656 if (PCI_FUNC(dev->devfn) == func->function) {
657 /* check if this bridge has ejectable slots */
658 if ((detect_ejectable_slots(func->handle) > 0))
659 dev->is_hotplug_bridge = 1;
660 break;
661 }
662 }
663}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
666 * enable_device - enable, configure a slot
667 * @slot: slot to be enabled
668 *
669 * This function should be called per *physical slot*,
670 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100672static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700675 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700677 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200678 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (slot->flags & SLOT_ENABLED)
681 goto err_exit;
682
Jiang Liu2ca344e2013-01-31 00:10:09 +0800683 list_for_each_entry(func, &slot->funcs, sibling)
684 acpiphp_bus_add(func);
685
Rajesh Shah42f49a62005-04-28 00:25:53 -0700686 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
687 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800688 /* Maybe only part of funcs are added. */
689 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto err_exit;
691 }
692
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800693 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700694 for (pass = 0; pass < 2; pass++) {
695 list_for_each_entry(dev, &bus->devices, bus_list) {
696 if (PCI_SLOT(dev->devfn) != slot->device)
697 continue;
698 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800699 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700700 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800701 if (pass && dev->subordinate) {
702 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200703 pcibios_resource_survey_bus(dev->subordinate);
704 __pci_bus_size_bridges(dev->subordinate,
705 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800706 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800707 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Jiang Liud66ecb72013-06-23 01:01:35 +0200711 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700712 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600713 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800714 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700715 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100716
717 list_for_each_entry(dev, &bus->devices, bus_list) {
718 /* Assume that newly added devices are powered on already. */
719 if (!dev->is_added)
720 dev->current_state = PCI_D0;
721 }
722
Rajesh Shah42f49a62005-04-28 00:25:53 -0700723 pci_bus_add_devices(bus);
724
Amos Kongf382a082011-11-25 15:03:07 +0800725 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600726 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600727 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
728 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800729 if (!dev) {
730 /* Do not set SLOT_ENABLED flag if some funcs
731 are not added. */
732 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900733 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Amos Kongce29ca32012-05-23 10:20:35 -0600742/* return first device in slot, acquiring a reference on it */
743static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
744{
745 struct pci_bus *bus = slot->bridge->pci_bus;
746 struct pci_dev *dev;
747 struct pci_dev *ret = NULL;
748
749 down_read(&pci_bus_sem);
750 list_for_each_entry(dev, &bus->devices, bus_list)
751 if (PCI_SLOT(dev->devfn) == slot->device) {
752 ret = pci_dev_get(dev);
753 break;
754 }
755 up_read(&pci_bus_sem);
756
757 return ret;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/**
761 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800762 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
764static int disable_device(struct acpiphp_slot *slot)
765{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600767 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900768
Amos Kongce29ca32012-05-23 10:20:35 -0600769 /*
770 * enable_device() enumerates all functions in this device via
771 * pci_scan_slot(), whether they have associated ACPI hotplug
772 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
773 * here.
774 */
775 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600776 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600777 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700778 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700779
Alex Chiang9d911d72009-05-21 16:21:15 -0600780 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900781 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
784 slot->flags &= (~SLOT_ENABLED);
785
Alex Chiang9d911d72009-05-21 16:21:15 -0600786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789
790/**
791 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800792 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800794 * If a slot has _STA for each function and if any one of them
795 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800797 * If a slot doesn't have _STA and if any one of its functions'
798 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800800 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
802static unsigned int get_slot_status(struct acpiphp_slot *slot)
803{
804 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400805 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct acpiphp_func *func;
808
Alex Chiang58c08622009-10-26 21:25:27 -0600809 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (func->flags & FUNC_HAS_STA) {
811 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
812 if (ACPI_SUCCESS(status) && sta)
813 break;
814 } else {
815 pci_bus_read_config_dword(slot->bridge->pci_bus,
816 PCI_DEVFN(slot->device,
817 func->function),
818 PCI_VENDOR_ID, &dvid);
819 if (dvid != 0xffffffff) {
820 sta = ACPI_STA_ALL;
821 break;
822 }
823 }
824 }
825
826 return (unsigned int)sta;
827}
828
829/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700830 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800831 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700832 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700833int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700834{
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700835 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700836
Alex Chiang58c08622009-10-26 21:25:27 -0600837 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700838 /* We don't want to call _EJ0 on non-existing functions. */
839 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800840 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700841 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800842 else
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700843 break;
844 }
845 }
846 return 0;
847}
848
849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800851 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 *
853 * Iterate over all slots under this bridge and make sure that if a
854 * card is present they are enabled, and if not they are disabled.
855 */
856static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
857{
858 struct acpiphp_slot *slot;
859 int retval = 0;
860 int enabled, disabled;
861
862 enabled = disabled = 0;
863
Yijing Wangad41dd92013-04-12 05:44:27 +0000864 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned int status = get_slot_status(slot);
866 if (slot->flags & SLOT_ENABLED) {
867 if (status == ACPI_STA_ALL)
868 continue;
869 retval = acpiphp_disable_slot(slot);
870 if (retval) {
871 err("Error occurred in disabling\n");
872 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700873 } else {
874 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876 disabled++;
877 } else {
878 if (status != ACPI_STA_ALL)
879 continue;
880 retval = acpiphp_enable_slot(slot);
881 if (retval) {
882 err("Error occurred in enabling\n");
883 goto err_exit;
884 }
885 enabled++;
886 }
887 }
888
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800889 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 err_exit:
892 return retval;
893}
894
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600895static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700896{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700897 struct pci_dev *dev;
898
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600899 list_for_each_entry(dev, &bus->devices, bus_list)
900 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700901}
902
903/*
904 * Remove devices for which we could not assign resources, call
905 * arch specific code to fix-up the bus
906 */
907static void acpiphp_sanitize_bus(struct pci_bus *bus)
908{
Yijing Wangd65eba62013-04-12 05:44:17 +0000909 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700910 int i;
911 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
912
Yijing Wangd65eba62013-04-12 05:44:17 +0000913 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700914 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
915 struct resource *res = &dev->resource[i];
916 if ((res->flags & type_mask) && !res->start &&
917 res->end) {
918 /* Could not assign a required resources
919 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800920 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700921 break;
922 }
923 }
924 }
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * ACPI event handlers
929 */
930
Gary Hade0bbd6422007-07-05 11:10:48 -0700931static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700932check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
933{
934 struct acpiphp_bridge *bridge;
935 char objname[64];
936 struct acpi_buffer buffer = { .length = sizeof(objname),
937 .pointer = objname };
938
939 bridge = acpiphp_handle_to_bridge(handle);
940 if (bridge) {
941 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
942 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800943 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700944 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000945 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700946 }
947 return AE_OK ;
948}
949
Yinghai Lu3f327e32013-05-07 11:06:03 -0600950void acpiphp_check_host_bridge(acpi_handle handle)
951{
952 struct acpiphp_bridge *bridge;
953
954 bridge = acpiphp_handle_to_bridge(handle);
955 if (bridge) {
956 acpiphp_check_bridge(bridge);
957 put_bridge(bridge);
958 }
959
960 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
961 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
962}
963
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400964static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct acpiphp_bridge *bridge;
967 char objname[64];
968 struct acpi_buffer buffer = { .length = sizeof(objname),
969 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800970 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400971 acpi_handle handle;
972 u32 type;
973
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800974 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400975 handle = hp_work->handle;
976 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +0800977 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100979 acpi_scan_lock_acquire();
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
982
983 switch (type) {
984 case ACPI_NOTIFY_BUS_CHECK:
985 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800986 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800987 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
988 acpiphp_check_bridge(bridge);
989 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
990 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992
993 case ACPI_NOTIFY_DEVICE_CHECK:
994 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800995 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 acpiphp_check_bridge(bridge);
997 break;
998
999 case ACPI_NOTIFY_DEVICE_WAKE:
1000 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001001 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 break;
1003
1004 case ACPI_NOTIFY_EJECT_REQUEST:
1005 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001006 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001007 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001008 struct acpiphp_slot *slot;
1009 slot = bridge->func->slot;
1010 if (!acpiphp_disable_slot(slot))
1011 acpiphp_eject_slot(slot);
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 break;
1014
1015 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1016 printk(KERN_ERR "Device %s cannot be configured due"
1017 " to a frequency mismatch\n", objname);
1018 break;
1019
1020 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1021 printk(KERN_ERR "Device %s cannot be configured due"
1022 " to a bus mode mismatch\n", objname);
1023 break;
1024
1025 case ACPI_NOTIFY_POWER_FAULT:
1026 printk(KERN_ERR "Device %s has suffered a power fault\n",
1027 objname);
1028 break;
1029
1030 default:
1031 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1032 break;
1033 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001034
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001035 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001036 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Jiang Liu3d54a312013-04-12 05:44:28 +00001037 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001041 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * @handle: Notify()'ed acpi_handle
1043 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001044 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001046 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001048static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1049 void *context)
1050{
Jiang Liu3d54a312013-04-12 05:44:28 +00001051 struct acpiphp_bridge *bridge = context;
1052
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001053 /*
1054 * Currently the code adds all hotplug events to the kacpid_wq
1055 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1056 * The proper way to fix this is to reorganize the code so that
1057 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1058 * For now just re-add this work to the kacpi_hotplug_wq so we
1059 * don't deadlock on hotplug actions.
1060 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001061 get_bridge(bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001062 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001063}
1064
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001065static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001067 struct acpiphp_func *func = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 char objname[64];
1069 struct acpi_buffer buffer = { .length = sizeof(objname),
1070 .pointer = objname };
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 switch (type) {
1075 case ACPI_NOTIFY_BUS_CHECK:
1076 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001077 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 acpiphp_enable_slot(func->slot);
1079 break;
1080
1081 case ACPI_NOTIFY_DEVICE_CHECK:
1082 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001083 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 acpiphp_check_bridge(func->slot->bridge);
1085 break;
1086
1087 case ACPI_NOTIFY_DEVICE_WAKE:
1088 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001089 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
1091
1092 case ACPI_NOTIFY_EJECT_REQUEST:
1093 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001094 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001095 if (!(acpiphp_disable_slot(func->slot)))
1096 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 break;
1098
1099 default:
1100 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1101 break;
1102 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001103}
1104
1105static void _handle_hotplug_event_func(struct work_struct *work)
1106{
1107 struct acpi_hp_work *hp_work;
1108 struct acpiphp_func *func;
1109
1110 hp_work = container_of(work, struct acpi_hp_work, work);
1111 func = hp_work->context;
1112 acpi_scan_lock_acquire();
1113
1114 hotplug_event_func(hp_work->handle, hp_work->type, func);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001115
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001116 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001117 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Jiang Liu3d54a312013-04-12 05:44:28 +00001118 put_bridge(func->slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001121/**
1122 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1123 * @handle: Notify()'ed acpi_handle
1124 * @type: Notify code
1125 * @context: pointer to acpiphp_func structure
1126 *
1127 * Handles ACPI event notification on slots.
1128 */
1129static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1130 void *context)
1131{
Jiang Liu3d54a312013-04-12 05:44:28 +00001132 struct acpiphp_func *func = context;
1133
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001134 /*
1135 * Currently the code adds all hotplug events to the kacpid_wq
1136 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1137 * The proper way to fix this is to reorganize the code so that
1138 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1139 * For now just re-add this work to the kacpi_hotplug_wq so we
1140 * don't deadlock on hotplug actions.
1141 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001142 get_bridge(func->slot->bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001143 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001146/*
1147 * Create hotplug slots for the PCI bus.
1148 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001150void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001152 acpi_handle handle;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001153 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001155 if (acpiphp_disabled)
1156 return;
1157
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001158 handle = ACPI_HANDLE(bus->bridge);
1159 if (!handle || detect_ejectable_slots(handle) <= 0)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001160 return;
1161
1162 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1163 if (bridge == NULL) {
1164 err("out of memory\n");
1165 return;
1166 }
1167
Yijing Wangad41dd92013-04-12 05:44:27 +00001168 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001169 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001170 bridge->handle = handle;
1171 bridge->pci_dev = pci_dev_get(bus->self);
1172 bridge->pci_bus = bus;
1173
1174 /*
1175 * Grab a ref to the subordinate PCI bus in case the bus is
1176 * removed via PCI core logical hotplug. The ref pins the bus
1177 * (which we access during module unload).
1178 */
1179 get_device(&bus->dev);
1180
1181 if (!pci_is_root_bus(bridge->pci_bus) &&
Jiang Liuecd046d2013-06-29 00:24:43 +08001182 acpi_has_method(bridge->handle, "_EJ0")) {
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001183 dbg("found ejectable p2p bridge\n");
1184 bridge->flags |= BRIDGE_HAS_EJ0;
1185 bridge->func = acpiphp_bridge_handle_to_function(handle);
1186 }
1187
1188 init_bridge_misc(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001191/* Destroy hotplug slots associated with the PCI bus */
1192void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001194 struct acpiphp_bridge *bridge, *tmp;
1195
1196 if (acpiphp_disabled)
1197 return;
1198
1199 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1200 if (bridge->pci_bus == bus) {
1201 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001202 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001203 break;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
1208 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001209 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
1211int acpiphp_enable_slot(struct acpiphp_slot *slot)
1212{
1213 int retval;
1214
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001215 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* wake up all functions */
1218 retval = power_on_slot(slot);
1219 if (retval)
1220 goto err_exit;
1221
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001222 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /* configure all functions */
1224 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001225 if (retval)
1226 power_off_slot(slot);
1227 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001228 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001229 power_off_slot(slot);
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001233 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return retval;
1235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/**
1238 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001239 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
1241int acpiphp_disable_slot(struct acpiphp_slot *slot)
1242{
1243 int retval = 0;
1244
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001245 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /* unconfigure all functions */
1248 retval = disable_device(slot);
1249 if (retval)
1250 goto err_exit;
1251
1252 /* power off all functions */
1253 retval = power_off_slot(slot);
1254 if (retval)
1255 goto err_exit;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001258 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return retval;
1260}
1261
1262
1263/*
1264 * slot enabled: 1
1265 * slot disabled: 0
1266 */
1267u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1268{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001269 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272
1273/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001274 * latch open: 1
1275 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
1277u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1278{
1279 unsigned int sta;
1280
1281 sta = get_slot_status(slot);
1282
Jiang Liuce15d872013-04-12 05:44:19 +00001283 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286
1287/*
1288 * adapter presence : 1
1289 * absence : 0
1290 */
1291u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1292{
1293 unsigned int sta;
1294
1295 sta = get_slot_status(slot);
1296
1297 return (sta == 0) ? 0 : 1;
1298}