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