xref: /linux/drivers/pci/hotplug/rpadlpar_core.c (revision 55f3538c4923e9dfca132e99ebec370e8094afda)
1 /*
2  * Interface for Dynamic Logical Partitioning of I/O Slots on
3  * RPA-compliant PPC64 platform.
4  *
5  * John Rose <johnrose@austin.ibm.com>
6  * Linda Xie <lxie@us.ibm.com>
7  *
8  * October 2003
9  *
10  * Copyright (C) 2003 IBM.
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17 
18 #undef DEBUG
19 
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25 
26 #include <asm/pci-bridge.h>
27 #include <linux/mutex.h>
28 #include <asm/rtas.h>
29 #include <asm/vio.h>
30 #include <linux/firmware.h>
31 
32 #include "../pci.h"
33 #include "rpaphp.h"
34 #include "rpadlpar.h"
35 
36 static DEFINE_MUTEX(rpadlpar_mutex);
37 
38 #define DLPAR_MODULE_NAME "rpadlpar_io"
39 
40 #define NODE_TYPE_VIO  1
41 #define NODE_TYPE_SLOT 2
42 #define NODE_TYPE_PHB  3
43 
44 static struct device_node *find_vio_slot_node(char *drc_name)
45 {
46 	struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
47 	struct device_node *dn = NULL;
48 	int rc;
49 
50 	if (!parent)
51 		return NULL;
52 
53 	while ((dn = of_get_next_child(parent, dn))) {
54 		rc = rpaphp_check_drc_props(dn, drc_name, NULL);
55 		if (rc == 0)
56 			break;
57 	}
58 
59 	return dn;
60 }
61 
62 /* Find dlpar-capable pci node that contains the specified name and type */
63 static struct device_node *find_php_slot_pci_node(char *drc_name,
64 						  char *drc_type)
65 {
66 	struct device_node *np = NULL;
67 	int rc;
68 
69 	while ((np = of_find_node_by_name(np, "pci"))) {
70 		rc = rpaphp_check_drc_props(np, drc_name, drc_type);
71 		if (rc == 0)
72 			break;
73 	}
74 
75 	return np;
76 }
77 
78 static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
79 {
80 	struct device_node *dn;
81 
82 	dn = find_php_slot_pci_node(drc_name, "SLOT");
83 	if (dn) {
84 		*node_type = NODE_TYPE_SLOT;
85 		return dn;
86 	}
87 
88 	dn = find_php_slot_pci_node(drc_name, "PHB");
89 	if (dn) {
90 		*node_type = NODE_TYPE_PHB;
91 		return dn;
92 	}
93 
94 	dn = find_vio_slot_node(drc_name);
95 	if (dn) {
96 		*node_type = NODE_TYPE_VIO;
97 		return dn;
98 	}
99 
100 	return NULL;
101 }
102 
103 /**
104  * find_php_slot - return hotplug slot structure for device node
105  * @dn: target &device_node
106  *
107  * This routine will return the hotplug slot structure
108  * for a given device node. Note that built-in PCI slots
109  * may be dlpar-able, but not hot-pluggable, so this routine
110  * will return NULL for built-in PCI slots.
111  */
112 static struct slot *find_php_slot(struct device_node *dn)
113 {
114 	struct slot *slot, *next;
115 
116 	list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
117 				 rpaphp_slot_list) {
118 		if (slot->dn == dn)
119 			return slot;
120 	}
121 
122 	return NULL;
123 }
124 
125 static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
126 					struct device_node *dev_dn)
127 {
128 	struct pci_dev *tmp = NULL;
129 	struct device_node *child_dn;
130 
131 	list_for_each_entry(tmp, &parent->devices, bus_list) {
132 		child_dn = pci_device_to_OF_node(tmp);
133 		if (child_dn == dev_dn)
134 			return tmp;
135 	}
136 	return NULL;
137 }
138 
139 static void dlpar_pci_add_bus(struct device_node *dn)
140 {
141 	struct pci_dn *pdn = PCI_DN(dn);
142 	struct pci_controller *phb = pdn->phb;
143 	struct pci_dev *dev = NULL;
144 
145 	eeh_add_device_tree_early(pdn);
146 
147 	/* Add EADS device to PHB bus, adding new entry to bus->devices */
148 	dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
149 	if (!dev) {
150 		printk(KERN_ERR "%s: failed to create pci dev for %pOF\n",
151 				__func__, dn);
152 		return;
153 	}
154 
155 	/* Scan below the new bridge */
156 	if (pci_is_bridge(dev))
157 		of_scan_pci_bridge(dev);
158 
159 	/* Map IO space for child bus, which may or may not succeed */
160 	pcibios_map_io_space(dev->subordinate);
161 
162 	/* Finish adding it : resource allocation, adding devices, etc...
163 	 * Note that we need to perform the finish pass on the -parent-
164 	 * bus of the EADS bridge so the bridge device itself gets
165 	 * properly added
166 	 */
167 	pcibios_finish_adding_to_bus(phb->bus);
168 }
169 
170 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
171 {
172 	struct pci_dev *dev;
173 	struct pci_controller *phb;
174 
175 	if (pci_find_bus_by_node(dn))
176 		return -EINVAL;
177 
178 	/* Add pci bus */
179 	dlpar_pci_add_bus(dn);
180 
181 	/* Confirm new bridge dev was created */
182 	phb = PCI_DN(dn)->phb;
183 	dev = dlpar_find_new_dev(phb->bus, dn);
184 
185 	if (!dev) {
186 		printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
187 			drc_name);
188 		return -EIO;
189 	}
190 
191 	if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
192 		printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
193 			__func__, dev->hdr_type, drc_name);
194 		return -EIO;
195 	}
196 
197 	/* Add hotplug slot */
198 	if (rpaphp_add_slot(dn)) {
199 		printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
200 			__func__, drc_name);
201 		return -EIO;
202 	}
203 	return 0;
204 }
205 
206 static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
207 {
208 	struct slot *slot;
209 	struct pci_dn *pdn;
210 	int rc = 0;
211 
212 	if (!pci_find_bus_by_node(dn))
213 		return -EINVAL;
214 
215 	/* If pci slot is hotpluggable, use hotplug to remove it */
216 	slot = find_php_slot(dn);
217 	if (slot && rpaphp_deregister_slot(slot)) {
218 		printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
219 		       __func__, drc_name);
220 		return -EIO;
221 	}
222 
223 	pdn = dn->data;
224 	BUG_ON(!pdn || !pdn->phb);
225 	rc = remove_phb_dynamic(pdn->phb);
226 	if (rc < 0)
227 		return rc;
228 
229 	pdn->phb = NULL;
230 
231 	return 0;
232 }
233 
234 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
235 {
236 	struct pci_controller *phb;
237 
238 	if (PCI_DN(dn) && PCI_DN(dn)->phb) {
239 		/* PHB already exists */
240 		return -EINVAL;
241 	}
242 
243 	phb = init_phb_dynamic(dn);
244 	if (!phb)
245 		return -EIO;
246 
247 	if (rpaphp_add_slot(dn)) {
248 		printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
249 			__func__, drc_name);
250 		return -EIO;
251 	}
252 	return 0;
253 }
254 
255 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
256 {
257 	struct vio_dev *vio_dev;
258 
259 	vio_dev = vio_find_node(dn);
260 	if (vio_dev) {
261 		put_device(&vio_dev->dev);
262 		return -EINVAL;
263 	}
264 
265 	if (!vio_register_device_node(dn)) {
266 		printk(KERN_ERR
267 			"%s: failed to register vio node %s\n",
268 			__func__, drc_name);
269 		return -EIO;
270 	}
271 	return 0;
272 }
273 
274 /**
275  * dlpar_add_slot - DLPAR add an I/O Slot
276  * @drc_name: drc-name of newly added slot
277  *
278  * Make the hotplug module and the kernel aware of a newly added I/O Slot.
279  * Return Codes:
280  * 0			Success
281  * -ENODEV		Not a valid drc_name
282  * -EINVAL		Slot already added
283  * -ERESTARTSYS		Signalled before obtaining lock
284  * -EIO			Internal PCI Error
285  */
286 int dlpar_add_slot(char *drc_name)
287 {
288 	struct device_node *dn = NULL;
289 	int node_type;
290 	int rc = -EIO;
291 
292 	if (mutex_lock_interruptible(&rpadlpar_mutex))
293 		return -ERESTARTSYS;
294 
295 	/* Find newly added node */
296 	dn = find_dlpar_node(drc_name, &node_type);
297 	if (!dn) {
298 		rc = -ENODEV;
299 		goto exit;
300 	}
301 
302 	switch (node_type) {
303 		case NODE_TYPE_VIO:
304 			rc = dlpar_add_vio_slot(drc_name, dn);
305 			break;
306 		case NODE_TYPE_SLOT:
307 			rc = dlpar_add_pci_slot(drc_name, dn);
308 			break;
309 		case NODE_TYPE_PHB:
310 			rc = dlpar_add_phb(drc_name, dn);
311 			break;
312 	}
313 
314 	printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
315 exit:
316 	mutex_unlock(&rpadlpar_mutex);
317 	return rc;
318 }
319 
320 /**
321  * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
322  * @drc_name: drc-name of newly added slot
323  * @dn: &device_node
324  *
325  * Remove the kernel and hotplug representations of an I/O Slot.
326  * Return Codes:
327  * 0			Success
328  * -EINVAL		Vio dev doesn't exist
329  */
330 static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
331 {
332 	struct vio_dev *vio_dev;
333 
334 	vio_dev = vio_find_node(dn);
335 	if (!vio_dev)
336 		return -EINVAL;
337 
338 	vio_unregister_device(vio_dev);
339 
340 	put_device(&vio_dev->dev);
341 
342 	return 0;
343 }
344 
345 /**
346  * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
347  * @drc_name: drc-name of newly added slot
348  * @dn: &device_node
349  *
350  * Remove the kernel and hotplug representations of a PCI I/O Slot.
351  * Return Codes:
352  * 0			Success
353  * -ENODEV		Not a valid drc_name
354  * -EIO			Internal PCI Error
355  */
356 int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
357 {
358 	struct pci_bus *bus;
359 	struct slot *slot;
360 	int ret = 0;
361 
362 	pci_lock_rescan_remove();
363 
364 	bus = pci_find_bus_by_node(dn);
365 	if (!bus) {
366 		ret = -EINVAL;
367 		goto out;
368 	}
369 
370 	pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
371 		 bus->self ? pci_name(bus->self) : "<!PHB!>");
372 
373 	slot = find_php_slot(dn);
374 	if (slot) {
375 		pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
376 			 pci_domain_nr(bus), bus->number);
377 
378 		if (rpaphp_deregister_slot(slot)) {
379 			printk(KERN_ERR
380 				"%s: unable to remove hotplug slot %s\n",
381 				__func__, drc_name);
382 			ret = -EIO;
383 			goto out;
384 		}
385 	}
386 
387 	/* Remove all devices below slot */
388 	pci_hp_remove_devices(bus);
389 
390 	/* Unmap PCI IO space */
391 	if (pcibios_unmap_io_space(bus)) {
392 		printk(KERN_ERR "%s: failed to unmap bus range\n",
393 			__func__);
394 		ret = -ERANGE;
395 		goto out;
396 	}
397 
398 	/* Remove the EADS bridge device itself */
399 	BUG_ON(!bus->self);
400 	pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
401 	pci_stop_and_remove_bus_device(bus->self);
402 
403  out:
404 	pci_unlock_rescan_remove();
405 	return ret;
406 }
407 
408 /**
409  * dlpar_remove_slot - DLPAR remove an I/O Slot
410  * @drc_name: drc-name of newly added slot
411  *
412  * Remove the kernel and hotplug representations of an I/O Slot.
413  * Return Codes:
414  * 0			Success
415  * -ENODEV		Not a valid drc_name
416  * -EINVAL		Slot already removed
417  * -ERESTARTSYS		Signalled before obtaining lock
418  * -EIO			Internal Error
419  */
420 int dlpar_remove_slot(char *drc_name)
421 {
422 	struct device_node *dn;
423 	int node_type;
424 	int rc = 0;
425 
426 	if (mutex_lock_interruptible(&rpadlpar_mutex))
427 		return -ERESTARTSYS;
428 
429 	dn = find_dlpar_node(drc_name, &node_type);
430 	if (!dn) {
431 		rc = -ENODEV;
432 		goto exit;
433 	}
434 
435 	switch (node_type) {
436 		case NODE_TYPE_VIO:
437 			rc = dlpar_remove_vio_slot(drc_name, dn);
438 			break;
439 		case NODE_TYPE_PHB:
440 			rc = dlpar_remove_phb(drc_name, dn);
441 			break;
442 		case NODE_TYPE_SLOT:
443 			rc = dlpar_remove_pci_slot(drc_name, dn);
444 			break;
445 	}
446 	vm_unmap_aliases();
447 
448 	printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
449 exit:
450 	mutex_unlock(&rpadlpar_mutex);
451 	return rc;
452 }
453 
454 static inline int is_dlpar_capable(void)
455 {
456 	int rc = rtas_token("ibm,configure-connector");
457 
458 	return (int) (rc != RTAS_UNKNOWN_SERVICE);
459 }
460 
461 int __init rpadlpar_io_init(void)
462 {
463 
464 	if (!is_dlpar_capable()) {
465 		printk(KERN_WARNING "%s: partition not DLPAR capable\n",
466 			__func__);
467 		return -EPERM;
468 	}
469 
470 	return dlpar_sysfs_init();
471 }
472 
473 void rpadlpar_io_exit(void)
474 {
475 	dlpar_sysfs_exit();
476 	return;
477 }
478 
479 module_init(rpadlpar_io_init);
480 module_exit(rpadlpar_io_exit);
481 MODULE_LICENSE("GPL");
482