[PATCH] dvb: fix NULL pointer dereference when loading the budget-av module
[linux-2.6/kvm.git] / drivers / pci / hotplug / rpadlpar_core.c
blobad1017da8656c350c5b6390aa25e74d1ac7e4b0d
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 <asm/vio.h>
23 #include "../pci.h"
24 #include "rpaphp.h"
25 #include "rpadlpar.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;
39 char *name;
40 int rc;
42 if (!parent)
43 return 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)))
48 break;
51 return dn;
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,
56 char *drc_type)
58 struct device_node *np = NULL;
59 char *name;
60 char *type;
61 int rc;
63 while ((np = of_find_node_by_type(np, "pci"))) {
64 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
65 if (rc == 0)
66 if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
67 break;
70 return np;
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");
78 if (dn) {
79 *node_type = NODE_TYPE_SLOT;
80 return dn;
83 dn = find_php_slot_pci_node(drc_name, "PHB");
84 if (dn) {
85 *node_type = NODE_TYPE_PHB;
86 return dn;
89 dn = find_vio_slot_node(drc_name);
90 if (dn) {
91 *node_type = NODE_TYPE_VIO;
92 return dn;
95 return NULL;
98 static struct slot *find_slot(struct device_node *dn)
100 struct list_head *tmp, *n;
101 struct slot *slot;
103 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
104 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
105 if (slot->dn == dn)
106 return slot;
109 return NULL;
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);
119 int i;
121 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
122 struct resource *r = &dev->resource[i];
124 if (r->parent || !r->start || !r->flags)
125 continue;
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;
140 u8 sec_busno;
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);
147 if (!child) {
148 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
149 return -ENOMEM;
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);
168 return 0;
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)
180 return tmp;
182 return NULL;
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__);
194 return NULL;
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);
202 if (dev) {
203 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
204 printk(KERN_ERR "%s: unexpected header type %d\n",
205 __FUNCTION__, dev->hdr_type);
206 return NULL;
209 if (pci_add_secondary_bus(dn, dev))
210 return NULL;
213 return dev;
216 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
218 struct pci_dev *dev;
219 int rc;
221 if (rpaphp_find_pci_bus(dn))
222 return -EINVAL;
224 /* Add pci bus */
225 dev = dlpar_pci_add_bus(dn);
226 if (!dev) {
227 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
228 drc_name);
229 return -EIO;
232 if (dn->child) {
233 rc = rpaphp_config_pci_adapter(dev->subordinate);
234 if (rc < 0) {
235 printk(KERN_ERR "%s: unable to enable slot %s\n",
236 __FUNCTION__, drc_name);
237 return -EIO;
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);
245 return -EIO;
247 return 0;
250 static int dlpar_remove_root_bus(struct pci_controller *phb)
252 struct pci_bus *phb_bus;
253 int rc;
255 phb_bus = phb->bus;
256 if (!(list_empty(&phb_bus->children) &&
257 list_empty(&phb_bus->devices))) {
258 return -EBUSY;
261 rc = pcibios_remove_root_bus(phb);
262 if (rc)
263 return -EIO;
265 device_unregister(phb_bus->bridge);
266 pci_remove_bus(phb_bus);
268 return 0;
271 static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
273 struct slot *slot;
274 struct pci_dn *pdn;
275 int rc = 0;
277 if (!rpaphp_find_pci_bus(dn))
278 return -EINVAL;
280 slot = find_slot(dn);
281 if (slot) {
282 /* Remove hotplug slot */
283 if (rpaphp_remove_slot(slot)) {
284 printk(KERN_ERR
285 "%s: unable to remove hotplug slot %s\n",
286 __FUNCTION__, drc_name);
287 return -EIO;
291 pdn = dn->data;
292 BUG_ON(!pdn || !pdn->phb);
293 rc = dlpar_remove_root_bus(pdn->phb);
294 if (rc < 0)
295 return rc;
297 pdn->phb = NULL;
299 return 0;
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 */
308 return -EINVAL;
311 phb = init_phb_dynamic(dn);
312 if (!phb)
313 return -EIO;
315 if (rpaphp_add_slot(dn)) {
316 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
317 __FUNCTION__, drc_name);
318 return -EIO;
320 return 0;
323 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
325 if (vio_find_node(dn))
326 return -EINVAL;
328 if (!vio_register_device_node(dn)) {
329 printk(KERN_ERR
330 "%s: failed to register vio node %s\n",
331 __FUNCTION__, drc_name);
332 return -EIO;
334 return 0;
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.
343 * Return Codes -
344 * 0 Success
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;
353 int node_type;
354 int rc = -EIO;
356 if (down_interruptible(&rpadlpar_sem))
357 return -ERESTARTSYS;
359 /* Find newly added node */
360 dn = find_dlpar_node(drc_name, &node_type);
361 if (!dn) {
362 rc = -ENODEV;
363 goto exit;
366 switch (node_type) {
367 case NODE_TYPE_VIO:
368 rc = dlpar_add_vio_slot(drc_name, dn);
369 break;
370 case NODE_TYPE_SLOT:
371 rc = dlpar_add_pci_slot(drc_name, dn);
372 break;
373 case NODE_TYPE_PHB:
374 rc = dlpar_add_phb(drc_name, dn);
375 break;
378 printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
379 exit:
380 up(&rpadlpar_sem);
381 return rc;
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
389 * of an I/O Slot.
390 * Return Codes:
391 * 0 Success
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);
399 if (!vio_dev)
400 return -EINVAL;
402 vio_unregister_device(vio_dev);
403 return 0;
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
411 * of a PCI I/O Slot.
412 * Return Codes:
413 * 0 Success
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)
419 struct pci_bus *bus;
420 struct slot *slot;
422 bus = rpaphp_find_pci_bus(dn);
423 if (!bus)
424 return -EINVAL;
426 slot = find_slot(dn);
427 if (slot) {
428 /* Remove hotplug slot */
429 if (rpaphp_remove_slot(slot)) {
430 printk(KERN_ERR
431 "%s: unable to remove hotplug slot %s\n",
432 __FUNCTION__, drc_name);
433 return -EIO;
437 if (unmap_bus_range(bus)) {
438 printk(KERN_ERR "%s: failed to unmap bus range\n",
439 __FUNCTION__);
440 return -ERANGE;
443 BUG_ON(!bus->self);
444 pci_remove_bus_device(bus->self);
445 return 0;
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
453 * of an I/O Slot.
454 * Return Codes:
455 * 0 Success
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;
464 int node_type;
465 int rc = 0;
467 if (down_interruptible(&rpadlpar_sem))
468 return -ERESTARTSYS;
470 dn = find_dlpar_node(drc_name, &node_type);
471 if (!dn) {
472 rc = -ENODEV;
473 goto exit;
476 switch (node_type) {
477 case NODE_TYPE_VIO:
478 rc = dlpar_remove_vio_slot(drc_name, dn);
479 break;
480 case NODE_TYPE_PHB:
481 rc = dlpar_remove_phb(drc_name, dn);
482 break;
483 case NODE_TYPE_SLOT:
484 rc = dlpar_remove_pci_slot(drc_name, dn);
485 break;
487 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
488 exit:
489 up(&rpadlpar_sem);
490 return rc;
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)
502 int rc = 0;
504 if (!is_dlpar_capable()) {
505 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
506 __FUNCTION__);
507 return -EPERM;
510 rc = dlpar_sysfs_init();
511 return rc;
514 void rpadlpar_io_exit(void)
516 dlpar_sysfs_exit();
517 return;
520 module_init(rpadlpar_io_init);
521 module_exit(rpadlpar_io_exit);
522 MODULE_LICENSE("GPL");