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_bridge (acpi_handle, u32, void *);
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_func(acpi_handle handle, u32 type, void *context);
66 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
67 static void free_bridge(struct kref *kref);
68 
69 /* callback routine to check for the existence of a pci dock device */
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 
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 
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 {
163 	struct acpiphp_context *context;
164 	struct acpiphp_bridge *bridge;
165 	struct acpiphp_slot *slot, *next;
166 	struct acpiphp_func *func, *tmp;
167 
168 	mutex_lock(&acpiphp_context_lock);
169 
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) {
174 			context = func->context;
175 			context->func = NULL;
176 			acpiphp_put_context(context);
177 			kfree(func);
178 		}
179 		kfree(slot);
180 	}
181 
182 	/* Release the reference acquired by acpiphp_enumerate_slots(). */
183 	if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)
184 		put_bridge(bridge->func->slot->bridge);
185 
186 	put_device(&bridge->pci_bus->dev);
187 	pci_dev_put(bridge->pci_dev);
188 	context = bridge->context;
189 	context->bridge = NULL;
190 	acpiphp_put_context(context);
191 	kfree(bridge);
192 
193 	mutex_unlock(&acpiphp_context_lock);
194 }
195 
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  */
203 static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
204 {
205 	struct acpiphp_func *func = data;
206 	struct pci_bus *bus = func->slot->bridge->pci_bus;
207 	u32 buses;
208 
209 	if (!bus->self)
210 		return;
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 
219 	if (((buses >> 8) & 0xff) != bus->busn_res.start) {
220 		buses = (buses & 0xff000000)
221 			| ((unsigned int)(bus->primary)     <<  0)
222 			| ((unsigned int)(bus->busn_res.start)   <<  8)
223 			| ((unsigned int)(bus->busn_res.end) << 16);
224 		pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
225 	}
226 }
227 
228 
229 static const struct acpi_dock_ops acpiphp_dock_ops = {
230 	.fixup = post_dock_fixups,
231 	.handler = hotplug_event_func,
232 };
233 
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, &reg32))
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 
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 
277 /* callback routine to register each ACPI PCI slot object */
278 static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
279 				 void **rv)
280 {
281 	struct acpiphp_bridge *bridge = data;
282 	struct acpiphp_context *context;
283 	struct acpiphp_slot *slot;
284 	struct acpiphp_func *newfunc;
285 	acpi_status status = AE_OK;
286 	unsigned long long adr, sun;
287 	int device, function, retval, found = 0;
288 	struct pci_bus *pbus = bridge->pci_bus;
289 	struct pci_dev *pdev;
290 	u32 val;
291 
292 	if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
293 		return AE_OK;
294 
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 
304 	pdev = bridge->pci_dev;
305 	if (pdev && device_is_managed_by_native_pciehp(pdev))
306 		return AE_OK;
307 
308 	newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
309 	if (!newfunc)
310 		return AE_NO_MEMORY;
311 
312 	newfunc->handle = handle;
313 	newfunc->function = function;
314 
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 
327 	if (acpi_has_method(handle, "_EJ0"))
328 		newfunc->flags = FUNC_HAS_EJ0;
329 
330 	if (acpi_has_method(handle, "_STA"))
331 		newfunc->flags |= FUNC_HAS_STA;
332 
333 	if (acpi_has_method(handle, "_PS0"))
334 		newfunc->flags |= FUNC_HAS_PS0;
335 
336 	if (acpi_has_method(handle, "_PS3"))
337 		newfunc->flags |= FUNC_HAS_PS3;
338 
339 	if (acpi_has_method(handle, "_DCK"))
340 		newfunc->flags |= FUNC_HAS_DCK;
341 
342 	status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
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 	}
350 
351 	/* search for objects that share the same slot */
352 	list_for_each_entry(slot, &bridge->slots, node)
353 		if (slot->device == device) {
354 			if (slot->sun != sun)
355 				warn("sibling found, but _SUN doesn't match!\n");
356 			found = 1;
357 			break;
358 		}
359 
360 	if (!found) {
361 		slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
362 		if (!slot) {
363 			status = AE_NO_MEMORY;
364 			goto err_out;
365 		}
366 
367 		slot->bridge = bridge;
368 		slot->device = device;
369 		slot->sun = sun;
370 		INIT_LIST_HEAD(&slot->funcs);
371 		mutex_init(&slot->crit_sect);
372 
373 		mutex_lock(&bridge_mutex);
374 		list_add_tail(&slot->node, &bridge->slots);
375 		mutex_unlock(&bridge_mutex);
376 		bridge->nr_slots++;
377 
378 		dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
379 		    slot->sun, pci_domain_nr(pbus), pbus->number, device);
380 		retval = acpiphp_register_hotplug_slot(slot);
381 		if (retval) {
382 			if (retval == -EBUSY)
383 				warn("Slot %llu already registered by another "
384 					"hotplug driver\n", slot->sun);
385 			else
386 				warn("acpiphp_register_hotplug_slot failed "
387 					"(err code = 0x%x)\n", retval);
388 
389 			status = AE_OK;
390 			goto err;
391 		}
392 	}
393 
394 	newfunc->slot = slot;
395 	mutex_lock(&bridge_mutex);
396 	list_add_tail(&newfunc->sibling, &slot->funcs);
397 	mutex_unlock(&bridge_mutex);
398 
399 	if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
400 				       &val, 60*1000))
401 		slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
402 
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
407 		 */
408 		newfunc->flags &= ~FUNC_HAS_EJ0;
409 		if (register_hotplug_dock_device(handle,
410 			&acpiphp_dock_ops, newfunc,
411 			acpiphp_dock_init, acpiphp_dock_release))
412 			dbg("failed to register dock device\n");
413 	}
414 
415 	/* install notify handler */
416 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
417 		status = acpi_install_notify_handler(handle,
418 					     ACPI_SYSTEM_NOTIFY,
419 					     handle_hotplug_event_func,
420 					     newfunc);
421 
422 		if (ACPI_FAILURE(status))
423 			err("failed to register interrupt notify handler\n");
424 	}
425 
426 	return AE_OK;
427 
428  err:
429 	bridge->nr_slots--;
430 	mutex_lock(&bridge_mutex);
431 	list_del(&slot->node);
432 	mutex_unlock(&bridge_mutex);
433 	kfree(slot);
434 
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;
442 }
443 
444 
445 /* see if it's worth looking at this bridge */
446 static int detect_ejectable_slots(acpi_handle handle)
447 {
448 	int found = acpi_pci_detect_ejectable(handle);
449 	if (!found) {
450 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
451 				    is_pci_dock_device, NULL, (void *)&found, NULL);
452 	}
453 	return found;
454 }
455 
456 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
457 {
458 	struct acpiphp_bridge *bridge;
459 
460 	mutex_lock(&bridge_mutex);
461 	list_for_each_entry(bridge, &bridge_list, list)
462 		if (bridge->handle == handle) {
463 			get_bridge(bridge);
464 			mutex_unlock(&bridge_mutex);
465 			return bridge;
466 		}
467 	mutex_unlock(&bridge_mutex);
468 
469 	return NULL;
470 }
471 
472 static void cleanup_bridge(struct acpiphp_bridge *bridge)
473 {
474 	struct acpiphp_slot *slot;
475 	struct acpiphp_func *func;
476 	acpi_status status;
477 	acpi_handle handle = bridge->handle;
478 
479 	if (!pci_is_root_bus(bridge->pci_bus)) {
480 		status = acpi_remove_notify_handler(handle,
481 					    ACPI_SYSTEM_NOTIFY,
482 					    handle_hotplug_event_bridge);
483 		if (ACPI_FAILURE(status))
484 			err("failed to remove notify handler\n");
485 	}
486 
487 	if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
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 
496 	list_for_each_entry(slot, &bridge->slots, node) {
497 		list_for_each_entry(func, &slot->funcs, sibling) {
498 			if (is_dock_device(func->handle)) {
499 				unregister_hotplug_dock_device(func->handle);
500 			}
501 			if (!(func->flags & FUNC_HAS_DCK)) {
502 				status = acpi_remove_notify_handler(func->handle,
503 						ACPI_SYSTEM_NOTIFY,
504 						handle_hotplug_event_func);
505 				if (ACPI_FAILURE(status))
506 					err("failed to remove notify handler\n");
507 			}
508 		}
509 		acpiphp_unregister_hotplug_slot(slot);
510 	}
511 
512 	mutex_lock(&bridge_mutex);
513 	list_del(&bridge->list);
514 	mutex_unlock(&bridge_mutex);
515 }
516 
517 static int power_on_slot(struct acpiphp_slot *slot)
518 {
519 	acpi_status status;
520 	struct acpiphp_func *func;
521 	int retval = 0;
522 
523 	/* if already enabled, just skip */
524 	if (slot->flags & SLOT_POWEREDON)
525 		goto err_exit;
526 
527 	list_for_each_entry(func, &slot->funcs, sibling) {
528 		if (func->flags & FUNC_HAS_PS0) {
529 			dbg("%s: executing _PS0\n", __func__);
530 			status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
531 			if (ACPI_FAILURE(status)) {
532 				warn("%s: _PS0 failed\n", __func__);
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;
553 
554 	int retval = 0;
555 
556 	/* if already disabled, just skip */
557 	if ((slot->flags & SLOT_POWEREDON) == 0)
558 		goto err_exit;
559 
560 	list_for_each_entry(func, &slot->funcs, sibling) {
561 		if (func->flags & FUNC_HAS_PS3) {
562 			status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
563 			if (ACPI_FAILURE(status)) {
564 				warn("%s: _PS3 failed\n", __func__);
565 				retval = -1;
566 				goto err_exit;
567 			} else
568 				break;
569 		}
570 	}
571 
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 
581 
582 /**
583  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
584  * @bus: bus to start search with
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 	 */
599 	max = bus->busn_res.start;
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 
610 /**
611  * acpiphp_bus_add - add a new bus to acpi subsystem
612  * @func: acpiphp_func of the bridge
613  */
614 static int acpiphp_bus_add(struct acpiphp_func *func)
615 {
616 	struct acpi_device *device;
617 	int ret_val;
618 
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 		 */
624 		acpi_bus_trim(device);
625 	}
626 
627 	ret_val = acpi_bus_scan(func->handle);
628 	if (!ret_val)
629 		ret_val = acpi_bus_get_device(func->handle, &device);
630 
631 	if (ret_val)
632 		dbg("error adding bus, %x\n", -ret_val);
633 
634 	return ret_val;
635 }
636 
637 
638 /**
639  * acpiphp_bus_trim - trim a bus from acpi subsystem
640  * @handle: handle to acpi namespace
641  */
642 static int acpiphp_bus_trim(acpi_handle handle)
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 
653 	acpi_bus_trim(device);
654 	return 0;
655 }
656 
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 
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 }
698 
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.
705  */
706 static int __ref enable_device(struct acpiphp_slot *slot)
707 {
708 	struct pci_dev *dev;
709 	struct pci_bus *bus = slot->bridge->pci_bus;
710 	struct acpiphp_func *func;
711 	int num, max, pass;
712 	LIST_HEAD(add_list);
713 
714 	if (slot->flags & SLOT_ENABLED)
715 		goto err_exit;
716 
717 	list_for_each_entry(func, &slot->funcs, sibling)
718 		acpiphp_bus_add(func);
719 
720 	num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
721 	if (num == 0) {
722 		/* Maybe only part of funcs are added. */
723 		dbg("No new device found\n");
724 		goto err_exit;
725 	}
726 
727 	max = acpiphp_max_busnr(bus);
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 ||
733 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
734 				max = pci_scan_bridge(bus, dev, max, pass);
735 				if (pass && dev->subordinate) {
736 					check_hotplug_bridge(slot, dev);
737 					pcibios_resource_survey_bus(dev->subordinate);
738 					__pci_bus_size_bridges(dev->subordinate,
739 							       &add_list);
740 				}
741 			}
742 		}
743 	}
744 
745 	__pci_bus_assign_resources(bus, &add_list, NULL);
746 	acpiphp_sanitize_bus(bus);
747 	acpiphp_set_hpp_values(bus);
748 	acpiphp_set_acpi_region(slot);
749 	pci_enable_bridges(bus);
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 
757 	pci_bus_add_devices(bus);
758 
759 	slot->flags |= SLOT_ENABLED;
760 	list_for_each_entry(func, &slot->funcs, sibling) {
761 		dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
762 						  func->function));
763 		if (!dev) {
764 			/* Do not set SLOT_ENABLED flag if some funcs
765 			   are not added. */
766 			slot->flags &= (~SLOT_ENABLED);
767 			continue;
768 		}
769 	}
770 
771 
772  err_exit:
773 	return 0;
774 }
775 
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 
794 /**
795  * disable_device - disable a slot
796  * @slot: ACPI PHP slot
797  */
798 static int disable_device(struct acpiphp_slot *slot)
799 {
800 	struct acpiphp_func *func;
801 	struct pci_dev *pdev;
802 
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))) {
810 		pci_stop_and_remove_bus_device(pdev);
811 		pci_dev_put(pdev);
812 	}
813 
814 	list_for_each_entry(func, &slot->funcs, sibling) {
815 		acpiphp_bus_trim(func->handle);
816 	}
817 
818 	slot->flags &= (~SLOT_ENABLED);
819 
820 	return 0;
821 }
822 
823 
824 /**
825  * get_slot_status - get ACPI slot status
826  * @slot: ACPI PHP slot
827  *
828  * If a slot has _STA for each function and if any one of them
829  * returned non-zero status, return it.
830  *
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.
833  *
834  * Otherwise return 0.
835  */
836 static unsigned int get_slot_status(struct acpiphp_slot *slot)
837 {
838 	acpi_status status;
839 	unsigned long long sta = 0;
840 	u32 dvid;
841 	struct acpiphp_func *func;
842 
843 	list_for_each_entry(func, &slot->funcs, sibling) {
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 /**
864  * acpiphp_eject_slot - physically eject the slot
865  * @slot: ACPI PHP slot
866  */
867 int acpiphp_eject_slot(struct acpiphp_slot *slot)
868 {
869 	struct acpiphp_func *func;
870 
871 	list_for_each_entry(func, &slot->funcs, sibling) {
872 		/* We don't want to call _EJ0 on non-existing functions. */
873 		if ((func->flags & FUNC_HAS_EJ0)) {
874 			if (ACPI_FAILURE(acpi_evaluate_ej0(func->handle)))
875 				return -1;
876 			else
877 				break;
878 		}
879 	}
880 	return 0;
881 }
882 
883 /**
884  * acpiphp_check_bridge - re-enumerate devices
885  * @bridge: where to begin re-enumeration
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 
898 	list_for_each_entry(slot, &bridge->slots, node) {
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;
907 			} else {
908 				acpiphp_eject_slot(slot);
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 
923 	dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
924 
925  err_exit:
926 	return retval;
927 }
928 
929 static void acpiphp_set_hpp_values(struct pci_bus *bus)
930 {
931 	struct pci_dev *dev;
932 
933 	list_for_each_entry(dev, &bus->devices, bus_list)
934 		pci_configure_slot(dev);
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 {
943 	struct pci_dev *dev, *tmp;
944 	int i;
945 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
946 
947 	list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
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 */
954 				pci_stop_and_remove_bus_device(dev);
955 				break;
956 			}
957 		}
958 	}
959 }
960 
961 /*
962  * ACPI event handlers
963  */
964 
965 static acpi_status
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",
977 			__func__, objname);
978 		acpiphp_check_bridge(bridge);
979 		put_bridge(bridge);
980 	}
981 	return AE_OK ;
982 }
983 
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 
998 static void _handle_hotplug_event_bridge(struct work_struct *work)
999 {
1000 	struct acpiphp_bridge *bridge;
1001 	char objname[64];
1002 	struct acpi_buffer buffer = { .length = sizeof(objname),
1003 				      .pointer = objname };
1004 	struct acpi_hp_work *hp_work;
1005 	acpi_handle handle;
1006 	u32 type;
1007 
1008 	hp_work = container_of(work, struct acpi_hp_work, work);
1009 	handle = hp_work->handle;
1010 	type = hp_work->type;
1011 	bridge = (struct acpiphp_bridge *)hp_work->context;
1012 
1013 	acpi_scan_lock_acquire();
1014 
1015 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1016 
1017 	switch (type) {
1018 	case ACPI_NOTIFY_BUS_CHECK:
1019 		/* bus re-enumerate */
1020 		dbg("%s: Bus check notify on %s\n", __func__, objname);
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);
1025 		break;
1026 
1027 	case ACPI_NOTIFY_DEVICE_CHECK:
1028 		/* device check */
1029 		dbg("%s: Device check notify on %s\n", __func__, objname);
1030 		acpiphp_check_bridge(bridge);
1031 		break;
1032 
1033 	case ACPI_NOTIFY_DEVICE_WAKE:
1034 		/* wake event */
1035 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1036 		break;
1037 
1038 	case ACPI_NOTIFY_EJECT_REQUEST:
1039 		/* request device eject */
1040 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1041 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1042 			struct acpiphp_slot *slot;
1043 			slot = bridge->func->slot;
1044 			if (!acpiphp_disable_slot(slot))
1045 				acpiphp_eject_slot(slot);
1046 		}
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 	}
1068 
1069 	acpi_scan_lock_release();
1070 	kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1071 	put_bridge(bridge);
1072 }
1073 
1074 /**
1075  * handle_hotplug_event_bridge - handle ACPI event on bridges
1076  * @handle: Notify()'ed acpi_handle
1077  * @type: Notify code
1078  * @context: pointer to acpiphp_bridge structure
1079  *
1080  * Handles ACPI event notification on {host,p2p} bridges.
1081  */
1082 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1083 					void *context)
1084 {
1085 	struct acpiphp_bridge *bridge = context;
1086 
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 	 */
1095 	get_bridge(bridge);
1096 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1097 }
1098 
1099 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1100 {
1101 	struct acpiphp_func *func = context;
1102 	char objname[64];
1103 	struct acpi_buffer buffer = { .length = sizeof(objname),
1104 				      .pointer = objname };
1105 
1106 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1107 
1108 	switch (type) {
1109 	case ACPI_NOTIFY_BUS_CHECK:
1110 		/* bus re-enumerate */
1111 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1112 		acpiphp_enable_slot(func->slot);
1113 		break;
1114 
1115 	case ACPI_NOTIFY_DEVICE_CHECK:
1116 		/* device check : re-enumerate from parent bus */
1117 		dbg("%s: Device check notify on %s\n", __func__, objname);
1118 		acpiphp_check_bridge(func->slot->bridge);
1119 		break;
1120 
1121 	case ACPI_NOTIFY_DEVICE_WAKE:
1122 		/* wake event */
1123 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1124 		break;
1125 
1126 	case ACPI_NOTIFY_EJECT_REQUEST:
1127 		/* request device eject */
1128 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1129 		if (!(acpiphp_disable_slot(func->slot)))
1130 			acpiphp_eject_slot(func->slot);
1131 		break;
1132 
1133 	default:
1134 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1135 		break;
1136 	}
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);
1149 
1150 	acpi_scan_lock_release();
1151 	kfree(hp_work); /* allocated in handle_hotplug_event_func */
1152 	put_bridge(func->slot->bridge);
1153 }
1154 
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 {
1166 	struct acpiphp_func *func = context;
1167 
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 	 */
1176 	get_bridge(func->slot->bridge);
1177 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1178 }
1179 
1180 /*
1181  * Create hotplug slots for the PCI bus.
1182  * It should always return 0 to avoid skipping following notifiers.
1183  */
1184 void acpiphp_enumerate_slots(struct pci_bus *bus)
1185 {
1186 	struct acpiphp_context *context;
1187 	struct acpiphp_bridge *bridge;
1188 	acpi_handle handle;
1189 	acpi_status status;
1190 
1191 	if (acpiphp_disabled)
1192 		return;
1193 
1194 	handle = ACPI_HANDLE(bus->bridge);
1195 	if (!handle || detect_ejectable_slots(handle) <= 0)
1196 		return;
1197 
1198 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1199 	if (!bridge) {
1200 		acpi_handle_err(handle, "No memory for bridge object\n");
1201 		return;
1202 	}
1203 
1204 	INIT_LIST_HEAD(&bridge->slots);
1205 	kref_init(&bridge->ref);
1206 	bridge->handle = handle;
1207 	bridge->pci_dev = pci_dev_get(bus->self);
1208 	bridge->pci_bus = bus;
1209 
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 
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 
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;
1243 	}
1244 
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;
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,
1269 						    handle_hotplug_event_func);
1270 		if (ACPI_FAILURE(status))
1271 			acpi_handle_err(handle,
1272 					"failed to remove notify handler\n");
1273 	}
1274 	return;
1275 
1276  err:
1277 	cleanup_bridge(bridge);
1278 	put_bridge(bridge);
1279 }
1280 
1281 /* Destroy hotplug slots associated with the PCI bus */
1282 void acpiphp_remove_slots(struct pci_bus *bus)
1283 {
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);
1292 			put_bridge(bridge);
1293 			break;
1294 		}
1295 }
1296 
1297 /**
1298  * acpiphp_enable_slot - power on slot
1299  * @slot: ACPI PHP slot
1300  */
1301 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1302 {
1303 	int retval;
1304 
1305 	mutex_lock(&slot->crit_sect);
1306 
1307 	/* wake up all functions */
1308 	retval = power_on_slot(slot);
1309 	if (retval)
1310 		goto err_exit;
1311 
1312 	if (get_slot_status(slot) == ACPI_STA_ALL) {
1313 		/* configure all functions */
1314 		retval = enable_device(slot);
1315 		if (retval)
1316 			power_off_slot(slot);
1317 	} else {
1318 		dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1319 		power_off_slot(slot);
1320 	}
1321 
1322  err_exit:
1323 	mutex_unlock(&slot->crit_sect);
1324 	return retval;
1325 }
1326 
1327 /**
1328  * acpiphp_disable_slot - power off slot
1329  * @slot: ACPI PHP slot
1330  */
1331 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1332 {
1333 	int retval = 0;
1334 
1335 	mutex_lock(&slot->crit_sect);
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 
1347  err_exit:
1348 	mutex_unlock(&slot->crit_sect);
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 {
1359 	return (slot->flags & SLOT_POWEREDON);
1360 }
1361 
1362 
1363 /*
1364  * latch   open:  1
1365  * latch closed:  0
1366  */
1367 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1368 {
1369 	unsigned int sta;
1370 
1371 	sta = get_slot_status(slot);
1372 
1373 	return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
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 }
1389