Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 7 | * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com) |
| 8 | * Copyright (C) 2003-2005 Hewlett Packard |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 9 | * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com) |
| 10 | * Copyright (C) 2005 Intel Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 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 Accardi | 998be20 | 2006-07-26 10:52:33 -0700 | [diff] [blame] | 29 | * Send feedback to <kristen.c.accardi@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
| 31 | */ |
| 32 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Lifetime rules for pci_dev: |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 35 | * - 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 Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 38 | * - 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 Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 40 | */ |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/init.h> |
| 43 | #include <linux/module.h> |
| 44 | |
| 45 | #include <linux/kernel.h> |
| 46 | #include <linux/pci.h> |
Greg Kroah-Hartman | 7a54f25 | 2006-10-13 20:05:19 -0700 | [diff] [blame] | 47 | #include <linux/pci_hotplug.h> |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 48 | #include <linux/pci-acpi.h> |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 49 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 51 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #include "../pci.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include "acpiphp.h" |
| 55 | |
| 56 | static LIST_HEAD(bridge_list); |
| 57 | |
| 58 | #define MY_NAME "acpiphp_glue" |
| 59 | |
| 60 | static void handle_hotplug_event_bridge (acpi_handle, u32, void *); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 61 | static void acpiphp_sanitize_bus(struct pci_bus *bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 62 | static void acpiphp_set_hpp_values(struct pci_bus *bus); |
Len Brown | 2b85e13 | 2006-06-27 01:50:14 -0400 | [diff] [blame] | 63 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 64 | |
MUNEDA Takahiro | 5a340ed | 2007-11-09 19:07:02 +0900 | [diff] [blame] | 65 | /* callback routine to check for the existence of a pci dock device */ |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 66 | static acpi_status |
| 67 | is_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 Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 79 | /* |
| 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 | */ |
| 86 | static 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 Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 103 | if (((buses >> 8) & 0xff) != bus->busn_res.start) { |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 104 | buses = (buses & 0xff000000) |
Alex Chiang | 2a9d352 | 2008-12-11 11:17:55 -0700 | [diff] [blame] | 105 | | ((unsigned int)(bus->primary) << 0) |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 106 | | ((unsigned int)(bus->busn_res.start) << 8) |
| 107 | | ((unsigned int)(bus->busn_res.end) << 16); |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 108 | pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses); |
| 109 | } |
| 110 | return NOTIFY_OK; |
| 111 | } |
| 112 | |
| 113 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 114 | static const struct acpi_dock_ops acpiphp_dock_ops = { |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 115 | .handler = handle_hotplug_event_func, |
| 116 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Jiang Liu | 5ba113f | 2012-08-22 23:16:45 +0800 | [diff] [blame] | 118 | /* Check whether the PCI device is managed by native PCIe hotplug driver */ |
| 119 | static 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, ®32)) |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* callback routine to register each ACPI PCI slot object */ |
| 148 | static acpi_status |
| 149 | register_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 Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 156 | unsigned long long adr, sun; |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 157 | int device, function, retval; |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 158 | struct pci_bus *pbus = bridge->pci_bus; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 159 | struct pci_dev *pdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 161 | if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return AE_OK; |
| 163 | |
Bjorn Helgaas | dfb117b | 2012-06-20 16:18:29 -0600 | [diff] [blame] | 164 | 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. Wysocki | 619a518 | 2011-12-13 00:02:28 +0100 | [diff] [blame] | 173 | pdev = pbus->self; |
Jiang Liu | 5ba113f | 2012-08-22 23:16:45 +0800 | [diff] [blame] | 174 | if (pdev && device_is_managed_by_native_pciehp(pdev)) |
| 175 | return AE_OK; |
Rafael J. Wysocki | 619a518 | 2011-12-13 00:02:28 +0100 | [diff] [blame] | 176 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 177 | newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | if (!newfunc) |
| 179 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | INIT_LIST_HEAD(&newfunc->sibling); |
| 182 | newfunc->handle = handle; |
| 183 | newfunc->function = function; |
Matthew Garrett | 56ee325 | 2008-11-25 21:48:14 +0000 | [diff] [blame] | 184 | |
| 185 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp))) |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 186 | newfunc->flags = FUNC_HAS_EJ0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 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 Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 197 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp))) |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 198 | newfunc->flags |= FUNC_HAS_DCK; |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun); |
Kristen Accardi | 95b38b3 | 2006-06-28 03:09:54 -0400 | [diff] [blame] | 201 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 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 Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 218 | slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (!slot) { |
| 220 | kfree(newfunc); |
| 221 | return AE_NO_MEMORY; |
| 222 | } |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | slot->bridge = bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | slot->device = device; |
| 226 | slot->sun = sun; |
| 227 | INIT_LIST_HEAD(&slot->funcs); |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 228 | mutex_init(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | slot->next = bridge->slots; |
| 231 | bridge->slots = slot; |
| 232 | |
| 233 | bridge->nr_slots++; |
| 234 | |
Justin Chen | b6adc19 | 2008-12-11 11:16:44 -0700 | [diff] [blame] | 235 | dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n", |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 236 | slot->sun, pci_domain_nr(pbus), pbus->number, device); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 237 | retval = acpiphp_register_hotplug_slot(slot); |
| 238 | if (retval) { |
Alex Chiang | f46753c | 2008-06-10 15:28:50 -0600 | [diff] [blame] | 239 | if (retval == -EBUSY) |
Justin Chen | b6adc19 | 2008-12-11 11:16:44 -0700 | [diff] [blame] | 240 | warn("Slot %llu already registered by another " |
Alex Chiang | f46753c | 2008-06-10 15:28:50 -0600 | [diff] [blame] | 241 | "hotplug driver\n", slot->sun); |
| 242 | else |
| 243 | warn("acpiphp_register_hotplug_slot failed " |
| 244 | "(err code = 0x%x)\n", retval); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 245 | goto err_exit; |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | newfunc->slot = slot; |
| 250 | list_add_tail(&newfunc->sibling, &slot->funcs); |
| 251 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 252 | pdev = pci_get_slot(pbus, PCI_DEVFN(device, function)); |
| 253 | if (pdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON); |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 255 | pci_dev_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 258 | 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 Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 262 | */ |
| 263 | newfunc->flags &= ~FUNC_HAS_EJ0; |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 264 | if (register_hotplug_dock_device(handle, |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 265 | &acpiphp_dock_ops, newfunc)) |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 266 | 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 Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* install notify handler */ |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 278 | if (!(newfunc->flags & FUNC_HAS_DCK)) { |
| 279 | status = acpi_install_notify_handler(handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | ACPI_SYSTEM_NOTIFY, |
| 281 | handle_hotplug_event_func, |
| 282 | newfunc); |
| 283 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 284 | if (ACPI_FAILURE(status)) |
| 285 | err("failed to register interrupt notify handler\n"); |
| 286 | } else |
| 287 | status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 289 | return status; |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 290 | |
| 291 | err_exit: |
| 292 | bridge->nr_slots--; |
| 293 | bridge->slots = slot->next; |
| 294 | kfree(slot); |
| 295 | kfree(newfunc); |
| 296 | |
| 297 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
| 301 | /* see if it's worth looking at this bridge */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 302 | static int detect_ejectable_slots(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Alex Chiang | 7f53866 | 2009-09-10 12:34:09 -0600 | [diff] [blame] | 304 | int found = acpi_pci_detect_ejectable(handle); |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 305 | if (!found) { |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 306 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 307 | is_pci_dock_device, NULL, (void *)&found, NULL); |
Kenji Kaneshige | e8c331e | 2008-12-17 12:09:12 +0900 | [diff] [blame] | 308 | } |
| 309 | return found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */ |
| 313 | static void init_bridge_misc(struct acpiphp_bridge *bridge) |
| 314 | { |
| 315 | acpi_status status; |
| 316 | |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 317 | /* must be added to the list prior to calling register_slot */ |
| 318 | list_add(&bridge->list, &bridge_list); |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | /* register all slot objects under this bridge */ |
| 321 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 322 | register_slot, NULL, bridge, NULL); |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 323 | if (ACPI_FAILURE(status)) { |
| 324 | list_del(&bridge->list); |
| 325 | return; |
| 326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 328 | /* install notify handler for P2P bridges */ |
| 329 | if (!pci_is_root_bus(bridge->pci_bus)) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 330 | 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 Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 337 | status = acpi_install_notify_handler(bridge->handle, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | ACPI_SYSTEM_NOTIFY, |
| 339 | handle_hotplug_event_bridge, |
| 340 | bridge); |
| 341 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 342 | if (ACPI_FAILURE(status)) { |
| 343 | err("failed to register interrupt notify handler\n"); |
| 344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 349 | /* find acpiphp_func from acpiphp_bridge */ |
| 350 | static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle) |
| 351 | { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 352 | struct acpiphp_bridge *bridge; |
| 353 | struct acpiphp_slot *slot; |
| 354 | struct acpiphp_func *func; |
| 355 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 356 | list_for_each_entry(bridge, &bridge_list, list) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 357 | for (slot = bridge->slots; slot; slot = slot->next) { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 358 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 359 | if (func->handle == handle) |
| 360 | return func; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge) |
| 370 | { |
| 371 | acpi_handle dummy_handle; |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 372 | struct acpiphp_func *func; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 373 | |
| 374 | if (ACPI_SUCCESS(acpi_get_handle(bridge->handle, |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 375 | "_EJ0", &dummy_handle))) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 376 | bridge->flags |= BRIDGE_HAS_EJ0; |
| 377 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 378 | dbg("found ejectable p2p bridge\n"); |
| 379 | |
| 380 | /* make link between PCI bridge and PCI function */ |
| 381 | func = acpiphp_bridge_handle_to_function(bridge->handle); |
| 382 | if (!func) |
| 383 | return; |
| 384 | bridge->func = func; |
| 385 | func->bridge = bridge; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* allocate and initialize host bridge data structure */ |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 391 | static void add_host_bridge(struct acpi_pci_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | struct acpiphp_bridge *bridge; |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 394 | acpi_handle handle = root->device->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 396 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (bridge == NULL) |
| 398 | return; |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | bridge->handle = handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 402 | bridge->pci_bus = root->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | init_bridge_misc(bridge); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | /* allocate and initialize PCI-to-PCI bridge data structure */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 409 | static void add_p2p_bridge(acpi_handle *handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
| 411 | struct acpiphp_bridge *bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 413 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (bridge == NULL) { |
| 415 | err("out of memory\n"); |
| 416 | return; |
| 417 | } |
| 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | bridge->handle = handle; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 420 | config_p2p_bridge_flags(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 422 | bridge->pci_dev = acpi_get_pci_dev(handle); |
| 423 | bridge->pci_bus = bridge->pci_dev->subordinate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (!bridge->pci_bus) { |
| 425 | err("This is not a PCI-to-PCI bridge!\n"); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 426 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Alex Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 429 | /* |
| 430 | * Grab a ref to the subordinate PCI bus in case the bus is |
| 431 | * removed via PCI core logical hotplug. The ref pins the bus |
| 432 | * (which we access during module unload). |
| 433 | */ |
| 434 | get_device(&bridge->pci_bus->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | init_bridge_misc(bridge); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 437 | return; |
| 438 | err: |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 439 | pci_dev_put(bridge->pci_dev); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 440 | kfree(bridge); |
| 441 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | |
| 445 | /* callback routine to find P2P bridges */ |
| 446 | static acpi_status |
| 447 | find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 448 | { |
| 449 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | struct pci_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 452 | dev = acpi_get_pci_dev(handle); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 453 | if (!dev || !dev->subordinate) |
| 454 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | /* check if this bridge has ejectable slots */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 457 | if ((detect_ejectable_slots(handle) > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev)); |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 459 | add_p2p_bridge(handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 462 | /* search P2P bridges under this p2p bridge */ |
| 463 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 464 | find_p2p_bridge, NULL, NULL, NULL); |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 465 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 466 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 467 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 468 | out: |
| 469 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return AE_OK; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* find hot-pluggable slots, and then find P2P bridge */ |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 475 | static int add_bridge(struct acpi_pci_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 478 | unsigned long long tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | acpi_handle dummy_handle; |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 480 | acpi_handle handle = root->device->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | /* if the bridge doesn't have _STA, we assume it is always there */ |
| 483 | status = acpi_get_handle(handle, "_STA", &dummy_handle); |
| 484 | if (ACPI_SUCCESS(status)) { |
| 485 | status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp); |
| 486 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 487 | dbg("%s: _STA evaluation failure\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | if ((tmp & ACPI_STA_FUNCTIONING) == 0) |
| 491 | /* don't register this object */ |
| 492 | return 0; |
| 493 | } |
| 494 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 495 | /* check if this bridge has ejectable slots */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 496 | if (detect_ejectable_slots(handle) > 0) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 497 | dbg("found PCI host-bus bridge with hot-pluggable slots\n"); |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 498 | add_host_bridge(root); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | /* search P2P bridges under this host bridge */ |
| 502 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 503 | find_p2p_bridge, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 506 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 511 | static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) |
| 512 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 513 | struct acpiphp_bridge *bridge; |
| 514 | |
| 515 | list_for_each_entry(bridge, &bridge_list, list) |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 516 | if (bridge->handle == handle) |
| 517 | return bridge; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 518 | |
| 519 | return NULL; |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 522 | static void cleanup_bridge(struct acpiphp_bridge *bridge) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 524 | struct acpiphp_slot *slot, *next; |
| 525 | struct acpiphp_func *func, *tmp; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 526 | acpi_status status; |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 527 | acpi_handle handle = bridge->handle; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 528 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 529 | if (!pci_is_root_bus(bridge->pci_bus)) { |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 530 | status = acpi_remove_notify_handler(handle, |
| 531 | ACPI_SYSTEM_NOTIFY, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 532 | handle_hotplug_event_bridge); |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 533 | if (ACPI_FAILURE(status)) |
| 534 | err("failed to remove notify handler\n"); |
| 535 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 536 | |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 537 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 538 | status = acpi_install_notify_handler(bridge->func->handle, |
| 539 | ACPI_SYSTEM_NOTIFY, |
| 540 | handle_hotplug_event_func, |
| 541 | bridge->func); |
| 542 | if (ACPI_FAILURE(status)) |
| 543 | err("failed to install interrupt notify handler\n"); |
| 544 | } |
| 545 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 546 | slot = bridge->slots; |
| 547 | while (slot) { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 548 | next = slot->next; |
| 549 | list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) { |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 550 | if (is_dock_device(func->handle)) { |
| 551 | unregister_hotplug_dock_device(func->handle); |
| 552 | unregister_dock_notifier(&func->nb); |
| 553 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 554 | if (!(func->flags & FUNC_HAS_DCK)) { |
| 555 | status = acpi_remove_notify_handler(func->handle, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 556 | ACPI_SYSTEM_NOTIFY, |
| 557 | handle_hotplug_event_func); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 558 | if (ACPI_FAILURE(status)) |
| 559 | err("failed to remove notify handler\n"); |
| 560 | } |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 561 | list_del(&func->sibling); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 562 | kfree(func); |
| 563 | } |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 564 | acpiphp_unregister_hotplug_slot(slot); |
| 565 | list_del(&slot->funcs); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 566 | kfree(slot); |
| 567 | slot = next; |
| 568 | } |
| 569 | |
Alex Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 570 | /* |
| 571 | * Only P2P bridges have a pci_dev |
| 572 | */ |
| 573 | if (bridge->pci_dev) |
| 574 | put_device(&bridge->pci_bus->dev); |
| 575 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 576 | pci_dev_put(bridge->pci_dev); |
| 577 | list_del(&bridge->list); |
| 578 | kfree(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 581 | static acpi_status |
| 582 | cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 583 | { |
| 584 | struct acpiphp_bridge *bridge; |
| 585 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 586 | /* cleanup p2p bridges under this P2P bridge |
| 587 | in a depth-first manner */ |
| 588 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 589 | cleanup_p2p_bridge, NULL, NULL, NULL); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 590 | |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 591 | bridge = acpiphp_handle_to_bridge(handle); |
| 592 | if (bridge) |
| 593 | cleanup_bridge(bridge); |
| 594 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 595 | return AE_OK; |
| 596 | } |
| 597 | |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 598 | static void remove_bridge(struct acpi_pci_root *root) |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 599 | { |
| 600 | struct acpiphp_bridge *bridge; |
Taku Izumi | 55bfe3c | 2012-09-18 15:22:35 +0900 | [diff] [blame] | 601 | acpi_handle handle = root->device->handle; |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 602 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 603 | /* cleanup p2p bridges under this host bridge |
| 604 | in a depth-first manner */ |
| 605 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 606 | (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 607 | |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 608 | /* |
| 609 | * On root bridges with hotplug slots directly underneath (ie, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 610 | * no p2p bridge between), we call cleanup_bridge(). |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 611 | * |
| 612 | * The else clause cleans up root bridges that either had no |
| 613 | * hotplug slots at all, or had a p2p bridge underneath. |
| 614 | */ |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 615 | bridge = acpiphp_handle_to_bridge(handle); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 616 | if (bridge) |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 617 | cleanup_bridge(bridge); |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | static int power_on_slot(struct acpiphp_slot *slot) |
| 621 | { |
| 622 | acpi_status status; |
| 623 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | int retval = 0; |
| 625 | |
| 626 | /* if already enabled, just skip */ |
| 627 | if (slot->flags & SLOT_POWEREDON) |
| 628 | goto err_exit; |
| 629 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 630 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (func->flags & FUNC_HAS_PS0) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 632 | dbg("%s: executing _PS0\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); |
| 634 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 635 | warn("%s: _PS0 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | retval = -1; |
| 637 | goto err_exit; |
| 638 | } else |
| 639 | break; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /* TBD: evaluate _STA to check if the slot is enabled */ |
| 644 | |
| 645 | slot->flags |= SLOT_POWEREDON; |
| 646 | |
| 647 | err_exit: |
| 648 | return retval; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | static int power_off_slot(struct acpiphp_slot *slot) |
| 653 | { |
| 654 | acpi_status status; |
| 655 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | int retval = 0; |
| 658 | |
| 659 | /* if already disabled, just skip */ |
| 660 | if ((slot->flags & SLOT_POWEREDON) == 0) |
| 661 | goto err_exit; |
| 662 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 663 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 2f523b1 | 2005-04-28 00:25:55 -0700 | [diff] [blame] | 664 | if (func->flags & FUNC_HAS_PS3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); |
| 666 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 667 | warn("%s: _PS3 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | retval = -1; |
| 669 | goto err_exit; |
| 670 | } else |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | /* TBD: evaluate _STA to check if the slot is disabled */ |
| 676 | |
| 677 | slot->flags &= (~SLOT_POWEREDON); |
| 678 | |
| 679 | err_exit: |
| 680 | return retval; |
| 681 | } |
| 682 | |
| 683 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 684 | |
| 685 | /** |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 686 | * acpiphp_max_busnr - return the highest reserved bus number under the given bus. |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 687 | * @bus: bus to start search with |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 688 | */ |
| 689 | static unsigned char acpiphp_max_busnr(struct pci_bus *bus) |
| 690 | { |
| 691 | struct list_head *tmp; |
| 692 | unsigned char max, n; |
| 693 | |
| 694 | /* |
| 695 | * pci_bus_max_busnr will return the highest |
| 696 | * reserved busnr for all these children. |
| 697 | * that is equivalent to the bus->subordinate |
| 698 | * value. We don't want to use the parent's |
| 699 | * bus->subordinate value because it could have |
| 700 | * padding in it. |
| 701 | */ |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 702 | max = bus->busn_res.start; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 703 | |
| 704 | list_for_each(tmp, &bus->children) { |
| 705 | n = pci_bus_max_busnr(pci_bus_b(tmp)); |
| 706 | if (n > max) |
| 707 | max = n; |
| 708 | } |
| 709 | return max; |
| 710 | } |
| 711 | |
| 712 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 713 | /** |
| 714 | * acpiphp_bus_add - add a new bus to acpi subsystem |
| 715 | * @func: acpiphp_func of the bridge |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 716 | */ |
| 717 | static int acpiphp_bus_add(struct acpiphp_func *func) |
| 718 | { |
Rafael J. Wysocki | 636458d | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 719 | struct acpi_device *device; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 720 | int ret_val; |
| 721 | |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 722 | if (!acpi_bus_get_device(func->handle, &device)) { |
| 723 | dbg("bus exists... trim\n"); |
| 724 | /* this shouldn't be in here, so remove |
| 725 | * the bus then re-add it... |
| 726 | */ |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 727 | acpi_bus_trim(device); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 730 | ret_val = acpi_bus_scan(func->handle); |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 731 | if (!ret_val) |
| 732 | ret_val = acpi_bus_get_device(func->handle, &device); |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 733 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 734 | if (ret_val) |
| 735 | dbg("error adding bus, %x\n", -ret_val); |
| 736 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 737 | return ret_val; |
| 738 | } |
| 739 | |
| 740 | |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 741 | /** |
| 742 | * acpiphp_bus_trim - trim a bus from acpi subsystem |
| 743 | * @handle: handle to acpi namespace |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 744 | */ |
Adrian Bunk | 6d47a5e | 2006-08-14 23:07:38 -0700 | [diff] [blame] | 745 | static int acpiphp_bus_trim(acpi_handle handle) |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 746 | { |
| 747 | struct acpi_device *device; |
| 748 | int retval; |
| 749 | |
| 750 | retval = acpi_bus_get_device(handle, &device); |
| 751 | if (retval) { |
| 752 | dbg("acpi_device not found\n"); |
| 753 | return retval; |
| 754 | } |
| 755 | |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 756 | acpi_bus_trim(device); |
| 757 | return 0; |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 758 | } |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 759 | |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 760 | static void acpiphp_set_acpi_region(struct acpiphp_slot *slot) |
| 761 | { |
| 762 | struct acpiphp_func *func; |
| 763 | union acpi_object params[2]; |
| 764 | struct acpi_object_list arg_list; |
| 765 | |
| 766 | list_for_each_entry(func, &slot->funcs, sibling) { |
| 767 | arg_list.count = 2; |
| 768 | arg_list.pointer = params; |
| 769 | params[0].type = ACPI_TYPE_INTEGER; |
| 770 | params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG; |
| 771 | params[1].type = ACPI_TYPE_INTEGER; |
| 772 | params[1].integer.value = 1; |
| 773 | /* _REG is optional, we don't care about if there is failure */ |
| 774 | acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL); |
| 775 | } |
| 776 | } |
| 777 | |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 778 | static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev) |
| 779 | { |
| 780 | struct acpiphp_func *func; |
| 781 | |
| 782 | if (!dev->subordinate) |
| 783 | return; |
| 784 | |
| 785 | /* quirk, or pcie could set it already */ |
| 786 | if (dev->is_hotplug_bridge) |
| 787 | return; |
| 788 | |
| 789 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 790 | return; |
| 791 | |
| 792 | list_for_each_entry(func, &slot->funcs, sibling) { |
| 793 | if (PCI_FUNC(dev->devfn) == func->function) { |
| 794 | /* check if this bridge has ejectable slots */ |
| 795 | if ((detect_ejectable_slots(func->handle) > 0)) |
| 796 | dev->is_hotplug_bridge = 1; |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | /** |
| 802 | * enable_device - enable, configure a slot |
| 803 | * @slot: slot to be enabled |
| 804 | * |
| 805 | * This function should be called per *physical slot*, |
| 806 | * not per each slot object in ACPI namespace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | */ |
Sam Ravnborg | 0ab2b57 | 2008-02-17 10:45:28 +0100 | [diff] [blame] | 808 | static int __ref enable_device(struct acpiphp_slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | struct pci_dev *dev; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 811 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | struct acpiphp_func *func; |
| 813 | int retval = 0; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 814 | int num, max, pass; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 815 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | |
| 817 | if (slot->flags & SLOT_ENABLED) |
| 818 | goto err_exit; |
| 819 | |
Jiang Liu | 2ca344e | 2013-01-31 00:10:09 +0800 | [diff] [blame] | 820 | list_for_each_entry(func, &slot->funcs, sibling) |
| 821 | acpiphp_bus_add(func); |
| 822 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 823 | num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 824 | if (num == 0) { |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 825 | /* Maybe only part of funcs are added. */ |
| 826 | dbg("No new device found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | goto err_exit; |
| 828 | } |
| 829 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 830 | max = acpiphp_max_busnr(bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 831 | for (pass = 0; pass < 2; pass++) { |
| 832 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 833 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 834 | continue; |
| 835 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 836 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 837 | max = pci_scan_bridge(bus, dev, max, pass); |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 838 | if (pass && dev->subordinate) { |
| 839 | check_hotplug_bridge(slot, dev); |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 840 | pci_bus_size_bridges(dev->subordinate); |
Yinghai Lu | 1f96a96 | 2013-01-21 13:20:42 -0800 | [diff] [blame] | 841 | } |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 842 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 846 | pci_bus_assign_resources(bus); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 847 | acpiphp_sanitize_bus(bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 848 | acpiphp_set_hpp_values(bus); |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 849 | acpiphp_set_acpi_region(slot); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 850 | pci_enable_bridges(bus); |
Ian Campbell | 69643e4 | 2011-05-11 17:00:32 +0100 | [diff] [blame] | 851 | |
| 852 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 853 | /* Assume that newly added devices are powered on already. */ |
| 854 | if (!dev->is_added) |
| 855 | dev->current_state = PCI_D0; |
| 856 | } |
| 857 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 858 | pci_bus_add_devices(bus); |
| 859 | |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 860 | slot->flags |= SLOT_ENABLED; |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 861 | list_for_each_entry(func, &slot->funcs, sibling) { |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 862 | dev = pci_get_slot(bus, PCI_DEVFN(slot->device, |
| 863 | func->function)); |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 864 | if (!dev) { |
| 865 | /* Do not set SLOT_ENABLED flag if some funcs |
| 866 | are not added. */ |
| 867 | slot->flags &= (~SLOT_ENABLED); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 868 | continue; |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 869 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 870 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 871 | if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE && |
| 872 | dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) { |
| 873 | pci_dev_put(dev); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 874 | continue; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 875 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 876 | |
| 877 | status = find_p2p_bridge(func->handle, (u32)1, bus, NULL); |
| 878 | if (ACPI_FAILURE(status)) |
| 879 | warn("find_p2p_bridge failed (error code = 0x%x)\n", |
| 880 | status); |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 881 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | err_exit: |
| 886 | return retval; |
| 887 | } |
| 888 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 889 | /* return first device in slot, acquiring a reference on it */ |
| 890 | static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot) |
| 891 | { |
| 892 | struct pci_bus *bus = slot->bridge->pci_bus; |
| 893 | struct pci_dev *dev; |
| 894 | struct pci_dev *ret = NULL; |
| 895 | |
| 896 | down_read(&pci_bus_sem); |
| 897 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 898 | if (PCI_SLOT(dev->devfn) == slot->device) { |
| 899 | ret = pci_dev_get(dev); |
| 900 | break; |
| 901 | } |
| 902 | up_read(&pci_bus_sem); |
| 903 | |
| 904 | return ret; |
| 905 | } |
| 906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | /** |
| 908 | * disable_device - disable a slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 909 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | */ |
| 911 | static int disable_device(struct acpiphp_slot *slot) |
| 912 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | struct acpiphp_func *func; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 914 | struct pci_dev *pdev; |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 915 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 917 | /* The slot will be enabled when func 0 is added, so check |
| 918 | func 0 before disable the slot. */ |
| 919 | pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 920 | if (!pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | goto err_exit; |
Amos Kong | 638f293 | 2012-05-22 21:58:40 -0600 | [diff] [blame] | 922 | pci_dev_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 924 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 925 | if (func->bridge) { |
| 926 | /* cleanup p2p bridges under this P2P bridge */ |
| 927 | cleanup_p2p_bridge(func->bridge->handle, |
| 928 | (u32)1, NULL, NULL); |
| 929 | func->bridge = NULL; |
| 930 | } |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 931 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 932 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 933 | /* |
| 934 | * enable_device() enumerates all functions in this device via |
| 935 | * pci_scan_slot(), whether they have associated ACPI hotplug |
| 936 | * methods (_EJ0, etc.) or not. Therefore, we remove all functions |
| 937 | * here. |
| 938 | */ |
| 939 | while ((pdev = dev_in_slot(slot))) { |
Bjorn Helgaas | 34e5484 | 2012-08-17 10:03:47 -0600 | [diff] [blame] | 940 | pci_stop_and_remove_bus_device(pdev); |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 941 | pci_dev_put(pdev); |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 942 | } |
Satoru Takeuchi | 0dad351 | 2006-09-12 10:17:46 -0700 | [diff] [blame] | 943 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 944 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 945 | acpiphp_bus_trim(func->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | slot->flags &= (~SLOT_ENABLED); |
| 949 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 950 | err_exit: |
| 951 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | |
| 955 | /** |
| 956 | * get_slot_status - get ACPI slot status |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 957 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 959 | * If a slot has _STA for each function and if any one of them |
| 960 | * returned non-zero status, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 962 | * If a slot doesn't have _STA and if any one of its functions' |
| 963 | * configuration space is configured, return 0x0f as a _STA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 965 | * Otherwise return 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | */ |
| 967 | static unsigned int get_slot_status(struct acpiphp_slot *slot) |
| 968 | { |
| 969 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 970 | unsigned long long sta = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | u32 dvid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | struct acpiphp_func *func; |
| 973 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 974 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | if (func->flags & FUNC_HAS_STA) { |
| 976 | status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); |
| 977 | if (ACPI_SUCCESS(status) && sta) |
| 978 | break; |
| 979 | } else { |
| 980 | pci_bus_read_config_dword(slot->bridge->pci_bus, |
| 981 | PCI_DEVFN(slot->device, |
| 982 | func->function), |
| 983 | PCI_VENDOR_ID, &dvid); |
| 984 | if (dvid != 0xffffffff) { |
| 985 | sta = ACPI_STA_ALL; |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | return (unsigned int)sta; |
| 992 | } |
| 993 | |
| 994 | /** |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 995 | * acpiphp_eject_slot - physically eject the slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 996 | * @slot: ACPI PHP slot |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 997 | */ |
Gary Hade | bfceafc | 2007-07-05 11:10:46 -0700 | [diff] [blame] | 998 | int acpiphp_eject_slot(struct acpiphp_slot *slot) |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 999 | { |
| 1000 | acpi_status status; |
| 1001 | struct acpiphp_func *func; |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1002 | struct acpi_object_list arg_list; |
| 1003 | union acpi_object arg; |
| 1004 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 1005 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1006 | /* We don't want to call _EJ0 on non-existing functions. */ |
| 1007 | if ((func->flags & FUNC_HAS_EJ0)) { |
| 1008 | /* _EJ0 method take one argument */ |
| 1009 | arg_list.count = 1; |
| 1010 | arg_list.pointer = &arg; |
| 1011 | arg.type = ACPI_TYPE_INTEGER; |
| 1012 | arg.integer.value = 1; |
| 1013 | |
| 1014 | status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL); |
| 1015 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1016 | warn("%s: _EJ0 failed\n", __func__); |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1017 | return -1; |
| 1018 | } else |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | * acpiphp_check_bridge - re-enumerate devices |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1027 | * @bridge: where to begin re-enumeration |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * |
| 1029 | * Iterate over all slots under this bridge and make sure that if a |
| 1030 | * card is present they are enabled, and if not they are disabled. |
| 1031 | */ |
| 1032 | static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) |
| 1033 | { |
| 1034 | struct acpiphp_slot *slot; |
| 1035 | int retval = 0; |
| 1036 | int enabled, disabled; |
| 1037 | |
| 1038 | enabled = disabled = 0; |
| 1039 | |
| 1040 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 1041 | unsigned int status = get_slot_status(slot); |
| 1042 | if (slot->flags & SLOT_ENABLED) { |
| 1043 | if (status == ACPI_STA_ALL) |
| 1044 | continue; |
| 1045 | retval = acpiphp_disable_slot(slot); |
| 1046 | if (retval) { |
| 1047 | err("Error occurred in disabling\n"); |
| 1048 | goto err_exit; |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1049 | } else { |
| 1050 | acpiphp_eject_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | disabled++; |
| 1053 | } else { |
| 1054 | if (status != ACPI_STA_ALL) |
| 1055 | continue; |
| 1056 | retval = acpiphp_enable_slot(slot); |
| 1057 | if (retval) { |
| 1058 | err("Error occurred in enabling\n"); |
| 1059 | goto err_exit; |
| 1060 | } |
| 1061 | enabled++; |
| 1062 | } |
| 1063 | } |
| 1064 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1065 | dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
| 1067 | err_exit: |
| 1068 | return retval; |
| 1069 | } |
| 1070 | |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 1071 | static void acpiphp_set_hpp_values(struct pci_bus *bus) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1072 | { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1073 | struct pci_dev *dev; |
| 1074 | |
Bjorn Helgaas | e81995b | 2009-09-14 16:35:35 -0600 | [diff] [blame] | 1075 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 1076 | pci_configure_slot(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Remove devices for which we could not assign resources, call |
| 1081 | * arch specific code to fix-up the bus |
| 1082 | */ |
| 1083 | static void acpiphp_sanitize_bus(struct pci_bus *bus) |
| 1084 | { |
Yijing Wang | d65eba6 | 2013-04-12 05:44:17 +0000 | [diff] [blame^] | 1085 | struct pci_dev *dev, *tmp; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1086 | int i; |
| 1087 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; |
| 1088 | |
Yijing Wang | d65eba6 | 2013-04-12 05:44:17 +0000 | [diff] [blame^] | 1089 | list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1090 | for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { |
| 1091 | struct resource *res = &dev->resource[i]; |
| 1092 | if ((res->flags & type_mask) && !res->start && |
| 1093 | res->end) { |
| 1094 | /* Could not assign a required resources |
| 1095 | * for this device, remove it */ |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 1096 | pci_stop_and_remove_bus_device(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * ACPI event handlers |
| 1105 | */ |
| 1106 | |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1107 | static acpi_status |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1108 | check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 1109 | { |
| 1110 | struct acpiphp_bridge *bridge; |
| 1111 | char objname[64]; |
| 1112 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1113 | .pointer = objname }; |
| 1114 | |
| 1115 | bridge = acpiphp_handle_to_bridge(handle); |
| 1116 | if (bridge) { |
| 1117 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1118 | dbg("%s: re-enumerating slots under %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1119 | __func__, objname); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1120 | acpiphp_check_bridge(bridge); |
| 1121 | } |
| 1122 | return AE_OK ; |
| 1123 | } |
| 1124 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1125 | static void _handle_hotplug_event_bridge(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
| 1127 | struct acpiphp_bridge *bridge; |
| 1128 | char objname[64]; |
| 1129 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1130 | .pointer = objname }; |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1131 | struct acpi_hp_work *hp_work; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1132 | acpi_handle handle; |
| 1133 | u32 type; |
| 1134 | |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1135 | hp_work = container_of(work, struct acpi_hp_work, work); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1136 | handle = hp_work->handle; |
| 1137 | type = hp_work->type; |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 1138 | bridge = (struct acpiphp_bridge *)hp_work->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1140 | acpi_scan_lock_acquire(); |
| 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1143 | |
| 1144 | switch (type) { |
| 1145 | case ACPI_NOTIFY_BUS_CHECK: |
| 1146 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1147 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 1148 | dbg("%s: re-enumerating slots under %s\n", __func__, objname); |
| 1149 | acpiphp_check_bridge(bridge); |
| 1150 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
| 1151 | ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | break; |
| 1153 | |
| 1154 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1155 | /* device check */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1156 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | acpiphp_check_bridge(bridge); |
| 1158 | break; |
| 1159 | |
| 1160 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1161 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1162 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | break; |
| 1164 | |
| 1165 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1166 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1167 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 1168 | if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1169 | struct acpiphp_slot *slot; |
| 1170 | slot = bridge->func->slot; |
| 1171 | if (!acpiphp_disable_slot(slot)) |
| 1172 | acpiphp_eject_slot(slot); |
| 1173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | |
| 1176 | case ACPI_NOTIFY_FREQUENCY_MISMATCH: |
| 1177 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1178 | " to a frequency mismatch\n", objname); |
| 1179 | break; |
| 1180 | |
| 1181 | case ACPI_NOTIFY_BUS_MODE_MISMATCH: |
| 1182 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1183 | " to a bus mode mismatch\n", objname); |
| 1184 | break; |
| 1185 | |
| 1186 | case ACPI_NOTIFY_POWER_FAULT: |
| 1187 | printk(KERN_ERR "Device %s has suffered a power fault\n", |
| 1188 | objname); |
| 1189 | break; |
| 1190 | |
| 1191 | default: |
| 1192 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1193 | break; |
| 1194 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1195 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1196 | acpi_scan_lock_release(); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1197 | kfree(hp_work); /* allocated in handle_hotplug_event_bridge */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | /** |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1201 | * handle_hotplug_event_bridge - handle ACPI event on bridges |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | * @handle: Notify()'ed acpi_handle |
| 1203 | * @type: Notify code |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1204 | * @context: pointer to acpiphp_bridge structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | * |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1206 | * Handles ACPI event notification on {host,p2p} bridges. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | */ |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1208 | static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, |
| 1209 | void *context) |
| 1210 | { |
| 1211 | /* |
| 1212 | * Currently the code adds all hotplug events to the kacpid_wq |
| 1213 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 1214 | * The proper way to fix this is to reorganize the code so that |
| 1215 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 1216 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 1217 | * don't deadlock on hotplug actions. |
| 1218 | */ |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1219 | alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static void _handle_hotplug_event_func(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | { |
| 1224 | struct acpiphp_func *func; |
| 1225 | char objname[64]; |
| 1226 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1227 | .pointer = objname }; |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1228 | struct acpi_hp_work *hp_work; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1229 | acpi_handle handle; |
| 1230 | u32 type; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1231 | |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1232 | hp_work = container_of(work, struct acpi_hp_work, work); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1233 | handle = hp_work->handle; |
| 1234 | type = hp_work->type; |
Jiang Liu | be6d286 | 2013-01-31 00:10:10 +0800 | [diff] [blame] | 1235 | func = (struct acpiphp_func *)hp_work->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1237 | acpi_scan_lock_acquire(); |
| 1238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | switch (type) { |
| 1242 | case ACPI_NOTIFY_BUS_CHECK: |
| 1243 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1244 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | acpiphp_enable_slot(func->slot); |
| 1246 | break; |
| 1247 | |
| 1248 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1249 | /* device check : re-enumerate from parent bus */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1250 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | acpiphp_check_bridge(func->slot->bridge); |
| 1252 | break; |
| 1253 | |
| 1254 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1255 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1256 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | |
| 1259 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1260 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1261 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1262 | if (!(acpiphp_disable_slot(func->slot))) |
| 1263 | acpiphp_eject_slot(func->slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | break; |
| 1265 | |
| 1266 | default: |
| 1267 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1268 | break; |
| 1269 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1270 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1271 | acpi_scan_lock_release(); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1272 | kfree(hp_work); /* allocated in handle_hotplug_event_func */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1275 | /** |
| 1276 | * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots) |
| 1277 | * @handle: Notify()'ed acpi_handle |
| 1278 | * @type: Notify code |
| 1279 | * @context: pointer to acpiphp_func structure |
| 1280 | * |
| 1281 | * Handles ACPI event notification on slots. |
| 1282 | */ |
| 1283 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, |
| 1284 | void *context) |
| 1285 | { |
| 1286 | /* |
| 1287 | * Currently the code adds all hotplug events to the kacpid_wq |
| 1288 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 1289 | * The proper way to fix this is to reorganize the code so that |
| 1290 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 1291 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 1292 | * don't deadlock on hotplug actions. |
| 1293 | */ |
Yinghai Lu | 92d8aff | 2013-01-21 13:20:47 -0800 | [diff] [blame] | 1294 | alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | static struct acpi_pci_driver acpi_pci_hp_driver = { |
| 1298 | .add = add_bridge, |
| 1299 | .remove = remove_bridge, |
| 1300 | }; |
| 1301 | |
| 1302 | /** |
| 1303 | * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | */ |
| 1305 | int __init acpiphp_glue_init(void) |
| 1306 | { |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 1307 | acpi_pci_register_driver(&acpi_pci_hp_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /** |
| 1314 | * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures |
| 1315 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1316 | * This function frees all data allocated in acpiphp_glue_init(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | */ |
Kristen Carlson Accardi | 031f30d | 2006-12-16 15:26:04 -0800 | [diff] [blame] | 1318 | void acpiphp_glue_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | acpi_pci_unregister_driver(&acpi_pci_hp_driver); |
| 1321 | } |
| 1322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | /** |
| 1324 | * acpiphp_enable_slot - power on slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1325 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | */ |
| 1327 | int acpiphp_enable_slot(struct acpiphp_slot *slot) |
| 1328 | { |
| 1329 | int retval; |
| 1330 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1331 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | /* wake up all functions */ |
| 1334 | retval = power_on_slot(slot); |
| 1335 | if (retval) |
| 1336 | goto err_exit; |
| 1337 | |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1338 | if (get_slot_status(slot) == ACPI_STA_ALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | /* configure all functions */ |
| 1340 | retval = enable_device(slot); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1341 | if (retval) |
| 1342 | power_off_slot(slot); |
| 1343 | } else { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1344 | dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1345 | power_off_slot(slot); |
| 1346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1349 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | return retval; |
| 1351 | } |
| 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | /** |
| 1354 | * acpiphp_disable_slot - power off slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1355 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | */ |
| 1357 | int acpiphp_disable_slot(struct acpiphp_slot *slot) |
| 1358 | { |
| 1359 | int retval = 0; |
| 1360 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1361 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | /* unconfigure all functions */ |
| 1364 | retval = disable_device(slot); |
| 1365 | if (retval) |
| 1366 | goto err_exit; |
| 1367 | |
| 1368 | /* power off all functions */ |
| 1369 | retval = power_off_slot(slot); |
| 1370 | if (retval) |
| 1371 | goto err_exit; |
| 1372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1374 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | return retval; |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | /* |
| 1380 | * slot enabled: 1 |
| 1381 | * slot disabled: 0 |
| 1382 | */ |
| 1383 | u8 acpiphp_get_power_status(struct acpiphp_slot *slot) |
| 1384 | { |
Rajesh Shah | 8d50e332 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1385 | return (slot->flags & SLOT_POWEREDON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | |
| 1389 | /* |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1390 | * latch open: 1 |
| 1391 | * latch closed: 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | */ |
| 1393 | u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) |
| 1394 | { |
| 1395 | unsigned int sta; |
| 1396 | |
| 1397 | sta = get_slot_status(slot); |
| 1398 | |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1399 | return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | |
| 1403 | /* |
| 1404 | * adapter presence : 1 |
| 1405 | * absence : 0 |
| 1406 | */ |
| 1407 | u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) |
| 1408 | { |
| 1409 | unsigned int sta; |
| 1410 | |
| 1411 | sta = get_slot_status(slot); |
| 1412 | |
| 1413 | return (sta == 0) ? 0 : 1; |
| 1414 | } |