d7f1319f167a058e2ae2e1662611f0c13bda9ec0
[pandora-kernel.git] / drivers / pci / hotplug / rpadlpar_core.c
1 /*
2  * Interface for Dynamic Logical Partitioning of I/O Slots on
3  * RPA-compliant PPC64 platform.
4  *
5  * John Rose <johnrose@austin.ibm.com>
6  * Linda Xie <lxie@us.ibm.com>
7  *
8  * October 2003
9  *
10  * Copyright (C) 2003 IBM.
11  *
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.
16  */
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"
26
27 static DECLARE_MUTEX(rpadlpar_sem);
28
29 #define NODE_TYPE_VIO  1
30 #define NODE_TYPE_SLOT 2
31 #define NODE_TYPE_PHB  3
32
33 static struct device_node *find_vio_slot_node(char *drc_name)
34 {
35         struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
36         struct device_node *dn = NULL;
37         char *name;
38         int rc;
39
40         if (!parent)
41                 return NULL;
42
43         while ((dn = of_get_next_child(parent, dn))) {
44                 rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
45                 if ((rc == 0) && (!strcmp(drc_name, name)))
46                         break;
47         }
48
49         return dn;
50 }
51
52 /* Find dlpar-capable pci node that contains the specified name and type */
53 static struct device_node *find_php_slot_pci_node(char *drc_name,
54                                                   char *drc_type)
55 {
56         struct device_node *np = NULL;
57         char *name;
58         char *type;
59         int rc;
60
61         while ((np = of_find_node_by_type(np, "pci"))) {
62                 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
63                 if (rc == 0)
64                         if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
65                                 break;
66         }
67
68         return np;
69 }
70
71 static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
72 {
73         struct device_node *dn;
74
75         dn = find_php_slot_pci_node(drc_name, "SLOT");
76         if (dn) {
77                 *node_type = NODE_TYPE_SLOT;
78                 return dn;
79         }
80
81         dn = find_php_slot_pci_node(drc_name, "PHB");
82         if (dn) {
83                 *node_type = NODE_TYPE_PHB;
84                 return dn;
85         }
86
87         dn = find_vio_slot_node(drc_name);
88         if (dn) {
89                 *node_type = NODE_TYPE_VIO;
90                 return dn;
91         }
92
93         return NULL;
94 }
95
96 static struct slot *find_slot(char *drc_name)
97 {
98         struct list_head *tmp, *n;
99         struct slot *slot;
100
101         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
102                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
103                 if (strcmp(slot->location, drc_name) == 0)
104                         return slot;
105         }
106
107         return NULL;
108 }
109
110 static void rpadlpar_claim_one_bus(struct pci_bus *b)
111 {
112         struct list_head *ld;
113         struct pci_bus *child_bus;
114
115         for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
116                 struct pci_dev *dev = pci_dev_b(ld);
117                 int i;
118
119                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
120                         struct resource *r = &dev->resource[i];
121
122                         if (r->parent || !r->start || !r->flags)
123                                 continue;
124                         rpaphp_claim_resource(dev, i);
125                 }
126         }
127
128         list_for_each_entry(child_bus, &b->children, node)
129                 rpadlpar_claim_one_bus(child_bus);
130 }
131
132 static int pci_add_secondary_bus(struct device_node *dn,
133                 struct pci_dev *bridge_dev)
134 {
135         struct pci_controller *hose = dn->phb;
136         struct pci_bus *child;
137         u8 sec_busno;
138
139         /* Get busno of downstream bus */
140         pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
141
142         /* Allocate and add to children of bridge_dev->bus */
143         child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
144         if (!child) {
145                 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
146                 return -ENOMEM;
147         }
148
149         sprintf(child->name, "PCI Bus #%02x", child->number);
150
151         /* Fixup subordinate bridge bases and resources */
152         pcibios_fixup_bus(child);
153
154         /* Claim new bus resources */
155         rpadlpar_claim_one_bus(bridge_dev->bus);
156
157         if (hose->last_busno < child->number)
158                 hose->last_busno = child->number;
159
160         dn->bussubno = child->number;
161
162         /* ioremap() for child bus, which may or may not succeed */
163         remap_bus_range(child);
164
165         return 0;
166 }
167
168 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
169 {
170         struct pci_controller *hose = dn->phb;
171         struct pci_dev *dev = NULL;
172
173         /* Scan phb bus for EADS device, adding new one to bus->devices */
174         if (!pci_scan_single_device(hose->bus, dn->devfn)) {
175                 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
176                 return NULL;
177         }
178
179         /* Add new devices to global lists.  Register in proc, sysfs. */
180         pci_bus_add_devices(hose->bus);
181
182         /* Confirm new bridge dev was created */
183         dev = rpaphp_find_pci_dev(dn);
184         if (!dev) {
185                 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
186                 return NULL;
187         }
188
189         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
190                 printk(KERN_ERR "%s: unexpected header type %d\n",
191                         __FUNCTION__, dev->hdr_type);
192                 return NULL;
193         }
194
195         if (pci_add_secondary_bus(dn, dev))
196                 return NULL;
197
198         return dev;
199 }
200
201 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
202 {
203         struct pci_bus *secondary_bus;
204
205         if (!bridge_dev) {
206                 printk(KERN_ERR "%s: unexpected null device\n",
207                         __FUNCTION__);
208                 return -EINVAL;
209         }
210
211         secondary_bus = bridge_dev->subordinate;
212
213         if (unmap_bus_range(secondary_bus)) {
214                 printk(KERN_ERR "%s: failed to unmap bus range\n",
215                         __FUNCTION__);
216                 return -ERANGE;
217         }
218
219         pci_remove_bus_device(bridge_dev);
220         return 0;
221 }
222
223 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
224 {
225         struct pci_dev *dev;
226
227         /* Add pci bus */
228         dev = dlpar_pci_add_bus(dn);
229         if (!dev) {
230                 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
231                         drc_name);
232                 return -EIO;
233         }
234
235         /* Add hotplug slot */
236         if (rpaphp_add_slot(dn)) {
237                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
238                         __FUNCTION__, drc_name);
239                 return -EIO;
240         }
241         return 0;
242 }
243
244 static int dlpar_remove_root_bus(struct pci_controller *phb)
245 {
246         struct pci_bus *phb_bus;
247         int rc;
248
249         phb_bus = phb->bus;
250         if (!(list_empty(&phb_bus->children) &&
251               list_empty(&phb_bus->devices))) {
252                 return -EBUSY;
253         }
254
255         rc = pcibios_remove_root_bus(phb);
256         if (rc)
257                 return -EIO;
258
259         device_unregister(phb_bus->bridge);
260         pci_remove_bus(phb_bus);
261
262         return 0;
263 }
264
265 static int dlpar_remove_phb(struct slot *slot)
266 {
267         struct pci_controller *phb;
268         struct device_node *dn;
269         int rc = 0;
270
271         dn = slot->dn;
272         if (!dn) {
273                 printk(KERN_ERR "%s: unexpected NULL slot device node\n",
274                                 __FUNCTION__);
275                 return -EIO;
276         }
277
278         phb = dn->phb;
279         if (!phb) {
280                 printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
281                                 __FUNCTION__);
282                 return -EIO;
283         }
284
285         if (rpaphp_remove_slot(slot)) {
286                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
287                         __FUNCTION__, slot->location);
288                 return -EIO;
289         }
290
291         rc = dlpar_remove_root_bus(phb);
292         if (rc < 0)
293                 return rc;
294
295         return 0;
296 }
297
298 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
299 {
300         struct pci_controller *phb;
301
302         phb = init_phb_dynamic(dn);
303         if (!phb)
304                 return -EINVAL;
305
306         if (rpaphp_add_slot(dn)) {
307                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
308                         __FUNCTION__, drc_name);
309                 return -EIO;
310         }
311         return 0;
312 }
313
314 /**
315  * dlpar_add_slot - DLPAR add an I/O Slot
316  * @drc_name: drc-name of newly added slot
317  *
318  * Make the hotplug module and the kernel aware
319  * of a newly added I/O Slot.
320  * Return Codes -
321  * 0                    Success
322  * -ENODEV              Not a valid drc_name
323  * -EINVAL              Slot already added
324  * -ERESTARTSYS         Signalled before obtaining lock
325  * -EIO                 Internal PCI Error
326  */
327 int dlpar_add_slot(char *drc_name)
328 {
329         struct device_node *dn = NULL;
330         int node_type;
331         int rc;
332
333         if (down_interruptible(&rpadlpar_sem))
334                 return -ERESTARTSYS;
335
336         /* Check for existing hotplug slot */
337         if (find_slot(drc_name)) {
338                 rc = -EINVAL;
339                 goto exit;
340         }
341
342         /* Find newly added node */
343         dn = find_dlpar_node(drc_name, &node_type);
344         if (!dn) {
345                 rc = -ENODEV;
346                 goto exit;
347         }
348
349         rc = -EIO;
350         switch (node_type) {
351                 case NODE_TYPE_VIO:
352                         if (!vio_register_device_node(dn)) {
353                                 printk(KERN_ERR
354                                         "%s: failed to register vio node %s\n",
355                                         __FUNCTION__, drc_name);
356                                 goto exit;
357                         }
358                         break;
359                 case NODE_TYPE_SLOT:
360                         rc = dlpar_add_pci_slot(drc_name, dn);
361                         if (rc)
362                                 goto exit;
363                         break;
364                 case NODE_TYPE_PHB:
365                         rc = dlpar_add_phb(drc_name, dn);
366                         if (rc)
367                                 goto exit;
368                         break;
369                 default:
370                         printk("%s: unexpected node type\n", __FUNCTION__);
371                         goto exit;
372         }
373
374         rc = 0;
375 exit:
376         up(&rpadlpar_sem);
377         return rc;
378 }
379
380 /**
381  * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
382  * @drc_name: drc-name of newly added slot
383  *
384  * Remove the kernel and hotplug representations
385  * of an I/O Slot.
386  * Return Codes:
387  * 0                    Success
388  * -EIO                 Internal  Error
389  */
390 static int dlpar_remove_vio_slot(struct device_node *dn, char *drc_name)
391 {
392         struct vio_dev *vio_dev;
393
394         vio_dev = vio_find_node(dn);
395         if (!vio_dev) {
396                 printk(KERN_ERR "%s: %s does not correspond to a vio dev\n",
397                                 __FUNCTION__, drc_name);
398                 return -EIO;
399         }
400
401         vio_unregister_device(vio_dev);
402         return 0;
403 }
404
405 /**
406  * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
407  * @drc_name: drc-name of newly added slot
408  *
409  * Remove the kernel and hotplug representations
410  * of a PCI I/O Slot.
411  * Return Codes:
412  * 0                    Success
413  * -ENODEV              Not a valid drc_name
414  * -EIO                 Internal PCI Error
415  */
416 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
417 {
418         struct pci_dev *bridge_dev;
419
420         bridge_dev = slot->bridge;
421         if (!bridge_dev) {
422                 printk(KERN_ERR "%s: unexpected null bridge device\n",
423                         __FUNCTION__);
424                 return -EIO;
425         }
426
427         /* Remove hotplug slot */
428         if (rpaphp_remove_slot(slot)) {
429                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
430                         __FUNCTION__, drc_name);
431                 return -EIO;
432         }
433
434         /* Remove pci bus */
435
436         if (dlpar_pci_remove_bus(bridge_dev)) {
437                 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
438                         __FUNCTION__, drc_name);
439                 return -EIO;
440         }
441         return 0;
442 }
443
444 /**
445  * dlpar_remove_slot - DLPAR remove an I/O Slot
446  * @drc_name: drc-name of newly added slot
447  *
448  * Remove the kernel and hotplug representations
449  * of an I/O Slot.
450  * Return Codes:
451  * 0                    Success
452  * -ENODEV              Not a valid drc_name
453  * -EINVAL              Slot already removed
454  * -ERESTARTSYS         Signalled before obtaining lock
455  * -EIO                 Internal Error
456  */
457 int dlpar_remove_slot(char *drc_name)
458 {
459         struct device_node *dn;
460         struct slot *slot;
461         int node_type;
462         int rc = 0;
463
464         if (down_interruptible(&rpadlpar_sem))
465                 return -ERESTARTSYS;
466
467         dn = find_dlpar_node(drc_name, &node_type);
468         if (!dn) {
469                 rc = -ENODEV;
470                 goto exit;
471         }
472
473         if (node_type == NODE_TYPE_VIO) {
474                 rc = dlpar_remove_vio_slot(dn, drc_name);
475         } else {
476                 slot = find_slot(drc_name);
477                 if (!slot) {
478                         rc = -EINVAL;
479                         goto exit;
480                 }
481
482                 if (node_type == NODE_TYPE_PHB)
483                         rc = dlpar_remove_phb(slot);
484                 else {
485                         /* NODE_TYPE_SLOT */
486                         rc = dlpar_remove_pci_slot(slot, drc_name);
487                 }
488         }
489 exit:
490         up(&rpadlpar_sem);
491         return rc;
492 }
493
494 static inline int is_dlpar_capable(void)
495 {
496         int rc = rtas_token("ibm,configure-connector");
497
498         return (int) (rc != RTAS_UNKNOWN_SERVICE);
499 }
500
501 int __init rpadlpar_io_init(void)
502 {
503         int rc = 0;
504
505         if (!is_dlpar_capable()) {
506                 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
507                         __FUNCTION__);
508                 return -EPERM;
509         }
510
511         rc = dlpar_sysfs_init();
512         return rc;
513 }
514
515 void rpadlpar_io_exit(void)
516 {
517         dlpar_sysfs_exit();
518         return;
519 }
520
521 module_init(rpadlpar_io_init);
522 module_exit(rpadlpar_io_exit);
523 MODULE_LICENSE("GPL");