rpaphp_core.c 10.9 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
#include <linux/smp.h>
#include <linux/init.h>
32
#include <linux/vmalloc.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39
#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"

40
bool rpaphp_debug;
L
Linus Torvalds 已提交
41
LIST_HEAD(rpaphp_slot_head);
42
EXPORT_SYMBOL_GPL(rpaphp_slot_head);
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53

#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");

54
module_param_named(debug, rpaphp_debug, bool, 0644);
L
Linus Torvalds 已提交
55 56 57

/**
 * set_attention_status - set attention LED
R
Randy Dunlap 已提交
58 59 60
 * @hotplug_slot: target &hotplug_slot
 * @value: LED control value
 *
L
Linus Torvalds 已提交
61 62 63 64 65 66
 * 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)
{
67
	int rc;
L
Linus Torvalds 已提交
68 69 70 71 72 73
	struct slot *slot = (struct slot *)hotplug_slot->private;

	switch (value) {
	case 0:
	case 1:
	case 2:
74 75 76
		break;
	default:
		value = 1;
L
Linus Torvalds 已提交
77 78
		break;
	}
79 80 81 82 83 84

	rc = rtas_set_indicator(DR_INDICATOR, slot->index, value);
	if (!rc)
		hotplug_slot->info->attention_status = value;

	return rc;
L
Linus Torvalds 已提交
85 86 87 88 89 90 91
}

/**
 * get_power_status - get power status of a slot
 * @hotplug_slot: slot to get status
 * @value: pointer to store status
 */
R
Ryan Desfosses 已提交
92
static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
93
{
94
	int retval, level;
L
Linus Torvalds 已提交
95 96
	struct slot *slot = (struct slot *)hotplug_slot->private;

B
Bogicevic Sasa 已提交
97
	retval = rtas_get_power_level(slot->power_domain, &level);
98 99
	if (!retval)
		*value = level;
L
Linus Torvalds 已提交
100 101 102 103 104
	return retval;
}

/**
 * get_attention_status - get attention LED status
R
Randy Dunlap 已提交
105 106
 * @hotplug_slot: slot to get status
 * @value: pointer to store status
L
Linus Torvalds 已提交
107
 */
R
Ryan Desfosses 已提交
108
static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
109 110
{
	struct slot *slot = (struct slot *)hotplug_slot->private;
111 112
	*value = slot->hotplug_slot->info->attention_status;
	return 0;
L
Linus Torvalds 已提交
113 114
}

R
Ryan Desfosses 已提交
115
static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
L
Linus Torvalds 已提交
116 117
{
	struct slot *slot = (struct slot *)hotplug_slot->private;
118
	int rc, state;
L
Linus Torvalds 已提交
119

120 121 122 123 124 125 126 127 128 129 130 131
	rc = rpaphp_get_sensor_state(slot, &state);

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

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

	return 0;
L
Linus Torvalds 已提交
132 133
}

134
static enum pci_bus_speed get_max_bus_speed(struct slot *slot)
L
Linus Torvalds 已提交
135
{
136
	enum pci_bus_speed speed;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143
	switch (slot->type) {
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
144
		speed = PCI_SPEED_33MHz;	/* speed for case 1-6 */
L
Linus Torvalds 已提交
145 146 147
		break;
	case 7:
	case 8:
148
		speed = PCI_SPEED_66MHz;
L
Linus Torvalds 已提交
149 150 151
		break;
	case 11:
	case 14:
152
		speed = PCI_SPEED_66MHz_PCIX;
L
Linus Torvalds 已提交
153 154 155
		break;
	case 12:
	case 15:
156
		speed = PCI_SPEED_100MHz_PCIX;
L
Linus Torvalds 已提交
157 158 159
		break;
	case 13:
	case 16:
160
		speed = PCI_SPEED_133MHz_PCIX;
L
Linus Torvalds 已提交
161 162
		break;
	default:
163
		speed = PCI_SPEED_UNKNOWN;
L
Linus Torvalds 已提交
164 165
		break;
	}
166 167

	return speed;
L
Linus Torvalds 已提交
168 169
}

170 171 172
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 已提交
173
{
174
	const int *indexes, *names, *types, *domains;
L
Linus Torvalds 已提交
175

176 177 178 179
	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
	names = of_get_property(dn, "ibm,drc-names", NULL);
	types = of_get_property(dn, "ibm,drc-types", NULL);
	domains = of_get_property(dn, "ibm,drc-power-domains", NULL);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	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)
{
206 207 208
	const int *indexes, *names;
	const int *types, *domains;
	const unsigned int *my_index;
L
Linus Torvalds 已提交
209 210 211
	char *name_tmp, *type_tmp;
	int i, rc;

212
	my_index = of_get_property(dn, "ibm,my-drc-index", NULL);
L
Linus Torvalds 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
	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 */
227
	for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
L
Linus Torvalds 已提交
228 229
		if ((unsigned int) indexes[i + 1] == *my_index) {
			if (drc_name)
230
				*drc_name = name_tmp;
L
Linus Torvalds 已提交
231 232 233
			if (drc_type)
				*drc_type = type_tmp;
			if (drc_index)
234
				*drc_index = be32_to_cpu(*my_index);
L
Linus Torvalds 已提交
235
			if (drc_power_domain)
236
				*drc_power_domain = be32_to_cpu(domains[i+1]);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244
			return 0;
		}
		name_tmp += (strlen(name_tmp) + 1);
		type_tmp += (strlen(type_tmp) + 1);
	}

	return -EINVAL;
}
245
EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259

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;
}

260 261
/**
 * is_php_dn() - return 1 if this is a hotpluggable pci slot, else 0
R
Randy Dunlap 已提交
262 263 264 265 266
 * @dn: target &device_node
 * @indexes: passed to get_children_props()
 * @names: passed to get_children_props()
 * @types: returned from get_children_props()
 * @power_domains:
267 268 269 270 271 272
 *
 * This routine will return true only if the device node is
 * a hotpluggable slot. This routine will return false
 * for built-in pci slots (even when the built-in slots are
 * dlparable.)
 */
273 274
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 已提交
275
{
276
	const int *drc_types;
L
Linus Torvalds 已提交
277 278 279
	int rc;

	rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
280 281
	if (rc < 0)
		return 0;
L
Linus Torvalds 已提交
282

283 284 285 286 287
	if (!is_php_type((char *) &drc_types[1]))
		return 0;

	*types = drc_types;
	return 1;
L
Linus Torvalds 已提交
288 289
}

290
/**
291
 * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
R
Randy Dunlap 已提交
292
 * @dn: device node of slot
293
 *
294
 * This subroutine will register a hotpluggable slot with the
L
Lucas De Marchi 已提交
295
 * PCI hotplug infrastructure. This routine is typically called
296 297 298 299 300 301 302
 * during boot time, if the hotplug slots are present at boot time,
 * or is called later, by the dlpar add code, if the slot is
 * being dynamically added during runtime.
 *
 * If the device node points at an embedded (built-in) slot, this
 * routine will just return without doing anything, since embedded
 * slots cannot be hotplugged.
303
 *
R
Randy Dunlap 已提交
304
 * To remove a slot, it suffices to call rpaphp_deregister_slot().
305
 */
L
Linus Torvalds 已提交
306 307 308 309
int rpaphp_add_slot(struct device_node *dn)
{
	struct slot *slot;
	int retval = 0;
310
	int i;
311
	const int *indexes, *names, *types, *power_domains;
L
Linus Torvalds 已提交
312 313
	char *name, *type;

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

317
	/* If this is not a hotplug slot, return without doing anything. */
318 319 320
	if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
		return 0;

321
	dbg("Entry %s: dn=%pOF\n", __func__, dn);
L
Linus Torvalds 已提交
322 323

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

329 330 331
		index = be32_to_cpu(indexes[i + 1]);
		slot = alloc_slot_struct(dn, index, name,
					 be32_to_cpu(power_domains[i + 1]));
332 333 334 335
		if (!slot)
			return -ENOMEM;

		slot->type = simple_strtoul(type, NULL, 10);
336

337
		dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
338
				index, name, type);
L
Linus Torvalds 已提交
339

340
		retval = rpaphp_enable_slot(slot);
341 342 343
		if (!retval)
			retval = rpaphp_register_slot(slot);

344 345 346
		if (retval)
			dealloc_slot_struct(slot);

347 348
		name += strlen(name) + 1;
		type += strlen(type) + 1;
L
Linus Torvalds 已提交
349
	}
350
	dbg("%s - Exit: rc[%d]\n", __func__, retval);
351 352

	/* XXX FIXME: reports a failure only if last entry in loop failed */
L
Linus Torvalds 已提交
353 354
	return retval;
}
355
EXPORT_SYMBOL_GPL(rpaphp_add_slot);
L
Linus Torvalds 已提交
356 357 358

static void __exit cleanup_slots(void)
{
359
	struct slot *slot, *next;
L
Linus Torvalds 已提交
360 361 362 363

	/*
	 * Unregister all of our slots with the pci_hotplug subsystem,
	 * and free up all memory that we had allocated.
364
	 * memory will be freed in release_slot callback.
L
Linus Torvalds 已提交
365 366
	 */

367 368
	list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
				 rpaphp_slot_list) {
L
Linus Torvalds 已提交
369 370 371 372 373 374 375 376
		list_del(&slot->rpaphp_slot_list);
		pci_hp_deregister(slot->hotplug_slot);
	}
	return;
}

static int __init rpaphp_init(void)
{
377
	struct device_node *dn;
378

L
Linus Torvalds 已提交
379
	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
380

381
	for_each_node_by_name(dn, "pci")
382 383 384
		rpaphp_add_slot(dn);

	return 0;
L
Linus Torvalds 已提交
385 386 387 388 389 390 391
}

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

L
Linas Vepstas 已提交
392
static int enable_slot(struct hotplug_slot *hotplug_slot)
L
Linus Torvalds 已提交
393
{
L
Linas Vepstas 已提交
394
	struct slot *slot = (struct slot *)hotplug_slot->private;
395 396
	int state;
	int retval;
L
Linus Torvalds 已提交
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) {
406
		pci_lock_rescan_remove();
407
		pci_hp_add_devices(slot->bus);
408
		pci_unlock_rescan_remove();
409 410 411 412
		slot->state = CONFIGURED;
	} else if (state == EMPTY) {
		slot->state = EMPTY;
	} else {
413
		err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
414 415
		slot->state = NOT_VALID;
		return -EINVAL;
L
Linus Torvalds 已提交
416
	}
417 418

	slot->bus->max_bus_speed = get_max_bus_speed(slot);
419 420 421
	return 0;
}

L
Linas Vepstas 已提交
422
static int disable_slot(struct hotplug_slot *hotplug_slot)
423 424
{
	struct slot *slot = (struct slot *)hotplug_slot->private;
425 426
	if (slot->state == NOT_CONFIGURED)
		return -EINVAL;
L
Linus Torvalds 已提交
427

428
	pci_lock_rescan_remove();
429
	pci_hp_remove_devices(slot->bus);
430
	pci_unlock_rescan_remove();
431 432
	vm_unmap_aliases();

433 434 435 436
	slot->state = NOT_CONFIGURED;
	return 0;
}

437 438 439 440 441 442 443 444 445
struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
	.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,
};

L
Linus Torvalds 已提交
446 447
module_init(rpaphp_init);
module_exit(rpaphp_exit);