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 <asm/pci-bridge.h>
20 #include <asm/semaphore.h>
25 static DECLARE_MUTEX(rpadlpar_sem);
27 static inline int is_hotplug_capable(struct device_node *dn)
29 unsigned char *ptr = get_property(dn, "ibm,fw-pci-hot-plug-ctrl", NULL);
31 return (int) (ptr != NULL);
34 static char *get_node_drc_name(struct device_node *dn)
39 drc_names = (int *) get_property(dn, "ibm,drc-names", NULL);
41 ptr = (char *) &drc_names[1];
46 static struct device_node *find_php_slot_vio_node(char *drc_name)
48 struct device_node *child;
49 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
54 for (child = of_get_next_child(parent, NULL);
55 child; child = of_get_next_child(parent, child)) {
59 loc_code = get_property(child, "ibm,loc-code", NULL);
60 if (loc_code && !strcmp(loc_code, drc_name))
67 static struct device_node *find_php_slot_pci_node(char *drc_name)
69 struct device_node *np = NULL;
72 while ((np = of_find_node_by_type(np, "pci")))
73 if (is_hotplug_capable(np)) {
74 name = get_node_drc_name(np);
75 if (name && (!strcmp(drc_name, name)))
82 static struct slot *find_slot(char *drc_name)
84 struct list_head *tmp, *n;
87 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
88 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
89 if (strcmp(slot->location, drc_name) == 0)
96 static void rpadlpar_claim_one_bus(struct pci_bus *b)
99 struct pci_bus *child_bus;
101 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
102 struct pci_dev *dev = pci_dev_b(ld);
105 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
106 struct resource *r = &dev->resource[i];
108 if (r->parent || !r->start || !r->flags)
110 rpaphp_claim_resource(dev, i);
114 list_for_each_entry(child_bus, &b->children, node)
115 rpadlpar_claim_one_bus(child_bus);
118 static int pci_add_secondary_bus(struct device_node *dn,
119 struct pci_dev *bridge_dev)
121 struct pci_controller *hose = dn->phb;
122 struct pci_bus *child;
125 /* Get busno of downstream bus */
126 pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
128 /* Allocate and add to children of bridge_dev->bus */
129 child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
131 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
135 sprintf(child->name, "PCI Bus #%02x", child->number);
137 /* Fixup subordinate bridge bases and resources */
138 pcibios_fixup_bus(child);
140 /* Claim new bus resources */
141 rpadlpar_claim_one_bus(bridge_dev->bus);
143 if (hose->last_busno < child->number)
144 hose->last_busno = child->number;
146 dn->bussubno = child->number;
148 /* ioremap() for child bus */
149 if (remap_bus_range(child)) {
150 printk(KERN_ERR "%s: could not ioremap() child bus\n",
158 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
160 struct pci_controller *hose = dn->phb;
161 struct pci_dev *dev = NULL;
163 /* Scan phb bus for EADS device, adding new one to bus->devices */
164 if (!pci_scan_single_device(hose->bus, dn->devfn)) {
165 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
169 /* Add new devices to global lists. Register in proc, sysfs. */
170 pci_bus_add_devices(hose->bus);
172 /* Confirm new bridge dev was created */
173 dev = rpaphp_find_pci_dev(dn);
175 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
179 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
180 printk(KERN_ERR "%s: unexpected header type %d\n",
181 __FUNCTION__, dev->hdr_type);
185 if (pci_add_secondary_bus(dn, dev))
191 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
193 struct pci_bus *secondary_bus;
196 printk(KERN_ERR "%s: unexpected null device\n",
201 secondary_bus = bridge_dev->subordinate;
203 if (unmap_bus_range(secondary_bus)) {
204 printk(KERN_ERR "%s: failed to unmap bus range\n",
209 pci_remove_bus_device(bridge_dev);
213 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
218 dev = dlpar_pci_add_bus(dn);
220 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
229 * dlpar_add_slot - DLPAR add an I/O Slot
230 * @drc_name: drc-name of newly added slot
232 * Make the hotplug module and the kernel aware
233 * of a newly added I/O Slot.
236 * -ENODEV Not a valid drc_name
237 * -EINVAL Slot already added
238 * -ERESTARTSYS Signalled before obtaining lock
239 * -EIO Internal PCI Error
241 int dlpar_add_slot(char *drc_name)
243 struct device_node *dn;
246 if (down_interruptible(&rpadlpar_sem))
249 /* Check for existing hotplug slot */
250 if (find_slot(drc_name)) {
255 dn = find_php_slot_vio_node(drc_name);
257 dn = find_php_slot_pci_node(drc_name);
259 rc = dlpar_add_pci_slot(drc_name, dn);
266 /* Add hotplug slot for new VIOA or PCI */
267 if (!rc && rpaphp_add_slot(dn)) {
268 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
269 __FUNCTION__, drc_name);
278 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
279 * @drc_name: drc-name of newly added slot
281 * Remove the kernel and hotplug representations
285 * -EIO Internal Error
287 int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
289 /* Remove hotplug slot */
291 if (rpaphp_remove_slot(slot)) {
292 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
293 __FUNCTION__, drc_name);
300 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
301 * @drc_name: drc-name of newly added slot
303 * Remove the kernel and hotplug representations
307 * -ENODEV Not a valid drc_name
308 * -EIO Internal PCI Error
310 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
312 struct device_node *dn = find_php_slot_pci_node(drc_name);
313 struct pci_dev *bridge_dev;
318 bridge_dev = slot->bridge;
320 printk(KERN_ERR "%s: unexpected null bridge device\n",
325 /* Remove hotplug slot */
326 if (rpaphp_remove_slot(slot)) {
327 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
328 __FUNCTION__, drc_name);
333 if (dlpar_pci_remove_bus(bridge_dev)) {
334 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
335 __FUNCTION__, drc_name);
342 * dlpar_remove_slot - DLPAR remove an I/O Slot
343 * @drc_name: drc-name of newly added slot
345 * Remove the kernel and hotplug representations
349 * -ENODEV Not a valid drc_name
350 * -EINVAL Slot already removed
351 * -ERESTARTSYS Signalled before obtaining lock
352 * -EIO Internal Error
354 int dlpar_remove_slot(char *drc_name)
359 if (down_interruptible(&rpadlpar_sem))
362 slot = find_slot(drc_name);
368 switch (slot->dev_type) {
370 rc = dlpar_remove_pci_slot(slot, drc_name);
374 rc = dlpar_remove_vio_slot(slot, drc_name);
385 static inline int is_dlpar_capable(void)
387 int rc = rtas_token("ibm,configure-connector");
389 return (int) (rc != RTAS_UNKNOWN_SERVICE);
392 int __init rpadlpar_io_init(void)
396 if (!is_dlpar_capable()) {
397 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
402 rc = dlpar_sysfs_init();
406 void rpadlpar_io_exit(void)
412 module_init(rpadlpar_io_init);
413 module_exit(rpadlpar_io_exit);
414 MODULE_LICENSE("GPL");