rpaphp_core.c 10.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
 *
 * All rights reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to <lxie@us.ibm.com>
 *
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/pci.h>
29
#include <linux/pci_hotplug.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <linux/slab.h>
#include <linux/smp.h>
#include <linux/smp_lock.h>
#include <linux/init.h>
#include <asm/eeh.h>       /* for eeh_add_device() */
#include <asm/rtas.h>		/* rtas_call */
#include <asm/pci-bridge.h>	/* for pci_controller */
#include "../pci.h"		/* for pci_add_new_bus */
				/* and pci_do_scan_bus */
#include "rpaphp.h"

int debug;
static struct semaphore rpaphp_sem;
LIST_HEAD(rpaphp_slot_head);

#define DRIVER_VERSION	"0.1"
#define DRIVER_AUTHOR	"Linda Xie <lxie@us.ibm.com>"
#define DRIVER_DESC	"RPA HOT Plug PCI Controller Driver"

#define MAX_LOC_CODE 128

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

module_param(debug, bool, 0644);

static int rpaphp_get_attention_status(struct slot *slot)
{
	return slot->hotplug_slot->info->attention_status;
}

/**
 * set_attention_status - set attention LED
 * echo 0 > attention -- set LED OFF
 * echo 1 > attention -- set LED ON
 * echo 2 > attention -- set LED ID(identify, light is blinking)
 *
 */
static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
{
	int retval = 0;
	struct slot *slot = (struct slot *)hotplug_slot->private;

	down(&rpaphp_sem);
	switch (value) {
	case 0:
		retval = rpaphp_set_attention_status(slot, LED_OFF);
		hotplug_slot->info->attention_status = 0;
		break;
	case 1:
	default:
		retval = rpaphp_set_attention_status(slot, LED_ON);
		hotplug_slot->info->attention_status = 1;
		break;
	case 2:
		retval = rpaphp_set_attention_status(slot, LED_ID);
		hotplug_slot->info->attention_status = 2;
		break;
	}
	up(&rpaphp_sem);
	return retval;
}

/**
 * get_power_status - get power status of a slot
 * @hotplug_slot: slot to get status
 * @value: pointer to store status
 *
 *
 */
static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
{
103
	int retval, level;
L
Linus Torvalds 已提交
104 105 106
	struct slot *slot = (struct slot *)hotplug_slot->private;

	down(&rpaphp_sem);
107 108 109
	retval = rtas_get_power_level (slot->power_domain, &level);
	if (!retval)
		*value = level;
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	up(&rpaphp_sem);
	return retval;
}

/**
 * get_attention_status - get attention LED status
 *
 *
 */
static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
{
	int retval = 0;
	struct slot *slot = (struct slot *)hotplug_slot->private;

	down(&rpaphp_sem);
	*value = rpaphp_get_attention_status(slot);
	up(&rpaphp_sem);
	return retval;
}

static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
{
	struct slot *slot = (struct slot *)hotplug_slot->private;
133
	int rc, state;
L
Linus Torvalds 已提交
134 135

	down(&rpaphp_sem);
136
	rc = rpaphp_get_sensor_state(slot, &state);
L
Linus Torvalds 已提交
137
	up(&rpaphp_sem);
138 139 140 141 142 143 144 145 146 147 148

	*value = NOT_VALID;
	if (rc)
		return rc;

	if (state == EMPTY)
		*value = EMPTY;
	else if (state == PRESENT)
		*value = slot->state;

	return 0;
L
Linus Torvalds 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
}

static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
{
	struct slot *slot = (struct slot *)hotplug_slot->private;

	down(&rpaphp_sem);
	switch (slot->type) {
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
		*value = PCI_SPEED_33MHz;	/* speed for case 1-6 */
		break;
	case 7:
	case 8:
		*value = PCI_SPEED_66MHz;
		break;
	case 11:
	case 14:
		*value = PCI_SPEED_66MHz_PCIX;
		break;
	case 12:
	case 15:
		*value = PCI_SPEED_100MHz_PCIX;
		break;
	case 13:
	case 16:
		*value = PCI_SPEED_133MHz_PCIX;
		break;
	default:
		*value = PCI_SPEED_UNKNOWN;
		break;

	}
	up(&rpaphp_sem);
	return 0;
}

190 191 192
static int get_children_props(struct device_node *dn, const int **drc_indexes,
		const int **drc_names, const int **drc_types,
		const int **drc_power_domains)
L
Linus Torvalds 已提交
193
{
194
	const int *indexes, *names, *types, *domains;
L
Linus Torvalds 已提交
195

196 197 198 199
	indexes = get_property(dn, "ibm,drc-indexes", NULL);
	names = get_property(dn, "ibm,drc-names", NULL);
	types = get_property(dn, "ibm,drc-types", NULL);
	domains = get_property(dn, "ibm,drc-power-domains", NULL);
L
Linus Torvalds 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

	if (!indexes || !names || !types || !domains) {
		/* Slot does not have dynamically-removable children */
		return -EINVAL;
	}
	if (drc_indexes)
		*drc_indexes = indexes;
	if (drc_names)
		/* &drc_names[1] contains NULL terminated slot names */
		*drc_names = names;
	if (drc_types)
		/* &drc_types[1] contains NULL terminated slot types */
		*drc_types = types;
	if (drc_power_domains)
		*drc_power_domains = domains;

	return 0;
}

/* To get the DRC props describing the current node, first obtain it's
 * my-drc-index property.  Next obtain the DRC list from it's parent.  Use
 * the my-drc-index for correlation, and obtain the requested properties.
 */
int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
		char **drc_name, char **drc_type, int *drc_power_domain)
{
226 227 228
	const int *indexes, *names;
	const int *types, *domains;
	const unsigned int *my_index;
L
Linus Torvalds 已提交
229 230 231
	char *name_tmp, *type_tmp;
	int i, rc;

232
	my_index = get_property(dn, "ibm,my-drc-index", NULL);
L
Linus Torvalds 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	if (!my_index) {
		/* Node isn't DLPAR/hotplug capable */
		return -EINVAL;
	}

	rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
	if (rc < 0) {
		return -EINVAL;
	}

	name_tmp = (char *) &names[1];
	type_tmp = (char *) &types[1];

	/* Iterate through parent properties, looking for my-drc-index */
	for (i = 0; i < indexes[0]; i++) {
		if ((unsigned int) indexes[i + 1] == *my_index) {
			if (drc_name)
                		*drc_name = name_tmp;
			if (drc_type)
				*drc_type = type_tmp;
			if (drc_index)
				*drc_index = *my_index;
			if (drc_power_domain)
				*drc_power_domain = domains[i+1];
			return 0;
		}
		name_tmp += (strlen(name_tmp) + 1);
		type_tmp += (strlen(type_tmp) + 1);
	}

	return -EINVAL;
}

static int is_php_type(char *drc_type)
{
	unsigned long value;
	char *endptr;

	/* PCI Hotplug nodes have an integer for drc_type */
	value = simple_strtoul(drc_type, &endptr, 10);
	if (endptr == drc_type)
		return 0;

	return 1;
}

279 280
static int is_php_dn(struct device_node *dn, const int **indexes,
		const int **names, const int **types, const int **power_domains)
L
Linus Torvalds 已提交
281
{
282
	const int *drc_types;
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
	int rc;

	rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
	if (rc >= 0) {
		if (is_php_type((char *) &drc_types[1])) {
			*types = drc_types;
			return 1;
		}
	}

	return 0;
}

296 297 298
/**
 * rpaphp_add_slot -- add hotplug or dlpar slot
 *
L
Linus Torvalds 已提交
299 300 301 302 303
 *	rpaphp not only registers PCI hotplug slots(HOTPLUG), 
 *	but also logical DR slots(EMBEDDED).
 *	HOTPLUG slot: An adapter can be physically added/removed. 
 *	EMBEDDED slot: An adapter can be logically removed/added
 *		  from/to a partition with the slot.
304
 */
L
Linus Torvalds 已提交
305 306 307 308
int rpaphp_add_slot(struct device_node *dn)
{
	struct slot *slot;
	int retval = 0;
309
	int i;
310
	const int *indexes, *names, *types, *power_domains;
L
Linus Torvalds 已提交
311 312
	char *name, *type;

313 314 315 316 317 318
	if (!dn->name || strcmp(dn->name, "pci"))
		return 0;

	if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
		return 0;

L
Linus Torvalds 已提交
319 320 321
	dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);

	/* register PCI devices */
322 323 324 325 326 327 328 329 330
	name = (char *) &names[1];
	type = (char *) &types[1];
	for (i = 0; i < indexes[0]; i++) {

		slot = alloc_slot_struct(dn, indexes[i + 1], name, power_domains[i + 1]);
		if (!slot)
			return -ENOMEM;

		slot->type = simple_strtoul(type, NULL, 10);
L
Linus Torvalds 已提交
331
				
332 333
		dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
				indexes[i + 1], name, type);
L
Linus Torvalds 已提交
334

335
		retval = rpaphp_register_pci_slot(slot);
336 337 338
		if (retval)
			dealloc_slot_struct(slot);

339 340
		name += strlen(name) + 1;
		type += strlen(type) + 1;
L
Linus Torvalds 已提交
341
	}
342
	dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
343 344

	/* XXX FIXME: reports a failure only if last entry in loop failed */
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	return retval;
}

static void __exit cleanup_slots(void)
{
	struct list_head *tmp, *n;
	struct slot *slot;

	/*
	 * Unregister all of our slots with the pci_hotplug subsystem,
	 * and free up all memory that we had allocated.
	 * memory will be freed in release_slot callback. 
	 */

	list_for_each_safe(tmp, n, &rpaphp_slot_head) {
		slot = list_entry(tmp, struct slot, rpaphp_slot_list);
		list_del(&slot->rpaphp_slot_list);
		pci_hp_deregister(slot->hotplug_slot);
	}
	return;
}

static int __init rpaphp_init(void)
{
369 370
	struct device_node *dn = NULL;

L
Linus Torvalds 已提交
371
	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
372 373
	init_MUTEX(&rpaphp_sem);

374
	while ((dn = of_find_node_by_name(dn, "pci")))
375 376 377
		rpaphp_add_slot(dn);

	return 0;
L
Linus Torvalds 已提交
378 379 380 381 382 383 384
}

static void __exit rpaphp_exit(void)
{
	cleanup_slots();
}

385
static int __enable_slot(struct slot *slot)
L
Linus Torvalds 已提交
386
{
387 388
	int state;
	int retval;
L
Linus Torvalds 已提交
389

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	if (slot->state == CONFIGURED)
		return 0;

	retval = rpaphp_get_sensor_state(slot, &state);
	if (retval)
		return retval;

	if (state == PRESENT) {
		pcibios_add_pci_devices(slot->bus);
		slot->state = CONFIGURED;
	} else if (state == EMPTY) {
		slot->state = EMPTY;
	} else {
		err("%s: slot[%s] is in invalid state\n", __FUNCTION__, slot->name);
		slot->state = NOT_VALID;
		return -EINVAL;
L
Linus Torvalds 已提交
406
	}
407 408 409 410 411 412 413
	return 0;
}

static int enable_slot(struct hotplug_slot *hotplug_slot)
{
	int retval;
	struct slot *slot = (struct slot *)hotplug_slot->private;
L
Linus Torvalds 已提交
414 415

	down(&rpaphp_sem);
416
	retval = __enable_slot(slot);
L
Linus Torvalds 已提交
417
	up(&rpaphp_sem);
418

L
Linus Torvalds 已提交
419 420 421
	return retval;
}

422
static int __disable_slot(struct slot *slot)
L
Linus Torvalds 已提交
423
{
424
	struct pci_dev *dev, *tmp;
L
Linus Torvalds 已提交
425

426 427
	if (slot->state == NOT_CONFIGURED)
		return -EINVAL;
L
Linus Torvalds 已提交
428

429 430 431
	list_for_each_entry_safe(dev, tmp, &slot->bus->devices, bus_list) {
		eeh_remove_bus_device(dev);
		pci_remove_bus_device(dev);
L
Linus Torvalds 已提交
432 433
	}

434 435 436 437 438 439 440 441 442
	slot->state = NOT_CONFIGURED;
	return 0;
}

static int disable_slot(struct hotplug_slot *hotplug_slot)
{
	struct slot *slot = (struct slot *)hotplug_slot->private;
	int retval;

L
Linus Torvalds 已提交
443
	down(&rpaphp_sem);
444
	retval = __disable_slot (slot);
L
Linus Torvalds 已提交
445
	up(&rpaphp_sem);
446

L
Linus Torvalds 已提交
447 448 449
	return retval;
}

450 451 452 453 454 455 456 457 458 459 460
struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
	.owner = THIS_MODULE,
	.enable_slot = enable_slot,
	.disable_slot = disable_slot,
	.set_attention_status = set_attention_status,
	.get_power_status = get_power_status,
	.get_attention_status = get_attention_status,
	.get_adapter_status = get_adapter_status,
	.get_max_bus_speed = get_max_bus_speed,
};

L
Linus Torvalds 已提交
461 462 463 464 465 466
module_init(rpaphp_init);
module_exit(rpaphp_exit);

EXPORT_SYMBOL_GPL(rpaphp_add_slot);
EXPORT_SYMBOL_GPL(rpaphp_slot_head);
EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);