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>
18 #include <linux/pci.h>
19 #include <linux/string.h>
20 #include <linux/vmalloc.h>
22 #include <asm/pci-bridge.h>
23 #include <linux/mutex.h>
26 #include <linux/firmware.h>
32 static DEFINE_MUTEX(rpadlpar_mutex
);
34 #define DLPAR_MODULE_NAME "rpadlpar_io"
36 #define NODE_TYPE_VIO 1
37 #define NODE_TYPE_SLOT 2
38 #define NODE_TYPE_PHB 3
40 static struct device_node
*find_vio_slot_node(char *drc_name
)
42 struct device_node
*parent
= of_find_node_by_name(NULL
, "vdevice");
43 struct device_node
*dn
= NULL
;
49 while ((dn
= of_get_next_child(parent
, dn
))) {
50 rc
= rpaphp_check_drc_props(dn
, drc_name
, NULL
);
59 /* Find dlpar-capable pci node that contains the specified name and type */
60 static struct device_node
*find_php_slot_pci_node(char *drc_name
,
63 struct device_node
*np
= NULL
;
66 while ((np
= of_find_node_by_name(np
, "pci"))) {
67 rc
= rpaphp_check_drc_props(np
, drc_name
, drc_type
);
75 /* Returns a device_node with its reference count incremented */
76 static struct device_node
*find_dlpar_node(char *drc_name
, int *node_type
)
78 struct device_node
*dn
;
80 dn
= find_php_slot_pci_node(drc_name
, "SLOT");
82 *node_type
= NODE_TYPE_SLOT
;
86 dn
= find_php_slot_pci_node(drc_name
, "PHB");
88 *node_type
= NODE_TYPE_PHB
;
92 dn
= find_vio_slot_node(drc_name
);
94 *node_type
= NODE_TYPE_VIO
;
102 * find_php_slot - return hotplug slot structure for device node
103 * @dn: target &device_node
105 * This routine will return the hotplug slot structure
106 * for a given device node. Note that built-in PCI slots
107 * may be dlpar-able, but not hot-pluggable, so this routine
108 * will return NULL for built-in PCI slots.
110 static struct slot
*find_php_slot(struct device_node
*dn
)
112 struct slot
*slot
, *next
;
114 list_for_each_entry_safe(slot
, next
, &rpaphp_slot_head
,
123 static struct pci_dev
*dlpar_find_new_dev(struct pci_bus
*parent
,
124 struct device_node
*dev_dn
)
126 struct pci_dev
*tmp
= NULL
;
127 struct device_node
*child_dn
;
129 list_for_each_entry(tmp
, &parent
->devices
, bus_list
) {
130 child_dn
= pci_device_to_OF_node(tmp
);
131 if (child_dn
== dev_dn
)
137 static void dlpar_pci_add_bus(struct device_node
*dn
)
139 struct pci_dn
*pdn
= PCI_DN(dn
);
140 struct pci_controller
*phb
= pdn
->phb
;
141 struct pci_dev
*dev
= NULL
;
143 pseries_eeh_init_edev_recursive(pdn
);
145 /* Add EADS device to PHB bus, adding new entry to bus->devices */
146 dev
= of_create_pci_dev(dn
, phb
->bus
, pdn
->devfn
);
148 printk(KERN_ERR
"%s: failed to create pci dev for %pOF\n",
153 /* Scan below the new bridge */
154 if (pci_is_bridge(dev
))
155 of_scan_pci_bridge(dev
);
157 /* Map IO space for child bus, which may or may not succeed */
158 pcibios_map_io_space(dev
->subordinate
);
160 /* Finish adding it : resource allocation, adding devices, etc...
161 * Note that we need to perform the finish pass on the -parent-
162 * bus of the EADS bridge so the bridge device itself gets
165 pcibios_finish_adding_to_bus(phb
->bus
);
168 static int dlpar_add_pci_slot(char *drc_name
, struct device_node
*dn
)
171 struct pci_controller
*phb
;
173 if (pci_find_bus_by_node(dn
))
177 dlpar_pci_add_bus(dn
);
179 /* Confirm new bridge dev was created */
180 phb
= PCI_DN(dn
)->phb
;
181 dev
= dlpar_find_new_dev(phb
->bus
, dn
);
184 printk(KERN_ERR
"%s: unable to add bus %s\n", __func__
,
189 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
190 printk(KERN_ERR
"%s: unexpected header type %d, unable to add bus %s\n",
191 __func__
, dev
->hdr_type
, drc_name
);
195 /* Add hotplug slot */
196 if (rpaphp_add_slot(dn
)) {
197 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
204 static int dlpar_remove_phb(char *drc_name
, struct device_node
*dn
)
210 if (!pci_find_bus_by_node(dn
))
213 /* If pci slot is hotpluggable, use hotplug to remove it */
214 slot
= find_php_slot(dn
);
215 if (slot
&& rpaphp_deregister_slot(slot
)) {
216 printk(KERN_ERR
"%s: unable to remove hotplug slot %s\n",
222 BUG_ON(!pdn
|| !pdn
->phb
);
223 rc
= remove_phb_dynamic(pdn
->phb
);
232 static int dlpar_add_phb(char *drc_name
, struct device_node
*dn
)
234 struct pci_controller
*phb
;
236 if (PCI_DN(dn
) && PCI_DN(dn
)->phb
) {
237 /* PHB already exists */
241 phb
= init_phb_dynamic(dn
);
245 if (rpaphp_add_slot(dn
)) {
246 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
253 static int dlpar_add_vio_slot(char *drc_name
, struct device_node
*dn
)
255 struct vio_dev
*vio_dev
;
257 vio_dev
= vio_find_node(dn
);
259 put_device(&vio_dev
->dev
);
263 if (!vio_register_device_node(dn
)) {
265 "%s: failed to register vio node %s\n",
273 * dlpar_add_slot - DLPAR add an I/O Slot
274 * @drc_name: drc-name of newly added slot
276 * Make the hotplug module and the kernel aware of a newly added I/O Slot.
279 * -ENODEV Not a valid drc_name
280 * -EINVAL Slot already added
281 * -ERESTARTSYS Signalled before obtaining lock
282 * -EIO Internal PCI Error
284 int dlpar_add_slot(char *drc_name
)
286 struct device_node
*dn
= NULL
;
290 if (mutex_lock_interruptible(&rpadlpar_mutex
))
293 /* Find newly added node */
294 dn
= find_dlpar_node(drc_name
, &node_type
);
302 rc
= dlpar_add_vio_slot(drc_name
, dn
);
305 rc
= dlpar_add_pci_slot(drc_name
, dn
);
308 rc
= dlpar_add_phb(drc_name
, dn
);
313 printk(KERN_INFO
"%s: slot %s added\n", DLPAR_MODULE_NAME
, drc_name
);
315 mutex_unlock(&rpadlpar_mutex
);
320 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
321 * @drc_name: drc-name of newly added slot
324 * Remove the kernel and hotplug representations of an I/O Slot.
327 * -EINVAL Vio dev doesn't exist
329 static int dlpar_remove_vio_slot(char *drc_name
, struct device_node
*dn
)
331 struct vio_dev
*vio_dev
;
333 vio_dev
= vio_find_node(dn
);
337 vio_unregister_device(vio_dev
);
339 put_device(&vio_dev
->dev
);
345 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
346 * @drc_name: drc-name of newly added slot
349 * Remove the kernel and hotplug representations of a PCI I/O Slot.
352 * -ENODEV Not a valid drc_name
353 * -EIO Internal PCI Error
355 int dlpar_remove_pci_slot(char *drc_name
, struct device_node
*dn
)
361 pci_lock_rescan_remove();
363 bus
= pci_find_bus_by_node(dn
);
369 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
370 bus
->self
? pci_name(bus
->self
) : "<!PHB!>");
372 slot
= find_php_slot(dn
);
374 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
375 pci_domain_nr(bus
), bus
->number
);
377 if (rpaphp_deregister_slot(slot
)) {
379 "%s: unable to remove hotplug slot %s\n",
386 /* Remove all devices below slot */
387 pci_hp_remove_devices(bus
);
389 /* Unmap PCI IO space */
390 if (pcibios_unmap_io_space(bus
)) {
391 printk(KERN_ERR
"%s: failed to unmap bus range\n",
397 /* Remove the EADS bridge device itself */
399 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus
->self
));
400 pci_stop_and_remove_bus_device(bus
->self
);
403 pci_unlock_rescan_remove();
408 * dlpar_remove_slot - DLPAR remove an I/O Slot
409 * @drc_name: drc-name of newly added slot
411 * Remove the kernel and hotplug representations of an I/O Slot.
414 * -ENODEV Not a valid drc_name
415 * -EINVAL Slot already removed
416 * -ERESTARTSYS Signalled before obtaining lock
417 * -EIO Internal Error
419 int dlpar_remove_slot(char *drc_name
)
421 struct device_node
*dn
;
425 if (mutex_lock_interruptible(&rpadlpar_mutex
))
428 dn
= find_dlpar_node(drc_name
, &node_type
);
436 rc
= dlpar_remove_vio_slot(drc_name
, dn
);
439 rc
= dlpar_remove_phb(drc_name
, dn
);
442 rc
= dlpar_remove_pci_slot(drc_name
, dn
);
448 printk(KERN_INFO
"%s: slot %s removed\n", DLPAR_MODULE_NAME
, drc_name
);
450 mutex_unlock(&rpadlpar_mutex
);
454 static inline int is_dlpar_capable(void)
456 int rc
= rtas_token("ibm,configure-connector");
458 return (int) (rc
!= RTAS_UNKNOWN_SERVICE
);
461 int __init
rpadlpar_io_init(void)
464 if (!is_dlpar_capable()) {
465 printk(KERN_WARNING
"%s: partition not DLPAR capable\n",
470 return dlpar_sysfs_init();
473 void rpadlpar_io_exit(void)
478 module_init(rpadlpar_io_init
);
479 module_exit(rpadlpar_io_exit
);
480 MODULE_LICENSE("GPL");