1/*
2 * PCI Dynamic LPAR, PCI Hot Plug and PCI EEH recovery code
3 * for RPA-compliant PPC64 platform.
4 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
5 * Copyright (C) 2005 International Business Machines
6 *
7 * Updates, 2005, John Rose <johnrose@austin.ibm.com>
8 * Updates, 2005, Linas Vepstas <linas@austin.ibm.com>
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
20 * NON INFRINGEMENT.  See the GNU General Public License for more
21 * details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/pci.h>
29#include <linux/export.h>
30#include <asm/pci-bridge.h>
31#include <asm/ppc-pci.h>
32#include <asm/firmware.h>
33#include <asm/eeh.h>
34
35#include "pseries.h"
36
37static struct pci_bus *
38find_bus_among_children(struct pci_bus *bus,
39                        struct device_node *dn)
40{
41	struct pci_bus *child = NULL;
42	struct pci_bus *tmp;
43	struct device_node *busdn;
44
45	busdn = pci_bus_to_OF_node(bus);
46	if (busdn == dn)
47		return bus;
48
49	list_for_each_entry(tmp, &bus->children, node) {
50		child = find_bus_among_children(tmp, dn);
51		if (child)
52			break;
53	};
54	return child;
55}
56
57struct pci_bus *
58pcibios_find_pci_bus(struct device_node *dn)
59{
60	struct pci_dn *pdn = dn->data;
61
62	if (!pdn  || !pdn->phb || !pdn->phb->bus)
63		return NULL;
64
65	return find_bus_among_children(pdn->phb->bus, dn);
66}
67EXPORT_SYMBOL_GPL(pcibios_find_pci_bus);
68
69struct pci_controller *init_phb_dynamic(struct device_node *dn)
70{
71	struct pci_controller *phb;
72
73	pr_debug("PCI: Initializing new hotplug PHB %s\n", dn->full_name);
74
75	phb = pcibios_alloc_controller(dn);
76	if (!phb)
77		return NULL;
78	rtas_setup_phb(phb);
79	pci_process_bridge_OF_ranges(phb, dn, 0);
80	phb->controller_ops = pseries_pci_controller_ops;
81
82	pci_devs_phb_init_dynamic(phb);
83
84	/* Create EEH devices for the PHB */
85	eeh_dev_phb_init_dynamic(phb);
86
87	if (dn->child)
88		eeh_add_device_tree_early(PCI_DN(dn));
89
90	pcibios_scan_phb(phb);
91	pcibios_finish_adding_to_bus(phb->bus);
92
93	return phb;
94}
95EXPORT_SYMBOL_GPL(init_phb_dynamic);
96
97/* RPA-specific bits for removing PHBs */
98int remove_phb_dynamic(struct pci_controller *phb)
99{
100	struct pci_bus *b = phb->bus;
101	struct resource *res;
102	int rc, i;
103
104	pr_debug("PCI: Removing PHB %04x:%02x...\n",
105		 pci_domain_nr(b), b->number);
106
107	/* We cannot to remove a root bus that has children */
108	if (!(list_empty(&b->children) && list_empty(&b->devices)))
109		return -EBUSY;
110
111	/* We -know- there aren't any child devices anymore at this stage
112	 * and thus, we can safely unmap the IO space as it's not in use
113	 */
114	res = &phb->io_resource;
115	if (res->flags & IORESOURCE_IO) {
116		rc = pcibios_unmap_io_space(b);
117		if (rc) {
118			printk(KERN_ERR "%s: failed to unmap IO on bus %s\n",
119			       __func__, b->name);
120			return 1;
121		}
122	}
123
124	/* Remove the PCI bus and unregister the bridge device from sysfs */
125	phb->bus = NULL;
126	pci_remove_bus(b);
127	device_unregister(b->bridge);
128
129	/* Now release the IO resource */
130	if (res->flags & IORESOURCE_IO)
131		release_resource(res);
132
133	/* Release memory resources */
134	for (i = 0; i < 3; ++i) {
135		res = &phb->mem_resources[i];
136		if (!(res->flags & IORESOURCE_MEM))
137			continue;
138		release_resource(res);
139	}
140
141	/* Free pci_controller data structure */
142	pcibios_free_controller(phb);
143
144	return 0;
145}
146EXPORT_SYMBOL_GPL(remove_phb_dynamic);
147