blob: e2f9ea03aecef5e5b1c6c243ac9e8f3824b9f178 [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)
Jiang Liu3d54a312013-04-12 05:44:28 +0000184 put_bridge(bridge->func->slot->bridge);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200185
Jiang Liu3d54a312013-04-12 05:44:28 +0000186 put_device(&bridge->pci_bus->dev);
187 pci_dev_put(bridge->pci_dev);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200188 context->bridge = 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;
Yijing Wangad41dd92013-04-12 05:44:27 +0000286 int device, function, retval, found = 0;
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");
Yijing Wangad41dd92013-04-12 05:44:27 +0000355 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358
Yijing Wangad41dd92013-04-12 05:44:27 +0000359 if (!found) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100360 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!slot) {
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200362 status = AE_NO_MEMORY;
363 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 slot->device = device;
368 slot->sun = sun;
369 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100370 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jiang Liu3d54a312013-04-12 05:44:28 +0000372 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000373 list_add_tail(&slot->node, &bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +0000374 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 bridge->nr_slots++;
376
Justin Chenb6adc192008-12-11 11:16:44 -0700377 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900378 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800379 retval = acpiphp_register_hotplug_slot(slot);
380 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600381 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700382 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600383 "hotplug driver\n", slot->sun);
384 else
385 warn("acpiphp_register_hotplug_slot failed "
386 "(err code = 0x%x)\n", retval);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200387
388 status = AE_OK;
389 goto err;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
393 newfunc->slot = slot;
Jiang Liu3d54a312013-04-12 05:44:28 +0000394 mutex_lock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 list_add_tail(&newfunc->sibling, &slot->funcs);
Jiang Liu3d54a312013-04-12 05:44:28 +0000396 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000398 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
399 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400402 if (is_dock_device(handle)) {
403 /* we don't want to call this device's _EJ0
404 * because we want the dock notify handler
405 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800406 */
407 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400408 if (register_hotplug_dock_device(handle,
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200409 &acpiphp_dock_ops, context,
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200410 acpiphp_dock_init, acpiphp_dock_release))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400411 dbg("failed to register dock device\n");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800415 if (!(newfunc->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200416 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
417 handle_hotplug_event,
418 context);
419 if (ACPI_SUCCESS(status))
420 context->handler_for_func = true;
421 else
Kristen Accardi20416ea2006-02-23 17:56:03 -0800422 err("failed to register interrupt notify handler\n");
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Rafael J. Wysocki2e862c52013-07-13 23:27:23 +0200425 return AE_OK;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800426
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200427 err:
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800428 bridge->nr_slots--;
Jiang Liu3d54a312013-04-12 05:44:28 +0000429 mutex_lock(&bridge_mutex);
Yijing Wangad41dd92013-04-12 05:44:27 +0000430 list_del(&slot->node);
Jiang Liu3d54a312013-04-12 05:44:28 +0000431 mutex_unlock(&bridge_mutex);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800432 kfree(slot);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800433
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +0200434 err_out:
435 mutex_lock(&acpiphp_context_lock);
436 context->func = NULL;
437 acpiphp_put_context(context);
438 mutex_unlock(&acpiphp_context_lock);
439 kfree(newfunc);
440 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
443
444/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600445static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Alex Chiang7f538662009-09-10 12:34:09 -0600447 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900448 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600449 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800450 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900451 }
452 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Rajesh Shah42f49a62005-04-28 00:25:53 -0700455static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
456{
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200457 struct acpiphp_context *context;
458 struct acpiphp_bridge *bridge = NULL;
Alex Chiang58c08622009-10-26 21:25:27 -0600459
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200460 mutex_lock(&acpiphp_context_lock);
461 context = acpiphp_get_context(handle);
462 if (context) {
463 bridge = context->bridge;
464 if (bridge)
Jiang Liu3d54a312013-04-12 05:44:28 +0000465 get_bridge(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700466
Rafael J. Wysockied13feb2013-07-13 23:27:24 +0200467 acpiphp_put_context(context);
468 }
469 mutex_unlock(&acpiphp_context_lock);
470 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Rajesh Shah364d5092005-04-28 00:25:54 -0700473static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Jiang Liu3d54a312013-04-12 05:44:28 +0000475 struct acpiphp_slot *slot;
476 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700477 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700478 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700479
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200480 if (!bridge->context->handler_for_func) {
481 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
482 handle_hotplug_event);
Yinghai Lu668192b2013-01-21 13:20:48 -0800483 if (ACPI_FAILURE(status))
484 err("failed to remove notify handler\n");
485 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700486
Jiang Liu3d54a312013-04-12 05:44:28 +0000487 list_for_each_entry(slot, &bridge->slots, node) {
488 list_for_each_entry(func, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400489 if (is_dock_device(func->handle)) {
490 unregister_hotplug_dock_device(func->handle);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400491 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800492 if (!(func->flags & FUNC_HAS_DCK)) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200493 func->context->handler_for_func = false;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800494 status = acpi_remove_notify_handler(func->handle,
Rafael J. Wysocki87831272013-07-13 23:27:24 +0200495 ACPI_SYSTEM_NOTIFY,
496 handle_hotplug_event);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800497 if (ACPI_FAILURE(status))
498 err("failed to remove notify handler\n");
499 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700500 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800501 acpiphp_unregister_hotplug_slot(slot);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700502 }
503
Jiang Liu3d54a312013-04-12 05:44:28 +0000504 mutex_lock(&bridge_mutex);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700505 list_del(&bridge->list);
Jiang Liu3d54a312013-04-12 05:44:28 +0000506 mutex_unlock(&bridge_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static int power_on_slot(struct acpiphp_slot *slot)
510{
511 acpi_status status;
512 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int retval = 0;
514
515 /* if already enabled, just skip */
516 if (slot->flags & SLOT_POWEREDON)
517 goto err_exit;
518
Alex Chiang58c08622009-10-26 21:25:27 -0600519 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800521 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
523 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800524 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 retval = -1;
526 goto err_exit;
527 } else
528 break;
529 }
530 }
531
532 /* TBD: evaluate _STA to check if the slot is enabled */
533
534 slot->flags |= SLOT_POWEREDON;
535
536 err_exit:
537 return retval;
538}
539
540
541static int power_off_slot(struct acpiphp_slot *slot)
542{
543 acpi_status status;
544 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 int retval = 0;
547
548 /* if already disabled, just skip */
549 if ((slot->flags & SLOT_POWEREDON) == 0)
550 goto err_exit;
551
Alex Chiang58c08622009-10-26 21:25:27 -0600552 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700553 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
555 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800556 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 retval = -1;
558 goto err_exit;
559 } else
560 break;
561 }
562 }
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* TBD: evaluate _STA to check if the slot is disabled */
565
566 slot->flags &= (~SLOT_POWEREDON);
567
568 err_exit:
569 return retval;
570}
571
572
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800573
574/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800575 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800576 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800577 */
578static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
579{
580 struct list_head *tmp;
581 unsigned char max, n;
582
583 /*
584 * pci_bus_max_busnr will return the highest
585 * reserved busnr for all these children.
586 * that is equivalent to the bus->subordinate
587 * value. We don't want to use the parent's
588 * bus->subordinate value because it could have
589 * padding in it.
590 */
Yinghai Lub918c622012-05-17 18:51:11 -0700591 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800592
593 list_for_each(tmp, &bus->children) {
594 n = pci_bus_max_busnr(pci_bus_b(tmp));
595 if (n > max)
596 max = n;
597 }
598 return max;
599}
600
601
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800602/**
603 * acpiphp_bus_add - add a new bus to acpi subsystem
604 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800605 */
606static int acpiphp_bus_add(struct acpiphp_func *func)
607{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100608 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800609 int ret_val;
610
Kristen Accardi20416ea2006-02-23 17:56:03 -0800611 if (!acpi_bus_get_device(func->handle, &device)) {
612 dbg("bus exists... trim\n");
613 /* this shouldn't be in here, so remove
614 * the bus then re-add it...
615 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100616 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800617 }
618
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100619 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100620 if (!ret_val)
621 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800622
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100623 if (ret_val)
624 dbg("error adding bus, %x\n", -ret_val);
625
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800626 return ret_val;
627}
628
629
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900630/**
631 * acpiphp_bus_trim - trim a bus from acpi subsystem
632 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900633 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700634static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900635{
636 struct acpi_device *device;
637 int retval;
638
639 retval = acpi_bus_get_device(handle, &device);
640 if (retval) {
641 dbg("acpi_device not found\n");
642 return retval;
643 }
644
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100645 acpi_bus_trim(device);
646 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900647}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800648
Shaohua Lid0607052010-02-25 10:59:34 +0800649static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
650{
651 struct acpiphp_func *func;
652 union acpi_object params[2];
653 struct acpi_object_list arg_list;
654
655 list_for_each_entry(func, &slot->funcs, sibling) {
656 arg_list.count = 2;
657 arg_list.pointer = params;
658 params[0].type = ACPI_TYPE_INTEGER;
659 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
660 params[1].type = ACPI_TYPE_INTEGER;
661 params[1].integer.value = 1;
662 /* _REG is optional, we don't care about if there is failure */
663 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
664 }
665}
666
Yinghai Lu1f96a962013-01-21 13:20:42 -0800667static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
668{
669 struct acpiphp_func *func;
670
671 if (!dev->subordinate)
672 return;
673
674 /* quirk, or pcie could set it already */
675 if (dev->is_hotplug_bridge)
676 return;
677
678 if (PCI_SLOT(dev->devfn) != slot->device)
679 return;
680
681 list_for_each_entry(func, &slot->funcs, sibling) {
682 if (PCI_FUNC(dev->devfn) == func->function) {
683 /* check if this bridge has ejectable slots */
684 if ((detect_ejectable_slots(func->handle) > 0))
685 dev->is_hotplug_bridge = 1;
686 break;
687 }
688 }
689}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691/**
692 * enable_device - enable, configure a slot
693 * @slot: slot to be enabled
694 *
695 * This function should be called per *physical slot*,
696 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100698static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700701 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct acpiphp_func *func;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700703 int num, max, pass;
Jiang Liud66ecb72013-06-23 01:01:35 +0200704 LIST_HEAD(add_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (slot->flags & SLOT_ENABLED)
707 goto err_exit;
708
Jiang Liu2ca344e2013-01-31 00:10:09 +0800709 list_for_each_entry(func, &slot->funcs, sibling)
710 acpiphp_bus_add(func);
711
Rajesh Shah42f49a62005-04-28 00:25:53 -0700712 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
713 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800714 /* Maybe only part of funcs are added. */
715 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto err_exit;
717 }
718
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800719 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700720 for (pass = 0; pass < 2; pass++) {
721 list_for_each_entry(dev, &bus->devices, bus_list) {
722 if (PCI_SLOT(dev->devfn) != slot->device)
723 continue;
724 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800725 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700726 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800727 if (pass && dev->subordinate) {
728 check_hotplug_bridge(slot, dev);
Jiang Liud66ecb72013-06-23 01:01:35 +0200729 pcibios_resource_survey_bus(dev->subordinate);
730 __pci_bus_size_bridges(dev->subordinate,
731 &add_list);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800732 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800733 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Jiang Liud66ecb72013-06-23 01:01:35 +0200737 __pci_bus_assign_resources(bus, &add_list, NULL);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700738 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600739 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800740 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700741 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100742
743 list_for_each_entry(dev, &bus->devices, bus_list) {
744 /* Assume that newly added devices are powered on already. */
745 if (!dev->is_added)
746 dev->current_state = PCI_D0;
747 }
748
Rajesh Shah42f49a62005-04-28 00:25:53 -0700749 pci_bus_add_devices(bus);
750
Amos Kongf382a082011-11-25 15:03:07 +0800751 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600752 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600753 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
754 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800755 if (!dev) {
756 /* Do not set SLOT_ENABLED flag if some funcs
757 are not added. */
758 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900759 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 err_exit:
Jiang Liu3d54a312013-04-12 05:44:28 +0000765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Amos Kongce29ca32012-05-23 10:20:35 -0600768/* return first device in slot, acquiring a reference on it */
769static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
770{
771 struct pci_bus *bus = slot->bridge->pci_bus;
772 struct pci_dev *dev;
773 struct pci_dev *ret = NULL;
774
775 down_read(&pci_bus_sem);
776 list_for_each_entry(dev, &bus->devices, bus_list)
777 if (PCI_SLOT(dev->devfn) == slot->device) {
778 ret = pci_dev_get(dev);
779 break;
780 }
781 up_read(&pci_bus_sem);
782
783 return ret;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/**
787 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800788 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
790static int disable_device(struct acpiphp_slot *slot)
791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600793 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900794
Amos Kongce29ca32012-05-23 10:20:35 -0600795 /*
796 * enable_device() enumerates all functions in this device via
797 * pci_scan_slot(), whether they have associated ACPI hotplug
798 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
799 * here.
800 */
801 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600802 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600803 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700804 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700805
Alex Chiang9d911d72009-05-21 16:21:15 -0600806 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900807 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 slot->flags &= (~SLOT_ENABLED);
811
Alex Chiang9d911d72009-05-21 16:21:15 -0600812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815
816/**
817 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800818 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800820 * If a slot has _STA for each function and if any one of them
821 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800823 * If a slot doesn't have _STA and if any one of its functions'
824 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800826 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
828static unsigned int get_slot_status(struct acpiphp_slot *slot)
829{
830 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400831 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 struct acpiphp_func *func;
834
Alex Chiang58c08622009-10-26 21:25:27 -0600835 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (func->flags & FUNC_HAS_STA) {
837 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
838 if (ACPI_SUCCESS(status) && sta)
839 break;
840 } else {
841 pci_bus_read_config_dword(slot->bridge->pci_bus,
842 PCI_DEVFN(slot->device,
843 func->function),
844 PCI_VENDOR_ID, &dvid);
845 if (dvid != 0xffffffff) {
846 sta = ACPI_STA_ALL;
847 break;
848 }
849 }
850 }
851
852 return (unsigned int)sta;
853}
854
855/**
Rajesh Shah8d50e332005-04-28 00:25:57 -0700856 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800857 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -0700858 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700859int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -0700860{
Rajesh Shah8d50e332005-04-28 00:25:57 -0700861 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700862
Alex Chiang58c08622009-10-26 21:25:27 -0600863 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -0700864 /* We don't want to call _EJ0 on non-existing functions. */
865 if ((func->flags & FUNC_HAS_EJ0)) {
Jiang Liuecd046d2013-06-29 00:24:43 +0800866 if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
Rajesh Shah8d50e332005-04-28 00:25:57 -0700867 return -1;
Jiang Liuecd046d2013-06-29 00:24:43 +0800868 else
Rajesh Shah8d50e332005-04-28 00:25:57 -0700869 break;
870 }
871 }
872 return 0;
873}
874
875/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800877 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 *
879 * Iterate over all slots under this bridge and make sure that if a
880 * card is present they are enabled, and if not they are disabled.
881 */
882static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
883{
884 struct acpiphp_slot *slot;
885 int retval = 0;
886 int enabled, disabled;
887
888 enabled = disabled = 0;
889
Yijing Wangad41dd92013-04-12 05:44:27 +0000890 list_for_each_entry(slot, &bridge->slots, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 unsigned int status = get_slot_status(slot);
892 if (slot->flags & SLOT_ENABLED) {
893 if (status == ACPI_STA_ALL)
894 continue;
895 retval = acpiphp_disable_slot(slot);
896 if (retval) {
897 err("Error occurred in disabling\n");
898 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -0700899 } else {
900 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 disabled++;
903 } else {
904 if (status != ACPI_STA_ALL)
905 continue;
906 retval = acpiphp_enable_slot(slot);
907 if (retval) {
908 err("Error occurred in enabling\n");
909 goto err_exit;
910 }
911 enabled++;
912 }
913 }
914
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800915 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 err_exit:
918 return retval;
919}
920
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600921static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700922{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700923 struct pci_dev *dev;
924
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600925 list_for_each_entry(dev, &bus->devices, bus_list)
926 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700927}
928
929/*
930 * Remove devices for which we could not assign resources, call
931 * arch specific code to fix-up the bus
932 */
933static void acpiphp_sanitize_bus(struct pci_bus *bus)
934{
Yijing Wangd65eba62013-04-12 05:44:17 +0000935 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700936 int i;
937 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
938
Yijing Wangd65eba62013-04-12 05:44:17 +0000939 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700940 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
941 struct resource *res = &dev->resource[i];
942 if ((res->flags & type_mask) && !res->start &&
943 res->end) {
944 /* Could not assign a required resources
945 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800946 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700947 break;
948 }
949 }
950 }
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/*
954 * ACPI event handlers
955 */
956
Gary Hade0bbd6422007-07-05 11:10:48 -0700957static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700958check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
959{
960 struct acpiphp_bridge *bridge;
961 char objname[64];
962 struct acpi_buffer buffer = { .length = sizeof(objname),
963 .pointer = objname };
964
965 bridge = acpiphp_handle_to_bridge(handle);
966 if (bridge) {
967 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
968 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800969 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700970 acpiphp_check_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +0000971 put_bridge(bridge);
Gary Hade0bbd6422007-07-05 11:10:48 -0700972 }
973 return AE_OK ;
974}
975
Yinghai Lu3f327e32013-05-07 11:06:03 -0600976void acpiphp_check_host_bridge(acpi_handle handle)
977{
978 struct acpiphp_bridge *bridge;
979
980 bridge = acpiphp_handle_to_bridge(handle);
981 if (bridge) {
982 acpiphp_check_bridge(bridge);
983 put_bridge(bridge);
984 }
985
986 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
987 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
988}
989
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200990static void hotplug_event(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200992 struct acpiphp_context *context = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct acpiphp_bridge *bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200994 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 char objname[64];
996 struct acpi_buffer buffer = { .length = sizeof(objname),
997 .pointer = objname };
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400998
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +0200999 mutex_lock(&acpiphp_context_lock);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001000 bridge = context->bridge;
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001001 if (bridge)
1002 get_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001004 /*
1005 * If context->func is not NULL, we are holding a reference to its
1006 * parent bridge, so it won't go away until we drop that reference.
1007 */
1008 func = context->func;
1009 mutex_unlock(&acpiphp_context_lock);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1012
1013 switch (type) {
1014 case ACPI_NOTIFY_BUS_CHECK:
1015 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001016 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +08001017 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001018 if (bridge) {
1019 acpiphp_check_bridge(bridge);
1020 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1021 ACPI_UINT32_MAX, check_sub_bridges,
1022 NULL, NULL, NULL);
1023 } else {
1024 acpiphp_enable_slot(func->slot);
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027
1028 case ACPI_NOTIFY_DEVICE_CHECK:
1029 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001030 dbg("%s: Device check notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001031 if (bridge)
1032 acpiphp_check_bridge(bridge);
1033 else
1034 acpiphp_check_bridge(func->slot->bridge);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037
1038 case ACPI_NOTIFY_DEVICE_WAKE:
1039 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001040 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
1042
1043 case ACPI_NOTIFY_EJECT_REQUEST:
1044 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001045 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001046 if (!func)
1047 break;
1048
1049 if (bridge && !(bridge->flags & BRIDGE_HAS_EJ0))
1050 break;
1051
1052 if (!(acpiphp_disable_slot(func->slot)))
1053 acpiphp_eject_slot(func->slot);
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056
1057 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1058 printk(KERN_ERR "Device %s cannot be configured due"
1059 " to a frequency mismatch\n", objname);
1060 break;
1061
1062 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1063 printk(KERN_ERR "Device %s cannot be configured due"
1064 " to a bus mode mismatch\n", objname);
1065 break;
1066
1067 case ACPI_NOTIFY_POWER_FAULT:
1068 printk(KERN_ERR "Device %s has suffered a power fault\n",
1069 objname);
1070 break;
1071
1072 default:
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001073 warn("notify_handler: unknown event type 0x%x for %s\n", type,
1074 objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001077
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001078 if (bridge)
1079 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001082static void hotplug_event_work(struct work_struct *work)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001083{
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001084 struct acpiphp_context *context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001085 struct acpi_hp_work *hp_work;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001086
1087 hp_work = container_of(work, struct acpi_hp_work, work);
Rafael J. Wysockic8ebcf12013-07-13 23:27:24 +02001088 context = hp_work->context;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +02001089 acpi_scan_lock_acquire();
1090
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001091 hotplug_event(hp_work->handle, hp_work->type, context);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001092
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001093 acpi_scan_lock_release();
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001094 kfree(hp_work); /* allocated in handle_hotplug_event() */
1095
1096 mutex_lock(&acpiphp_context_lock);
1097 if (context->func)
1098 put_bridge(context->func->slot->bridge);
1099 else
1100 acpiphp_put_context(context);
1101
1102 mutex_unlock(&acpiphp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001105/**
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001106 * handle_hotplug_event - handle ACPI hotplug event
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001107 * @handle: Notify()'ed acpi_handle
1108 * @type: Notify code
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001109 * @data: pointer to acpiphp_context structure
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001110 *
1111 * Handles ACPI event notification on slots.
1112 */
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001113static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001114{
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001115 struct acpiphp_context *context;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001116
1117 mutex_lock(&acpiphp_context_lock);
1118 context = acpiphp_get_context(handle);
1119 if (context) {
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001120 if (context->func) {
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001121 get_bridge(context->func->slot->bridge);
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001122 acpiphp_put_context(context);
1123 } else if (!context->bridge) {
1124 acpiphp_put_context(context);
1125 context = NULL;
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001126 }
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001127 }
1128 mutex_unlock(&acpiphp_context_lock);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001129 /*
1130 * Currently the code adds all hotplug events to the kacpid_wq
1131 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1132 * The proper way to fix this is to reorganize the code so that
1133 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1134 * For now just re-add this work to the kacpi_hotplug_wq so we
1135 * don't deadlock on hotplug actions.
1136 */
Rafael J. Wysocki43e5c092013-07-13 23:27:24 +02001137 if (context)
1138 alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001141/*
1142 * Create hotplug slots for the PCI bus.
1143 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 */
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001145void acpiphp_enumerate_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001147 struct acpiphp_context *context;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001148 struct acpiphp_bridge *bridge;
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001149 acpi_handle handle;
1150 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001152 if (acpiphp_disabled)
1153 return;
1154
Rafael J. Wysockibe1c9de2013-07-13 23:27:23 +02001155 handle = ACPI_HANDLE(bus->bridge);
1156 if (!handle || detect_ejectable_slots(handle) <= 0)
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001157 return;
1158
1159 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001160 if (!bridge) {
1161 acpi_handle_err(handle, "No memory for bridge object\n");
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001162 return;
1163 }
1164
Yijing Wangad41dd92013-04-12 05:44:27 +00001165 INIT_LIST_HEAD(&bridge->slots);
Jiang Liu3d54a312013-04-12 05:44:28 +00001166 kref_init(&bridge->ref);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001167 bridge->handle = handle;
1168 bridge->pci_dev = pci_dev_get(bus->self);
1169 bridge->pci_bus = bus;
1170
Rafael J. Wysockicb7b8ce2013-07-13 23:27:24 +02001171 mutex_lock(&acpiphp_context_lock);
1172 context = acpiphp_get_context(handle);
1173 if (!context) {
1174 context = acpiphp_init_context(handle);
1175 if (!context) {
1176 mutex_unlock(&acpiphp_context_lock);
1177 acpi_handle_err(handle, "No hotplug context\n");
1178 kfree(bridge);
1179 return;
1180 }
1181 }
1182 bridge->context = context;
1183 context->bridge = bridge;
1184 mutex_unlock(&acpiphp_context_lock);
1185
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001186 /*
1187 * Grab a ref to the subordinate PCI bus in case the bus is
1188 * removed via PCI core logical hotplug. The ref pins the bus
1189 * (which we access during module unload).
1190 */
1191 get_device(&bus->dev);
1192
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001193 /* must be added to the list prior to calling register_slot */
1194 mutex_lock(&bridge_mutex);
1195 list_add(&bridge->list, &bridge_list);
1196 mutex_unlock(&bridge_mutex);
1197
1198 /* register all slot objects under this bridge */
1199 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, 1,
1200 register_slot, NULL, bridge, NULL);
1201 if (ACPI_FAILURE(status)) {
1202 acpi_handle_err(bridge->handle, "failed to register slots\n");
1203 goto err;
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001204 }
1205
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001206 if (pci_is_root_bus(bridge->pci_bus))
1207 return;
1208
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001209 if (acpi_has_method(bridge->handle, "_EJ0")) {
1210 dbg("found ejectable p2p bridge\n");
1211 bridge->flags |= BRIDGE_HAS_EJ0;
1212 }
1213 if (context->handler_for_func) {
1214 /* Notify handler already installed. */
1215 bridge->func = context->func;
1216 get_bridge(context->func->slot->bridge);
1217 return;
1218 }
1219
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001220 /* install notify handler for P2P bridges */
1221 status = acpi_install_notify_handler(bridge->handle, ACPI_SYSTEM_NOTIFY,
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001222 handle_hotplug_event, NULL);
1223 if (ACPI_SUCCESS(status))
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001224 return;
1225
Rafael J. Wysocki87831272013-07-13 23:27:24 +02001226 acpi_handle_err(bridge->handle, "failed to register notify handler\n");
Rafael J. Wysocki2552002a2013-07-13 23:27:23 +02001227
1228 err:
1229 cleanup_bridge(bridge);
1230 put_bridge(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001233/* Destroy hotplug slots associated with the PCI bus */
1234void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001236 struct acpiphp_bridge *bridge, *tmp;
1237
1238 if (acpiphp_disabled)
1239 return;
1240
1241 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1242 if (bridge->pci_bus == bus) {
1243 cleanup_bridge(bridge);
Jiang Liu3d54a312013-04-12 05:44:28 +00001244 put_bridge(bridge);
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001245 break;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**
1250 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001251 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
1253int acpiphp_enable_slot(struct acpiphp_slot *slot)
1254{
1255 int retval;
1256
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001257 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* wake up all functions */
1260 retval = power_on_slot(slot);
1261 if (retval)
1262 goto err_exit;
1263
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001264 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /* configure all functions */
1266 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001267 if (retval)
1268 power_off_slot(slot);
1269 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001270 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001271 power_off_slot(slot);
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001275 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return retval;
1277}
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279/**
1280 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001281 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283int acpiphp_disable_slot(struct acpiphp_slot *slot)
1284{
1285 int retval = 0;
1286
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001287 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* unconfigure all functions */
1290 retval = disable_device(slot);
1291 if (retval)
1292 goto err_exit;
1293
1294 /* power off all functions */
1295 retval = power_off_slot(slot);
1296 if (retval)
1297 goto err_exit;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001300 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return retval;
1302}
1303
1304
1305/*
1306 * slot enabled: 1
1307 * slot disabled: 0
1308 */
1309u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1310{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001311 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314
1315/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001316 * latch open: 1
1317 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 */
1319u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1320{
1321 unsigned int sta;
1322
1323 sta = get_slot_status(slot);
1324
Jiang Liuce15d872013-04-12 05:44:19 +00001325 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
1328
1329/*
1330 * adapter presence : 1
1331 * absence : 0
1332 */
1333u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1334{
1335 unsigned int sta;
1336
1337 sta = get_slot_status(slot);
1338
1339 return (sta == 0) ? 0 : 1;
1340}