xref: /openbmc/linux/arch/powerpc/platforms/pseries/dlpar.c (revision 634a0b8fb8826aa990df621a00158a052374b538)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ab519a01SNathan Fontenot /*
3ab519a01SNathan Fontenot  * Support for dynamic reconfiguration for PCI, Memory, and CPU
4ab519a01SNathan Fontenot  * Hotplug and Dynamic Logical Partitioning on RPA platforms.
5ab519a01SNathan Fontenot  *
6ab519a01SNathan Fontenot  * Copyright (C) 2009 Nathan Fontenot
7ab519a01SNathan Fontenot  * Copyright (C) 2009 IBM Corporation
8ab519a01SNathan Fontenot  */
9ab519a01SNathan Fontenot 
10999e2dadSNathan Fontenot #define pr_fmt(fmt)	"dlpar: " fmt
11999e2dadSNathan Fontenot 
12ab519a01SNathan Fontenot #include <linux/kernel.h>
13ab519a01SNathan Fontenot #include <linux/notifier.h>
14ab519a01SNathan Fontenot #include <linux/spinlock.h>
15ab519a01SNathan Fontenot #include <linux/cpu.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171cf3d8b3SNathan Fontenot #include <linux/of.h>
1806bacefcSAndy Shevchenko 
1906bacefcSAndy Shevchenko #include "of_helpers.h"
201217d34bSAnton Blanchard #include "pseries.h"
21ab519a01SNathan Fontenot 
22ab519a01SNathan Fontenot #include <asm/prom.h>
23ab519a01SNathan Fontenot #include <asm/machdep.h>
247c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
25ab519a01SNathan Fontenot #include <asm/rtas.h>
26ab519a01SNathan Fontenot 
277c98bd72SDaniel Axtens static struct workqueue_struct *pseries_hp_wq;
289054619eSJohn Allen 
299054619eSJohn Allen struct pseries_hp_work {
309054619eSJohn Allen 	struct work_struct work;
319054619eSJohn Allen 	struct pseries_hp_errorlog *errlog;
329054619eSJohn Allen };
339054619eSJohn Allen 
34ab519a01SNathan Fontenot struct cc_workarea {
35d6f1e7abSBharata B Rao 	__be32	drc_index;
36d6f1e7abSBharata B Rao 	__be32	zero;
37d6f1e7abSBharata B Rao 	__be32	name_offset;
38d6f1e7abSBharata B Rao 	__be32	prop_length;
39d6f1e7abSBharata B Rao 	__be32	prop_offset;
40ab519a01SNathan Fontenot };
41ab519a01SNathan Fontenot 
4220648974SNathan Fontenot void dlpar_free_cc_property(struct property *prop)
43ab519a01SNathan Fontenot {
44ab519a01SNathan Fontenot 	kfree(prop->name);
45ab519a01SNathan Fontenot 	kfree(prop->value);
46ab519a01SNathan Fontenot 	kfree(prop);
47ab519a01SNathan Fontenot }
48ab519a01SNathan Fontenot 
49ab519a01SNathan Fontenot static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
50ab519a01SNathan Fontenot {
51ab519a01SNathan Fontenot 	struct property *prop;
52ab519a01SNathan Fontenot 	char *name;
53ab519a01SNathan Fontenot 	char *value;
54ab519a01SNathan Fontenot 
55ab519a01SNathan Fontenot 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
56ab519a01SNathan Fontenot 	if (!prop)
57ab519a01SNathan Fontenot 		return NULL;
58ab519a01SNathan Fontenot 
59d6f1e7abSBharata B Rao 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
60ab519a01SNathan Fontenot 	prop->name = kstrdup(name, GFP_KERNEL);
61efa9ace6SGen Zhang 	if (!prop->name) {
62efa9ace6SGen Zhang 		dlpar_free_cc_property(prop);
63efa9ace6SGen Zhang 		return NULL;
64efa9ace6SGen Zhang 	}
65ab519a01SNathan Fontenot 
66d6f1e7abSBharata B Rao 	prop->length = be32_to_cpu(ccwa->prop_length);
67d6f1e7abSBharata B Rao 	value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
68e72ed6b5SNishanth Aravamudan 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
69ab519a01SNathan Fontenot 	if (!prop->value) {
70ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
71ab519a01SNathan Fontenot 		return NULL;
72ab519a01SNathan Fontenot 	}
73ab519a01SNathan Fontenot 
74ab519a01SNathan Fontenot 	return prop;
75ab519a01SNathan Fontenot }
76ab519a01SNathan Fontenot 
7706665989SRob Herring static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
78ab519a01SNathan Fontenot {
79ab519a01SNathan Fontenot 	struct device_node *dn;
8006665989SRob Herring 	const char *name;
818d5ff320STyrel Datwyler 
82ab519a01SNathan Fontenot 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
83ab519a01SNathan Fontenot 	if (!dn)
84ab519a01SNathan Fontenot 		return NULL;
85ab519a01SNathan Fontenot 
8606665989SRob Herring 	name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
8706665989SRob Herring 	dn->full_name = kstrdup(name, GFP_KERNEL);
88ab519a01SNathan Fontenot 	if (!dn->full_name) {
89ab519a01SNathan Fontenot 		kfree(dn);
90ab519a01SNathan Fontenot 		return NULL;
91ab519a01SNathan Fontenot 	}
92ab519a01SNathan Fontenot 
931578cb76STyrel Datwyler 	of_node_set_flag(dn, OF_DYNAMIC);
9497a9a717STyrel Datwyler 	of_node_init(dn);
951578cb76STyrel Datwyler 
96ab519a01SNathan Fontenot 	return dn;
97ab519a01SNathan Fontenot }
98ab519a01SNathan Fontenot 
99ab519a01SNathan Fontenot static void dlpar_free_one_cc_node(struct device_node *dn)
100ab519a01SNathan Fontenot {
101ab519a01SNathan Fontenot 	struct property *prop;
102ab519a01SNathan Fontenot 
103ab519a01SNathan Fontenot 	while (dn->properties) {
104ab519a01SNathan Fontenot 		prop = dn->properties;
105ab519a01SNathan Fontenot 		dn->properties = prop->next;
106ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
107ab519a01SNathan Fontenot 	}
108ab519a01SNathan Fontenot 
109ab519a01SNathan Fontenot 	kfree(dn->full_name);
110ab519a01SNathan Fontenot 	kfree(dn);
111ab519a01SNathan Fontenot }
112ab519a01SNathan Fontenot 
11320648974SNathan Fontenot void dlpar_free_cc_nodes(struct device_node *dn)
114ab519a01SNathan Fontenot {
115ab519a01SNathan Fontenot 	if (dn->child)
116ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->child);
117ab519a01SNathan Fontenot 
118ab519a01SNathan Fontenot 	if (dn->sibling)
119ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->sibling);
120ab519a01SNathan Fontenot 
121ab519a01SNathan Fontenot 	dlpar_free_one_cc_node(dn);
122ab519a01SNathan Fontenot }
123ab519a01SNathan Fontenot 
1249c740025SAnton Blanchard #define COMPLETE	0
125ab519a01SNathan Fontenot #define NEXT_SIBLING    1
126ab519a01SNathan Fontenot #define NEXT_CHILD      2
127ab519a01SNathan Fontenot #define NEXT_PROPERTY   3
128ab519a01SNathan Fontenot #define PREV_PARENT     4
129ab519a01SNathan Fontenot #define MORE_MEMORY     5
130ab519a01SNathan Fontenot #define ERR_CFG_USE     -9003
131ab519a01SNathan Fontenot 
132d6f1e7abSBharata B Rao struct device_node *dlpar_configure_connector(__be32 drc_index,
1338d5ff320STyrel Datwyler 					      struct device_node *parent)
134ab519a01SNathan Fontenot {
135ab519a01SNathan Fontenot 	struct device_node *dn;
136ab519a01SNathan Fontenot 	struct device_node *first_dn = NULL;
137ab519a01SNathan Fontenot 	struct device_node *last_dn = NULL;
138ab519a01SNathan Fontenot 	struct property *property;
139ab519a01SNathan Fontenot 	struct property *last_property = NULL;
140ab519a01SNathan Fontenot 	struct cc_workarea *ccwa;
14193f68f1eSNathan Fontenot 	char *data_buf;
142ab519a01SNathan Fontenot 	int cc_token;
14393f68f1eSNathan Fontenot 	int rc = -1;
144ab519a01SNathan Fontenot 
145ab519a01SNathan Fontenot 	cc_token = rtas_token("ibm,configure-connector");
146ab519a01SNathan Fontenot 	if (cc_token == RTAS_UNKNOWN_SERVICE)
147ab519a01SNathan Fontenot 		return NULL;
148ab519a01SNathan Fontenot 
14993f68f1eSNathan Fontenot 	data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
15093f68f1eSNathan Fontenot 	if (!data_buf)
15193f68f1eSNathan Fontenot 		return NULL;
15293f68f1eSNathan Fontenot 
15393f68f1eSNathan Fontenot 	ccwa = (struct cc_workarea *)&data_buf[0];
154ab519a01SNathan Fontenot 	ccwa->drc_index = drc_index;
155ab519a01SNathan Fontenot 	ccwa->zero = 0;
156ab519a01SNathan Fontenot 
15793f68f1eSNathan Fontenot 	do {
15893f68f1eSNathan Fontenot 		/* Since we release the rtas_data_buf lock between configure
15993f68f1eSNathan Fontenot 		 * connector calls we want to re-populate the rtas_data_buffer
16093f68f1eSNathan Fontenot 		 * with the contents of the previous call.
16193f68f1eSNathan Fontenot 		 */
16293f68f1eSNathan Fontenot 		spin_lock(&rtas_data_buf_lock);
16393f68f1eSNathan Fontenot 
16493f68f1eSNathan Fontenot 		memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
165ab519a01SNathan Fontenot 		rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
16693f68f1eSNathan Fontenot 		memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
16793f68f1eSNathan Fontenot 
16893f68f1eSNathan Fontenot 		spin_unlock(&rtas_data_buf_lock);
16993f68f1eSNathan Fontenot 
170768d70e1SNathan Lynch 		if (rtas_busy_delay(rc))
171768d70e1SNathan Lynch 			continue;
172768d70e1SNathan Lynch 
173ab519a01SNathan Fontenot 		switch (rc) {
1749c740025SAnton Blanchard 		case COMPLETE:
1759c740025SAnton Blanchard 			break;
1769c740025SAnton Blanchard 
177ab519a01SNathan Fontenot 		case NEXT_SIBLING:
17806665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
179ab519a01SNathan Fontenot 			if (!dn)
180ab519a01SNathan Fontenot 				goto cc_error;
181ab519a01SNathan Fontenot 
182ab519a01SNathan Fontenot 			dn->parent = last_dn->parent;
183ab519a01SNathan Fontenot 			last_dn->sibling = dn;
184ab519a01SNathan Fontenot 			last_dn = dn;
185ab519a01SNathan Fontenot 			break;
186ab519a01SNathan Fontenot 
187ab519a01SNathan Fontenot 		case NEXT_CHILD:
18806665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
189ab519a01SNathan Fontenot 			if (!dn)
190ab519a01SNathan Fontenot 				goto cc_error;
191ab519a01SNathan Fontenot 
1928d5ff320STyrel Datwyler 			if (!first_dn) {
1938d5ff320STyrel Datwyler 				dn->parent = parent;
194ab519a01SNathan Fontenot 				first_dn = dn;
1958d5ff320STyrel Datwyler 			} else {
196ab519a01SNathan Fontenot 				dn->parent = last_dn;
197ab519a01SNathan Fontenot 				if (last_dn)
198ab519a01SNathan Fontenot 					last_dn->child = dn;
199ab519a01SNathan Fontenot 			}
200ab519a01SNathan Fontenot 
201ab519a01SNathan Fontenot 			last_dn = dn;
202ab519a01SNathan Fontenot 			break;
203ab519a01SNathan Fontenot 
204ab519a01SNathan Fontenot 		case NEXT_PROPERTY:
205ab519a01SNathan Fontenot 			property = dlpar_parse_cc_property(ccwa);
206ab519a01SNathan Fontenot 			if (!property)
207ab519a01SNathan Fontenot 				goto cc_error;
208ab519a01SNathan Fontenot 
209ab519a01SNathan Fontenot 			if (!last_dn->properties)
210ab519a01SNathan Fontenot 				last_dn->properties = property;
211ab519a01SNathan Fontenot 			else
212ab519a01SNathan Fontenot 				last_property->next = property;
213ab519a01SNathan Fontenot 
214ab519a01SNathan Fontenot 			last_property = property;
215ab519a01SNathan Fontenot 			break;
216ab519a01SNathan Fontenot 
217ab519a01SNathan Fontenot 		case PREV_PARENT:
218ab519a01SNathan Fontenot 			last_dn = last_dn->parent;
219ab519a01SNathan Fontenot 			break;
220ab519a01SNathan Fontenot 
221ab519a01SNathan Fontenot 		case MORE_MEMORY:
222ab519a01SNathan Fontenot 		case ERR_CFG_USE:
223ab519a01SNathan Fontenot 		default:
224ab519a01SNathan Fontenot 			printk(KERN_ERR "Unexpected Error (%d) "
225ab519a01SNathan Fontenot 			       "returned from configure-connector\n", rc);
226ab519a01SNathan Fontenot 			goto cc_error;
227ab519a01SNathan Fontenot 		}
22893f68f1eSNathan Fontenot 	} while (rc);
229ab519a01SNathan Fontenot 
230ab519a01SNathan Fontenot cc_error:
23193f68f1eSNathan Fontenot 	kfree(data_buf);
23293f68f1eSNathan Fontenot 
23393f68f1eSNathan Fontenot 	if (rc) {
234ab519a01SNathan Fontenot 		if (first_dn)
235ab519a01SNathan Fontenot 			dlpar_free_cc_nodes(first_dn);
23693f68f1eSNathan Fontenot 
237ab519a01SNathan Fontenot 		return NULL;
238ab519a01SNathan Fontenot 	}
239ab519a01SNathan Fontenot 
24093f68f1eSNathan Fontenot 	return first_dn;
24193f68f1eSNathan Fontenot }
24293f68f1eSNathan Fontenot 
243215ee763SRob Herring int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
244ab519a01SNathan Fontenot {
245ab519a01SNathan Fontenot 	int rc;
246ab519a01SNathan Fontenot 
247215ee763SRob Herring 	dn->parent = parent;
248ab519a01SNathan Fontenot 
2491cf3d8b3SNathan Fontenot 	rc = of_attach_node(dn);
2503aef19f0SAkinobu Mita 	if (rc) {
251b7c670d6SRob Herring 		printk(KERN_ERR "Failed to add device node %pOF\n", dn);
2523aef19f0SAkinobu Mita 		return rc;
253ab519a01SNathan Fontenot 	}
254ab519a01SNathan Fontenot 
255ab519a01SNathan Fontenot 	return 0;
256ab519a01SNathan Fontenot }
257ab519a01SNathan Fontenot 
258ab519a01SNathan Fontenot int dlpar_detach_node(struct device_node *dn)
259ab519a01SNathan Fontenot {
2605935ff43STyrel Datwyler 	struct device_node *child;
2611cf3d8b3SNathan Fontenot 	int rc;
262ab519a01SNathan Fontenot 
2635935ff43STyrel Datwyler 	child = of_get_next_child(dn, NULL);
2645935ff43STyrel Datwyler 	while (child) {
2655935ff43STyrel Datwyler 		dlpar_detach_node(child);
2665935ff43STyrel Datwyler 		child = of_get_next_child(dn, child);
2675935ff43STyrel Datwyler 	}
2685935ff43STyrel Datwyler 
2691cf3d8b3SNathan Fontenot 	rc = of_detach_node(dn);
2701cf3d8b3SNathan Fontenot 	if (rc)
2711cf3d8b3SNathan Fontenot 		return rc;
2721cf3d8b3SNathan Fontenot 
2735b3f5c40SFrank Rowand 	of_node_put(dn);
2745b3f5c40SFrank Rowand 
275ab519a01SNathan Fontenot 	return 0;
276ab519a01SNathan Fontenot }
277ab519a01SNathan Fontenot 
278ab519a01SNathan Fontenot #define DR_ENTITY_SENSE		9003
279ab519a01SNathan Fontenot #define DR_ENTITY_PRESENT	1
280ab519a01SNathan Fontenot #define DR_ENTITY_UNUSABLE	2
281ab519a01SNathan Fontenot #define ALLOCATION_STATE	9003
282ab519a01SNathan Fontenot #define ALLOC_UNUSABLE		0
283ab519a01SNathan Fontenot #define ALLOC_USABLE		1
284ab519a01SNathan Fontenot #define ISOLATION_STATE		9001
285ab519a01SNathan Fontenot #define ISOLATE			0
286ab519a01SNathan Fontenot #define UNISOLATE		1
287ab519a01SNathan Fontenot 
288ab519a01SNathan Fontenot int dlpar_acquire_drc(u32 drc_index)
289ab519a01SNathan Fontenot {
290ab519a01SNathan Fontenot 	int dr_status, rc;
291ab519a01SNathan Fontenot 
292bfb0c9fcSNathan Lynch 	rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
293ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
294ab519a01SNathan Fontenot 		return -1;
295ab519a01SNathan Fontenot 
296ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
297ab519a01SNathan Fontenot 	if (rc)
298ab519a01SNathan Fontenot 		return rc;
299ab519a01SNathan Fontenot 
300ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
301ab519a01SNathan Fontenot 	if (rc) {
302ab519a01SNathan Fontenot 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
303ab519a01SNathan Fontenot 		return rc;
304ab519a01SNathan Fontenot 	}
305ab519a01SNathan Fontenot 
306ab519a01SNathan Fontenot 	return 0;
307ab519a01SNathan Fontenot }
308ab519a01SNathan Fontenot 
309ab519a01SNathan Fontenot int dlpar_release_drc(u32 drc_index)
310ab519a01SNathan Fontenot {
311ab519a01SNathan Fontenot 	int dr_status, rc;
312ab519a01SNathan Fontenot 
313bfb0c9fcSNathan Lynch 	rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
314ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_PRESENT)
315ab519a01SNathan Fontenot 		return -1;
316ab519a01SNathan Fontenot 
317ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
318ab519a01SNathan Fontenot 	if (rc)
319ab519a01SNathan Fontenot 		return rc;
320ab519a01SNathan Fontenot 
321ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
322ab519a01SNathan Fontenot 	if (rc) {
323ab519a01SNathan Fontenot 		rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
324ab519a01SNathan Fontenot 		return rc;
325ab519a01SNathan Fontenot 	}
326ab519a01SNathan Fontenot 
327ab519a01SNathan Fontenot 	return 0;
328ab519a01SNathan Fontenot }
329ab519a01SNathan Fontenot 
3300e3b3ff8SDaniel Henrique Barboza int dlpar_unisolate_drc(u32 drc_index)
3310e3b3ff8SDaniel Henrique Barboza {
3320e3b3ff8SDaniel Henrique Barboza 	int dr_status, rc;
3330e3b3ff8SDaniel Henrique Barboza 
334bfb0c9fcSNathan Lynch 	rc = rtas_get_sensor(DR_ENTITY_SENSE, drc_index, &dr_status);
3350e3b3ff8SDaniel Henrique Barboza 	if (rc || dr_status != DR_ENTITY_PRESENT)
3360e3b3ff8SDaniel Henrique Barboza 		return -1;
3370e3b3ff8SDaniel Henrique Barboza 
3380e3b3ff8SDaniel Henrique Barboza 	rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
3390e3b3ff8SDaniel Henrique Barboza 
3400e3b3ff8SDaniel Henrique Barboza 	return 0;
3410e3b3ff8SDaniel Henrique Barboza }
3420e3b3ff8SDaniel Henrique Barboza 
343fd12527aSNathan Fontenot int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
344999e2dadSNathan Fontenot {
345999e2dadSNathan Fontenot 	int rc;
346999e2dadSNathan Fontenot 
347999e2dadSNathan Fontenot 	/* pseries error logs are in BE format, convert to cpu type */
348999e2dadSNathan Fontenot 	switch (hp_elog->id_type) {
349999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_COUNT:
350999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_count =
351999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_count);
352999e2dadSNathan Fontenot 		break;
353999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_INDEX:
354999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_index =
355999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_index);
356333f7b76SSahil Mehta 		break;
357333f7b76SSahil Mehta 	case PSERIES_HP_ELOG_ID_DRC_IC:
358333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count =
359333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.count);
360333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index =
361333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.index);
362999e2dadSNathan Fontenot 	}
363999e2dadSNathan Fontenot 
364999e2dadSNathan Fontenot 	switch (hp_elog->resource) {
365999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_MEM:
366999e2dadSNathan Fontenot 		rc = dlpar_memory(hp_elog);
367999e2dadSNathan Fontenot 		break;
368e9d764f8SNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_CPU:
369e9d764f8SNathan Fontenot 		rc = dlpar_cpu(hp_elog);
370e9d764f8SNathan Fontenot 		break;
3714c5d87dbSOliver O'Halloran 	case PSERIES_HP_ELOG_RESOURCE_PMEM:
3724c5d87dbSOliver O'Halloran 		rc = dlpar_hp_pmem(hp_elog);
3734c5d87dbSOliver O'Halloran 		break;
3744c5d87dbSOliver O'Halloran 
375999e2dadSNathan Fontenot 	default:
376999e2dadSNathan Fontenot 		pr_warn_ratelimited("Invalid resource (%d) specified\n",
377999e2dadSNathan Fontenot 				    hp_elog->resource);
378999e2dadSNathan Fontenot 		rc = -EINVAL;
379999e2dadSNathan Fontenot 	}
380999e2dadSNathan Fontenot 
381999e2dadSNathan Fontenot 	return rc;
382999e2dadSNathan Fontenot }
383999e2dadSNathan Fontenot 
3847c98bd72SDaniel Axtens static void pseries_hp_work_fn(struct work_struct *work)
3859054619eSJohn Allen {
3869054619eSJohn Allen 	struct pseries_hp_work *hp_work =
3879054619eSJohn Allen 			container_of(work, struct pseries_hp_work, work);
3889054619eSJohn Allen 
3899054619eSJohn Allen 	handle_dlpar_errorlog(hp_work->errlog);
3909054619eSJohn Allen 
3919054619eSJohn Allen 	kfree(hp_work->errlog);
392*634a0b8fSXu Wang 	kfree(work);
3939054619eSJohn Allen }
3949054619eSJohn Allen 
395fd12527aSNathan Fontenot void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
3969054619eSJohn Allen {
3979054619eSJohn Allen 	struct pseries_hp_work *work;
3989054619eSJohn Allen 	struct pseries_hp_errorlog *hp_errlog_copy;
3999054619eSJohn Allen 
400348ea30fSNathan Lynch 	hp_errlog_copy = kmemdup(hp_errlog, sizeof(*hp_errlog), GFP_ATOMIC);
401348ea30fSNathan Lynch 	if (!hp_errlog_copy)
402348ea30fSNathan Lynch 		return;
4039054619eSJohn Allen 
404348ea30fSNathan Lynch 	work = kmalloc(sizeof(struct pseries_hp_work), GFP_ATOMIC);
4059054619eSJohn Allen 	if (work) {
4069054619eSJohn Allen 		INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
4079054619eSJohn Allen 		work->errlog = hp_errlog_copy;
4089054619eSJohn Allen 		queue_work(pseries_hp_wq, (struct work_struct *)work);
4099054619eSJohn Allen 	} else {
41090ce3514SAndrew Donnellan 		kfree(hp_errlog_copy);
4119054619eSJohn Allen 	}
4129054619eSJohn Allen }
4139054619eSJohn Allen 
41425b587fbSNathan Fontenot static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
41525b587fbSNathan Fontenot {
41625b587fbSNathan Fontenot 	char *arg;
41725b587fbSNathan Fontenot 
41825b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
41925b587fbSNathan Fontenot 	if (!arg)
42025b587fbSNathan Fontenot 		return -EINVAL;
42125b587fbSNathan Fontenot 
42225b587fbSNathan Fontenot 	if (sysfs_streq(arg, "memory")) {
42325b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
42425b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "cpu")) {
42525b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
42625b587fbSNathan Fontenot 	} else {
42725b587fbSNathan Fontenot 		pr_err("Invalid resource specified.\n");
42825b587fbSNathan Fontenot 		return -EINVAL;
42925b587fbSNathan Fontenot 	}
43025b587fbSNathan Fontenot 
43125b587fbSNathan Fontenot 	return 0;
43225b587fbSNathan Fontenot }
43325b587fbSNathan Fontenot 
43425b587fbSNathan Fontenot static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
43525b587fbSNathan Fontenot {
43625b587fbSNathan Fontenot 	char *arg;
43725b587fbSNathan Fontenot 
43825b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
43925b587fbSNathan Fontenot 	if (!arg)
44025b587fbSNathan Fontenot 		return -EINVAL;
44125b587fbSNathan Fontenot 
44225b587fbSNathan Fontenot 	if (sysfs_streq(arg, "add")) {
44325b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
44425b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "remove")) {
44525b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
44625b587fbSNathan Fontenot 	} else {
44725b587fbSNathan Fontenot 		pr_err("Invalid action specified.\n");
44825b587fbSNathan Fontenot 		return -EINVAL;
44925b587fbSNathan Fontenot 	}
45025b587fbSNathan Fontenot 
45125b587fbSNathan Fontenot 	return 0;
45225b587fbSNathan Fontenot }
45325b587fbSNathan Fontenot 
45425b587fbSNathan Fontenot static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
45525b587fbSNathan Fontenot {
45625b587fbSNathan Fontenot 	char *arg;
45725b587fbSNathan Fontenot 	u32 count, index;
45825b587fbSNathan Fontenot 
45925b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
46025b587fbSNathan Fontenot 	if (!arg)
46125b587fbSNathan Fontenot 		return -EINVAL;
46225b587fbSNathan Fontenot 
463333f7b76SSahil Mehta 	if (sysfs_streq(arg, "indexed-count")) {
464333f7b76SSahil Mehta 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
465333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
466333f7b76SSahil Mehta 		if (!arg) {
467333f7b76SSahil Mehta 			pr_err("No DRC count specified.\n");
468333f7b76SSahil Mehta 			return -EINVAL;
469333f7b76SSahil Mehta 		}
470333f7b76SSahil Mehta 
471333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &count)) {
472333f7b76SSahil Mehta 			pr_err("Invalid DRC count specified.\n");
473333f7b76SSahil Mehta 			return -EINVAL;
474333f7b76SSahil Mehta 		}
475333f7b76SSahil Mehta 
476333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
477333f7b76SSahil Mehta 		if (!arg) {
478333f7b76SSahil Mehta 			pr_err("No DRC Index specified.\n");
479333f7b76SSahil Mehta 			return -EINVAL;
480333f7b76SSahil Mehta 		}
481333f7b76SSahil Mehta 
482333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &index)) {
483333f7b76SSahil Mehta 			pr_err("Invalid DRC Index specified.\n");
484333f7b76SSahil Mehta 			return -EINVAL;
485333f7b76SSahil Mehta 		}
486333f7b76SSahil Mehta 
487333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count = cpu_to_be32(count);
488333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index = cpu_to_be32(index);
489333f7b76SSahil Mehta 	} else if (sysfs_streq(arg, "index")) {
49025b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
49125b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
49225b587fbSNathan Fontenot 		if (!arg) {
49325b587fbSNathan Fontenot 			pr_err("No DRC Index specified.\n");
49425b587fbSNathan Fontenot 			return -EINVAL;
49525b587fbSNathan Fontenot 		}
49625b587fbSNathan Fontenot 
49725b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &index)) {
49825b587fbSNathan Fontenot 			pr_err("Invalid DRC Index specified.\n");
49925b587fbSNathan Fontenot 			return -EINVAL;
50025b587fbSNathan Fontenot 		}
50125b587fbSNathan Fontenot 
50225b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_index = cpu_to_be32(index);
50325b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "count")) {
50425b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
50525b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
50625b587fbSNathan Fontenot 		if (!arg) {
50725b587fbSNathan Fontenot 			pr_err("No DRC count specified.\n");
50825b587fbSNathan Fontenot 			return -EINVAL;
50925b587fbSNathan Fontenot 		}
51025b587fbSNathan Fontenot 
51125b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &count)) {
51225b587fbSNathan Fontenot 			pr_err("Invalid DRC count specified.\n");
51325b587fbSNathan Fontenot 			return -EINVAL;
51425b587fbSNathan Fontenot 		}
51525b587fbSNathan Fontenot 
51625b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_count = cpu_to_be32(count);
51725b587fbSNathan Fontenot 	} else {
51825b587fbSNathan Fontenot 		pr_err("Invalid id_type specified.\n");
51925b587fbSNathan Fontenot 		return -EINVAL;
52025b587fbSNathan Fontenot 	}
52125b587fbSNathan Fontenot 
52225b587fbSNathan Fontenot 	return 0;
52325b587fbSNathan Fontenot }
52425b587fbSNathan Fontenot 
525999e2dadSNathan Fontenot static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
526999e2dadSNathan Fontenot 			   const char *buf, size_t count)
527999e2dadSNathan Fontenot {
528fd12527aSNathan Fontenot 	struct pseries_hp_errorlog hp_elog;
52925b587fbSNathan Fontenot 	char *argbuf;
53025b587fbSNathan Fontenot 	char *args;
531999e2dadSNathan Fontenot 	int rc;
532999e2dadSNathan Fontenot 
53325b587fbSNathan Fontenot 	args = argbuf = kstrdup(buf, GFP_KERNEL);
5346e7a4da7SMarkus Elfring 	if (!argbuf)
53525b587fbSNathan Fontenot 		return -ENOMEM;
536999e2dadSNathan Fontenot 
53725b587fbSNathan Fontenot 	/*
53825b587fbSNathan Fontenot 	 * Parse out the request from the user, this will be in the form:
539999e2dadSNathan Fontenot 	 * <resource> <action> <id_type> <id>
540999e2dadSNathan Fontenot 	 */
541fd12527aSNathan Fontenot 	rc = dlpar_parse_resource(&args, &hp_elog);
54225b587fbSNathan Fontenot 	if (rc)
543999e2dadSNathan Fontenot 		goto dlpar_store_out;
544999e2dadSNathan Fontenot 
545fd12527aSNathan Fontenot 	rc = dlpar_parse_action(&args, &hp_elog);
54625b587fbSNathan Fontenot 	if (rc)
547999e2dadSNathan Fontenot 		goto dlpar_store_out;
548999e2dadSNathan Fontenot 
549fd12527aSNathan Fontenot 	rc = dlpar_parse_id_type(&args, &hp_elog);
55025b587fbSNathan Fontenot 	if (rc)
551999e2dadSNathan Fontenot 		goto dlpar_store_out;
552999e2dadSNathan Fontenot 
553fd12527aSNathan Fontenot 	rc = handle_dlpar_errorlog(&hp_elog);
554999e2dadSNathan Fontenot 
555999e2dadSNathan Fontenot dlpar_store_out:
55625b587fbSNathan Fontenot 	kfree(argbuf);
55725b587fbSNathan Fontenot 
55825b587fbSNathan Fontenot 	if (rc)
55925b587fbSNathan Fontenot 		pr_err("Could not handle DLPAR request \"%s\"\n", buf);
56025b587fbSNathan Fontenot 
561999e2dadSNathan Fontenot 	return rc ? rc : count;
562999e2dadSNathan Fontenot }
563999e2dadSNathan Fontenot 
564673bc435SNathan Fontenot static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
565673bc435SNathan Fontenot 			  char *buf)
566673bc435SNathan Fontenot {
567673bc435SNathan Fontenot 	return sprintf(buf, "%s\n", "memory,cpu");
568673bc435SNathan Fontenot }
569673bc435SNathan Fontenot 
5706f428096SGreg Kroah-Hartman static CLASS_ATTR_RW(dlpar);
571999e2dadSNathan Fontenot 
572e2d59152SMichael Ellerman int __init dlpar_workqueue_init(void)
5731a8061c4SNathan Fontenot {
574e2d59152SMichael Ellerman 	if (pseries_hp_wq)
575e2d59152SMichael Ellerman 		return 0;
576e2d59152SMichael Ellerman 
5779054619eSJohn Allen 	pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
5789054619eSJohn Allen 			WQ_UNBOUND, 1);
579e2d59152SMichael Ellerman 
580e2d59152SMichael Ellerman 	return pseries_hp_wq ? 0 : -ENOMEM;
581e2d59152SMichael Ellerman }
582e2d59152SMichael Ellerman 
583e2d59152SMichael Ellerman static int __init dlpar_sysfs_init(void)
584e2d59152SMichael Ellerman {
585e2d59152SMichael Ellerman 	int rc;
586e2d59152SMichael Ellerman 
587e2d59152SMichael Ellerman 	rc = dlpar_workqueue_init();
588e2d59152SMichael Ellerman 	if (rc)
589e2d59152SMichael Ellerman 		return rc;
590e2d59152SMichael Ellerman 
591183deeeaSNathan Fontenot 	return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
5921a8061c4SNathan Fontenot }
593e2d59152SMichael Ellerman machine_device_initcall(pseries, dlpar_sysfs_init);
5941a8061c4SNathan Fontenot 
595