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 
59 #define MY_NAME "acpiphp_glue"
60 
61 static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
62 static void acpiphp_sanitize_bus(struct pci_bus *bus);
63 static void acpiphp_set_hpp_values(struct pci_bus *bus);
64 static void hotplug_event_func(acpi_handle handle, u32 type, void *context);
65 static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
66 static void free_bridge(struct kref *kref);
67 
68 /* callback routine to check for the existence of a pci dock device */
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 
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 
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 
139 	if (((buses >> 8) & 0xff) != bus->busn_res.start) {
140 		buses = (buses & 0xff000000)
141 			| ((unsigned int)(bus->primary)     <<  0)
142 			| ((unsigned int)(bus->busn_res.start)   <<  8)
143 			| ((unsigned int)(bus->busn_res.end) << 16);
144 		pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
145 	}
146 	return NOTIFY_OK;
147 }
148 
149 
150 static const struct acpi_dock_ops acpiphp_dock_ops = {
151 	.handler = hotplug_event_func,
152 };
153 
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, &reg32))
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 
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 
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;
204 	acpi_handle tmp;
205 	acpi_status status = AE_OK;
206 	unsigned long long adr, sun;
207 	int device, function, retval, found = 0;
208 	struct pci_bus *pbus = bridge->pci_bus;
209 	struct pci_dev *pdev;
210 	u32 val;
211 
212 	if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
213 		return AE_OK;
214 
215 	status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
216 	if (ACPI_FAILURE(status)) {
217 		warn("can't evaluate _ADR (%#x)\n", status);
218 		return AE_OK;
219 	}
220 
221 	device = (adr >> 16) & 0xffff;
222 	function = adr & 0xffff;
223 
224 	pdev = bridge->pci_dev;
225 	if (pdev && device_is_managed_by_native_pciehp(pdev))
226 		return AE_OK;
227 
228 	newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
229 	if (!newfunc)
230 		return AE_NO_MEMORY;
231 
232 	newfunc->handle = handle;
233 	newfunc->function = function;
234 
235 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
236 		newfunc->flags = FUNC_HAS_EJ0;
237 
238 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
239 		newfunc->flags |= FUNC_HAS_STA;
240 
241 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
242 		newfunc->flags |= FUNC_HAS_PS0;
243 
244 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
245 		newfunc->flags |= FUNC_HAS_PS3;
246 
247 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
248 		newfunc->flags |= FUNC_HAS_DCK;
249 
250 	status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
251 	if (ACPI_FAILURE(status)) {
252 		/*
253 		 * use the count of the number of slots we've found
254 		 * for the number of the slot
255 		 */
256 		sun = bridge->nr_slots+1;
257 	}
258 
259 	/* search for objects that share the same slot */
260 	list_for_each_entry(slot, &bridge->slots, node)
261 		if (slot->device == device) {
262 			if (slot->sun != sun)
263 				warn("sibling found, but _SUN doesn't match!\n");
264 			found = 1;
265 			break;
266 		}
267 
268 	if (!found) {
269 		slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
270 		if (!slot) {
271 			kfree(newfunc);
272 			return AE_NO_MEMORY;
273 		}
274 
275 		slot->bridge = bridge;
276 		slot->device = device;
277 		slot->sun = sun;
278 		INIT_LIST_HEAD(&slot->funcs);
279 		mutex_init(&slot->crit_sect);
280 
281 		mutex_lock(&bridge_mutex);
282 		list_add_tail(&slot->node, &bridge->slots);
283 		mutex_unlock(&bridge_mutex);
284 		bridge->nr_slots++;
285 
286 		dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
287 		    slot->sun, pci_domain_nr(pbus), pbus->number, device);
288 		retval = acpiphp_register_hotplug_slot(slot);
289 		if (retval) {
290 			if (retval == -EBUSY)
291 				warn("Slot %llu already registered by another "
292 					"hotplug driver\n", slot->sun);
293 			else
294 				warn("acpiphp_register_hotplug_slot failed "
295 					"(err code = 0x%x)\n", retval);
296 			goto err_exit;
297 		}
298 	}
299 
300 	newfunc->slot = slot;
301 	mutex_lock(&bridge_mutex);
302 	list_add_tail(&newfunc->sibling, &slot->funcs);
303 	mutex_unlock(&bridge_mutex);
304 
305 	if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
306 				       &val, 60*1000))
307 		slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
308 
309 	if (is_dock_device(handle)) {
310 		/* we don't want to call this device's _EJ0
311 		 * because we want the dock notify handler
312 		 * to call it after it calls _DCK
313 		 */
314 		newfunc->flags &= ~FUNC_HAS_EJ0;
315 		if (register_hotplug_dock_device(handle,
316 			&acpiphp_dock_ops, newfunc,
317 			acpiphp_dock_init, acpiphp_dock_release))
318 			dbg("failed to register dock device\n");
319 
320 		/* we need to be notified when dock events happen
321 		 * outside of the hotplug operation, since we may
322 		 * need to do fixups before we can hotplug.
323 		 */
324 		newfunc->nb.notifier_call = post_dock_fixups;
325 		if (register_dock_notifier(&newfunc->nb))
326 			dbg("failed to register a dock notifier");
327 	}
328 
329 	/* install notify handler */
330 	if (!(newfunc->flags & FUNC_HAS_DCK)) {
331 		status = acpi_install_notify_handler(handle,
332 					     ACPI_SYSTEM_NOTIFY,
333 					     handle_hotplug_event_func,
334 					     newfunc);
335 
336 		if (ACPI_FAILURE(status))
337 			err("failed to register interrupt notify handler\n");
338 	} else
339 		status = AE_OK;
340 
341 	return status;
342 
343  err_exit:
344 	bridge->nr_slots--;
345 	mutex_lock(&bridge_mutex);
346 	list_del(&slot->node);
347 	mutex_unlock(&bridge_mutex);
348 	kfree(slot);
349 	kfree(newfunc);
350 
351 	return AE_OK;
352 }
353 
354 
355 /* see if it's worth looking at this bridge */
356 static int detect_ejectable_slots(acpi_handle handle)
357 {
358 	int found = acpi_pci_detect_ejectable(handle);
359 	if (!found) {
360 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
361 				    is_pci_dock_device, NULL, (void *)&found, NULL);
362 	}
363 	return found;
364 }
365 
366 /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
367 static void init_bridge_misc(struct acpiphp_bridge *bridge)
368 {
369 	acpi_status status;
370 
371 	/* must be added to the list prior to calling register_slot */
372 	mutex_lock(&bridge_mutex);
373 	list_add(&bridge->list, &bridge_list);
374 	mutex_unlock(&bridge_mutex);
375 
376 	/* register all slot objects under this bridge */
377 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
378 				     register_slot, NULL, bridge, NULL);
379 	if (ACPI_FAILURE(status)) {
380 		mutex_lock(&bridge_mutex);
381 		list_del(&bridge->list);
382 		mutex_unlock(&bridge_mutex);
383 		return;
384 	}
385 
386 	/* install notify handler for P2P bridges */
387 	if (!pci_is_root_bus(bridge->pci_bus)) {
388 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
389 			status = acpi_remove_notify_handler(bridge->func->handle,
390 						ACPI_SYSTEM_NOTIFY,
391 						handle_hotplug_event_func);
392 			if (ACPI_FAILURE(status))
393 				err("failed to remove notify handler\n");
394 		}
395 		status = acpi_install_notify_handler(bridge->handle,
396 					     ACPI_SYSTEM_NOTIFY,
397 					     handle_hotplug_event_bridge,
398 					     bridge);
399 
400 		if (ACPI_FAILURE(status)) {
401 			err("failed to register interrupt notify handler\n");
402 		}
403 	}
404 }
405 
406 
407 /* find acpiphp_func from acpiphp_bridge */
408 static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
409 {
410 	struct acpiphp_bridge *bridge;
411 	struct acpiphp_slot *slot;
412 	struct acpiphp_func *func = NULL;
413 
414 	mutex_lock(&bridge_mutex);
415 	list_for_each_entry(bridge, &bridge_list, list) {
416 		list_for_each_entry(slot, &bridge->slots, node) {
417 			list_for_each_entry(func, &slot->funcs, sibling) {
418 				if (func->handle == handle) {
419 					get_bridge(func->slot->bridge);
420 					mutex_unlock(&bridge_mutex);
421 					return func;
422 				}
423 			}
424 		}
425 	}
426 	mutex_unlock(&bridge_mutex);
427 
428 	return NULL;
429 }
430 
431 
432 static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
433 {
434 	struct acpiphp_bridge *bridge;
435 
436 	mutex_lock(&bridge_mutex);
437 	list_for_each_entry(bridge, &bridge_list, list)
438 		if (bridge->handle == handle) {
439 			get_bridge(bridge);
440 			mutex_unlock(&bridge_mutex);
441 			return bridge;
442 		}
443 	mutex_unlock(&bridge_mutex);
444 
445 	return NULL;
446 }
447 
448 static void cleanup_bridge(struct acpiphp_bridge *bridge)
449 {
450 	struct acpiphp_slot *slot;
451 	struct acpiphp_func *func;
452 	acpi_status status;
453 	acpi_handle handle = bridge->handle;
454 
455 	if (!pci_is_root_bus(bridge->pci_bus)) {
456 		status = acpi_remove_notify_handler(handle,
457 					    ACPI_SYSTEM_NOTIFY,
458 					    handle_hotplug_event_bridge);
459 		if (ACPI_FAILURE(status))
460 			err("failed to remove notify handler\n");
461 	}
462 
463 	if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
464 		status = acpi_install_notify_handler(bridge->func->handle,
465 						ACPI_SYSTEM_NOTIFY,
466 						handle_hotplug_event_func,
467 						bridge->func);
468 		if (ACPI_FAILURE(status))
469 			err("failed to install interrupt notify handler\n");
470 	}
471 
472 	list_for_each_entry(slot, &bridge->slots, node) {
473 		list_for_each_entry(func, &slot->funcs, sibling) {
474 			if (is_dock_device(func->handle)) {
475 				unregister_hotplug_dock_device(func->handle);
476 				unregister_dock_notifier(&func->nb);
477 			}
478 			if (!(func->flags & FUNC_HAS_DCK)) {
479 				status = acpi_remove_notify_handler(func->handle,
480 						ACPI_SYSTEM_NOTIFY,
481 						handle_hotplug_event_func);
482 				if (ACPI_FAILURE(status))
483 					err("failed to remove notify handler\n");
484 			}
485 		}
486 		acpiphp_unregister_hotplug_slot(slot);
487 	}
488 
489 	mutex_lock(&bridge_mutex);
490 	list_del(&bridge->list);
491 	mutex_unlock(&bridge_mutex);
492 }
493 
494 static int power_on_slot(struct acpiphp_slot *slot)
495 {
496 	acpi_status status;
497 	struct acpiphp_func *func;
498 	int retval = 0;
499 
500 	/* if already enabled, just skip */
501 	if (slot->flags & SLOT_POWEREDON)
502 		goto err_exit;
503 
504 	list_for_each_entry(func, &slot->funcs, sibling) {
505 		if (func->flags & FUNC_HAS_PS0) {
506 			dbg("%s: executing _PS0\n", __func__);
507 			status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
508 			if (ACPI_FAILURE(status)) {
509 				warn("%s: _PS0 failed\n", __func__);
510 				retval = -1;
511 				goto err_exit;
512 			} else
513 				break;
514 		}
515 	}
516 
517 	/* TBD: evaluate _STA to check if the slot is enabled */
518 
519 	slot->flags |= SLOT_POWEREDON;
520 
521  err_exit:
522 	return retval;
523 }
524 
525 
526 static int power_off_slot(struct acpiphp_slot *slot)
527 {
528 	acpi_status status;
529 	struct acpiphp_func *func;
530 
531 	int retval = 0;
532 
533 	/* if already disabled, just skip */
534 	if ((slot->flags & SLOT_POWEREDON) == 0)
535 		goto err_exit;
536 
537 	list_for_each_entry(func, &slot->funcs, sibling) {
538 		if (func->flags & FUNC_HAS_PS3) {
539 			status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
540 			if (ACPI_FAILURE(status)) {
541 				warn("%s: _PS3 failed\n", __func__);
542 				retval = -1;
543 				goto err_exit;
544 			} else
545 				break;
546 		}
547 	}
548 
549 	/* TBD: evaluate _STA to check if the slot is disabled */
550 
551 	slot->flags &= (~SLOT_POWEREDON);
552 
553  err_exit:
554 	return retval;
555 }
556 
557 
558 
559 /**
560  * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
561  * @bus: bus to start search with
562  */
563 static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
564 {
565 	struct list_head *tmp;
566 	unsigned char max, n;
567 
568 	/*
569 	 * pci_bus_max_busnr will return the highest
570 	 * reserved busnr for all these children.
571 	 * that is equivalent to the bus->subordinate
572 	 * value.  We don't want to use the parent's
573 	 * bus->subordinate value because it could have
574 	 * padding in it.
575 	 */
576 	max = bus->busn_res.start;
577 
578 	list_for_each(tmp, &bus->children) {
579 		n = pci_bus_max_busnr(pci_bus_b(tmp));
580 		if (n > max)
581 			max = n;
582 	}
583 	return max;
584 }
585 
586 
587 /**
588  * acpiphp_bus_add - add a new bus to acpi subsystem
589  * @func: acpiphp_func of the bridge
590  */
591 static int acpiphp_bus_add(struct acpiphp_func *func)
592 {
593 	struct acpi_device *device;
594 	int ret_val;
595 
596 	if (!acpi_bus_get_device(func->handle, &device)) {
597 		dbg("bus exists... trim\n");
598 		/* this shouldn't be in here, so remove
599 		 * the bus then re-add it...
600 		 */
601 		acpi_bus_trim(device);
602 	}
603 
604 	ret_val = acpi_bus_scan(func->handle);
605 	if (!ret_val)
606 		ret_val = acpi_bus_get_device(func->handle, &device);
607 
608 	if (ret_val)
609 		dbg("error adding bus, %x\n", -ret_val);
610 
611 	return ret_val;
612 }
613 
614 
615 /**
616  * acpiphp_bus_trim - trim a bus from acpi subsystem
617  * @handle: handle to acpi namespace
618  */
619 static int acpiphp_bus_trim(acpi_handle handle)
620 {
621 	struct acpi_device *device;
622 	int retval;
623 
624 	retval = acpi_bus_get_device(handle, &device);
625 	if (retval) {
626 		dbg("acpi_device not found\n");
627 		return retval;
628 	}
629 
630 	acpi_bus_trim(device);
631 	return 0;
632 }
633 
634 static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
635 {
636 	struct acpiphp_func *func;
637 	union acpi_object params[2];
638 	struct acpi_object_list arg_list;
639 
640 	list_for_each_entry(func, &slot->funcs, sibling) {
641 		arg_list.count = 2;
642 		arg_list.pointer = params;
643 		params[0].type = ACPI_TYPE_INTEGER;
644 		params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
645 		params[1].type = ACPI_TYPE_INTEGER;
646 		params[1].integer.value = 1;
647 		/* _REG is optional, we don't care about if there is failure */
648 		acpi_evaluate_object(func->handle, "_REG", &arg_list, NULL);
649 	}
650 }
651 
652 static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
653 {
654 	struct acpiphp_func *func;
655 
656 	if (!dev->subordinate)
657 		return;
658 
659 	/* quirk, or pcie could set it already */
660 	if (dev->is_hotplug_bridge)
661 		return;
662 
663 	if (PCI_SLOT(dev->devfn) != slot->device)
664 		return;
665 
666 	list_for_each_entry(func, &slot->funcs, sibling) {
667 		if (PCI_FUNC(dev->devfn) == func->function) {
668 			/* check if this bridge has ejectable slots */
669 			if ((detect_ejectable_slots(func->handle) > 0))
670 				dev->is_hotplug_bridge = 1;
671 			break;
672 		}
673 	}
674 }
675 
676 /**
677  * enable_device - enable, configure a slot
678  * @slot: slot to be enabled
679  *
680  * This function should be called per *physical slot*,
681  * not per each slot object in ACPI namespace.
682  */
683 static int __ref enable_device(struct acpiphp_slot *slot)
684 {
685 	struct pci_dev *dev;
686 	struct pci_bus *bus = slot->bridge->pci_bus;
687 	struct acpiphp_func *func;
688 	int num, max, pass;
689 	LIST_HEAD(add_list);
690 
691 	if (slot->flags & SLOT_ENABLED)
692 		goto err_exit;
693 
694 	list_for_each_entry(func, &slot->funcs, sibling)
695 		acpiphp_bus_add(func);
696 
697 	num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
698 	if (num == 0) {
699 		/* Maybe only part of funcs are added. */
700 		dbg("No new device found\n");
701 		goto err_exit;
702 	}
703 
704 	max = acpiphp_max_busnr(bus);
705 	for (pass = 0; pass < 2; pass++) {
706 		list_for_each_entry(dev, &bus->devices, bus_list) {
707 			if (PCI_SLOT(dev->devfn) != slot->device)
708 				continue;
709 			if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
710 			    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
711 				max = pci_scan_bridge(bus, dev, max, pass);
712 				if (pass && dev->subordinate) {
713 					check_hotplug_bridge(slot, dev);
714 					pcibios_resource_survey_bus(dev->subordinate);
715 					__pci_bus_size_bridges(dev->subordinate,
716 							       &add_list);
717 				}
718 			}
719 		}
720 	}
721 
722 	__pci_bus_assign_resources(bus, &add_list, NULL);
723 	acpiphp_sanitize_bus(bus);
724 	acpiphp_set_hpp_values(bus);
725 	acpiphp_set_acpi_region(slot);
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 
733 	pci_bus_add_devices(bus);
734 
735 	slot->flags |= SLOT_ENABLED;
736 	list_for_each_entry(func, &slot->funcs, sibling) {
737 		dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
738 						  func->function));
739 		if (!dev) {
740 			/* Do not set SLOT_ENABLED flag if some funcs
741 			   are not added. */
742 			slot->flags &= (~SLOT_ENABLED);
743 			continue;
744 		}
745 	}
746 
747 
748  err_exit:
749 	return 0;
750 }
751 
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 
770 /**
771  * disable_device - disable a slot
772  * @slot: ACPI PHP slot
773  */
774 static int disable_device(struct acpiphp_slot *slot)
775 {
776 	struct acpiphp_func *func;
777 	struct pci_dev *pdev;
778 
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))) {
786 		pci_stop_and_remove_bus_device(pdev);
787 		pci_dev_put(pdev);
788 	}
789 
790 	list_for_each_entry(func, &slot->funcs, sibling) {
791 		acpiphp_bus_trim(func->handle);
792 	}
793 
794 	slot->flags &= (~SLOT_ENABLED);
795 
796 	return 0;
797 }
798 
799 
800 /**
801  * get_slot_status - get ACPI slot status
802  * @slot: ACPI PHP slot
803  *
804  * If a slot has _STA for each function and if any one of them
805  * returned non-zero status, return it.
806  *
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.
809  *
810  * Otherwise return 0.
811  */
812 static unsigned int get_slot_status(struct acpiphp_slot *slot)
813 {
814 	acpi_status status;
815 	unsigned long long sta = 0;
816 	u32 dvid;
817 	struct acpiphp_func *func;
818 
819 	list_for_each_entry(func, &slot->funcs, sibling) {
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 
839 /**
840  * acpiphp_eject_slot - physically eject the slot
841  * @slot: ACPI PHP slot
842  */
843 int acpiphp_eject_slot(struct acpiphp_slot *slot)
844 {
845 	acpi_status status;
846 	struct acpiphp_func *func;
847 	struct acpi_object_list arg_list;
848 	union acpi_object arg;
849 
850 	list_for_each_entry(func, &slot->funcs, sibling) {
851 		/* We don't want to call _EJ0 on non-existing functions. */
852 		if ((func->flags & FUNC_HAS_EJ0)) {
853 			/* _EJ0 method take one argument */
854 			arg_list.count = 1;
855 			arg_list.pointer = &arg;
856 			arg.type = ACPI_TYPE_INTEGER;
857 			arg.integer.value = 1;
858 
859 			status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
860 			if (ACPI_FAILURE(status)) {
861 				warn("%s: _EJ0 failed\n", __func__);
862 				return -1;
863 			} else
864 				break;
865 		}
866 	}
867 	return 0;
868 }
869 
870 /**
871  * acpiphp_check_bridge - re-enumerate devices
872  * @bridge: where to begin re-enumeration
873  *
874  * Iterate over all slots under this bridge and make sure that if a
875  * card is present they are enabled, and if not they are disabled.
876  */
877 static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
878 {
879 	struct acpiphp_slot *slot;
880 	int retval = 0;
881 	int enabled, disabled;
882 
883 	enabled = disabled = 0;
884 
885 	list_for_each_entry(slot, &bridge->slots, node) {
886 		unsigned int status = get_slot_status(slot);
887 		if (slot->flags & SLOT_ENABLED) {
888 			if (status == ACPI_STA_ALL)
889 				continue;
890 			retval = acpiphp_disable_slot(slot);
891 			if (retval) {
892 				err("Error occurred in disabling\n");
893 				goto err_exit;
894 			} else {
895 				acpiphp_eject_slot(slot);
896 			}
897 			disabled++;
898 		} else {
899 			if (status != ACPI_STA_ALL)
900 				continue;
901 			retval = acpiphp_enable_slot(slot);
902 			if (retval) {
903 				err("Error occurred in enabling\n");
904 				goto err_exit;
905 			}
906 			enabled++;
907 		}
908 	}
909 
910 	dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
911 
912  err_exit:
913 	return retval;
914 }
915 
916 static void acpiphp_set_hpp_values(struct pci_bus *bus)
917 {
918 	struct pci_dev *dev;
919 
920 	list_for_each_entry(dev, &bus->devices, bus_list)
921 		pci_configure_slot(dev);
922 }
923 
924 /*
925  * Remove devices for which we could not assign resources, call
926  * arch specific code to fix-up the bus
927  */
928 static void acpiphp_sanitize_bus(struct pci_bus *bus)
929 {
930 	struct pci_dev *dev, *tmp;
931 	int i;
932 	unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
933 
934 	list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
935 		for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
936 			struct resource *res = &dev->resource[i];
937 			if ((res->flags & type_mask) && !res->start &&
938 					res->end) {
939 				/* Could not assign a required resources
940 				 * for this device, remove it */
941 				pci_stop_and_remove_bus_device(dev);
942 				break;
943 			}
944 		}
945 	}
946 }
947 
948 /*
949  * ACPI event handlers
950  */
951 
952 static acpi_status
953 check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
954 {
955 	struct acpiphp_bridge *bridge;
956 	char objname[64];
957 	struct acpi_buffer buffer = { .length = sizeof(objname),
958 				      .pointer = objname };
959 
960 	bridge = acpiphp_handle_to_bridge(handle);
961 	if (bridge) {
962 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
963 		dbg("%s: re-enumerating slots under %s\n",
964 			__func__, objname);
965 		acpiphp_check_bridge(bridge);
966 		put_bridge(bridge);
967 	}
968 	return AE_OK ;
969 }
970 
971 void acpiphp_check_host_bridge(acpi_handle handle)
972 {
973 	struct acpiphp_bridge *bridge;
974 
975 	bridge = acpiphp_handle_to_bridge(handle);
976 	if (bridge) {
977 		acpiphp_check_bridge(bridge);
978 		put_bridge(bridge);
979 	}
980 
981 	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
982 		ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
983 }
984 
985 static void _handle_hotplug_event_bridge(struct work_struct *work)
986 {
987 	struct acpiphp_bridge *bridge;
988 	char objname[64];
989 	struct acpi_buffer buffer = { .length = sizeof(objname),
990 				      .pointer = objname };
991 	struct acpi_hp_work *hp_work;
992 	acpi_handle handle;
993 	u32 type;
994 
995 	hp_work = container_of(work, struct acpi_hp_work, work);
996 	handle = hp_work->handle;
997 	type = hp_work->type;
998 	bridge = (struct acpiphp_bridge *)hp_work->context;
999 
1000 	acpi_scan_lock_acquire();
1001 
1002 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1003 
1004 	switch (type) {
1005 	case ACPI_NOTIFY_BUS_CHECK:
1006 		/* bus re-enumerate */
1007 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1008 		dbg("%s: re-enumerating slots under %s\n", __func__, objname);
1009 		acpiphp_check_bridge(bridge);
1010 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1011 			ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
1012 		break;
1013 
1014 	case ACPI_NOTIFY_DEVICE_CHECK:
1015 		/* device check */
1016 		dbg("%s: Device check notify on %s\n", __func__, objname);
1017 		acpiphp_check_bridge(bridge);
1018 		break;
1019 
1020 	case ACPI_NOTIFY_DEVICE_WAKE:
1021 		/* wake event */
1022 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1023 		break;
1024 
1025 	case ACPI_NOTIFY_EJECT_REQUEST:
1026 		/* request device eject */
1027 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1028 		if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
1029 			struct acpiphp_slot *slot;
1030 			slot = bridge->func->slot;
1031 			if (!acpiphp_disable_slot(slot))
1032 				acpiphp_eject_slot(slot);
1033 		}
1034 		break;
1035 
1036 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
1037 		printk(KERN_ERR "Device %s cannot be configured due"
1038 				" to a frequency mismatch\n", objname);
1039 		break;
1040 
1041 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
1042 		printk(KERN_ERR "Device %s cannot be configured due"
1043 				" to a bus mode mismatch\n", objname);
1044 		break;
1045 
1046 	case ACPI_NOTIFY_POWER_FAULT:
1047 		printk(KERN_ERR "Device %s has suffered a power fault\n",
1048 				objname);
1049 		break;
1050 
1051 	default:
1052 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1053 		break;
1054 	}
1055 
1056 	acpi_scan_lock_release();
1057 	kfree(hp_work); /* allocated in handle_hotplug_event_bridge */
1058 	put_bridge(bridge);
1059 }
1060 
1061 /**
1062  * handle_hotplug_event_bridge - handle ACPI event on bridges
1063  * @handle: Notify()'ed acpi_handle
1064  * @type: Notify code
1065  * @context: pointer to acpiphp_bridge structure
1066  *
1067  * Handles ACPI event notification on {host,p2p} bridges.
1068  */
1069 static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
1070 					void *context)
1071 {
1072 	struct acpiphp_bridge *bridge = context;
1073 
1074 	/*
1075 	 * Currently the code adds all hotplug events to the kacpid_wq
1076 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1077 	 * The proper way to fix this is to reorganize the code so that
1078 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1079 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1080 	 * don't deadlock on hotplug actions.
1081 	 */
1082 	get_bridge(bridge);
1083 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_bridge);
1084 }
1085 
1086 static void hotplug_event_func(acpi_handle handle, u32 type, void *context)
1087 {
1088 	struct acpiphp_func *func = context;
1089 	char objname[64];
1090 	struct acpi_buffer buffer = { .length = sizeof(objname),
1091 				      .pointer = objname };
1092 
1093 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1094 
1095 	switch (type) {
1096 	case ACPI_NOTIFY_BUS_CHECK:
1097 		/* bus re-enumerate */
1098 		dbg("%s: Bus check notify on %s\n", __func__, objname);
1099 		acpiphp_enable_slot(func->slot);
1100 		break;
1101 
1102 	case ACPI_NOTIFY_DEVICE_CHECK:
1103 		/* device check : re-enumerate from parent bus */
1104 		dbg("%s: Device check notify on %s\n", __func__, objname);
1105 		acpiphp_check_bridge(func->slot->bridge);
1106 		break;
1107 
1108 	case ACPI_NOTIFY_DEVICE_WAKE:
1109 		/* wake event */
1110 		dbg("%s: Device wake notify on %s\n", __func__, objname);
1111 		break;
1112 
1113 	case ACPI_NOTIFY_EJECT_REQUEST:
1114 		/* request device eject */
1115 		dbg("%s: Device eject notify on %s\n", __func__, objname);
1116 		if (!(acpiphp_disable_slot(func->slot)))
1117 			acpiphp_eject_slot(func->slot);
1118 		break;
1119 
1120 	default:
1121 		warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
1122 		break;
1123 	}
1124 }
1125 
1126 static void _handle_hotplug_event_func(struct work_struct *work)
1127 {
1128 	struct acpi_hp_work *hp_work;
1129 	struct acpiphp_func *func;
1130 
1131 	hp_work = container_of(work, struct acpi_hp_work, work);
1132 	func = hp_work->context;
1133 	acpi_scan_lock_acquire();
1134 
1135 	hotplug_event_func(hp_work->handle, hp_work->type, func);
1136 
1137 	acpi_scan_lock_release();
1138 	kfree(hp_work); /* allocated in handle_hotplug_event_func */
1139 	put_bridge(func->slot->bridge);
1140 }
1141 
1142 /**
1143  * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
1144  * @handle: Notify()'ed acpi_handle
1145  * @type: Notify code
1146  * @context: pointer to acpiphp_func structure
1147  *
1148  * Handles ACPI event notification on slots.
1149  */
1150 static void handle_hotplug_event_func(acpi_handle handle, u32 type,
1151 				      void *context)
1152 {
1153 	struct acpiphp_func *func = context;
1154 
1155 	/*
1156 	 * Currently the code adds all hotplug events to the kacpid_wq
1157 	 * queue when it should add hotplug events to the kacpi_hotplug_wq.
1158 	 * The proper way to fix this is to reorganize the code so that
1159 	 * drivers (dock, etc.) do not call acpi_os_execute(), etc.
1160 	 * For now just re-add this work to the kacpi_hotplug_wq so we
1161 	 * don't deadlock on hotplug actions.
1162 	 */
1163 	get_bridge(func->slot->bridge);
1164 	alloc_acpi_hp_work(handle, type, context, _handle_hotplug_event_func);
1165 }
1166 
1167 /*
1168  * Create hotplug slots for the PCI bus.
1169  * It should always return 0 to avoid skipping following notifiers.
1170  */
1171 void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
1172 {
1173 	acpi_handle dummy_handle;
1174 	struct acpiphp_bridge *bridge;
1175 
1176 	if (acpiphp_disabled)
1177 		return;
1178 
1179 	if (detect_ejectable_slots(handle) <= 0)
1180 		return;
1181 
1182 	bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
1183 	if (bridge == NULL) {
1184 		err("out of memory\n");
1185 		return;
1186 	}
1187 
1188 	INIT_LIST_HEAD(&bridge->slots);
1189 	kref_init(&bridge->ref);
1190 	bridge->handle = handle;
1191 	bridge->pci_dev = pci_dev_get(bus->self);
1192 	bridge->pci_bus = bus;
1193 
1194 	/*
1195 	 * Grab a ref to the subordinate PCI bus in case the bus is
1196 	 * removed via PCI core logical hotplug. The ref pins the bus
1197 	 * (which we access during module unload).
1198 	 */
1199 	get_device(&bus->dev);
1200 
1201 	if (!pci_is_root_bus(bridge->pci_bus) &&
1202 	    ACPI_SUCCESS(acpi_get_handle(bridge->handle,
1203 					"_EJ0", &dummy_handle))) {
1204 		dbg("found ejectable p2p bridge\n");
1205 		bridge->flags |= BRIDGE_HAS_EJ0;
1206 		bridge->func = acpiphp_bridge_handle_to_function(handle);
1207 	}
1208 
1209 	init_bridge_misc(bridge);
1210 }
1211 
1212 /* Destroy hotplug slots associated with the PCI bus */
1213 void acpiphp_remove_slots(struct pci_bus *bus)
1214 {
1215 	struct acpiphp_bridge *bridge, *tmp;
1216 
1217 	if (acpiphp_disabled)
1218 		return;
1219 
1220 	list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
1221 		if (bridge->pci_bus == bus) {
1222 			cleanup_bridge(bridge);
1223 			put_bridge(bridge);
1224 			break;
1225 		}
1226 }
1227 
1228 /**
1229  * acpiphp_enable_slot - power on slot
1230  * @slot: ACPI PHP slot
1231  */
1232 int acpiphp_enable_slot(struct acpiphp_slot *slot)
1233 {
1234 	int retval;
1235 
1236 	mutex_lock(&slot->crit_sect);
1237 
1238 	/* wake up all functions */
1239 	retval = power_on_slot(slot);
1240 	if (retval)
1241 		goto err_exit;
1242 
1243 	if (get_slot_status(slot) == ACPI_STA_ALL) {
1244 		/* configure all functions */
1245 		retval = enable_device(slot);
1246 		if (retval)
1247 			power_off_slot(slot);
1248 	} else {
1249 		dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
1250 		power_off_slot(slot);
1251 	}
1252 
1253  err_exit:
1254 	mutex_unlock(&slot->crit_sect);
1255 	return retval;
1256 }
1257 
1258 /**
1259  * acpiphp_disable_slot - power off slot
1260  * @slot: ACPI PHP slot
1261  */
1262 int acpiphp_disable_slot(struct acpiphp_slot *slot)
1263 {
1264 	int retval = 0;
1265 
1266 	mutex_lock(&slot->crit_sect);
1267 
1268 	/* unconfigure all functions */
1269 	retval = disable_device(slot);
1270 	if (retval)
1271 		goto err_exit;
1272 
1273 	/* power off all functions */
1274 	retval = power_off_slot(slot);
1275 	if (retval)
1276 		goto err_exit;
1277 
1278  err_exit:
1279 	mutex_unlock(&slot->crit_sect);
1280 	return retval;
1281 }
1282 
1283 
1284 /*
1285  * slot enabled:  1
1286  * slot disabled: 0
1287  */
1288 u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
1289 {
1290 	return (slot->flags & SLOT_POWEREDON);
1291 }
1292 
1293 
1294 /*
1295  * latch   open:  1
1296  * latch closed:  0
1297  */
1298 u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
1299 {
1300 	unsigned int sta;
1301 
1302 	sta = get_slot_status(slot);
1303 
1304 	return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
1305 }
1306 
1307 
1308 /*
1309  * adapter presence : 1
1310  *          absence : 0
1311  */
1312 u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
1313 {
1314 	unsigned int sta;
1315 
1316 	sta = get_slot_status(slot);
1317 
1318 	return (sta == 0) ? 0 : 1;
1319 }
1320