2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
5 * Copyright (C) 2005 Nathan Lynch
6 * Copyright (C) 2005 IBM Corporation
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.
14 #include <linux/kernel.h>
15 #include <linux/notifier.h>
16 #include <linux/proc_fs.h>
17 #include <linux/slab.h>
21 #include <asm/machdep.h>
22 #include <asm/uaccess.h>
25 #include "of_helpers.h"
27 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
29 struct device_node *np;
32 np = kzalloc(sizeof(*np), GFP_KERNEL);
36 np->full_name = kstrdup(path, GFP_KERNEL);
40 np->properties = proplist;
41 of_node_set_flag(np, OF_DYNAMIC);
44 np->parent = pseries_of_derive_parent(path);
45 if (IS_ERR(np->parent)) {
46 err = PTR_ERR(np->parent);
50 err = of_attach_node(np);
52 printk(KERN_ERR "Failed to add device node %s\n", path);
56 of_node_put(np->parent);
62 of_node_put(np->parent);
69 static int pSeries_reconfig_remove_node(struct device_node *np)
71 struct device_node *parent, *child;
73 parent = of_get_parent(np);
77 if ((child = of_get_next_child(np, NULL))) {
89 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
90 * OF device nodes. Should be deprecated as soon as we get an
91 * in-kernel wrapper for the RTAS ibm,configure-connector call.
94 static void release_prop_list(const struct property *prop)
96 struct property *next;
97 for (; prop; prop = next) {
107 * parse_next_property - process the next property from raw input buffer
108 * @buf: input buffer, must be nul-terminated
109 * @end: end of the input buffer + 1, for validation
110 * @name: return value; set to property name in buf
111 * @length: return value; set to length of value
112 * @value: return value; set to the property value in buf
114 * Note that the caller must make copies of the name and value returned,
115 * this function does no allocation or copying of the data. Return value
116 * is set to the next name in buf, or NULL on error.
118 static char * parse_next_property(char *buf, char *end, char **name, int *length,
119 unsigned char **value)
125 tmp = strchr(buf, ' ');
127 printk(KERN_ERR "property parse failed in %s at line %d\n",
134 printk(KERN_ERR "property parse failed in %s at line %d\n",
139 /* now we're on the length */
141 *length = simple_strtoul(tmp, &tmp, 10);
143 printk(KERN_ERR "property parse failed in %s at line %d\n",
147 if (*tmp != ' ' || ++tmp >= end) {
148 printk(KERN_ERR "property parse failed in %s at line %d\n",
153 /* now we're on the value */
157 printk(KERN_ERR "property parse failed in %s at line %d\n",
161 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
162 printk(KERN_ERR "property parse failed in %s at line %d\n",
168 /* and now we should be on the next name, or the end */
172 static struct property *new_property(const char *name, const int length,
173 const unsigned char *value, struct property *last)
175 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
180 if (!(new->name = kstrdup(name, GFP_KERNEL)))
182 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
185 memcpy(new->value, value, length);
186 *(((char *)new->value) + length) = 0;
187 new->length = length;
198 static int do_add_node(char *buf, size_t bufsize)
200 char *path, *end, *name;
201 struct device_node *np;
202 struct property *prop = NULL;
203 unsigned char* value;
208 buf = strchr(buf, ' ');
214 if ((np = of_find_node_by_path(path))) {
219 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
221 (buf = parse_next_property(buf, end, &name, &length, &value))) {
222 struct property *last = prop;
224 prop = new_property(name, length, value, last);
236 rv = pSeries_reconfig_add_node(path, prop);
240 release_prop_list(prop);
244 static int do_remove_node(char *buf)
246 struct device_node *node;
249 if ((node = of_find_node_by_path(buf)))
250 rv = pSeries_reconfig_remove_node(node);
256 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
264 buf = strchr(buf, ' ');
270 handle = simple_strtoul(handle_str, NULL, 0);
272 *npp = of_find_node_by_phandle(handle);
276 static int do_add_property(char *buf, size_t bufsize)
278 struct property *prop = NULL;
279 struct device_node *np;
280 unsigned char *value;
284 buf = parse_node(buf, bufsize, &np);
289 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
292 prop = new_property(name, length, value, NULL);
296 of_add_property(np, prop);
301 static int do_remove_property(char *buf, size_t bufsize)
303 struct device_node *np;
305 struct property *prop;
306 buf = parse_node(buf, bufsize, &np);
311 tmp = strchr(buf,' ');
315 if (strlen(buf) == 0)
318 prop = of_find_property(np, buf, NULL);
320 return of_remove_property(np, prop);
323 static int do_update_property(char *buf, size_t bufsize)
325 struct device_node *np;
326 unsigned char *value;
327 char *name, *end, *next_prop;
329 struct property *newprop;
330 buf = parse_node(buf, bufsize, &np);
336 next_prop = parse_next_property(buf, end, &name, &length, &value);
343 newprop = new_property(name, length, value, NULL);
347 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
348 slb_set_size(*(int *)value);
350 return of_update_property(np, newprop);
354 * ofdt_write - perform operations on the Open Firmware device tree
357 * @buf: command and arguments
358 * @count: size of the command buffer
361 * Operations supported at this time are addition and removal of
362 * whole nodes along with their properties. Operations on individual
363 * properties are not implemented (yet).
365 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
372 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
376 if (copy_from_user(kbuf, buf, count)) {
383 tmp = strchr(kbuf, ' ');
391 if (!strcmp(kbuf, "add_node"))
392 rv = do_add_node(tmp, count - (tmp - kbuf));
393 else if (!strcmp(kbuf, "remove_node"))
394 rv = do_remove_node(tmp);
395 else if (!strcmp(kbuf, "add_property"))
396 rv = do_add_property(tmp, count - (tmp - kbuf));
397 else if (!strcmp(kbuf, "remove_property"))
398 rv = do_remove_property(tmp, count - (tmp - kbuf));
399 else if (!strcmp(kbuf, "update_property"))
400 rv = do_update_property(tmp, count - (tmp - kbuf));
405 return rv ? rv : count;
408 static const struct file_operations ofdt_fops = {
410 .llseek = noop_llseek,
413 /* create /proc/powerpc/ofdt write-only by root */
414 static int proc_ppc64_create_ofdt(void)
416 struct proc_dir_entry *ent;
418 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
420 proc_set_size(ent, 0);
424 machine_device_initcall(pseries, proc_ppc64_create_ofdt);