blob: 95af39c9a396b197d1a9cb6db7c16b3e151ccf72 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ACPI PCI HotPlug glue functions to ACPI CA subsystem
3 *
4 * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
5 * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
6 * Copyright (C) 2002,2003 NEC Corporation
Rajesh Shah42f49a62005-04-28 00:25:53 -07007 * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
8 * Copyright (C) 2003-2005 Hewlett Packard
Rajesh Shah8e7561c2005-04-28 00:25:56 -07009 * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
10 * Copyright (C) 2005 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * All rights reserved.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
22 * NON INFRINGEMENT. See the GNU General Public License for more
23 * details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
Kristen Carlson Accardi998be202006-07-26 10:52:33 -070029 * Send feedback to <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 */
32
Rajesh Shah42f49a62005-04-28 00:25:53 -070033/*
34 * Lifetime rules for pci_dev:
Rajesh Shah42f49a62005-04-28 00:25:53 -070035 * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
36 * when the bridge is scanned and it loses a refcount when the bridge
37 * is removed.
Alex Chiang5d4a4b22009-03-30 10:50:14 -060038 * - When a P2P bridge is present, we elevate the refcount on the subordinate
39 * bus. It loses the refcount when the the driver unloads.
Rajesh Shah42f49a62005-04-28 00:25:53 -070040 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/module.h>
44
45#include <linux/kernel.h>
46#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070047#include <linux/pci_hotplug.h>
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +090048#include <linux/pci-acpi.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010049#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Prarit Bhargava6af8bef2011-09-28 19:40:53 -040051#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "acpiphp.h"
55
56static LIST_HEAD(bridge_list);
Jiang Liu3d54a312013-04-12 05:44:28 +000057static DEFINE_MUTEX(bridge_mutex);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +020058static DEFINE_MUTEX(acpiphp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define MY_NAME "acpiphp_glue"
61
Rafael J. Wysocki87831272013-07-13 23:27:24 +020062static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070063static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060064static void acpiphp_set_hpp_values(struct pci_bus *bus);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +020065static void hotplug_event(acpi_handle handle, u32 type, void *data);
Jiang Liu3d54a312013-04-12 05:44:28 +000066static void free_bridge(struct kref *kref);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070067
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
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +020082static void acpiphp_context_handler(acpi_handle handle, void *context)
83{
84 /* Intentionally empty. */
85}
86
87/**
88 * acpiphp_init_context - Create hotplug context and grab a reference to it.
89 * @handle: ACPI object handle to create the context for.
90 *
91 * Call under acpiphp_context_lock.
92 */
93static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
94{
95 struct acpiphp_context *context;
96 acpi_status status;
97
98 context = kzalloc(sizeof(*context), GFP_KERNEL);
99 if (!context)
100 return NULL;
101
102 context->handle = handle;
103 context->refcount = 1;
104 status = acpi_attach_data(handle, acpiphp_context_handler, context);
105 if (ACPI_FAILURE(status)) {
106 kfree(context);
107 return NULL;
108 }
109 return context;
110}
111
112/**
113 * acpiphp_get_context - Get hotplug context and grab a reference to it.
114 * @handle: ACPI object handle to get the context for.
115 *
116 * Call under acpiphp_context_lock.
117 */
118static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
119{
120 struct acpiphp_context *context = NULL;
121 acpi_status status;
122 void *data;
123
124 status = acpi_get_data(handle, acpiphp_context_handler, &data);
125 if (ACPI_SUCCESS(status)) {
126 context = data;
127 context->refcount++;
128 }
129 return context;
130}
131
132/**
133 * acpiphp_put_context - Drop a reference to ACPI hotplug context.
134 * @handle: ACPI object handle to put the context for.
135 *
136 * The context object is removed if there are no more references to it.
137 *
138 * Call under acpiphp_context_lock.
139 */
140static void acpiphp_put_context(struct acpiphp_context *context)
141{
142 if (--context->refcount)
143 return;
144
145 WARN_ON(context->func || context->bridge);
146 acpi_detach_data(context->handle, acpiphp_context_handler);
147 kfree(context);
148}
149
Jiang Liu3d54a312013-04-12 05:44:28 +0000150static inline void get_bridge(struct acpiphp_bridge *bridge)
151{
152 kref_get(&bridge->ref);
153}
154
155static inline void put_bridge(struct acpiphp_bridge *bridge)
156{
157 kref_put(&bridge->ref, free_bridge);
158}
159
160static void free_bridge(struct kref *kref)
161{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200162 struct acpiphp_context *context;
Jiang Liu3d54a312013-04-12 05:44:28 +0000163 struct acpiphp_bridge *bridge;
164 struct acpiphp_slot *slot, *next;
165 struct acpiphp_func *func, *tmp;
166
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200167 mutex_lock(&acpiphp_context_lock);
168
Jiang Liu3d54a312013-04-12 05:44:28 +0000169 bridge = container_of(kref, struct acpiphp_bridge, ref);
170
171 list_for_each_entry_safe(slot, next, &bridge->slots, node) {
172 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200173 context = func->context;
174 context->func = NULL;
175 acpiphp_put_context(context);
Jiang Liu3d54a312013-04-12 05:44:28 +0000176 kfree(func);
177 }
178 kfree(slot);
179 }
180
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200181 context = bridge->context;
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200182 /* Release the reference acquired by acpiphp_enumerate_slots(). */
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200183 if (context->handler_for_func)
Rafael J. Wysockif2818112013-07-13 23:27:24 +0200184 put_bridge(context->func->slot->bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200185
Jiang Liu3d54a312013-04-12 05:44:28 +0000186 put_device(&bridge->pci_bus->dev);
187 pci_dev_put(bridge->pci_dev);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200188 context->bridge = NULL;
189 acpiphp_put_context(context);
Jiang Liu3d54a312013-04-12 05:44:28 +0000190 kfree(bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200191
192 mutex_unlock(&acpiphp_context_lock);
Jiang Liu3d54a312013-04-12 05:44:28 +0000193}
194
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400195/*
196 * the _DCK method can do funny things... and sometimes not
197 * hah-hah funny.
198 *
199 * TBD - figure out a way to only call fixups for
200 * systems that require them.
201 */
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200202static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400203{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200204 struct acpiphp_context *context = data;
205 struct pci_bus *bus = context->func->slot->bridge->pci_bus;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400206 u32 buses;
207
208 if (!bus->self)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200209 return;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400210
211 /* fixup bad _DCK function that rewrites
212 * secondary bridge on slot
213 */
214 pci_read_config_dword(bus->self,
215 PCI_PRIMARY_BUS,
216 &buses);
217
Yinghai Lub918c622012-05-17 18:51:11 -0700218 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400219 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700220 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700221 | ((unsigned int)(bus->busn_res.start) << 8)
222 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400223 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
224 }
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400225}
226
227
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400228static const struct acpi_dock_ops acpiphp_dock_ops = {
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200229 .fixup = post_dock_fixups,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200230 .handler = hotplug_event,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800231};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jiang Liu5ba113f2012-08-22 23:16:45 +0800233/* Check whether the PCI device is managed by native PCIe hotplug driver */
234static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
235{
236 u32 reg32;
237 acpi_handle tmp;
238 struct acpi_pci_root *root;
239
240 /* Check whether the PCIe port supports native PCIe hotplug */
241 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
242 return false;
243 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
244 return false;
245
246 /*
247 * Check whether native PCIe hotplug has been enabled for
248 * this PCIe hierarchy.
249 */
250 tmp = acpi_find_root_bridge_handle(pdev);
251 if (!tmp)
252 return false;
253 root = acpi_pci_find_root(tmp);
254 if (!root)
255 return false;
256 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
257 return false;
258
259 return true;
260}
261
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200262static void acpiphp_dock_init(void *data)
263{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200264 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200265
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200266 get_bridge(context->func->slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200267}
268
269static void acpiphp_dock_release(void *data)
270{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200271 struct acpiphp_context *context = data;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200272
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200273 put_bridge(context->func->slot->bridge);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/* callback routine to register each ACPI PCI slot object */
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200277static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
278 void **rv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200280 struct acpiphp_bridge *bridge = data;
281 struct acpiphp_context *context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct acpiphp_slot *slot;
283 struct acpiphp_func *newfunc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400285 unsigned long long adr, sun;
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200286 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900287 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600288 struct pci_dev *pdev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000289 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900291 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return AE_OK;
293
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600294 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
295 if (ACPI_FAILURE(status)) {
296 warn("can't evaluate _ADR (%#x)\n", status);
297 return AE_OK;
298 }
299
300 device = (adr >> 16) & 0xffff;
301 function = adr & 0xffff;
302
Jiang Liu3d54a312013-04-12 05:44:28 +0000303 pdev = bridge->pci_dev;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800304 if (pdev && device_is_managed_by_native_pciehp(pdev))
305 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100306
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100307 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!newfunc)
309 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 newfunc->handle = handle;
312 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000313
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200314 mutex_lock(&acpiphp_context_lock);
315 context = acpiphp_init_context(handle);
316 if (!context) {
317 mutex_unlock(&acpiphp_context_lock);
318 acpi_handle_err(handle, "No hotplug context\n");
319 kfree(newfunc);
320 return AE_NOT_EXIST;
321 }
322 newfunc->context = context;
323 context->func = newfunc;
324 mutex_unlock(&acpiphp_context_lock);
325
Jiang Liuecd046d2013-06-29 00:24:43 +0800326 if (acpi_has_method(handle, "_EJ0"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800327 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Jiang Liuecd046d2013-06-29 00:24:43 +0800329 if (acpi_has_method(handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 newfunc->flags |= FUNC_HAS_STA;
331
Jiang Liuecd046d2013-06-29 00:24:43 +0800332 if (acpi_has_method(handle, "_PS0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 newfunc->flags |= FUNC_HAS_PS0;
334
Jiang Liuecd046d2013-06-29 00:24:43 +0800335 if (acpi_has_method(handle, "_PS3"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 newfunc->flags |= FUNC_HAS_PS3;
337
Jiang Liuecd046d2013-06-29 00:24:43 +0800338 if (acpi_has_method(handle, "_DCK"))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800339 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400342 if (ACPI_FAILURE(status)) {
343 /*
344 * use the count of the number of slots we've found
345 * for the number of the slot
346 */
347 sun = bridge->nr_slots+1;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* search for objects that share the same slot */
Yijing Wangad41dd92013-04-12 05:44:27 +0000351 list_for_each_entry(slot, &bridge->slots, node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (slot->device == device) {
353 if (slot->sun != sun)
354 warn("sibling found, but _SUN doesn't match!\n");
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200355
356 goto slot_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200359 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
360 if (!slot) {
361 status = AE_NO_MEMORY;
362 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Rafael J. Wysockiac372332013-07-13 23:27:24 +0200365 slot->bridge = bridge;
366 slot->device = device;
367 slot->sun = sun;
368 INIT_LIST_HEAD(&slot->funcs);
369 mutex_init(&slot->crit_sect);
370
371 mutex_lock(&bridge_mutex);
372 list_add_tail(&slot->node, &bridge->slots);
373 mutex_unlock(&bridge_mutex);
374 bridge->nr_slots++;
375
376 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
377 slot->sun, pci_domain_nr(pbus), pbus->number, device);
378
379 retval = acpiphp_register_hotplug_slot(slot);
380 if (retval) {
381 if (retval == -EBUSY)
382 warn("Slot %llu already registered by another "
383 "hotplug driver\n", slot->sun);
384 else
385 warn("acpiphp_register_hotplug_slot failed "
386 "(err code = 0x%x)\n", retval);
387
388 bridge->nr_slots--;
389 mutex_lock(&bridge_mutex);
390 list_del(&slot->node);
391 mutex_unlock(&bridge_mutex);
392 kfree(slot);
393 status = AE_OK;
394 goto err;
395 }
396
397 slot_found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000399 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000401 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000403 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
404 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400407 if (is_dock_device(handle)) {
408 /* we don't want to call this device's _EJ0
409 * because we want the dock notify handler
410 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800411 */
412 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400413 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200414 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200415 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400416 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800417 }
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800420 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200421 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
422 handle_hotplug_event,
423 context);
424 if (ACPI_SUCCESS(status))
425 context->handler_for_func = true;
426 else
Kristen Accardi20416ea2006-02-23 17:56:03 -0800427 err("failed to register interrupt notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200430 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800431
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200432 err:
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200433 mutex_lock(&acpiphp_context_lock);
434 context->func = NULL;
435 acpiphp_put_context(context);
436 mutex_unlock(&acpiphp_context_lock);
437 kfree(newfunc);
438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441
442/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600443static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Alex Chiang7f538662009-09-10 12:34:09 -0600445 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900446 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600447 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800448 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900449 }
450 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Rajesh Shah42f49a62005-04-28 00:25:53 -0700453static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
454{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200455 struct acpiphp_context *context;
456 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600457
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200458 mutex_lock(&acpiphp_context_lock);
459 context = acpiphp_get_context(handle);
460 if (context) {
461 bridge = context->bridge;
462 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000463 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700464
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200465 acpiphp_put_context(context);
466 }
467 mutex_unlock(&acpiphp_context_lock);
468 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Rajesh Shah364d5092005-04-28 00:25:54 -0700471static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jiang Liu3d54a312013-04-12 05:44:28 +0000473 struct acpiphp_slot *slot;
474 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700475 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700476 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700477
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200478 if (!bridge->context->handler_for_func) {
479 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
480 handle_hotplug_event);
Yinghai Lu668192b2013-01-21 13:20:48 -0800481 if (ACPI_FAILURE(status))
482 err("failed to remove notify handler\n");
483 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700484
Jiang Liu3d54a312013-04-12 05:44:28 +0000485 list_for_each_entry(slot, &bridge->slots, node) {
486 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400487 if (is_dock_device(func->handle)) {
488 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400489 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800490 if (!(func->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200491 func->context->handler_for_func = false;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800492 status = acpi_remove_notify_handler(func->handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200493 ACPI_SYSTEM_NOTIFY,
494 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800495 if (ACPI_FAILURE(status))
496 err("failed to remove notify handler\n");
497 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700498 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800499 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700500 }
501
Jiang Liu3d54a312013-04-12 05:44:28 +0000502 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700503 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000504 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static int power_on_slot(struct acpiphp_slot *slot)
508{
509 acpi_status status;
510 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 int retval = 0;
512
513 /* if already enabled, just skip */
514 if (slot->flags & SLOT_POWEREDON)
515 goto err_exit;
516
Alex Chiang58c08622009-10-26 21:25:27 -0600517 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800519 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
521 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800522 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 retval = -1;
524 goto err_exit;
525 } else
526 break;
527 }
528 }
529
530 /* TBD: evaluate _STA to check if the slot is enabled */
531
532 slot->flags |= SLOT_POWEREDON;
533
534 err_exit:
535 return retval;
536}
537
538
539static int power_off_slot(struct acpiphp_slot *slot)
540{
541 acpi_status status;
542 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 int retval = 0;
545
546 /* if already disabled, just skip */
547 if ((slot->flags & SLOT_POWEREDON) == 0)
548 goto err_exit;
549
Alex Chiang58c08622009-10-26 21:25:27 -0600550 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700551 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
553 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800554 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 retval = -1;
556 goto err_exit;
557 } else
558 break;
559 }
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* TBD: evaluate _STA to check if the slot is disabled */
563
564 slot->flags &= (~SLOT_POWEREDON);
565
566 err_exit:
567 return retval;
568}
569
570
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800571
572/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800573 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800574 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800575 */
576static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
577{
578 struct list_head *tmp;
579 unsigned char max, n;
580
581 /*
582 * pci_bus_max_busnr will return the highest
583 * reserved busnr for all these children.
584 * that is equivalent to the bus->subordinate
585 * value. We don't want to use the parent's
586 * bus->subordinate value because it could have
587 * padding in it.
588 */
Yinghai Lub918c622012-05-17 18:51:11 -0700589 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800590
591 list_for_each(tmp, &bus->children) {
592 n = pci_bus_max_busnr(pci_bus_b(tmp));
593 if (n > max)
594 max = n;
595 }
596 return max;
597}
598
599
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800600/**
601 * acpiphp_bus_add - add a new bus to acpi subsystem
602 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800603 */
604static int acpiphp_bus_add(struct acpiphp_func *func)
605{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100606 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800607 int ret_val;
608
Kristen Accardi20416ea2006-02-23 17:56:03 -0800609 if (!acpi_bus_get_device(func->handle, &device)) {
610 dbg("bus exists... trim\n");
611 /* this shouldn't be in here, so remove
612 * the bus then re-add it...
613 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100614 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800615 }
616
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100617 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100618 if (!ret_val)
619 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800620
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100621 if (ret_val)
622 dbg("error adding bus, %x\n", -ret_val);
623
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800624 return ret_val;
625}
626
627
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900628/**
629 * acpiphp_bus_trim - trim a bus from acpi subsystem
630 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900631 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700632static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900633{
634 struct acpi_device *device;
635 int retval;
636
637 retval = acpi_bus_get_device(handle, &device);
638 if (retval) {
639 dbg("acpi_device not found\n");
640 return retval;
641 }
642
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100643 acpi_bus_trim(device);
644 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900645}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800646
Shaohua Lid0607052010-02-25 10:59:34 +0800647static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
648{
649 struct acpiphp_func *func;
650 union acpi_object params[2];
651 struct acpi_object_list arg_list;
652
653 list_for_each_entry(func, &slot->funcs, sibling) {
654 arg_list.count = 2;
655 arg_list.pointer = params;
656 params[0].type = ACPI_TYPE_INTEGER;
657 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
658 params[1].type = ACPI_TYPE_INTEGER;
659 params[1].integer.value = 1;
660 /* _REG is optional, we don't care about if there is failure */
661 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
662 }
663}
664
Yinghai Lu1f96a962013-01-21 13:20:42 -0800665static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
666{
667 struct acpiphp_func *func;
668
669 if (!dev->subordinate)
670 return;
671
672 /* quirk, or pcie could set it already */
673 if (dev->is_hotplug_bridge)
674 return;
675
676 if (PCI_SLOT(dev->devfn) != slot->device)
677 return;
678
679 list_for_each_entry(func, &slot->funcs, sibling) {
680 if (PCI_FUNC(dev->devfn) == func->function) {
681 /* check if this bridge has ejectable slots */
682 if ((detect_ejectable_slots(func->handle) > 0))
683 dev->is_hotplug_bridge = 1;
684 break;
685 }
686 }
687}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/**
690 * enable_device - enable, configure a slot
691 * @slot: slot to be enabled
692 *
693 * This function should be called per *physical slot*,
694 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100696static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700699 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700701 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200702 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (slot->flags & SLOT_ENABLED)
705 goto err_exit;
706
Jiang Liu2ca344e2013-01-31 00:10:09 +0800707 list_for_each_entry(func, &slot->funcs, sibling)
708 acpiphp_bus_add(func);
709
Rajesh Shah42f49a62005-04-28 00:25:53 -0700710 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
711 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800712 /* Maybe only part of funcs are added. */
713 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto err_exit;
715 }
716
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800717 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700718 for (pass = 0; pass < 2; pass++) {
719 list_for_each_entry(dev, &bus->devices, bus_list) {
720 if (PCI_SLOT(dev->devfn) != slot->device)
721 continue;
722 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800723 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700724 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800725 if (pass && dev->subordinate) {
726 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200727 pcibios_resource_survey_bus(dev->subordinate);
728 __pci_bus_size_bridges(dev->subordinate,
729 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800730 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800731 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Jiang Liud66ecb72013-06-23 01:01:35 +0200735 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700736 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600737 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800738 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700739 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100740
741 list_for_each_entry(dev, &bus->devices, bus_list) {
742 /* Assume that newly added devices are powered on already. */
743 if (!dev->is_added)
744 dev->current_state = PCI_D0;
745 }
746
Rajesh Shah42f49a62005-04-28 00:25:53 -0700747 pci_bus_add_devices(bus);
748
Amos Kongf382a082011-11-25 15:03:07 +0800749 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600750 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600751 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
752 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800753 if (!dev) {
754 /* Do not set SLOT_ENABLED flag if some funcs
755 are not added. */
756 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900757 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Amos Kongce29ca32012-05-23 10:20:35 -0600766/* return first device in slot, acquiring a reference on it */
767static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
768{
769 struct pci_bus *bus = slot->bridge->pci_bus;
770 struct pci_dev *dev;
771 struct pci_dev *ret = NULL;
772
773 down_read(&pci_bus_sem);
774 list_for_each_entry(dev, &bus->devices, bus_list)
775 if (PCI_SLOT(dev->devfn) == slot->device) {
776 ret = pci_dev_get(dev);
777 break;
778 }
779 up_read(&pci_bus_sem);
780
781 return ret;
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/**
785 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800786 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
788static int disable_device(struct acpiphp_slot *slot)
789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600791 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900792
Amos Kongce29ca32012-05-23 10:20:35 -0600793 /*
794 * enable_device() enumerates all functions in this device via
795 * pci_scan_slot(), whether they have associated ACPI hotplug
796 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
797 * here.
798 */
799 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600800 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600801 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700802 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700803
Alex Chiang9d911d72009-05-21 16:21:15 -0600804 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900805 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 slot->flags &= (~SLOT_ENABLED);
809
Alex Chiang9d911d72009-05-21 16:21:15 -0600810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813
814/**
815 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800816 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800818 * If a slot has _STA for each function and if any one of them
819 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800821 * If a slot doesn't have _STA and if any one of its functions'
822 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800824 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826static unsigned int get_slot_status(struct acpiphp_slot *slot)
827{
828 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400829 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct acpiphp_func *func;
832
Alex Chiang58c08622009-10-26 21:25:27 -0600833 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (func->flags & FUNC_HAS_STA) {
835 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
836 if (ACPI_SUCCESS(status) && sta)
837 break;
838 } else {
839 pci_bus_read_config_dword(slot->bridge->pci_bus,
840 PCI_DEVFN(slot->device,
841 func->function),
842 PCI_VENDOR_ID, &dvid);
843 if (dvid != 0xffffffff) {
844 sta = ACPI_STA_ALL;
845 break;
846 }
847 }
848 }
849
850 return (unsigned int)sta;
851}
852
853/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700854 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800855 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700856 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700857int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700858{
Rajesh Shah8d50e332005-04-28 00:25:57 -0700859 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700860
Alex Chiang58c08622009-10-26 21:25:27 -0600861 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700862 /* We don't want to call _EJ0 on non-existing functions. */
863 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800864 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e332005-04-28 00:25:57 -0700865 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800866 else
Rajesh Shah8d50e332005-04-28 00:25:57 -0700867 break;
868 }
869 }
870 return 0;
871}
872
873/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800875 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
877 * Iterate over all slots under this bridge and make sure that if a
878 * card is present they are enabled, and if not they are disabled.
879 */
880static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
881{
882 struct acpiphp_slot *slot;
883 int retval = 0;
884 int enabled, disabled;
885
886 enabled = disabled = 0;
887
Yijing Wangad41dd92013-04-12 05:44:27 +0000888 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int status = get_slot_status(slot);
890 if (slot->flags & SLOT_ENABLED) {
891 if (status == ACPI_STA_ALL)
892 continue;
893 retval = acpiphp_disable_slot(slot);
894 if (retval) {
895 err("Error occurred in disabling\n");
896 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700897 } else {
898 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 disabled++;
901 } else {
902 if (status != ACPI_STA_ALL)
903 continue;
904 retval = acpiphp_enable_slot(slot);
905 if (retval) {
906 err("Error occurred in enabling\n");
907 goto err_exit;
908 }
909 enabled++;
910 }
911 }
912
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800913 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 err_exit:
916 return retval;
917}
918
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600919static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700920{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700921 struct pci_dev *dev;
922
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600923 list_for_each_entry(dev, &bus->devices, bus_list)
924 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700925}
926
927/*
928 * Remove devices for which we could not assign resources, call
929 * arch specific code to fix-up the bus
930 */
931static void acpiphp_sanitize_bus(struct pci_bus *bus)
932{
Yijing Wangd65eba62013-04-12 05:44:17 +0000933 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700934 int i;
935 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
936
Yijing Wangd65eba62013-04-12 05:44:17 +0000937 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700938 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
939 struct resource *res = &dev->resource[i];
940 if ((res->flags & type_mask) && !res->start &&
941 res->end) {
942 /* Could not assign a required resources
943 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800944 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700945 break;
946 }
947 }
948 }
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * ACPI event handlers
953 */
954
Gary Hade0bbd6422007-07-05 11:10:48 -0700955static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700956check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
957{
958 struct acpiphp_bridge *bridge;
959 char objname[64];
960 struct acpi_buffer buffer = { .length = sizeof(objname),
961 .pointer = objname };
962
963 bridge = acpiphp_handle_to_bridge(handle);
964 if (bridge) {
965 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
966 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800967 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700968 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000969 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700970 }
971 return AE_OK ;
972}
973
Yinghai Lu3f327e32013-05-07 11:06:03 -0600974void acpiphp_check_host_bridge(acpi_handle handle)
975{
976 struct acpiphp_bridge *bridge;
977
978 bridge = acpiphp_handle_to_bridge(handle);
979 if (bridge) {
980 acpiphp_check_bridge(bridge);
981 put_bridge(bridge);
982 }
983
984 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
985 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
986}
987
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200988static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200990 struct acpiphp_context *context = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct acpiphp_bridge *bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200992 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 char objname[64];
994 struct acpi_buffer buffer = { .length = sizeof(objname),
995 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400996
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200997 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +0200998 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200999 if (bridge)
1000 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001002 /*
1003 * If context->func is not NULL, we are holding a reference to its
1004 * parent bridge, so it won't go away until we drop that reference.
1005 */
1006 func = context->func;
1007 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1010
1011 switch (type) {
1012 case ACPI_NOTIFY_BUS_CHECK:
1013 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001014 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001015 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001016 if (bridge) {
1017 acpiphp_check_bridge(bridge);
1018 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1019 ACPI_UINT32_MAX, check_sub_bridges,
1020 NULL, NULL, NULL);
1021 } else {
1022 acpiphp_enable_slot(func->slot);
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025
1026 case ACPI_NOTIFY_DEVICE_CHECK:
1027 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001028 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001029 if (bridge)
1030 acpiphp_check_bridge(bridge);
1031 else
1032 acpiphp_check_bridge(func->slot->bridge);
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 break;
1035
1036 case ACPI_NOTIFY_DEVICE_WAKE:
1037 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001038 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 break;
1040
1041 case ACPI_NOTIFY_EJECT_REQUEST:
1042 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001043 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001044 if (!func)
1045 break;
1046
1047 if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
1048 break;
1049
1050 if (!(acpiphp_disable_slot(func->slot)))
1051 acpiphp_eject_slot(func->slot);
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 break;
1054
1055 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1056 printk(KERN_ERR "Device %s cannot be configured due"
1057 " to a frequency mismatch\n", objname);
1058 break;
1059
1060 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1061 printk(KERN_ERR "Device %s cannot be configured due"
1062 " to a bus mode mismatch\n", objname);
1063 break;
1064
1065 case ACPI_NOTIFY_POWER_FAULT:
1066 printk(KERN_ERR "Device %s has suffered a power fault\n",
1067 objname);
1068 break;
1069
1070 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001071 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1072 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001075
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001076 if (bridge)
1077 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001080static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001081{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001082 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001083 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001084
1085 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001086 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001087 acpi_scan_lock_acquire();
1088
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001089 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001090
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001091 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001092 kfree(hp_work); /* allocated in handle_hotplug_event() */
1093
1094 mutex_lock(&acpiphp_context_lock);
1095 if (context->func)
1096 put_bridge(context->func->slot->bridge);
1097 else
1098 acpiphp_put_context(context);
1099
1100 mutex_unlock(&acpiphp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001103/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001104 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001105 * @handle: Notify()'ed acpi_handle
1106 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001107 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001108 *
1109 * Handles ACPI event notification on slots.
1110 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001111static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001112{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001113 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001114
1115 mutex_lock(&acpiphp_context_lock);
1116 context = acpiphp_get_context(handle);
1117 if (context) {
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001118 if (context->func) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001119 get_bridge(context->func->slot->bridge);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001120 acpiphp_put_context(context);
1121 } else if (!context->bridge) {
1122 acpiphp_put_context(context);
1123 context = NULL;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001124 }
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001125 }
1126 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001127 /*
1128 * Currently the code adds all hotplug events to the kacpid_wq
1129 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1130 * The proper way to fix this is to reorganize the code so that
1131 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1132 * For now just re-add this work to the kacpi_hotplug_wq so we
1133 * don't deadlock on hotplug actions.
1134 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001135 if (context)
1136 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001137}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001139/*
1140 * Create hotplug slots for the PCI bus.
1141 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001143void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001145 struct acpiphp_context *context;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001146 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001147 acpi_handle handle;
1148 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001150 if (acpiphp_disabled)
1151 return;
1152
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001153 handle = ACPI_HANDLE(bus->bridge);
1154 if (!handle || detect_ejectable_slots(handle) <= 0)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001155 return;
1156
1157 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001158 if (!bridge) {
1159 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001160 return;
1161 }
1162
Yijing Wangad41dd92013-04-12 05:44:27 +00001163 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001164 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001165 bridge->handle = handle;
1166 bridge->pci_dev = pci_dev_get(bus->self);
1167 bridge->pci_bus = bus;
1168
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001169 mutex_lock(&acpiphp_context_lock);
1170 context = acpiphp_get_context(handle);
1171 if (!context) {
1172 context = acpiphp_init_context(handle);
1173 if (!context) {
1174 mutex_unlock(&acpiphp_context_lock);
1175 acpi_handle_err(handle, "No hotplug context\n");
1176 kfree(bridge);
1177 return;
1178 }
1179 }
1180 bridge->context = context;
1181 context->bridge = bridge;
1182 mutex_unlock(&acpiphp_context_lock);
1183
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001184 /*
1185 * Grab a ref to the subordinate PCI bus in case the bus is
1186 * removed via PCI core logical hotplug. The ref pins the bus
1187 * (which we access during module unload).
1188 */
1189 get_device(&bus->dev);
1190
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001191 /* must be added to the list prior to calling register_slot */
1192 mutex_lock(&bridge_mutex);
1193 list_add(&bridge->list, &bridge_list);
1194 mutex_unlock(&bridge_mutex);
1195
1196 /* register all slot objects under this bridge */
1197 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1198 register_slot, NULL, bridge, NULL);
1199 if (ACPI_FAILURE(status)) {
1200 acpi_handle_err(bridge->handle, "failed to register slots\n");
1201 goto err;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001202 }
1203
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001204 if (pci_is_root_bus(bridge->pci_bus))
1205 return;
1206
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001207 if (acpi_has_method(bridge->handle, "_EJ0")) {
1208 dbg("found ejectable p2p bridge\n");
1209 bridge->flags |= BRIDGE_HAS_EJ0;
1210 }
1211 if (context->handler_for_func) {
1212 /* Notify handler already installed. */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001213 get_bridge(context->func->slot->bridge);
1214 return;
1215 }
1216
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001217 /* install notify handler for P2P bridges */
1218 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001219 handle_hotplug_event, NULL);
1220 if (ACPI_SUCCESS(status))
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001221 return;
1222
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001223 acpi_handle_err(bridge->handle, "failed to register notify handler\n");
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001224
1225 err:
1226 cleanup_bridge(bridge);
1227 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001230/* Destroy hotplug slots associated with the PCI bus */
1231void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001233 struct acpiphp_bridge *bridge, *tmp;
1234
1235 if (acpiphp_disabled)
1236 return;
1237
1238 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1239 if (bridge->pci_bus == bus) {
1240 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001241 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001242 break;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/**
1247 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001248 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
1250int acpiphp_enable_slot(struct acpiphp_slot *slot)
1251{
1252 int retval;
1253
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001254 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* wake up all functions */
1257 retval = power_on_slot(slot);
1258 if (retval)
1259 goto err_exit;
1260
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001261 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* configure all functions */
1263 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001264 if (retval)
1265 power_off_slot(slot);
1266 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001267 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001268 power_off_slot(slot);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001272 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return retval;
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/**
1277 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001278 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
1280int acpiphp_disable_slot(struct acpiphp_slot *slot)
1281{
1282 int retval = 0;
1283
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001284 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /* unconfigure all functions */
1287 retval = disable_device(slot);
1288 if (retval)
1289 goto err_exit;
1290
1291 /* power off all functions */
1292 retval = power_off_slot(slot);
1293 if (retval)
1294 goto err_exit;
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001297 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return retval;
1299}
1300
1301
1302/*
1303 * slot enabled: 1
1304 * slot disabled: 0
1305 */
1306u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1307{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001308 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311
1312/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001313 * latch open: 1
1314 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1317{
1318 unsigned int sta;
1319
1320 sta = get_slot_status(slot);
1321
Jiang Liuce15d872013-04-12 05:44:19 +00001322 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325
1326/*
1327 * adapter presence : 1
1328 * absence : 0
1329 */
1330u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1331{
1332 unsigned int sta;
1333
1334 sta = get_slot_status(slot);
1335
1336 return (sta == 0) ? 0 : 1;
1337}