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