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 #define NODE_TYPE_VIO 1
29 #define NODE_TYPE_SLOT 2
30 #define NODE_TYPE_PHB 3
32 static struct device_node *find_php_slot_vio_node(char *drc_name)
34 struct device_node *child;
35 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
41 for (child = of_get_next_child(parent, NULL);
42 child; child = of_get_next_child(parent, child)) {
43 loc_code = get_property(child, "ibm,loc-code", NULL);
44 if (loc_code && !strncmp(loc_code, drc_name, strlen(drc_name)))
51 /* Find dlpar-capable pci node that contains the specified name and type */
52 static struct device_node *find_php_slot_pci_node(char *drc_name,
55 struct device_node *np = NULL;
60 while ((np = of_find_node_by_type(np, "pci"))) {
61 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
63 if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
70 static struct device_node *find_newly_added_node(char *drc_name, int *node_type)
72 struct device_node *dn;
74 dn = find_php_slot_pci_node(drc_name, "SLOT");
76 *node_type = NODE_TYPE_SLOT;
80 dn = find_php_slot_pci_node(drc_name, "PHB");
82 *node_type = NODE_TYPE_PHB;
86 dn = find_php_slot_vio_node(drc_name);
88 *node_type = NODE_TYPE_VIO;
95 static struct slot *find_slot(char *drc_name)
97 struct list_head *tmp, *n;
100 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
101 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
102 if (strcmp(slot->location, drc_name) == 0)
109 static void rpadlpar_claim_one_bus(struct pci_bus *b)
111 struct list_head *ld;
112 struct pci_bus *child_bus;
114 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
115 struct pci_dev *dev = pci_dev_b(ld);
118 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
119 struct resource *r = &dev->resource[i];
121 if (r->parent || !r->start || !r->flags)
123 rpaphp_claim_resource(dev, i);
127 list_for_each_entry(child_bus, &b->children, node)
128 rpadlpar_claim_one_bus(child_bus);
131 static int pci_add_secondary_bus(struct device_node *dn,
132 struct pci_dev *bridge_dev)
134 struct pci_controller *hose = dn->phb;
135 struct pci_bus *child;
138 /* Get busno of downstream bus */
139 pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
141 /* Allocate and add to children of bridge_dev->bus */
142 child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
144 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
148 sprintf(child->name, "PCI Bus #%02x", child->number);
150 /* Fixup subordinate bridge bases and resources */
151 pcibios_fixup_bus(child);
153 /* Claim new bus resources */
154 rpadlpar_claim_one_bus(bridge_dev->bus);
156 if (hose->last_busno < child->number)
157 hose->last_busno = child->number;
159 dn->bussubno = child->number;
161 /* ioremap() for child bus, which may or may not succeed */
162 remap_bus_range(child);
167 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
169 struct pci_controller *hose = dn->phb;
170 struct pci_dev *dev = NULL;
172 /* Scan phb bus for EADS device, adding new one to bus->devices */
173 if (!pci_scan_single_device(hose->bus, dn->devfn)) {
174 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
178 /* Add new devices to global lists. Register in proc, sysfs. */
179 pci_bus_add_devices(hose->bus);
181 /* Confirm new bridge dev was created */
182 dev = rpaphp_find_pci_dev(dn);
184 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
188 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
189 printk(KERN_ERR "%s: unexpected header type %d\n",
190 __FUNCTION__, dev->hdr_type);
194 if (pci_add_secondary_bus(dn, dev))
200 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
202 struct pci_bus *secondary_bus;
205 printk(KERN_ERR "%s: unexpected null device\n",
210 secondary_bus = bridge_dev->subordinate;
212 if (unmap_bus_range(secondary_bus)) {
213 printk(KERN_ERR "%s: failed to unmap bus range\n",
218 pci_remove_bus_device(bridge_dev);
222 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
227 dev = dlpar_pci_add_bus(dn);
229 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
237 static int dlpar_remove_root_bus(struct pci_controller *phb)
239 struct pci_bus *phb_bus;
243 if (!(list_empty(&phb_bus->children) &&
244 list_empty(&phb_bus->devices))) {
248 rc = pcibios_remove_root_bus(phb);
252 device_unregister(phb_bus->bridge);
253 pci_remove_bus(phb_bus);
258 static int dlpar_remove_phb(struct slot *slot)
260 struct pci_controller *phb;
261 struct device_node *dn;
266 printk(KERN_ERR "%s: unexpected NULL slot device node\n",
273 printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
278 if (rpaphp_remove_slot(slot)) {
279 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
280 __FUNCTION__, slot->location);
284 rc = dlpar_remove_root_bus(phb);
291 static int dlpar_add_phb(struct device_node *dn)
293 struct pci_controller *phb;
295 phb = init_phb_dynamic(dn);
303 * dlpar_add_slot - DLPAR add an I/O Slot
304 * @drc_name: drc-name of newly added slot
306 * Make the hotplug module and the kernel aware
307 * of a newly added I/O Slot.
310 * -ENODEV Not a valid drc_name
311 * -EINVAL Slot already added
312 * -ERESTARTSYS Signalled before obtaining lock
313 * -EIO Internal PCI Error
315 int dlpar_add_slot(char *drc_name)
317 struct device_node *dn = NULL;
321 if (down_interruptible(&rpadlpar_sem))
324 /* Check for existing hotplug slot */
325 if (find_slot(drc_name)) {
330 dn = find_newly_added_node(drc_name, &node_type);
338 /* Just add hotplug slot */
341 rc = dlpar_add_pci_slot(drc_name, dn);
344 rc = dlpar_add_phb(dn);
347 printk("%s: unexpected node type\n", __FUNCTION__);
351 if (!rc && rpaphp_add_slot(dn)) {
352 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
353 __FUNCTION__, drc_name);
362 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
363 * @drc_name: drc-name of newly added slot
365 * Remove the kernel and hotplug representations
369 * -EIO Internal Error
371 int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
373 /* Remove hotplug slot */
375 if (rpaphp_remove_slot(slot)) {
376 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
377 __FUNCTION__, drc_name);
384 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
385 * @drc_name: drc-name of newly added slot
387 * Remove the kernel and hotplug representations
391 * -ENODEV Not a valid drc_name
392 * -EIO Internal PCI Error
394 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
396 struct pci_dev *bridge_dev;
398 bridge_dev = slot->bridge;
400 printk(KERN_ERR "%s: unexpected null bridge device\n",
405 /* Remove hotplug slot */
406 if (rpaphp_remove_slot(slot)) {
407 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
408 __FUNCTION__, drc_name);
414 if (dlpar_pci_remove_bus(bridge_dev)) {
415 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
416 __FUNCTION__, drc_name);
423 * dlpar_remove_slot - DLPAR remove an I/O Slot
424 * @drc_name: drc-name of newly added slot
426 * Remove the kernel and hotplug representations
430 * -ENODEV Not a valid drc_name
431 * -EINVAL Slot already removed
432 * -ERESTARTSYS Signalled before obtaining lock
433 * -EIO Internal Error
435 int dlpar_remove_slot(char *drc_name)
440 if (down_interruptible(&rpadlpar_sem))
443 if (!find_php_slot_vio_node(drc_name) &&
444 !find_php_slot_pci_node(drc_name, "SLOT") &&
445 !find_php_slot_pci_node(drc_name, "PHB")) {
450 slot = find_slot(drc_name);
456 if (slot->type == PHB) {
457 rc = dlpar_remove_phb(slot);
459 switch (slot->dev_type) {
461 rc = dlpar_remove_pci_slot(slot, drc_name);
465 rc = dlpar_remove_vio_slot(slot, drc_name);
474 static inline int is_dlpar_capable(void)
476 int rc = rtas_token("ibm,configure-connector");
478 return (int) (rc != RTAS_UNKNOWN_SERVICE);
481 int __init rpadlpar_io_init(void)
485 if (!is_dlpar_capable()) {
486 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
491 rc = dlpar_sysfs_init();
495 void rpadlpar_io_exit(void)
501 module_init(rpadlpar_io_init);
502 module_exit(rpadlpar_io_exit);
503 MODULE_LICENSE("GPL");