blob: a0a7133a1d12346746aff58d17724ca2082e73cd [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 */
122static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
123 void *v)
124{
125 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
126 struct pci_bus *bus = func->slot->bridge->pci_bus;
127 u32 buses;
128
129 if (!bus->self)
130 return NOTIFY_OK;
131
132 /* fixup bad _DCK function that rewrites
133 * secondary bridge on slot
134 */
135 pci_read_config_dword(bus->self,
136 PCI_PRIMARY_BUS,
137 &buses);
138
Yinghai Lub918c622012-05-17 18:51:11 -0700139 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400140 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700141 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700142 | ((unsigned int)(bus->busn_res.start) << 8)
143 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400144 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
145 }
146 return NOTIFY_OK;
147}
148
149
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400150static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200151 .handler = hotplug_event_func,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800152};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jiang Liu5ba113f2012-08-22 23:16:45 +0800154/* Check whether the PCI device is managed by native PCIe hotplug driver */
155static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
156{
157 u32 reg32;
158 acpi_handle tmp;
159 struct acpi_pci_root *root;
160
161 /* Check whether the PCIe port supports native PCIe hotplug */
162 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
163 return false;
164 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
165 return false;
166
167 /*
168 * Check whether native PCIe hotplug has been enabled for
169 * this PCIe hierarchy.
170 */
171 tmp = acpi_find_root_bridge_handle(pdev);
172 if (!tmp)
173 return false;
174 root = acpi_pci_find_root(tmp);
175 if (!root)
176 return false;
177 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
178 return false;
179
180 return true;
181}
182
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200183static void acpiphp_dock_init(void *data)
184{
185 struct acpiphp_func *func = data;
186
187 get_bridge(func->slot->bridge);
188}
189
190static void acpiphp_dock_release(void *data)
191{
192 struct acpiphp_func *func = data;
193
194 put_bridge(func->slot->bridge);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* callback routine to register each ACPI PCI slot object */
198static acpi_status
199register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
200{
201 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
202 struct acpiphp_slot *slot;
203 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400205 unsigned long long adr, sun;
Yijing Wangad41dd92013-04-12 05:44:27 +0000206 int device, function, retval, found = 0;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900207 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600208 struct pci_dev *pdev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000209 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900211 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return AE_OK;
213
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600214 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
215 if (ACPI_FAILURE(status)) {
216 warn("can't evaluate _ADR (%#x)\n", status);
217 return AE_OK;
218 }
219
220 device = (adr >> 16) & 0xffff;
221 function = adr & 0xffff;
222
Jiang Liu3d54a312013-04-12 05:44:28 +0000223 pdev = bridge->pci_dev;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800224 if (pdev && device_is_managed_by_native_pciehp(pdev))
225 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100226
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100227 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!newfunc)
229 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 newfunc->handle = handle;
232 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000233
Jiang Liuecd046d2013-06-29 00:24:43 +0800234 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800235 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jiang Liuecd046d2013-06-29 00:24:43 +0800237 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 newfunc->flags |= FUNC_HAS_STA;
239
Jiang Liuecd046d2013-06-29 00:24:43 +0800240 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 newfunc->flags |= FUNC_HAS_PS0;
242
Jiang Liuecd046d2013-06-29 00:24:43 +0800243 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 newfunc->flags |= FUNC_HAS_PS3;
245
Jiang Liuecd046d2013-06-29 00:24:43 +0800246 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800247 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400250 if (ACPI_FAILURE(status)) {
251 /*
252 * use the count of the number of slots we've found
253 * for the number of the slot
254 */
255 sun = bridge->nr_slots+1;
256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000259 list_for_each_entry(slot, &bridge->slots, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (slot->device == device) {
261 if (slot->sun != sun)
262 warn("sibling found, but _SUN doesn't match!\n");
Yijing Wangad41dd92013-04-12 05:44:27 +0000263 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 break;
265 }
266
Yijing Wangad41dd92013-04-12 05:44:27 +0000267 if (!found) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100268 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!slot) {
270 kfree(newfunc);
271 return AE_NO_MEMORY;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 slot->device = device;
276 slot->sun = sun;
277 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100278 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Jiang Liu3d54a312013-04-12 05:44:28 +0000280 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000281 list_add_tail(&slot->node, &bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +0000282 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 bridge->nr_slots++;
284
Justin Chenb6adc192008-12-11 11:16:44 -0700285 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900286 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800287 retval = acpiphp_register_hotplug_slot(slot);
288 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600289 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700290 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600291 "hotplug driver\n", slot->sun);
292 else
293 warn("acpiphp_register_hotplug_slot failed "
294 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800295 goto err_exit;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000300 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000302 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000304 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
305 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400308 if (is_dock_device(handle)) {
309 /* we don't want to call this device's _EJ0
310 * because we want the dock notify handler
311 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800312 */
313 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400314 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200315 &acpiphp_dock_ops, newfunc,
316 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400317 dbg("failed to register dock device\n");
318
319 /* we need to be notified when dock events happen
320 * outside of the hotplug operation, since we may
321 * need to do fixups before we can hotplug.
322 */
323 newfunc->nb.notifier_call = post_dock_fixups;
324 if (register_dock_notifier(&newfunc->nb))
325 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800326 }
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800329 if (!(newfunc->flags & FUNC_HAS_DCK)) {
330 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ACPI_SYSTEM_NOTIFY,
332 handle_hotplug_event_func,
333 newfunc);
334
Kristen Accardi20416ea2006-02-23 17:56:03 -0800335 if (ACPI_FAILURE(status))
336 err("failed to register interrupt notify handler\n");
337 } else
338 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Kristen Accardi20416ea2006-02-23 17:56:03 -0800340 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800341
342 err_exit:
343 bridge->nr_slots--;
Jiang Liu3d54a312013-04-12 05:44:28 +0000344 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000345 list_del(&slot->node);
Jiang Liu3d54a312013-04-12 05:44:28 +0000346 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800347 kfree(slot);
348 kfree(newfunc);
349
350 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353
354/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600355static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Alex Chiang7f538662009-09-10 12:34:09 -0600357 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900358 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600359 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800360 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900361 }
362 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
366static void init_bridge_misc(struct acpiphp_bridge *bridge)
367{
368 acpi_status status;
369
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800370 /* must be added to the list prior to calling register_slot */
Jiang Liu3d54a312013-04-12 05:44:28 +0000371 mutex_lock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800372 list_add(&bridge->list, &bridge_list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000373 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* register all slot objects under this bridge */
376 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800377 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800378 if (ACPI_FAILURE(status)) {
Jiang Liu3d54a312013-04-12 05:44:28 +0000379 mutex_lock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800380 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000381 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800382 return;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jiang Liube6d2862013-01-31 00:10:10 +0800385 /* install notify handler for P2P bridges */
386 if (!pci_is_root_bus(bridge->pci_bus)) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900387 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
388 status = acpi_remove_notify_handler(bridge->func->handle,
389 ACPI_SYSTEM_NOTIFY,
390 handle_hotplug_event_func);
391 if (ACPI_FAILURE(status))
392 err("failed to remove notify handler\n");
393 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700394 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ACPI_SYSTEM_NOTIFY,
396 handle_hotplug_event_bridge,
397 bridge);
398
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700399 if (ACPI_FAILURE(status)) {
400 err("failed to register interrupt notify handler\n");
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900406/* find acpiphp_func from acpiphp_bridge */
407static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
408{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900409 struct acpiphp_bridge *bridge;
410 struct acpiphp_slot *slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000411 struct acpiphp_func *func = NULL;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900412
Jiang Liu3d54a312013-04-12 05:44:28 +0000413 mutex_lock(&bridge_mutex);
Alex Chiang58c08622009-10-26 21:25:27 -0600414 list_for_each_entry(bridge, &bridge_list, list) {
Yijing Wangad41dd92013-04-12 05:44:27 +0000415 list_for_each_entry(slot, &bridge->slots, node) {
Alex Chiang58c08622009-10-26 21:25:27 -0600416 list_for_each_entry(func, &slot->funcs, sibling) {
Jiang Liu3d54a312013-04-12 05:44:28 +0000417 if (func->handle == handle) {
418 get_bridge(func->slot->bridge);
419 mutex_unlock(&bridge_mutex);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900420 return func;
Jiang Liu3d54a312013-04-12 05:44:28 +0000421 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900422 }
423 }
424 }
Jiang Liu3d54a312013-04-12 05:44:28 +0000425 mutex_unlock(&bridge_mutex);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900426
427 return NULL;
428}
429
430
Rajesh Shah42f49a62005-04-28 00:25:53 -0700431static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
432{
Alex Chiang58c08622009-10-26 21:25:27 -0600433 struct acpiphp_bridge *bridge;
434
Jiang Liu3d54a312013-04-12 05:44:28 +0000435 mutex_lock(&bridge_mutex);
Alex Chiang58c08622009-10-26 21:25:27 -0600436 list_for_each_entry(bridge, &bridge_list, list)
Jiang Liu3d54a312013-04-12 05:44:28 +0000437 if (bridge->handle == handle) {
438 get_bridge(bridge);
439 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700440 return bridge;
Jiang Liu3d54a312013-04-12 05:44:28 +0000441 }
442 mutex_unlock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700443
444 return NULL;
445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Rajesh Shah364d5092005-04-28 00:25:54 -0700447static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Jiang Liu3d54a312013-04-12 05:44:28 +0000449 struct acpiphp_slot *slot;
450 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700451 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700452 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700453
Jiang Liube6d2862013-01-31 00:10:10 +0800454 if (!pci_is_root_bus(bridge->pci_bus)) {
Yinghai Lu668192b2013-01-21 13:20:48 -0800455 status = acpi_remove_notify_handler(handle,
456 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700457 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800458 if (ACPI_FAILURE(status))
459 err("failed to remove notify handler\n");
460 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700461
Jiang Liube6d2862013-01-31 00:10:10 +0800462 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900463 status = acpi_install_notify_handler(bridge->func->handle,
464 ACPI_SYSTEM_NOTIFY,
465 handle_hotplug_event_func,
466 bridge->func);
467 if (ACPI_FAILURE(status))
468 err("failed to install interrupt notify handler\n");
469 }
470
Jiang Liu3d54a312013-04-12 05:44:28 +0000471 list_for_each_entry(slot, &bridge->slots, node) {
472 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400473 if (is_dock_device(func->handle)) {
474 unregister_hotplug_dock_device(func->handle);
475 unregister_dock_notifier(&func->nb);
476 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800477 if (!(func->flags & FUNC_HAS_DCK)) {
478 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700479 ACPI_SYSTEM_NOTIFY,
480 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800481 if (ACPI_FAILURE(status))
482 err("failed to remove notify handler\n");
483 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700484 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800485 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486 }
487
Jiang Liu3d54a312013-04-12 05:44:28 +0000488 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700489 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000490 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int power_on_slot(struct acpiphp_slot *slot)
494{
495 acpi_status status;
496 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int retval = 0;
498
499 /* if already enabled, just skip */
500 if (slot->flags & SLOT_POWEREDON)
501 goto err_exit;
502
Alex Chiang58c08622009-10-26 21:25:27 -0600503 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800505 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
507 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800508 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 retval = -1;
510 goto err_exit;
511 } else
512 break;
513 }
514 }
515
516 /* TBD: evaluate _STA to check if the slot is enabled */
517
518 slot->flags |= SLOT_POWEREDON;
519
520 err_exit:
521 return retval;
522}
523
524
525static int power_off_slot(struct acpiphp_slot *slot)
526{
527 acpi_status status;
528 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 int retval = 0;
531
532 /* if already disabled, just skip */
533 if ((slot->flags & SLOT_POWEREDON) == 0)
534 goto err_exit;
535
Alex Chiang58c08622009-10-26 21:25:27 -0600536 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700537 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
539 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800540 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 retval = -1;
542 goto err_exit;
543 } else
544 break;
545 }
546 }
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* TBD: evaluate _STA to check if the slot is disabled */
549
550 slot->flags &= (~SLOT_POWEREDON);
551
552 err_exit:
553 return retval;
554}
555
556
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800557
558/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800559 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800560 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800561 */
562static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
563{
564 struct list_head *tmp;
565 unsigned char max, n;
566
567 /*
568 * pci_bus_max_busnr will return the highest
569 * reserved busnr for all these children.
570 * that is equivalent to the bus->subordinate
571 * value. We don't want to use the parent's
572 * bus->subordinate value because it could have
573 * padding in it.
574 */
Yinghai Lub918c622012-05-17 18:51:11 -0700575 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800576
577 list_for_each(tmp, &bus->children) {
578 n = pci_bus_max_busnr(pci_bus_b(tmp));
579 if (n > max)
580 max = n;
581 }
582 return max;
583}
584
585
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800586/**
587 * acpiphp_bus_add - add a new bus to acpi subsystem
588 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800589 */
590static int acpiphp_bus_add(struct acpiphp_func *func)
591{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100592 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800593 int ret_val;
594
Kristen Accardi20416ea2006-02-23 17:56:03 -0800595 if (!acpi_bus_get_device(func->handle, &device)) {
596 dbg("bus exists... trim\n");
597 /* this shouldn't be in here, so remove
598 * the bus then re-add it...
599 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100600 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800601 }
602
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100603 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100604 if (!ret_val)
605 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800606
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100607 if (ret_val)
608 dbg("error adding bus, %x\n", -ret_val);
609
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800610 return ret_val;
611}
612
613
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900614/**
615 * acpiphp_bus_trim - trim a bus from acpi subsystem
616 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900617 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700618static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900619{
620 struct acpi_device *device;
621 int retval;
622
623 retval = acpi_bus_get_device(handle, &device);
624 if (retval) {
625 dbg("acpi_device not found\n");
626 return retval;
627 }
628
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100629 acpi_bus_trim(device);
630 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900631}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800632
Shaohua Lid0607052010-02-25 10:59:34 +0800633static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
634{
635 struct acpiphp_func *func;
636 union acpi_object params[2];
637 struct acpi_object_list arg_list;
638
639 list_for_each_entry(func, &slot->funcs, sibling) {
640 arg_list.count = 2;
641 arg_list.pointer = params;
642 params[0].type = ACPI_TYPE_INTEGER;
643 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
644 params[1].type = ACPI_TYPE_INTEGER;
645 params[1].integer.value = 1;
646 /* _REG is optional, we don't care about if there is failure */
647 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
648 }
649}
650
Yinghai Lu1f96a962013-01-21 13:20:42 -0800651static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
652{
653 struct acpiphp_func *func;
654
655 if (!dev->subordinate)
656 return;
657
658 /* quirk, or pcie could set it already */
659 if (dev->is_hotplug_bridge)
660 return;
661
662 if (PCI_SLOT(dev->devfn) != slot->device)
663 return;
664
665 list_for_each_entry(func, &slot->funcs, sibling) {
666 if (PCI_FUNC(dev->devfn) == func->function) {
667 /* check if this bridge has ejectable slots */
668 if ((detect_ejectable_slots(func->handle) > 0))
669 dev->is_hotplug_bridge = 1;
670 break;
671 }
672 }
673}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
676 * enable_device - enable, configure a slot
677 * @slot: slot to be enabled
678 *
679 * This function should be called per *physical slot*,
680 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100682static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700685 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700687 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200688 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (slot->flags & SLOT_ENABLED)
691 goto err_exit;
692
Jiang Liu2ca344e2013-01-31 00:10:09 +0800693 list_for_each_entry(func, &slot->funcs, sibling)
694 acpiphp_bus_add(func);
695
Rajesh Shah42f49a62005-04-28 00:25:53 -0700696 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
697 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800698 /* Maybe only part of funcs are added. */
699 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto err_exit;
701 }
702
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800703 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700704 for (pass = 0; pass < 2; pass++) {
705 list_for_each_entry(dev, &bus->devices, bus_list) {
706 if (PCI_SLOT(dev->devfn) != slot->device)
707 continue;
708 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800709 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700710 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800711 if (pass && dev->subordinate) {
712 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200713 pcibios_resource_survey_bus(dev->subordinate);
714 __pci_bus_size_bridges(dev->subordinate,
715 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800716 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800717 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Jiang Liud66ecb72013-06-23 01:01:35 +0200721 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700722 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600723 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800724 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700725 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100726
727 list_for_each_entry(dev, &bus->devices, bus_list) {
728 /* Assume that newly added devices are powered on already. */
729 if (!dev->is_added)
730 dev->current_state = PCI_D0;
731 }
732
Rajesh Shah42f49a62005-04-28 00:25:53 -0700733 pci_bus_add_devices(bus);
734
Amos Kongf382a082011-11-25 15:03:07 +0800735 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600736 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600737 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
738 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800739 if (!dev) {
740 /* Do not set SLOT_ENABLED flag if some funcs
741 are not added. */
742 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900743 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Amos Kongce29ca32012-05-23 10:20:35 -0600752/* return first device in slot, acquiring a reference on it */
753static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
754{
755 struct pci_bus *bus = slot->bridge->pci_bus;
756 struct pci_dev *dev;
757 struct pci_dev *ret = NULL;
758
759 down_read(&pci_bus_sem);
760 list_for_each_entry(dev, &bus->devices, bus_list)
761 if (PCI_SLOT(dev->devfn) == slot->device) {
762 ret = pci_dev_get(dev);
763 break;
764 }
765 up_read(&pci_bus_sem);
766
767 return ret;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/**
771 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800772 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
774static int disable_device(struct acpiphp_slot *slot)
775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600777 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900778
Amos Kongce29ca32012-05-23 10:20:35 -0600779 /*
780 * enable_device() enumerates all functions in this device via
781 * pci_scan_slot(), whether they have associated ACPI hotplug
782 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
783 * here.
784 */
785 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600786 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600787 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700788 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700789
Alex Chiang9d911d72009-05-21 16:21:15 -0600790 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900791 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 slot->flags &= (~SLOT_ENABLED);
795
Alex Chiang9d911d72009-05-21 16:21:15 -0600796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799
800/**
801 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800802 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800804 * If a slot has _STA for each function and if any one of them
805 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800807 * If a slot doesn't have _STA and if any one of its functions'
808 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800810 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
812static unsigned int get_slot_status(struct acpiphp_slot *slot)
813{
814 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400815 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct acpiphp_func *func;
818
Alex Chiang58c08622009-10-26 21:25:27 -0600819 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (func->flags & FUNC_HAS_STA) {
821 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
822 if (ACPI_SUCCESS(status) && sta)
823 break;
824 } else {
825 pci_bus_read_config_dword(slot->bridge->pci_bus,
826 PCI_DEVFN(slot->device,
827 func->function),
828 PCI_VENDOR_ID, &dvid);
829 if (dvid != 0xffffffff) {
830 sta = ACPI_STA_ALL;
831 break;
832 }
833 }
834 }
835
836 return (unsigned int)sta;
837}
838
839/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700840 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800841 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700842 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700843int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700844{
Rajesh Shah8d50e332005-04-28 00:25:57 -0700845 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700846
Alex Chiang58c08622009-10-26 21:25:27 -0600847 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700848 /* We don't want to call _EJ0 on non-existing functions. */
849 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800850 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e332005-04-28 00:25:57 -0700851 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800852 else
Rajesh Shah8d50e332005-04-28 00:25:57 -0700853 break;
854 }
855 }
856 return 0;
857}
858
859/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800861 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 *
863 * Iterate over all slots under this bridge and make sure that if a
864 * card is present they are enabled, and if not they are disabled.
865 */
866static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
867{
868 struct acpiphp_slot *slot;
869 int retval = 0;
870 int enabled, disabled;
871
872 enabled = disabled = 0;
873
Yijing Wangad41dd92013-04-12 05:44:27 +0000874 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned int status = get_slot_status(slot);
876 if (slot->flags & SLOT_ENABLED) {
877 if (status == ACPI_STA_ALL)
878 continue;
879 retval = acpiphp_disable_slot(slot);
880 if (retval) {
881 err("Error occurred in disabling\n");
882 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700883 } else {
884 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 disabled++;
887 } else {
888 if (status != ACPI_STA_ALL)
889 continue;
890 retval = acpiphp_enable_slot(slot);
891 if (retval) {
892 err("Error occurred in enabling\n");
893 goto err_exit;
894 }
895 enabled++;
896 }
897 }
898
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800899 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 err_exit:
902 return retval;
903}
904
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600905static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700906{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700907 struct pci_dev *dev;
908
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600909 list_for_each_entry(dev, &bus->devices, bus_list)
910 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700911}
912
913/*
914 * Remove devices for which we could not assign resources, call
915 * arch specific code to fix-up the bus
916 */
917static void acpiphp_sanitize_bus(struct pci_bus *bus)
918{
Yijing Wangd65eba62013-04-12 05:44:17 +0000919 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700920 int i;
921 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
922
Yijing Wangd65eba62013-04-12 05:44:17 +0000923 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700924 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
925 struct resource *res = &dev->resource[i];
926 if ((res->flags & type_mask) && !res->start &&
927 res->end) {
928 /* Could not assign a required resources
929 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800930 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700931 break;
932 }
933 }
934 }
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * ACPI event handlers
939 */
940
Gary Hade0bbd6422007-07-05 11:10:48 -0700941static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700942check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
943{
944 struct acpiphp_bridge *bridge;
945 char objname[64];
946 struct acpi_buffer buffer = { .length = sizeof(objname),
947 .pointer = objname };
948
949 bridge = acpiphp_handle_to_bridge(handle);
950 if (bridge) {
951 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
952 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800953 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700954 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000955 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700956 }
957 return AE_OK ;
958}
959
Yinghai Lu3f327e32013-05-07 11:06:03 -0600960void acpiphp_check_host_bridge(acpi_handle handle)
961{
962 struct acpiphp_bridge *bridge;
963
964 bridge = acpiphp_handle_to_bridge(handle);
965 if (bridge) {
966 acpiphp_check_bridge(bridge);
967 put_bridge(bridge);
968 }
969
970 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
971 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
972}
973
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400974static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct acpiphp_bridge *bridge;
977 char objname[64];
978 struct acpi_buffer buffer = { .length = sizeof(objname),
979 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800980 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400981 acpi_handle handle;
982 u32 type;
983
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800984 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400985 handle = hp_work->handle;
986 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +0800987 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100989 acpi_scan_lock_acquire();
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
992
993 switch (type) {
994 case ACPI_NOTIFY_BUS_CHECK:
995 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800996 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800997 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
998 acpiphp_check_bridge(bridge);
999 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1000 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002
1003 case ACPI_NOTIFY_DEVICE_CHECK:
1004 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001005 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 acpiphp_check_bridge(bridge);
1007 break;
1008
1009 case ACPI_NOTIFY_DEVICE_WAKE:
1010 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001011 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
1013
1014 case ACPI_NOTIFY_EJECT_REQUEST:
1015 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001016 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001017 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001018 struct acpiphp_slot *slot;
1019 slot = bridge->func->slot;
1020 if (!acpiphp_disable_slot(slot))
1021 acpiphp_eject_slot(slot);
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 break;
1024
1025 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1026 printk(KERN_ERR "Device %s cannot be configured due"
1027 " to a frequency mismatch\n", objname);
1028 break;
1029
1030 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1031 printk(KERN_ERR "Device %s cannot be configured due"
1032 " to a bus mode mismatch\n", objname);
1033 break;
1034
1035 case ACPI_NOTIFY_POWER_FAULT:
1036 printk(KERN_ERR "Device %s has suffered a power fault\n",
1037 objname);
1038 break;
1039
1040 default:
1041 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1042 break;
1043 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001044
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001045 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001046 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Jiang Liu3d54a312013-04-12 05:44:28 +00001047 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001051 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 * @handle: Notify()'ed acpi_handle
1053 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001054 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001056 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001058static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1059 void *context)
1060{
Jiang Liu3d54a312013-04-12 05:44:28 +00001061 struct acpiphp_bridge *bridge = context;
1062
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001063 /*
1064 * Currently the code adds all hotplug events to the kacpid_wq
1065 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1066 * The proper way to fix this is to reorganize the code so that
1067 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1068 * For now just re-add this work to the kacpi_hotplug_wq so we
1069 * don't deadlock on hotplug actions.
1070 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001071 get_bridge(bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001072 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001073}
1074
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001075static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001077 struct acpiphp_func *func = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 char objname[64];
1079 struct acpi_buffer buffer = { .length = sizeof(objname),
1080 .pointer = objname };
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 switch (type) {
1085 case ACPI_NOTIFY_BUS_CHECK:
1086 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001087 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 acpiphp_enable_slot(func->slot);
1089 break;
1090
1091 case ACPI_NOTIFY_DEVICE_CHECK:
1092 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001093 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 acpiphp_check_bridge(func->slot->bridge);
1095 break;
1096
1097 case ACPI_NOTIFY_DEVICE_WAKE:
1098 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001099 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
1101
1102 case ACPI_NOTIFY_EJECT_REQUEST:
1103 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001104 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001105 if (!(acpiphp_disable_slot(func->slot)))
1106 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
1108
1109 default:
1110 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1111 break;
1112 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001113}
1114
1115static void _handle_hotplug_event_func(struct work_struct *work)
1116{
1117 struct acpi_hp_work *hp_work;
1118 struct acpiphp_func *func;
1119
1120 hp_work = container_of(work, struct acpi_hp_work, work);
1121 func = hp_work->context;
1122 acpi_scan_lock_acquire();
1123
1124 hotplug_event_func(hp_work->handle, hp_work->type, func);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001125
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001126 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001127 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Jiang Liu3d54a312013-04-12 05:44:28 +00001128 put_bridge(func->slot->bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001131/**
1132 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1133 * @handle: Notify()'ed acpi_handle
1134 * @type: Notify code
1135 * @context: pointer to acpiphp_func structure
1136 *
1137 * Handles ACPI event notification on slots.
1138 */
1139static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1140 void *context)
1141{
Jiang Liu3d54a312013-04-12 05:44:28 +00001142 struct acpiphp_func *func = context;
1143
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001144 /*
1145 * Currently the code adds all hotplug events to the kacpid_wq
1146 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1147 * The proper way to fix this is to reorganize the code so that
1148 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1149 * For now just re-add this work to the kacpi_hotplug_wq so we
1150 * don't deadlock on hotplug actions.
1151 */
Jiang Liu3d54a312013-04-12 05:44:28 +00001152 get_bridge(func->slot->bridge);
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001153 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001156/*
1157 * Create hotplug slots for the PCI bus.
1158 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001160void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001162 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001164 if (acpiphp_disabled)
1165 return;
1166
1167 if (detect_ejectable_slots(handle) <= 0)
1168 return;
1169
1170 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1171 if (bridge == NULL) {
1172 err("out of memory\n");
1173 return;
1174 }
1175
Yijing Wangad41dd92013-04-12 05:44:27 +00001176 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001177 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001178 bridge->handle = handle;
1179 bridge->pci_dev = pci_dev_get(bus->self);
1180 bridge->pci_bus = bus;
1181
1182 /*
1183 * Grab a ref to the subordinate PCI bus in case the bus is
1184 * removed via PCI core logical hotplug. The ref pins the bus
1185 * (which we access during module unload).
1186 */
1187 get_device(&bus->dev);
1188
1189 if (!pci_is_root_bus(bridge->pci_bus) &&
Jiang Liuecd046d2013-06-29 00:24:43 +08001190 acpi_has_method(bridge->handle, "_EJ0")) {
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001191 dbg("found ejectable p2p bridge\n");
1192 bridge->flags |= BRIDGE_HAS_EJ0;
1193 bridge->func = acpiphp_bridge_handle_to_function(handle);
1194 }
1195
1196 init_bridge_misc(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001199/* Destroy hotplug slots associated with the PCI bus */
1200void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001202 struct acpiphp_bridge *bridge, *tmp;
1203
1204 if (acpiphp_disabled)
1205 return;
1206
1207 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1208 if (bridge->pci_bus == bus) {
1209 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001210 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001211 break;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/**
1216 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001217 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
1219int acpiphp_enable_slot(struct acpiphp_slot *slot)
1220{
1221 int retval;
1222
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001223 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /* wake up all functions */
1226 retval = power_on_slot(slot);
1227 if (retval)
1228 goto err_exit;
1229
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001230 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* configure all functions */
1232 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001233 if (retval)
1234 power_off_slot(slot);
1235 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001236 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001237 power_off_slot(slot);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001241 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return retval;
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245/**
1246 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001247 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
1249int acpiphp_disable_slot(struct acpiphp_slot *slot)
1250{
1251 int retval = 0;
1252
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001253 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /* unconfigure all functions */
1256 retval = disable_device(slot);
1257 if (retval)
1258 goto err_exit;
1259
1260 /* power off all functions */
1261 retval = power_off_slot(slot);
1262 if (retval)
1263 goto err_exit;
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001266 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return retval;
1268}
1269
1270
1271/*
1272 * slot enabled: 1
1273 * slot disabled: 0
1274 */
1275u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1276{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001277 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280
1281/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001282 * latch open: 1
1283 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1286{
1287 unsigned int sta;
1288
1289 sta = get_slot_status(slot);
1290
Jiang Liuce15d872013-04-12 05:44:19 +00001291 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294
1295/*
1296 * adapter presence : 1
1297 * absence : 0
1298 */
1299u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1300{
1301 unsigned int sta;
1302
1303 sta = get_slot_status(slot);
1304
1305 return (sta == 0) ? 0 : 1;
1306}