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