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/kref.h>
16 #include <linux/notifier.h>
17 #include <linux/proc_fs.h>
18 #include <linux/slab.h>
22 #include <asm/machdep.h>
23 #include <asm/uaccess.h>
27 * derive_parent - basically like dirname(1)
28 * @path: the full_name of a node to be added to the tree
30 * Returns the node which should be the parent of the node
31 * described by path. E.g., for path = "/foo/bar", returns
32 * the node with full_name = "/foo".
34 static struct device_node *derive_parent(const char *path)
36 struct device_node *parent = NULL;
37 char *parent_path = "/";
38 size_t parent_path_len = strrchr(path, '/') - path + 1;
40 /* reject if path is "/" */
41 if (!strcmp(path, "/"))
42 return ERR_PTR(-EINVAL);
44 if (strrchr(path, '/') != path) {
45 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
47 return ERR_PTR(-ENOMEM);
48 strlcpy(parent_path, path, parent_path_len);
50 parent = of_find_node_by_path(parent_path);
52 return ERR_PTR(-EINVAL);
53 if (strcmp(parent_path, "/"))
58 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
60 struct device_node *np;
63 np = kzalloc(sizeof(*np), GFP_KERNEL);
67 np->full_name = kstrdup(path, GFP_KERNEL);
71 np->properties = proplist;
72 of_node_set_flag(np, OF_DYNAMIC);
75 np->parent = derive_parent(path);
76 if (IS_ERR(np->parent)) {
77 err = PTR_ERR(np->parent);
81 err = of_attach_node(np);
83 printk(KERN_ERR "Failed to add device node %s\n", path);
87 of_node_put(np->parent);
93 of_node_put(np->parent);
100 static int pSeries_reconfig_remove_node(struct device_node *np)
102 struct device_node *parent, *child;
104 parent = of_get_parent(np);
108 if ((child = of_get_next_child(np, NULL))) {
116 of_node_put(np); /* Must decrement the refcount */
121 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
122 * OF device nodes. Should be deprecated as soon as we get an
123 * in-kernel wrapper for the RTAS ibm,configure-connector call.
126 static void release_prop_list(const struct property *prop)
128 struct property *next;
129 for (; prop; prop = next) {
139 * parse_next_property - process the next property from raw input buffer
140 * @buf: input buffer, must be nul-terminated
141 * @end: end of the input buffer + 1, for validation
142 * @name: return value; set to property name in buf
143 * @length: return value; set to length of value
144 * @value: return value; set to the property value in buf
146 * Note that the caller must make copies of the name and value returned,
147 * this function does no allocation or copying of the data. Return value
148 * is set to the next name in buf, or NULL on error.
150 static char * parse_next_property(char *buf, char *end, char **name, int *length,
151 unsigned char **value)
157 tmp = strchr(buf, ' ');
159 printk(KERN_ERR "property parse failed in %s at line %d\n",
166 printk(KERN_ERR "property parse failed in %s at line %d\n",
171 /* now we're on the length */
173 *length = simple_strtoul(tmp, &tmp, 10);
175 printk(KERN_ERR "property parse failed in %s at line %d\n",
179 if (*tmp != ' ' || ++tmp >= end) {
180 printk(KERN_ERR "property parse failed in %s at line %d\n",
185 /* now we're on the value */
189 printk(KERN_ERR "property parse failed in %s at line %d\n",
193 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
194 printk(KERN_ERR "property parse failed in %s at line %d\n",
200 /* and now we should be on the next name, or the end */
204 static struct property *new_property(const char *name, const int length,
205 const unsigned char *value, struct property *last)
207 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
212 if (!(new->name = kstrdup(name, GFP_KERNEL)))
214 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
217 memcpy(new->value, value, length);
218 *(((char *)new->value) + length) = 0;
219 new->length = length;
230 static int do_add_node(char *buf, size_t bufsize)
232 char *path, *end, *name;
233 struct device_node *np;
234 struct property *prop = NULL;
235 unsigned char* value;
240 buf = strchr(buf, ' ');
246 if ((np = of_find_node_by_path(path))) {
251 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
253 (buf = parse_next_property(buf, end, &name, &length, &value))) {
254 struct property *last = prop;
256 prop = new_property(name, length, value, last);
268 rv = pSeries_reconfig_add_node(path, prop);
272 release_prop_list(prop);
276 static int do_remove_node(char *buf)
278 struct device_node *node;
281 if ((node = of_find_node_by_path(buf)))
282 rv = pSeries_reconfig_remove_node(node);
288 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
296 buf = strchr(buf, ' ');
302 handle = simple_strtoul(handle_str, NULL, 0);
304 *npp = of_find_node_by_phandle(handle);
308 static int do_add_property(char *buf, size_t bufsize)
310 struct property *prop = NULL;
311 struct device_node *np;
312 unsigned char *value;
316 buf = parse_node(buf, bufsize, &np);
321 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
324 prop = new_property(name, length, value, NULL);
328 of_add_property(np, prop);
333 static int do_remove_property(char *buf, size_t bufsize)
335 struct device_node *np;
337 struct property *prop;
338 buf = parse_node(buf, bufsize, &np);
343 tmp = strchr(buf,' ');
347 if (strlen(buf) == 0)
350 prop = of_find_property(np, buf, NULL);
352 return of_remove_property(np, prop);
355 static int do_update_property(char *buf, size_t bufsize)
357 struct device_node *np;
358 unsigned char *value;
359 char *name, *end, *next_prop;
361 struct property *newprop;
362 buf = parse_node(buf, bufsize, &np);
368 next_prop = parse_next_property(buf, end, &name, &length, &value);
375 newprop = new_property(name, length, value, NULL);
379 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
380 slb_set_size(*(int *)value);
382 return of_update_property(np, newprop);
386 * ofdt_write - perform operations on the Open Firmware device tree
389 * @buf: command and arguments
390 * @count: size of the command buffer
393 * Operations supported at this time are addition and removal of
394 * whole nodes along with their properties. Operations on individual
395 * properties are not implemented (yet).
397 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
404 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
408 if (copy_from_user(kbuf, buf, count)) {
415 tmp = strchr(kbuf, ' ');
423 if (!strcmp(kbuf, "add_node"))
424 rv = do_add_node(tmp, count - (tmp - kbuf));
425 else if (!strcmp(kbuf, "remove_node"))
426 rv = do_remove_node(tmp);
427 else if (!strcmp(kbuf, "add_property"))
428 rv = do_add_property(tmp, count - (tmp - kbuf));
429 else if (!strcmp(kbuf, "remove_property"))
430 rv = do_remove_property(tmp, count - (tmp - kbuf));
431 else if (!strcmp(kbuf, "update_property"))
432 rv = do_update_property(tmp, count - (tmp - kbuf));
437 return rv ? rv : count;
440 static const struct file_operations ofdt_fops = {
442 .llseek = noop_llseek,
445 /* create /proc/powerpc/ofdt write-only by root */
446 static int proc_ppc64_create_ofdt(void)
448 struct proc_dir_entry *ent;
450 if (!machine_is(pseries))
453 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
459 __initcall(proc_ppc64_create_ofdt);