xref: /openbmc/linux/arch/powerpc/platforms/pseries/dlpar.c (revision fd12527a1da42dcb906b694e01794e8d438f7d10)
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 };
369054619eSJohn Allen 
37ab519a01SNathan Fontenot struct cc_workarea {
38d6f1e7abSBharata B Rao 	__be32	drc_index;
39d6f1e7abSBharata B Rao 	__be32	zero;
40d6f1e7abSBharata B Rao 	__be32	name_offset;
41d6f1e7abSBharata B Rao 	__be32	prop_length;
42d6f1e7abSBharata B Rao 	__be32	prop_offset;
43ab519a01SNathan Fontenot };
44ab519a01SNathan Fontenot 
4520648974SNathan Fontenot void dlpar_free_cc_property(struct property *prop)
46ab519a01SNathan Fontenot {
47ab519a01SNathan Fontenot 	kfree(prop->name);
48ab519a01SNathan Fontenot 	kfree(prop->value);
49ab519a01SNathan Fontenot 	kfree(prop);
50ab519a01SNathan Fontenot }
51ab519a01SNathan Fontenot 
52ab519a01SNathan Fontenot static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
53ab519a01SNathan Fontenot {
54ab519a01SNathan Fontenot 	struct property *prop;
55ab519a01SNathan Fontenot 	char *name;
56ab519a01SNathan Fontenot 	char *value;
57ab519a01SNathan Fontenot 
58ab519a01SNathan Fontenot 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
59ab519a01SNathan Fontenot 	if (!prop)
60ab519a01SNathan Fontenot 		return NULL;
61ab519a01SNathan Fontenot 
62d6f1e7abSBharata B Rao 	name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
63ab519a01SNathan Fontenot 	prop->name = kstrdup(name, GFP_KERNEL);
64ab519a01SNathan Fontenot 
65d6f1e7abSBharata B Rao 	prop->length = be32_to_cpu(ccwa->prop_length);
66d6f1e7abSBharata B Rao 	value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
67e72ed6b5SNishanth Aravamudan 	prop->value = kmemdup(value, prop->length, GFP_KERNEL);
68ab519a01SNathan Fontenot 	if (!prop->value) {
69ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
70ab519a01SNathan Fontenot 		return NULL;
71ab519a01SNathan Fontenot 	}
72ab519a01SNathan Fontenot 
73ab519a01SNathan Fontenot 	return prop;
74ab519a01SNathan Fontenot }
75ab519a01SNathan Fontenot 
7606665989SRob Herring static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
77ab519a01SNathan Fontenot {
78ab519a01SNathan Fontenot 	struct device_node *dn;
7906665989SRob Herring 	const char *name;
808d5ff320STyrel Datwyler 
81ab519a01SNathan Fontenot 	dn = kzalloc(sizeof(*dn), GFP_KERNEL);
82ab519a01SNathan Fontenot 	if (!dn)
83ab519a01SNathan Fontenot 		return NULL;
84ab519a01SNathan Fontenot 
8506665989SRob Herring 	name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
8606665989SRob Herring 	dn->full_name = kstrdup(name, GFP_KERNEL);
87ab519a01SNathan Fontenot 	if (!dn->full_name) {
88ab519a01SNathan Fontenot 		kfree(dn);
89ab519a01SNathan Fontenot 		return NULL;
90ab519a01SNathan Fontenot 	}
91ab519a01SNathan Fontenot 
921578cb76STyrel Datwyler 	of_node_set_flag(dn, OF_DYNAMIC);
9397a9a717STyrel Datwyler 	of_node_init(dn);
941578cb76STyrel Datwyler 
95ab519a01SNathan Fontenot 	return dn;
96ab519a01SNathan Fontenot }
97ab519a01SNathan Fontenot 
98ab519a01SNathan Fontenot static void dlpar_free_one_cc_node(struct device_node *dn)
99ab519a01SNathan Fontenot {
100ab519a01SNathan Fontenot 	struct property *prop;
101ab519a01SNathan Fontenot 
102ab519a01SNathan Fontenot 	while (dn->properties) {
103ab519a01SNathan Fontenot 		prop = dn->properties;
104ab519a01SNathan Fontenot 		dn->properties = prop->next;
105ab519a01SNathan Fontenot 		dlpar_free_cc_property(prop);
106ab519a01SNathan Fontenot 	}
107ab519a01SNathan Fontenot 
108ab519a01SNathan Fontenot 	kfree(dn->full_name);
109ab519a01SNathan Fontenot 	kfree(dn);
110ab519a01SNathan Fontenot }
111ab519a01SNathan Fontenot 
11220648974SNathan Fontenot void dlpar_free_cc_nodes(struct device_node *dn)
113ab519a01SNathan Fontenot {
114ab519a01SNathan Fontenot 	if (dn->child)
115ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->child);
116ab519a01SNathan Fontenot 
117ab519a01SNathan Fontenot 	if (dn->sibling)
118ab519a01SNathan Fontenot 		dlpar_free_cc_nodes(dn->sibling);
119ab519a01SNathan Fontenot 
120ab519a01SNathan Fontenot 	dlpar_free_one_cc_node(dn);
121ab519a01SNathan Fontenot }
122ab519a01SNathan Fontenot 
1239c740025SAnton Blanchard #define COMPLETE	0
124ab519a01SNathan Fontenot #define NEXT_SIBLING    1
125ab519a01SNathan Fontenot #define NEXT_CHILD      2
126ab519a01SNathan Fontenot #define NEXT_PROPERTY   3
127ab519a01SNathan Fontenot #define PREV_PARENT     4
128ab519a01SNathan Fontenot #define MORE_MEMORY     5
129ab519a01SNathan Fontenot #define CALL_AGAIN	-2
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 
170ab519a01SNathan Fontenot 		switch (rc) {
1719c740025SAnton Blanchard 		case COMPLETE:
1729c740025SAnton Blanchard 			break;
1739c740025SAnton Blanchard 
174ab519a01SNathan Fontenot 		case NEXT_SIBLING:
17506665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
176ab519a01SNathan Fontenot 			if (!dn)
177ab519a01SNathan Fontenot 				goto cc_error;
178ab519a01SNathan Fontenot 
179ab519a01SNathan Fontenot 			dn->parent = last_dn->parent;
180ab519a01SNathan Fontenot 			last_dn->sibling = dn;
181ab519a01SNathan Fontenot 			last_dn = dn;
182ab519a01SNathan Fontenot 			break;
183ab519a01SNathan Fontenot 
184ab519a01SNathan Fontenot 		case NEXT_CHILD:
18506665989SRob Herring 			dn = dlpar_parse_cc_node(ccwa);
186ab519a01SNathan Fontenot 			if (!dn)
187ab519a01SNathan Fontenot 				goto cc_error;
188ab519a01SNathan Fontenot 
1898d5ff320STyrel Datwyler 			if (!first_dn) {
1908d5ff320STyrel Datwyler 				dn->parent = parent;
191ab519a01SNathan Fontenot 				first_dn = dn;
1928d5ff320STyrel Datwyler 			} else {
193ab519a01SNathan Fontenot 				dn->parent = last_dn;
194ab519a01SNathan Fontenot 				if (last_dn)
195ab519a01SNathan Fontenot 					last_dn->child = dn;
196ab519a01SNathan Fontenot 			}
197ab519a01SNathan Fontenot 
198ab519a01SNathan Fontenot 			last_dn = dn;
199ab519a01SNathan Fontenot 			break;
200ab519a01SNathan Fontenot 
201ab519a01SNathan Fontenot 		case NEXT_PROPERTY:
202ab519a01SNathan Fontenot 			property = dlpar_parse_cc_property(ccwa);
203ab519a01SNathan Fontenot 			if (!property)
204ab519a01SNathan Fontenot 				goto cc_error;
205ab519a01SNathan Fontenot 
206ab519a01SNathan Fontenot 			if (!last_dn->properties)
207ab519a01SNathan Fontenot 				last_dn->properties = property;
208ab519a01SNathan Fontenot 			else
209ab519a01SNathan Fontenot 				last_property->next = property;
210ab519a01SNathan Fontenot 
211ab519a01SNathan Fontenot 			last_property = property;
212ab519a01SNathan Fontenot 			break;
213ab519a01SNathan Fontenot 
214ab519a01SNathan Fontenot 		case PREV_PARENT:
215ab519a01SNathan Fontenot 			last_dn = last_dn->parent;
216ab519a01SNathan Fontenot 			break;
217ab519a01SNathan Fontenot 
218ab519a01SNathan Fontenot 		case CALL_AGAIN:
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 
273ab519a01SNathan Fontenot 	return 0;
274ab519a01SNathan Fontenot }
275ab519a01SNathan Fontenot 
276ab519a01SNathan Fontenot #define DR_ENTITY_SENSE		9003
277ab519a01SNathan Fontenot #define DR_ENTITY_PRESENT	1
278ab519a01SNathan Fontenot #define DR_ENTITY_UNUSABLE	2
279ab519a01SNathan Fontenot #define ALLOCATION_STATE	9003
280ab519a01SNathan Fontenot #define ALLOC_UNUSABLE		0
281ab519a01SNathan Fontenot #define ALLOC_USABLE		1
282ab519a01SNathan Fontenot #define ISOLATION_STATE		9001
283ab519a01SNathan Fontenot #define ISOLATE			0
284ab519a01SNathan Fontenot #define UNISOLATE		1
285ab519a01SNathan Fontenot 
286ab519a01SNathan Fontenot int dlpar_acquire_drc(u32 drc_index)
287ab519a01SNathan Fontenot {
288ab519a01SNathan Fontenot 	int dr_status, rc;
289ab519a01SNathan Fontenot 
290ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
291ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
292ab519a01SNathan Fontenot 	if (rc || dr_status != DR_ENTITY_UNUSABLE)
293ab519a01SNathan Fontenot 		return -1;
294ab519a01SNathan Fontenot 
295ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
296ab519a01SNathan Fontenot 	if (rc)
297ab519a01SNathan Fontenot 		return rc;
298ab519a01SNathan Fontenot 
299ab519a01SNathan Fontenot 	rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
300ab519a01SNathan Fontenot 	if (rc) {
301ab519a01SNathan Fontenot 		rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
302ab519a01SNathan Fontenot 		return rc;
303ab519a01SNathan Fontenot 	}
304ab519a01SNathan Fontenot 
305ab519a01SNathan Fontenot 	return 0;
306ab519a01SNathan Fontenot }
307ab519a01SNathan Fontenot 
308ab519a01SNathan Fontenot int dlpar_release_drc(u32 drc_index)
309ab519a01SNathan Fontenot {
310ab519a01SNathan Fontenot 	int dr_status, rc;
311ab519a01SNathan Fontenot 
312ab519a01SNathan Fontenot 	rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
313ab519a01SNathan Fontenot 		       DR_ENTITY_SENSE, drc_index);
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 
330*fd12527aSNathan Fontenot int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
331999e2dadSNathan Fontenot {
332999e2dadSNathan Fontenot 	int rc;
333999e2dadSNathan Fontenot 
334999e2dadSNathan Fontenot 	/* pseries error logs are in BE format, convert to cpu type */
335999e2dadSNathan Fontenot 	switch (hp_elog->id_type) {
336999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_COUNT:
337999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_count =
338999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_count);
339999e2dadSNathan Fontenot 		break;
340999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_ID_DRC_INDEX:
341999e2dadSNathan Fontenot 		hp_elog->_drc_u.drc_index =
342999e2dadSNathan Fontenot 				be32_to_cpu(hp_elog->_drc_u.drc_index);
343333f7b76SSahil Mehta 		break;
344333f7b76SSahil Mehta 	case PSERIES_HP_ELOG_ID_DRC_IC:
345333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count =
346333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.count);
347333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index =
348333f7b76SSahil Mehta 				be32_to_cpu(hp_elog->_drc_u.ic.index);
349999e2dadSNathan Fontenot 	}
350999e2dadSNathan Fontenot 
351999e2dadSNathan Fontenot 	switch (hp_elog->resource) {
352999e2dadSNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_MEM:
353999e2dadSNathan Fontenot 		rc = dlpar_memory(hp_elog);
354999e2dadSNathan Fontenot 		break;
355e9d764f8SNathan Fontenot 	case PSERIES_HP_ELOG_RESOURCE_CPU:
356e9d764f8SNathan Fontenot 		rc = dlpar_cpu(hp_elog);
357e9d764f8SNathan Fontenot 		break;
358999e2dadSNathan Fontenot 	default:
359999e2dadSNathan Fontenot 		pr_warn_ratelimited("Invalid resource (%d) specified\n",
360999e2dadSNathan Fontenot 				    hp_elog->resource);
361999e2dadSNathan Fontenot 		rc = -EINVAL;
362999e2dadSNathan Fontenot 	}
363999e2dadSNathan Fontenot 
364999e2dadSNathan Fontenot 	return rc;
365999e2dadSNathan Fontenot }
366999e2dadSNathan Fontenot 
3677c98bd72SDaniel Axtens static void pseries_hp_work_fn(struct work_struct *work)
3689054619eSJohn Allen {
3699054619eSJohn Allen 	struct pseries_hp_work *hp_work =
3709054619eSJohn Allen 			container_of(work, struct pseries_hp_work, work);
3719054619eSJohn Allen 
3729054619eSJohn Allen 	handle_dlpar_errorlog(hp_work->errlog);
3739054619eSJohn Allen 
3749054619eSJohn Allen 	kfree(hp_work->errlog);
3759054619eSJohn Allen 	kfree((void *)work);
3769054619eSJohn Allen }
3779054619eSJohn Allen 
378*fd12527aSNathan Fontenot void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog)
3799054619eSJohn Allen {
3809054619eSJohn Allen 	struct pseries_hp_work *work;
3819054619eSJohn Allen 	struct pseries_hp_errorlog *hp_errlog_copy;
3829054619eSJohn Allen 
3839054619eSJohn Allen 	hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
3849054619eSJohn Allen 				 GFP_KERNEL);
3859054619eSJohn Allen 	memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
3869054619eSJohn Allen 
3879054619eSJohn Allen 	work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
3889054619eSJohn Allen 	if (work) {
3899054619eSJohn Allen 		INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
3909054619eSJohn Allen 		work->errlog = hp_errlog_copy;
3919054619eSJohn Allen 		queue_work(pseries_hp_wq, (struct work_struct *)work);
3929054619eSJohn Allen 	} else {
39390ce3514SAndrew Donnellan 		kfree(hp_errlog_copy);
3949054619eSJohn Allen 	}
3959054619eSJohn Allen }
3969054619eSJohn Allen 
39725b587fbSNathan Fontenot static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
39825b587fbSNathan Fontenot {
39925b587fbSNathan Fontenot 	char *arg;
40025b587fbSNathan Fontenot 
40125b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
40225b587fbSNathan Fontenot 	if (!arg)
40325b587fbSNathan Fontenot 		return -EINVAL;
40425b587fbSNathan Fontenot 
40525b587fbSNathan Fontenot 	if (sysfs_streq(arg, "memory")) {
40625b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
40725b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "cpu")) {
40825b587fbSNathan Fontenot 		hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
40925b587fbSNathan Fontenot 	} else {
41025b587fbSNathan Fontenot 		pr_err("Invalid resource specified.\n");
41125b587fbSNathan Fontenot 		return -EINVAL;
41225b587fbSNathan Fontenot 	}
41325b587fbSNathan Fontenot 
41425b587fbSNathan Fontenot 	return 0;
41525b587fbSNathan Fontenot }
41625b587fbSNathan Fontenot 
41725b587fbSNathan Fontenot static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
41825b587fbSNathan Fontenot {
41925b587fbSNathan Fontenot 	char *arg;
42025b587fbSNathan Fontenot 
42125b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
42225b587fbSNathan Fontenot 	if (!arg)
42325b587fbSNathan Fontenot 		return -EINVAL;
42425b587fbSNathan Fontenot 
42525b587fbSNathan Fontenot 	if (sysfs_streq(arg, "add")) {
42625b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
42725b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "remove")) {
42825b587fbSNathan Fontenot 		hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
42925b587fbSNathan Fontenot 	} else {
43025b587fbSNathan Fontenot 		pr_err("Invalid action specified.\n");
43125b587fbSNathan Fontenot 		return -EINVAL;
43225b587fbSNathan Fontenot 	}
43325b587fbSNathan Fontenot 
43425b587fbSNathan Fontenot 	return 0;
43525b587fbSNathan Fontenot }
43625b587fbSNathan Fontenot 
43725b587fbSNathan Fontenot static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
43825b587fbSNathan Fontenot {
43925b587fbSNathan Fontenot 	char *arg;
44025b587fbSNathan Fontenot 	u32 count, index;
44125b587fbSNathan Fontenot 
44225b587fbSNathan Fontenot 	arg = strsep(cmd, " ");
44325b587fbSNathan Fontenot 	if (!arg)
44425b587fbSNathan Fontenot 		return -EINVAL;
44525b587fbSNathan Fontenot 
446333f7b76SSahil Mehta 	if (sysfs_streq(arg, "indexed-count")) {
447333f7b76SSahil Mehta 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
448333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
449333f7b76SSahil Mehta 		if (!arg) {
450333f7b76SSahil Mehta 			pr_err("No DRC count specified.\n");
451333f7b76SSahil Mehta 			return -EINVAL;
452333f7b76SSahil Mehta 		}
453333f7b76SSahil Mehta 
454333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &count)) {
455333f7b76SSahil Mehta 			pr_err("Invalid DRC count specified.\n");
456333f7b76SSahil Mehta 			return -EINVAL;
457333f7b76SSahil Mehta 		}
458333f7b76SSahil Mehta 
459333f7b76SSahil Mehta 		arg = strsep(cmd, " ");
460333f7b76SSahil Mehta 		if (!arg) {
461333f7b76SSahil Mehta 			pr_err("No DRC Index specified.\n");
462333f7b76SSahil Mehta 			return -EINVAL;
463333f7b76SSahil Mehta 		}
464333f7b76SSahil Mehta 
465333f7b76SSahil Mehta 		if (kstrtou32(arg, 0, &index)) {
466333f7b76SSahil Mehta 			pr_err("Invalid DRC Index specified.\n");
467333f7b76SSahil Mehta 			return -EINVAL;
468333f7b76SSahil Mehta 		}
469333f7b76SSahil Mehta 
470333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.count = cpu_to_be32(count);
471333f7b76SSahil Mehta 		hp_elog->_drc_u.ic.index = cpu_to_be32(index);
472333f7b76SSahil Mehta 	} else if (sysfs_streq(arg, "index")) {
47325b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
47425b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
47525b587fbSNathan Fontenot 		if (!arg) {
47625b587fbSNathan Fontenot 			pr_err("No DRC Index specified.\n");
47725b587fbSNathan Fontenot 			return -EINVAL;
47825b587fbSNathan Fontenot 		}
47925b587fbSNathan Fontenot 
48025b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &index)) {
48125b587fbSNathan Fontenot 			pr_err("Invalid DRC Index specified.\n");
48225b587fbSNathan Fontenot 			return -EINVAL;
48325b587fbSNathan Fontenot 		}
48425b587fbSNathan Fontenot 
48525b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_index = cpu_to_be32(index);
48625b587fbSNathan Fontenot 	} else if (sysfs_streq(arg, "count")) {
48725b587fbSNathan Fontenot 		hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
48825b587fbSNathan Fontenot 		arg = strsep(cmd, " ");
48925b587fbSNathan Fontenot 		if (!arg) {
49025b587fbSNathan Fontenot 			pr_err("No DRC count specified.\n");
49125b587fbSNathan Fontenot 			return -EINVAL;
49225b587fbSNathan Fontenot 		}
49325b587fbSNathan Fontenot 
49425b587fbSNathan Fontenot 		if (kstrtou32(arg, 0, &count)) {
49525b587fbSNathan Fontenot 			pr_err("Invalid DRC count specified.\n");
49625b587fbSNathan Fontenot 			return -EINVAL;
49725b587fbSNathan Fontenot 		}
49825b587fbSNathan Fontenot 
49925b587fbSNathan Fontenot 		hp_elog->_drc_u.drc_count = cpu_to_be32(count);
50025b587fbSNathan Fontenot 	} else {
50125b587fbSNathan Fontenot 		pr_err("Invalid id_type specified.\n");
50225b587fbSNathan Fontenot 		return -EINVAL;
50325b587fbSNathan Fontenot 	}
50425b587fbSNathan Fontenot 
50525b587fbSNathan Fontenot 	return 0;
50625b587fbSNathan Fontenot }
50725b587fbSNathan Fontenot 
508999e2dadSNathan Fontenot static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
509999e2dadSNathan Fontenot 			   const char *buf, size_t count)
510999e2dadSNathan Fontenot {
511*fd12527aSNathan Fontenot 	struct pseries_hp_errorlog hp_elog;
51225b587fbSNathan Fontenot 	char *argbuf;
51325b587fbSNathan Fontenot 	char *args;
514999e2dadSNathan Fontenot 	int rc;
515999e2dadSNathan Fontenot 
51625b587fbSNathan Fontenot 	args = argbuf = kstrdup(buf, GFP_KERNEL);
517*fd12527aSNathan Fontenot 	if (!argbuf) {
51825b587fbSNathan Fontenot 		pr_info("Could not allocate resources for DLPAR operation\n");
51925b587fbSNathan Fontenot 		kfree(argbuf);
52025b587fbSNathan Fontenot 		return -ENOMEM;
521999e2dadSNathan Fontenot 	}
522999e2dadSNathan Fontenot 
52325b587fbSNathan Fontenot 	/*
52425b587fbSNathan Fontenot 	 * Parse out the request from the user, this will be in the form:
525999e2dadSNathan Fontenot 	 * <resource> <action> <id_type> <id>
526999e2dadSNathan Fontenot 	 */
527*fd12527aSNathan Fontenot 	rc = dlpar_parse_resource(&args, &hp_elog);
52825b587fbSNathan Fontenot 	if (rc)
529999e2dadSNathan Fontenot 		goto dlpar_store_out;
530999e2dadSNathan Fontenot 
531*fd12527aSNathan Fontenot 	rc = dlpar_parse_action(&args, &hp_elog);
53225b587fbSNathan Fontenot 	if (rc)
533999e2dadSNathan Fontenot 		goto dlpar_store_out;
534999e2dadSNathan Fontenot 
535*fd12527aSNathan Fontenot 	rc = dlpar_parse_id_type(&args, &hp_elog);
53625b587fbSNathan Fontenot 	if (rc)
537999e2dadSNathan Fontenot 		goto dlpar_store_out;
538999e2dadSNathan Fontenot 
539*fd12527aSNathan Fontenot 	rc = handle_dlpar_errorlog(&hp_elog);
540999e2dadSNathan Fontenot 
541999e2dadSNathan Fontenot dlpar_store_out:
54225b587fbSNathan Fontenot 	kfree(argbuf);
54325b587fbSNathan Fontenot 
54425b587fbSNathan Fontenot 	if (rc)
54525b587fbSNathan Fontenot 		pr_err("Could not handle DLPAR request \"%s\"\n", buf);
54625b587fbSNathan Fontenot 
547999e2dadSNathan Fontenot 	return rc ? rc : count;
548999e2dadSNathan Fontenot }
549999e2dadSNathan Fontenot 
550673bc435SNathan Fontenot static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
551673bc435SNathan Fontenot 			  char *buf)
552673bc435SNathan Fontenot {
553673bc435SNathan Fontenot 	return sprintf(buf, "%s\n", "memory,cpu");
554673bc435SNathan Fontenot }
555673bc435SNathan Fontenot 
5566f428096SGreg Kroah-Hartman static CLASS_ATTR_RW(dlpar);
557999e2dadSNathan Fontenot 
558e2d59152SMichael Ellerman int __init dlpar_workqueue_init(void)
5591a8061c4SNathan Fontenot {
560e2d59152SMichael Ellerman 	if (pseries_hp_wq)
561e2d59152SMichael Ellerman 		return 0;
562e2d59152SMichael Ellerman 
5639054619eSJohn Allen 	pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
5649054619eSJohn Allen 			WQ_UNBOUND, 1);
565e2d59152SMichael Ellerman 
566e2d59152SMichael Ellerman 	return pseries_hp_wq ? 0 : -ENOMEM;
567e2d59152SMichael Ellerman }
568e2d59152SMichael Ellerman 
569e2d59152SMichael Ellerman static int __init dlpar_sysfs_init(void)
570e2d59152SMichael Ellerman {
571e2d59152SMichael Ellerman 	int rc;
572e2d59152SMichael Ellerman 
573e2d59152SMichael Ellerman 	rc = dlpar_workqueue_init();
574e2d59152SMichael Ellerman 	if (rc)
575e2d59152SMichael Ellerman 		return rc;
576e2d59152SMichael Ellerman 
577183deeeaSNathan Fontenot 	return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
5781a8061c4SNathan Fontenot }
579e2d59152SMichael Ellerman machine_device_initcall(pseries, dlpar_sysfs_init);
5801a8061c4SNathan Fontenot 
581