Merge remote-tracking branch 'spi/topic/rspi' into spi-pdata
[deliverable/linux.git] / drivers / pci / hotplug / rpadlpar_core.c
CommitLineData
1da177e4
LT
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 */
fd6852c8
BH
17
18#undef DEBUG
19
1da177e4 20#include <linux/init.h>
eefa9cfc 21#include <linux/module.h>
1da177e4 22#include <linux/pci.h>
4e57b681 23#include <linux/string.h>
b4a26be9 24#include <linux/vmalloc.h>
4e57b681 25
1da177e4 26#include <asm/pci-bridge.h>
14cc3e2b 27#include <linux/mutex.h>
1da177e4 28#include <asm/rtas.h>
5eeb8c63 29#include <asm/vio.h>
4e57b681 30
1da177e4
LT
31#include "../pci.h"
32#include "rpaphp.h"
33#include "rpadlpar.h"
34
14cc3e2b 35static DEFINE_MUTEX(rpadlpar_mutex);
1da177e4 36
56d8456b
JR
37#define DLPAR_MODULE_NAME "rpadlpar_io"
38
1da177e4
LT
39#define NODE_TYPE_VIO 1
40#define NODE_TYPE_SLOT 2
41#define NODE_TYPE_PHB 3
42
5eeb8c63 43static struct device_node *find_vio_slot_node(char *drc_name)
1da177e4 44{
1da177e4 45 struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
5eeb8c63
JR
46 struct device_node *dn = NULL;
47 char *name;
48 int rc;
1da177e4
LT
49
50 if (!parent)
51 return NULL;
52
5eeb8c63
JR
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;
1da177e4
LT
57 }
58
5eeb8c63 59 return dn;
1da177e4
LT
60}
61
62/* Find dlpar-capable pci node that contains the specified name and type */
63static 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
a57ed79e 71 while ((np = of_find_node_by_name(np, "pci"))) {
1da177e4
LT
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
5eeb8c63 81static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
1da177e4
LT
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
5eeb8c63 97 dn = find_vio_slot_node(drc_name);
1da177e4
LT
98 if (dn) {
99 *node_type = NODE_TYPE_VIO;
100 return dn;
101 }
102
103 return NULL;
104}
105
8485d1a1
LV
106/**
107 * find_php_slot - return hotplug slot structure for device node
26e6c66e 108 * @dn: target &device_node
8485d1a1
LV
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 */
115static struct slot *find_php_slot(struct device_node *dn)
1da177e4
LT
116{
117 struct list_head *tmp, *n;
118 struct slot *slot;
119
8737d6a9 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 }
1da177e4 125
8737d6a9 126 return NULL;
1da177e4
LT
127}
128
0945cd5f
JR
129static 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
8737d6a9 143static void dlpar_pci_add_bus(struct device_node *dn)
1da177e4 144{
8737d6a9 145 struct pci_dn *pdn = PCI_DN(dn);
5fa80fcd 146 struct pci_controller *phb = pdn->phb;
1da177e4
LT
147 struct pci_dev *dev = NULL;
148
d681db4a 149 eeh_add_device_tree_early(dn);
150
5fa80fcd
JR
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",
66bef8c0 155 __func__, dn->full_name);
8737d6a9 156 return;
1da177e4
LT
157 }
158
fd6852c8 159 /* Scan below the new bridge */
5fa80fcd
JR
160 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
161 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
98d9f30c 162 of_scan_pci_bridge(dev);
5fa80fcd 163
3d5134ee
BH
164 /* Map IO space for child bus, which may or may not succeed */
165 pcibios_map_io_space(dev->subordinate);
5fa80fcd 166
fd6852c8
BH
167 /* Finish adding it : resource allocation, adding devices, etc...
168 * Note that we need to perform the finish pass on the -parent-
169 * bus of the EADS bridge so the bridge device itself gets
170 * properly added
171 */
172 pcibios_finish_adding_to_bus(phb->bus);
1da177e4
LT
173}
174
940903c5 175static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
1da177e4
LT
176{
177 struct pci_dev *dev;
8737d6a9 178 struct pci_controller *phb;
1da177e4 179
01657868 180 if (pcibios_find_pci_bus(dn))
56d8456b
JR
181 return -EINVAL;
182
1da177e4 183 /* Add pci bus */
8737d6a9 184 dlpar_pci_add_bus(dn);
185
186 /* Confirm new bridge dev was created */
187 phb = PCI_DN(dn)->phb;
188 dev = dlpar_find_new_dev(phb->bus, dn);
189
1da177e4 190 if (!dev) {
66bef8c0 191 printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
1da177e4
LT
192 drc_name);
193 return -EIO;
194 }
195
8737d6a9 196 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
197 printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
66bef8c0 198 __func__, dev->hdr_type, drc_name);
8737d6a9 199 return -EIO;
200 }
201
5eeb8c63
JR
202 /* Add hotplug slot */
203 if (rpaphp_add_slot(dn)) {
204 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
66bef8c0 205 __func__, drc_name);
5eeb8c63
JR
206 return -EIO;
207 }
1da177e4
LT
208 return 0;
209}
210
56d8456b 211static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
1da177e4 212{
56d8456b 213 struct slot *slot;
1635317f 214 struct pci_dn *pdn;
1da177e4
LT
215 int rc = 0;
216
01657868 217 if (!pcibios_find_pci_bus(dn))
56d8456b 218 return -EINVAL;
1da177e4 219
8485d1a1
LV
220 /* If pci slot is hotplugable, use hotplug to remove it */
221 slot = find_php_slot(dn);
fd6852c8
BH
222 if (slot && rpaphp_deregister_slot(slot)) {
223 printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
224 __func__, drc_name);
225 return -EIO;
1da177e4
LT
226 }
227
1635317f
PM
228 pdn = dn->data;
229 BUG_ON(!pdn || !pdn->phb);
fd6852c8 230 rc = remove_phb_dynamic(pdn->phb);
1da177e4
LT
231 if (rc < 0)
232 return rc;
233
1635317f 234 pdn->phb = NULL;
56d8456b 235
1da177e4
LT
236 return 0;
237}
238
5eeb8c63 239static int dlpar_add_phb(char *drc_name, struct device_node *dn)
1da177e4
LT
240{
241 struct pci_controller *phb;
242
fe98aeab 243 if (PCI_DN(dn) && PCI_DN(dn)->phb) {
56d8456b
JR
244 /* PHB already exists */
245 return -EINVAL;
246 }
247
1da177e4
LT
248 phb = init_phb_dynamic(dn);
249 if (!phb)
56d8456b 250 return -EIO;
1da177e4 251
5eeb8c63
JR
252 if (rpaphp_add_slot(dn)) {
253 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
66bef8c0 254 __func__, drc_name);
5eeb8c63
JR
255 return -EIO;
256 }
1da177e4
LT
257 return 0;
258}
259
56d8456b
JR
260static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
261{
262 if (vio_find_node(dn))
263 return -EINVAL;
264
265 if (!vio_register_device_node(dn)) {
266 printk(KERN_ERR
267 "%s: failed to register vio node %s\n",
66bef8c0 268 __func__, drc_name);
56d8456b
JR
269 return -EIO;
270 }
271 return 0;
272}
273
1da177e4
LT
274/**
275 * dlpar_add_slot - DLPAR add an I/O Slot
276 * @drc_name: drc-name of newly added slot
277 *
26e6c66e
RD
278 * Make the hotplug module and the kernel aware of a newly added I/O Slot.
279 * Return Codes:
1da177e4
LT
280 * 0 Success
281 * -ENODEV Not a valid drc_name
282 * -EINVAL Slot already added
283 * -ERESTARTSYS Signalled before obtaining lock
284 * -EIO Internal PCI Error
285 */
286int dlpar_add_slot(char *drc_name)
287{
288 struct device_node *dn = NULL;
289 int node_type;
56d8456b 290 int rc = -EIO;
1da177e4 291
14cc3e2b 292 if (mutex_lock_interruptible(&rpadlpar_mutex))
1da177e4
LT
293 return -ERESTARTSYS;
294
5eeb8c63
JR
295 /* Find newly added node */
296 dn = find_dlpar_node(drc_name, &node_type);
1da177e4
LT
297 if (!dn) {
298 rc = -ENODEV;
299 goto exit;
300 }
301
302 switch (node_type) {
303 case NODE_TYPE_VIO:
56d8456b 304 rc = dlpar_add_vio_slot(drc_name, dn);
1da177e4
LT
305 break;
306 case NODE_TYPE_SLOT:
307 rc = dlpar_add_pci_slot(drc_name, dn);
308 break;
309 case NODE_TYPE_PHB:
5eeb8c63 310 rc = dlpar_add_phb(drc_name, dn);
1da177e4 311 break;
1da177e4
LT
312 }
313
56d8456b 314 printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
1da177e4 315exit:
14cc3e2b 316 mutex_unlock(&rpadlpar_mutex);
1da177e4
LT
317 return rc;
318}
319
320/**
321 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
322 * @drc_name: drc-name of newly added slot
26e6c66e 323 * @dn: &device_node
1da177e4 324 *
26e6c66e 325 * Remove the kernel and hotplug representations of an I/O Slot.
1da177e4
LT
326 * Return Codes:
327 * 0 Success
56d8456b 328 * -EINVAL Vio dev doesn't exist
1da177e4 329 */
56d8456b 330static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
1da177e4 331{
5eeb8c63 332 struct vio_dev *vio_dev;
1da177e4 333
5eeb8c63 334 vio_dev = vio_find_node(dn);
56d8456b
JR
335 if (!vio_dev)
336 return -EINVAL;
5eeb8c63
JR
337
338 vio_unregister_device(vio_dev);
1da177e4
LT
339 return 0;
340}
341
342/**
26e6c66e 343 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
1da177e4 344 * @drc_name: drc-name of newly added slot
26e6c66e 345 * @dn: &device_node
1da177e4 346 *
26e6c66e 347 * Remove the kernel and hotplug representations of a PCI I/O Slot.
1da177e4
LT
348 * Return Codes:
349 * 0 Success
350 * -ENODEV Not a valid drc_name
351 * -EIO Internal PCI Error
352 */
56d8456b 353int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
1da177e4 354{
56d8456b
JR
355 struct pci_bus *bus;
356 struct slot *slot;
1da177e4 357
01657868 358 bus = pcibios_find_pci_bus(dn);
56d8456b
JR
359 if (!bus)
360 return -EINVAL;
361
fd6852c8
BH
362 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
363 bus->self ? pci_name(bus->self) : "<!PHB!>");
364
8485d1a1 365 slot = find_php_slot(dn);
56d8456b 366 if (slot) {
fd6852c8
BH
367 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
368 pci_domain_nr(bus), bus->number);
369
f6afbad8 370 if (rpaphp_deregister_slot(slot)) {
56d8456b
JR
371 printk(KERN_ERR
372 "%s: unable to remove hotplug slot %s\n",
66bef8c0 373 __func__, drc_name);
56d8456b
JR
374 return -EIO;
375 }
fd6852c8
BH
376 }
377
378 /* Remove all devices below slot */
379 pcibios_remove_pci_devices(bus);
1da177e4 380
fd6852c8 381 /* Unmap PCI IO space */
3d5134ee 382 if (pcibios_unmap_io_space(bus)) {
9c209c91 383 printk(KERN_ERR "%s: failed to unmap bus range\n",
66bef8c0 384 __func__);
9c209c91 385 return -ERANGE;
1da177e4 386 }
9c209c91 387
fd6852c8 388 /* Remove the EADS bridge device itself */
9c209c91 389 BUG_ON(!bus->self);
fd6852c8 390 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
210647af 391 pci_stop_and_remove_bus_device(bus->self);
fd6852c8 392
1da177e4
LT
393 return 0;
394}
395
396/**
397 * dlpar_remove_slot - DLPAR remove an I/O Slot
398 * @drc_name: drc-name of newly added slot
399 *
26e6c66e 400 * Remove the kernel and hotplug representations of an I/O Slot.
1da177e4
LT
401 * Return Codes:
402 * 0 Success
403 * -ENODEV Not a valid drc_name
404 * -EINVAL Slot already removed
405 * -ERESTARTSYS Signalled before obtaining lock
406 * -EIO Internal Error
407 */
408int dlpar_remove_slot(char *drc_name)
409{
5eeb8c63 410 struct device_node *dn;
5eeb8c63 411 int node_type;
1da177e4
LT
412 int rc = 0;
413
14cc3e2b 414 if (mutex_lock_interruptible(&rpadlpar_mutex))
1da177e4
LT
415 return -ERESTARTSYS;
416
5eeb8c63
JR
417 dn = find_dlpar_node(drc_name, &node_type);
418 if (!dn) {
1da177e4
LT
419 rc = -ENODEV;
420 goto exit;
421 }
422
56d8456b
JR
423 switch (node_type) {
424 case NODE_TYPE_VIO:
425 rc = dlpar_remove_vio_slot(drc_name, dn);
426 break;
427 case NODE_TYPE_PHB:
428 rc = dlpar_remove_phb(drc_name, dn);
429 break;
430 case NODE_TYPE_SLOT:
431 rc = dlpar_remove_pci_slot(drc_name, dn);
432 break;
1da177e4 433 }
b4a26be9
BH
434 vm_unmap_aliases();
435
56d8456b 436 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
1da177e4 437exit:
14cc3e2b 438 mutex_unlock(&rpadlpar_mutex);
1da177e4
LT
439 return rc;
440}
441
442static inline int is_dlpar_capable(void)
443{
444 int rc = rtas_token("ibm,configure-connector");
445
446 return (int) (rc != RTAS_UNKNOWN_SERVICE);
447}
448
449int __init rpadlpar_io_init(void)
450{
451 int rc = 0;
452
453 if (!is_dlpar_capable()) {
454 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
66bef8c0 455 __func__);
1da177e4
LT
456 return -EPERM;
457 }
458
459 rc = dlpar_sysfs_init();
460 return rc;
461}
462
463void rpadlpar_io_exit(void)
464{
465 dlpar_sysfs_exit();
466 return;
467}
468
469module_init(rpadlpar_io_init);
470module_exit(rpadlpar_io_exit);
471MODULE_LICENSE("GPL");
This page took 0.702383 seconds and 5 git commands to generate.