• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Support for dynamic reconfiguration for PCI, Memory, and CPU
3  * Hotplug and Dynamic Logical Partitioning on RPA platforms.
4  *
5  * Copyright (C) 2009 Nathan Fontenot
6  * Copyright (C) 2009 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  */
12 
13 #define pr_fmt(fmt)	"dlpar: " fmt
14 
15 #include <linux/kernel.h>
16 #include <linux/notifier.h>
17 #include <linux/spinlock.h>
18 #include <linux/cpu.h>
19 #include <linux/slab.h>
20 #include <linux/of.h>
21 
22 #include "of_helpers.h"
23 #include "pseries.h"
24 
25 #include <asm/prom.h>
26 #include <asm/machdep.h>
27 #include <linux/uaccess.h>
28 #include <asm/rtas.h>
29 
30 static struct workqueue_struct *pseries_hp_wq;
31 
32 struct pseries_hp_work {
33 	struct work_struct work;
34 	struct pseries_hp_errorlog *errlog;
35 	struct completion *hp_completion;
36 	int *rc;
37 };
38 
39 struct cc_workarea {
40 	__be32	drc_index;
41 	__be32	zero;
42 	__be32	name_offset;
43 	__be32	prop_length;
44 	__be32	prop_offset;
45 };
46 
dlpar_free_cc_property(struct property * prop)47 void dlpar_free_cc_property(struct property *prop)
48 {
49 	kfree(prop->name);
50 	kfree(prop->value);
51 	kfree(prop);
52 }
53 
dlpar_parse_cc_property(struct cc_workarea * ccwa)54 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
55 {
56 	struct property *prop;
57 	char *name;
58 	char *value;
59 
60 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
61 	if (!prop)
62 		return NULL;
63 
64 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
65 	prop->name = kstrdup(name, GFP_KERNEL);
66 	if (!prop->name) {
67 		dlpar_free_cc_property(prop);
68 		return NULL;
69 	}
70 
71 	prop->length = be32_to_cpu(ccwa->prop_length);
72 	value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
73 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
74 	if (!prop->value) {
75 		dlpar_free_cc_property(prop);
76 		return NULL;
77 	}
78 
79 	return prop;
80 }
81 
dlpar_parse_cc_node(struct cc_workarea * ccwa)82 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
83 {
84 	struct device_node *dn;
85 	const char *name;
86 
87 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
88 	if (!dn)
89 		return NULL;
90 
91 	name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
92 	dn->full_name = kstrdup(name, GFP_KERNEL);
93 	if (!dn->full_name) {
94 		kfree(dn);
95 		return NULL;
96 	}
97 
98 	of_node_set_flag(dn, OF_DYNAMIC);
99 	of_node_init(dn);
100 
101 	return dn;
102 }
103 
dlpar_free_one_cc_node(struct device_node * dn)104 static void dlpar_free_one_cc_node(struct device_node *dn)
105 {
106 	struct property *prop;
107 
108 	while (dn->properties) {
109 		prop = dn->properties;
110 		dn->properties = prop->next;
111 		dlpar_free_cc_property(prop);
112 	}
113 
114 	kfree(dn->full_name);
115 	kfree(dn);
116 }
117 
dlpar_free_cc_nodes(struct device_node * dn)118 void dlpar_free_cc_nodes(struct device_node *dn)
119 {
120 	if (dn->child)
121 		dlpar_free_cc_nodes(dn->child);
122 
123 	if (dn->sibling)
124 		dlpar_free_cc_nodes(dn->sibling);
125 
126 	dlpar_free_one_cc_node(dn);
127 }
128 
129 #define COMPLETE	0
130 #define NEXT_SIBLING    1
131 #define NEXT_CHILD      2
132 #define NEXT_PROPERTY   3
133 #define PREV_PARENT     4
134 #define MORE_MEMORY     5
135 #define CALL_AGAIN	-2
136 #define ERR_CFG_USE     -9003
137 
dlpar_configure_connector(__be32 drc_index,struct device_node * parent)138 struct device_node *dlpar_configure_connector(__be32 drc_index,
139 					      struct device_node *parent)
140 {
141 	struct device_node *dn;
142 	struct device_node *first_dn = NULL;
143 	struct device_node *last_dn = NULL;
144 	struct property *property;
145 	struct property *last_property = NULL;
146 	struct cc_workarea *ccwa;
147 	char *data_buf;
148 	int cc_token;
149 	int rc = -1;
150 
151 	cc_token = rtas_token("ibm,configure-connector");
152 	if (cc_token == RTAS_UNKNOWN_SERVICE)
153 		return NULL;
154 
155 	data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
156 	if (!data_buf)
157 		return NULL;
158 
159 	ccwa = (struct cc_workarea *)&data_buf[0];
160 	ccwa->drc_index = drc_index;
161 	ccwa->zero = 0;
162 
163 	do {
164 		/* Since we release the rtas_data_buf lock between configure
165 		 * connector calls we want to re-populate the rtas_data_buffer
166 		 * with the contents of the previous call.
167 		 */
168 		spin_lock(&rtas_data_buf_lock);
169 
170 		memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
171 		rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
172 		memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
173 
174 		spin_unlock(&rtas_data_buf_lock);
175 
176 		switch (rc) {
177 		case COMPLETE:
178 			break;
179 
180 		case NEXT_SIBLING:
181 			dn = dlpar_parse_cc_node(ccwa);
182 			if (!dn)
183 				goto cc_error;
184 
185 			dn->parent = last_dn->parent;
186 			last_dn->sibling = dn;
187 			last_dn = dn;
188 			break;
189 
190 		case NEXT_CHILD:
191 			dn = dlpar_parse_cc_node(ccwa);
192 			if (!dn)
193 				goto cc_error;
194 
195 			if (!first_dn) {
196 				dn->parent = parent;
197 				first_dn = dn;
198 			} else {
199 				dn->parent = last_dn;
200 				if (last_dn)
201 					last_dn->child = dn;
202 			}
203 
204 			last_dn = dn;
205 			break;
206 
207 		case NEXT_PROPERTY:
208 			property = dlpar_parse_cc_property(ccwa);
209 			if (!property)
210 				goto cc_error;
211 
212 			if (!last_dn->properties)
213 				last_dn->properties = property;
214 			else
215 				last_property->next = property;
216 
217 			last_property = property;
218 			break;
219 
220 		case PREV_PARENT:
221 			last_dn = last_dn->parent;
222 			break;
223 
224 		case CALL_AGAIN:
225 			break;
226 
227 		case MORE_MEMORY:
228 		case ERR_CFG_USE:
229 		default:
230 			printk(KERN_ERR "Unexpected Error (%d) "
231 			       "returned from configure-connector\n", rc);
232 			goto cc_error;
233 		}
234 	} while (rc);
235 
236 cc_error:
237 	kfree(data_buf);
238 
239 	if (rc) {
240 		if (first_dn)
241 			dlpar_free_cc_nodes(first_dn);
242 
243 		return NULL;
244 	}
245 
246 	return first_dn;
247 }
248 
dlpar_attach_node(struct device_node * dn,struct device_node * parent)249 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
250 {
251 	int rc;
252 
253 	dn->parent = parent;
254 
255 	rc = of_attach_node(dn);
256 	if (rc) {
257 		printk(KERN_ERR "Failed to add device node %pOF\n", dn);
258 		return rc;
259 	}
260 
261 	return 0;
262 }
263 
dlpar_detach_node(struct device_node * dn)264 int dlpar_detach_node(struct device_node *dn)
265 {
266 	struct device_node *child;
267 	int rc;
268 
269 	child = of_get_next_child(dn, NULL);
270 	while (child) {
271 		dlpar_detach_node(child);
272 		child = of_get_next_child(dn, child);
273 	}
274 
275 	rc = of_detach_node(dn);
276 	if (rc)
277 		return rc;
278 
279 	of_node_put(dn);
280 
281 	return 0;
282 }
283 
284 #define DR_ENTITY_SENSE		9003
285 #define DR_ENTITY_PRESENT	1
286 #define DR_ENTITY_UNUSABLE	2
287 #define ALLOCATION_STATE	9003
288 #define ALLOC_UNUSABLE		0
289 #define ALLOC_USABLE		1
290 #define ISOLATION_STATE		9001
291 #define ISOLATE			0
292 #define UNISOLATE		1
293 
dlpar_acquire_drc(u32 drc_index)294 int dlpar_acquire_drc(u32 drc_index)
295 {
296 	int dr_status, rc;
297 
298 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
299 		       DR_ENTITY_SENSE, drc_index);
300 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
301 		return -1;
302 
303 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
304 	if (rc)
305 		return rc;
306 
307 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
308 	if (rc) {
309 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
310 		return rc;
311 	}
312 
313 	return 0;
314 }
315 
dlpar_release_drc(u32 drc_index)316 int dlpar_release_drc(u32 drc_index)
317 {
318 	int dr_status, rc;
319 
320 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
321 		       DR_ENTITY_SENSE, drc_index);
322 	if (rc || dr_status != DR_ENTITY_PRESENT)
323 		return -1;
324 
325 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
326 	if (rc)
327 		return rc;
328 
329 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
330 	if (rc) {
331 		rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
332 		return rc;
333 	}
334 
335 	return 0;
336 }
337 
handle_dlpar_errorlog(struct pseries_hp_errorlog * hp_elog)338 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
339 {
340 	int rc;
341 
342 	/* pseries error logs are in BE format, convert to cpu type */
343 	switch (hp_elog->id_type) {
344 	case PSERIES_HP_ELOG_ID_DRC_COUNT:
345 		hp_elog->_drc_u.drc_count =
346 				be32_to_cpu(hp_elog->_drc_u.drc_count);
347 		break;
348 	case PSERIES_HP_ELOG_ID_DRC_INDEX:
349 		hp_elog->_drc_u.drc_index =
350 				be32_to_cpu(hp_elog->_drc_u.drc_index);
351 		break;
352 	case PSERIES_HP_ELOG_ID_DRC_IC:
353 		hp_elog->_drc_u.ic.count =
354 				be32_to_cpu(hp_elog->_drc_u.ic.count);
355 		hp_elog->_drc_u.ic.index =
356 				be32_to_cpu(hp_elog->_drc_u.ic.index);
357 	}
358 
359 	switch (hp_elog->resource) {
360 	case PSERIES_HP_ELOG_RESOURCE_MEM:
361 		rc = dlpar_memory(hp_elog);
362 		break;
363 	case PSERIES_HP_ELOG_RESOURCE_CPU:
364 		rc = dlpar_cpu(hp_elog);
365 		break;
366 	default:
367 		pr_warn_ratelimited("Invalid resource (%d) specified\n",
368 				    hp_elog->resource);
369 		rc = -EINVAL;
370 	}
371 
372 	return rc;
373 }
374 
pseries_hp_work_fn(struct work_struct * work)375 static void pseries_hp_work_fn(struct work_struct *work)
376 {
377 	struct pseries_hp_work *hp_work =
378 			container_of(work, struct pseries_hp_work, work);
379 
380 	if (hp_work->rc)
381 		*(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
382 	else
383 		handle_dlpar_errorlog(hp_work->errlog);
384 
385 	if (hp_work->hp_completion)
386 		complete(hp_work->hp_completion);
387 
388 	kfree(hp_work->errlog);
389 	kfree((void *)work);
390 }
391 
queue_hotplug_event(struct pseries_hp_errorlog * hp_errlog,struct completion * hotplug_done,int * rc)392 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
393 			 struct completion *hotplug_done, int *rc)
394 {
395 	struct pseries_hp_work *work;
396 	struct pseries_hp_errorlog *hp_errlog_copy;
397 
398 	hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
399 				 GFP_KERNEL);
400 	memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
401 
402 	work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
403 	if (work) {
404 		INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
405 		work->errlog = hp_errlog_copy;
406 		work->hp_completion = hotplug_done;
407 		work->rc = rc;
408 		queue_work(pseries_hp_wq, (struct work_struct *)work);
409 	} else {
410 		*rc = -ENOMEM;
411 		kfree(hp_errlog_copy);
412 		complete(hotplug_done);
413 	}
414 }
415 
dlpar_parse_resource(char ** cmd,struct pseries_hp_errorlog * hp_elog)416 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
417 {
418 	char *arg;
419 
420 	arg = strsep(cmd, " ");
421 	if (!arg)
422 		return -EINVAL;
423 
424 	if (sysfs_streq(arg, "memory")) {
425 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
426 	} else if (sysfs_streq(arg, "cpu")) {
427 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
428 	} else {
429 		pr_err("Invalid resource specified.\n");
430 		return -EINVAL;
431 	}
432 
433 	return 0;
434 }
435 
dlpar_parse_action(char ** cmd,struct pseries_hp_errorlog * hp_elog)436 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
437 {
438 	char *arg;
439 
440 	arg = strsep(cmd, " ");
441 	if (!arg)
442 		return -EINVAL;
443 
444 	if (sysfs_streq(arg, "add")) {
445 		hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
446 	} else if (sysfs_streq(arg, "remove")) {
447 		hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
448 	} else {
449 		pr_err("Invalid action specified.\n");
450 		return -EINVAL;
451 	}
452 
453 	return 0;
454 }
455 
dlpar_parse_id_type(char ** cmd,struct pseries_hp_errorlog * hp_elog)456 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
457 {
458 	char *arg;
459 	u32 count, index;
460 
461 	arg = strsep(cmd, " ");
462 	if (!arg)
463 		return -EINVAL;
464 
465 	if (sysfs_streq(arg, "indexed-count")) {
466 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
467 		arg = strsep(cmd, " ");
468 		if (!arg) {
469 			pr_err("No DRC count specified.\n");
470 			return -EINVAL;
471 		}
472 
473 		if (kstrtou32(arg, 0, &count)) {
474 			pr_err("Invalid DRC count specified.\n");
475 			return -EINVAL;
476 		}
477 
478 		arg = strsep(cmd, " ");
479 		if (!arg) {
480 			pr_err("No DRC Index specified.\n");
481 			return -EINVAL;
482 		}
483 
484 		if (kstrtou32(arg, 0, &index)) {
485 			pr_err("Invalid DRC Index specified.\n");
486 			return -EINVAL;
487 		}
488 
489 		hp_elog->_drc_u.ic.count = cpu_to_be32(count);
490 		hp_elog->_drc_u.ic.index = cpu_to_be32(index);
491 	} else if (sysfs_streq(arg, "index")) {
492 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
493 		arg = strsep(cmd, " ");
494 		if (!arg) {
495 			pr_err("No DRC Index specified.\n");
496 			return -EINVAL;
497 		}
498 
499 		if (kstrtou32(arg, 0, &index)) {
500 			pr_err("Invalid DRC Index specified.\n");
501 			return -EINVAL;
502 		}
503 
504 		hp_elog->_drc_u.drc_index = cpu_to_be32(index);
505 	} else if (sysfs_streq(arg, "count")) {
506 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
507 		arg = strsep(cmd, " ");
508 		if (!arg) {
509 			pr_err("No DRC count specified.\n");
510 			return -EINVAL;
511 		}
512 
513 		if (kstrtou32(arg, 0, &count)) {
514 			pr_err("Invalid DRC count specified.\n");
515 			return -EINVAL;
516 		}
517 
518 		hp_elog->_drc_u.drc_count = cpu_to_be32(count);
519 	} else {
520 		pr_err("Invalid id_type specified.\n");
521 		return -EINVAL;
522 	}
523 
524 	return 0;
525 }
526 
dlpar_store(struct class * class,struct class_attribute * attr,const char * buf,size_t count)527 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
528 			   const char *buf, size_t count)
529 {
530 	struct pseries_hp_errorlog *hp_elog;
531 	struct completion hotplug_done;
532 	char *argbuf;
533 	char *args;
534 	int rc;
535 
536 	args = argbuf = kstrdup(buf, GFP_KERNEL);
537 	hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
538 	if (!hp_elog || !argbuf) {
539 		pr_info("Could not allocate resources for DLPAR operation\n");
540 		kfree(argbuf);
541 		kfree(hp_elog);
542 		return -ENOMEM;
543 	}
544 
545 	/*
546 	 * Parse out the request from the user, this will be in the form:
547 	 * <resource> <action> <id_type> <id>
548 	 */
549 	rc = dlpar_parse_resource(&args, hp_elog);
550 	if (rc)
551 		goto dlpar_store_out;
552 
553 	rc = dlpar_parse_action(&args, hp_elog);
554 	if (rc)
555 		goto dlpar_store_out;
556 
557 	rc = dlpar_parse_id_type(&args, hp_elog);
558 	if (rc)
559 		goto dlpar_store_out;
560 
561 	init_completion(&hotplug_done);
562 	queue_hotplug_event(hp_elog, &hotplug_done, &rc);
563 	wait_for_completion(&hotplug_done);
564 
565 dlpar_store_out:
566 	kfree(argbuf);
567 	kfree(hp_elog);
568 
569 	if (rc)
570 		pr_err("Could not handle DLPAR request \"%s\"\n", buf);
571 
572 	return rc ? rc : count;
573 }
574 
dlpar_show(struct class * class,struct class_attribute * attr,char * buf)575 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
576 			  char *buf)
577 {
578 	return sprintf(buf, "%s\n", "memory,cpu");
579 }
580 
581 static CLASS_ATTR_RW(dlpar);
582 
dlpar_workqueue_init(void)583 int __init dlpar_workqueue_init(void)
584 {
585 	if (pseries_hp_wq)
586 		return 0;
587 
588 	pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
589 			WQ_UNBOUND, 1);
590 
591 	return pseries_hp_wq ? 0 : -ENOMEM;
592 }
593 
dlpar_sysfs_init(void)594 static int __init dlpar_sysfs_init(void)
595 {
596 	int rc;
597 
598 	rc = dlpar_workqueue_init();
599 	if (rc)
600 		return rc;
601 
602 	return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
603 }
604 machine_device_initcall(pseries, dlpar_sysfs_init);
605 
606