2 * Interface for Dynamic Logical Partitioning of I/O Slots on
3 * RPA-compliant PPC64 platform.
5 * John Rose <johnrose@austin.ibm.com>
6 * Linda Xie <lxie@us.ibm.com>
10 * Copyright (C) 2003 IBM.
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.
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>
26 #include <asm/pci-bridge.h>
27 #include <linux/mutex.h>
35 static DEFINE_MUTEX(rpadlpar_mutex);
37 #define DLPAR_MODULE_NAME "rpadlpar_io"
39 #define NODE_TYPE_VIO 1
40 #define NODE_TYPE_SLOT 2
41 #define NODE_TYPE_PHB 3
43 static struct device_node *find_vio_slot_node(char *drc_name)
45 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
46 struct device_node *dn = NULL;
53 while ((dn = of_get_next_child(parent, dn))) {
54 rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
55 if ((rc == 0) && (!strcmp(drc_name, name)))
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,
66 struct device_node *np = NULL;
71 while ((np = of_find_node_by_name(np, "pci"))) {
72 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
74 if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
81 static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
83 struct device_node *dn;
85 dn = find_php_slot_pci_node(drc_name, "SLOT");
87 *node_type = NODE_TYPE_SLOT;
91 dn = find_php_slot_pci_node(drc_name, "PHB");
93 *node_type = NODE_TYPE_PHB;
97 dn = find_vio_slot_node(drc_name);
99 *node_type = NODE_TYPE_VIO;
107 * find_php_slot - return hotplug slot structure for device node
108 * @dn: target &device_node
110 * This routine will return the hotplug slot structure
111 * for a given device node. Note that built-in PCI slots
112 * may be dlpar-able, but not hot-pluggable, so this routine
113 * will return NULL for built-in PCI slots.
115 static struct slot *find_php_slot(struct device_node *dn)
117 struct list_head *tmp, *n;
120 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
121 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
129 static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
130 struct device_node *dev_dn)
132 struct pci_dev *tmp = NULL;
133 struct device_node *child_dn;
135 list_for_each_entry(tmp, &parent->devices, bus_list) {
136 child_dn = pci_device_to_OF_node(tmp);
137 if (child_dn == dev_dn)
143 static void dlpar_pci_add_bus(struct device_node *dn)
145 struct pci_dn *pdn = PCI_DN(dn);
146 struct pci_controller *phb = pdn->phb;
147 struct pci_dev *dev = NULL;
149 eeh_add_device_tree_early(dn);
151 /* Add EADS device to PHB bus, adding new entry to bus->devices */
152 dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
154 printk(KERN_ERR "%s: failed to create pci dev for %s\n",
155 __func__, dn->full_name);
159 /* Scan below the new bridge */
160 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
161 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
162 of_scan_pci_bridge(dev);
164 /* Map IO space for child bus, which may or may not succeed */
165 pcibios_map_io_space(dev->subordinate);
167 /* Finish adding it : resource allocation, adding devices, etc...
168 * Note that we need to perform the finish pass on the -parent-
169 * bus of the EADS bridge so the bridge device itself gets
172 pcibios_finish_adding_to_bus(phb->bus);
175 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
178 struct pci_controller *phb;
180 if (pcibios_find_pci_bus(dn))
184 dlpar_pci_add_bus(dn);
186 /* Confirm new bridge dev was created */
187 phb = PCI_DN(dn)->phb;
188 dev = dlpar_find_new_dev(phb->bus, dn);
191 printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
196 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
197 printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
198 __func__, dev->hdr_type, drc_name);
202 /* Add hotplug slot */
203 if (rpaphp_add_slot(dn)) {
204 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
211 static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
217 if (!pcibios_find_pci_bus(dn))
220 /* If pci slot is hotpluggable, use hotplug to remove it */
221 slot = find_php_slot(dn);
222 if (slot && rpaphp_deregister_slot(slot)) {
223 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
229 BUG_ON(!pdn || !pdn->phb);
230 rc = remove_phb_dynamic(pdn->phb);
239 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
241 struct pci_controller *phb;
243 if (PCI_DN(dn) && PCI_DN(dn)->phb) {
244 /* PHB already exists */
248 phb = init_phb_dynamic(dn);
252 if (rpaphp_add_slot(dn)) {
253 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
260 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
262 if (vio_find_node(dn))
265 if (!vio_register_device_node(dn)) {
267 "%s: failed to register vio node %s\n",
275 * dlpar_add_slot - DLPAR add an I/O Slot
276 * @drc_name: drc-name of newly added slot
278 * Make the hotplug module and the kernel aware of a newly added I/O Slot.
281 * -ENODEV Not a valid drc_name
282 * -EINVAL Slot already added
283 * -ERESTARTSYS Signalled before obtaining lock
284 * -EIO Internal PCI Error
286 int dlpar_add_slot(char *drc_name)
288 struct device_node *dn = NULL;
292 if (mutex_lock_interruptible(&rpadlpar_mutex))
295 /* Find newly added node */
296 dn = find_dlpar_node(drc_name, &node_type);
304 rc = dlpar_add_vio_slot(drc_name, dn);
307 rc = dlpar_add_pci_slot(drc_name, dn);
310 rc = dlpar_add_phb(drc_name, dn);
314 printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
316 mutex_unlock(&rpadlpar_mutex);
321 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
322 * @drc_name: drc-name of newly added slot
325 * Remove the kernel and hotplug representations of an I/O Slot.
328 * -EINVAL Vio dev doesn't exist
330 static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
332 struct vio_dev *vio_dev;
334 vio_dev = vio_find_node(dn);
338 vio_unregister_device(vio_dev);
343 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
344 * @drc_name: drc-name of newly added slot
347 * Remove the kernel and hotplug representations of a PCI I/O Slot.
350 * -ENODEV Not a valid drc_name
351 * -EIO Internal PCI Error
353 int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
359 pci_lock_rescan_remove();
361 bus = pcibios_find_pci_bus(dn);
367 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
368 bus->self ? pci_name(bus->self) : "<!PHB!>");
370 slot = find_php_slot(dn);
372 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
373 pci_domain_nr(bus), bus->number);
375 if (rpaphp_deregister_slot(slot)) {
377 "%s: unable to remove hotplug slot %s\n",
384 /* Remove all devices below slot */
385 pcibios_remove_pci_devices(bus);
387 /* Unmap PCI IO space */
388 if (pcibios_unmap_io_space(bus)) {
389 printk(KERN_ERR "%s: failed to unmap bus range\n",
395 /* Remove the EADS bridge device itself */
397 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
398 pci_stop_and_remove_bus_device(bus->self);
401 pci_unlock_rescan_remove();
406 * dlpar_remove_slot - DLPAR remove an I/O Slot
407 * @drc_name: drc-name of newly added slot
409 * Remove the kernel and hotplug representations of an I/O Slot.
412 * -ENODEV Not a valid drc_name
413 * -EINVAL Slot already removed
414 * -ERESTARTSYS Signalled before obtaining lock
415 * -EIO Internal Error
417 int dlpar_remove_slot(char *drc_name)
419 struct device_node *dn;
423 if (mutex_lock_interruptible(&rpadlpar_mutex))
426 dn = find_dlpar_node(drc_name, &node_type);
434 rc = dlpar_remove_vio_slot(drc_name, dn);
437 rc = dlpar_remove_phb(drc_name, dn);
440 rc = dlpar_remove_pci_slot(drc_name, dn);
445 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
447 mutex_unlock(&rpadlpar_mutex);
451 static inline int is_dlpar_capable(void)
453 int rc = rtas_token("ibm,configure-connector");
455 return (int) (rc != RTAS_UNKNOWN_SERVICE);
458 int __init rpadlpar_io_init(void)
462 if (!is_dlpar_capable()) {
463 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
468 rc = dlpar_sysfs_init();
472 void rpadlpar_io_exit(void)
478 module_init(rpadlpar_io_init);
479 module_exit(rpadlpar_io_exit);
480 MODULE_LICENSE("GPL");