xref: /openbmc/linux/arch/powerpc/platforms/pseries/dlpar.c (revision 06665989d2af86fd3cae14a0fbb4d8ecd2c5b6ee)
1ab519a01SNathan Fontenot /*
2ab519a01SNathan Fontenot  * Support for dynamic reconfiguration for PCI, Memory, and CPU
3ab519a01SNathan Fontenot  * Hotplug and Dynamic Logical Partitioning on RPA platforms.
4ab519a01SNathan Fontenot  *
5ab519a01SNathan Fontenot  * Copyright (C) 2009 Nathan Fontenot
6ab519a01SNathan Fontenot  * Copyright (C) 2009 IBM Corporation
7ab519a01SNathan Fontenot  *
8ab519a01SNathan Fontenot  * This program is free software; you can redistribute it and/or
9ab519a01SNathan Fontenot  * modify it under the terms of the GNU General Public License version
10ab519a01SNathan Fontenot  * 2 as published by the Free Software Foundation.
11ab519a01SNathan Fontenot  */
12ab519a01SNathan Fontenot 
13999e2dadSNathan Fontenot #define pr_fmt(fmt)	"dlpar: " fmt
14999e2dadSNathan Fontenot 
15ab519a01SNathan Fontenot #include <linux/kernel.h>
16ab519a01SNathan Fontenot #include <linux/notifier.h>
17ab519a01SNathan Fontenot #include <linux/spinlock.h>
18ab519a01SNathan Fontenot #include <linux/cpu.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
201cf3d8b3SNathan Fontenot #include <linux/of.h>
2106bacefcSAndy Shevchenko 
2206bacefcSAndy Shevchenko #include "of_helpers.h"
231217d34bSAnton Blanchard #include "pseries.h"
24ab519a01SNathan Fontenot 
25ab519a01SNathan Fontenot #include <asm/prom.h>
26ab519a01SNathan Fontenot #include <asm/machdep.h>
277c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
28ab519a01SNathan Fontenot #include <asm/rtas.h>
29ab519a01SNathan Fontenot 
307c98bd72SDaniel Axtens static struct workqueue_struct *pseries_hp_wq;
319054619eSJohn Allen 
329054619eSJohn Allen struct pseries_hp_work {
339054619eSJohn Allen 	struct work_struct work;
349054619eSJohn Allen 	struct pseries_hp_errorlog *errlog;
359054619eSJohn Allen 	struct completion *hp_completion;
369054619eSJohn Allen 	int *rc;
379054619eSJohn Allen };
389054619eSJohn Allen 
39ab519a01SNathan Fontenot struct cc_workarea {
40d6f1e7abSBharata B Rao 	__be32	drc_index;
41d6f1e7abSBharata B Rao 	__be32	zero;
42d6f1e7abSBharata B Rao 	__be32	name_offset;
43d6f1e7abSBharata B Rao 	__be32	prop_length;
44d6f1e7abSBharata B Rao 	__be32	prop_offset;
45ab519a01SNathan Fontenot };
46ab519a01SNathan Fontenot 
4720648974SNathan Fontenot void dlpar_free_cc_property(struct property *prop)
48ab519a01SNathan Fontenot {
49ab519a01SNathan Fontenot 	kfree(prop->name);
50ab519a01SNathan Fontenot 	kfree(prop->value);
51ab519a01SNathan Fontenot 	kfree(prop);
52ab519a01SNathan Fontenot }
53ab519a01SNathan Fontenot 
54ab519a01SNathan Fontenot static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
55ab519a01SNathan Fontenot {
56ab519a01SNathan Fontenot 	struct property *prop;
57ab519a01SNathan Fontenot 	char *name;
58ab519a01SNathan Fontenot 	char *value;
59ab519a01SNathan Fontenot 
60ab519a01SNathan Fontenot 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
61ab519a01SNathan Fontenot 	if (!prop)
62ab519a01SNathan Fontenot 		return NULL;
63ab519a01SNathan Fontenot 
64d6f1e7abSBharata B Rao 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
65ab519a01SNathan Fontenot 	prop->name = kstrdup(name, GFP_KERNEL);
66ab519a01SNathan Fontenot 
67d6f1e7abSBharata B Rao 	prop->length = be32_to_cpu(ccwa->prop_length);
68d6f1e7abSBharata B Rao 	value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
69e72ed6b5SNishanth Aravamudan 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
70ab519a01SNathan Fontenot 	if (!prop->value) {
71ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
72ab519a01SNathan Fontenot 		return NULL;
73ab519a01SNathan Fontenot 	}
74ab519a01SNathan Fontenot 
75ab519a01SNathan Fontenot 	return prop;
76ab519a01SNathan Fontenot }
77ab519a01SNathan Fontenot 
78*06665989SRob Herring static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
79ab519a01SNathan Fontenot {
80ab519a01SNathan Fontenot 	struct device_node *dn;
81*06665989SRob Herring 	const char *name;
828d5ff320STyrel Datwyler 
83ab519a01SNathan Fontenot 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
84ab519a01SNathan Fontenot 	if (!dn)
85ab519a01SNathan Fontenot 		return NULL;
86ab519a01SNathan Fontenot 
87*06665989SRob Herring 	name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
88*06665989SRob Herring 	dn->full_name = kstrdup(name, GFP_KERNEL);
89ab519a01SNathan Fontenot 	if (!dn->full_name) {
90ab519a01SNathan Fontenot 		kfree(dn);
91ab519a01SNathan Fontenot 		return NULL;
92ab519a01SNathan Fontenot 	}
93ab519a01SNathan Fontenot 
941578cb76STyrel Datwyler 	of_node_set_flag(dn, OF_DYNAMIC);
9597a9a717STyrel Datwyler 	of_node_init(dn);
961578cb76STyrel Datwyler 
97ab519a01SNathan Fontenot 	return dn;
98ab519a01SNathan Fontenot }
99ab519a01SNathan Fontenot 
100ab519a01SNathan Fontenot static void dlpar_free_one_cc_node(struct device_node *dn)
101ab519a01SNathan Fontenot {
102ab519a01SNathan Fontenot 	struct property *prop;
103ab519a01SNathan Fontenot 
104ab519a01SNathan Fontenot 	while (dn->properties) {
105ab519a01SNathan Fontenot 		prop = dn->properties;
106ab519a01SNathan Fontenot 		dn->properties = prop->next;
107ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
108ab519a01SNathan Fontenot 	}
109ab519a01SNathan Fontenot 
110ab519a01SNathan Fontenot 	kfree(dn->full_name);
111ab519a01SNathan Fontenot 	kfree(dn);
112ab519a01SNathan Fontenot }
113ab519a01SNathan Fontenot 
11420648974SNathan Fontenot void dlpar_free_cc_nodes(struct device_node *dn)
115ab519a01SNathan Fontenot {
116ab519a01SNathan Fontenot 	if (dn->child)
117ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->child);
118ab519a01SNathan Fontenot 
119ab519a01SNathan Fontenot 	if (dn->sibling)
120ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->sibling);
121ab519a01SNathan Fontenot 
122ab519a01SNathan Fontenot 	dlpar_free_one_cc_node(dn);
123ab519a01SNathan Fontenot }
124ab519a01SNathan Fontenot 
1259c740025SAnton Blanchard #define COMPLETE	0
126ab519a01SNathan Fontenot #define NEXT_SIBLING    1
127ab519a01SNathan Fontenot #define NEXT_CHILD      2
128ab519a01SNathan Fontenot #define NEXT_PROPERTY   3
129ab519a01SNathan Fontenot #define PREV_PARENT     4
130ab519a01SNathan Fontenot #define MORE_MEMORY     5
131ab519a01SNathan Fontenot #define CALL_AGAIN	-2
132ab519a01SNathan Fontenot #define ERR_CFG_USE     -9003
133ab519a01SNathan Fontenot 
134d6f1e7abSBharata B Rao struct device_node *dlpar_configure_connector(__be32 drc_index,
1358d5ff320STyrel Datwyler 					      struct device_node *parent)
136ab519a01SNathan Fontenot {
137ab519a01SNathan Fontenot 	struct device_node *dn;
138ab519a01SNathan Fontenot 	struct device_node *first_dn = NULL;
139ab519a01SNathan Fontenot 	struct device_node *last_dn = NULL;
140ab519a01SNathan Fontenot 	struct property *property;
141ab519a01SNathan Fontenot 	struct property *last_property = NULL;
142ab519a01SNathan Fontenot 	struct cc_workarea *ccwa;
14393f68f1eSNathan Fontenot 	char *data_buf;
144ab519a01SNathan Fontenot 	int cc_token;
14593f68f1eSNathan Fontenot 	int rc = -1;
146ab519a01SNathan Fontenot 
147ab519a01SNathan Fontenot 	cc_token = rtas_token("ibm,configure-connector");
148ab519a01SNathan Fontenot 	if (cc_token == RTAS_UNKNOWN_SERVICE)
149ab519a01SNathan Fontenot 		return NULL;
150ab519a01SNathan Fontenot 
15193f68f1eSNathan Fontenot 	data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
15293f68f1eSNathan Fontenot 	if (!data_buf)
15393f68f1eSNathan Fontenot 		return NULL;
15493f68f1eSNathan Fontenot 
15593f68f1eSNathan Fontenot 	ccwa = (struct cc_workarea *)&data_buf[0];
156ab519a01SNathan Fontenot 	ccwa->drc_index = drc_index;
157ab519a01SNathan Fontenot 	ccwa->zero = 0;
158ab519a01SNathan Fontenot 
15993f68f1eSNathan Fontenot 	do {
16093f68f1eSNathan Fontenot 		/* Since we release the rtas_data_buf lock between configure
16193f68f1eSNathan Fontenot 		 * connector calls we want to re-populate the rtas_data_buffer
16293f68f1eSNathan Fontenot 		 * with the contents of the previous call.
16393f68f1eSNathan Fontenot 		 */
16493f68f1eSNathan Fontenot 		spin_lock(&rtas_data_buf_lock);
16593f68f1eSNathan Fontenot 
16693f68f1eSNathan Fontenot 		memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
167ab519a01SNathan Fontenot 		rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
16893f68f1eSNathan Fontenot 		memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
16993f68f1eSNathan Fontenot 
17093f68f1eSNathan Fontenot 		spin_unlock(&rtas_data_buf_lock);
17193f68f1eSNathan Fontenot 
172ab519a01SNathan Fontenot 		switch (rc) {
1739c740025SAnton Blanchard 		case COMPLETE:
1749c740025SAnton Blanchard 			break;
1759c740025SAnton Blanchard 
176ab519a01SNathan Fontenot 		case NEXT_SIBLING:
177*06665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
178ab519a01SNathan Fontenot 			if (!dn)
179ab519a01SNathan Fontenot 				goto cc_error;
180ab519a01SNathan Fontenot 
181ab519a01SNathan Fontenot 			dn->parent = last_dn->parent;
182ab519a01SNathan Fontenot 			last_dn->sibling = dn;
183ab519a01SNathan Fontenot 			last_dn = dn;
184ab519a01SNathan Fontenot 			break;
185ab519a01SNathan Fontenot 
186ab519a01SNathan Fontenot 		case NEXT_CHILD:
187*06665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
188ab519a01SNathan Fontenot 			if (!dn)
189ab519a01SNathan Fontenot 				goto cc_error;
190ab519a01SNathan Fontenot 
1918d5ff320STyrel Datwyler 			if (!first_dn) {
1928d5ff320STyrel Datwyler 				dn->parent = parent;
193ab519a01SNathan Fontenot 				first_dn = dn;
1948d5ff320STyrel Datwyler 			} else {
195ab519a01SNathan Fontenot 				dn->parent = last_dn;
196ab519a01SNathan Fontenot 				if (last_dn)
197ab519a01SNathan Fontenot 					last_dn->child = dn;
198ab519a01SNathan Fontenot 			}
199ab519a01SNathan Fontenot 
200ab519a01SNathan Fontenot 			last_dn = dn;
201ab519a01SNathan Fontenot 			break;
202ab519a01SNathan Fontenot 
203ab519a01SNathan Fontenot 		case NEXT_PROPERTY:
204ab519a01SNathan Fontenot 			property = dlpar_parse_cc_property(ccwa);
205ab519a01SNathan Fontenot 			if (!property)
206ab519a01SNathan Fontenot 				goto cc_error;
207ab519a01SNathan Fontenot 
208ab519a01SNathan Fontenot 			if (!last_dn->properties)
209ab519a01SNathan Fontenot 				last_dn->properties = property;
210ab519a01SNathan Fontenot 			else
211ab519a01SNathan Fontenot 				last_property->next = property;
212ab519a01SNathan Fontenot 
213ab519a01SNathan Fontenot 			last_property = property;
214ab519a01SNathan Fontenot 			break;
215ab519a01SNathan Fontenot 
216ab519a01SNathan Fontenot 		case PREV_PARENT:
217ab519a01SNathan Fontenot 			last_dn = last_dn->parent;
218ab519a01SNathan Fontenot 			break;
219ab519a01SNathan Fontenot 
220ab519a01SNathan Fontenot 		case CALL_AGAIN:
221ab519a01SNathan Fontenot 			break;
222ab519a01SNathan Fontenot 
223ab519a01SNathan Fontenot 		case MORE_MEMORY:
224ab519a01SNathan Fontenot 		case ERR_CFG_USE:
225ab519a01SNathan Fontenot 		default:
226ab519a01SNathan Fontenot 			printk(KERN_ERR "Unexpected Error (%d) "
227ab519a01SNathan Fontenot 			       "returned from configure-connector\n", rc);
228ab519a01SNathan Fontenot 			goto cc_error;
229ab519a01SNathan Fontenot 		}
23093f68f1eSNathan Fontenot 	} while (rc);
231ab519a01SNathan Fontenot 
232ab519a01SNathan Fontenot cc_error:
23393f68f1eSNathan Fontenot 	kfree(data_buf);
23493f68f1eSNathan Fontenot 
23593f68f1eSNathan Fontenot 	if (rc) {
236ab519a01SNathan Fontenot 		if (first_dn)
237ab519a01SNathan Fontenot 			dlpar_free_cc_nodes(first_dn);
23893f68f1eSNathan Fontenot 
239ab519a01SNathan Fontenot 		return NULL;
240ab519a01SNathan Fontenot 	}
241ab519a01SNathan Fontenot 
24293f68f1eSNathan Fontenot 	return first_dn;
24393f68f1eSNathan Fontenot }
24493f68f1eSNathan Fontenot 
245215ee763SRob Herring int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
246ab519a01SNathan Fontenot {
247ab519a01SNathan Fontenot 	int rc;
248ab519a01SNathan Fontenot 
249215ee763SRob Herring 	dn->parent = parent;
250ab519a01SNathan Fontenot 
2511cf3d8b3SNathan Fontenot 	rc = of_attach_node(dn);
2523aef19f0SAkinobu Mita 	if (rc) {
253b7c670d6SRob Herring 		printk(KERN_ERR "Failed to add device node %pOF\n", dn);
2543aef19f0SAkinobu Mita 		return rc;
255ab519a01SNathan Fontenot 	}
256ab519a01SNathan Fontenot 
257ab519a01SNathan Fontenot 	of_node_put(dn->parent);
258ab519a01SNathan Fontenot 	return 0;
259ab519a01SNathan Fontenot }
260ab519a01SNathan Fontenot 
261ab519a01SNathan Fontenot int dlpar_detach_node(struct device_node *dn)
262ab519a01SNathan Fontenot {
2635935ff43STyrel Datwyler 	struct device_node *child;
2641cf3d8b3SNathan Fontenot 	int rc;
265ab519a01SNathan Fontenot 
2665935ff43STyrel Datwyler 	child = of_get_next_child(dn, NULL);
2675935ff43STyrel Datwyler 	while (child) {
2685935ff43STyrel Datwyler 		dlpar_detach_node(child);
2695935ff43STyrel Datwyler 		child = of_get_next_child(dn, child);
2705935ff43STyrel Datwyler 	}
2715935ff43STyrel Datwyler 
2721cf3d8b3SNathan Fontenot 	rc = of_detach_node(dn);
2731cf3d8b3SNathan Fontenot 	if (rc)
2741cf3d8b3SNathan Fontenot 		return rc;
2751cf3d8b3SNathan Fontenot 
276ab519a01SNathan Fontenot 	return 0;
277ab519a01SNathan Fontenot }
278ab519a01SNathan Fontenot 
279ab519a01SNathan Fontenot #define DR_ENTITY_SENSE		9003
280ab519a01SNathan Fontenot #define DR_ENTITY_PRESENT	1
281ab519a01SNathan Fontenot #define DR_ENTITY_UNUSABLE	2
282ab519a01SNathan Fontenot #define ALLOCATION_STATE	9003
283ab519a01SNathan Fontenot #define ALLOC_UNUSABLE		0
284ab519a01SNathan Fontenot #define ALLOC_USABLE		1
285ab519a01SNathan Fontenot #define ISOLATION_STATE		9001
286ab519a01SNathan Fontenot #define ISOLATE			0
287ab519a01SNathan Fontenot #define UNISOLATE		1
288ab519a01SNathan Fontenot 
289ab519a01SNathan Fontenot int dlpar_acquire_drc(u32 drc_index)
290ab519a01SNathan Fontenot {
291ab519a01SNathan Fontenot 	int dr_status, rc;
292ab519a01SNathan Fontenot 
293ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
294ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
295ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
296ab519a01SNathan Fontenot 		return -1;
297ab519a01SNathan Fontenot 
298ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
299ab519a01SNathan Fontenot 	if (rc)
300ab519a01SNathan Fontenot 		return rc;
301ab519a01SNathan Fontenot 
302ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
303ab519a01SNathan Fontenot 	if (rc) {
304ab519a01SNathan Fontenot 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
305ab519a01SNathan Fontenot 		return rc;
306ab519a01SNathan Fontenot 	}
307ab519a01SNathan Fontenot 
308ab519a01SNathan Fontenot 	return 0;
309ab519a01SNathan Fontenot }
310ab519a01SNathan Fontenot 
311ab519a01SNathan Fontenot int dlpar_release_drc(u32 drc_index)
312ab519a01SNathan Fontenot {
313ab519a01SNathan Fontenot 	int dr_status, rc;
314ab519a01SNathan Fontenot 
315ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
316ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
317ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_PRESENT)
318ab519a01SNathan Fontenot 		return -1;
319ab519a01SNathan Fontenot 
320ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
321ab519a01SNathan Fontenot 	if (rc)
322ab519a01SNathan Fontenot 		return rc;
323ab519a01SNathan Fontenot 
324ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
325ab519a01SNathan Fontenot 	if (rc) {
326ab519a01SNathan Fontenot 		rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
327ab519a01SNathan Fontenot 		return rc;
328ab519a01SNathan Fontenot 	}
329ab519a01SNathan Fontenot 
330ab519a01SNathan Fontenot 	return 0;
331ab519a01SNathan Fontenot }
332ab519a01SNathan Fontenot 
333999e2dadSNathan Fontenot static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
334999e2dadSNathan Fontenot {
335999e2dadSNathan Fontenot 	int rc;
336999e2dadSNathan Fontenot 
337999e2dadSNathan Fontenot 	/* pseries error logs are in BE format, convert to cpu type */
338999e2dadSNathan Fontenot 	switch (hp_elog->id_type) {
339999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_COUNT:
340999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_count =
341999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_count);
342999e2dadSNathan Fontenot 		break;
343999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_INDEX:
344999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_index =
345999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_index);
346333f7b76SSahil Mehta 		break;
347333f7b76SSahil Mehta 	case PSERIES_HP_ELOG_ID_DRC_IC:
348333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count =
349333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.count);
350333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index =
351333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.index);
352999e2dadSNathan Fontenot 	}
353999e2dadSNathan Fontenot 
354999e2dadSNathan Fontenot 	switch (hp_elog->resource) {
355999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_MEM:
356999e2dadSNathan Fontenot 		rc = dlpar_memory(hp_elog);
357999e2dadSNathan Fontenot 		break;
358e9d764f8SNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_CPU:
359e9d764f8SNathan Fontenot 		rc = dlpar_cpu(hp_elog);
360e9d764f8SNathan Fontenot 		break;
361999e2dadSNathan Fontenot 	default:
362999e2dadSNathan Fontenot 		pr_warn_ratelimited("Invalid resource (%d) specified\n",
363999e2dadSNathan Fontenot 				    hp_elog->resource);
364999e2dadSNathan Fontenot 		rc = -EINVAL;
365999e2dadSNathan Fontenot 	}
366999e2dadSNathan Fontenot 
367999e2dadSNathan Fontenot 	return rc;
368999e2dadSNathan Fontenot }
369999e2dadSNathan Fontenot 
3707c98bd72SDaniel Axtens static void pseries_hp_work_fn(struct work_struct *work)
3719054619eSJohn Allen {
3729054619eSJohn Allen 	struct pseries_hp_work *hp_work =
3739054619eSJohn Allen 			container_of(work, struct pseries_hp_work, work);
3749054619eSJohn Allen 
3759054619eSJohn Allen 	if (hp_work->rc)
3769054619eSJohn Allen 		*(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
3779054619eSJohn Allen 	else
3789054619eSJohn Allen 		handle_dlpar_errorlog(hp_work->errlog);
3799054619eSJohn Allen 
3809054619eSJohn Allen 	if (hp_work->hp_completion)
3819054619eSJohn Allen 		complete(hp_work->hp_completion);
3829054619eSJohn Allen 
3839054619eSJohn Allen 	kfree(hp_work->errlog);
3849054619eSJohn Allen 	kfree((void *)work);
3859054619eSJohn Allen }
3869054619eSJohn Allen 
3879054619eSJohn Allen void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
3889054619eSJohn Allen 			 struct completion *hotplug_done, int *rc)
3899054619eSJohn Allen {
3909054619eSJohn Allen 	struct pseries_hp_work *work;
3919054619eSJohn Allen 	struct pseries_hp_errorlog *hp_errlog_copy;
3929054619eSJohn Allen 
3939054619eSJohn Allen 	hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
3949054619eSJohn Allen 				 GFP_KERNEL);
3959054619eSJohn Allen 	memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
3969054619eSJohn Allen 
3979054619eSJohn Allen 	work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
3989054619eSJohn Allen 	if (work) {
3999054619eSJohn Allen 		INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
4009054619eSJohn Allen 		work->errlog = hp_errlog_copy;
4019054619eSJohn Allen 		work->hp_completion = hotplug_done;
4029054619eSJohn Allen 		work->rc = rc;
4039054619eSJohn Allen 		queue_work(pseries_hp_wq, (struct work_struct *)work);
4049054619eSJohn Allen 	} else {
4059054619eSJohn Allen 		*rc = -ENOMEM;
40690ce3514SAndrew Donnellan 		kfree(hp_errlog_copy);
4079054619eSJohn Allen 		complete(hotplug_done);
4089054619eSJohn Allen 	}
4099054619eSJohn Allen }
4109054619eSJohn Allen 
41125b587fbSNathan Fontenot static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
41225b587fbSNathan Fontenot {
41325b587fbSNathan Fontenot 	char *arg;
41425b587fbSNathan Fontenot 
41525b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
41625b587fbSNathan Fontenot 	if (!arg)
41725b587fbSNathan Fontenot 		return -EINVAL;
41825b587fbSNathan Fontenot 
41925b587fbSNathan Fontenot 	if (sysfs_streq(arg, "memory")) {
42025b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
42125b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "cpu")) {
42225b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
42325b587fbSNathan Fontenot 	} else {
42425b587fbSNathan Fontenot 		pr_err("Invalid resource specified.\n");
42525b587fbSNathan Fontenot 		return -EINVAL;
42625b587fbSNathan Fontenot 	}
42725b587fbSNathan Fontenot 
42825b587fbSNathan Fontenot 	return 0;
42925b587fbSNathan Fontenot }
43025b587fbSNathan Fontenot 
43125b587fbSNathan Fontenot static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
43225b587fbSNathan Fontenot {
43325b587fbSNathan Fontenot 	char *arg;
43425b587fbSNathan Fontenot 
43525b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
43625b587fbSNathan Fontenot 	if (!arg)
43725b587fbSNathan Fontenot 		return -EINVAL;
43825b587fbSNathan Fontenot 
43925b587fbSNathan Fontenot 	if (sysfs_streq(arg, "add")) {
44025b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
44125b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "remove")) {
44225b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
44325b587fbSNathan Fontenot 	} else {
44425b587fbSNathan Fontenot 		pr_err("Invalid action specified.\n");
44525b587fbSNathan Fontenot 		return -EINVAL;
44625b587fbSNathan Fontenot 	}
44725b587fbSNathan Fontenot 
44825b587fbSNathan Fontenot 	return 0;
44925b587fbSNathan Fontenot }
45025b587fbSNathan Fontenot 
45125b587fbSNathan Fontenot static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
45225b587fbSNathan Fontenot {
45325b587fbSNathan Fontenot 	char *arg;
45425b587fbSNathan Fontenot 	u32 count, index;
45525b587fbSNathan Fontenot 
45625b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
45725b587fbSNathan Fontenot 	if (!arg)
45825b587fbSNathan Fontenot 		return -EINVAL;
45925b587fbSNathan Fontenot 
460333f7b76SSahil Mehta 	if (sysfs_streq(arg, "indexed-count")) {
461333f7b76SSahil Mehta 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
462333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
463333f7b76SSahil Mehta 		if (!arg) {
464333f7b76SSahil Mehta 			pr_err("No DRC count specified.\n");
465333f7b76SSahil Mehta 			return -EINVAL;
466333f7b76SSahil Mehta 		}
467333f7b76SSahil Mehta 
468333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &count)) {
469333f7b76SSahil Mehta 			pr_err("Invalid DRC count specified.\n");
470333f7b76SSahil Mehta 			return -EINVAL;
471333f7b76SSahil Mehta 		}
472333f7b76SSahil Mehta 
473333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
474333f7b76SSahil Mehta 		if (!arg) {
475333f7b76SSahil Mehta 			pr_err("No DRC Index specified.\n");
476333f7b76SSahil Mehta 			return -EINVAL;
477333f7b76SSahil Mehta 		}
478333f7b76SSahil Mehta 
479333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &index)) {
480333f7b76SSahil Mehta 			pr_err("Invalid DRC Index specified.\n");
481333f7b76SSahil Mehta 			return -EINVAL;
482333f7b76SSahil Mehta 		}
483333f7b76SSahil Mehta 
484333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count = cpu_to_be32(count);
485333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index = cpu_to_be32(index);
486333f7b76SSahil Mehta 	} else if (sysfs_streq(arg, "index")) {
48725b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
48825b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
48925b587fbSNathan Fontenot 		if (!arg) {
49025b587fbSNathan Fontenot 			pr_err("No DRC Index specified.\n");
49125b587fbSNathan Fontenot 			return -EINVAL;
49225b587fbSNathan Fontenot 		}
49325b587fbSNathan Fontenot 
49425b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &index)) {
49525b587fbSNathan Fontenot 			pr_err("Invalid DRC Index specified.\n");
49625b587fbSNathan Fontenot 			return -EINVAL;
49725b587fbSNathan Fontenot 		}
49825b587fbSNathan Fontenot 
49925b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_index = cpu_to_be32(index);
50025b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "count")) {
50125b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
50225b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
50325b587fbSNathan Fontenot 		if (!arg) {
50425b587fbSNathan Fontenot 			pr_err("No DRC count specified.\n");
50525b587fbSNathan Fontenot 			return -EINVAL;
50625b587fbSNathan Fontenot 		}
50725b587fbSNathan Fontenot 
50825b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &count)) {
50925b587fbSNathan Fontenot 			pr_err("Invalid DRC count specified.\n");
51025b587fbSNathan Fontenot 			return -EINVAL;
51125b587fbSNathan Fontenot 		}
51225b587fbSNathan Fontenot 
51325b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_count = cpu_to_be32(count);
51425b587fbSNathan Fontenot 	} else {
51525b587fbSNathan Fontenot 		pr_err("Invalid id_type specified.\n");
51625b587fbSNathan Fontenot 		return -EINVAL;
51725b587fbSNathan Fontenot 	}
51825b587fbSNathan Fontenot 
51925b587fbSNathan Fontenot 	return 0;
52025b587fbSNathan Fontenot }
52125b587fbSNathan Fontenot 
522999e2dadSNathan Fontenot static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
523999e2dadSNathan Fontenot 			   const char *buf, size_t count)
524999e2dadSNathan Fontenot {
525999e2dadSNathan Fontenot 	struct pseries_hp_errorlog *hp_elog;
5269054619eSJohn Allen 	struct completion hotplug_done;
52725b587fbSNathan Fontenot 	char *argbuf;
52825b587fbSNathan Fontenot 	char *args;
529999e2dadSNathan Fontenot 	int rc;
530999e2dadSNathan Fontenot 
53125b587fbSNathan Fontenot 	args = argbuf = kstrdup(buf, GFP_KERNEL);
532999e2dadSNathan Fontenot 	hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
53325b587fbSNathan Fontenot 	if (!hp_elog || !argbuf) {
53425b587fbSNathan Fontenot 		pr_info("Could not allocate resources for DLPAR operation\n");
53525b587fbSNathan Fontenot 		kfree(argbuf);
53625b587fbSNathan Fontenot 		kfree(hp_elog);
53725b587fbSNathan Fontenot 		return -ENOMEM;
538999e2dadSNathan Fontenot 	}
539999e2dadSNathan Fontenot 
54025b587fbSNathan Fontenot 	/*
54125b587fbSNathan Fontenot 	 * Parse out the request from the user, this will be in the form:
542999e2dadSNathan Fontenot 	 * <resource> <action> <id_type> <id>
543999e2dadSNathan Fontenot 	 */
54425b587fbSNathan Fontenot 	rc = dlpar_parse_resource(&args, hp_elog);
54525b587fbSNathan Fontenot 	if (rc)
546999e2dadSNathan Fontenot 		goto dlpar_store_out;
547999e2dadSNathan Fontenot 
54825b587fbSNathan Fontenot 	rc = dlpar_parse_action(&args, hp_elog);
54925b587fbSNathan Fontenot 	if (rc)
550999e2dadSNathan Fontenot 		goto dlpar_store_out;
551999e2dadSNathan Fontenot 
55225b587fbSNathan Fontenot 	rc = dlpar_parse_id_type(&args, hp_elog);
55325b587fbSNathan Fontenot 	if (rc)
554999e2dadSNathan Fontenot 		goto dlpar_store_out;
555999e2dadSNathan Fontenot 
5561dc75956SJohn Allen 	init_completion(&hotplug_done);
5571dc75956SJohn Allen 	queue_hotplug_event(hp_elog, &hotplug_done, &rc);
5581dc75956SJohn Allen 	wait_for_completion(&hotplug_done);
559999e2dadSNathan Fontenot 
560999e2dadSNathan Fontenot dlpar_store_out:
56125b587fbSNathan Fontenot 	kfree(argbuf);
562999e2dadSNathan Fontenot 	kfree(hp_elog);
56325b587fbSNathan Fontenot 
56425b587fbSNathan Fontenot 	if (rc)
56525b587fbSNathan Fontenot 		pr_err("Could not handle DLPAR request \"%s\"\n", buf);
56625b587fbSNathan Fontenot 
567999e2dadSNathan Fontenot 	return rc ? rc : count;
568999e2dadSNathan Fontenot }
569999e2dadSNathan Fontenot 
570673bc435SNathan Fontenot static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
571673bc435SNathan Fontenot 			  char *buf)
572673bc435SNathan Fontenot {
573673bc435SNathan Fontenot 	return sprintf(buf, "%s\n", "memory,cpu");
574673bc435SNathan Fontenot }
575673bc435SNathan Fontenot 
5766f428096SGreg Kroah-Hartman static CLASS_ATTR_RW(dlpar);
577999e2dadSNathan Fontenot 
5781a8061c4SNathan Fontenot static int __init pseries_dlpar_init(void)
5791a8061c4SNathan Fontenot {
5809054619eSJohn Allen 	pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
5819054619eSJohn Allen 					WQ_UNBOUND, 1);
582183deeeaSNathan Fontenot 	return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
5831a8061c4SNathan Fontenot }
5841a8061c4SNathan Fontenot machine_device_initcall(pseries, pseries_dlpar_init);
5851a8061c4SNathan Fontenot 
586