]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/pci/hotplug/rpadlpar_core.c
f2a73f70e58c416bee4b4a72ca050246433d8ec5
[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_find_new_dev(struct pci_bus *parent,
169                                         struct device_node *dev_dn)
170 {
171         struct pci_dev *tmp = NULL;
172         struct device_node *child_dn;
173
174         list_for_each_entry(tmp, &parent->devices, bus_list) {
175                 child_dn = pci_device_to_OF_node(tmp);
176                 if (child_dn == dev_dn)
177                         return tmp;
178         }
179         return NULL;
180 }
181
182 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn)
183 {
184         struct pci_controller *hose = dn->phb;
185         struct pci_dev *dev = NULL;
186
187         /* Scan phb bus for EADS device, adding new one to bus->devices */
188         if (!pci_scan_single_device(hose->bus, dn->devfn)) {
189                 printk(KERN_ERR "%s: found no device on bus\n", __FUNCTION__);
190                 return NULL;
191         }
192
193         /* Add new devices to global lists.  Register in proc, sysfs. */
194         pci_bus_add_devices(hose->bus);
195
196         /* Confirm new bridge dev was created */
197         dev = dlpar_find_new_dev(hose->bus, dn);
198         if (dev) {
199                 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
200                         printk(KERN_ERR "%s: unexpected header type %d\n",
201                                 __FUNCTION__, dev->hdr_type);
202                         return NULL;
203                 }
204
205                 if (pci_add_secondary_bus(dn, dev))
206                         return NULL;
207         }
208
209         return dev;
210 }
211
212 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
213 {
214         struct pci_dev *dev;
215         int rc;
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         if (dn->child) {
226                 rc = rpaphp_config_pci_adapter(dev->subordinate);
227                 if (rc < 0) {
228                         printk(KERN_ERR "%s: unable to enable slot %s\n",
229                                 __FUNCTION__, drc_name);
230                         return -EIO;
231                 }
232         }
233
234         /* Add hotplug slot */
235         if (rpaphp_add_slot(dn)) {
236                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
237                         __FUNCTION__, drc_name);
238                 return -EIO;
239         }
240         return 0;
241 }
242
243 static int dlpar_remove_root_bus(struct pci_controller *phb)
244 {
245         struct pci_bus *phb_bus;
246         int rc;
247
248         phb_bus = phb->bus;
249         if (!(list_empty(&phb_bus->children) &&
250               list_empty(&phb_bus->devices))) {
251                 return -EBUSY;
252         }
253
254         rc = pcibios_remove_root_bus(phb);
255         if (rc)
256                 return -EIO;
257
258         device_unregister(phb_bus->bridge);
259         pci_remove_bus(phb_bus);
260
261         return 0;
262 }
263
264 static int dlpar_remove_phb(struct slot *slot)
265 {
266         struct pci_controller *phb;
267         struct device_node *dn;
268         int rc = 0;
269
270         dn = slot->dn;
271         if (!dn) {
272                 printk(KERN_ERR "%s: unexpected NULL slot device node\n",
273                                 __FUNCTION__);
274                 return -EIO;
275         }
276
277         phb = dn->phb;
278         if (!phb) {
279                 printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
280                                 __FUNCTION__);
281                 return -EIO;
282         }
283
284         if (rpaphp_remove_slot(slot)) {
285                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
286                         __FUNCTION__, slot->location);
287                 return -EIO;
288         }
289
290         rc = dlpar_remove_root_bus(phb);
291         if (rc < 0)
292                 return rc;
293
294         return 0;
295 }
296
297 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
298 {
299         struct pci_controller *phb;
300
301         phb = init_phb_dynamic(dn);
302         if (!phb)
303                 return -EINVAL;
304
305         if (rpaphp_add_slot(dn)) {
306                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
307                         __FUNCTION__, drc_name);
308                 return -EIO;
309         }
310         return 0;
311 }
312
313 /**
314  * dlpar_add_slot - DLPAR add an I/O Slot
315  * @drc_name: drc-name of newly added slot
316  *
317  * Make the hotplug module and the kernel aware
318  * of a newly added I/O Slot.
319  * Return Codes -
320  * 0                    Success
321  * -ENODEV              Not a valid drc_name
322  * -EINVAL              Slot already added
323  * -ERESTARTSYS         Signalled before obtaining lock
324  * -EIO                 Internal PCI Error
325  */
326 int dlpar_add_slot(char *drc_name)
327 {
328         struct device_node *dn = NULL;
329         int node_type;
330         int rc;
331
332         if (down_interruptible(&rpadlpar_sem))
333                 return -ERESTARTSYS;
334
335         /* Check for existing hotplug slot */
336         if (find_slot(drc_name)) {
337                 rc = -EINVAL;
338                 goto exit;
339         }
340
341         /* Find newly added node */
342         dn = find_dlpar_node(drc_name, &node_type);
343         if (!dn) {
344                 rc = -ENODEV;
345                 goto exit;
346         }
347
348         rc = -EIO;
349         switch (node_type) {
350                 case NODE_TYPE_VIO:
351                         if (!vio_register_device_node(dn)) {
352                                 printk(KERN_ERR
353                                         "%s: failed to register vio node %s\n",
354                                         __FUNCTION__, drc_name);
355                                 goto exit;
356                         }
357                         break;
358                 case NODE_TYPE_SLOT:
359                         rc = dlpar_add_pci_slot(drc_name, dn);
360                         if (rc)
361                                 goto exit;
362                         break;
363                 case NODE_TYPE_PHB:
364                         rc = dlpar_add_phb(drc_name, dn);
365                         if (rc)
366                                 goto exit;
367                         break;
368                 default:
369                         printk("%s: unexpected node type\n", __FUNCTION__);
370                         goto exit;
371         }
372
373         rc = 0;
374 exit:
375         up(&rpadlpar_sem);
376         return rc;
377 }
378
379 /**
380  * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
381  * @drc_name: drc-name of newly added slot
382  *
383  * Remove the kernel and hotplug representations
384  * of an I/O Slot.
385  * Return Codes:
386  * 0                    Success
387  * -EIO                 Internal  Error
388  */
389 static int dlpar_remove_vio_slot(struct device_node *dn, char *drc_name)
390 {
391         struct vio_dev *vio_dev;
392
393         vio_dev = vio_find_node(dn);
394         if (!vio_dev) {
395                 printk(KERN_ERR "%s: %s does not correspond to a vio dev\n",
396                                 __FUNCTION__, drc_name);
397                 return -EIO;
398         }
399
400         vio_unregister_device(vio_dev);
401         return 0;
402 }
403
404 /**
405  * dlpar_remove_slot - DLPAR remove a PCI I/O Slot
406  * @drc_name: drc-name of newly added slot
407  *
408  * Remove the kernel and hotplug representations
409  * of a PCI I/O Slot.
410  * Return Codes:
411  * 0                    Success
412  * -ENODEV              Not a valid drc_name
413  * -EIO                 Internal PCI Error
414  */
415 int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
416 {
417         struct pci_bus *bus = slot->bus;
418
419         /* Remove hotplug slot */
420         if (rpaphp_remove_slot(slot)) {
421                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
422                         __FUNCTION__, drc_name);
423                 return -EIO;
424         }
425
426         if (unmap_bus_range(bus)) {
427                 printk(KERN_ERR "%s: failed to unmap bus range\n",
428                         __FUNCTION__);
429                 return -ERANGE;
430         }
431
432         BUG_ON(!bus->self);
433         pci_remove_bus_device(bus->self);
434         return 0;
435 }
436
437 /**
438  * dlpar_remove_slot - DLPAR remove an I/O Slot
439  * @drc_name: drc-name of newly added slot
440  *
441  * Remove the kernel and hotplug representations
442  * of an I/O Slot.
443  * Return Codes:
444  * 0                    Success
445  * -ENODEV              Not a valid drc_name
446  * -EINVAL              Slot already removed
447  * -ERESTARTSYS         Signalled before obtaining lock
448  * -EIO                 Internal Error
449  */
450 int dlpar_remove_slot(char *drc_name)
451 {
452         struct device_node *dn;
453         struct slot *slot;
454         int node_type;
455         int rc = 0;
456
457         if (down_interruptible(&rpadlpar_sem))
458                 return -ERESTARTSYS;
459
460         dn = find_dlpar_node(drc_name, &node_type);
461         if (!dn) {
462                 rc = -ENODEV;
463                 goto exit;
464         }
465
466         if (node_type == NODE_TYPE_VIO) {
467                 rc = dlpar_remove_vio_slot(dn, drc_name);
468         } else {
469                 slot = find_slot(drc_name);
470                 if (!slot) {
471                         rc = -EINVAL;
472                         goto exit;
473                 }
474
475                 if (node_type == NODE_TYPE_PHB)
476                         rc = dlpar_remove_phb(slot);
477                 else {
478                         /* NODE_TYPE_SLOT */
479                         rc = dlpar_remove_pci_slot(slot, drc_name);
480                 }
481         }
482 exit:
483         up(&rpadlpar_sem);
484         return rc;
485 }
486
487 static inline int is_dlpar_capable(void)
488 {
489         int rc = rtas_token("ibm,configure-connector");
490
491         return (int) (rc != RTAS_UNKNOWN_SERVICE);
492 }
493
494 int __init rpadlpar_io_init(void)
495 {
496         int rc = 0;
497
498         if (!is_dlpar_capable()) {
499                 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
500                         __FUNCTION__);
501                 return -EPERM;
502         }
503
504         rc = dlpar_sysfs_init();
505         return rc;
506 }
507
508 void rpadlpar_io_exit(void)
509 {
510         dlpar_sysfs_exit();
511         return;
512 }
513
514 module_init(rpadlpar_io_init);
515 module_exit(rpadlpar_io_exit);
516 MODULE_LICENSE("GPL");