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