xref: /linux/drivers/pci/hotplug/rpaphp_core.c (revision 55f3538c4923e9dfca132e99ebec370e8094afda)
1 /*
2  * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3  * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15  * NON INFRINGEMENT.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * Send feedback to <lxie@us.ibm.com>
23  *
24  */
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/pci.h>
29 #include <linux/pci_hotplug.h>
30 #include <linux/smp.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
33 #include <asm/firmware.h>
34 #include <asm/eeh.h>       /* for eeh_add_device() */
35 #include <asm/rtas.h>		/* rtas_call */
36 #include <asm/pci-bridge.h>	/* for pci_controller */
37 #include "../pci.h"		/* for pci_add_new_bus */
38 				/* and pci_do_scan_bus */
39 #include "rpaphp.h"
40 
41 bool rpaphp_debug;
42 LIST_HEAD(rpaphp_slot_head);
43 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
44 
45 #define DRIVER_VERSION	"0.1"
46 #define DRIVER_AUTHOR	"Linda Xie <lxie@us.ibm.com>"
47 #define DRIVER_DESC	"RPA HOT Plug PCI Controller Driver"
48 
49 #define MAX_LOC_CODE 128
50 
51 MODULE_AUTHOR(DRIVER_AUTHOR);
52 MODULE_DESCRIPTION(DRIVER_DESC);
53 MODULE_LICENSE("GPL");
54 
55 module_param_named(debug, rpaphp_debug, bool, 0644);
56 
57 /**
58  * set_attention_status - set attention LED
59  * @hotplug_slot: target &hotplug_slot
60  * @value: LED control value
61  *
62  * echo 0 > attention -- set LED OFF
63  * echo 1 > attention -- set LED ON
64  * echo 2 > attention -- set LED ID(identify, light is blinking)
65  */
66 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
67 {
68 	int rc;
69 	struct slot *slot = (struct slot *)hotplug_slot->private;
70 
71 	switch (value) {
72 	case 0:
73 	case 1:
74 	case 2:
75 		break;
76 	default:
77 		value = 1;
78 		break;
79 	}
80 
81 	rc = rtas_set_indicator(DR_INDICATOR, slot->index, value);
82 	if (!rc)
83 		hotplug_slot->info->attention_status = value;
84 
85 	return rc;
86 }
87 
88 /**
89  * get_power_status - get power status of a slot
90  * @hotplug_slot: slot to get status
91  * @value: pointer to store status
92  */
93 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
94 {
95 	int retval, level;
96 	struct slot *slot = (struct slot *)hotplug_slot->private;
97 
98 	retval = rtas_get_power_level(slot->power_domain, &level);
99 	if (!retval)
100 		*value = level;
101 	return retval;
102 }
103 
104 /**
105  * get_attention_status - get attention LED status
106  * @hotplug_slot: slot to get status
107  * @value: pointer to store status
108  */
109 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
110 {
111 	struct slot *slot = (struct slot *)hotplug_slot->private;
112 	*value = slot->hotplug_slot->info->attention_status;
113 	return 0;
114 }
115 
116 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
117 {
118 	struct slot *slot = (struct slot *)hotplug_slot->private;
119 	int rc, state;
120 
121 	rc = rpaphp_get_sensor_state(slot, &state);
122 
123 	*value = NOT_VALID;
124 	if (rc)
125 		return rc;
126 
127 	if (state == EMPTY)
128 		*value = EMPTY;
129 	else if (state == PRESENT)
130 		*value = slot->state;
131 
132 	return 0;
133 }
134 
135 static enum pci_bus_speed get_max_bus_speed(struct slot *slot)
136 {
137 	enum pci_bus_speed speed;
138 	switch (slot->type) {
139 	case 1:
140 	case 2:
141 	case 3:
142 	case 4:
143 	case 5:
144 	case 6:
145 		speed = PCI_SPEED_33MHz;	/* speed for case 1-6 */
146 		break;
147 	case 7:
148 	case 8:
149 		speed = PCI_SPEED_66MHz;
150 		break;
151 	case 11:
152 	case 14:
153 		speed = PCI_SPEED_66MHz_PCIX;
154 		break;
155 	case 12:
156 	case 15:
157 		speed = PCI_SPEED_100MHz_PCIX;
158 		break;
159 	case 13:
160 	case 16:
161 		speed = PCI_SPEED_133MHz_PCIX;
162 		break;
163 	default:
164 		speed = PCI_SPEED_UNKNOWN;
165 		break;
166 	}
167 
168 	return speed;
169 }
170 
171 static int get_children_props(struct device_node *dn, const int **drc_indexes,
172 		const int **drc_names, const int **drc_types,
173 		const int **drc_power_domains)
174 {
175 	const int *indexes, *names, *types, *domains;
176 
177 	indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
178 	names = of_get_property(dn, "ibm,drc-names", NULL);
179 	types = of_get_property(dn, "ibm,drc-types", NULL);
180 	domains = of_get_property(dn, "ibm,drc-power-domains", NULL);
181 
182 	if (!indexes || !names || !types || !domains) {
183 		/* Slot does not have dynamically-removable children */
184 		return -EINVAL;
185 	}
186 	if (drc_indexes)
187 		*drc_indexes = indexes;
188 	if (drc_names)
189 		/* &drc_names[1] contains NULL terminated slot names */
190 		*drc_names = names;
191 	if (drc_types)
192 		/* &drc_types[1] contains NULL terminated slot types */
193 		*drc_types = types;
194 	if (drc_power_domains)
195 		*drc_power_domains = domains;
196 
197 	return 0;
198 }
199 
200 
201 /* Verify the existence of 'drc_name' and/or 'drc_type' within the
202  * current node.  First obtain it's my-drc-index property.  Next,
203  * obtain the DRC info from it's parent.  Use the my-drc-index for
204  * correlation, and obtain/validate the requested properties.
205  */
206 
207 static int rpaphp_check_drc_props_v1(struct device_node *dn, char *drc_name,
208 				char *drc_type, unsigned int my_index)
209 {
210 	char *name_tmp, *type_tmp;
211 	const int *indexes, *names;
212 	const int *types, *domains;
213 	int i, rc;
214 
215 	rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
216 	if (rc < 0) {
217 		return -EINVAL;
218 	}
219 
220 	name_tmp = (char *) &names[1];
221 	type_tmp = (char *) &types[1];
222 
223 	/* Iterate through parent properties, looking for my-drc-index */
224 	for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
225 		if ((unsigned int) indexes[i + 1] == my_index)
226 			break;
227 
228 		name_tmp += (strlen(name_tmp) + 1);
229 		type_tmp += (strlen(type_tmp) + 1);
230 	}
231 
232 	if (((drc_name == NULL) || (drc_name && !strcmp(drc_name, name_tmp))) &&
233 	    ((drc_type == NULL) || (drc_type && !strcmp(drc_type, type_tmp))))
234 		return 0;
235 
236 	return -EINVAL;
237 }
238 
239 static int rpaphp_check_drc_props_v2(struct device_node *dn, char *drc_name,
240 				char *drc_type, unsigned int my_index)
241 {
242 	struct property *info;
243 	unsigned int entries;
244 	struct of_drc_info drc;
245 	const __be32 *value;
246 	char cell_drc_name[MAX_DRC_NAME_LEN];
247 	int j, fndit;
248 
249 	info = of_find_property(dn->parent, "ibm,drc-info", NULL);
250 	if (info == NULL)
251 		return -EINVAL;
252 
253 	value = of_prop_next_u32(info, NULL, &entries);
254 	if (!value)
255 		return -EINVAL;
256 
257 	for (j = 0; j < entries; j++) {
258 		of_read_drc_info_cell(&info, &value, &drc);
259 
260 		/* Should now know end of current entry */
261 
262 		if (my_index > drc.last_drc_index)
263 			continue;
264 
265 		fndit = 1;
266 		break;
267 	}
268 	/* Found it */
269 
270 	if (fndit)
271 		sprintf(cell_drc_name, "%s%d", drc.drc_name_prefix,
272 			my_index);
273 
274 	if (((drc_name == NULL) ||
275 	     (drc_name && !strcmp(drc_name, cell_drc_name))) &&
276 	    ((drc_type == NULL) ||
277 	     (drc_type && !strcmp(drc_type, drc.drc_type))))
278 		return 0;
279 
280 	return -EINVAL;
281 }
282 
283 int rpaphp_check_drc_props(struct device_node *dn, char *drc_name,
284 			char *drc_type)
285 {
286 	const unsigned int *my_index;
287 
288 	my_index = of_get_property(dn, "ibm,my-drc-index", NULL);
289 	if (!my_index) {
290 		/* Node isn't DLPAR/hotplug capable */
291 		return -EINVAL;
292 	}
293 
294 	if (firmware_has_feature(FW_FEATURE_DRC_INFO))
295 		return rpaphp_check_drc_props_v2(dn, drc_name, drc_type,
296 						*my_index);
297 	else
298 		return rpaphp_check_drc_props_v1(dn, drc_name, drc_type,
299 						*my_index);
300 }
301 EXPORT_SYMBOL_GPL(rpaphp_check_drc_props);
302 
303 
304 static int is_php_type(char *drc_type)
305 {
306 	unsigned long value;
307 	char *endptr;
308 
309 	/* PCI Hotplug nodes have an integer for drc_type */
310 	value = simple_strtoul(drc_type, &endptr, 10);
311 	if (endptr == drc_type)
312 		return 0;
313 
314 	return 1;
315 }
316 
317 /**
318  * is_php_dn() - return 1 if this is a hotpluggable pci slot, else 0
319  * @dn: target &device_node
320  * @indexes: passed to get_children_props()
321  * @names: passed to get_children_props()
322  * @types: returned from get_children_props()
323  * @power_domains:
324  *
325  * This routine will return true only if the device node is
326  * a hotpluggable slot. This routine will return false
327  * for built-in pci slots (even when the built-in slots are
328  * dlparable.)
329  */
330 static int is_php_dn(struct device_node *dn, const int **indexes,
331 		const int **names, const int **types, const int **power_domains)
332 {
333 	const int *drc_types;
334 	int rc;
335 
336 	rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
337 	if (rc < 0)
338 		return 0;
339 
340 	if (!is_php_type((char *) &drc_types[1]))
341 		return 0;
342 
343 	*types = drc_types;
344 	return 1;
345 }
346 
347 /**
348  * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
349  * @dn: device node of slot
350  *
351  * This subroutine will register a hotpluggable slot with the
352  * PCI hotplug infrastructure. This routine is typically called
353  * during boot time, if the hotplug slots are present at boot time,
354  * or is called later, by the dlpar add code, if the slot is
355  * being dynamically added during runtime.
356  *
357  * If the device node points at an embedded (built-in) slot, this
358  * routine will just return without doing anything, since embedded
359  * slots cannot be hotplugged.
360  *
361  * To remove a slot, it suffices to call rpaphp_deregister_slot().
362  */
363 int rpaphp_add_slot(struct device_node *dn)
364 {
365 	struct slot *slot;
366 	int retval = 0;
367 	int i;
368 	const int *indexes, *names, *types, *power_domains;
369 	char *name, *type;
370 
371 	if (!dn->name || strcmp(dn->name, "pci"))
372 		return 0;
373 
374 	/* If this is not a hotplug slot, return without doing anything. */
375 	if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
376 		return 0;
377 
378 	dbg("Entry %s: dn=%pOF\n", __func__, dn);
379 
380 	/* register PCI devices */
381 	name = (char *) &names[1];
382 	type = (char *) &types[1];
383 	for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
384 		int index;
385 
386 		index = be32_to_cpu(indexes[i + 1]);
387 		slot = alloc_slot_struct(dn, index, name,
388 					 be32_to_cpu(power_domains[i + 1]));
389 		if (!slot)
390 			return -ENOMEM;
391 
392 		slot->type = simple_strtoul(type, NULL, 10);
393 
394 		dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
395 				index, name, type);
396 
397 		retval = rpaphp_enable_slot(slot);
398 		if (!retval)
399 			retval = rpaphp_register_slot(slot);
400 
401 		if (retval)
402 			dealloc_slot_struct(slot);
403 
404 		name += strlen(name) + 1;
405 		type += strlen(type) + 1;
406 	}
407 	dbg("%s - Exit: rc[%d]\n", __func__, retval);
408 
409 	/* XXX FIXME: reports a failure only if last entry in loop failed */
410 	return retval;
411 }
412 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
413 
414 static void __exit cleanup_slots(void)
415 {
416 	struct slot *slot, *next;
417 
418 	/*
419 	 * Unregister all of our slots with the pci_hotplug subsystem,
420 	 * and free up all memory that we had allocated.
421 	 * memory will be freed in release_slot callback.
422 	 */
423 
424 	list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
425 				 rpaphp_slot_list) {
426 		list_del(&slot->rpaphp_slot_list);
427 		pci_hp_deregister(slot->hotplug_slot);
428 	}
429 	return;
430 }
431 
432 static int __init rpaphp_init(void)
433 {
434 	struct device_node *dn;
435 
436 	info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
437 
438 	for_each_node_by_name(dn, "pci")
439 		rpaphp_add_slot(dn);
440 
441 	return 0;
442 }
443 
444 static void __exit rpaphp_exit(void)
445 {
446 	cleanup_slots();
447 }
448 
449 static int enable_slot(struct hotplug_slot *hotplug_slot)
450 {
451 	struct slot *slot = (struct slot *)hotplug_slot->private;
452 	int state;
453 	int retval;
454 
455 	if (slot->state == CONFIGURED)
456 		return 0;
457 
458 	retval = rpaphp_get_sensor_state(slot, &state);
459 	if (retval)
460 		return retval;
461 
462 	if (state == PRESENT) {
463 		pci_lock_rescan_remove();
464 		pci_hp_add_devices(slot->bus);
465 		pci_unlock_rescan_remove();
466 		slot->state = CONFIGURED;
467 	} else if (state == EMPTY) {
468 		slot->state = EMPTY;
469 	} else {
470 		err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
471 		slot->state = NOT_VALID;
472 		return -EINVAL;
473 	}
474 
475 	slot->bus->max_bus_speed = get_max_bus_speed(slot);
476 	return 0;
477 }
478 
479 static int disable_slot(struct hotplug_slot *hotplug_slot)
480 {
481 	struct slot *slot = (struct slot *)hotplug_slot->private;
482 	if (slot->state == NOT_CONFIGURED)
483 		return -EINVAL;
484 
485 	pci_lock_rescan_remove();
486 	pci_hp_remove_devices(slot->bus);
487 	pci_unlock_rescan_remove();
488 	vm_unmap_aliases();
489 
490 	slot->state = NOT_CONFIGURED;
491 	return 0;
492 }
493 
494 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
495 	.enable_slot = enable_slot,
496 	.disable_slot = disable_slot,
497 	.set_attention_status = set_attention_status,
498 	.get_power_status = get_power_status,
499 	.get_attention_status = get_attention_status,
500 	.get_adapter_status = get_adapter_status,
501 };
502 
503 module_init(rpaphp_init);
504 module_exit(rpaphp_exit);
505