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.
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <linux/string.h>
21 #include <asm/pci-bridge.h>
22 #include <linux/mutex.h>
30 static DEFINE_MUTEX(rpadlpar_mutex
);
32 #define DLPAR_MODULE_NAME "rpadlpar_io"
34 #define NODE_TYPE_VIO 1
35 #define NODE_TYPE_SLOT 2
36 #define NODE_TYPE_PHB 3
38 static struct device_node
*find_vio_slot_node(char *drc_name
)
40 struct device_node
*parent
= of_find_node_by_name(NULL
, "vdevice");
41 struct device_node
*dn
= NULL
;
48 while ((dn
= of_get_next_child(parent
, dn
))) {
49 rc
= rpaphp_get_drc_props(dn
, NULL
, &name
, NULL
, NULL
);
50 if ((rc
== 0) && (!strcmp(drc_name
, name
)))
57 /* Find dlpar-capable pci node that contains the specified name and type */
58 static struct device_node
*find_php_slot_pci_node(char *drc_name
,
61 struct device_node
*np
= NULL
;
66 while ((np
= of_find_node_by_name(np
, "pci"))) {
67 rc
= rpaphp_get_drc_props(np
, NULL
, &name
, &type
, NULL
);
69 if (!strcmp(drc_name
, name
) && !strcmp(drc_type
, type
))
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
104 * This routine will return the hotplug slot structure
105 * for a given device node. Note that built-in PCI slots
106 * may be dlpar-able, but not hot-pluggable, so this routine
107 * will return NULL for built-in PCI slots.
109 static struct slot
*find_php_slot(struct device_node
*dn
)
111 struct list_head
*tmp
, *n
;
114 list_for_each_safe(tmp
, n
, &rpaphp_slot_head
) {
115 slot
= list_entry(tmp
, struct slot
, rpaphp_slot_list
);
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 eeh_add_device_tree_early(dn
);
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 %s\n",
149 __FUNCTION__
, dn
->full_name
);
153 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
154 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
155 of_scan_pci_bridge(dn
, dev
);
157 pcibios_fixup_new_pci_devices(dev
->subordinate
,0);
159 /* Claim new bus resources */
160 pcibios_claim_one_bus(dev
->bus
);
162 /* Map IO space for child bus, which may or may not succeed */
163 pcibios_map_io_space(dev
->subordinate
);
165 /* Add new devices to global lists. Register in proc, sysfs. */
166 pci_bus_add_devices(phb
->bus
);
169 static int dlpar_add_pci_slot(char *drc_name
, struct device_node
*dn
)
172 struct pci_controller
*phb
;
174 if (pcibios_find_pci_bus(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", __FUNCTION__
,
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 __FUNCTION__
, 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",
199 __FUNCTION__
, drc_name
);
205 static int dlpar_remove_root_bus(struct pci_controller
*phb
)
207 struct pci_bus
*phb_bus
;
211 if (!(list_empty(&phb_bus
->children
) &&
212 list_empty(&phb_bus
->devices
))) {
216 rc
= pcibios_remove_root_bus(phb
);
220 device_unregister(phb_bus
->bridge
);
221 pci_remove_bus(phb_bus
);
226 static int dlpar_remove_phb(char *drc_name
, struct device_node
*dn
)
232 if (!pcibios_find_pci_bus(dn
))
235 /* If pci slot is hotplugable, use hotplug to remove it */
236 slot
= find_php_slot(dn
);
238 if (rpaphp_deregister_slot(slot
)) {
240 "%s: unable to remove hotplug slot %s\n",
241 __FUNCTION__
, drc_name
);
247 BUG_ON(!pdn
|| !pdn
->phb
);
248 rc
= dlpar_remove_root_bus(pdn
->phb
);
257 static int dlpar_add_phb(char *drc_name
, struct device_node
*dn
)
259 struct pci_controller
*phb
;
261 if (PCI_DN(dn
) && PCI_DN(dn
)->phb
) {
262 /* PHB already exists */
266 phb
= init_phb_dynamic(dn
);
270 if (rpaphp_add_slot(dn
)) {
271 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
272 __FUNCTION__
, drc_name
);
278 static int dlpar_add_vio_slot(char *drc_name
, struct device_node
*dn
)
280 if (vio_find_node(dn
))
283 if (!vio_register_device_node(dn
)) {
285 "%s: failed to register vio node %s\n",
286 __FUNCTION__
, drc_name
);
293 * dlpar_add_slot - DLPAR add an I/O Slot
294 * @drc_name: drc-name of newly added slot
296 * Make the hotplug module and the kernel aware
297 * of a newly added I/O Slot.
300 * -ENODEV Not a valid drc_name
301 * -EINVAL Slot already added
302 * -ERESTARTSYS Signalled before obtaining lock
303 * -EIO Internal PCI Error
305 int dlpar_add_slot(char *drc_name
)
307 struct device_node
*dn
= NULL
;
311 if (mutex_lock_interruptible(&rpadlpar_mutex
))
314 /* Find newly added node */
315 dn
= find_dlpar_node(drc_name
, &node_type
);
323 rc
= dlpar_add_vio_slot(drc_name
, dn
);
326 rc
= dlpar_add_pci_slot(drc_name
, dn
);
329 rc
= dlpar_add_phb(drc_name
, dn
);
333 printk(KERN_INFO
"%s: slot %s added\n", DLPAR_MODULE_NAME
, drc_name
);
335 mutex_unlock(&rpadlpar_mutex
);
340 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
341 * @drc_name: drc-name of newly added slot
343 * Remove the kernel and hotplug representations
347 * -EINVAL Vio dev doesn't exist
349 static int dlpar_remove_vio_slot(char *drc_name
, struct device_node
*dn
)
351 struct vio_dev
*vio_dev
;
353 vio_dev
= vio_find_node(dn
);
357 vio_unregister_device(vio_dev
);
362 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
363 * @drc_name: drc-name of newly added slot
365 * Remove the kernel and hotplug representations
369 * -ENODEV Not a valid drc_name
370 * -EIO Internal PCI Error
372 int dlpar_remove_pci_slot(char *drc_name
, struct device_node
*dn
)
377 bus
= pcibios_find_pci_bus(dn
);
381 /* If pci slot is hotplugable, use hotplug to remove it */
382 slot
= find_php_slot(dn
);
384 if (rpaphp_deregister_slot(slot
)) {
386 "%s: unable to remove hotplug slot %s\n",
387 __FUNCTION__
, drc_name
);
391 pcibios_remove_pci_devices(bus
);
393 if (pcibios_unmap_io_space(bus
)) {
394 printk(KERN_ERR
"%s: failed to unmap bus range\n",
400 pci_remove_bus_device(bus
->self
);
405 * dlpar_remove_slot - DLPAR remove an I/O Slot
406 * @drc_name: drc-name of newly added slot
408 * Remove the kernel and hotplug representations
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
);
443 printk(KERN_INFO
"%s: slot %s removed\n", DLPAR_MODULE_NAME
, drc_name
);
445 mutex_unlock(&rpadlpar_mutex
);
449 static inline int is_dlpar_capable(void)
451 int rc
= rtas_token("ibm,configure-connector");
453 return (int) (rc
!= RTAS_UNKNOWN_SERVICE
);
456 int __init
rpadlpar_io_init(void)
460 if (!is_dlpar_capable()) {
461 printk(KERN_WARNING
"%s: partition not DLPAR capable\n",
466 rc
= dlpar_sysfs_init();
470 void rpadlpar_io_exit(void)
476 module_init(rpadlpar_io_init
);
477 module_exit(rpadlpar_io_exit
);
478 MODULE_LICENSE("GPL");