Linux Audio

Check our new training course

Loading...
  1/*
  2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
  3 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
  4 *
  5 * Copyright (C) 2005 Nathan Lynch
  6 * Copyright (C) 2005 IBM Corporation
  7 *
  8 *
  9 *	This program is free software; you can redistribute it and/or
 10 *	modify it under the terms of the GNU General Public License version
 11 *	2 as published by the Free Software Foundation.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/kref.h>
 16#include <linux/notifier.h>
 17#include <linux/proc_fs.h>
 18#include <linux/slab.h>
 19
 20#include <asm/prom.h>
 21#include <asm/machdep.h>
 22#include <asm/uaccess.h>
 23#include <asm/pSeries_reconfig.h>
 24#include <asm/mmu.h>
 25
 26
 27
 28/*
 29 * Routines for "runtime" addition and removal of device tree nodes.
 30 */
 31#ifdef CONFIG_PROC_DEVICETREE
 32/*
 33 * Add a node to /proc/device-tree.
 34 */
 35static void add_node_proc_entries(struct device_node *np)
 36{
 37	struct proc_dir_entry *ent;
 38
 39	ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
 40	if (ent)
 41		proc_device_tree_add_node(np, ent);
 42}
 43
 44static void remove_node_proc_entries(struct device_node *np)
 45{
 46	struct property *pp = np->properties;
 47	struct device_node *parent = np->parent;
 48
 49	while (pp) {
 50		remove_proc_entry(pp->name, np->pde);
 51		pp = pp->next;
 52	}
 53	if (np->pde)
 54		remove_proc_entry(np->pde->name, parent->pde);
 55}
 56#else /* !CONFIG_PROC_DEVICETREE */
 57static void add_node_proc_entries(struct device_node *np)
 58{
 59	return;
 60}
 61
 62static void remove_node_proc_entries(struct device_node *np)
 63{
 64	return;
 65}
 66#endif /* CONFIG_PROC_DEVICETREE */
 67
 68/**
 69 *	derive_parent - basically like dirname(1)
 70 *	@path:  the full_name of a node to be added to the tree
 71 *
 72 *	Returns the node which should be the parent of the node
 73 *	described by path.  E.g., for path = "/foo/bar", returns
 74 *	the node with full_name = "/foo".
 75 */
 76static struct device_node *derive_parent(const char *path)
 77{
 78	struct device_node *parent = NULL;
 79	char *parent_path = "/";
 80	size_t parent_path_len = strrchr(path, '/') - path + 1;
 81
 82	/* reject if path is "/" */
 83	if (!strcmp(path, "/"))
 84		return ERR_PTR(-EINVAL);
 85
 86	if (strrchr(path, '/') != path) {
 87		parent_path = kmalloc(parent_path_len, GFP_KERNEL);
 88		if (!parent_path)
 89			return ERR_PTR(-ENOMEM);
 90		strlcpy(parent_path, path, parent_path_len);
 91	}
 92	parent = of_find_node_by_path(parent_path);
 93	if (!parent)
 94		return ERR_PTR(-EINVAL);
 95	if (strcmp(parent_path, "/"))
 96		kfree(parent_path);
 97	return parent;
 98}
 99
100static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
101
102int pSeries_reconfig_notifier_register(struct notifier_block *nb)
103{
104	return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
105}
106EXPORT_SYMBOL_GPL(pSeries_reconfig_notifier_register);
107
108void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
109{
110	blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
111}
112EXPORT_SYMBOL_GPL(pSeries_reconfig_notifier_unregister);
113
114int pSeries_reconfig_notify(unsigned long action, void *p)
115{
116	int err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
117						action, p);
118
119	return notifier_to_errno(err);
120}
121
122static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
123{
124	struct device_node *np;
125	int err = -ENOMEM;
126
127	np = kzalloc(sizeof(*np), GFP_KERNEL);
128	if (!np)
129		goto out_err;
130
131	np->full_name = kstrdup(path, GFP_KERNEL);
132	if (!np->full_name)
133		goto out_err;
134
135	np->properties = proplist;
136	of_node_set_flag(np, OF_DYNAMIC);
137	kref_init(&np->kref);
138
139	np->parent = derive_parent(path);
140	if (IS_ERR(np->parent)) {
141		err = PTR_ERR(np->parent);
142		goto out_err;
143	}
144
145	err = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, np);
146	if (err) {
147		printk(KERN_ERR "Failed to add device node %s\n", path);
148		goto out_err;
149	}
150
151	of_attach_node(np);
152
153	add_node_proc_entries(np);
154
155	of_node_put(np->parent);
156
157	return 0;
158
159out_err:
160	if (np) {
161		of_node_put(np->parent);
162		kfree(np->full_name);
163		kfree(np);
164	}
165	return err;
166}
167
168static int pSeries_reconfig_remove_node(struct device_node *np)
169{
170	struct device_node *parent, *child;
171
172	parent = of_get_parent(np);
173	if (!parent)
174		return -EINVAL;
175
176	if ((child = of_get_next_child(np, NULL))) {
177		of_node_put(child);
178		of_node_put(parent);
179		return -EBUSY;
180	}
181
182	remove_node_proc_entries(np);
183
184	pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, np);
185	of_detach_node(np);
186
187	of_node_put(parent);
188	of_node_put(np); /* Must decrement the refcount */
189	return 0;
190}
191
192/*
193 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
194 * OF device nodes.  Should be deprecated as soon as we get an
195 * in-kernel wrapper for the RTAS ibm,configure-connector call.
196 */
197
198static void release_prop_list(const struct property *prop)
199{
200	struct property *next;
201	for (; prop; prop = next) {
202		next = prop->next;
203		kfree(prop->name);
204		kfree(prop->value);
205		kfree(prop);
206	}
207
208}
209
210/**
211 * parse_next_property - process the next property from raw input buffer
212 * @buf: input buffer, must be nul-terminated
213 * @end: end of the input buffer + 1, for validation
214 * @name: return value; set to property name in buf
215 * @length: return value; set to length of value
216 * @value: return value; set to the property value in buf
217 *
218 * Note that the caller must make copies of the name and value returned,
219 * this function does no allocation or copying of the data.  Return value
220 * is set to the next name in buf, or NULL on error.
221 */
222static char * parse_next_property(char *buf, char *end, char **name, int *length,
223				  unsigned char **value)
224{
225	char *tmp;
226
227	*name = buf;
228
229	tmp = strchr(buf, ' ');
230	if (!tmp) {
231		printk(KERN_ERR "property parse failed in %s at line %d\n",
232		       __func__, __LINE__);
233		return NULL;
234	}
235	*tmp = '\0';
236
237	if (++tmp >= end) {
238		printk(KERN_ERR "property parse failed in %s at line %d\n",
239		       __func__, __LINE__);
240		return NULL;
241	}
242
243	/* now we're on the length */
244	*length = -1;
245	*length = simple_strtoul(tmp, &tmp, 10);
246	if (*length == -1) {
247		printk(KERN_ERR "property parse failed in %s at line %d\n",
248		       __func__, __LINE__);
249		return NULL;
250	}
251	if (*tmp != ' ' || ++tmp >= end) {
252		printk(KERN_ERR "property parse failed in %s at line %d\n",
253		       __func__, __LINE__);
254		return NULL;
255	}
256
257	/* now we're on the value */
258	*value = tmp;
259	tmp += *length;
260	if (tmp > end) {
261		printk(KERN_ERR "property parse failed in %s at line %d\n",
262		       __func__, __LINE__);
263		return NULL;
264	}
265	else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
266		printk(KERN_ERR "property parse failed in %s at line %d\n",
267		       __func__, __LINE__);
268		return NULL;
269	}
270	tmp++;
271
272	/* and now we should be on the next name, or the end */
273	return tmp;
274}
275
276static struct property *new_property(const char *name, const int length,
277				     const unsigned char *value, struct property *last)
278{
279	struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
280
281	if (!new)
282		return NULL;
283
284	if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
285		goto cleanup;
286	if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
287		goto cleanup;
288
289	strcpy(new->name, name);
290	memcpy(new->value, value, length);
291	*(((char *)new->value) + length) = 0;
292	new->length = length;
293	new->next = last;
294	return new;
295
296cleanup:
297	kfree(new->name);
298	kfree(new->value);
299	kfree(new);
300	return NULL;
301}
302
303static int do_add_node(char *buf, size_t bufsize)
304{
305	char *path, *end, *name;
306	struct device_node *np;
307	struct property *prop = NULL;
308	unsigned char* value;
309	int length, rv = 0;
310
311	end = buf + bufsize;
312	path = buf;
313	buf = strchr(buf, ' ');
314	if (!buf)
315		return -EINVAL;
316	*buf = '\0';
317	buf++;
318
319	if ((np = of_find_node_by_path(path))) {
320		of_node_put(np);
321		return -EINVAL;
322	}
323
324	/* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
325	while (buf < end &&
326	       (buf = parse_next_property(buf, end, &name, &length, &value))) {
327		struct property *last = prop;
328
329		prop = new_property(name, length, value, last);
330		if (!prop) {
331			rv = -ENOMEM;
332			prop = last;
333			goto out;
334		}
335	}
336	if (!buf) {
337		rv = -EINVAL;
338		goto out;
339	}
340
341	rv = pSeries_reconfig_add_node(path, prop);
342
343out:
344	if (rv)
345		release_prop_list(prop);
346	return rv;
347}
348
349static int do_remove_node(char *buf)
350{
351	struct device_node *node;
352	int rv = -ENODEV;
353
354	if ((node = of_find_node_by_path(buf)))
355		rv = pSeries_reconfig_remove_node(node);
356
357	of_node_put(node);
358	return rv;
359}
360
361static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
362{
363	char *handle_str;
364	phandle handle;
365	*npp = NULL;
366
367	handle_str = buf;
368
369	buf = strchr(buf, ' ');
370	if (!buf)
371		return NULL;
372	*buf = '\0';
373	buf++;
374
375	handle = simple_strtoul(handle_str, NULL, 0);
376
377	*npp = of_find_node_by_phandle(handle);
378	return buf;
379}
380
381static int do_add_property(char *buf, size_t bufsize)
382{
383	struct property *prop = NULL;
384	struct device_node *np;
385	unsigned char *value;
386	char *name, *end;
387	int length;
388	end = buf + bufsize;
389	buf = parse_node(buf, bufsize, &np);
390
391	if (!np)
392		return -ENODEV;
393
394	if (parse_next_property(buf, end, &name, &length, &value) == NULL)
395		return -EINVAL;
396
397	prop = new_property(name, length, value, NULL);
398	if (!prop)
399		return -ENOMEM;
400
401	prom_add_property(np, prop);
402
403	return 0;
404}
405
406static int do_remove_property(char *buf, size_t bufsize)
407{
408	struct device_node *np;
409	char *tmp;
410	struct property *prop;
411	buf = parse_node(buf, bufsize, &np);
412
413	if (!np)
414		return -ENODEV;
415
416	tmp = strchr(buf,' ');
417	if (tmp)
418		*tmp = '\0';
419
420	if (strlen(buf) == 0)
421		return -EINVAL;
422
423	prop = of_find_property(np, buf, NULL);
424
425	return prom_remove_property(np, prop);
426}
427
428static int do_update_property(char *buf, size_t bufsize)
429{
430	struct device_node *np;
431	struct pSeries_reconfig_prop_update upd_value;
432	unsigned char *value;
433	char *name, *end, *next_prop;
434	int rc, length;
435	struct property *newprop, *oldprop;
436	buf = parse_node(buf, bufsize, &np);
437	end = buf + bufsize;
438
439	if (!np)
440		return -ENODEV;
441
442	next_prop = parse_next_property(buf, end, &name, &length, &value);
443	if (!next_prop)
444		return -EINVAL;
445
446	newprop = new_property(name, length, value, NULL);
447	if (!newprop)
448		return -ENOMEM;
449
450	if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
451		slb_set_size(*(int *)value);
452
453	oldprop = of_find_property(np, name,NULL);
454	if (!oldprop) {
455		if (strlen(name))
456			return prom_add_property(np, newprop);
457		return -ENODEV;
458	}
459
460	upd_value.node = np;
461	upd_value.property = newprop;
462	pSeries_reconfig_notify(PSERIES_UPDATE_PROPERTY, &upd_value);
463
464	rc = prom_update_property(np, newprop, oldprop);
465	if (rc)
466		return rc;
467
468	/* For memory under the ibm,dynamic-reconfiguration-memory node
469	 * of the device tree, adding and removing memory is just an update
470	 * to the ibm,dynamic-memory property instead of adding/removing a
471	 * memory node in the device tree.  For these cases we still need to
472	 * involve the notifier chain.
473	 */
474	if (!strcmp(name, "ibm,dynamic-memory")) {
475		int action;
476
477		next_prop = parse_next_property(next_prop, end, &name,
478						&length, &value);
479		if (!next_prop)
480			return -EINVAL;
481
482		if (!strcmp(name, "add"))
483			action = PSERIES_DRCONF_MEM_ADD;
484		else
485			action = PSERIES_DRCONF_MEM_REMOVE;
486
487		rc = pSeries_reconfig_notify(action, value);
488		if (rc) {
489			prom_update_property(np, oldprop, newprop);
490			return rc;
491		}
492	}
493
494	return 0;
495}
496
497/**
498 * ofdt_write - perform operations on the Open Firmware device tree
499 *
500 * @file: not used
501 * @buf: command and arguments
502 * @count: size of the command buffer
503 * @off: not used
504 *
505 * Operations supported at this time are addition and removal of
506 * whole nodes along with their properties.  Operations on individual
507 * properties are not implemented (yet).
508 */
509static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
510			  loff_t *off)
511{
512	int rv = 0;
513	char *kbuf;
514	char *tmp;
515
516	if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
517		rv = -ENOMEM;
518		goto out;
519	}
520	if (copy_from_user(kbuf, buf, count)) {
521		rv = -EFAULT;
522		goto out;
523	}
524
525	kbuf[count] = '\0';
526
527	tmp = strchr(kbuf, ' ');
528	if (!tmp) {
529		rv = -EINVAL;
530		goto out;
531	}
532	*tmp = '\0';
533	tmp++;
534
535	if (!strcmp(kbuf, "add_node"))
536		rv = do_add_node(tmp, count - (tmp - kbuf));
537	else if (!strcmp(kbuf, "remove_node"))
538		rv = do_remove_node(tmp);
539	else if (!strcmp(kbuf, "add_property"))
540		rv = do_add_property(tmp, count - (tmp - kbuf));
541	else if (!strcmp(kbuf, "remove_property"))
542		rv = do_remove_property(tmp, count - (tmp - kbuf));
543	else if (!strcmp(kbuf, "update_property"))
544		rv = do_update_property(tmp, count - (tmp - kbuf));
545	else
546		rv = -EINVAL;
547out:
548	kfree(kbuf);
549	return rv ? rv : count;
550}
551
552static const struct file_operations ofdt_fops = {
553	.write = ofdt_write,
554	.llseek = noop_llseek,
555};
556
557/* create /proc/powerpc/ofdt write-only by root */
558static int proc_ppc64_create_ofdt(void)
559{
560	struct proc_dir_entry *ent;
561
562	if (!machine_is(pseries))
563		return 0;
564
565	ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
566	if (ent)
567		ent->size = 0;
568
569	return 0;
570}
571__initcall(proc_ppc64_create_ofdt);