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 | |
| 328 | /* install notify handler */ |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 329 | if (bridge->type != BRIDGE_TYPE_HOST) { |
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; |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* allocate and initialize host bridge data structure */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 406 | static void add_host_bridge(acpi_handle *handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | struct acpiphp_bridge *bridge; |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 409 | struct acpi_pci_root *root = acpi_pci_find_root(handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 411 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | if (bridge == NULL) |
| 413 | return; |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | bridge->type = BRIDGE_TYPE_HOST; |
| 416 | bridge->handle = handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 418 | bridge->pci_bus = root->bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | init_bridge_misc(bridge); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /* allocate and initialize PCI-to-PCI bridge data structure */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 425 | static void add_p2p_bridge(acpi_handle *handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct acpiphp_bridge *bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Eric Sesterhenn | f5afe80 | 2006-02-28 15:34:49 +0100 | [diff] [blame] | 429 | bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (bridge == NULL) { |
| 431 | err("out of memory\n"); |
| 432 | return; |
| 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | bridge->type = BRIDGE_TYPE_P2P; |
| 436 | bridge->handle = handle; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 437 | config_p2p_bridge_flags(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 439 | bridge->pci_dev = acpi_get_pci_dev(handle); |
| 440 | bridge->pci_bus = bridge->pci_dev->subordinate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (!bridge->pci_bus) { |
| 442 | err("This is not a PCI-to-PCI bridge!\n"); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 443 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Alex Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 446 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | init_bridge_misc(bridge); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 454 | return; |
| 455 | err: |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 456 | pci_dev_put(bridge->pci_dev); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 457 | kfree(bridge); |
| 458 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | |
| 462 | /* callback routine to find P2P bridges */ |
| 463 | static acpi_status |
| 464 | find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 465 | { |
| 466 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct pci_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 469 | dev = acpi_get_pci_dev(handle); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 470 | if (!dev || !dev->subordinate) |
| 471 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
| 473 | /* check if this bridge has ejectable slots */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 474 | if ((detect_ejectable_slots(handle) > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | 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] | 476 | add_p2p_bridge(handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 479 | /* search P2P bridges under this p2p bridge */ |
| 480 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 481 | find_p2p_bridge, NULL, NULL, NULL); |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 482 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 483 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Kenji Kaneshige | 7c8f25d | 2006-02-27 22:15:49 +0900 | [diff] [blame] | 484 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 485 | out: |
| 486 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | return AE_OK; |
| 488 | } |
| 489 | |
| 490 | |
| 491 | /* find hot-pluggable slots, and then find P2P bridge */ |
| 492 | static int add_bridge(acpi_handle handle) |
| 493 | { |
| 494 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 495 | unsigned long long tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | acpi_handle dummy_handle; |
| 497 | |
| 498 | /* if the bridge doesn't have _STA, we assume it is always there */ |
| 499 | status = acpi_get_handle(handle, "_STA", &dummy_handle); |
| 500 | if (ACPI_SUCCESS(status)) { |
| 501 | status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp); |
| 502 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 503 | dbg("%s: _STA evaluation failure\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | if ((tmp & ACPI_STA_FUNCTIONING) == 0) |
| 507 | /* don't register this object */ |
| 508 | return 0; |
| 509 | } |
| 510 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 511 | /* check if this bridge has ejectable slots */ |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 512 | if (detect_ejectable_slots(handle) > 0) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 513 | dbg("found PCI host-bus bridge with hot-pluggable slots\n"); |
Alex Chiang | 6edd767 | 2009-09-10 12:34:04 -0600 | [diff] [blame] | 514 | add_host_bridge(handle); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | /* search P2P bridges under this host bridge */ |
| 518 | status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 519 | find_p2p_bridge, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | if (ACPI_FAILURE(status)) |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 522 | warn("find_p2p_bridge failed (error code = 0x%x)\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 527 | static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle) |
| 528 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 529 | struct acpiphp_bridge *bridge; |
| 530 | |
| 531 | list_for_each_entry(bridge, &bridge_list, list) |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 532 | if (bridge->handle == handle) |
| 533 | return bridge; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 534 | |
| 535 | return NULL; |
| 536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 538 | static void cleanup_bridge(struct acpiphp_bridge *bridge) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 540 | struct acpiphp_slot *slot, *next; |
| 541 | struct acpiphp_func *func, *tmp; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 542 | acpi_status status; |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 543 | acpi_handle handle = bridge->handle; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 544 | |
| 545 | status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 546 | handle_hotplug_event_bridge); |
| 547 | if (ACPI_FAILURE(status)) |
| 548 | err("failed to remove notify handler\n"); |
| 549 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 550 | if ((bridge->type != BRIDGE_TYPE_HOST) && |
| 551 | ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) { |
| 552 | status = acpi_install_notify_handler(bridge->func->handle, |
| 553 | ACPI_SYSTEM_NOTIFY, |
| 554 | handle_hotplug_event_func, |
| 555 | bridge->func); |
| 556 | if (ACPI_FAILURE(status)) |
| 557 | err("failed to install interrupt notify handler\n"); |
| 558 | } |
| 559 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 560 | slot = bridge->slots; |
| 561 | while (slot) { |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 562 | next = slot->next; |
| 563 | list_for_each_entry_safe(func, tmp, &slot->funcs, sibling) { |
Kristen Accardi | 4e8662b | 2006-06-28 03:08:06 -0400 | [diff] [blame] | 564 | if (is_dock_device(func->handle)) { |
| 565 | unregister_hotplug_dock_device(func->handle); |
| 566 | unregister_dock_notifier(&func->nb); |
| 567 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 568 | if (!(func->flags & FUNC_HAS_DCK)) { |
| 569 | status = acpi_remove_notify_handler(func->handle, |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 570 | ACPI_SYSTEM_NOTIFY, |
| 571 | handle_hotplug_event_func); |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 572 | if (ACPI_FAILURE(status)) |
| 573 | err("failed to remove notify handler\n"); |
| 574 | } |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 575 | list_del(&func->sibling); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 576 | kfree(func); |
| 577 | } |
MUNEDA Takahiro | e27da38 | 2006-02-23 17:56:08 -0800 | [diff] [blame] | 578 | acpiphp_unregister_hotplug_slot(slot); |
| 579 | list_del(&slot->funcs); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 580 | kfree(slot); |
| 581 | slot = next; |
| 582 | } |
| 583 | |
Alex Chiang | 5d4a4b2 | 2009-03-30 10:50:14 -0600 | [diff] [blame] | 584 | /* |
| 585 | * Only P2P bridges have a pci_dev |
| 586 | */ |
| 587 | if (bridge->pci_dev) |
| 588 | put_device(&bridge->pci_bus->dev); |
| 589 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 590 | pci_dev_put(bridge->pci_dev); |
| 591 | list_del(&bridge->list); |
| 592 | kfree(bridge); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 595 | static acpi_status |
| 596 | cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 597 | { |
| 598 | struct acpiphp_bridge *bridge; |
| 599 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 600 | /* cleanup p2p bridges under this P2P bridge |
| 601 | in a depth-first manner */ |
| 602 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 603 | cleanup_p2p_bridge, NULL, NULL, NULL); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 604 | |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 605 | bridge = acpiphp_handle_to_bridge(handle); |
| 606 | if (bridge) |
| 607 | cleanup_bridge(bridge); |
| 608 | |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 609 | return AE_OK; |
| 610 | } |
| 611 | |
| 612 | static void remove_bridge(acpi_handle handle) |
| 613 | { |
| 614 | struct acpiphp_bridge *bridge; |
| 615 | |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 616 | /* cleanup p2p bridges under this host bridge |
| 617 | in a depth-first manner */ |
| 618 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 619 | (u32)1, cleanup_p2p_bridge, NULL, NULL, NULL); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 620 | |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 621 | /* |
| 622 | * On root bridges with hotplug slots directly underneath (ie, |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 623 | * no p2p bridge between), we call cleanup_bridge(). |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 624 | * |
| 625 | * The else clause cleans up root bridges that either had no |
| 626 | * hotplug slots at all, or had a p2p bridge underneath. |
| 627 | */ |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 628 | bridge = acpiphp_handle_to_bridge(handle); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 629 | if (bridge) |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 630 | cleanup_bridge(bridge); |
Alex Chiang | a13307c | 2008-07-01 20:02:23 -0600 | [diff] [blame] | 631 | else |
| 632 | acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 633 | handle_hotplug_event_bridge); |
Rajesh Shah | 364d509 | 2005-04-28 00:25:54 -0700 | [diff] [blame] | 634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | static int power_on_slot(struct acpiphp_slot *slot) |
| 637 | { |
| 638 | acpi_status status; |
| 639 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | int retval = 0; |
| 641 | |
| 642 | /* if already enabled, just skip */ |
| 643 | if (slot->flags & SLOT_POWEREDON) |
| 644 | goto err_exit; |
| 645 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 646 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | if (func->flags & FUNC_HAS_PS0) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 648 | dbg("%s: executing _PS0\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL); |
| 650 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 651 | warn("%s: _PS0 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | retval = -1; |
| 653 | goto err_exit; |
| 654 | } else |
| 655 | break; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /* TBD: evaluate _STA to check if the slot is enabled */ |
| 660 | |
| 661 | slot->flags |= SLOT_POWEREDON; |
| 662 | |
| 663 | err_exit: |
| 664 | return retval; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | static int power_off_slot(struct acpiphp_slot *slot) |
| 669 | { |
| 670 | acpi_status status; |
| 671 | struct acpiphp_func *func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
| 673 | int retval = 0; |
| 674 | |
| 675 | /* if already disabled, just skip */ |
| 676 | if ((slot->flags & SLOT_POWEREDON) == 0) |
| 677 | goto err_exit; |
| 678 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 679 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 2f523b1 | 2005-04-28 00:25:55 -0700 | [diff] [blame] | 680 | if (func->flags & FUNC_HAS_PS3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL); |
| 682 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 683 | warn("%s: _PS3 failed\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | retval = -1; |
| 685 | goto err_exit; |
| 686 | } else |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* TBD: evaluate _STA to check if the slot is disabled */ |
| 692 | |
| 693 | slot->flags &= (~SLOT_POWEREDON); |
| 694 | |
| 695 | err_exit: |
| 696 | return retval; |
| 697 | } |
| 698 | |
| 699 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 700 | |
| 701 | /** |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 702 | * 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] | 703 | * @bus: bus to start search with |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 704 | */ |
| 705 | static unsigned char acpiphp_max_busnr(struct pci_bus *bus) |
| 706 | { |
| 707 | struct list_head *tmp; |
| 708 | unsigned char max, n; |
| 709 | |
| 710 | /* |
| 711 | * pci_bus_max_busnr will return the highest |
| 712 | * reserved busnr for all these children. |
| 713 | * that is equivalent to the bus->subordinate |
| 714 | * value. We don't want to use the parent's |
| 715 | * bus->subordinate value because it could have |
| 716 | * padding in it. |
| 717 | */ |
Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 718 | max = bus->busn_res.start; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 719 | |
| 720 | list_for_each(tmp, &bus->children) { |
| 721 | n = pci_bus_max_busnr(pci_bus_b(tmp)); |
| 722 | if (n > max) |
| 723 | max = n; |
| 724 | } |
| 725 | return max; |
| 726 | } |
| 727 | |
| 728 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 729 | /** |
| 730 | * acpiphp_bus_add - add a new bus to acpi subsystem |
| 731 | * @func: acpiphp_func of the bridge |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 732 | */ |
| 733 | static int acpiphp_bus_add(struct acpiphp_func *func) |
| 734 | { |
| 735 | acpi_handle phandle; |
| 736 | struct acpi_device *device, *pdevice; |
| 737 | int ret_val; |
| 738 | |
| 739 | acpi_get_parent(func->handle, &phandle); |
| 740 | if (acpi_bus_get_device(phandle, &pdevice)) { |
| 741 | dbg("no parent device, assuming NULL\n"); |
| 742 | pdevice = NULL; |
| 743 | } |
Kristen Accardi | 20416ea | 2006-02-23 17:56:03 -0800 | [diff] [blame] | 744 | if (!acpi_bus_get_device(func->handle, &device)) { |
| 745 | dbg("bus exists... trim\n"); |
| 746 | /* this shouldn't be in here, so remove |
| 747 | * the bus then re-add it... |
| 748 | */ |
| 749 | ret_val = acpi_bus_trim(device, 1); |
| 750 | dbg("acpi_bus_trim return %x\n", ret_val); |
| 751 | } |
| 752 | |
| 753 | ret_val = acpi_bus_add(&device, pdevice, func->handle, |
| 754 | ACPI_BUS_TYPE_DEVICE); |
| 755 | if (ret_val) { |
| 756 | dbg("error adding bus, %x\n", |
| 757 | -ret_val); |
| 758 | goto acpiphp_bus_add_out; |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 759 | } |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 760 | ret_val = acpi_bus_start(device); |
| 761 | |
| 762 | acpiphp_bus_add_out: |
| 763 | return ret_val; |
| 764 | } |
| 765 | |
| 766 | |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 767 | /** |
| 768 | * acpiphp_bus_trim - trim a bus from acpi subsystem |
| 769 | * @handle: handle to acpi namespace |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 770 | */ |
Adrian Bunk | 6d47a5e | 2006-08-14 23:07:38 -0700 | [diff] [blame] | 771 | static int acpiphp_bus_trim(acpi_handle handle) |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 772 | { |
| 773 | struct acpi_device *device; |
| 774 | int retval; |
| 775 | |
| 776 | retval = acpi_bus_get_device(handle, &device); |
| 777 | if (retval) { |
| 778 | dbg("acpi_device not found\n"); |
| 779 | return retval; |
| 780 | } |
| 781 | |
| 782 | retval = acpi_bus_trim(device, 1); |
| 783 | if (retval) |
| 784 | err("cannot remove from acpi list\n"); |
| 785 | |
| 786 | return retval; |
| 787 | } |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 788 | |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 789 | static void acpiphp_set_acpi_region(struct acpiphp_slot *slot) |
| 790 | { |
| 791 | struct acpiphp_func *func; |
| 792 | union acpi_object params[2]; |
| 793 | struct acpi_object_list arg_list; |
| 794 | |
| 795 | list_for_each_entry(func, &slot->funcs, sibling) { |
| 796 | arg_list.count = 2; |
| 797 | arg_list.pointer = params; |
| 798 | params[0].type = ACPI_TYPE_INTEGER; |
| 799 | params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG; |
| 800 | params[1].type = ACPI_TYPE_INTEGER; |
| 801 | params[1].integer.value = 1; |
| 802 | /* _REG is optional, we don't care about if there is failure */ |
| 803 | acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL); |
| 804 | } |
| 805 | } |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | /** |
| 808 | * enable_device - enable, configure a slot |
| 809 | * @slot: slot to be enabled |
| 810 | * |
| 811 | * This function should be called per *physical slot*, |
| 812 | * not per each slot object in ACPI namespace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
Sam Ravnborg | 0ab2b57 | 2008-02-17 10:45:28 +0100 | [diff] [blame] | 814 | static int __ref enable_device(struct acpiphp_slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | struct pci_dev *dev; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 817 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | struct acpiphp_func *func; |
| 819 | int retval = 0; |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 820 | int num, max, pass; |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 821 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
| 823 | if (slot->flags & SLOT_ENABLED) |
| 824 | goto err_exit; |
| 825 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 826 | num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 827 | if (num == 0) { |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 828 | /* Maybe only part of funcs are added. */ |
| 829 | dbg("No new device found\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | goto err_exit; |
| 831 | } |
| 832 | |
Kristen Accardi | 15a1ae7 | 2006-02-23 17:55:58 -0800 | [diff] [blame] | 833 | max = acpiphp_max_busnr(bus); |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 834 | for (pass = 0; pass < 2; pass++) { |
| 835 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 836 | if (PCI_SLOT(dev->devfn) != slot->device) |
| 837 | continue; |
| 838 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 839 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 840 | max = pci_scan_bridge(bus, dev, max, pass); |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 841 | if (pass && dev->subordinate) |
Kristen Accardi | c64b5ee | 2005-12-14 09:37:26 -0800 | [diff] [blame] | 842 | pci_bus_size_bridges(dev->subordinate); |
| 843 | } |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 847 | list_for_each_entry(func, &slot->funcs, sibling) |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 848 | acpiphp_bus_add(func); |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 849 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 850 | pci_bus_assign_resources(bus); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 851 | acpiphp_sanitize_bus(bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 852 | acpiphp_set_hpp_values(bus); |
Shaohua Li | d060705 | 2010-02-25 10:59:34 +0800 | [diff] [blame] | 853 | acpiphp_set_acpi_region(slot); |
Kristen Accardi | 8e5dce3 | 2005-10-18 17:21:40 -0700 | [diff] [blame] | 854 | pci_enable_bridges(bus); |
Ian Campbell | 69643e4 | 2011-05-11 17:00:32 +0100 | [diff] [blame] | 855 | |
| 856 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 857 | /* Assume that newly added devices are powered on already. */ |
| 858 | if (!dev->is_added) |
| 859 | dev->current_state = PCI_D0; |
| 860 | } |
| 861 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 862 | pci_bus_add_devices(bus); |
| 863 | |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 864 | slot->flags |= SLOT_ENABLED; |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 865 | list_for_each_entry(func, &slot->funcs, sibling) { |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 866 | dev = pci_get_slot(bus, PCI_DEVFN(slot->device, |
| 867 | func->function)); |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 868 | if (!dev) { |
| 869 | /* Do not set SLOT_ENABLED flag if some funcs |
| 870 | are not added. */ |
| 871 | slot->flags &= (~SLOT_ENABLED); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 872 | continue; |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 873 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 874 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 875 | if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE && |
| 876 | dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) { |
| 877 | pci_dev_put(dev); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 878 | continue; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 879 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 880 | |
| 881 | status = find_p2p_bridge(func->handle, (u32)1, bus, NULL); |
| 882 | if (ACPI_FAILURE(status)) |
| 883 | warn("find_p2p_bridge failed (error code = 0x%x)\n", |
| 884 | status); |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 885 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | err_exit: |
| 890 | return retval; |
| 891 | } |
| 892 | |
Satoru Takeuchi | d5cdb67 | 2006-09-12 10:19:00 -0700 | [diff] [blame] | 893 | static void disable_bridges(struct pci_bus *bus) |
| 894 | { |
| 895 | struct pci_dev *dev; |
| 896 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 897 | if (dev->subordinate) { |
| 898 | disable_bridges(dev->subordinate); |
| 899 | pci_disable_device(dev); |
| 900 | } |
| 901 | } |
| 902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 904 | /* return first device in slot, acquiring a reference on it */ |
| 905 | static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot) |
| 906 | { |
| 907 | struct pci_bus *bus = slot->bridge->pci_bus; |
| 908 | struct pci_dev *dev; |
| 909 | struct pci_dev *ret = NULL; |
| 910 | |
| 911 | down_read(&pci_bus_sem); |
| 912 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 913 | if (PCI_SLOT(dev->devfn) == slot->device) { |
| 914 | ret = pci_dev_get(dev); |
| 915 | break; |
| 916 | } |
| 917 | up_read(&pci_bus_sem); |
| 918 | |
| 919 | return ret; |
| 920 | } |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | /** |
| 923 | * disable_device - disable a slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 924 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
| 926 | static int disable_device(struct acpiphp_slot *slot) |
| 927 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | struct acpiphp_func *func; |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 929 | struct pci_dev *pdev; |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 930 | struct pci_bus *bus = slot->bridge->pci_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | |
Amos Kong | f382a08 | 2011-11-25 15:03:07 +0800 | [diff] [blame] | 932 | /* The slot will be enabled when func 0 is added, so check |
| 933 | func 0 before disable the slot. */ |
| 934 | pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0)); |
| 935 | if (!pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | goto err_exit; |
Amos Kong | 638f293 | 2012-05-22 21:58:40 -0600 | [diff] [blame] | 937 | pci_dev_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 939 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 940 | if (func->bridge) { |
| 941 | /* cleanup p2p bridges under this P2P bridge */ |
| 942 | cleanup_p2p_bridge(func->bridge->handle, |
| 943 | (u32)1, NULL, NULL); |
| 944 | func->bridge = NULL; |
| 945 | } |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 946 | } |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 947 | |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 948 | /* |
| 949 | * enable_device() enumerates all functions in this device via |
| 950 | * pci_scan_slot(), whether they have associated ACPI hotplug |
| 951 | * methods (_EJ0, etc.) or not. Therefore, we remove all functions |
| 952 | * here. |
| 953 | */ |
| 954 | while ((pdev = dev_in_slot(slot))) { |
| 955 | pci_stop_bus_device(pdev); |
| 956 | if (pdev->subordinate) { |
| 957 | disable_bridges(pdev->subordinate); |
| 958 | pci_disable_device(pdev); |
Satoru Takeuchi | d5cdb67 | 2006-09-12 10:19:00 -0700 | [diff] [blame] | 959 | } |
Amos Kong | ce29ca3 | 2012-05-23 10:20:35 -0600 | [diff] [blame] | 960 | __pci_remove_bus_device(pdev); |
| 961 | pci_dev_put(pdev); |
Satoru Takeuchi | 600812e | 2006-09-12 10:22:53 -0700 | [diff] [blame] | 962 | } |
Satoru Takeuchi | 0dad351 | 2006-09-12 10:17:46 -0700 | [diff] [blame] | 963 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 964 | list_for_each_entry(func, &slot->funcs, sibling) { |
MUNEDA Takahiro | 92c9be9 | 2006-03-22 14:49:09 +0900 | [diff] [blame] | 965 | acpiphp_bus_trim(func->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | slot->flags &= (~SLOT_ENABLED); |
| 969 | |
Alex Chiang | 9d911d7 | 2009-05-21 16:21:15 -0600 | [diff] [blame] | 970 | err_exit: |
| 971 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | |
| 975 | /** |
| 976 | * get_slot_status - get ACPI slot status |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 977 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 979 | * If a slot has _STA for each function and if any one of them |
| 980 | * returned non-zero status, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 982 | * If a slot doesn't have _STA and if any one of its functions' |
| 983 | * configuration space is configured, return 0x0f as a _STA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 985 | * Otherwise return 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | */ |
| 987 | static unsigned int get_slot_status(struct acpiphp_slot *slot) |
| 988 | { |
| 989 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 990 | unsigned long long sta = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | u32 dvid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | struct acpiphp_func *func; |
| 993 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 994 | list_for_each_entry(func, &slot->funcs, sibling) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | if (func->flags & FUNC_HAS_STA) { |
| 996 | status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta); |
| 997 | if (ACPI_SUCCESS(status) && sta) |
| 998 | break; |
| 999 | } else { |
| 1000 | pci_bus_read_config_dword(slot->bridge->pci_bus, |
| 1001 | PCI_DEVFN(slot->device, |
| 1002 | func->function), |
| 1003 | PCI_VENDOR_ID, &dvid); |
| 1004 | if (dvid != 0xffffffff) { |
| 1005 | sta = ACPI_STA_ALL; |
| 1006 | break; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | return (unsigned int)sta; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1015 | * acpiphp_eject_slot - physically eject the slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1016 | * @slot: ACPI PHP slot |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1017 | */ |
Gary Hade | bfceafc | 2007-07-05 11:10:46 -0700 | [diff] [blame] | 1018 | int acpiphp_eject_slot(struct acpiphp_slot *slot) |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1019 | { |
| 1020 | acpi_status status; |
| 1021 | struct acpiphp_func *func; |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1022 | struct acpi_object_list arg_list; |
| 1023 | union acpi_object arg; |
| 1024 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 1025 | list_for_each_entry(func, &slot->funcs, sibling) { |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1026 | /* We don't want to call _EJ0 on non-existing functions. */ |
| 1027 | if ((func->flags & FUNC_HAS_EJ0)) { |
| 1028 | /* _EJ0 method take one argument */ |
| 1029 | arg_list.count = 1; |
| 1030 | arg_list.pointer = &arg; |
| 1031 | arg.type = ACPI_TYPE_INTEGER; |
| 1032 | arg.integer.value = 1; |
| 1033 | |
| 1034 | status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL); |
| 1035 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1036 | warn("%s: _EJ0 failed\n", __func__); |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1037 | return -1; |
| 1038 | } else |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | * acpiphp_check_bridge - re-enumerate devices |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1047 | * @bridge: where to begin re-enumeration |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | * |
| 1049 | * Iterate over all slots under this bridge and make sure that if a |
| 1050 | * card is present they are enabled, and if not they are disabled. |
| 1051 | */ |
| 1052 | static int acpiphp_check_bridge(struct acpiphp_bridge *bridge) |
| 1053 | { |
| 1054 | struct acpiphp_slot *slot; |
| 1055 | int retval = 0; |
| 1056 | int enabled, disabled; |
| 1057 | |
| 1058 | enabled = disabled = 0; |
| 1059 | |
| 1060 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 1061 | unsigned int status = get_slot_status(slot); |
| 1062 | if (slot->flags & SLOT_ENABLED) { |
| 1063 | if (status == ACPI_STA_ALL) |
| 1064 | continue; |
| 1065 | retval = acpiphp_disable_slot(slot); |
| 1066 | if (retval) { |
| 1067 | err("Error occurred in disabling\n"); |
| 1068 | goto err_exit; |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1069 | } else { |
| 1070 | acpiphp_eject_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
| 1072 | disabled++; |
| 1073 | } else { |
| 1074 | if (status != ACPI_STA_ALL) |
| 1075 | continue; |
| 1076 | retval = acpiphp_enable_slot(slot); |
| 1077 | if (retval) { |
| 1078 | err("Error occurred in enabling\n"); |
| 1079 | goto err_exit; |
| 1080 | } |
| 1081 | enabled++; |
| 1082 | } |
| 1083 | } |
| 1084 | |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1085 | dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
| 1087 | err_exit: |
| 1088 | return retval; |
| 1089 | } |
| 1090 | |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 1091 | static void acpiphp_set_hpp_values(struct pci_bus *bus) |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1092 | { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1093 | struct pci_dev *dev; |
| 1094 | |
Bjorn Helgaas | e81995b | 2009-09-14 16:35:35 -0600 | [diff] [blame] | 1095 | list_for_each_entry(dev, &bus->devices, bus_list) |
| 1096 | pci_configure_slot(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * Remove devices for which we could not assign resources, call |
| 1101 | * arch specific code to fix-up the bus |
| 1102 | */ |
| 1103 | static void acpiphp_sanitize_bus(struct pci_bus *bus) |
| 1104 | { |
| 1105 | struct pci_dev *dev; |
| 1106 | int i; |
| 1107 | unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM; |
| 1108 | |
| 1109 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 1110 | for (i=0; i<PCI_BRIDGE_RESOURCES; i++) { |
| 1111 | struct resource *res = &dev->resource[i]; |
| 1112 | if ((res->flags & type_mask) && !res->start && |
| 1113 | res->end) { |
| 1114 | /* Could not assign a required resources |
| 1115 | * for this device, remove it */ |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 1116 | pci_stop_and_remove_bus_device(dev); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* Program resources in newly inserted bridge */ |
| 1124 | static int acpiphp_configure_bridge (acpi_handle handle) |
| 1125 | { |
Alex Chiang | 7f53866 | 2009-09-10 12:34:09 -0600 | [diff] [blame] | 1126 | struct pci_bus *bus; |
| 1127 | |
| 1128 | if (acpi_is_root_bridge(handle)) { |
| 1129 | struct acpi_pci_root *root = acpi_pci_find_root(handle); |
| 1130 | bus = root->bus; |
| 1131 | } else { |
| 1132 | struct pci_dev *pdev = acpi_get_pci_dev(handle); |
| 1133 | bus = pdev->subordinate; |
| 1134 | pci_dev_put(pdev); |
| 1135 | } |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1136 | |
| 1137 | pci_bus_size_bridges(bus); |
| 1138 | pci_bus_assign_resources(bus); |
| 1139 | acpiphp_sanitize_bus(bus); |
Bjorn Helgaas | fca6825 | 2009-09-14 16:35:10 -0600 | [diff] [blame] | 1140 | acpiphp_set_hpp_values(bus); |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1141 | pci_enable_bridges(bus); |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static void handle_bridge_insertion(acpi_handle handle, u32 type) |
| 1146 | { |
| 1147 | struct acpi_device *device, *pdevice; |
| 1148 | acpi_handle phandle; |
| 1149 | |
| 1150 | if ((type != ACPI_NOTIFY_BUS_CHECK) && |
| 1151 | (type != ACPI_NOTIFY_DEVICE_CHECK)) { |
| 1152 | err("unexpected notification type %d\n", type); |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | acpi_get_parent(handle, &phandle); |
| 1157 | if (acpi_bus_get_device(phandle, &pdevice)) { |
| 1158 | dbg("no parent device, assuming NULL\n"); |
| 1159 | pdevice = NULL; |
| 1160 | } |
| 1161 | if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) { |
| 1162 | err("cannot add bridge to acpi list\n"); |
| 1163 | return; |
| 1164 | } |
| 1165 | if (!acpiphp_configure_bridge(handle) && |
| 1166 | !acpi_bus_start(device)) |
| 1167 | add_bridge(handle); |
| 1168 | else |
| 1169 | err("cannot configure and start bridge\n"); |
| 1170 | |
| 1171 | } |
| 1172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | /* |
| 1174 | * ACPI event handlers |
| 1175 | */ |
| 1176 | |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1177 | static acpi_status |
| 1178 | count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 1179 | { |
| 1180 | int *count = (int *)context; |
| 1181 | struct acpiphp_bridge *bridge; |
| 1182 | |
| 1183 | bridge = acpiphp_handle_to_bridge(handle); |
| 1184 | if (bridge) |
| 1185 | (*count)++; |
| 1186 | return AE_OK ; |
| 1187 | } |
| 1188 | |
| 1189 | static acpi_status |
| 1190 | check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 1191 | { |
| 1192 | struct acpiphp_bridge *bridge; |
| 1193 | char objname[64]; |
| 1194 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1195 | .pointer = objname }; |
| 1196 | |
| 1197 | bridge = acpiphp_handle_to_bridge(handle); |
| 1198 | if (bridge) { |
| 1199 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1200 | dbg("%s: re-enumerating slots under %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1201 | __func__, objname); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1202 | acpiphp_check_bridge(bridge); |
| 1203 | } |
| 1204 | return AE_OK ; |
| 1205 | } |
| 1206 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1207 | struct acpiphp_hp_work { |
| 1208 | struct work_struct work; |
| 1209 | acpi_handle handle; |
| 1210 | u32 type; |
| 1211 | void *context; |
| 1212 | }; |
| 1213 | |
| 1214 | static void alloc_acpiphp_hp_work(acpi_handle handle, u32 type, |
| 1215 | void *context, |
| 1216 | void (*func)(struct work_struct *work)) |
| 1217 | { |
| 1218 | struct acpiphp_hp_work *hp_work; |
| 1219 | int ret; |
| 1220 | |
| 1221 | hp_work = kmalloc(sizeof(*hp_work), GFP_KERNEL); |
| 1222 | if (!hp_work) |
| 1223 | return; |
| 1224 | |
| 1225 | hp_work->handle = handle; |
| 1226 | hp_work->type = type; |
| 1227 | hp_work->context = context; |
| 1228 | |
| 1229 | INIT_WORK(&hp_work->work, func); |
| 1230 | ret = queue_work(kacpi_hotplug_wq, &hp_work->work); |
| 1231 | if (!ret) |
| 1232 | kfree(hp_work); |
| 1233 | } |
| 1234 | |
| 1235 | static void _handle_hotplug_event_bridge(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
| 1237 | struct acpiphp_bridge *bridge; |
| 1238 | char objname[64]; |
| 1239 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1240 | .pointer = objname }; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1241 | struct acpi_device *device; |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1242 | int num_sub_bridges = 0; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1243 | struct acpiphp_hp_work *hp_work; |
| 1244 | acpi_handle handle; |
| 1245 | u32 type; |
| 1246 | |
| 1247 | hp_work = container_of(work, struct acpiphp_hp_work, work); |
| 1248 | handle = hp_work->handle; |
| 1249 | type = hp_work->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1251 | if (acpi_bus_get_device(handle, &device)) { |
| 1252 | /* This bridge must have just been physically inserted */ |
| 1253 | handle_bridge_insertion(handle, type); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1254 | goto out; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | bridge = acpiphp_handle_to_bridge(handle); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1258 | if (type == ACPI_NOTIFY_BUS_CHECK) { |
| 1259 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 1260 | count_sub_bridges, NULL, &num_sub_bridges, NULL); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | if (!bridge && !num_sub_bridges) { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1264 | err("cannot get bridge info\n"); |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1265 | goto out; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
| 1268 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1269 | |
| 1270 | switch (type) { |
| 1271 | case ACPI_NOTIFY_BUS_CHECK: |
| 1272 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1273 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1274 | if (bridge) { |
| 1275 | dbg("%s: re-enumerating slots under %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1276 | __func__, objname); |
Gary Hade | 0bbd642 | 2007-07-05 11:10:48 -0700 | [diff] [blame] | 1277 | acpiphp_check_bridge(bridge); |
| 1278 | } |
| 1279 | if (num_sub_bridges) |
| 1280 | acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 1281 | ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | break; |
| 1283 | |
| 1284 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1285 | /* device check */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1286 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | acpiphp_check_bridge(bridge); |
| 1288 | break; |
| 1289 | |
| 1290 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1291 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1292 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | break; |
| 1294 | |
| 1295 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1296 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1297 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
MUNEDA Takahiro | 551bcb7 | 2006-03-22 14:49:20 +0900 | [diff] [blame] | 1298 | if ((bridge->type != BRIDGE_TYPE_HOST) && |
| 1299 | (bridge->flags & BRIDGE_HAS_EJ0)) { |
| 1300 | struct acpiphp_slot *slot; |
| 1301 | slot = bridge->func->slot; |
| 1302 | if (!acpiphp_disable_slot(slot)) |
| 1303 | acpiphp_eject_slot(slot); |
| 1304 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | break; |
| 1306 | |
| 1307 | case ACPI_NOTIFY_FREQUENCY_MISMATCH: |
| 1308 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1309 | " to a frequency mismatch\n", objname); |
| 1310 | break; |
| 1311 | |
| 1312 | case ACPI_NOTIFY_BUS_MODE_MISMATCH: |
| 1313 | printk(KERN_ERR "Device %s cannot be configured due" |
| 1314 | " to a bus mode mismatch\n", objname); |
| 1315 | break; |
| 1316 | |
| 1317 | case ACPI_NOTIFY_POWER_FAULT: |
| 1318 | printk(KERN_ERR "Device %s has suffered a power fault\n", |
| 1319 | objname); |
| 1320 | break; |
| 1321 | |
| 1322 | default: |
| 1323 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1324 | break; |
| 1325 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1326 | |
| 1327 | out: |
| 1328 | kfree(hp_work); /* allocated in handle_hotplug_event_bridge */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | /** |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1332 | * handle_hotplug_event_bridge - handle ACPI event on bridges |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | * @handle: Notify()'ed acpi_handle |
| 1334 | * @type: Notify code |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1335 | * @context: pointer to acpiphp_bridge structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | * |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1337 | * Handles ACPI event notification on {host,p2p} bridges. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | */ |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1339 | static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, |
| 1340 | void *context) |
| 1341 | { |
| 1342 | /* |
| 1343 | * Currently the code adds all hotplug events to the kacpid_wq |
| 1344 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 1345 | * The proper way to fix this is to reorganize the code so that |
| 1346 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 1347 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 1348 | * don't deadlock on hotplug actions. |
| 1349 | */ |
| 1350 | alloc_acpiphp_hp_work(handle, type, context, |
| 1351 | _handle_hotplug_event_bridge); |
| 1352 | } |
| 1353 | |
| 1354 | static void _handle_hotplug_event_func(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | { |
| 1356 | struct acpiphp_func *func; |
| 1357 | char objname[64]; |
| 1358 | struct acpi_buffer buffer = { .length = sizeof(objname), |
| 1359 | .pointer = objname }; |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1360 | struct acpiphp_hp_work *hp_work; |
| 1361 | acpi_handle handle; |
| 1362 | u32 type; |
| 1363 | void *context; |
| 1364 | |
| 1365 | hp_work = container_of(work, struct acpiphp_hp_work, work); |
| 1366 | handle = hp_work->handle; |
| 1367 | type = hp_work->type; |
| 1368 | context = hp_work->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
| 1370 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1371 | |
| 1372 | func = (struct acpiphp_func *)context; |
| 1373 | |
| 1374 | switch (type) { |
| 1375 | case ACPI_NOTIFY_BUS_CHECK: |
| 1376 | /* bus re-enumerate */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1377 | dbg("%s: Bus check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | acpiphp_enable_slot(func->slot); |
| 1379 | break; |
| 1380 | |
| 1381 | case ACPI_NOTIFY_DEVICE_CHECK: |
| 1382 | /* device check : re-enumerate from parent bus */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1383 | dbg("%s: Device check notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | acpiphp_check_bridge(func->slot->bridge); |
| 1385 | break; |
| 1386 | |
| 1387 | case ACPI_NOTIFY_DEVICE_WAKE: |
| 1388 | /* wake event */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1389 | dbg("%s: Device wake notify on %s\n", __func__, objname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 1393 | /* request device eject */ |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1394 | dbg("%s: Device eject notify on %s\n", __func__, objname); |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1395 | if (!(acpiphp_disable_slot(func->slot))) |
| 1396 | acpiphp_eject_slot(func->slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | |
| 1399 | default: |
| 1400 | warn("notify_handler: unknown event type 0x%x for %s\n", type, objname); |
| 1401 | break; |
| 1402 | } |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1403 | |
| 1404 | kfree(hp_work); /* allocated in handle_hotplug_event_func */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Prarit Bhargava | 6af8bef | 2011-09-28 19:40:53 -0400 | [diff] [blame] | 1407 | /** |
| 1408 | * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots) |
| 1409 | * @handle: Notify()'ed acpi_handle |
| 1410 | * @type: Notify code |
| 1411 | * @context: pointer to acpiphp_func structure |
| 1412 | * |
| 1413 | * Handles ACPI event notification on slots. |
| 1414 | */ |
| 1415 | static void handle_hotplug_event_func(acpi_handle handle, u32 type, |
| 1416 | void *context) |
| 1417 | { |
| 1418 | /* |
| 1419 | * Currently the code adds all hotplug events to the kacpid_wq |
| 1420 | * queue when it should add hotplug events to the kacpi_hotplug_wq. |
| 1421 | * The proper way to fix this is to reorganize the code so that |
| 1422 | * drivers (dock, etc.) do not call acpi_os_execute(), etc. |
| 1423 | * For now just re-add this work to the kacpi_hotplug_wq so we |
| 1424 | * don't deadlock on hotplug actions. |
| 1425 | */ |
| 1426 | alloc_acpiphp_hp_work(handle, type, context, |
| 1427 | _handle_hotplug_event_func); |
| 1428 | } |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1429 | |
| 1430 | static acpi_status |
| 1431 | find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv) |
| 1432 | { |
| 1433 | int *count = (int *)context; |
| 1434 | |
Rafael J. Wysocki | 0d52f54 | 2011-10-22 00:43:38 +0200 | [diff] [blame] | 1435 | if (!acpi_is_root_bridge(handle)) |
| 1436 | return AE_OK; |
| 1437 | |
Rafael J. Wysocki | 0d52f54 | 2011-10-22 00:43:38 +0200 | [diff] [blame] | 1438 | (*count)++; |
| 1439 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 1440 | handle_hotplug_event_bridge, NULL); |
| 1441 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1442 | return AE_OK ; |
| 1443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
| 1445 | static struct acpi_pci_driver acpi_pci_hp_driver = { |
| 1446 | .add = add_bridge, |
| 1447 | .remove = remove_bridge, |
| 1448 | }; |
| 1449 | |
| 1450 | /** |
| 1451 | * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | */ |
| 1453 | int __init acpiphp_glue_init(void) |
| 1454 | { |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1455 | int num = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1457 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
Lin Ming | 2263576 | 2009-11-13 10:06:08 +0800 | [diff] [blame] | 1458 | ACPI_UINT32_MAX, find_root_bridges, NULL, &num, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
| 1460 | if (num <= 0) |
| 1461 | return -1; |
Rajesh Shah | 8e7561c | 2005-04-28 00:25:56 -0700 | [diff] [blame] | 1462 | else |
| 1463 | acpi_pci_register_driver(&acpi_pci_hp_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
| 1468 | |
| 1469 | /** |
| 1470 | * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures |
| 1471 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1472 | * This function frees all data allocated in acpiphp_glue_init(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | */ |
Kristen Carlson Accardi | 031f30d | 2006-12-16 15:26:04 -0800 | [diff] [blame] | 1474 | void acpiphp_glue_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | acpi_pci_unregister_driver(&acpi_pci_hp_driver); |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | /** |
| 1481 | * acpiphp_get_num_slots - count number of slots in a system |
| 1482 | */ |
| 1483 | int __init acpiphp_get_num_slots(void) |
| 1484 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | struct acpiphp_bridge *bridge; |
Akinobu Mita | 467c442 | 2006-10-30 13:07:58 -0800 | [diff] [blame] | 1486 | int num_slots = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Alex Chiang | 58c0862 | 2009-10-26 21:25:27 -0600 | [diff] [blame] | 1488 | list_for_each_entry(bridge, &bridge_list, list) { |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1489 | dbg("Bus %04x:%02x has %d slot%s\n", |
| 1490 | pci_domain_nr(bridge->pci_bus), |
| 1491 | bridge->pci_bus->number, bridge->nr_slots, |
| 1492 | bridge->nr_slots == 1 ? "" : "s"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | num_slots += bridge->nr_slots; |
| 1494 | } |
| 1495 | |
Rajesh Shah | 42f49a6 | 2005-04-28 00:25:53 -0700 | [diff] [blame] | 1496 | dbg("Total %d slots\n", num_slots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | return num_slots; |
| 1498 | } |
| 1499 | |
| 1500 | |
| 1501 | #if 0 |
| 1502 | /** |
| 1503 | * acpiphp_for_each_slot - call function for each slot |
| 1504 | * @fn: callback function |
| 1505 | * @data: context to be passed to callback function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | */ |
| 1507 | static int acpiphp_for_each_slot(acpiphp_callback fn, void *data) |
| 1508 | { |
| 1509 | struct list_head *node; |
| 1510 | struct acpiphp_bridge *bridge; |
| 1511 | struct acpiphp_slot *slot; |
| 1512 | int retval = 0; |
| 1513 | |
| 1514 | list_for_each (node, &bridge_list) { |
| 1515 | bridge = (struct acpiphp_bridge *)node; |
| 1516 | for (slot = bridge->slots; slot; slot = slot->next) { |
| 1517 | retval = fn(slot, data); |
| 1518 | if (!retval) |
| 1519 | goto err_exit; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | err_exit: |
| 1524 | return retval; |
| 1525 | } |
| 1526 | #endif |
| 1527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
| 1529 | /** |
| 1530 | * acpiphp_enable_slot - power on slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1531 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | */ |
| 1533 | int acpiphp_enable_slot(struct acpiphp_slot *slot) |
| 1534 | { |
| 1535 | int retval; |
| 1536 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1537 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | /* wake up all functions */ |
| 1540 | retval = power_on_slot(slot); |
| 1541 | if (retval) |
| 1542 | goto err_exit; |
| 1543 | |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1544 | if (get_slot_status(slot) == ACPI_STA_ALL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | /* configure all functions */ |
| 1546 | retval = enable_device(slot); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1547 | if (retval) |
| 1548 | power_off_slot(slot); |
| 1549 | } else { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 1550 | dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__); |
MUNEDA Takahiro | cde0e5d | 2006-03-22 14:49:33 +0900 | [diff] [blame] | 1551 | power_off_slot(slot); |
| 1552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
| 1554 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1555 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | return retval; |
| 1557 | } |
| 1558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | /** |
| 1560 | * acpiphp_disable_slot - power off slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 1561 | * @slot: ACPI PHP slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | */ |
| 1563 | int acpiphp_disable_slot(struct acpiphp_slot *slot) |
| 1564 | { |
| 1565 | int retval = 0; |
| 1566 | |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1567 | mutex_lock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* unconfigure all functions */ |
| 1570 | retval = disable_device(slot); |
| 1571 | if (retval) |
| 1572 | goto err_exit; |
| 1573 | |
| 1574 | /* power off all functions */ |
| 1575 | retval = power_off_slot(slot); |
| 1576 | if (retval) |
| 1577 | goto err_exit; |
| 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | err_exit: |
Ingo Molnar | 6aa4cdd | 2006-01-13 16:02:15 +0100 | [diff] [blame] | 1580 | mutex_unlock(&slot->crit_sect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | return retval; |
| 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | /* |
| 1586 | * slot enabled: 1 |
| 1587 | * slot disabled: 0 |
| 1588 | */ |
| 1589 | u8 acpiphp_get_power_status(struct acpiphp_slot *slot) |
| 1590 | { |
Rajesh Shah | 8d50e33 | 2005-04-28 00:25:57 -0700 | [diff] [blame] | 1591 | return (slot->flags & SLOT_POWEREDON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | /* |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1596 | * latch open: 1 |
| 1597 | * latch closed: 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | */ |
| 1599 | u8 acpiphp_get_latch_status(struct acpiphp_slot *slot) |
| 1600 | { |
| 1601 | unsigned int sta; |
| 1602 | |
| 1603 | sta = get_slot_status(slot); |
| 1604 | |
MUNEDA Takahiro | 35ae61a | 2006-10-25 11:44:57 -0700 | [diff] [blame] | 1605 | return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | /* |
| 1610 | * adapter presence : 1 |
| 1611 | * absence : 0 |
| 1612 | */ |
| 1613 | u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot) |
| 1614 | { |
| 1615 | unsigned int sta; |
| 1616 | |
| 1617 | sta = get_slot_status(slot); |
| 1618 | |
| 1619 | return (sta == 0) ? 0 : 1; |
| 1620 | } |