Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / 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
18 #undef DEBUG
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/string.h>
24 #include <linux/vmalloc.h>
25
26 #include <asm/pci-bridge.h>
27 #include <linux/mutex.h>
28 #include <asm/rtas.h>
29 #include <asm/vio.h>
30
31 #include "../pci.h"
32 #include "rpaphp.h"
33 #include "rpadlpar.h"
34
35 static DEFINE_MUTEX(rpadlpar_mutex);
36
37 #define DLPAR_MODULE_NAME "rpadlpar_io"
38
39 #define NODE_TYPE_VIO  1
40 #define NODE_TYPE_SLOT 2
41 #define NODE_TYPE_PHB  3
42
43 static struct device_node *find_vio_slot_node(char *drc_name)
44 {
45         struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
46         struct device_node *dn = NULL;
47         char *name;
48         int rc;
49
50         if (!parent)
51                 return NULL;
52
53         while ((dn = of_get_next_child(parent, dn))) {
54                 rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
55                 if ((rc == 0) && (!strcmp(drc_name, name)))
56                         break;
57         }
58
59         return dn;
60 }
61
62 /* Find dlpar-capable pci node that contains the specified name and type */
63 static struct device_node *find_php_slot_pci_node(char *drc_name,
64                                                   char *drc_type)
65 {
66         struct device_node *np = NULL;
67         char *name;
68         char *type;
69         int rc;
70
71         while ((np = of_find_node_by_name(np, "pci"))) {
72                 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
73                 if (rc == 0)
74                         if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
75                                 break;
76         }
77
78         return np;
79 }
80
81 static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
82 {
83         struct device_node *dn;
84
85         dn = find_php_slot_pci_node(drc_name, "SLOT");
86         if (dn) {
87                 *node_type = NODE_TYPE_SLOT;
88                 return dn;
89         }
90
91         dn = find_php_slot_pci_node(drc_name, "PHB");
92         if (dn) {
93                 *node_type = NODE_TYPE_PHB;
94                 return dn;
95         }
96
97         dn = find_vio_slot_node(drc_name);
98         if (dn) {
99                 *node_type = NODE_TYPE_VIO;
100                 return dn;
101         }
102
103         return NULL;
104 }
105
106 /**
107  * find_php_slot - return hotplug slot structure for device node
108  * @dn: target &device_node
109  *
110  * This routine will return the hotplug slot structure
111  * for a given device node. Note that built-in PCI slots
112  * may be dlpar-able, but not hot-pluggable, so this routine
113  * will return NULL for built-in PCI slots.
114  */
115 static struct slot *find_php_slot(struct device_node *dn)
116 {
117         struct list_head *tmp, *n;
118         struct slot *slot;
119
120         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
121                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
122                 if (slot->dn == dn)
123                         return slot;
124         }
125
126         return NULL;
127 }
128
129 static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
130                                         struct device_node *dev_dn)
131 {
132         struct pci_dev *tmp = NULL;
133         struct device_node *child_dn;
134
135         list_for_each_entry(tmp, &parent->devices, bus_list) {
136                 child_dn = pci_device_to_OF_node(tmp);
137                 if (child_dn == dev_dn)
138                         return tmp;
139         }
140         return NULL;
141 }
142
143 static void dlpar_pci_add_bus(struct device_node *dn)
144 {
145         struct pci_dn *pdn = PCI_DN(dn);
146         struct pci_controller *phb = pdn->phb;
147         struct pci_dev *dev = NULL;
148
149         eeh_add_device_tree_early(pdn);
150
151         /* Add EADS device to PHB bus, adding new entry to bus->devices */
152         dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
153         if (!dev) {
154                 printk(KERN_ERR "%s: failed to create pci dev for %s\n",
155                                 __func__, dn->full_name);
156                 return;
157         }
158
159         /* Scan below the new bridge */
160         if (pci_is_bridge(dev))
161                 of_scan_pci_bridge(dev);
162
163         /* Map IO space for child bus, which may or may not succeed */
164         pcibios_map_io_space(dev->subordinate);
165
166         /* Finish adding it : resource allocation, adding devices, etc...
167          * Note that we need to perform the finish pass on the -parent-
168          * bus of the EADS bridge so the bridge device itself gets
169          * properly added
170          */
171         pcibios_finish_adding_to_bus(phb->bus);
172 }
173
174 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
175 {
176         struct pci_dev *dev;
177         struct pci_controller *phb;
178
179         if (pcibios_find_pci_bus(dn))
180                 return -EINVAL;
181
182         /* Add pci bus */
183         dlpar_pci_add_bus(dn);
184
185         /* Confirm new bridge dev was created */
186         phb = PCI_DN(dn)->phb;
187         dev = dlpar_find_new_dev(phb->bus, dn);
188
189         if (!dev) {
190                 printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
191                         drc_name);
192                 return -EIO;
193         }
194
195         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
196                 printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
197                         __func__, dev->hdr_type, drc_name);
198                 return -EIO;
199         }
200
201         /* Add hotplug slot */
202         if (rpaphp_add_slot(dn)) {
203                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
204                         __func__, drc_name);
205                 return -EIO;
206         }
207         return 0;
208 }
209
210 static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
211 {
212         struct slot *slot;
213         struct pci_dn *pdn;
214         int rc = 0;
215
216         if (!pcibios_find_pci_bus(dn))
217                 return -EINVAL;
218
219         /* If pci slot is hotpluggable, use hotplug to remove it */
220         slot = find_php_slot(dn);
221         if (slot && rpaphp_deregister_slot(slot)) {
222                 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
223                        __func__, drc_name);
224                 return -EIO;
225         }
226
227         pdn = dn->data;
228         BUG_ON(!pdn || !pdn->phb);
229         rc = remove_phb_dynamic(pdn->phb);
230         if (rc < 0)
231                 return rc;
232
233         pdn->phb = NULL;
234
235         return 0;
236 }
237
238 static int dlpar_add_phb(char *drc_name, struct device_node *dn)
239 {
240         struct pci_controller *phb;
241
242         if (PCI_DN(dn) && PCI_DN(dn)->phb) {
243                 /* PHB already exists */
244                 return -EINVAL;
245         }
246
247         phb = init_phb_dynamic(dn);
248         if (!phb)
249                 return -EIO;
250
251         if (rpaphp_add_slot(dn)) {
252                 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
253                         __func__, drc_name);
254                 return -EIO;
255         }
256         return 0;
257 }
258
259 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
260 {
261         struct vio_dev *vio_dev;
262
263         vio_dev = vio_find_node(dn);
264         if (vio_dev) {
265                 put_device(&vio_dev->dev);
266                 return -EINVAL;
267         }
268
269         if (!vio_register_device_node(dn)) {
270                 printk(KERN_ERR
271                         "%s: failed to register vio node %s\n",
272                         __func__, drc_name);
273                 return -EIO;
274         }
275         return 0;
276 }
277
278 /**
279  * dlpar_add_slot - DLPAR add an I/O Slot
280  * @drc_name: drc-name of newly added slot
281  *
282  * Make the hotplug module and the kernel aware of a newly added I/O Slot.
283  * Return Codes:
284  * 0                    Success
285  * -ENODEV              Not a valid drc_name
286  * -EINVAL              Slot already added
287  * -ERESTARTSYS         Signalled before obtaining lock
288  * -EIO                 Internal PCI Error
289  */
290 int dlpar_add_slot(char *drc_name)
291 {
292         struct device_node *dn = NULL;
293         int node_type;
294         int rc = -EIO;
295
296         if (mutex_lock_interruptible(&rpadlpar_mutex))
297                 return -ERESTARTSYS;
298
299         /* Find newly added node */
300         dn = find_dlpar_node(drc_name, &node_type);
301         if (!dn) {
302                 rc = -ENODEV;
303                 goto exit;
304         }
305
306         switch (node_type) {
307                 case NODE_TYPE_VIO:
308                         rc = dlpar_add_vio_slot(drc_name, dn);
309                         break;
310                 case NODE_TYPE_SLOT:
311                         rc = dlpar_add_pci_slot(drc_name, dn);
312                         break;
313                 case NODE_TYPE_PHB:
314                         rc = dlpar_add_phb(drc_name, dn);
315                         break;
316         }
317
318         printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
319 exit:
320         mutex_unlock(&rpadlpar_mutex);
321         return rc;
322 }
323
324 /**
325  * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
326  * @drc_name: drc-name of newly added slot
327  * @dn: &device_node
328  *
329  * Remove the kernel and hotplug representations of an I/O Slot.
330  * Return Codes:
331  * 0                    Success
332  * -EINVAL              Vio dev doesn't exist
333  */
334 static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
335 {
336         struct vio_dev *vio_dev;
337
338         vio_dev = vio_find_node(dn);
339         if (!vio_dev)
340                 return -EINVAL;
341
342         vio_unregister_device(vio_dev);
343
344         put_device(&vio_dev->dev);
345
346         return 0;
347 }
348
349 /**
350  * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
351  * @drc_name: drc-name of newly added slot
352  * @dn: &device_node
353  *
354  * Remove the kernel and hotplug representations of a PCI I/O Slot.
355  * Return Codes:
356  * 0                    Success
357  * -ENODEV              Not a valid drc_name
358  * -EIO                 Internal PCI Error
359  */
360 int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
361 {
362         struct pci_bus *bus;
363         struct slot *slot;
364         int ret = 0;
365
366         pci_lock_rescan_remove();
367
368         bus = pcibios_find_pci_bus(dn);
369         if (!bus) {
370                 ret = -EINVAL;
371                 goto out;
372         }
373
374         pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
375                  bus->self ? pci_name(bus->self) : "<!PHB!>");
376
377         slot = find_php_slot(dn);
378         if (slot) {
379                 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
380                          pci_domain_nr(bus), bus->number);
381
382                 if (rpaphp_deregister_slot(slot)) {
383                         printk(KERN_ERR
384                                 "%s: unable to remove hotplug slot %s\n",
385                                 __func__, drc_name);
386                         ret = -EIO;
387                         goto out;
388                 }
389         }
390
391         /* Remove all devices below slot */
392         pcibios_remove_pci_devices(bus);
393
394         /* Unmap PCI IO space */
395         if (pcibios_unmap_io_space(bus)) {
396                 printk(KERN_ERR "%s: failed to unmap bus range\n",
397                         __func__);
398                 ret = -ERANGE;
399                 goto out;
400         }
401
402         /* Remove the EADS bridge device itself */
403         BUG_ON(!bus->self);
404         pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
405         pci_stop_and_remove_bus_device(bus->self);
406
407  out:
408         pci_unlock_rescan_remove();
409         return ret;
410 }
411
412 /**
413  * dlpar_remove_slot - DLPAR remove an I/O Slot
414  * @drc_name: drc-name of newly added slot
415  *
416  * Remove the kernel and hotplug representations of an I/O Slot.
417  * Return Codes:
418  * 0                    Success
419  * -ENODEV              Not a valid drc_name
420  * -EINVAL              Slot already removed
421  * -ERESTARTSYS         Signalled before obtaining lock
422  * -EIO                 Internal Error
423  */
424 int dlpar_remove_slot(char *drc_name)
425 {
426         struct device_node *dn;
427         int node_type;
428         int rc = 0;
429
430         if (mutex_lock_interruptible(&rpadlpar_mutex))
431                 return -ERESTARTSYS;
432
433         dn = find_dlpar_node(drc_name, &node_type);
434         if (!dn) {
435                 rc = -ENODEV;
436                 goto exit;
437         }
438
439         switch (node_type) {
440                 case NODE_TYPE_VIO:
441                         rc = dlpar_remove_vio_slot(drc_name, dn);
442                         break;
443                 case NODE_TYPE_PHB:
444                         rc = dlpar_remove_phb(drc_name, dn);
445                         break;
446                 case NODE_TYPE_SLOT:
447                         rc = dlpar_remove_pci_slot(drc_name, dn);
448                         break;
449         }
450         vm_unmap_aliases();
451
452         printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
453 exit:
454         mutex_unlock(&rpadlpar_mutex);
455         return rc;
456 }
457
458 static inline int is_dlpar_capable(void)
459 {
460         int rc = rtas_token("ibm,configure-connector");
461
462         return (int) (rc != RTAS_UNKNOWN_SERVICE);
463 }
464
465 int __init rpadlpar_io_init(void)
466 {
467         int rc = 0;
468
469         if (!is_dlpar_capable()) {
470                 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
471                         __func__);
472                 return -EPERM;
473         }
474
475         rc = dlpar_sysfs_init();
476         return rc;
477 }
478
479 void rpadlpar_io_exit(void)
480 {
481         dlpar_sysfs_exit();
482         return;
483 }
484
485 module_init(rpadlpar_io_init);
486 module_exit(rpadlpar_io_exit);
487 MODULE_LICENSE("GPL");