1 // SPDX-License-Identifier: GPL-2.0+
3 * Interface for Dynamic Logical Partitioning of I/O Slots on
4 * RPA-compliant PPC64 platform.
6 * John Rose <johnrose@austin.ibm.com>
7 * Linda Xie <lxie@us.ibm.com>
11 * Copyright (C) 2003 IBM.
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/string.h>
21 #include <linux/vmalloc.h>
23 #include <asm/pci-bridge.h>
24 #include <linux/mutex.h>
27 #include <linux/firmware.h>
33 static DEFINE_MUTEX(rpadlpar_mutex);
35 #define DLPAR_MODULE_NAME "rpadlpar_io"
37 #define NODE_TYPE_VIO 1
38 #define NODE_TYPE_SLOT 2
39 #define NODE_TYPE_PHB 3
41 static struct device_node *find_vio_slot_node(char *drc_name)
43 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
44 struct device_node *dn;
50 for_each_child_of_node(parent, dn) {
51 rc = rpaphp_check_drc_props(dn, drc_name, NULL);
60 /* Find dlpar-capable pci node that contains the specified name and type */
61 static struct device_node *find_php_slot_pci_node(char *drc_name,
64 struct device_node *np;
67 for_each_node_by_name(np, "pci") {
68 rc = rpaphp_check_drc_props(np, drc_name, drc_type);
76 /* Returns a device_node with its reference count incremented */
77 static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
79 struct device_node *dn;
81 dn = find_php_slot_pci_node(drc_name, "SLOT");
83 *node_type = NODE_TYPE_SLOT;
87 dn = find_php_slot_pci_node(drc_name, "PHB");
89 *node_type = NODE_TYPE_PHB;
93 dn = find_vio_slot_node(drc_name);
95 *node_type = NODE_TYPE_VIO;
103 * find_php_slot - return hotplug slot structure for device node
104 * @dn: target &device_node
106 * This routine will return the hotplug slot structure
107 * for a given device node. Note that built-in PCI slots
108 * may be dlpar-able, but not hot-pluggable, so this routine
109 * will return NULL for built-in PCI slots.
111 static struct slot *find_php_slot(struct device_node *dn)
113 struct slot *slot, *next;
115 list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
124 static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
125 struct device_node *dev_dn)
127 struct pci_dev *tmp = NULL;
128 struct device_node *child_dn;
130 list_for_each_entry(tmp, &parent->devices, bus_list) {
131 child_dn = pci_device_to_OF_node(tmp);
132 if (child_dn == dev_dn)
138 static void dlpar_pci_add_bus(struct device_node *dn)
140 struct pci_dn *pdn = PCI_DN(dn);
141 struct pci_controller *phb = pdn->phb;
142 struct pci_dev *dev = NULL;
144 pseries_eeh_init_edev_recursive(pdn);
146 /* Add EADS device to PHB bus, adding new entry to bus->devices */
147 dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
149 printk(KERN_ERR "%s: failed to create pci dev for %pOF\n",
154 /* Scan below the new bridge */
155 if (pci_is_bridge(dev))
156 of_scan_pci_bridge(dev);
158 /* Map IO space for child bus, which may or may not succeed */
159 pcibios_map_io_space(dev->subordinate);
161 /* Finish adding it : resource allocation, adding devices, etc...
162 * Note that we need to perform the finish pass on the -parent-
163 * bus of the EADS bridge so the bridge device itself gets
166 pcibios_finish_adding_to_bus(phb->bus);
169 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
172 struct pci_controller *phb;
174 if (pci_find_bus_by_node(dn))
178 dlpar_pci_add_bus(dn);
180 /* Confirm new bridge dev was created */
181 phb = PCI_DN(dn)->phb;
182 dev = dlpar_find_new_dev(phb->bus, dn);
185 printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
190 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
191 printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
192 __func__, dev->hdr_type, drc_name);
196 /* Add hotplug slot */
197 if (rpaphp_add_slot(dn)) {
198 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
205 static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
211 if (!pci_find_bus_by_node(dn))
214 /* If pci slot is hotpluggable, use hotplug to remove it */
215 slot = find_php_slot(dn);
216 if (slot && rpaphp_deregister_slot(slot)) {
217 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
223 BUG_ON(!pdn || !pdn->phb);
224 rc = remove_phb_dynamic(pdn->phb);
233 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
235 struct pci_controller *phb;
237 if (PCI_DN(dn) && PCI_DN(dn)->phb) {
238 /* PHB already exists */
242 phb = init_phb_dynamic(dn);
246 if (rpaphp_add_slot(dn)) {
247 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
254 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
256 struct vio_dev *vio_dev;
258 vio_dev = vio_find_node(dn);
260 put_device(&vio_dev->dev);
264 if (!vio_register_device_node(dn)) {
266 "%s: failed to register vio node %s\n",
274 * dlpar_add_slot - DLPAR add an I/O Slot
275 * @drc_name: drc-name of newly added slot
277 * Make the hotplug module and the kernel aware of a newly added I/O Slot.
280 * -ENODEV Not a valid drc_name
281 * -EINVAL Slot already added
282 * -ERESTARTSYS Signalled before obtaining lock
283 * -EIO Internal PCI Error
285 int dlpar_add_slot(char *drc_name)
287 struct device_node *dn = NULL;
291 if (mutex_lock_interruptible(&rpadlpar_mutex))
294 /* Find newly added node */
295 dn = find_dlpar_node(drc_name, &node_type);
303 rc = dlpar_add_vio_slot(drc_name, dn);
306 rc = dlpar_add_pci_slot(drc_name, dn);
309 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);
340 put_device(&vio_dev->dev);
346 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
347 * @drc_name: drc-name of newly added slot
350 * Remove the kernel and hotplug representations of a PCI I/O Slot.
353 * -ENODEV Not a valid drc_name
354 * -EIO Internal PCI Error
356 static int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
362 pci_lock_rescan_remove();
364 bus = pci_find_bus_by_node(dn);
370 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
371 bus->self ? pci_name(bus->self) : "<!PHB!>");
373 slot = find_php_slot(dn);
375 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
376 pci_domain_nr(bus), bus->number);
378 if (rpaphp_deregister_slot(slot)) {
380 "%s: unable to remove hotplug slot %s\n",
387 /* Remove all devices below slot */
388 pci_hp_remove_devices(bus);
390 /* Unmap PCI IO space */
391 if (pcibios_unmap_io_space(bus)) {
392 printk(KERN_ERR "%s: failed to unmap bus range\n",
398 /* Remove the EADS bridge device itself */
400 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
401 pci_stop_and_remove_bus_device(bus->self);
404 pci_unlock_rescan_remove();
409 * dlpar_remove_slot - DLPAR remove an I/O Slot
410 * @drc_name: drc-name of newly added slot
412 * Remove the kernel and hotplug representations of an I/O Slot.
415 * -ENODEV Not a valid drc_name
416 * -EINVAL Slot already removed
417 * -ERESTARTSYS Signalled before obtaining lock
418 * -EIO Internal Error
420 int dlpar_remove_slot(char *drc_name)
422 struct device_node *dn;
426 if (mutex_lock_interruptible(&rpadlpar_mutex))
429 dn = find_dlpar_node(drc_name, &node_type);
437 rc = dlpar_remove_vio_slot(drc_name, dn);
440 rc = dlpar_remove_phb(drc_name, dn);
443 rc = dlpar_remove_pci_slot(drc_name, dn);
449 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
451 mutex_unlock(&rpadlpar_mutex);
455 static inline int is_dlpar_capable(void)
457 int rc = rtas_token("ibm,configure-connector");
459 return (int) (rc != RTAS_UNKNOWN_SERVICE);
462 static int __init rpadlpar_io_init(void)
465 if (!is_dlpar_capable()) {
466 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
471 return dlpar_sysfs_init();
474 static void __exit rpadlpar_io_exit(void)
479 module_init(rpadlpar_io_init);
480 module_exit(rpadlpar_io_exit);
481 MODULE_LICENSE("GPL");
482 MODULE_DESCRIPTION("RPA Dynamic Logical Partitioning driver for I/O slots");