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