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/pci.h>
22 #include <linux/string.h>
24 #include <asm/pci-bridge.h>
25 #include <linux/mutex.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
= NULL
;
51 while ((dn
= of_get_next_child(parent
, dn
))) {
52 rc
= rpaphp_get_drc_props(dn
, NULL
, &name
, NULL
, NULL
);
53 if ((rc
== 0) && (!strcmp(drc_name
, name
)))
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
= NULL
;
69 while ((np
= of_find_node_by_name(np
, "pci"))) {
70 rc
= rpaphp_get_drc_props(np
, NULL
, &name
, &type
, NULL
);
72 if (!strcmp(drc_name
, name
) && !strcmp(drc_type
, type
))
79 static struct device_node
*find_dlpar_node(char *drc_name
, int *node_type
)
81 struct device_node
*dn
;
83 dn
= find_php_slot_pci_node(drc_name
, "SLOT");
85 *node_type
= NODE_TYPE_SLOT
;
89 dn
= find_php_slot_pci_node(drc_name
, "PHB");
91 *node_type
= NODE_TYPE_PHB
;
95 dn
= find_vio_slot_node(drc_name
);
97 *node_type
= NODE_TYPE_VIO
;
105 * find_php_slot - return hotplug slot structure for device node
106 * @dn: target &device_node
108 * This routine will return the hotplug slot structure
109 * for a given device node. Note that built-in PCI slots
110 * may be dlpar-able, but not hot-pluggable, so this routine
111 * will return NULL for built-in PCI slots.
113 static struct slot
*find_php_slot(struct device_node
*dn
)
115 struct list_head
*tmp
, *n
;
118 list_for_each_safe(tmp
, n
, &rpaphp_slot_head
) {
119 slot
= list_entry(tmp
, struct slot
, rpaphp_slot_list
);
127 static struct pci_dev
*dlpar_find_new_dev(struct pci_bus
*parent
,
128 struct device_node
*dev_dn
)
130 struct pci_dev
*tmp
= NULL
;
131 struct device_node
*child_dn
;
133 list_for_each_entry(tmp
, &parent
->devices
, bus_list
) {
134 child_dn
= pci_device_to_OF_node(tmp
);
135 if (child_dn
== dev_dn
)
141 static void dlpar_pci_add_bus(struct device_node
*dn
)
143 struct pci_dn
*pdn
= PCI_DN(dn
);
144 struct pci_controller
*phb
= pdn
->phb
;
145 struct pci_dev
*dev
= NULL
;
147 eeh_add_device_tree_early(dn
);
149 /* Add EADS device to PHB bus, adding new entry to bus->devices */
150 dev
= of_create_pci_dev(dn
, phb
->bus
, pdn
->devfn
);
152 printk(KERN_ERR
"%s: failed to create pci dev for %s\n",
153 __func__
, dn
->full_name
);
157 /* Scan below the new bridge */
158 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
159 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
160 of_scan_pci_bridge(dn
, dev
);
162 /* Map IO space for child bus, which may or may not succeed */
163 pcibios_map_io_space(dev
->subordinate
);
165 /* Finish adding it : resource allocation, adding devices, etc...
166 * Note that we need to perform the finish pass on the -parent-
167 * bus of the EADS bridge so the bridge device itself gets
170 pcibios_finish_adding_to_bus(phb
->bus
);
173 static int dlpar_add_pci_slot(char *drc_name
, struct device_node
*dn
)
176 struct pci_controller
*phb
;
178 if (pcibios_find_pci_bus(dn
))
182 dlpar_pci_add_bus(dn
);
184 /* Confirm new bridge dev was created */
185 phb
= PCI_DN(dn
)->phb
;
186 dev
= dlpar_find_new_dev(phb
->bus
, dn
);
189 printk(KERN_ERR
"%s: unable to add bus %s\n", __func__
,
194 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
195 printk(KERN_ERR
"%s: unexpected header type %d, unable to add bus %s\n",
196 __func__
, dev
->hdr_type
, drc_name
);
200 /* Add hotplug slot */
201 if (rpaphp_add_slot(dn
)) {
202 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
209 static int dlpar_remove_phb(char *drc_name
, struct device_node
*dn
)
215 if (!pcibios_find_pci_bus(dn
))
218 /* If pci slot is hotplugable, use hotplug to remove it */
219 slot
= find_php_slot(dn
);
220 if (slot
&& rpaphp_deregister_slot(slot
)) {
221 printk(KERN_ERR
"%s: unable to remove hotplug slot %s\n",
227 BUG_ON(!pdn
|| !pdn
->phb
);
228 rc
= remove_phb_dynamic(pdn
->phb
);
237 static int dlpar_add_phb(char *drc_name
, struct device_node
*dn
)
239 struct pci_controller
*phb
;
241 if (PCI_DN(dn
) && PCI_DN(dn
)->phb
) {
242 /* PHB already exists */
246 phb
= init_phb_dynamic(dn
);
250 if (rpaphp_add_slot(dn
)) {
251 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
258 static int dlpar_add_vio_slot(char *drc_name
, struct device_node
*dn
)
260 if (vio_find_node(dn
))
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
);
312 printk(KERN_INFO
"%s: slot %s added\n", DLPAR_MODULE_NAME
, drc_name
);
314 mutex_unlock(&rpadlpar_mutex
);
319 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
320 * @drc_name: drc-name of newly added slot
323 * Remove the kernel and hotplug representations of an I/O Slot.
326 * -EINVAL Vio dev doesn't exist
328 static int dlpar_remove_vio_slot(char *drc_name
, struct device_node
*dn
)
330 struct vio_dev
*vio_dev
;
332 vio_dev
= vio_find_node(dn
);
336 vio_unregister_device(vio_dev
);
341 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
342 * @drc_name: drc-name of newly added slot
345 * Remove the kernel and hotplug representations of a PCI I/O Slot.
348 * -ENODEV Not a valid drc_name
349 * -EIO Internal PCI Error
351 int dlpar_remove_pci_slot(char *drc_name
, struct device_node
*dn
)
356 bus
= pcibios_find_pci_bus(dn
);
360 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
361 bus
->self
? pci_name(bus
->self
) : "<!PHB!>");
363 slot
= find_php_slot(dn
);
365 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
366 pci_domain_nr(bus
), bus
->number
);
368 if (rpaphp_deregister_slot(slot
)) {
370 "%s: unable to remove hotplug slot %s\n",
376 /* Remove all devices below slot */
377 pcibios_remove_pci_devices(bus
);
379 /* Unmap PCI IO space */
380 if (pcibios_unmap_io_space(bus
)) {
381 printk(KERN_ERR
"%s: failed to unmap bus range\n",
386 /* Remove the EADS bridge device itself */
388 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus
->self
));
389 eeh_remove_bus_device(bus
->self
);
390 pci_remove_bus_device(bus
->self
);
396 * dlpar_remove_slot - DLPAR remove an I/O Slot
397 * @drc_name: drc-name of newly added slot
399 * Remove the kernel and hotplug representations of an I/O Slot.
402 * -ENODEV Not a valid drc_name
403 * -EINVAL Slot already removed
404 * -ERESTARTSYS Signalled before obtaining lock
405 * -EIO Internal Error
407 int dlpar_remove_slot(char *drc_name
)
409 struct device_node
*dn
;
413 if (mutex_lock_interruptible(&rpadlpar_mutex
))
416 dn
= find_dlpar_node(drc_name
, &node_type
);
424 rc
= dlpar_remove_vio_slot(drc_name
, dn
);
427 rc
= dlpar_remove_phb(drc_name
, dn
);
430 rc
= dlpar_remove_pci_slot(drc_name
, dn
);
433 printk(KERN_INFO
"%s: slot %s removed\n", DLPAR_MODULE_NAME
, drc_name
);
435 mutex_unlock(&rpadlpar_mutex
);
439 static inline int is_dlpar_capable(void)
441 int rc
= rtas_token("ibm,configure-connector");
443 return (int) (rc
!= RTAS_UNKNOWN_SERVICE
);
446 int __init
rpadlpar_io_init(void)
450 if (!is_dlpar_capable()) {
451 printk(KERN_WARNING
"%s: partition not DLPAR capable\n",
456 rc
= dlpar_sysfs_init();
460 void rpadlpar_io_exit(void)
466 module_init(rpadlpar_io_init
);
467 module_exit(rpadlpar_io_exit
);
468 MODULE_LICENSE("GPL");