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