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>
23 #include <linux/vmalloc.h>
25 #include <asm/pci-bridge.h>
26 #include <linux/mutex.h>
34 static DEFINE_MUTEX(rpadlpar_mutex
);
36 #define DLPAR_MODULE_NAME "rpadlpar_io"
38 #define NODE_TYPE_VIO 1
39 #define NODE_TYPE_SLOT 2
40 #define NODE_TYPE_PHB 3
42 static struct device_node
*find_vio_slot_node(char *drc_name
)
44 struct device_node
*parent
= of_find_node_by_name(NULL
, "vdevice");
45 struct device_node
*dn
= NULL
;
52 while ((dn
= of_get_next_child(parent
, dn
))) {
53 rc
= rpaphp_get_drc_props(dn
, NULL
, &name
, NULL
, NULL
);
54 if ((rc
== 0) && (!strcmp(drc_name
, name
)))
61 /* Find dlpar-capable pci node that contains the specified name and type */
62 static struct device_node
*find_php_slot_pci_node(char *drc_name
,
65 struct device_node
*np
= NULL
;
70 while ((np
= of_find_node_by_name(np
, "pci"))) {
71 rc
= rpaphp_get_drc_props(np
, NULL
, &name
, &type
, NULL
);
73 if (!strcmp(drc_name
, name
) && !strcmp(drc_type
, type
))
80 static struct device_node
*find_dlpar_node(char *drc_name
, int *node_type
)
82 struct device_node
*dn
;
84 dn
= find_php_slot_pci_node(drc_name
, "SLOT");
86 *node_type
= NODE_TYPE_SLOT
;
90 dn
= find_php_slot_pci_node(drc_name
, "PHB");
92 *node_type
= NODE_TYPE_PHB
;
96 dn
= find_vio_slot_node(drc_name
);
98 *node_type
= NODE_TYPE_VIO
;
106 * find_php_slot - return hotplug slot structure for device node
107 * @dn: target &device_node
109 * This routine will return the hotplug slot structure
110 * for a given device node. Note that built-in PCI slots
111 * may be dlpar-able, but not hot-pluggable, so this routine
112 * will return NULL for built-in PCI slots.
114 static struct slot
*find_php_slot(struct device_node
*dn
)
116 struct list_head
*tmp
, *n
;
119 list_for_each_safe(tmp
, n
, &rpaphp_slot_head
) {
120 slot
= list_entry(tmp
, struct slot
, rpaphp_slot_list
);
128 static struct pci_dev
*dlpar_find_new_dev(struct pci_bus
*parent
,
129 struct device_node
*dev_dn
)
131 struct pci_dev
*tmp
= NULL
;
132 struct device_node
*child_dn
;
134 list_for_each_entry(tmp
, &parent
->devices
, bus_list
) {
135 child_dn
= pci_device_to_OF_node(tmp
);
136 if (child_dn
== dev_dn
)
142 static void dlpar_pci_add_bus(struct device_node
*dn
)
144 struct pci_dn
*pdn
= PCI_DN(dn
);
145 struct pci_controller
*phb
= pdn
->phb
;
146 struct pci_dev
*dev
= NULL
;
148 eeh_add_device_tree_early(dn
);
150 /* Add EADS device to PHB bus, adding new entry to bus->devices */
151 dev
= of_create_pci_dev(dn
, phb
->bus
, pdn
->devfn
);
153 printk(KERN_ERR
"%s: failed to create pci dev for %s\n",
154 __func__
, dn
->full_name
);
158 /* Scan below the new bridge */
159 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
||
160 dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)
161 of_scan_pci_bridge(dev
);
163 /* Map IO space for child bus, which may or may not succeed */
164 pcibios_map_io_space(dev
->subordinate
);
166 /* Finish adding it : resource allocation, adding devices, etc...
167 * Note that we need to perform the finish pass on the -parent-
168 * bus of the EADS bridge so the bridge device itself gets
171 pcibios_finish_adding_to_bus(phb
->bus
);
174 static int dlpar_add_pci_slot(char *drc_name
, struct device_node
*dn
)
177 struct pci_controller
*phb
;
179 if (pcibios_find_pci_bus(dn
))
183 dlpar_pci_add_bus(dn
);
185 /* Confirm new bridge dev was created */
186 phb
= PCI_DN(dn
)->phb
;
187 dev
= dlpar_find_new_dev(phb
->bus
, dn
);
190 printk(KERN_ERR
"%s: unable to add bus %s\n", __func__
,
195 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
196 printk(KERN_ERR
"%s: unexpected header type %d, unable to add bus %s\n",
197 __func__
, dev
->hdr_type
, drc_name
);
201 /* Add hotplug slot */
202 if (rpaphp_add_slot(dn
)) {
203 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
210 static int dlpar_remove_phb(char *drc_name
, struct device_node
*dn
)
216 if (!pcibios_find_pci_bus(dn
))
219 /* If pci slot is hotplugable, use hotplug to remove it */
220 slot
= find_php_slot(dn
);
221 if (slot
&& rpaphp_deregister_slot(slot
)) {
222 printk(KERN_ERR
"%s: unable to remove hotplug slot %s\n",
228 BUG_ON(!pdn
|| !pdn
->phb
);
229 rc
= remove_phb_dynamic(pdn
->phb
);
238 static int dlpar_add_phb(char *drc_name
, struct device_node
*dn
)
240 struct pci_controller
*phb
;
242 if (PCI_DN(dn
) && PCI_DN(dn
)->phb
) {
243 /* PHB already exists */
247 phb
= init_phb_dynamic(dn
);
251 if (rpaphp_add_slot(dn
)) {
252 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
259 static int dlpar_add_vio_slot(char *drc_name
, struct device_node
*dn
)
261 if (vio_find_node(dn
))
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
);
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
);
342 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
343 * @drc_name: drc-name of newly added slot
346 * Remove the kernel and hotplug representations of a PCI I/O Slot.
349 * -ENODEV Not a valid drc_name
350 * -EIO Internal PCI Error
352 int dlpar_remove_pci_slot(char *drc_name
, struct device_node
*dn
)
357 bus
= pcibios_find_pci_bus(dn
);
361 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
362 bus
->self
? pci_name(bus
->self
) : "<!PHB!>");
364 slot
= find_php_slot(dn
);
366 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
367 pci_domain_nr(bus
), bus
->number
);
369 if (rpaphp_deregister_slot(slot
)) {
371 "%s: unable to remove hotplug slot %s\n",
377 /* Remove all devices below slot */
378 pcibios_remove_pci_devices(bus
);
380 /* Unmap PCI IO space */
381 if (pcibios_unmap_io_space(bus
)) {
382 printk(KERN_ERR
"%s: failed to unmap bus range\n",
387 /* Remove the EADS bridge device itself */
389 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus
->self
));
390 eeh_remove_bus_device(bus
->self
);
391 pci_remove_bus_device(bus
->self
);
397 * dlpar_remove_slot - DLPAR remove an I/O Slot
398 * @drc_name: drc-name of newly added slot
400 * Remove the kernel and hotplug representations of an I/O Slot.
403 * -ENODEV Not a valid drc_name
404 * -EINVAL Slot already removed
405 * -ERESTARTSYS Signalled before obtaining lock
406 * -EIO Internal Error
408 int dlpar_remove_slot(char *drc_name
)
410 struct device_node
*dn
;
414 if (mutex_lock_interruptible(&rpadlpar_mutex
))
417 dn
= find_dlpar_node(drc_name
, &node_type
);
425 rc
= dlpar_remove_vio_slot(drc_name
, dn
);
428 rc
= dlpar_remove_phb(drc_name
, dn
);
431 rc
= dlpar_remove_pci_slot(drc_name
, dn
);
436 printk(KERN_INFO
"%s: slot %s removed\n", DLPAR_MODULE_NAME
, drc_name
);
438 mutex_unlock(&rpadlpar_mutex
);
442 static inline int is_dlpar_capable(void)
444 int rc
= rtas_token("ibm,configure-connector");
446 return (int) (rc
!= RTAS_UNKNOWN_SERVICE
);
449 int __init
rpadlpar_io_init(void)
453 if (!is_dlpar_capable()) {
454 printk(KERN_WARNING
"%s: partition not DLPAR capable\n",
459 rc
= dlpar_sysfs_init();
463 void rpadlpar_io_exit(void)
469 module_init(rpadlpar_io_init
);
470 module_exit(rpadlpar_io_exit
);
471 MODULE_LICENSE("GPL");