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>
26 static DECLARE_MUTEX(rpadlpar_sem);
28 static struct device_node *find_php_slot_vio_node(char *drc_name)
30 struct device_node *child;
31 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
37 for (child = of_get_next_child(parent, NULL);
38 child; child = of_get_next_child(parent, child)) {
39 loc_code = get_property(child, "ibm,loc-code", NULL);
40 if (loc_code && !strncmp(loc_code, drc_name, strlen(drc_name)))
47 static struct device_node *find_php_slot_pci_node(char *drc_name)
49 struct device_node *np = NULL;
52 while ((np = of_find_node_by_type(np, "pci")))
53 if (is_hotplug_capable(np)) {
54 name = rpaphp_get_drc_name(np);
55 if (name && (!strcmp(drc_name, name)))
62 static struct slot *find_slot(char *drc_name)
64 struct list_head *tmp, *n;
67 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
68 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
69 if (strcmp(slot->location, drc_name) == 0)
76 static void rpadlpar_claim_one_bus(struct pci_bus *b)
79 struct pci_bus *child_bus;
81 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
82 struct pci_dev *dev = pci_dev_b(ld);
85 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
86 struct resource *r = &dev->resource[i];
88 if (r->parent || !r->start || !r->flags)
90 rpaphp_claim_resource(dev, i);
94 list_for_each_entry(child_bus, &b->children, node)
95 rpadlpar_claim_one_bus(child_bus);
98 static int pci_add_secondary_bus(struct device_node *dn,
99 struct pci_dev *bridge_dev)
101 struct pci_controller *hose = dn->phb;
102 struct pci_bus *child;
105 /* Get busno of downstream bus */
106 pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
108 /* Allocate and add to children of bridge_dev->bus */
109 child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
111 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
115 sprintf(child->name, "PCI Bus #%02x", child->number);
117 /* Fixup subordinate bridge bases and resources */
118 pcibios_fixup_bus(child);
120 /* Claim new bus resources */
121 rpadlpar_claim_one_bus(bridge_dev->bus);
123 if (hose->last_busno < child->number)
124 hose->last_busno = child->number;
126 dn->bussubno = child->number;
128 /* ioremap() for child bus */
129 if (remap_bus_range(child)) {
130 printk(KERN_ERR "%s: could not ioremap() child bus\n",
138 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
140 struct pci_controller *hose = dn->phb;
141 struct pci_dev *dev = NULL;
143 /* Scan phb bus for EADS device, adding new one to bus->devices */
144 if (!pci_scan_single_device(hose->bus, dn->devfn)) {
145 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
149 /* Add new devices to global lists. Register in proc, sysfs. */
150 pci_bus_add_devices(hose->bus);
152 /* Confirm new bridge dev was created */
153 dev = rpaphp_find_pci_dev(dn);
155 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
159 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
160 printk(KERN_ERR "%s: unexpected header type %d\n",
161 __FUNCTION__, dev->hdr_type);
165 if (pci_add_secondary_bus(dn, dev))
171 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
173 struct pci_bus *secondary_bus;
176 printk(KERN_ERR "%s: unexpected null device\n",
181 secondary_bus = bridge_dev->subordinate;
183 if (unmap_bus_range(secondary_bus)) {
184 printk(KERN_ERR "%s: failed to unmap bus range\n",
189 pci_remove_bus_device(bridge_dev);
193 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
198 dev = dlpar_pci_add_bus(dn);
200 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
209 * dlpar_add_slot - DLPAR add an I/O Slot
210 * @drc_name: drc-name of newly added slot
212 * Make the hotplug module and the kernel aware
213 * of a newly added I/O Slot.
216 * -ENODEV Not a valid drc_name
217 * -EINVAL Slot already added
218 * -ERESTARTSYS Signalled before obtaining lock
219 * -EIO Internal PCI Error
221 int dlpar_add_slot(char *drc_name)
223 struct device_node *dn;
226 if (down_interruptible(&rpadlpar_sem))
229 /* Check for existing hotplug slot */
230 if (find_slot(drc_name)) {
235 dn = find_php_slot_vio_node(drc_name);
237 dn = find_php_slot_pci_node(drc_name);
239 rc = dlpar_add_pci_slot(drc_name, dn);
246 /* Add hotplug slot for new VIOA or PCI */
247 if (!rc && rpaphp_add_slot(dn)) {
248 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
249 __FUNCTION__, drc_name);
258 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
259 * @drc_name: drc-name of newly added slot
261 * Remove the kernel and hotplug representations
265 * -EIO Internal Error
267 int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
269 /* Remove hotplug slot */
271 if (rpaphp_remove_slot(slot)) {
272 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
273 __FUNCTION__, drc_name);
280 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
281 * @drc_name: drc-name of newly added slot
283 * Remove the kernel and hotplug representations
287 * -ENODEV Not a valid drc_name
288 * -EIO Internal PCI Error
290 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
292 struct pci_dev *bridge_dev;
294 bridge_dev = slot->bridge;
296 printk(KERN_ERR "%s: unexpected null bridge device\n",
301 /* Remove hotplug slot */
302 if (rpaphp_remove_slot(slot)) {
303 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
304 __FUNCTION__, drc_name);
310 if (dlpar_pci_remove_bus(bridge_dev)) {
311 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
312 __FUNCTION__, drc_name);
319 * dlpar_remove_slot - DLPAR remove an I/O Slot
320 * @drc_name: drc-name of newly added slot
322 * Remove the kernel and hotplug representations
326 * -ENODEV Not a valid drc_name
327 * -EINVAL Slot already removed
328 * -ERESTARTSYS Signalled before obtaining lock
329 * -EIO Internal Error
331 int dlpar_remove_slot(char *drc_name)
336 if (down_interruptible(&rpadlpar_sem))
339 if (!find_php_slot_vio_node(drc_name) &&
340 !find_php_slot_pci_node(drc_name)) {
345 slot = find_slot(drc_name);
351 switch (slot->dev_type) {
353 rc = dlpar_remove_pci_slot(slot, drc_name);
357 rc = dlpar_remove_vio_slot(slot, drc_name);
368 static inline int is_dlpar_capable(void)
370 int rc = rtas_token("ibm,configure-connector");
372 return (int) (rc != RTAS_UNKNOWN_SERVICE);
375 int __init rpadlpar_io_init(void)
379 if (!is_dlpar_capable()) {
380 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
385 rc = dlpar_sysfs_init();
389 void rpadlpar_io_exit(void)
395 module_init(rpadlpar_io_init);
396 module_exit(rpadlpar_io_exit);
397 MODULE_LICENSE("GPL");