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