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