[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / pci / hotplug / rpadlpar_core.c
blob86b384e427178e91a59911df426f60342343f808
1 /*
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>
8 * October 2003
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>
21 #include <asm/rtas.h>
22 #include "../pci.h"
23 #include "rpaphp.h"
24 #include "rpadlpar.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");
36 char *loc_code;
38 if (!parent)
39 return NULL;
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)))
45 return child;
48 return NULL;
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,
53 char *drc_type)
55 struct device_node *np = NULL;
56 char *name;
57 char *type;
58 int rc;
60 while ((np = of_find_node_by_type(np, "pci"))) {
61 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
62 if (rc == 0)
63 if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
64 break;
67 return np;
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");
75 if (dn) {
76 *node_type = NODE_TYPE_SLOT;
77 return dn;
80 dn = find_php_slot_pci_node(drc_name, "PHB");
81 if (dn) {
82 *node_type = NODE_TYPE_PHB;
83 return dn;
86 dn = find_php_slot_vio_node(drc_name);
87 if (dn) {
88 *node_type = NODE_TYPE_VIO;
89 return dn;
92 return NULL;
95 static struct slot *find_slot(char *drc_name)
97 struct list_head *tmp, *n;
98 struct slot *slot;
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)
103 return slot;
106 return NULL;
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);
116 int i;
118 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
119 struct resource *r = &dev->resource[i];
121 if (r->parent || !r->start || !r->flags)
122 continue;
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;
136 u8 sec_busno;
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);
143 if (!child) {
144 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
145 return -ENOMEM;
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);
164 return 0;
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__);
175 return NULL;
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);
183 if (!dev) {
184 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
185 return NULL;
188 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
189 printk(KERN_ERR "%s: unexpected header type %d\n",
190 __FUNCTION__, dev->hdr_type);
191 return NULL;
194 if (pci_add_secondary_bus(dn, dev))
195 return NULL;
197 return dev;
200 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
202 struct pci_bus *secondary_bus;
204 if (!bridge_dev) {
205 printk(KERN_ERR "%s: unexpected null device\n",
206 __FUNCTION__);
207 return -EINVAL;
210 secondary_bus = bridge_dev->subordinate;
212 if (unmap_bus_range(secondary_bus)) {
213 printk(KERN_ERR "%s: failed to unmap bus range\n",
214 __FUNCTION__);
215 return -ERANGE;
218 pci_remove_bus_device(bridge_dev);
219 return 0;
222 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
224 struct pci_dev *dev;
226 /* Add pci bus */
227 dev = dlpar_pci_add_bus(dn);
228 if (!dev) {
229 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
230 drc_name);
231 return -EIO;
234 return 0;
237 static int dlpar_remove_root_bus(struct pci_controller *phb)
239 struct pci_bus *phb_bus;
240 int rc;
242 phb_bus = phb->bus;
243 if (!(list_empty(&phb_bus->children) &&
244 list_empty(&phb_bus->devices))) {
245 return -EBUSY;
248 rc = pcibios_remove_root_bus(phb);
249 if (rc)
250 return -EIO;
252 device_unregister(phb_bus->bridge);
253 pci_remove_bus(phb_bus);
255 return 0;
258 static int dlpar_remove_phb(struct slot *slot)
260 struct pci_controller *phb;
261 struct device_node *dn;
262 int rc = 0;
264 dn = slot->dn;
265 if (!dn) {
266 printk(KERN_ERR "%s: unexpected NULL slot device node\n",
267 __FUNCTION__);
268 return -EIO;
271 phb = dn->phb;
272 if (!phb) {
273 printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
274 __FUNCTION__);
275 return -EIO;
278 if (rpaphp_remove_slot(slot)) {
279 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
280 __FUNCTION__, slot->location);
281 return -EIO;
284 rc = dlpar_remove_root_bus(phb);
285 if (rc < 0)
286 return rc;
288 return 0;
291 static int dlpar_add_phb(struct device_node *dn)
293 struct pci_controller *phb;
295 phb = init_phb_dynamic(dn);
296 if (!phb)
297 return -EINVAL;
299 return 0;
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.
308 * Return Codes -
309 * 0 Success
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;
318 int node_type;
319 int rc = 0;
321 if (down_interruptible(&rpadlpar_sem))
322 return -ERESTARTSYS;
324 /* Check for existing hotplug slot */
325 if (find_slot(drc_name)) {
326 rc = -EINVAL;
327 goto exit;
330 dn = find_newly_added_node(drc_name, &node_type);
331 if (!dn) {
332 rc = -ENODEV;
333 goto exit;
336 switch (node_type) {
337 case NODE_TYPE_VIO:
338 /* Just add hotplug slot */
339 break;
340 case NODE_TYPE_SLOT:
341 rc = dlpar_add_pci_slot(drc_name, dn);
342 break;
343 case NODE_TYPE_PHB:
344 rc = dlpar_add_phb(dn);
345 break;
346 default:
347 printk("%s: unexpected node type\n", __FUNCTION__);
348 return -EIO;
351 if (!rc && rpaphp_add_slot(dn)) {
352 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
353 __FUNCTION__, drc_name);
354 rc = -EIO;
356 exit:
357 up(&rpadlpar_sem);
358 return rc;
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
366 * of an I/O Slot.
367 * Return Codes:
368 * 0 Success
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);
378 return -EIO;
380 return 0;
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
388 * of a PCI I/O Slot.
389 * Return Codes:
390 * 0 Success
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;
399 if (!bridge_dev) {
400 printk(KERN_ERR "%s: unexpected null bridge device\n",
401 __FUNCTION__);
402 return -EIO;
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);
409 return -EIO;
412 /* Remove pci bus */
414 if (dlpar_pci_remove_bus(bridge_dev)) {
415 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
416 __FUNCTION__, drc_name);
417 return -EIO;
419 return 0;
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
427 * of an I/O Slot.
428 * Return Codes:
429 * 0 Success
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)
437 struct slot *slot;
438 int rc = 0;
440 if (down_interruptible(&rpadlpar_sem))
441 return -ERESTARTSYS;
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")) {
446 rc = -ENODEV;
447 goto exit;
450 slot = find_slot(drc_name);
451 if (!slot) {
452 rc = -EINVAL;
453 goto exit;
456 if (slot->type == PHB) {
457 rc = dlpar_remove_phb(slot);
458 } else {
459 switch (slot->dev_type) {
460 case PCI_DEV:
461 rc = dlpar_remove_pci_slot(slot, drc_name);
462 break;
464 case VIO_DEV:
465 rc = dlpar_remove_vio_slot(slot, drc_name);
466 break;
469 exit:
470 up(&rpadlpar_sem);
471 return rc;
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)
483 int rc = 0;
485 if (!is_dlpar_capable()) {
486 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
487 __FUNCTION__);
488 return -EPERM;
491 rc = dlpar_sysfs_init();
492 return rc;
495 void rpadlpar_io_exit(void)
497 dlpar_sysfs_exit();
498 return;
501 module_init(rpadlpar_io_init);
502 module_exit(rpadlpar_io_exit);
503 MODULE_LICENSE("GPL");