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