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>
26 * derive_parent - basically like dirname(1)
27 * @path: the full_name of a node to be added to the tree
29 * Returns the node which should be the parent of the node
30 * described by path. E.g., for path = "/foo/bar", returns
31 * the node with full_name = "/foo".
33 static struct device_node *derive_parent(const char *path)
35 struct device_node *parent = NULL;
36 char *parent_path = "/";
37 size_t parent_path_len = strrchr(path, '/') - path + 1;
39 /* reject if path is "/" */
40 if (!strcmp(path, "/"))
41 return ERR_PTR(-EINVAL);
43 if (strrchr(path, '/') != path) {
44 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
46 return ERR_PTR(-ENOMEM);
47 strlcpy(parent_path, path, parent_path_len);
49 parent = of_find_node_by_path(parent_path);
51 return ERR_PTR(-EINVAL);
52 if (strcmp(parent_path, "/"))
57 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
59 struct device_node *np;
62 np = kzalloc(sizeof(*np), GFP_KERNEL);
66 np->full_name = kstrdup(path, GFP_KERNEL);
70 np->properties = proplist;
71 of_node_set_flag(np, OF_DYNAMIC);
74 np->parent = derive_parent(path);
75 if (IS_ERR(np->parent)) {
76 err = PTR_ERR(np->parent);
80 err = of_attach_node(np);
82 printk(KERN_ERR "Failed to add device node %s\n", path);
86 of_node_put(np->parent);
92 of_node_put(np->parent);
99 static int pSeries_reconfig_remove_node(struct device_node *np)
101 struct device_node *parent, *child;
103 parent = of_get_parent(np);
107 if ((child = of_get_next_child(np, NULL))) {
115 of_node_put(np); /* Must decrement the refcount */
120 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
121 * OF device nodes. Should be deprecated as soon as we get an
122 * in-kernel wrapper for the RTAS ibm,configure-connector call.
125 static void release_prop_list(const struct property *prop)
127 struct property *next;
128 for (; prop; prop = next) {
138 * parse_next_property - process the next property from raw input buffer
139 * @buf: input buffer, must be nul-terminated
140 * @end: end of the input buffer + 1, for validation
141 * @name: return value; set to property name in buf
142 * @length: return value; set to length of value
143 * @value: return value; set to the property value in buf
145 * Note that the caller must make copies of the name and value returned,
146 * this function does no allocation or copying of the data. Return value
147 * is set to the next name in buf, or NULL on error.
149 static char * parse_next_property(char *buf, char *end, char **name, int *length,
150 unsigned char **value)
156 tmp = strchr(buf, ' ');
158 printk(KERN_ERR "property parse failed in %s at line %d\n",
165 printk(KERN_ERR "property parse failed in %s at line %d\n",
170 /* now we're on the length */
172 *length = simple_strtoul(tmp, &tmp, 10);
174 printk(KERN_ERR "property parse failed in %s at line %d\n",
178 if (*tmp != ' ' || ++tmp >= end) {
179 printk(KERN_ERR "property parse failed in %s at line %d\n",
184 /* now we're on the value */
188 printk(KERN_ERR "property parse failed in %s at line %d\n",
192 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
193 printk(KERN_ERR "property parse failed in %s at line %d\n",
199 /* and now we should be on the next name, or the end */
203 static struct property *new_property(const char *name, const int length,
204 const unsigned char *value, struct property *last)
206 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
211 if (!(new->name = kstrdup(name, GFP_KERNEL)))
213 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
216 memcpy(new->value, value, length);
217 *(((char *)new->value) + length) = 0;
218 new->length = length;
229 static int do_add_node(char *buf, size_t bufsize)
231 char *path, *end, *name;
232 struct device_node *np;
233 struct property *prop = NULL;
234 unsigned char* value;
239 buf = strchr(buf, ' ');
245 if ((np = of_find_node_by_path(path))) {
250 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
252 (buf = parse_next_property(buf, end, &name, &length, &value))) {
253 struct property *last = prop;
255 prop = new_property(name, length, value, last);
267 rv = pSeries_reconfig_add_node(path, prop);
271 release_prop_list(prop);
275 static int do_remove_node(char *buf)
277 struct device_node *node;
280 if ((node = of_find_node_by_path(buf)))
281 rv = pSeries_reconfig_remove_node(node);
287 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
295 buf = strchr(buf, ' ');
301 handle = simple_strtoul(handle_str, NULL, 0);
303 *npp = of_find_node_by_phandle(handle);
307 static int do_add_property(char *buf, size_t bufsize)
309 struct property *prop = NULL;
310 struct device_node *np;
311 unsigned char *value;
315 buf = parse_node(buf, bufsize, &np);
320 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
323 prop = new_property(name, length, value, NULL);
327 of_add_property(np, prop);
332 static int do_remove_property(char *buf, size_t bufsize)
334 struct device_node *np;
336 struct property *prop;
337 buf = parse_node(buf, bufsize, &np);
342 tmp = strchr(buf,' ');
346 if (strlen(buf) == 0)
349 prop = of_find_property(np, buf, NULL);
351 return of_remove_property(np, prop);
354 static int do_update_property(char *buf, size_t bufsize)
356 struct device_node *np;
357 unsigned char *value;
358 char *name, *end, *next_prop;
360 struct property *newprop;
361 buf = parse_node(buf, bufsize, &np);
367 next_prop = parse_next_property(buf, end, &name, &length, &value);
374 newprop = new_property(name, length, value, NULL);
378 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
379 slb_set_size(*(int *)value);
381 return of_update_property(np, newprop);
385 * ofdt_write - perform operations on the Open Firmware device tree
388 * @buf: command and arguments
389 * @count: size of the command buffer
392 * Operations supported at this time are addition and removal of
393 * whole nodes along with their properties. Operations on individual
394 * properties are not implemented (yet).
396 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
403 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
407 if (copy_from_user(kbuf, buf, count)) {
414 tmp = strchr(kbuf, ' ');
422 if (!strcmp(kbuf, "add_node"))
423 rv = do_add_node(tmp, count - (tmp - kbuf));
424 else if (!strcmp(kbuf, "remove_node"))
425 rv = do_remove_node(tmp);
426 else if (!strcmp(kbuf, "add_property"))
427 rv = do_add_property(tmp, count - (tmp - kbuf));
428 else if (!strcmp(kbuf, "remove_property"))
429 rv = do_remove_property(tmp, count - (tmp - kbuf));
430 else if (!strcmp(kbuf, "update_property"))
431 rv = do_update_property(tmp, count - (tmp - kbuf));
436 return rv ? rv : count;
439 static const struct file_operations ofdt_fops = {
441 .llseek = noop_llseek,
444 /* create /proc/powerpc/ofdt write-only by root */
445 static int proc_ppc64_create_ofdt(void)
447 struct proc_dir_entry *ent;
449 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
451 proc_set_size(ent, 0);
455 machine_device_initcall(pseries, proc_ppc64_create_ofdt);