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