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