xref: /openbmc/linux/drivers/pci/hotplug/rpaphp_pci.c (revision eca845c7)
1 /*
2  * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3  * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15  * NON INFRINGEMENT.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * Send feedback to <lxie@us.ibm.com>
23  *
24  */
25 #include <linux/pci.h>
26 #include <linux/string.h>
27 
28 #include <asm/pci-bridge.h>
29 #include <asm/rtas.h>
30 #include <asm/machdep.h>
31 
32 #include "../pci.h"		/* for pci_add_new_bus */
33 #include "rpaphp.h"
34 
35 static int rpaphp_get_sensor_state(struct slot *slot, int *state)
36 {
37 	int rc;
38 	int setlevel;
39 
40 	rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
41 
42 	if (rc < 0) {
43 		if (rc == -EFAULT || rc == -EEXIST) {
44 			dbg("%s: slot must be power up to get sensor-state\n",
45 			    __FUNCTION__);
46 
47 			/* some slots have to be powered up
48 			 * before get-sensor will succeed.
49 			 */
50 			rc = rtas_set_power_level(slot->power_domain, POWER_ON,
51 						  &setlevel);
52 			if (rc < 0) {
53 				dbg("%s: power on slot[%s] failed rc=%d.\n",
54 				    __FUNCTION__, slot->name, rc);
55 			} else {
56 				rc = rtas_get_sensor(DR_ENTITY_SENSE,
57 						     slot->index, state);
58 			}
59 		} else if (rc == -ENODEV)
60 			info("%s: slot is unusable\n", __FUNCTION__);
61 		else
62 			err("%s failed to get sensor state\n", __FUNCTION__);
63 	}
64 	return rc;
65 }
66 
67 /**
68  * get_pci_adapter_status - get the status of a slot
69  *
70  * 0-- slot is empty
71  * 1-- adapter is configured
72  * 2-- adapter is not configured
73  * 3-- not valid
74  */
75 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
76 {
77 	struct pci_bus *bus;
78 	int state, rc;
79 
80 	*value = NOT_VALID;
81 	rc = rpaphp_get_sensor_state(slot, &state);
82 	if (rc)
83 		goto exit;
84 
85  	if (state == EMPTY)
86  		*value = EMPTY;
87  	else if (state == PRESENT) {
88 		if (!is_init) {
89 			/* at run-time slot->state can be changed by */
90 			/* config/unconfig adapter */
91 			*value = slot->state;
92 		} else {
93 			bus = pcibios_find_pci_bus(slot->dn);
94 			if (bus && !list_empty(&bus->devices))
95 				*value = CONFIGURED;
96 			else
97 				*value = NOT_CONFIGURED;
98 		}
99 	}
100 exit:
101 	return rc;
102 }
103 
104 static void print_slot_pci_funcs(struct pci_bus *bus)
105 {
106 	struct device_node *dn;
107 	struct pci_dev *dev;
108 
109 	dn = pci_bus_to_OF_node(bus);
110 	if (!dn)
111 		return;
112 
113 	dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
114 	list_for_each_entry (dev, &bus->devices, bus_list)
115 		dbg("\t%s\n", pci_name(dev));
116 	return;
117 }
118 
119 int rpaphp_config_pci_adapter(struct pci_bus *bus)
120 {
121 	struct device_node *dn = pci_bus_to_OF_node(bus);
122 	int rc = -ENODEV;
123 
124 	dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
125 	if (!dn)
126 		goto exit;
127 
128 	pcibios_add_pci_devices(bus);
129 	print_slot_pci_funcs(bus);
130 	rc = 0;
131 exit:
132 	dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
133 	return rc;
134 }
135 EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
136 
137 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
138 {
139 	eeh_remove_device(dev);
140 	if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
141 		struct pci_bus *bus = dev->subordinate;
142 		struct list_head *ln;
143 		if (!bus)
144 			return;
145 		for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
146 			struct pci_dev *pdev = pci_dev_b(ln);
147 			if (pdev)
148 				rpaphp_eeh_remove_bus_device(pdev);
149 		}
150 
151 	}
152 	return;
153 }
154 
155 int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
156 {
157 	struct pci_dev *dev, *tmp;
158 
159 	list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
160 		rpaphp_eeh_remove_bus_device(dev);
161 		pci_remove_bus_device(dev);
162 	}
163 	return 0;
164 }
165 EXPORT_SYMBOL_GPL(rpaphp_unconfig_pci_adapter);
166 
167 static int setup_pci_hotplug_slot_info(struct slot *slot)
168 {
169 	struct hotplug_slot_info *hotplug_slot_info = slot->hotplug_slot->info;
170 
171 	dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
172 	    __FUNCTION__);
173 	rpaphp_get_power_status(slot, &hotplug_slot_info->power_status);
174 	rpaphp_get_pci_adapter_status(slot, 1,
175 				      &hotplug_slot_info->adapter_status);
176 	if (hotplug_slot_info->adapter_status == NOT_VALID) {
177 		err("%s: NOT_VALID: skip dn->full_name=%s\n",
178 		    __FUNCTION__, slot->dn->full_name);
179 		return -EINVAL;
180 	}
181 	return 0;
182 }
183 
184 static void set_slot_name(struct slot *slot)
185 {
186 	struct pci_bus *bus = slot->bus;
187 	struct pci_dev *bridge;
188 
189 	bridge = bus->self;
190 	if (bridge)
191 		strcpy(slot->name, pci_name(bridge));
192 	else
193 		sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
194 			bus->number);
195 }
196 
197 static int setup_pci_slot(struct slot *slot)
198 {
199 	struct device_node *dn = slot->dn;
200 	struct pci_bus *bus;
201 
202 	BUG_ON(!dn);
203 	bus = pcibios_find_pci_bus(dn);
204 	if (!bus) {
205 		err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
206 		goto exit_rc;
207 	}
208 
209 	slot->bus = bus;
210 	slot->pci_devs = &bus->devices;
211 	set_slot_name(slot);
212 
213 	/* find slot's pci_dev if it's not empty */
214 	if (slot->hotplug_slot->info->adapter_status == EMPTY) {
215 		slot->state = EMPTY;	/* slot is empty */
216 	} else {
217 		/* slot is occupied */
218 		if (!dn->child) {
219 			/* non-empty slot has to have child */
220 			err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
221 				__FUNCTION__, slot->name);
222 			goto exit_rc;
223 		}
224 
225 		if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
226 			dbg("%s CONFIGURING pci adapter in slot[%s]\n",
227 				__FUNCTION__, slot->name);
228 			if (rpaphp_config_pci_adapter(slot->bus)) {
229 				err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
230 				goto exit_rc;
231 			}
232 
233 		} else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
234 			err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
235 				__FUNCTION__, slot->name);
236 			goto exit_rc;
237 		}
238 		print_slot_pci_funcs(slot->bus);
239 		if (!list_empty(slot->pci_devs)) {
240 			slot->state = CONFIGURED;
241 		} else {
242 			/* DLPAR add as opposed to
243 		 	 * boot time */
244 			slot->state = NOT_CONFIGURED;
245 		}
246 	}
247 	return 0;
248 exit_rc:
249 	dealloc_slot_struct(slot);
250 	return -EINVAL;
251 }
252 
253 int register_pci_slot(struct slot *slot)
254 {
255 	int rc = -EINVAL;
256 
257 	if (setup_pci_hotplug_slot_info(slot))
258 		goto exit_rc;
259 	if (setup_pci_slot(slot))
260 		goto exit_rc;
261 	rc = register_slot(slot);
262 exit_rc:
263 	return rc;
264 }
265 
266 int rpaphp_enable_pci_slot(struct slot *slot)
267 {
268 	int retval = 0, state;
269 
270 	retval = rpaphp_get_sensor_state(slot, &state);
271 	if (retval)
272 		goto exit;
273 	dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
274 	/* if slot is not empty, enable the adapter */
275 	if (state == PRESENT) {
276 		dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
277 		retval = rpaphp_config_pci_adapter(slot->bus);
278 		if (!retval) {
279 			slot->state = CONFIGURED;
280 			info("%s: devices in slot[%s] configured\n",
281 					__FUNCTION__, slot->name);
282 		} else {
283 			slot->state = NOT_CONFIGURED;
284 			dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
285 			    __FUNCTION__, slot->name);
286 		}
287 	} else if (state == EMPTY) {
288 		dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
289 		slot->state = EMPTY;
290 	} else {
291 		err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
292 		    slot->name);
293 		slot->state = NOT_VALID;
294 		retval = -EINVAL;
295 	}
296 exit:
297 	dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
298 	return retval;
299 }
300