VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.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 "../pci.h"
23 #include "rpaphp.h"
24 #include "rpadlpar.h"
25
26 static DECLARE_MUTEX(rpadlpar_sem);
27
28 static struct device_node *find_php_slot_vio_node(char *drc_name)
29 {
30         struct device_node *child;
31         struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
32         char *loc_code;
33
34         if (!parent)
35                 return NULL;
36
37         for (child = of_get_next_child(parent, NULL);
38                 child; child = of_get_next_child(parent, child)) {
39                 loc_code = get_property(child, "ibm,loc-code", NULL);
40                 if (loc_code && !strncmp(loc_code, drc_name, strlen(drc_name)))
41                         return child;
42         }
43
44         return NULL;
45 }
46
47 static struct device_node *find_php_slot_pci_node(char *drc_name)
48 {
49         struct device_node *np = NULL;
50         char *name;
51
52         while ((np = of_find_node_by_type(np, "pci")))
53                 if (is_hotplug_capable(np)) {
54                         name = rpaphp_get_drc_name(np);
55                         if (name && (!strcmp(drc_name, name)))
56                                 break;
57                 }
58
59         return np;
60 }
61
62 static struct slot *find_slot(char *drc_name)
63 {
64         struct list_head *tmp, *n;
65         struct slot *slot;
66
67         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
68                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
69                 if (strcmp(slot->location, drc_name) == 0)
70                         return slot;
71         }
72
73         return NULL;
74 }
75
76 static void rpadlpar_claim_one_bus(struct pci_bus *b)
77 {
78         struct list_head *ld;
79         struct pci_bus *child_bus;
80
81         for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
82                 struct pci_dev *dev = pci_dev_b(ld);
83                 int i;
84
85                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
86                         struct resource *r = &dev->resource[i];
87
88                         if (r->parent || !r->start || !r->flags)
89                                 continue;
90                         rpaphp_claim_resource(dev, i);
91                 }
92         }
93
94         list_for_each_entry(child_bus, &b->children, node)
95                 rpadlpar_claim_one_bus(child_bus);
96 }
97
98 static int pci_add_secondary_bus(struct device_node *dn,
99                 struct pci_dev *bridge_dev)
100 {
101         struct pci_controller *hose = dn->phb;
102         struct pci_bus *child;
103         u8 sec_busno;
104
105         /* Get busno of downstream bus */
106         pci_read_config_byte(bridge_dev, PCI_SECONDARY_BUS, &sec_busno);
107
108         /* Allocate and add to children of bridge_dev->bus */
109         child = pci_add_new_bus(bridge_dev->bus, bridge_dev, sec_busno);
110         if (!child) {
111                 printk(KERN_ERR "%s: could not add secondary bus\n", __FUNCTION__);
112                 return 1;
113         }
114
115         sprintf(child->name, "PCI Bus #%02x", child->number);
116
117         /* Fixup subordinate bridge bases and resources */
118         pcibios_fixup_bus(child);
119
120         /* Claim new bus resources */
121         rpadlpar_claim_one_bus(bridge_dev->bus);
122
123         if (hose->last_busno < child->number)
124                 hose->last_busno = child->number;
125
126         dn->bussubno = child->number;
127
128         /* ioremap() for child bus */
129         if (remap_bus_range(child)) {
130                 printk(KERN_ERR "%s: could not ioremap() child bus\n",
131                         __FUNCTION__);
132                 return 1;
133         }
134
135         return 0;
136 }
137
138 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
139 {
140         struct pci_controller *hose = dn->phb;
141         struct pci_dev *dev = NULL;
142
143         /* Scan phb bus for EADS device, adding new one to bus->devices */
144         if (!pci_scan_single_device(hose->bus, dn->devfn)) {
145                 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
146                 return NULL;
147         }
148
149         /* Add new devices to global lists.  Register in proc, sysfs. */
150         pci_bus_add_devices(hose->bus);
151
152         /* Confirm new bridge dev was created */
153         dev = rpaphp_find_pci_dev(dn);
154         if (!dev) {
155                 printk(KERN_ERR "%s: failed to add pci device\n", __FUNCTION__);
156                 return NULL;
157         }
158
159         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
160                 printk(KERN_ERR "%s: unexpected header type %d\n",
161                         __FUNCTION__, dev->hdr_type);
162                 return NULL;
163         }
164
165         if (pci_add_secondary_bus(dn, dev))
166                 return NULL;
167
168         return dev;
169 }
170
171 static int dlpar_pci_remove_bus(struct pci_dev *bridge_dev)
172 {
173         struct pci_bus *secondary_bus;
174
175         if (!bridge_dev) {
176                 printk(KERN_ERR "%s: unexpected null device\n",
177                         __FUNCTION__);
178                 return 1;
179         }
180
181         secondary_bus = bridge_dev->subordinate;
182
183         if (unmap_bus_range(secondary_bus)) {
184                 printk(KERN_ERR "%s: failed to unmap bus range\n",
185                         __FUNCTION__);
186                 return 1;
187         }
188
189         pci_remove_bus_device(bridge_dev);
190         return 0;
191 }
192
193 static inline int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
194 {
195         struct pci_dev *dev;
196
197         /* Add pci bus */
198         dev = dlpar_pci_add_bus(dn);
199         if (!dev) {
200                 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__,
201                         drc_name);
202                 return -EIO;
203         }
204
205         return 0;
206 }
207
208 /**
209  * dlpar_add_slot - DLPAR add an I/O Slot
210  * @drc_name: drc-name of newly added slot
211  *
212  * Make the hotplug module and the kernel aware
213  * of a newly added I/O Slot.
214  * Return Codes -
215  * 0                    Success
216  * -ENODEV              Not a valid drc_name
217  * -EINVAL              Slot already added
218  * -ERESTARTSYS         Signalled before obtaining lock
219  * -EIO                 Internal PCI Error
220  */
221 int dlpar_add_slot(char *drc_name)
222 {
223         struct device_node *dn;
224         int rc = 0;
225
226         if (down_interruptible(&rpadlpar_sem))
227                 return -ERESTARTSYS;
228
229         /* Check for existing hotplug slot */
230         if (find_slot(drc_name)) {
231                 rc = -EINVAL;
232                 goto exit;
233         }
234
235         dn = find_php_slot_vio_node(drc_name);
236         if (!dn) {
237                 dn = find_php_slot_pci_node(drc_name);
238                 if (dn)
239                         rc = dlpar_add_pci_slot(drc_name, dn);
240                 else {
241                         rc = -ENODEV;
242                         goto exit;
243                 }
244         }
245
246         /* Add hotplug slot for new VIOA or PCI */
247         if (!rc && rpaphp_add_slot(dn)) {
248                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
249                         __FUNCTION__, drc_name);
250                 rc = -EIO;
251         }
252 exit:
253         up(&rpadlpar_sem);
254         return rc;
255 }
256
257 /**
258  * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
259  * @drc_name: drc-name of newly added slot
260  *
261  * Remove the kernel and hotplug representations
262  * of an I/O Slot.
263  * Return Codes:
264  * 0                    Success
265  * -EIO                 Internal  Error
266  */
267 int dlpar_remove_vio_slot(struct slot *slot, char *drc_name)
268 {
269         /* Remove hotplug slot */
270
271         if (rpaphp_remove_slot(slot)) {
272                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
273                         __FUNCTION__, drc_name);
274                 return -EIO;
275         }
276         return 0;
277 }
278
279 /**
280  * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
281  * @drc_name: drc-name of newly added slot
282  *
283  * Remove the kernel and hotplug representations
284  * of a PCI I/O Slot.
285  * Return Codes:
286  * 0                    Success
287  * -ENODEV              Not a valid drc_name
288  * -EIO                 Internal PCI Error
289  */
290 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
291 {
292         struct pci_dev *bridge_dev;
293
294         bridge_dev = slot->bridge;
295         if (!bridge_dev) {
296                 printk(KERN_ERR "%s: unexpected null bridge device\n",
297                         __FUNCTION__);
298                 return -EIO;
299         }
300
301         /* Remove hotplug slot */
302         if (rpaphp_remove_slot(slot)) {
303                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
304                         __FUNCTION__, drc_name);
305                 return -EIO;
306         }
307
308         /* Remove pci bus */
309
310         if (dlpar_pci_remove_bus(bridge_dev)) {
311                 printk(KERN_ERR "%s: unable to remove pci bus %s\n",
312                         __FUNCTION__, drc_name);
313                 return -EIO;
314         }
315         return 0;
316 }
317
318 /**
319  * dlpar_remove_slot - DLPAR remove an I/O Slot
320  * @drc_name: drc-name of newly added slot
321  *
322  * Remove the kernel and hotplug representations
323  * of an I/O Slot.
324  * Return Codes:
325  * 0                    Success
326  * -ENODEV              Not a valid drc_name
327  * -EINVAL              Slot already removed
328  * -ERESTARTSYS         Signalled before obtaining lock
329  * -EIO                 Internal Error
330  */
331 int dlpar_remove_slot(char *drc_name)
332 {
333         struct slot *slot;
334         int rc = 0;
335
336         if (down_interruptible(&rpadlpar_sem))
337                 return -ERESTARTSYS;
338
339         if (!find_php_slot_vio_node(drc_name) &&
340             !find_php_slot_pci_node(drc_name)) {
341                 rc = -ENODEV;
342                 goto exit;
343         }
344
345         slot = find_slot(drc_name);
346         if (!slot) {
347                 rc = -EINVAL;
348                 goto exit;
349         }
350         
351         switch (slot->dev_type) {
352                 case PCI_DEV:
353                         rc = dlpar_remove_pci_slot(slot, drc_name);
354                         break;
355
356                 case VIO_DEV:
357                         rc = dlpar_remove_vio_slot(slot, drc_name);
358                         break;
359
360                 default:
361                         rc = -EIO;
362         }
363 exit:
364         up(&rpadlpar_sem);
365         return rc;
366 }
367
368 static inline int is_dlpar_capable(void)
369 {
370         int rc = rtas_token("ibm,configure-connector");
371
372         return (int) (rc != RTAS_UNKNOWN_SERVICE);
373 }
374
375 int __init rpadlpar_io_init(void)
376 {
377         int rc = 0;
378
379         if (!is_dlpar_capable()) {
380                 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
381                         __FUNCTION__);
382                 return -EPERM;
383         }
384
385         rc = dlpar_sysfs_init();
386         return rc;
387 }
388
389 void rpadlpar_io_exit(void)
390 {
391         dlpar_sysfs_exit();
392         return;
393 }
394
395 module_init(rpadlpar_io_init);
396 module_exit(rpadlpar_io_exit);
397 MODULE_LICENSE("GPL");