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