blob: 20db4d6564a82615d51cf802c80bac886a79fa49 [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);
57
58#define MY_NAME "acpiphp_glue"
59
60static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070061static void acpiphp_sanitize_bus(struct pci_bus *bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -060062static void acpiphp_set_hpp_values(struct pci_bus *bus);
Len Brown2b85e132006-06-27 01:50:14 -040063static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
Kristen Accardi8e5dce32005-10-18 17:21:40 -070064
MUNEDA Takahiro5a340ed2007-11-09 19:07:02 +090065/* callback routine to check for the existence of a pci dock device */
Kristen Accardi4e8662b2006-06-28 03:08:06 -040066static acpi_status
67is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
68{
69 int *count = (int *)context;
70
71 if (is_dock_device(handle)) {
72 (*count)++;
73 return AE_CTRL_TERMINATE;
74 } else {
75 return AE_OK;
76 }
77}
78
Kristen Accardi4e8662b2006-06-28 03:08:06 -040079/*
80 * the _DCK method can do funny things... and sometimes not
81 * hah-hah funny.
82 *
83 * TBD - figure out a way to only call fixups for
84 * systems that require them.
85 */
86static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
87 void *v)
88{
89 struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
90 struct pci_bus *bus = func->slot->bridge->pci_bus;
91 u32 buses;
92
93 if (!bus->self)
94 return NOTIFY_OK;
95
96 /* fixup bad _DCK function that rewrites
97 * secondary bridge on slot
98 */
99 pci_read_config_dword(bus->self,
100 PCI_PRIMARY_BUS,
101 &buses);
102
Yinghai Lub918c622012-05-17 18:51:11 -0700103 if (((buses >> 8) & 0xff) != bus->busn_res.start) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400104 buses = (buses & 0xff000000)
Alex Chiang2a9d3522008-12-11 11:17:55 -0700105 | ((unsigned int)(bus->primary) << 0)
Yinghai Lub918c622012-05-17 18:51:11 -0700106 | ((unsigned int)(bus->busn_res.start) << 8)
107 | ((unsigned int)(bus->busn_res.end) << 16);
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400108 pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
109 }
110 return NOTIFY_OK;
111}
112
113
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400114static const struct acpi_dock_ops acpiphp_dock_ops = {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800115 .handler = handle_hotplug_event_func,
116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Jiang Liu5ba113f2012-08-22 23:16:45 +0800118/* Check whether the PCI device is managed by native PCIe hotplug driver */
119static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
120{
121 u32 reg32;
122 acpi_handle tmp;
123 struct acpi_pci_root *root;
124
125 /* Check whether the PCIe port supports native PCIe hotplug */
126 if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
127 return false;
128 if (!(reg32 & PCI_EXP_SLTCAP_HPC))
129 return false;
130
131 /*
132 * Check whether native PCIe hotplug has been enabled for
133 * this PCIe hierarchy.
134 */
135 tmp = acpi_find_root_bridge_handle(pdev);
136 if (!tmp)
137 return false;
138 root = acpi_pci_find_root(tmp);
139 if (!root)
140 return false;
141 if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
142 return false;
143
144 return true;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* callback routine to register each ACPI PCI slot object */
148static acpi_status
149register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
150{
151 struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
152 struct acpiphp_slot *slot;
153 struct acpiphp_func *newfunc;
154 acpi_handle tmp;
155 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400156 unsigned long long adr, sun;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800157 int device, function, retval;
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900158 struct pci_bus *pbus = bridge->pci_bus;
Alex Chiang9d911d72009-05-21 16:21:15 -0600159 struct pci_dev *pdev;
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000160 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900162 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return AE_OK;
164
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600165 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
166 if (ACPI_FAILURE(status)) {
167 warn("can't evaluate _ADR (%#x)\n", status);
168 return AE_OK;
169 }
170
171 device = (adr >> 16) & 0xffff;
172 function = adr & 0xffff;
173
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100174 pdev = pbus->self;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800175 if (pdev && device_is_managed_by_native_pciehp(pdev))
176 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100177
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100178 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!newfunc)
180 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 INIT_LIST_HEAD(&newfunc->sibling);
183 newfunc->handle = handle;
184 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000185
186 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800187 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
190 newfunc->flags |= FUNC_HAS_STA;
191
192 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
193 newfunc->flags |= FUNC_HAS_PS0;
194
195 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
196 newfunc->flags |= FUNC_HAS_PS3;
197
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400198 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800199 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400202 if (ACPI_FAILURE(status)) {
203 /*
204 * use the count of the number of slots we've found
205 * for the number of the slot
206 */
207 sun = bridge->nr_slots+1;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* search for objects that share the same slot */
211 for (slot = bridge->slots; slot; slot = slot->next)
212 if (slot->device == device) {
213 if (slot->sun != sun)
214 warn("sibling found, but _SUN doesn't match!\n");
215 break;
216 }
217
218 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100219 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!slot) {
221 kfree(newfunc);
222 return AE_NO_MEMORY;
223 }
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 slot->device = device;
227 slot->sun = sun;
228 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100229 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 slot->next = bridge->slots;
232 bridge->slots = slot;
233
234 bridge->nr_slots++;
235
Justin Chenb6adc192008-12-11 11:16:44 -0700236 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900237 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800238 retval = acpiphp_register_hotplug_slot(slot);
239 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600240 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700241 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600242 "hotplug driver\n", slot->sun);
243 else
244 warn("acpiphp_register_hotplug_slot failed "
245 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800246 goto err_exit;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 newfunc->slot = slot;
251 list_add_tail(&newfunc->sibling, &slot->funcs);
252
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000253 if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
254 &val, 60*1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400257 if (is_dock_device(handle)) {
258 /* we don't want to call this device's _EJ0
259 * because we want the dock notify handler
260 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800261 */
262 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400263 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800264 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400265 dbg("failed to register dock device\n");
266
267 /* we need to be notified when dock events happen
268 * outside of the hotplug operation, since we may
269 * need to do fixups before we can hotplug.
270 */
271 newfunc->nb.notifier_call = post_dock_fixups;
272 if (register_dock_notifier(&newfunc->nb))
273 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800274 }
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800277 if (!(newfunc->flags & FUNC_HAS_DCK)) {
278 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 ACPI_SYSTEM_NOTIFY,
280 handle_hotplug_event_func,
281 newfunc);
282
Kristen Accardi20416ea2006-02-23 17:56:03 -0800283 if (ACPI_FAILURE(status))
284 err("failed to register interrupt notify handler\n");
285 } else
286 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kristen Accardi20416ea2006-02-23 17:56:03 -0800288 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800289
290 err_exit:
291 bridge->nr_slots--;
292 bridge->slots = slot->next;
293 kfree(slot);
294 kfree(newfunc);
295
296 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299
300/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600301static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Alex Chiang7f538662009-09-10 12:34:09 -0600303 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900304 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600305 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800306 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900307 }
308 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
312static void init_bridge_misc(struct acpiphp_bridge *bridge)
313{
314 acpi_status status;
315
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800316 /* must be added to the list prior to calling register_slot */
317 list_add(&bridge->list, &bridge_list);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* register all slot objects under this bridge */
320 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800321 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800322 if (ACPI_FAILURE(status)) {
323 list_del(&bridge->list);
324 return;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Jiang Liube6d2862013-01-31 00:10:10 +0800327 /* install notify handler for P2P bridges */
328 if (!pci_is_root_bus(bridge->pci_bus)) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900329 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
330 status = acpi_remove_notify_handler(bridge->func->handle,
331 ACPI_SYSTEM_NOTIFY,
332 handle_hotplug_event_func);
333 if (ACPI_FAILURE(status))
334 err("failed to remove notify handler\n");
335 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700336 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ACPI_SYSTEM_NOTIFY,
338 handle_hotplug_event_bridge,
339 bridge);
340
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700341 if (ACPI_FAILURE(status)) {
342 err("failed to register interrupt notify handler\n");
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900348/* find acpiphp_func from acpiphp_bridge */
349static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
350{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900351 struct acpiphp_bridge *bridge;
352 struct acpiphp_slot *slot;
353 struct acpiphp_func *func;
354
Alex Chiang58c08622009-10-26 21:25:27 -0600355 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900356 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600357 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900358 if (func->handle == handle)
359 return func;
360 }
361 }
362 }
363
364 return NULL;
365}
366
367
Rajesh Shah42f49a62005-04-28 00:25:53 -0700368static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
369{
Alex Chiang58c08622009-10-26 21:25:27 -0600370 struct acpiphp_bridge *bridge;
371
372 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700373 if (bridge->handle == handle)
374 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700375
376 return NULL;
377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Rajesh Shah364d5092005-04-28 00:25:54 -0700379static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Alex Chiang58c08622009-10-26 21:25:27 -0600381 struct acpiphp_slot *slot, *next;
382 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700383 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700384 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700385
Jiang Liube6d2862013-01-31 00:10:10 +0800386 if (!pci_is_root_bus(bridge->pci_bus)) {
Yinghai Lu668192b2013-01-21 13:20:48 -0800387 status = acpi_remove_notify_handler(handle,
388 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700389 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800390 if (ACPI_FAILURE(status))
391 err("failed to remove notify handler\n");
392 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700393
Jiang Liube6d2862013-01-31 00:10:10 +0800394 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900395 status = acpi_install_notify_handler(bridge->func->handle,
396 ACPI_SYSTEM_NOTIFY,
397 handle_hotplug_event_func,
398 bridge->func);
399 if (ACPI_FAILURE(status))
400 err("failed to install interrupt notify handler\n");
401 }
402
Rajesh Shah42f49a62005-04-28 00:25:53 -0700403 slot = bridge->slots;
404 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600405 next = slot->next;
406 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400407 if (is_dock_device(func->handle)) {
408 unregister_hotplug_dock_device(func->handle);
409 unregister_dock_notifier(&func->nb);
410 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800411 if (!(func->flags & FUNC_HAS_DCK)) {
412 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700413 ACPI_SYSTEM_NOTIFY,
414 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800415 if (ACPI_FAILURE(status))
416 err("failed to remove notify handler\n");
417 }
Alex Chiang58c08622009-10-26 21:25:27 -0600418 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700419 kfree(func);
420 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800421 acpiphp_unregister_hotplug_slot(slot);
422 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700423 kfree(slot);
424 slot = next;
425 }
426
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000427 put_device(&bridge->pci_bus->dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700428 pci_dev_put(bridge->pci_dev);
429 list_del(&bridge->list);
430 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433static int power_on_slot(struct acpiphp_slot *slot)
434{
435 acpi_status status;
436 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int retval = 0;
438
439 /* if already enabled, just skip */
440 if (slot->flags & SLOT_POWEREDON)
441 goto err_exit;
442
Alex Chiang58c08622009-10-26 21:25:27 -0600443 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800445 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
447 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800448 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 retval = -1;
450 goto err_exit;
451 } else
452 break;
453 }
454 }
455
456 /* TBD: evaluate _STA to check if the slot is enabled */
457
458 slot->flags |= SLOT_POWEREDON;
459
460 err_exit:
461 return retval;
462}
463
464
465static int power_off_slot(struct acpiphp_slot *slot)
466{
467 acpi_status status;
468 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 int retval = 0;
471
472 /* if already disabled, just skip */
473 if ((slot->flags & SLOT_POWEREDON) == 0)
474 goto err_exit;
475
Alex Chiang58c08622009-10-26 21:25:27 -0600476 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700477 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
479 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800480 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 retval = -1;
482 goto err_exit;
483 } else
484 break;
485 }
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* TBD: evaluate _STA to check if the slot is disabled */
489
490 slot->flags &= (~SLOT_POWEREDON);
491
492 err_exit:
493 return retval;
494}
495
496
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800497
498/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800499 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800500 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800501 */
502static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
503{
504 struct list_head *tmp;
505 unsigned char max, n;
506
507 /*
508 * pci_bus_max_busnr will return the highest
509 * reserved busnr for all these children.
510 * that is equivalent to the bus->subordinate
511 * value. We don't want to use the parent's
512 * bus->subordinate value because it could have
513 * padding in it.
514 */
Yinghai Lub918c622012-05-17 18:51:11 -0700515 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800516
517 list_for_each(tmp, &bus->children) {
518 n = pci_bus_max_busnr(pci_bus_b(tmp));
519 if (n > max)
520 max = n;
521 }
522 return max;
523}
524
525
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800526/**
527 * acpiphp_bus_add - add a new bus to acpi subsystem
528 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800529 */
530static int acpiphp_bus_add(struct acpiphp_func *func)
531{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100532 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800533 int ret_val;
534
Kristen Accardi20416ea2006-02-23 17:56:03 -0800535 if (!acpi_bus_get_device(func->handle, &device)) {
536 dbg("bus exists... trim\n");
537 /* this shouldn't be in here, so remove
538 * the bus then re-add it...
539 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100540 acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800541 }
542
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100543 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100544 if (!ret_val)
545 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800546
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100547 if (ret_val)
548 dbg("error adding bus, %x\n", -ret_val);
549
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800550 return ret_val;
551}
552
553
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900554/**
555 * acpiphp_bus_trim - trim a bus from acpi subsystem
556 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900557 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700558static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900559{
560 struct acpi_device *device;
561 int retval;
562
563 retval = acpi_bus_get_device(handle, &device);
564 if (retval) {
565 dbg("acpi_device not found\n");
566 return retval;
567 }
568
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100569 acpi_bus_trim(device);
570 return 0;
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900571}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800572
Shaohua Lid0607052010-02-25 10:59:34 +0800573static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
574{
575 struct acpiphp_func *func;
576 union acpi_object params[2];
577 struct acpi_object_list arg_list;
578
579 list_for_each_entry(func, &slot->funcs, sibling) {
580 arg_list.count = 2;
581 arg_list.pointer = params;
582 params[0].type = ACPI_TYPE_INTEGER;
583 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
584 params[1].type = ACPI_TYPE_INTEGER;
585 params[1].integer.value = 1;
586 /* _REG is optional, we don't care about if there is failure */
587 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
588 }
589}
590
Yinghai Lu1f96a962013-01-21 13:20:42 -0800591static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
592{
593 struct acpiphp_func *func;
594
595 if (!dev->subordinate)
596 return;
597
598 /* quirk, or pcie could set it already */
599 if (dev->is_hotplug_bridge)
600 return;
601
602 if (PCI_SLOT(dev->devfn) != slot->device)
603 return;
604
605 list_for_each_entry(func, &slot->funcs, sibling) {
606 if (PCI_FUNC(dev->devfn) == func->function) {
607 /* check if this bridge has ejectable slots */
608 if ((detect_ejectable_slots(func->handle) > 0))
609 dev->is_hotplug_bridge = 1;
610 break;
611 }
612 }
613}
Jiang Liu3b63aaa2013-04-12 05:44:26 +0000614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/**
616 * enable_device - enable, configure a slot
617 * @slot: slot to be enabled
618 *
619 * This function should be called per *physical slot*,
620 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100622static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700625 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct acpiphp_func *func;
627 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700628 int num, max, pass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 if (slot->flags & SLOT_ENABLED)
631 goto err_exit;
632
Jiang Liu2ca344e2013-01-31 00:10:09 +0800633 list_for_each_entry(func, &slot->funcs, sibling)
634 acpiphp_bus_add(func);
635
Rajesh Shah42f49a62005-04-28 00:25:53 -0700636 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
637 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800638 /* Maybe only part of funcs are added. */
639 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto err_exit;
641 }
642
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800643 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700644 for (pass = 0; pass < 2; pass++) {
645 list_for_each_entry(dev, &bus->devices, bus_list) {
646 if (PCI_SLOT(dev->devfn) != slot->device)
647 continue;
648 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800649 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700650 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800651 if (pass && dev->subordinate) {
652 check_hotplug_bridge(slot, dev);
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800653 pci_bus_size_bridges(dev->subordinate);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800654 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800655 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Rajesh Shah42f49a62005-04-28 00:25:53 -0700659 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700660 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600661 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800662 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700663 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100664
665 list_for_each_entry(dev, &bus->devices, bus_list) {
666 /* Assume that newly added devices are powered on already. */
667 if (!dev->is_added)
668 dev->current_state = PCI_D0;
669 }
670
Rajesh Shah42f49a62005-04-28 00:25:53 -0700671 pci_bus_add_devices(bus);
672
Amos Kongf382a082011-11-25 15:03:07 +0800673 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600674 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600675 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
676 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800677 if (!dev) {
678 /* Do not set SLOT_ENABLED flag if some funcs
679 are not added. */
680 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900681 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 err_exit:
687 return retval;
688}
689
Amos Kongce29ca32012-05-23 10:20:35 -0600690/* return first device in slot, acquiring a reference on it */
691static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
692{
693 struct pci_bus *bus = slot->bridge->pci_bus;
694 struct pci_dev *dev;
695 struct pci_dev *ret = NULL;
696
697 down_read(&pci_bus_sem);
698 list_for_each_entry(dev, &bus->devices, bus_list)
699 if (PCI_SLOT(dev->devfn) == slot->device) {
700 ret = pci_dev_get(dev);
701 break;
702 }
703 up_read(&pci_bus_sem);
704
705 return ret;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
709 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800710 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
712static int disable_device(struct acpiphp_slot *slot)
713{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600715 struct pci_dev *pdev;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900716
Amos Kongce29ca32012-05-23 10:20:35 -0600717 /*
718 * enable_device() enumerates all functions in this device via
719 * pci_scan_slot(), whether they have associated ACPI hotplug
720 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
721 * here.
722 */
723 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600724 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600725 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700726 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700727
Alex Chiang9d911d72009-05-21 16:21:15 -0600728 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900729 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 slot->flags &= (~SLOT_ENABLED);
733
Alex Chiang9d911d72009-05-21 16:21:15 -0600734 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737
738/**
739 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800740 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800742 * If a slot has _STA for each function and if any one of them
743 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800745 * If a slot doesn't have _STA and if any one of its functions'
746 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800748 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750static unsigned int get_slot_status(struct acpiphp_slot *slot)
751{
752 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400753 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct acpiphp_func *func;
756
Alex Chiang58c08622009-10-26 21:25:27 -0600757 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (func->flags & FUNC_HAS_STA) {
759 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
760 if (ACPI_SUCCESS(status) && sta)
761 break;
762 } else {
763 pci_bus_read_config_dword(slot->bridge->pci_bus,
764 PCI_DEVFN(slot->device,
765 func->function),
766 PCI_VENDOR_ID, &dvid);
767 if (dvid != 0xffffffff) {
768 sta = ACPI_STA_ALL;
769 break;
770 }
771 }
772 }
773
774 return (unsigned int)sta;
775}
776
777/**
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700778 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800779 * @slot: ACPI PHP slot
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700780 */
Gary Hadebfceafc2007-07-05 11:10:46 -0700781int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700782{
783 acpi_status status;
784 struct acpiphp_func *func;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700785 struct acpi_object_list arg_list;
786 union acpi_object arg;
787
Alex Chiang58c08622009-10-26 21:25:27 -0600788 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700789 /* We don't want to call _EJ0 on non-existing functions. */
790 if ((func->flags & FUNC_HAS_EJ0)) {
791 /* _EJ0 method take one argument */
792 arg_list.count = 1;
793 arg_list.pointer = &arg;
794 arg.type = ACPI_TYPE_INTEGER;
795 arg.integer.value = 1;
796
797 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
798 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800799 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700800 return -1;
801 } else
802 break;
803 }
804 }
805 return 0;
806}
807
808/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -0800810 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 *
812 * Iterate over all slots under this bridge and make sure that if a
813 * card is present they are enabled, and if not they are disabled.
814 */
815static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
816{
817 struct acpiphp_slot *slot;
818 int retval = 0;
819 int enabled, disabled;
820
821 enabled = disabled = 0;
822
823 for (slot = bridge->slots; slot; slot = slot->next) {
824 unsigned int status = get_slot_status(slot);
825 if (slot->flags & SLOT_ENABLED) {
826 if (status == ACPI_STA_ALL)
827 continue;
828 retval = acpiphp_disable_slot(slot);
829 if (retval) {
830 err("Error occurred in disabling\n");
831 goto err_exit;
Rajesh Shah8d50e3322005-04-28 00:25:57 -0700832 } else {
833 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835 disabled++;
836 } else {
837 if (status != ACPI_STA_ALL)
838 continue;
839 retval = acpiphp_enable_slot(slot);
840 if (retval) {
841 err("Error occurred in enabling\n");
842 goto err_exit;
843 }
844 enabled++;
845 }
846 }
847
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800848 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 err_exit:
851 return retval;
852}
853
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600854static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700855{
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700856 struct pci_dev *dev;
857
Bjorn Helgaase81995b2009-09-14 16:35:35 -0600858 list_for_each_entry(dev, &bus->devices, bus_list)
859 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700860}
861
862/*
863 * Remove devices for which we could not assign resources, call
864 * arch specific code to fix-up the bus
865 */
866static void acpiphp_sanitize_bus(struct pci_bus *bus)
867{
Yijing Wangd65eba62013-04-12 05:44:17 +0000868 struct pci_dev *dev, *tmp;
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700869 int i;
870 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
871
Yijing Wangd65eba62013-04-12 05:44:17 +0000872 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700873 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
874 struct resource *res = &dev->resource[i];
875 if ((res->flags & type_mask) && !res->start &&
876 res->end) {
877 /* Could not assign a required resources
878 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -0800879 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700880 break;
881 }
882 }
883 }
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * ACPI event handlers
888 */
889
Gary Hade0bbd6422007-07-05 11:10:48 -0700890static acpi_status
Gary Hade0bbd6422007-07-05 11:10:48 -0700891check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
892{
893 struct acpiphp_bridge *bridge;
894 char objname[64];
895 struct acpi_buffer buffer = { .length = sizeof(objname),
896 .pointer = objname };
897
898 bridge = acpiphp_handle_to_bridge(handle);
899 if (bridge) {
900 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
901 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800902 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -0700903 acpiphp_check_bridge(bridge);
904 }
905 return AE_OK ;
906}
907
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400908static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct acpiphp_bridge *bridge;
911 char objname[64];
912 struct acpi_buffer buffer = { .length = sizeof(objname),
913 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800914 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400915 acpi_handle handle;
916 u32 type;
917
Yinghai Lu92d8aff2013-01-21 13:20:47 -0800918 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400919 handle = hp_work->handle;
920 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +0800921 bridge = (struct acpiphp_bridge *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100923 acpi_scan_lock_acquire();
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
926
927 switch (type) {
928 case ACPI_NOTIFY_BUS_CHECK:
929 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800930 dbg("%s: Bus check notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800931 dbg("%s: re-enumerating slots under %s\n", __func__, objname);
932 acpiphp_check_bridge(bridge);
933 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
934 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 break;
936
937 case ACPI_NOTIFY_DEVICE_CHECK:
938 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800939 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 acpiphp_check_bridge(bridge);
941 break;
942
943 case ACPI_NOTIFY_DEVICE_WAKE:
944 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800945 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 break;
947
948 case ACPI_NOTIFY_EJECT_REQUEST:
949 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800950 dbg("%s: Device eject notify on %s\n", __func__, objname);
Jiang Liube6d2862013-01-31 00:10:10 +0800951 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900952 struct acpiphp_slot *slot;
953 slot = bridge->func->slot;
954 if (!acpiphp_disable_slot(slot))
955 acpiphp_eject_slot(slot);
956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 break;
958
959 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
960 printk(KERN_ERR "Device %s cannot be configured due"
961 " to a frequency mismatch\n", objname);
962 break;
963
964 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
965 printk(KERN_ERR "Device %s cannot be configured due"
966 " to a bus mode mismatch\n", objname);
967 break;
968
969 case ACPI_NOTIFY_POWER_FAULT:
970 printk(KERN_ERR "Device %s has suffered a power fault\n",
971 objname);
972 break;
973
974 default:
975 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
976 break;
977 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400978
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100979 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400980 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400984 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * @handle: Notify()'ed acpi_handle
986 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400987 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400989 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -0400991static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
992 void *context)
993{
994 /*
995 * Currently the code adds all hotplug events to the kacpid_wq
996 * queue when it should add hotplug events to the kacpi_hotplug_wq.
997 * The proper way to fix this is to reorganize the code so that
998 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
999 * For now just re-add this work to the kacpi_hotplug_wq so we
1000 * don't deadlock on hotplug actions.
1001 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001002 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001003}
1004
1005static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct acpiphp_func *func;
1008 char objname[64];
1009 struct acpi_buffer buffer = { .length = sizeof(objname),
1010 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001011 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001012 acpi_handle handle;
1013 u32 type;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001014
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001015 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001016 handle = hp_work->handle;
1017 type = hp_work->type;
Jiang Liube6d2862013-01-31 00:10:10 +08001018 func = (struct acpiphp_func *)hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001020 acpi_scan_lock_acquire();
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 switch (type) {
1025 case ACPI_NOTIFY_BUS_CHECK:
1026 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001027 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 acpiphp_enable_slot(func->slot);
1029 break;
1030
1031 case ACPI_NOTIFY_DEVICE_CHECK:
1032 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001033 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 acpiphp_check_bridge(func->slot->bridge);
1035 break;
1036
1037 case ACPI_NOTIFY_DEVICE_WAKE:
1038 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001039 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 break;
1041
1042 case ACPI_NOTIFY_EJECT_REQUEST:
1043 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001044 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001045 if (!(acpiphp_disable_slot(func->slot)))
1046 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
1048
1049 default:
1050 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1051 break;
1052 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001053
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001054 acpi_scan_lock_release();
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001055 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001058/**
1059 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1060 * @handle: Notify()'ed acpi_handle
1061 * @type: Notify code
1062 * @context: pointer to acpiphp_func structure
1063 *
1064 * Handles ACPI event notification on slots.
1065 */
1066static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1067 void *context)
1068{
1069 /*
1070 * Currently the code adds all hotplug events to the kacpid_wq
1071 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1072 * The proper way to fix this is to reorganize the code so that
1073 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1074 * For now just re-add this work to the kacpi_hotplug_wq so we
1075 * don't deadlock on hotplug actions.
1076 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001077 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001080/*
1081 * Create hotplug slots for the PCI bus.
1082 * It should always return 0 to avoid skipping following notifiers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001084void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001086 acpi_handle dummy_handle;
1087 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001089 if (acpiphp_disabled)
1090 return;
1091
1092 if (detect_ejectable_slots(handle) <= 0)
1093 return;
1094
1095 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1096 if (bridge == NULL) {
1097 err("out of memory\n");
1098 return;
1099 }
1100
1101 bridge->handle = handle;
1102 bridge->pci_dev = pci_dev_get(bus->self);
1103 bridge->pci_bus = bus;
1104
1105 /*
1106 * Grab a ref to the subordinate PCI bus in case the bus is
1107 * removed via PCI core logical hotplug. The ref pins the bus
1108 * (which we access during module unload).
1109 */
1110 get_device(&bus->dev);
1111
1112 if (!pci_is_root_bus(bridge->pci_bus) &&
1113 ACPI_SUCCESS(acpi_get_handle(bridge->handle,
1114 "_EJ0", &dummy_handle))) {
1115 dbg("found ejectable p2p bridge\n");
1116 bridge->flags |= BRIDGE_HAS_EJ0;
1117 bridge->func = acpiphp_bridge_handle_to_function(handle);
1118 }
1119
1120 init_bridge_misc(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001123/* Destroy hotplug slots associated with the PCI bus */
1124void acpiphp_remove_slots(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Jiang Liu3b63aaa2013-04-12 05:44:26 +00001126 struct acpiphp_bridge *bridge, *tmp;
1127
1128 if (acpiphp_disabled)
1129 return;
1130
1131 list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1132 if (bridge->pci_bus == bus) {
1133 cleanup_bridge(bridge);
1134 break;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/**
1139 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001140 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
1142int acpiphp_enable_slot(struct acpiphp_slot *slot)
1143{
1144 int retval;
1145
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001146 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* wake up all functions */
1149 retval = power_on_slot(slot);
1150 if (retval)
1151 goto err_exit;
1152
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001153 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* configure all functions */
1155 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001156 if (retval)
1157 power_off_slot(slot);
1158 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001159 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001160 power_off_slot(slot);
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001164 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return retval;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
1169 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001170 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
1172int acpiphp_disable_slot(struct acpiphp_slot *slot)
1173{
1174 int retval = 0;
1175
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001176 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* unconfigure all functions */
1179 retval = disable_device(slot);
1180 if (retval)
1181 goto err_exit;
1182
1183 /* power off all functions */
1184 retval = power_off_slot(slot);
1185 if (retval)
1186 goto err_exit;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001189 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return retval;
1191}
1192
1193
1194/*
1195 * slot enabled: 1
1196 * slot disabled: 0
1197 */
1198u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1199{
Rajesh Shah8d50e3322005-04-28 00:25:57 -07001200 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203
1204/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001205 * latch open: 1
1206 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
1208u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1209{
1210 unsigned int sta;
1211
1212 sta = get_slot_status(slot);
1213
Jiang Liuce15d872013-04-12 05:44:19 +00001214 return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
1217
1218/*
1219 * adapter presence : 1
1220 * absence : 0
1221 */
1222u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1223{
1224 unsigned int sta;
1225
1226 sta = get_slot_status(slot);
1227
1228 return (sta == 0) ? 0 : 1;
1229}