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>
27 static DECLARE_MUTEX(rpadlpar_sem
);
29 #define DLPAR_MODULE_NAME "rpadlpar_io"
31 #define NODE_TYPE_VIO 1
32 #define NODE_TYPE_SLOT 2
33 #define NODE_TYPE_PHB 3
35 static struct device_node
*find_vio_slot_node(char *drc_name
)
37 struct device_node
*parent
= of_find_node_by_name(NULL
, "vdevice");
38 struct device_node
*dn
= NULL
;
45 while ((dn
= of_get_next_child(parent
, dn
))) {
46 rc
= rpaphp_get_drc_props(dn
, NULL
, &name
, NULL
, NULL
);
47 if ((rc
== 0) && (!strcmp(drc_name
, name
)))
54 /* Find dlpar-capable pci node that contains the specified name and type */
55 static struct device_node
*find_php_slot_pci_node(char *drc_name
,
58 struct device_node
*np
= NULL
;
63 while ((np
= of_find_node_by_type(np
, "pci"))) {
64 rc
= rpaphp_get_drc_props(np
, NULL
, &name
, &type
, NULL
);
66 if (!strcmp(drc_name
, name
) && !strcmp(drc_type
, type
))
73 static struct device_node
*find_dlpar_node(char *drc_name
, int *node_type
)
75 struct device_node
*dn
;
77 dn
= find_php_slot_pci_node(drc_name
, "SLOT");
79 *node_type
= NODE_TYPE_SLOT
;
83 dn
= find_php_slot_pci_node(drc_name
, "PHB");
85 *node_type
= NODE_TYPE_PHB
;
89 dn
= find_vio_slot_node(drc_name
);
91 *node_type
= NODE_TYPE_VIO
;
98 static struct slot
*find_slot(struct device_node
*dn
)
100 struct list_head
*tmp
, *n
;
103 list_for_each_safe(tmp
, n
, &rpaphp_slot_head
) {
104 slot
= list_entry(tmp
, struct slot
, rpaphp_slot_list
);
112 static void rpadlpar_claim_one_bus(struct pci_bus
*b
)
114 struct list_head
*ld
;
115 struct pci_bus
*child_bus
;
117 for (ld
= b
->devices
.next
; ld
!= &b
->devices
; ld
= ld
->next
) {
118 struct pci_dev
*dev
= pci_dev_b(ld
);
121 for (i
= 0; i
< PCI_NUM_RESOURCES
; i
++) {
122 struct resource
*r
= &dev
->resource
[i
];
124 if (r
->parent
|| !r
->start
|| !r
->flags
)
126 rpaphp_claim_resource(dev
, i
);
130 list_for_each_entry(child_bus
, &b
->children
, node
)
131 rpadlpar_claim_one_bus(child_bus
);
134 static int pci_add_secondary_bus(struct device_node
*dn
,
135 struct pci_dev
*bridge_dev
)
137 struct pci_dn
*pdn
= dn
->data
;
138 struct pci_controller
*hose
= pdn
->phb
;
139 struct pci_bus
*child
;
142 /* Get busno of downstream bus */
143 pci_read_config_byte(bridge_dev
, PCI_SECONDARY_BUS
, &sec_busno
);
145 /* Allocate and add to children of bridge_dev->bus */
146 child
= pci_add_new_bus(bridge_dev
->bus
, bridge_dev
, sec_busno
);
148 printk(KERN_ERR
"%s: could not add secondary bus\n", __FUNCTION__
);
152 sprintf(child
->name
, "PCI Bus #%02x", child
->number
);
154 /* Fixup subordinate bridge bases and resources */
155 pcibios_fixup_bus(child
);
157 /* Claim new bus resources */
158 rpadlpar_claim_one_bus(bridge_dev
->bus
);
160 if (hose
->last_busno
< child
->number
)
161 hose
->last_busno
= child
->number
;
163 pdn
->bussubno
= child
->number
;
165 /* ioremap() for child bus, which may or may not succeed */
166 remap_bus_range(child
);
171 static struct pci_dev
*dlpar_find_new_dev(struct pci_bus
*parent
,
172 struct device_node
*dev_dn
)
174 struct pci_dev
*tmp
= NULL
;
175 struct device_node
*child_dn
;
177 list_for_each_entry(tmp
, &parent
->devices
, bus_list
) {
178 child_dn
= pci_device_to_OF_node(tmp
);
179 if (child_dn
== dev_dn
)
185 static struct pci_dev
*dlpar_pci_add_bus(struct device_node
*dn
)
187 struct pci_dn
*pdn
= dn
->data
;
188 struct pci_controller
*hose
= pdn
->phb
;
189 struct pci_dev
*dev
= NULL
;
191 /* Scan phb bus for EADS device, adding new one to bus->devices */
192 if (!pci_scan_single_device(hose
->bus
, pdn
->devfn
)) {
193 printk(KERN_ERR
"%s: found no device on bus\n", __FUNCTION__
);
197 /* Add new devices to global lists. Register in proc, sysfs. */
198 pci_bus_add_devices(hose
->bus
);
200 /* Confirm new bridge dev was created */
201 dev
= dlpar_find_new_dev(hose
->bus
, dn
);
203 if (dev
->hdr_type
!= PCI_HEADER_TYPE_BRIDGE
) {
204 printk(KERN_ERR
"%s: unexpected header type %d\n",
205 __FUNCTION__
, dev
->hdr_type
);
209 if (pci_add_secondary_bus(dn
, dev
))
216 static int dlpar_add_pci_slot(char *drc_name
, struct device_node
*dn
)
221 if (rpaphp_find_pci_bus(dn
))
225 dev
= dlpar_pci_add_bus(dn
);
227 printk(KERN_ERR
"%s: unable to add bus %s\n", __FUNCTION__
,
233 rc
= rpaphp_config_pci_adapter(dev
->subordinate
);
235 printk(KERN_ERR
"%s: unable to enable slot %s\n",
236 __FUNCTION__
, drc_name
);
241 /* Add hotplug slot */
242 if (rpaphp_add_slot(dn
)) {
243 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
244 __FUNCTION__
, drc_name
);
250 static int dlpar_remove_root_bus(struct pci_controller
*phb
)
252 struct pci_bus
*phb_bus
;
256 if (!(list_empty(&phb_bus
->children
) &&
257 list_empty(&phb_bus
->devices
))) {
261 rc
= pcibios_remove_root_bus(phb
);
265 device_unregister(phb_bus
->bridge
);
266 pci_remove_bus(phb_bus
);
271 static int dlpar_remove_phb(char *drc_name
, struct device_node
*dn
)
277 if (!rpaphp_find_pci_bus(dn
))
280 slot
= find_slot(dn
);
282 /* Remove hotplug slot */
283 if (rpaphp_remove_slot(slot
)) {
285 "%s: unable to remove hotplug slot %s\n",
286 __FUNCTION__
, drc_name
);
292 BUG_ON(!pdn
|| !pdn
->phb
);
293 rc
= dlpar_remove_root_bus(pdn
->phb
);
302 static int dlpar_add_phb(char *drc_name
, struct device_node
*dn
)
304 struct pci_controller
*phb
;
306 if (PCI_DN(dn
)->phb
) {
307 /* PHB already exists */
311 phb
= init_phb_dynamic(dn
);
315 if (rpaphp_add_slot(dn
)) {
316 printk(KERN_ERR
"%s: unable to add hotplug slot %s\n",
317 __FUNCTION__
, drc_name
);
323 static int dlpar_add_vio_slot(char *drc_name
, struct device_node
*dn
)
325 if (vio_find_node(dn
))
328 if (!vio_register_device_node(dn
)) {
330 "%s: failed to register vio node %s\n",
331 __FUNCTION__
, drc_name
);
338 * dlpar_add_slot - DLPAR add an I/O Slot
339 * @drc_name: drc-name of newly added slot
341 * Make the hotplug module and the kernel aware
342 * of a newly added I/O Slot.
345 * -ENODEV Not a valid drc_name
346 * -EINVAL Slot already added
347 * -ERESTARTSYS Signalled before obtaining lock
348 * -EIO Internal PCI Error
350 int dlpar_add_slot(char *drc_name
)
352 struct device_node
*dn
= NULL
;
356 if (down_interruptible(&rpadlpar_sem
))
359 /* Find newly added node */
360 dn
= find_dlpar_node(drc_name
, &node_type
);
368 rc
= dlpar_add_vio_slot(drc_name
, dn
);
371 rc
= dlpar_add_pci_slot(drc_name
, dn
);
374 rc
= dlpar_add_phb(drc_name
, dn
);
378 printk(KERN_INFO
"%s: slot %s added\n", DLPAR_MODULE_NAME
, drc_name
);
385 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
386 * @drc_name: drc-name of newly added slot
388 * Remove the kernel and hotplug representations
392 * -EINVAL Vio dev doesn't exist
394 static int dlpar_remove_vio_slot(char *drc_name
, struct device_node
*dn
)
396 struct vio_dev
*vio_dev
;
398 vio_dev
= vio_find_node(dn
);
402 vio_unregister_device(vio_dev
);
407 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
408 * @drc_name: drc-name of newly added slot
410 * Remove the kernel and hotplug representations
414 * -ENODEV Not a valid drc_name
415 * -EIO Internal PCI Error
417 int dlpar_remove_pci_slot(char *drc_name
, struct device_node
*dn
)
422 bus
= rpaphp_find_pci_bus(dn
);
426 slot
= find_slot(dn
);
428 /* Remove hotplug slot */
429 if (rpaphp_remove_slot(slot
)) {
431 "%s: unable to remove hotplug slot %s\n",
432 __FUNCTION__
, drc_name
);
437 if (unmap_bus_range(bus
)) {
438 printk(KERN_ERR
"%s: failed to unmap bus range\n",
444 pci_remove_bus_device(bus
->self
);
449 * dlpar_remove_slot - DLPAR remove an I/O Slot
450 * @drc_name: drc-name of newly added slot
452 * Remove the kernel and hotplug representations
456 * -ENODEV Not a valid drc_name
457 * -EINVAL Slot already removed
458 * -ERESTARTSYS Signalled before obtaining lock
459 * -EIO Internal Error
461 int dlpar_remove_slot(char *drc_name
)
463 struct device_node
*dn
;
467 if (down_interruptible(&rpadlpar_sem
))
470 dn
= find_dlpar_node(drc_name
, &node_type
);
478 rc
= dlpar_remove_vio_slot(drc_name
, dn
);
481 rc
= dlpar_remove_phb(drc_name
, dn
);
484 rc
= dlpar_remove_pci_slot(drc_name
, dn
);
487 printk(KERN_INFO
"%s: slot %s removed\n", DLPAR_MODULE_NAME
, drc_name
);
493 static inline int is_dlpar_capable(void)
495 int rc
= rtas_token("ibm,configure-connector");
497 return (int) (rc
!= RTAS_UNKNOWN_SERVICE
);
500 int __init
rpadlpar_io_init(void)
504 if (!is_dlpar_capable()) {
505 printk(KERN_WARNING
"%s: partition not DLPAR capable\n",
510 rc
= dlpar_sysfs_init();
514 void rpadlpar_io_exit(void)
520 module_init(rpadlpar_io_init
);
521 module_exit(rpadlpar_io_exit
);
522 MODULE_LICENSE("GPL");