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");
55 for (child = of_get_next_child(parent, NULL);
56 child; child = of_get_next_child(parent, child)) {
57 loc_code = get_property(child, "ibm,loc-code", NULL);
58 if (loc_code && !strcmp(loc_code, drc_name))
65 static struct device_node *find_php_slot_pci_node(char *drc_name)
67 struct device_node *np = NULL;
70 while ((np = of_find_node_by_type(np, "pci")))
71 if (is_hotplug_capable(np)) {
72 name = get_node_drc_name(np);
73 if (name && (!strcmp(drc_name, name)))
80 static struct slot *find_slot(char *drc_name)
82 struct list_head *tmp, *n;
85 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
86 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
87 if (strcmp(slot->location, drc_name) == 0)
94 static void rpadlpar_claim_one_bus(struct pci_bus *b)
97 struct pci_bus *child_bus;
99 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
100 struct pci_dev *dev = pci_dev_b(ld);
103 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
104 struct resource *r = &dev->resource[i];
106 if (r->parent || !r->start || !r->flags)
108 rpaphp_claim_resource(dev, i);
112 list_for_each_entry(child_bus, &b->children, node)
113 rpadlpar_claim_one_bus(child_bus);
116 static int pci_add_secondary_bus(struct device_node *dn,
117 struct pci_dev *bridge_dev)
119 struct pci_controller *hose = dn->phb;
120 struct pci_bus *child;
123 /* Get busno of downstream bus */
124 pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
126 /* Allocate and add to children of bridge_dev->bus */
127 child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
129 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
133 sprintf(child->name, "PCI Bus #%02x", child->number);
135 /* Fixup subordinate bridge bases and resources */
136 pcibios_fixup_bus(child);
138 /* Claim new bus resources */
139 rpadlpar_claim_one_bus(bridge_dev->bus);
141 if (hose->last_busno < child->number)
142 hose->last_busno = child->number;
144 dn->bussubno = child->number;
146 /* ioremap() for child bus */
147 if (remap_bus_range(child)) {
148 printk(KERN_ERR "%s: could not ioremap() child bus\n",
156 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
158 struct pci_controller *hose = dn->phb;
159 struct pci_dev *dev = NULL;
161 /* Scan phb bus for EADS device, adding new one to bus->devices */
162 if (!pci_scan_single_device(hose->bus, dn->devfn)) {
163 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
167 /* Add new devices to global lists. Register in proc, sysfs. */
168 pci_bus_add_devices(hose->bus);
170 /* Confirm new bridge dev was created */
171 dev = rpaphp_find_pci_dev(dn);
173 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
177 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
178 printk(KERN_ERR "%s: unexpected header type %d\n",
179 __FUNCTION__, dev->hdr_type);
183 if (pci_add_secondary_bus(dn, dev))
189 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
191 struct pci_bus *secondary_bus;
194 printk(KERN_ERR "%s: unexpected null device\n",
199 secondary_bus = bridge_dev->subordinate;
201 if (unmap_bus_range(secondary_bus)) {
202 printk(KERN_ERR "%s: failed to unmap bus range\n",
207 pci_remove_bus_device(bridge_dev);
211 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
216 dev = dlpar_pci_add_bus(dn);
218 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
227 * dlpar_add_slot - DLPAR add an I/O Slot
228 * @drc_name: drc-name of newly added slot
230 * Make the hotplug module and the kernel aware
231 * of a newly added I/O Slot.
234 * -ENODEV Not a valid drc_name
235 * -EINVAL Slot already added
236 * -ERESTARTSYS Signalled before obtaining lock
237 * -EIO Internal PCI Error
239 int dlpar_add_slot(char *drc_name)
241 struct device_node *dn;
244 if (down_interruptible(&rpadlpar_sem))
247 /* Check for existing hotplug slot */
248 if (find_slot(drc_name)) {
253 dn = find_php_slot_vio_node(drc_name);
255 dn = find_php_slot_pci_node(drc_name);
257 rc = dlpar_add_pci_slot(drc_name, dn);
264 /* Add hotplug slot for new VIOA or PCI */
265 if (!rc && rpaphp_add_slot(dn)) {
266 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
267 __FUNCTION__, drc_name);
276 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
277 * @drc_name: drc-name of newly added slot
279 * Remove the kernel and hotplug representations
283 * -EIO Internal Error
285 int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
287 /* Remove hotplug slot */
289 if (rpaphp_remove_slot(slot)) {
290 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
291 __FUNCTION__, drc_name);
298 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
299 * @drc_name: drc-name of newly added slot
301 * Remove the kernel and hotplug representations
305 * -ENODEV Not a valid drc_name
306 * -EIO Internal PCI Error
308 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
310 struct pci_dev *bridge_dev;
312 bridge_dev = slot->bridge;
314 printk(KERN_ERR "%s: unexpected null bridge device\n",
319 /* Remove hotplug slot */
320 if (rpaphp_remove_slot(slot)) {
321 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
322 __FUNCTION__, drc_name);
327 if (dlpar_pci_remove_bus(bridge_dev)) {
328 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
329 __FUNCTION__, drc_name);
336 * dlpar_remove_slot - DLPAR remove an I/O Slot
337 * @drc_name: drc-name of newly added slot
339 * Remove the kernel and hotplug representations
343 * -ENODEV Not a valid drc_name
344 * -EINVAL Slot already removed
345 * -ERESTARTSYS Signalled before obtaining lock
346 * -EIO Internal Error
348 int dlpar_remove_slot(char *drc_name)
353 if (down_interruptible(&rpadlpar_sem))
356 if (!find_php_slot_vio_node(drc_name) &&
357 !find_php_slot_pci_node(drc_name)) {
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");