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