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>
20 #include <asm/machdep.h>
21 #include <asm/uaccess.h>
22 #include <asm/pSeries_reconfig.h>
27 * Routines for "runtime" addition and removal of device tree nodes.
29 #ifdef CONFIG_PROC_DEVICETREE
31 * Add a node to /proc/device-tree.
33 static void add_node_proc_entries(struct device_node *np)
35 struct proc_dir_entry *ent;
37 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
39 proc_device_tree_add_node(np, ent);
42 static void remove_node_proc_entries(struct device_node *np)
44 struct property *pp = np->properties;
45 struct device_node *parent = np->parent;
48 remove_proc_entry(pp->name, np->pde);
52 remove_proc_entry(np->pde->name, parent->pde);
54 #else /* !CONFIG_PROC_DEVICETREE */
55 static void add_node_proc_entries(struct device_node *np)
60 static void remove_node_proc_entries(struct device_node *np)
64 #endif /* CONFIG_PROC_DEVICETREE */
67 * derive_parent - basically like dirname(1)
68 * @path: the full_name of a node to be added to the tree
70 * Returns the node which should be the parent of the node
71 * described by path. E.g., for path = "/foo/bar", returns
72 * the node with full_name = "/foo".
74 static struct device_node *derive_parent(const char *path)
76 struct device_node *parent = NULL;
77 char *parent_path = "/";
78 size_t parent_path_len = strrchr(path, '/') - path + 1;
80 /* reject if path is "/" */
81 if (!strcmp(path, "/"))
82 return ERR_PTR(-EINVAL);
84 if (strrchr(path, '/') != path) {
85 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
87 return ERR_PTR(-ENOMEM);
88 strlcpy(parent_path, path, parent_path_len);
90 parent = of_find_node_by_path(parent_path);
92 return ERR_PTR(-EINVAL);
93 if (strcmp(parent_path, "/"))
98 static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
100 int pSeries_reconfig_notifier_register(struct notifier_block *nb)
102 return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
105 void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
107 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
110 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
112 struct device_node *np;
115 np = kzalloc(sizeof(*np), GFP_KERNEL);
119 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
123 strcpy(np->full_name, path);
125 np->properties = proplist;
126 of_node_set_flag(np, OF_DYNAMIC);
127 kref_init(&np->kref);
129 np->parent = derive_parent(path);
130 if (IS_ERR(np->parent)) {
131 err = PTR_ERR(np->parent);
135 err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
136 PSERIES_RECONFIG_ADD, np);
137 if (err == NOTIFY_BAD) {
138 printk(KERN_ERR "Failed to add device node %s\n", path);
139 err = -ENOMEM; /* For now, safe to assume kmalloc failure */
145 add_node_proc_entries(np);
147 of_node_put(np->parent);
153 of_node_put(np->parent);
154 kfree(np->full_name);
160 static int pSeries_reconfig_remove_node(struct device_node *np)
162 struct device_node *parent, *child;
164 parent = of_get_parent(np);
168 if ((child = of_get_next_child(np, NULL))) {
174 remove_node_proc_entries(np);
176 blocking_notifier_call_chain(&pSeries_reconfig_chain,
177 PSERIES_RECONFIG_REMOVE, np);
181 of_node_put(np); /* Must decrement the refcount */
186 * /proc/ppc64/ofdt - yucky binary interface for adding and removing
187 * OF device nodes. Should be deprecated as soon as we get an
188 * in-kernel wrapper for the RTAS ibm,configure-connector call.
191 static void release_prop_list(const struct property *prop)
193 struct property *next;
194 for (; prop; prop = next) {
204 * parse_next_property - process the next property from raw input buffer
205 * @buf: input buffer, must be nul-terminated
206 * @end: end of the input buffer + 1, for validation
207 * @name: return value; set to property name in buf
208 * @length: return value; set to length of value
209 * @value: return value; set to the property value in buf
211 * Note that the caller must make copies of the name and value returned,
212 * this function does no allocation or copying of the data. Return value
213 * is set to the next name in buf, or NULL on error.
215 static char * parse_next_property(char *buf, char *end, char **name, int *length,
216 unsigned char **value)
222 tmp = strchr(buf, ' ');
224 printk(KERN_ERR "property parse failed in %s at line %d\n",
231 printk(KERN_ERR "property parse failed in %s at line %d\n",
236 /* now we're on the length */
238 *length = simple_strtoul(tmp, &tmp, 10);
240 printk(KERN_ERR "property parse failed in %s at line %d\n",
244 if (*tmp != ' ' || ++tmp >= end) {
245 printk(KERN_ERR "property parse failed in %s at line %d\n",
250 /* now we're on the value */
254 printk(KERN_ERR "property parse failed in %s at line %d\n",
258 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
259 printk(KERN_ERR "property parse failed in %s at line %d\n",
265 /* and now we should be on the next name, or the end */
269 static struct property *new_property(const char *name, const int length,
270 const unsigned char *value, struct property *last)
272 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
277 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
279 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
282 strcpy(new->name, name);
283 memcpy(new->value, value, length);
284 *(((char *)new->value) + length) = 0;
285 new->length = length;
296 static int do_add_node(char *buf, size_t bufsize)
298 char *path, *end, *name;
299 struct device_node *np;
300 struct property *prop = NULL;
301 unsigned char* value;
306 buf = strchr(buf, ' ');
312 if ((np = of_find_node_by_path(path))) {
317 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
319 (buf = parse_next_property(buf, end, &name, &length, &value))) {
320 struct property *last = prop;
322 prop = new_property(name, length, value, last);
334 rv = pSeries_reconfig_add_node(path, prop);
338 release_prop_list(prop);
342 static int do_remove_node(char *buf)
344 struct device_node *node;
347 if ((node = of_find_node_by_path(buf)))
348 rv = pSeries_reconfig_remove_node(node);
354 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
362 buf = strchr(buf, ' ');
368 handle = simple_strtoul(handle_str, NULL, 10);
370 *npp = of_find_node_by_phandle(handle);
374 static int do_add_property(char *buf, size_t bufsize)
376 struct property *prop = NULL;
377 struct device_node *np;
378 unsigned char *value;
382 buf = parse_node(buf, bufsize, &np);
387 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
390 prop = new_property(name, length, value, NULL);
394 prom_add_property(np, prop);
399 static int do_remove_property(char *buf, size_t bufsize)
401 struct device_node *np;
403 struct property *prop;
404 buf = parse_node(buf, bufsize, &np);
409 tmp = strchr(buf,' ');
413 if (strlen(buf) == 0)
416 prop = of_find_property(np, buf, NULL);
418 return prom_remove_property(np, prop);
421 static int do_update_property(char *buf, size_t bufsize)
423 struct device_node *np;
424 unsigned char *value;
427 struct property *newprop, *oldprop;
428 buf = parse_node(buf, bufsize, &np);
434 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
437 newprop = new_property(name, length, value, NULL);
441 oldprop = of_find_property(np, name,NULL);
445 return prom_update_property(np, newprop, oldprop);
449 * ofdt_write - perform operations on the Open Firmware device tree
452 * @buf: command and arguments
453 * @count: size of the command buffer
456 * Operations supported at this time are addition and removal of
457 * whole nodes along with their properties. Operations on individual
458 * properties are not implemented (yet).
460 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
467 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
471 if (copy_from_user(kbuf, buf, count)) {
478 tmp = strchr(kbuf, ' ');
486 if (!strcmp(kbuf, "add_node"))
487 rv = do_add_node(tmp, count - (tmp - kbuf));
488 else if (!strcmp(kbuf, "remove_node"))
489 rv = do_remove_node(tmp);
490 else if (!strcmp(kbuf, "add_property"))
491 rv = do_add_property(tmp, count - (tmp - kbuf));
492 else if (!strcmp(kbuf, "remove_property"))
493 rv = do_remove_property(tmp, count - (tmp - kbuf));
494 else if (!strcmp(kbuf, "update_property"))
495 rv = do_update_property(tmp, count - (tmp - kbuf));
500 return rv ? rv : count;
503 static const struct file_operations ofdt_fops = {
507 /* create /proc/ppc64/ofdt write-only by root */
508 static int proc_ppc64_create_ofdt(void)
510 struct proc_dir_entry *ent;
512 if (!machine_is(pseries))
515 ent = proc_create("ppc64/ofdt", S_IWUSR, NULL, &ofdt_fops);
521 __initcall(proc_ppc64_create_ofdt);