1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35static 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
43static 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
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
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
81static 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
108
109
110
111
112
113
114
115static 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
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
143static 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(dn);
150
151
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
160 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
161 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
162 of_scan_pci_bridge(dev);
163
164
165 pcibios_map_io_space(dev->subordinate);
166
167
168
169
170
171
172 pcibios_finish_adding_to_bus(phb->bus);
173}
174
175static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
176{
177 struct pci_dev *dev;
178 struct pci_controller *phb;
179
180 if (pcibios_find_pci_bus(dn))
181 return -EINVAL;
182
183
184 dlpar_pci_add_bus(dn);
185
186
187 phb = PCI_DN(dn)->phb;
188 dev = dlpar_find_new_dev(phb->bus, dn);
189
190 if (!dev) {
191 printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
192 drc_name);
193 return -EIO;
194 }
195
196 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
197 printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
198 __func__, dev->hdr_type, drc_name);
199 return -EIO;
200 }
201
202
203 if (rpaphp_add_slot(dn)) {
204 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
205 __func__, drc_name);
206 return -EIO;
207 }
208 return 0;
209}
210
211static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
212{
213 struct slot *slot;
214 struct pci_dn *pdn;
215 int rc = 0;
216
217 if (!pcibios_find_pci_bus(dn))
218 return -EINVAL;
219
220
221 slot = find_php_slot(dn);
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;
226 }
227
228 pdn = dn->data;
229 BUG_ON(!pdn || !pdn->phb);
230 rc = remove_phb_dynamic(pdn->phb);
231 if (rc < 0)
232 return rc;
233
234 pdn->phb = NULL;
235
236 return 0;
237}
238
239static int dlpar_add_phb(char *drc_name, struct device_node *dn)
240{
241 struct pci_controller *phb;
242
243 if (PCI_DN(dn) && PCI_DN(dn)->phb) {
244
245 return -EINVAL;
246 }
247
248 phb = init_phb_dynamic(dn);
249 if (!phb)
250 return -EIO;
251
252 if (rpaphp_add_slot(dn)) {
253 printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
254 __func__, drc_name);
255 return -EIO;
256 }
257 return 0;
258}
259
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",
268 __func__, drc_name);
269 return -EIO;
270 }
271 return 0;
272}
273
274
275
276
277
278
279
280
281
282
283
284
285
286int dlpar_add_slot(char *drc_name)
287{
288 struct device_node *dn = NULL;
289 int node_type;
290 int rc = -EIO;
291
292 if (mutex_lock_interruptible(&rpadlpar_mutex))
293 return -ERESTARTSYS;
294
295
296 dn = find_dlpar_node(drc_name, &node_type);
297 if (!dn) {
298 rc = -ENODEV;
299 goto exit;
300 }
301
302 switch (node_type) {
303 case NODE_TYPE_VIO:
304 rc = dlpar_add_vio_slot(drc_name, dn);
305 break;
306 case NODE_TYPE_SLOT:
307 rc = dlpar_add_pci_slot(drc_name, dn);
308 break;
309 case NODE_TYPE_PHB:
310 rc = dlpar_add_phb(drc_name, dn);
311 break;
312 }
313
314 printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
315exit:
316 mutex_unlock(&rpadlpar_mutex);
317 return rc;
318}
319
320
321
322
323
324
325
326
327
328
329
330static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
331{
332 struct vio_dev *vio_dev;
333
334 vio_dev = vio_find_node(dn);
335 if (!vio_dev)
336 return -EINVAL;
337
338 vio_unregister_device(vio_dev);
339 return 0;
340}
341
342
343
344
345
346
347
348
349
350
351
352
353int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
354{
355 struct pci_bus *bus;
356 struct slot *slot;
357 int ret = 0;
358
359 pci_lock_rescan_remove();
360
361 bus = pcibios_find_pci_bus(dn);
362 if (!bus) {
363 ret = -EINVAL;
364 goto out;
365 }
366
367 pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
368 bus->self ? pci_name(bus->self) : "<!PHB!>");
369
370 slot = find_php_slot(dn);
371 if (slot) {
372 pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
373 pci_domain_nr(bus), bus->number);
374
375 if (rpaphp_deregister_slot(slot)) {
376 printk(KERN_ERR
377 "%s: unable to remove hotplug slot %s\n",
378 __func__, drc_name);
379 ret = -EIO;
380 goto out;
381 }
382 }
383
384
385 pcibios_remove_pci_devices(bus);
386
387
388 if (pcibios_unmap_io_space(bus)) {
389 printk(KERN_ERR "%s: failed to unmap bus range\n",
390 __func__);
391 ret = -ERANGE;
392 goto out;
393 }
394
395
396 BUG_ON(!bus->self);
397 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
398 pci_stop_and_remove_bus_device(bus->self);
399
400 out:
401 pci_unlock_rescan_remove();
402 return ret;
403}
404
405
406
407
408
409
410
411
412
413
414
415
416
417int dlpar_remove_slot(char *drc_name)
418{
419 struct device_node *dn;
420 int node_type;
421 int rc = 0;
422
423 if (mutex_lock_interruptible(&rpadlpar_mutex))
424 return -ERESTARTSYS;
425
426 dn = find_dlpar_node(drc_name, &node_type);
427 if (!dn) {
428 rc = -ENODEV;
429 goto exit;
430 }
431
432 switch (node_type) {
433 case NODE_TYPE_VIO:
434 rc = dlpar_remove_vio_slot(drc_name, dn);
435 break;
436 case NODE_TYPE_PHB:
437 rc = dlpar_remove_phb(drc_name, dn);
438 break;
439 case NODE_TYPE_SLOT:
440 rc = dlpar_remove_pci_slot(drc_name, dn);
441 break;
442 }
443 vm_unmap_aliases();
444
445 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
446exit:
447 mutex_unlock(&rpadlpar_mutex);
448 return rc;
449}
450
451static inline int is_dlpar_capable(void)
452{
453 int rc = rtas_token("ibm,configure-connector");
454
455 return (int) (rc != RTAS_UNKNOWN_SERVICE);
456}
457
458int __init rpadlpar_io_init(void)
459{
460 int rc = 0;
461
462 if (!is_dlpar_capable()) {
463 printk(KERN_WARNING "%s: partition not DLPAR capable\n",
464 __func__);
465 return -EPERM;
466 }
467
468 rc = dlpar_sysfs_init();
469 return rc;
470}
471
472void rpadlpar_io_exit(void)
473{
474 dlpar_sysfs_exit();
475 return;
476}
477
478module_init(rpadlpar_io_init);
479module_exit(rpadlpar_io_exit);
480MODULE_LICENSE("GPL");
481