blob: acb7af220310b0e7736c4b9e963b5ac7505b0602 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900161 if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return AE_OK;
163
Bjorn Helgaasdfb117b2012-06-20 16:18:29 -0600164 status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
165 if (ACPI_FAILURE(status)) {
166 warn("can't evaluate _ADR (%#x)\n", status);
167 return AE_OK;
168 }
169
170 device = (adr >> 16) & 0xffff;
171 function = adr & 0xffff;
172
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100173 pdev = pbus->self;
Jiang Liu5ba113f2012-08-22 23:16:45 +0800174 if (pdev && device_is_managed_by_native_pciehp(pdev))
175 return AE_OK;
Rafael J. Wysocki619a5182011-12-13 00:02:28 +0100176
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100177 newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!newfunc)
179 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 INIT_LIST_HEAD(&newfunc->sibling);
182 newfunc->handle = handle;
183 newfunc->function = function;
Matthew Garrett56ee3252008-11-25 21:48:14 +0000184
185 if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800186 newfunc->flags = FUNC_HAS_EJ0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
189 newfunc->flags |= FUNC_HAS_STA;
190
191 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
192 newfunc->flags |= FUNC_HAS_PS0;
193
194 if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
195 newfunc->flags |= FUNC_HAS_PS3;
196
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400197 if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
Kristen Accardi20416ea2006-02-23 17:56:03 -0800198 newfunc->flags |= FUNC_HAS_DCK;
Kristen Accardi20416ea2006-02-23 17:56:03 -0800199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
Kristen Accardi95b38b32006-06-28 03:09:54 -0400201 if (ACPI_FAILURE(status)) {
202 /*
203 * use the count of the number of slots we've found
204 * for the number of the slot
205 */
206 sun = bridge->nr_slots+1;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* search for objects that share the same slot */
210 for (slot = bridge->slots; slot; slot = slot->next)
211 if (slot->device == device) {
212 if (slot->sun != sun)
213 warn("sibling found, but _SUN doesn't match!\n");
214 break;
215 }
216
217 if (!slot) {
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100218 slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!slot) {
220 kfree(newfunc);
221 return AE_NO_MEMORY;
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 slot->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 slot->device = device;
226 slot->sun = sun;
227 INIT_LIST_HEAD(&slot->funcs);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100228 mutex_init(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 slot->next = bridge->slots;
231 bridge->slots = slot;
232
233 bridge->nr_slots++;
234
Justin Chenb6adc192008-12-11 11:16:44 -0700235 dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900236 slot->sun, pci_domain_nr(pbus), pbus->number, device);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800237 retval = acpiphp_register_hotplug_slot(slot);
238 if (retval) {
Alex Chiangf46753c2008-06-10 15:28:50 -0600239 if (retval == -EBUSY)
Justin Chenb6adc192008-12-11 11:16:44 -0700240 warn("Slot %llu already registered by another "
Alex Chiangf46753c2008-06-10 15:28:50 -0600241 "hotplug driver\n", slot->sun);
242 else
243 warn("acpiphp_register_hotplug_slot failed "
244 "(err code = 0x%x)\n", retval);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800245 goto err_exit;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 newfunc->slot = slot;
250 list_add_tail(&newfunc->sibling, &slot->funcs);
251
Alex Chiang9d911d72009-05-21 16:21:15 -0600252 pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
253 if (pdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
Alex Chiang9d911d72009-05-21 16:21:15 -0600255 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400258 if (is_dock_device(handle)) {
259 /* we don't want to call this device's _EJ0
260 * because we want the dock notify handler
261 * to call it after it calls _DCK
Kristen Accardi20416ea2006-02-23 17:56:03 -0800262 */
263 newfunc->flags &= ~FUNC_HAS_EJ0;
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400264 if (register_hotplug_dock_device(handle,
Shaohua Li1253f7a2008-08-28 10:06:16 +0800265 &acpiphp_dock_ops, newfunc))
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400266 dbg("failed to register dock device\n");
267
268 /* we need to be notified when dock events happen
269 * outside of the hotplug operation, since we may
270 * need to do fixups before we can hotplug.
271 */
272 newfunc->nb.notifier_call = post_dock_fixups;
273 if (register_dock_notifier(&newfunc->nb))
274 dbg("failed to register a dock notifier");
Kristen Accardi20416ea2006-02-23 17:56:03 -0800275 }
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* install notify handler */
Kristen Accardi20416ea2006-02-23 17:56:03 -0800278 if (!(newfunc->flags & FUNC_HAS_DCK)) {
279 status = acpi_install_notify_handler(handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ACPI_SYSTEM_NOTIFY,
281 handle_hotplug_event_func,
282 newfunc);
283
Kristen Accardi20416ea2006-02-23 17:56:03 -0800284 if (ACPI_FAILURE(status))
285 err("failed to register interrupt notify handler\n");
286 } else
287 status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kristen Accardi20416ea2006-02-23 17:56:03 -0800289 return status;
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800290
291 err_exit:
292 bridge->nr_slots--;
293 bridge->slots = slot->next;
294 kfree(slot);
295 kfree(newfunc);
296
297 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
301/* see if it's worth looking at this bridge */
Alex Chiang6edd7672009-09-10 12:34:04 -0600302static int detect_ejectable_slots(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Alex Chiang7f538662009-09-10 12:34:09 -0600304 int found = acpi_pci_detect_ejectable(handle);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900305 if (!found) {
Alex Chiang6edd7672009-09-10 12:34:04 -0600306 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800307 is_pci_dock_device, NULL, (void *)&found, NULL);
Kenji Kaneshigee8c331e2008-12-17 12:09:12 +0900308 }
309 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
313static void init_bridge_misc(struct acpiphp_bridge *bridge)
314{
315 acpi_status status;
316
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800317 /* must be added to the list prior to calling register_slot */
318 list_add(&bridge->list, &bridge_list);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /* register all slot objects under this bridge */
321 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800322 register_slot, NULL, bridge, NULL);
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800323 if (ACPI_FAILURE(status)) {
324 list_del(&bridge->list);
325 return;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* install notify handler */
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700329 if (bridge->type != BRIDGE_TYPE_HOST) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900330 if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
331 status = acpi_remove_notify_handler(bridge->func->handle,
332 ACPI_SYSTEM_NOTIFY,
333 handle_hotplug_event_func);
334 if (ACPI_FAILURE(status))
335 err("failed to remove notify handler\n");
336 }
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700337 status = acpi_install_notify_handler(bridge->handle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ACPI_SYSTEM_NOTIFY,
339 handle_hotplug_event_bridge,
340 bridge);
341
Rajesh Shah8e7561c2005-04-28 00:25:56 -0700342 if (ACPI_FAILURE(status)) {
343 err("failed to register interrupt notify handler\n");
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900349/* find acpiphp_func from acpiphp_bridge */
350static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
351{
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900352 struct acpiphp_bridge *bridge;
353 struct acpiphp_slot *slot;
354 struct acpiphp_func *func;
355
Alex Chiang58c08622009-10-26 21:25:27 -0600356 list_for_each_entry(bridge, &bridge_list, list) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900357 for (slot = bridge->slots; slot; slot = slot->next) {
Alex Chiang58c08622009-10-26 21:25:27 -0600358 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900359 if (func->handle == handle)
360 return func;
361 }
362 }
363 }
364
365 return NULL;
366}
367
368
369static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
370{
371 acpi_handle dummy_handle;
372
373 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
374 "_STA", &dummy_handle)))
375 bridge->flags |= BRIDGE_HAS_STA;
376
377 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
378 "_EJ0", &dummy_handle)))
379 bridge->flags |= BRIDGE_HAS_EJ0;
380
381 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
382 "_PS0", &dummy_handle)))
383 bridge->flags |= BRIDGE_HAS_PS0;
384
385 if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
386 "_PS3", &dummy_handle)))
387 bridge->flags |= BRIDGE_HAS_PS3;
388
389 /* is this ejectable p2p bridge? */
390 if (bridge->flags & BRIDGE_HAS_EJ0) {
391 struct acpiphp_func *func;
392
393 dbg("found ejectable p2p bridge\n");
394
395 /* make link between PCI bridge and PCI function */
396 func = acpiphp_bridge_handle_to_function(bridge->handle);
397 if (!func)
398 return;
399 bridge->func = func;
400 func->bridge = bridge;
401 }
402}
403
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* allocate and initialize host bridge data structure */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900406static void add_host_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900409 acpi_handle handle = root->device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100411 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (bridge == NULL)
413 return;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 bridge->type = BRIDGE_TYPE_HOST;
416 bridge->handle = handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alex Chiang6edd7672009-09-10 12:34:04 -0600418 bridge->pci_bus = root->bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 init_bridge_misc(bridge);
421}
422
423
424/* allocate and initialize PCI-to-PCI bridge data structure */
Alex Chiang6edd7672009-09-10 12:34:04 -0600425static void add_p2p_bridge(acpi_handle *handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct acpiphp_bridge *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100429 bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (bridge == NULL) {
431 err("out of memory\n");
432 return;
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 bridge->type = BRIDGE_TYPE_P2P;
436 bridge->handle = handle;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900437 config_p2p_bridge_flags(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Alex Chiang6edd7672009-09-10 12:34:04 -0600439 bridge->pci_dev = acpi_get_pci_dev(handle);
440 bridge->pci_bus = bridge->pci_dev->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!bridge->pci_bus) {
442 err("This is not a PCI-to-PCI bridge!\n");
Rajesh Shah42f49a62005-04-28 00:25:53 -0700443 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600446 /*
447 * Grab a ref to the subordinate PCI bus in case the bus is
448 * removed via PCI core logical hotplug. The ref pins the bus
449 * (which we access during module unload).
450 */
451 get_device(&bridge->pci_bus->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 init_bridge_misc(bridge);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700454 return;
455 err:
Alex Chiang6edd7672009-09-10 12:34:04 -0600456 pci_dev_put(bridge->pci_dev);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700457 kfree(bridge);
458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461
462/* callback routine to find P2P bridges */
463static acpi_status
464find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
465{
466 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct pci_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Alex Chiang6edd7672009-09-10 12:34:04 -0600469 dev = acpi_get_pci_dev(handle);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700470 if (!dev || !dev->subordinate)
471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600474 if ((detect_ejectable_slots(handle) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
Alex Chiang6edd7672009-09-10 12:34:04 -0600476 add_p2p_bridge(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900479 /* search P2P bridges under this p2p bridge */
480 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800481 find_p2p_bridge, NULL, NULL, NULL);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900482 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900483 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Kenji Kaneshige7c8f25d2006-02-27 22:15:49 +0900484
Rajesh Shah42f49a62005-04-28 00:25:53 -0700485 out:
486 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return AE_OK;
488}
489
490
491/* find hot-pluggable slots, and then find P2P bridge */
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900492static int add_bridge(struct acpi_pci_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400495 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 acpi_handle dummy_handle;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900497 acpi_handle handle = root->device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* if the bridge doesn't have _STA, we assume it is always there */
500 status = acpi_get_handle(handle, "_STA", &dummy_handle);
501 if (ACPI_SUCCESS(status)) {
502 status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
503 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800504 dbg("%s: _STA evaluation failure\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506 }
507 if ((tmp & ACPI_STA_FUNCTIONING) == 0)
508 /* don't register this object */
509 return 0;
510 }
511
Rajesh Shah42f49a62005-04-28 00:25:53 -0700512 /* check if this bridge has ejectable slots */
Alex Chiang6edd7672009-09-10 12:34:04 -0600513 if (detect_ejectable_slots(handle) > 0) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700514 dbg("found PCI host-bus bridge with hot-pluggable slots\n");
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900515 add_host_bridge(root);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* search P2P bridges under this host bridge */
519 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800520 find_p2p_bridge, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (ACPI_FAILURE(status))
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900523 warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 return 0;
526}
527
Rajesh Shah42f49a62005-04-28 00:25:53 -0700528static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
529{
Alex Chiang58c08622009-10-26 21:25:27 -0600530 struct acpiphp_bridge *bridge;
531
532 list_for_each_entry(bridge, &bridge_list, list)
Rajesh Shah42f49a62005-04-28 00:25:53 -0700533 if (bridge->handle == handle)
534 return bridge;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700535
536 return NULL;
537}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Rajesh Shah364d5092005-04-28 00:25:54 -0700539static void cleanup_bridge(struct acpiphp_bridge *bridge)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Alex Chiang58c08622009-10-26 21:25:27 -0600541 struct acpiphp_slot *slot, *next;
542 struct acpiphp_func *func, *tmp;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700543 acpi_status status;
Rajesh Shah364d5092005-04-28 00:25:54 -0700544 acpi_handle handle = bridge->handle;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700545
Yinghai Lu668192b2013-01-21 13:20:48 -0800546 if (bridge->type != BRIDGE_TYPE_HOST) {
547 status = acpi_remove_notify_handler(handle,
548 ACPI_SYSTEM_NOTIFY,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700549 handle_hotplug_event_bridge);
Yinghai Lu668192b2013-01-21 13:20:48 -0800550 if (ACPI_FAILURE(status))
551 err("failed to remove notify handler\n");
552 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700553
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900554 if ((bridge->type != BRIDGE_TYPE_HOST) &&
555 ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
556 status = acpi_install_notify_handler(bridge->func->handle,
557 ACPI_SYSTEM_NOTIFY,
558 handle_hotplug_event_func,
559 bridge->func);
560 if (ACPI_FAILURE(status))
561 err("failed to install interrupt notify handler\n");
562 }
563
Rajesh Shah42f49a62005-04-28 00:25:53 -0700564 slot = bridge->slots;
565 while (slot) {
Alex Chiang58c08622009-10-26 21:25:27 -0600566 next = slot->next;
567 list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) {
Kristen Accardi4e8662b2006-06-28 03:08:06 -0400568 if (is_dock_device(func->handle)) {
569 unregister_hotplug_dock_device(func->handle);
570 unregister_dock_notifier(&func->nb);
571 }
Kristen Accardi20416ea2006-02-23 17:56:03 -0800572 if (!(func->flags & FUNC_HAS_DCK)) {
573 status = acpi_remove_notify_handler(func->handle,
Rajesh Shah42f49a62005-04-28 00:25:53 -0700574 ACPI_SYSTEM_NOTIFY,
575 handle_hotplug_event_func);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800576 if (ACPI_FAILURE(status))
577 err("failed to remove notify handler\n");
578 }
Alex Chiang58c08622009-10-26 21:25:27 -0600579 list_del(&func->sibling);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700580 kfree(func);
581 }
MUNEDA Takahiroe27da382006-02-23 17:56:08 -0800582 acpiphp_unregister_hotplug_slot(slot);
583 list_del(&slot->funcs);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700584 kfree(slot);
585 slot = next;
586 }
587
Alex Chiang5d4a4b22009-03-30 10:50:14 -0600588 /*
589 * Only P2P bridges have a pci_dev
590 */
591 if (bridge->pci_dev)
592 put_device(&bridge->pci_bus->dev);
593
Rajesh Shah42f49a62005-04-28 00:25:53 -0700594 pci_dev_put(bridge->pci_dev);
595 list_del(&bridge->list);
596 kfree(bridge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Rajesh Shah364d5092005-04-28 00:25:54 -0700599static acpi_status
600cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
601{
602 struct acpiphp_bridge *bridge;
603
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900604 /* cleanup p2p bridges under this P2P bridge
605 in a depth-first manner */
606 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
Lin Ming22635762009-11-13 10:06:08 +0800607 cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900608
Alex Chianga13307c2008-07-01 20:02:23 -0600609 bridge = acpiphp_handle_to_bridge(handle);
610 if (bridge)
611 cleanup_bridge(bridge);
612
Rajesh Shah364d5092005-04-28 00:25:54 -0700613 return AE_OK;
614}
615
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900616static void remove_bridge(struct acpi_pci_root *root)
Rajesh Shah364d5092005-04-28 00:25:54 -0700617{
618 struct acpiphp_bridge *bridge;
Taku Izumi55bfe3c2012-09-18 15:22:35 +0900619 acpi_handle handle = root->device->handle;
Rajesh Shah364d5092005-04-28 00:25:54 -0700620
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900621 /* cleanup p2p bridges under this host bridge
622 in a depth-first manner */
623 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +0800624 (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900625
Alex Chianga13307c2008-07-01 20:02:23 -0600626 /*
627 * On root bridges with hotplug slots directly underneath (ie,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300628 * no p2p bridge between), we call cleanup_bridge().
Alex Chianga13307c2008-07-01 20:02:23 -0600629 *
630 * The else clause cleans up root bridges that either had no
631 * hotplug slots at all, or had a p2p bridge underneath.
632 */
Rajesh Shah364d5092005-04-28 00:25:54 -0700633 bridge = acpiphp_handle_to_bridge(handle);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900634 if (bridge)
Rajesh Shah364d5092005-04-28 00:25:54 -0700635 cleanup_bridge(bridge);
Rajesh Shah364d5092005-04-28 00:25:54 -0700636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638static int power_on_slot(struct acpiphp_slot *slot)
639{
640 acpi_status status;
641 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int retval = 0;
643
644 /* if already enabled, just skip */
645 if (slot->flags & SLOT_POWEREDON)
646 goto err_exit;
647
Alex Chiang58c08622009-10-26 21:25:27 -0600648 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (func->flags & FUNC_HAS_PS0) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800650 dbg("%s: executing _PS0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
652 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800653 warn("%s: _PS0 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 retval = -1;
655 goto err_exit;
656 } else
657 break;
658 }
659 }
660
661 /* TBD: evaluate _STA to check if the slot is enabled */
662
663 slot->flags |= SLOT_POWEREDON;
664
665 err_exit:
666 return retval;
667}
668
669
670static int power_off_slot(struct acpiphp_slot *slot)
671{
672 acpi_status status;
673 struct acpiphp_func *func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 int retval = 0;
676
677 /* if already disabled, just skip */
678 if ((slot->flags & SLOT_POWEREDON) == 0)
679 goto err_exit;
680
Alex Chiang58c08622009-10-26 21:25:27 -0600681 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah2f523b12005-04-28 00:25:55 -0700682 if (func->flags & FUNC_HAS_PS3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
684 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800685 warn("%s: _PS3 failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 retval = -1;
687 goto err_exit;
688 } else
689 break;
690 }
691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* TBD: evaluate _STA to check if the slot is disabled */
694
695 slot->flags &= (~SLOT_POWEREDON);
696
697 err_exit:
698 return retval;
699}
700
701
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800702
703/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800704 * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800705 * @bus: bus to start search with
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800706 */
707static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
708{
709 struct list_head *tmp;
710 unsigned char max, n;
711
712 /*
713 * pci_bus_max_busnr will return the highest
714 * reserved busnr for all these children.
715 * that is equivalent to the bus->subordinate
716 * value. We don't want to use the parent's
717 * bus->subordinate value because it could have
718 * padding in it.
719 */
Yinghai Lub918c622012-05-17 18:51:11 -0700720 max = bus->busn_res.start;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800721
722 list_for_each(tmp, &bus->children) {
723 n = pci_bus_max_busnr(pci_bus_b(tmp));
724 if (n > max)
725 max = n;
726 }
727 return max;
728}
729
730
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800731/**
732 * acpiphp_bus_add - add a new bus to acpi subsystem
733 * @func: acpiphp_func of the bridge
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800734 */
735static int acpiphp_bus_add(struct acpiphp_func *func)
736{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100737 struct acpi_device *device;
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800738 int ret_val;
739
Kristen Accardi20416ea2006-02-23 17:56:03 -0800740 if (!acpi_bus_get_device(func->handle, &device)) {
741 dbg("bus exists... trim\n");
742 /* this shouldn't be in here, so remove
743 * the bus then re-add it...
744 */
Rafael J. Wysockiae281792013-01-15 13:23:53 +0100745 ret_val = acpi_bus_trim(device);
Kristen Accardi20416ea2006-02-23 17:56:03 -0800746 dbg("acpi_bus_trim return %x\n", ret_val);
747 }
748
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100749 ret_val = acpi_bus_scan(func->handle);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100750 if (!ret_val)
751 ret_val = acpi_bus_get_device(func->handle, &device);
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800752
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100753 if (ret_val)
754 dbg("error adding bus, %x\n", -ret_val);
755
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800756 return ret_val;
757}
758
759
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900760/**
761 * acpiphp_bus_trim - trim a bus from acpi subsystem
762 * @handle: handle to acpi namespace
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900763 */
Adrian Bunk6d47a5e2006-08-14 23:07:38 -0700764static int acpiphp_bus_trim(acpi_handle handle)
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900765{
766 struct acpi_device *device;
767 int retval;
768
769 retval = acpi_bus_get_device(handle, &device);
770 if (retval) {
771 dbg("acpi_device not found\n");
772 return retval;
773 }
774
Rafael J. Wysockiae281792013-01-15 13:23:53 +0100775 retval = acpi_bus_trim(device);
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900776 if (retval)
777 err("cannot remove from acpi list\n");
778
779 return retval;
780}
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800781
Shaohua Lid0607052010-02-25 10:59:34 +0800782static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
783{
784 struct acpiphp_func *func;
785 union acpi_object params[2];
786 struct acpi_object_list arg_list;
787
788 list_for_each_entry(func, &slot->funcs, sibling) {
789 arg_list.count = 2;
790 arg_list.pointer = params;
791 params[0].type = ACPI_TYPE_INTEGER;
792 params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
793 params[1].type = ACPI_TYPE_INTEGER;
794 params[1].integer.value = 1;
795 /* _REG is optional, we don't care about if there is failure */
796 acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
797 }
798}
799
Yinghai Lu1f96a962013-01-21 13:20:42 -0800800static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
801{
802 struct acpiphp_func *func;
803
804 if (!dev->subordinate)
805 return;
806
807 /* quirk, or pcie could set it already */
808 if (dev->is_hotplug_bridge)
809 return;
810
811 if (PCI_SLOT(dev->devfn) != slot->device)
812 return;
813
814 list_for_each_entry(func, &slot->funcs, sibling) {
815 if (PCI_FUNC(dev->devfn) == func->function) {
816 /* check if this bridge has ejectable slots */
817 if ((detect_ejectable_slots(func->handle) > 0))
818 dev->is_hotplug_bridge = 1;
819 break;
820 }
821 }
822}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/**
824 * enable_device - enable, configure a slot
825 * @slot: slot to be enabled
826 *
827 * This function should be called per *physical slot*,
828 * not per each slot object in ACPI namespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
Sam Ravnborg0ab2b572008-02-17 10:45:28 +0100830static int __ref enable_device(struct acpiphp_slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct pci_dev *dev;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700833 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct acpiphp_func *func;
835 int retval = 0;
Rajesh Shah42f49a62005-04-28 00:25:53 -0700836 int num, max, pass;
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900837 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (slot->flags & SLOT_ENABLED)
840 goto err_exit;
841
Jiang Liu2ca344e2013-01-31 00:10:09 +0800842 list_for_each_entry(func, &slot->funcs, sibling)
843 acpiphp_bus_add(func);
844
Rajesh Shah42f49a62005-04-28 00:25:53 -0700845 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
846 if (num == 0) {
Amos Kongf382a082011-11-25 15:03:07 +0800847 /* Maybe only part of funcs are added. */
848 dbg("No new device found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto err_exit;
850 }
851
Kristen Accardi15a1ae72006-02-23 17:55:58 -0800852 max = acpiphp_max_busnr(bus);
Rajesh Shah42f49a62005-04-28 00:25:53 -0700853 for (pass = 0; pass < 2; pass++) {
854 list_for_each_entry(dev, &bus->devices, bus_list) {
855 if (PCI_SLOT(dev->devfn) != slot->device)
856 continue;
857 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800858 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
Rajesh Shah42f49a62005-04-28 00:25:53 -0700859 max = pci_scan_bridge(bus, dev, max, pass);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800860 if (pass && dev->subordinate) {
861 check_hotplug_bridge(slot, dev);
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800862 pci_bus_size_bridges(dev->subordinate);
Yinghai Lu1f96a962013-01-21 13:20:42 -0800863 }
Kristen Accardic64b5ee2005-12-14 09:37:26 -0800864 }
Rajesh Shah42f49a62005-04-28 00:25:53 -0700865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Rajesh Shah42f49a62005-04-28 00:25:53 -0700868 pci_bus_assign_resources(bus);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700869 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -0600870 acpiphp_set_hpp_values(bus);
Shaohua Lid0607052010-02-25 10:59:34 +0800871 acpiphp_set_acpi_region(slot);
Kristen Accardi8e5dce32005-10-18 17:21:40 -0700872 pci_enable_bridges(bus);
Ian Campbell69643e42011-05-11 17:00:32 +0100873
874 list_for_each_entry(dev, &bus->devices, bus_list) {
875 /* Assume that newly added devices are powered on already. */
876 if (!dev->is_added)
877 dev->current_state = PCI_D0;
878 }
879
Rajesh Shah42f49a62005-04-28 00:25:53 -0700880 pci_bus_add_devices(bus);
881
Amos Kongf382a082011-11-25 15:03:07 +0800882 slot->flags |= SLOT_ENABLED;
Alex Chiang58c08622009-10-26 21:25:27 -0600883 list_for_each_entry(func, &slot->funcs, sibling) {
Alex Chiang9d911d72009-05-21 16:21:15 -0600884 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
885 func->function));
Amos Kongf382a082011-11-25 15:03:07 +0800886 if (!dev) {
887 /* Do not set SLOT_ENABLED flag if some funcs
888 are not added. */
889 slot->flags &= (~SLOT_ENABLED);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900890 continue;
Amos Kongf382a082011-11-25 15:03:07 +0800891 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900892
Alex Chiang9d911d72009-05-21 16:21:15 -0600893 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
894 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
895 pci_dev_put(dev);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900896 continue;
Alex Chiang9d911d72009-05-21 16:21:15 -0600897 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900898
899 status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
900 if (ACPI_FAILURE(status))
901 warn("find_p2p_bridge failed (error code = 0x%x)\n",
902 status);
Alex Chiang9d911d72009-05-21 16:21:15 -0600903 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 err_exit:
908 return retval;
909}
910
Amos Kongce29ca32012-05-23 10:20:35 -0600911/* return first device in slot, acquiring a reference on it */
912static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
913{
914 struct pci_bus *bus = slot->bridge->pci_bus;
915 struct pci_dev *dev;
916 struct pci_dev *ret = NULL;
917
918 down_read(&pci_bus_sem);
919 list_for_each_entry(dev, &bus->devices, bus_list)
920 if (PCI_SLOT(dev->devfn) == slot->device) {
921 ret = pci_dev_get(dev);
922 break;
923 }
924 up_read(&pci_bus_sem);
925
926 return ret;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/**
930 * disable_device - disable a slot
Randy Dunlap26e6c662007-11-28 09:04:30 -0800931 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933static int disable_device(struct acpiphp_slot *slot)
934{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct acpiphp_func *func;
Alex Chiang9d911d72009-05-21 16:21:15 -0600936 struct pci_dev *pdev;
Amos Kongf382a082011-11-25 15:03:07 +0800937 struct pci_bus *bus = slot->bridge->pci_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Amos Kongf382a082011-11-25 15:03:07 +0800939 /* The slot will be enabled when func 0 is added, so check
940 func 0 before disable the slot. */
941 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
942 if (!pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto err_exit;
Amos Kong638f2932012-05-22 21:58:40 -0600944 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Alex Chiang9d911d72009-05-21 16:21:15 -0600946 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900947 if (func->bridge) {
948 /* cleanup p2p bridges under this P2P bridge */
949 cleanup_p2p_bridge(func->bridge->handle,
950 (u32)1, NULL, NULL);
951 func->bridge = NULL;
952 }
Amos Kongce29ca32012-05-23 10:20:35 -0600953 }
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +0900954
Amos Kongce29ca32012-05-23 10:20:35 -0600955 /*
956 * enable_device() enumerates all functions in this device via
957 * pci_scan_slot(), whether they have associated ACPI hotplug
958 * methods (_EJ0, etc.) or not. Therefore, we remove all functions
959 * here.
960 */
961 while ((pdev = dev_in_slot(slot))) {
Bjorn Helgaas34e54842012-08-17 10:03:47 -0600962 pci_stop_and_remove_bus_device(pdev);
Amos Kongce29ca32012-05-23 10:20:35 -0600963 pci_dev_put(pdev);
Satoru Takeuchi600812e2006-09-12 10:22:53 -0700964 }
Satoru Takeuchi0dad3512006-09-12 10:17:46 -0700965
Alex Chiang9d911d72009-05-21 16:21:15 -0600966 list_for_each_entry(func, &slot->funcs, sibling) {
MUNEDA Takahiro92c9be92006-03-22 14:49:09 +0900967 acpiphp_bus_trim(func->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969
970 slot->flags &= (~SLOT_ENABLED);
971
Alex Chiang9d911d72009-05-21 16:21:15 -0600972err_exit:
973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976
977/**
978 * get_slot_status - get ACPI slot status
Randy Dunlap26e6c662007-11-28 09:04:30 -0800979 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800981 * If a slot has _STA for each function and if any one of them
982 * returned non-zero status, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800984 * If a slot doesn't have _STA and if any one of its functions'
985 * configuration space is configured, return 0x0f as a _STA.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800987 * Otherwise return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
989static unsigned int get_slot_status(struct acpiphp_slot *slot)
990{
991 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400992 unsigned long long sta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 u32 dvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct acpiphp_func *func;
995
Alex Chiang58c08622009-10-26 21:25:27 -0600996 list_for_each_entry(func, &slot->funcs, sibling) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (func->flags & FUNC_HAS_STA) {
998 status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
999 if (ACPI_SUCCESS(status) && sta)
1000 break;
1001 } else {
1002 pci_bus_read_config_dword(slot->bridge->pci_bus,
1003 PCI_DEVFN(slot->device,
1004 func->function),
1005 PCI_VENDOR_ID, &dvid);
1006 if (dvid != 0xffffffff) {
1007 sta = ACPI_STA_ALL;
1008 break;
1009 }
1010 }
1011 }
1012
1013 return (unsigned int)sta;
1014}
1015
1016/**
Rajesh Shah8d50e332005-04-28 00:25:57 -07001017 * acpiphp_eject_slot - physically eject the slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001018 * @slot: ACPI PHP slot
Rajesh Shah8d50e332005-04-28 00:25:57 -07001019 */
Gary Hadebfceafc2007-07-05 11:10:46 -07001020int acpiphp_eject_slot(struct acpiphp_slot *slot)
Rajesh Shah8d50e332005-04-28 00:25:57 -07001021{
1022 acpi_status status;
1023 struct acpiphp_func *func;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001024 struct acpi_object_list arg_list;
1025 union acpi_object arg;
1026
Alex Chiang58c08622009-10-26 21:25:27 -06001027 list_for_each_entry(func, &slot->funcs, sibling) {
Rajesh Shah8d50e332005-04-28 00:25:57 -07001028 /* We don't want to call _EJ0 on non-existing functions. */
1029 if ((func->flags & FUNC_HAS_EJ0)) {
1030 /* _EJ0 method take one argument */
1031 arg_list.count = 1;
1032 arg_list.pointer = &arg;
1033 arg.type = ACPI_TYPE_INTEGER;
1034 arg.integer.value = 1;
1035
1036 status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
1037 if (ACPI_FAILURE(status)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001038 warn("%s: _EJ0 failed\n", __func__);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001039 return -1;
1040 } else
1041 break;
1042 }
1043 }
1044 return 0;
1045}
1046
1047/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * acpiphp_check_bridge - re-enumerate devices
Randy Dunlap26e6c662007-11-28 09:04:30 -08001049 * @bridge: where to begin re-enumeration
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 *
1051 * Iterate over all slots under this bridge and make sure that if a
1052 * card is present they are enabled, and if not they are disabled.
1053 */
1054static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
1055{
1056 struct acpiphp_slot *slot;
1057 int retval = 0;
1058 int enabled, disabled;
1059
1060 enabled = disabled = 0;
1061
1062 for (slot = bridge->slots; slot; slot = slot->next) {
1063 unsigned int status = get_slot_status(slot);
1064 if (slot->flags & SLOT_ENABLED) {
1065 if (status == ACPI_STA_ALL)
1066 continue;
1067 retval = acpiphp_disable_slot(slot);
1068 if (retval) {
1069 err("Error occurred in disabling\n");
1070 goto err_exit;
Rajesh Shah8d50e332005-04-28 00:25:57 -07001071 } else {
1072 acpiphp_eject_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
1074 disabled++;
1075 } else {
1076 if (status != ACPI_STA_ALL)
1077 continue;
1078 retval = acpiphp_enable_slot(slot);
1079 if (retval) {
1080 err("Error occurred in enabling\n");
1081 goto err_exit;
1082 }
1083 enabled++;
1084 }
1085 }
1086
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001087 dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 err_exit:
1090 return retval;
1091}
1092
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001093static void acpiphp_set_hpp_values(struct pci_bus *bus)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001094{
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001095 struct pci_dev *dev;
1096
Bjorn Helgaase81995b2009-09-14 16:35:35 -06001097 list_for_each_entry(dev, &bus->devices, bus_list)
1098 pci_configure_slot(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001099}
1100
1101/*
1102 * Remove devices for which we could not assign resources, call
1103 * arch specific code to fix-up the bus
1104 */
1105static void acpiphp_sanitize_bus(struct pci_bus *bus)
1106{
1107 struct pci_dev *dev;
1108 int i;
1109 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
1110
1111 list_for_each_entry(dev, &bus->devices, bus_list) {
1112 for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
1113 struct resource *res = &dev->resource[i];
1114 if ((res->flags & type_mask) && !res->start &&
1115 res->end) {
1116 /* Could not assign a required resources
1117 * for this device, remove it */
Yinghai Lu210647a2012-02-25 13:54:20 -08001118 pci_stop_and_remove_bus_device(dev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001119 break;
1120 }
1121 }
1122 }
1123}
1124
1125/* Program resources in newly inserted bridge */
Yinghai Lu668192b2013-01-21 13:20:48 -08001126static int acpiphp_configure_p2p_bridge(acpi_handle handle)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001127{
Yinghai Lu668192b2013-01-21 13:20:48 -08001128 struct pci_dev *pdev = acpi_get_pci_dev(handle);
1129 struct pci_bus *bus = pdev->subordinate;
Alex Chiang7f538662009-09-10 12:34:09 -06001130
Yinghai Lu668192b2013-01-21 13:20:48 -08001131 pci_dev_put(pdev);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001132
1133 pci_bus_size_bridges(bus);
1134 pci_bus_assign_resources(bus);
1135 acpiphp_sanitize_bus(bus);
Bjorn Helgaasfca68252009-09-14 16:35:10 -06001136 acpiphp_set_hpp_values(bus);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001137 pci_enable_bridges(bus);
1138 return 0;
1139}
1140
Yinghai Lu668192b2013-01-21 13:20:48 -08001141static void handle_p2p_bridge_insertion(acpi_handle handle, u32 type)
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001142{
Rafael J. Wysocki636458d2012-12-21 00:36:47 +01001143 struct acpi_device *device;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001144
1145 if ((type != ACPI_NOTIFY_BUS_CHECK) &&
1146 (type != ACPI_NOTIFY_DEVICE_CHECK)) {
1147 err("unexpected notification type %d\n", type);
1148 return;
1149 }
1150
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001151 if (acpi_bus_scan(handle)) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001152 err("cannot add bridge to acpi list\n");
1153 return;
1154 }
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001155 if (acpi_bus_get_device(handle, &device)) {
1156 err("ACPI device object missing\n");
1157 return;
1158 }
Yinghai Lu668192b2013-01-21 13:20:48 -08001159 if (!acpiphp_configure_p2p_bridge(handle))
1160 add_p2p_bridge(handle);
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001161 else
1162 err("cannot configure and start bridge\n");
1163
1164}
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166/*
1167 * ACPI event handlers
1168 */
1169
Gary Hade0bbd6422007-07-05 11:10:48 -07001170static acpi_status
1171count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1172{
1173 int *count = (int *)context;
1174 struct acpiphp_bridge *bridge;
1175
1176 bridge = acpiphp_handle_to_bridge(handle);
1177 if (bridge)
1178 (*count)++;
1179 return AE_OK ;
1180}
1181
1182static acpi_status
1183check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
1184{
1185 struct acpiphp_bridge *bridge;
1186 char objname[64];
1187 struct acpi_buffer buffer = { .length = sizeof(objname),
1188 .pointer = objname };
1189
1190 bridge = acpiphp_handle_to_bridge(handle);
1191 if (bridge) {
1192 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1193 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001194 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001195 acpiphp_check_bridge(bridge);
1196 }
1197 return AE_OK ;
1198}
1199
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001200static void _handle_hotplug_event_bridge(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct acpiphp_bridge *bridge;
1203 char objname[64];
1204 struct acpi_buffer buffer = { .length = sizeof(objname),
1205 .pointer = objname };
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001206 struct acpi_device *device;
Gary Hade0bbd6422007-07-05 11:10:48 -07001207 int num_sub_bridges = 0;
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001208 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001209 acpi_handle handle;
1210 u32 type;
1211
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001212 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001213 handle = hp_work->handle;
1214 type = hp_work->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001216 if (acpi_bus_get_device(handle, &device)) {
1217 /* This bridge must have just been physically inserted */
Yinghai Lu668192b2013-01-21 13:20:48 -08001218 handle_p2p_bridge_insertion(handle, type);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001219 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001220 }
1221
1222 bridge = acpiphp_handle_to_bridge(handle);
Gary Hade0bbd6422007-07-05 11:10:48 -07001223 if (type == ACPI_NOTIFY_BUS_CHECK) {
1224 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001225 count_sub_bridges, NULL, &num_sub_bridges, NULL);
Gary Hade0bbd6422007-07-05 11:10:48 -07001226 }
1227
1228 if (!bridge && !num_sub_bridges) {
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001229 err("cannot get bridge info\n");
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001230 goto out;
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1234
1235 switch (type) {
1236 case ACPI_NOTIFY_BUS_CHECK:
1237 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001238 dbg("%s: Bus check notify on %s\n", __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001239 if (bridge) {
1240 dbg("%s: re-enumerating slots under %s\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001241 __func__, objname);
Gary Hade0bbd6422007-07-05 11:10:48 -07001242 acpiphp_check_bridge(bridge);
1243 }
1244 if (num_sub_bridges)
1245 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
Lin Ming22635762009-11-13 10:06:08 +08001246 ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248
1249 case ACPI_NOTIFY_DEVICE_CHECK:
1250 /* device check */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001251 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 acpiphp_check_bridge(bridge);
1253 break;
1254
1255 case ACPI_NOTIFY_DEVICE_WAKE:
1256 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001257 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
1259
1260 case ACPI_NOTIFY_EJECT_REQUEST:
1261 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001262 dbg("%s: Device eject notify on %s\n", __func__, objname);
MUNEDA Takahiro551bcb72006-03-22 14:49:20 +09001263 if ((bridge->type != BRIDGE_TYPE_HOST) &&
1264 (bridge->flags & BRIDGE_HAS_EJ0)) {
1265 struct acpiphp_slot *slot;
1266 slot = bridge->func->slot;
1267 if (!acpiphp_disable_slot(slot))
1268 acpiphp_eject_slot(slot);
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
1271
1272 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1273 printk(KERN_ERR "Device %s cannot be configured due"
1274 " to a frequency mismatch\n", objname);
1275 break;
1276
1277 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1278 printk(KERN_ERR "Device %s cannot be configured due"
1279 " to a bus mode mismatch\n", objname);
1280 break;
1281
1282 case ACPI_NOTIFY_POWER_FAULT:
1283 printk(KERN_ERR "Device %s has suffered a power fault\n",
1284 objname);
1285 break;
1286
1287 default:
1288 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1289 break;
1290 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001291
1292out:
1293 kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/**
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001297 * handle_hotplug_event_bridge - handle ACPI event on bridges
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * @handle: Notify()'ed acpi_handle
1299 * @type: Notify code
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001300 * @context: pointer to acpiphp_bridge structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001302 * Handles ACPI event notification on {host,p2p} bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001304static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1305 void *context)
1306{
1307 /*
1308 * Currently the code adds all hotplug events to the kacpid_wq
1309 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1310 * The proper way to fix this is to reorganize the code so that
1311 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1312 * For now just re-add this work to the kacpi_hotplug_wq so we
1313 * don't deadlock on hotplug actions.
1314 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001315 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001316}
1317
1318static void _handle_hotplug_event_func(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct acpiphp_func *func;
1321 char objname[64];
1322 struct acpi_buffer buffer = { .length = sizeof(objname),
1323 .pointer = objname };
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001324 struct acpi_hp_work *hp_work;
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001325 acpi_handle handle;
1326 u32 type;
1327 void *context;
1328
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001329 hp_work = container_of(work, struct acpi_hp_work, work);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001330 handle = hp_work->handle;
1331 type = hp_work->type;
1332 context = hp_work->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1335
1336 func = (struct acpiphp_func *)context;
1337
1338 switch (type) {
1339 case ACPI_NOTIFY_BUS_CHECK:
1340 /* bus re-enumerate */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001341 dbg("%s: Bus check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 acpiphp_enable_slot(func->slot);
1343 break;
1344
1345 case ACPI_NOTIFY_DEVICE_CHECK:
1346 /* device check : re-enumerate from parent bus */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001347 dbg("%s: Device check notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 acpiphp_check_bridge(func->slot->bridge);
1349 break;
1350
1351 case ACPI_NOTIFY_DEVICE_WAKE:
1352 /* wake event */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001353 dbg("%s: Device wake notify on %s\n", __func__, objname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
1355
1356 case ACPI_NOTIFY_EJECT_REQUEST:
1357 /* request device eject */
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001358 dbg("%s: Device eject notify on %s\n", __func__, objname);
Rajesh Shah8d50e332005-04-28 00:25:57 -07001359 if (!(acpiphp_disable_slot(func->slot)))
1360 acpiphp_eject_slot(func->slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362
1363 default:
1364 warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1365 break;
1366 }
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001367
1368 kfree(hp_work); /* allocated in handle_hotplug_event_func */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001371/**
1372 * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1373 * @handle: Notify()'ed acpi_handle
1374 * @type: Notify code
1375 * @context: pointer to acpiphp_func structure
1376 *
1377 * Handles ACPI event notification on slots.
1378 */
1379static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1380 void *context)
1381{
1382 /*
1383 * Currently the code adds all hotplug events to the kacpid_wq
1384 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1385 * The proper way to fix this is to reorganize the code so that
1386 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1387 * For now just re-add this work to the kacpi_hotplug_wq so we
1388 * don't deadlock on hotplug actions.
1389 */
Yinghai Lu92d8aff2013-01-21 13:20:47 -08001390 alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
Prarit Bhargava6af8bef2011-09-28 19:40:53 -04001391}
Rajesh Shah8e7561c2005-04-28 00:25:56 -07001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393static struct acpi_pci_driver acpi_pci_hp_driver = {
1394 .add = add_bridge,
1395 .remove = remove_bridge,
1396};
1397
1398/**
1399 * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401int __init acpiphp_glue_init(void)
1402{
Yinghai Lu668192b2013-01-21 13:20:48 -08001403 acpi_pci_register_driver(&acpi_pci_hp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 return 0;
1406}
1407
1408
1409/**
1410 * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
1411 *
Randy Dunlap26e6c662007-11-28 09:04:30 -08001412 * This function frees all data allocated in acpiphp_glue_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
Kristen Carlson Accardi031f30d2006-12-16 15:26:04 -08001414void acpiphp_glue_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 acpi_pci_unregister_driver(&acpi_pci_hp_driver);
1417}
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/**
1420 * acpiphp_enable_slot - power on slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001421 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
1423int acpiphp_enable_slot(struct acpiphp_slot *slot)
1424{
1425 int retval;
1426
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001427 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* wake up all functions */
1430 retval = power_on_slot(slot);
1431 if (retval)
1432 goto err_exit;
1433
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001434 if (get_slot_status(slot) == ACPI_STA_ALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* configure all functions */
1436 retval = enable_device(slot);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001437 if (retval)
1438 power_off_slot(slot);
1439 } else {
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001440 dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
MUNEDA Takahirocde0e5d2006-03-22 14:49:33 +09001441 power_off_slot(slot);
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001445 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return retval;
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/**
1450 * acpiphp_disable_slot - power off slot
Randy Dunlap26e6c662007-11-28 09:04:30 -08001451 * @slot: ACPI PHP slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
1453int acpiphp_disable_slot(struct acpiphp_slot *slot)
1454{
1455 int retval = 0;
1456
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001457 mutex_lock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* unconfigure all functions */
1460 retval = disable_device(slot);
1461 if (retval)
1462 goto err_exit;
1463
1464 /* power off all functions */
1465 retval = power_off_slot(slot);
1466 if (retval)
1467 goto err_exit;
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 err_exit:
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +01001470 mutex_unlock(&slot->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return retval;
1472}
1473
1474
1475/*
1476 * slot enabled: 1
1477 * slot disabled: 0
1478 */
1479u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1480{
Rajesh Shah8d50e332005-04-28 00:25:57 -07001481 return (slot->flags & SLOT_POWEREDON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484
1485/*
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001486 * latch open: 1
1487 * latch closed: 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
1489u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1490{
1491 unsigned int sta;
1492
1493 sta = get_slot_status(slot);
1494
MUNEDA Takahiro35ae61a2006-10-25 11:44:57 -07001495 return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498
1499/*
1500 * adapter presence : 1
1501 * absence : 0
1502 */
1503u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1504{
1505 unsigned int sta;
1506
1507 sta = get_slot_status(slot);
1508
1509 return (sta == 0) ? 0 : 1;
1510}