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