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