rpadlpar_core.c 10.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Interface for Dynamic Logical Partitioning of I/O Slots on
 * RPA-compliant PPC64 platform.
 *
 * John Rose <johnrose@austin.ibm.com>
 * Linda Xie <lxie@us.ibm.com>
 *
 * October 2003
 *
 * Copyright (C) 2003 IBM.
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */
17 18 19

#undef DEBUG

L
Linus Torvalds 已提交
20
#include <linux/init.h>
21
#include <linux/module.h>
L
Linus Torvalds 已提交
22
#include <linux/pci.h>
T
Tim Schmielau 已提交
23
#include <linux/string.h>
24
#include <linux/vmalloc.h>
T
Tim Schmielau 已提交
25

L
Linus Torvalds 已提交
26
#include <asm/pci-bridge.h>
27
#include <linux/mutex.h>
L
Linus Torvalds 已提交
28
#include <asm/rtas.h>
29
#include <asm/vio.h>
T
Tim Schmielau 已提交
30

L
Linus Torvalds 已提交
31 32 33 34
#include "../pci.h"
#include "rpaphp.h"
#include "rpadlpar.h"

35
static DEFINE_MUTEX(rpadlpar_mutex);
L
Linus Torvalds 已提交
36

37 38
#define DLPAR_MODULE_NAME "rpadlpar_io"

L
Linus Torvalds 已提交
39 40 41 42
#define NODE_TYPE_VIO  1
#define NODE_TYPE_SLOT 2
#define NODE_TYPE_PHB  3

43
static struct device_node *find_vio_slot_node(char *drc_name)
L
Linus Torvalds 已提交
44 45
{
	struct device_node *parent = of_find_node_by_name(NULL, "vdevice");
46 47 48
	struct device_node *dn = NULL;
	char *name;
	int rc;
L
Linus Torvalds 已提交
49 50 51 52

	if (!parent)
		return NULL;

53 54 55 56
	while ((dn = of_get_next_child(parent, dn))) {
		rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL);
		if ((rc == 0) && (!strcmp(drc_name, name)))
			break;
L
Linus Torvalds 已提交
57 58
	}

59
	return dn;
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70
}

/* Find dlpar-capable pci node that contains the specified name and type */
static struct device_node *find_php_slot_pci_node(char *drc_name,
						  char *drc_type)
{
	struct device_node *np = NULL;
	char *name;
	char *type;
	int rc;

71
	while ((np = of_find_node_by_name(np, "pci"))) {
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80
		rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL);
		if (rc == 0)
			if (!strcmp(drc_name, name) && !strcmp(drc_type, type))
				break;
	}

	return np;
}

81
static struct device_node *find_dlpar_node(char *drc_name, int *node_type)
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
{
	struct device_node *dn;

	dn = find_php_slot_pci_node(drc_name, "SLOT");
	if (dn) {
		*node_type = NODE_TYPE_SLOT;
		return dn;
	}

	dn = find_php_slot_pci_node(drc_name, "PHB");
	if (dn) {
		*node_type = NODE_TYPE_PHB;
		return dn;
	}

97
	dn = find_vio_slot_node(drc_name);
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105
	if (dn) {
		*node_type = NODE_TYPE_VIO;
		return dn;
	}

	return NULL;
}

106 107
/**
 * find_php_slot - return hotplug slot structure for device node
R
Randy Dunlap 已提交
108
 * @dn: target &device_node
109 110 111 112 113 114 115
 *
 * This routine will return the hotplug slot structure
 * for a given device node. Note that built-in PCI slots
 * may be dlpar-able, but not hot-pluggable, so this routine
 * will return NULL for built-in PCI slots.
 */
static struct slot *find_php_slot(struct device_node *dn)
L
Linus Torvalds 已提交
116 117 118 119
{
	struct list_head *tmp, *n;
	struct slot *slot;

120 121 122 123 124
	list_for_each_safe(tmp, n, &rpaphp_slot_head) {
		slot = list_entry(tmp, struct slot, rpaphp_slot_list);
		if (slot->dn == dn)
			return slot;
	}
L
Linus Torvalds 已提交
125

126
	return NULL;
L
Linus Torvalds 已提交
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142
static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent,
					struct device_node *dev_dn)
{
	struct pci_dev *tmp = NULL;
	struct device_node *child_dn;

	list_for_each_entry(tmp, &parent->devices, bus_list) {
		child_dn = pci_device_to_OF_node(tmp);
		if (child_dn == dev_dn)
			return tmp;
	}
	return NULL;
}

143
static void dlpar_pci_add_bus(struct device_node *dn)
L
Linus Torvalds 已提交
144
{
145
	struct pci_dn *pdn = PCI_DN(dn);
146
	struct pci_controller *phb = pdn->phb;
L
Linus Torvalds 已提交
147 148
	struct pci_dev *dev = NULL;

149 150
	eeh_add_device_tree_early(dn);

151 152 153 154
	/* Add EADS device to PHB bus, adding new entry to bus->devices */
	dev = of_create_pci_dev(dn, phb->bus, pdn->devfn);
	if (!dev) {
		printk(KERN_ERR "%s: failed to create pci dev for %s\n",
155
				__func__, dn->full_name);
156
		return;
L
Linus Torvalds 已提交
157 158
	}

159
	/* Scan below the new bridge */
160 161
	if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
	    dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
162
		of_scan_pci_bridge(dev);
163

164 165
	/* Map IO space for child bus, which may or may not succeed */
	pcibios_map_io_space(dev->subordinate);
166

167 168 169 170 171 172
	/* Finish adding it : resource allocation, adding devices, etc...
	 * Note that we need to perform the finish pass on the -parent-
	 * bus of the EADS bridge so the bridge device itself gets
	 * properly added
	 */
	pcibios_finish_adding_to_bus(phb->bus);
L
Linus Torvalds 已提交
173 174
}

175
static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn)
L
Linus Torvalds 已提交
176 177
{
	struct pci_dev *dev;
178
	struct pci_controller *phb;
L
Linus Torvalds 已提交
179

180
	if (pcibios_find_pci_bus(dn))
181 182
		return -EINVAL;

L
Linus Torvalds 已提交
183
	/* Add pci bus */
184 185 186 187 188 189
	dlpar_pci_add_bus(dn);

	/* Confirm new bridge dev was created */
	phb = PCI_DN(dn)->phb;
	dev = dlpar_find_new_dev(phb->bus, dn);

L
Linus Torvalds 已提交
190
	if (!dev) {
191
		printk(KERN_ERR "%s: unable to add bus %s\n", __func__,
L
Linus Torvalds 已提交
192 193 194 195
			drc_name);
		return -EIO;
	}

196 197
	if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
		printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n",
198
			__func__, dev->hdr_type, drc_name);
199 200 201
		return -EIO;
	}

202 203 204
	/* Add hotplug slot */
	if (rpaphp_add_slot(dn)) {
		printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
205
			__func__, drc_name);
206 207
		return -EIO;
	}
L
Linus Torvalds 已提交
208 209 210
	return 0;
}

211
static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
L
Linus Torvalds 已提交
212
{
213
	struct slot *slot;
214
	struct pci_dn *pdn;
L
Linus Torvalds 已提交
215 216
	int rc = 0;

217
	if (!pcibios_find_pci_bus(dn))
218
		return -EINVAL;
L
Linus Torvalds 已提交
219

220 221
	/* If pci slot is hotplugable, use hotplug to remove it */
	slot = find_php_slot(dn);
222 223 224 225
	if (slot && rpaphp_deregister_slot(slot)) {
		printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
		       __func__, drc_name);
		return -EIO;
L
Linus Torvalds 已提交
226 227
	}

228 229
	pdn = dn->data;
	BUG_ON(!pdn || !pdn->phb);
230
	rc = remove_phb_dynamic(pdn->phb);
L
Linus Torvalds 已提交
231 232 233
	if (rc < 0)
		return rc;

234
	pdn->phb = NULL;
235

L
Linus Torvalds 已提交
236 237 238
	return 0;
}

239
static int dlpar_add_phb(char *drc_name, struct device_node *dn)
L
Linus Torvalds 已提交
240 241 242
{
	struct pci_controller *phb;

243
	if (PCI_DN(dn) && PCI_DN(dn)->phb) {
244 245 246 247
		/* PHB already exists */
		return -EINVAL;
	}

L
Linus Torvalds 已提交
248 249
	phb = init_phb_dynamic(dn);
	if (!phb)
250
		return -EIO;
L
Linus Torvalds 已提交
251

252 253
	if (rpaphp_add_slot(dn)) {
		printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
254
			__func__, drc_name);
255 256
		return -EIO;
	}
L
Linus Torvalds 已提交
257 258 259
	return 0;
}

260 261 262 263 264 265 266 267
static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
{
	if (vio_find_node(dn))
		return -EINVAL;

	if (!vio_register_device_node(dn)) {
		printk(KERN_ERR
			"%s: failed to register vio node %s\n",
268
			__func__, drc_name);
269 270 271 272 273
		return -EIO;
	}
	return 0;
}

L
Linus Torvalds 已提交
274 275 276 277
/**
 * dlpar_add_slot - DLPAR add an I/O Slot
 * @drc_name: drc-name of newly added slot
 *
R
Randy Dunlap 已提交
278 279
 * Make the hotplug module and the kernel aware of a newly added I/O Slot.
 * Return Codes:
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287 288 289
 * 0			Success
 * -ENODEV		Not a valid drc_name
 * -EINVAL		Slot already added
 * -ERESTARTSYS		Signalled before obtaining lock
 * -EIO			Internal PCI Error
 */
int dlpar_add_slot(char *drc_name)
{
	struct device_node *dn = NULL;
	int node_type;
290
	int rc = -EIO;
L
Linus Torvalds 已提交
291

292
	if (mutex_lock_interruptible(&rpadlpar_mutex))
L
Linus Torvalds 已提交
293 294
		return -ERESTARTSYS;

295 296
	/* Find newly added node */
	dn = find_dlpar_node(drc_name, &node_type);
L
Linus Torvalds 已提交
297 298 299 300 301 302 303
	if (!dn) {
		rc = -ENODEV;
		goto exit;
	}

	switch (node_type) {
		case NODE_TYPE_VIO:
304
			rc = dlpar_add_vio_slot(drc_name, dn);
L
Linus Torvalds 已提交
305 306 307 308 309
			break;
		case NODE_TYPE_SLOT:
			rc = dlpar_add_pci_slot(drc_name, dn);
			break;
		case NODE_TYPE_PHB:
310
			rc = dlpar_add_phb(drc_name, dn);
L
Linus Torvalds 已提交
311 312 313
			break;
	}

314
	printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
L
Linus Torvalds 已提交
315
exit:
316
	mutex_unlock(&rpadlpar_mutex);
L
Linus Torvalds 已提交
317 318 319 320 321 322
	return rc;
}

/**
 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot
 * @drc_name: drc-name of newly added slot
R
Randy Dunlap 已提交
323
 * @dn: &device_node
L
Linus Torvalds 已提交
324
 *
R
Randy Dunlap 已提交
325
 * Remove the kernel and hotplug representations of an I/O Slot.
L
Linus Torvalds 已提交
326 327
 * Return Codes:
 * 0			Success
328
 * -EINVAL		Vio dev doesn't exist
L
Linus Torvalds 已提交
329
 */
330
static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
L
Linus Torvalds 已提交
331
{
332
	struct vio_dev *vio_dev;
L
Linus Torvalds 已提交
333

334
	vio_dev = vio_find_node(dn);
335 336
	if (!vio_dev)
		return -EINVAL;
337 338

	vio_unregister_device(vio_dev);
L
Linus Torvalds 已提交
339 340 341 342
	return 0;
}

/**
R
Randy Dunlap 已提交
343
 * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot
L
Linus Torvalds 已提交
344
 * @drc_name: drc-name of newly added slot
R
Randy Dunlap 已提交
345
 * @dn: &device_node
L
Linus Torvalds 已提交
346
 *
R
Randy Dunlap 已提交
347
 * Remove the kernel and hotplug representations of a PCI I/O Slot.
L
Linus Torvalds 已提交
348 349 350 351 352
 * Return Codes:
 * 0			Success
 * -ENODEV		Not a valid drc_name
 * -EIO			Internal PCI Error
 */
353
int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
L
Linus Torvalds 已提交
354
{
355 356
	struct pci_bus *bus;
	struct slot *slot;
L
Linus Torvalds 已提交
357

358
	bus = pcibios_find_pci_bus(dn);
359 360 361
	if (!bus)
		return -EINVAL;

362 363 364
	pr_debug("PCI: Removing PCI slot below EADS bridge %s\n",
		 bus->self ? pci_name(bus->self) : "<!PHB!>");

365
	slot = find_php_slot(dn);
366
	if (slot) {
367 368 369
		pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n",
			 pci_domain_nr(bus), bus->number);

370
		if (rpaphp_deregister_slot(slot)) {
371 372
			printk(KERN_ERR
				"%s: unable to remove hotplug slot %s\n",
373
				__func__, drc_name);
374 375
			return -EIO;
		}
376 377 378 379
	}

	/* Remove all devices below slot */
	pcibios_remove_pci_devices(bus);
L
Linus Torvalds 已提交
380

381
	/* Unmap PCI IO space */
382
	if (pcibios_unmap_io_space(bus)) {
383
		printk(KERN_ERR "%s: failed to unmap bus range\n",
384
			__func__);
385
		return -ERANGE;
L
Linus Torvalds 已提交
386
	}
387

388
	/* Remove the EADS bridge device itself */
389
	BUG_ON(!bus->self);
390 391
	pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
	eeh_remove_bus_device(bus->self);
392
	pci_stop_and_remove_bus_device(bus->self);
393

L
Linus Torvalds 已提交
394 395 396 397 398 399 400
	return 0;
}

/**
 * dlpar_remove_slot - DLPAR remove an I/O Slot
 * @drc_name: drc-name of newly added slot
 *
R
Randy Dunlap 已提交
401
 * Remove the kernel and hotplug representations of an I/O Slot.
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410
 * Return Codes:
 * 0			Success
 * -ENODEV		Not a valid drc_name
 * -EINVAL		Slot already removed
 * -ERESTARTSYS		Signalled before obtaining lock
 * -EIO			Internal Error
 */
int dlpar_remove_slot(char *drc_name)
{
411 412
	struct device_node *dn;
	int node_type;
L
Linus Torvalds 已提交
413 414
	int rc = 0;

415
	if (mutex_lock_interruptible(&rpadlpar_mutex))
L
Linus Torvalds 已提交
416 417
		return -ERESTARTSYS;

418 419
	dn = find_dlpar_node(drc_name, &node_type);
	if (!dn) {
L
Linus Torvalds 已提交
420 421 422 423
		rc = -ENODEV;
		goto exit;
	}

424 425 426 427 428 429 430 431 432 433
	switch (node_type) {
		case NODE_TYPE_VIO:
			rc = dlpar_remove_vio_slot(drc_name, dn);
			break;
		case NODE_TYPE_PHB:
			rc = dlpar_remove_phb(drc_name, dn);
			break;
		case NODE_TYPE_SLOT:
			rc = dlpar_remove_pci_slot(drc_name, dn);
			break;
L
Linus Torvalds 已提交
434
	}
435 436
	vm_unmap_aliases();

437
	printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
L
Linus Torvalds 已提交
438
exit:
439
	mutex_unlock(&rpadlpar_mutex);
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
	return rc;
}

static inline int is_dlpar_capable(void)
{
	int rc = rtas_token("ibm,configure-connector");

	return (int) (rc != RTAS_UNKNOWN_SERVICE);
}

int __init rpadlpar_io_init(void)
{
	int rc = 0;

	if (!is_dlpar_capable()) {
		printk(KERN_WARNING "%s: partition not DLPAR capable\n",
456
			__func__);
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		return -EPERM;
	}

	rc = dlpar_sysfs_init();
	return rc;
}

void rpadlpar_io_exit(void)
{
	dlpar_sysfs_exit();
	return;
}

module_init(rpadlpar_io_init);
module_exit(rpadlpar_io_exit);
MODULE_LICENSE("GPL");