1 // SPDX-License-Identifier: GPL-2.0-only
3 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
4 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
6 * Copyright (C) 2005 Nathan Lynch
7 * Copyright (C) 2005 IBM Corporation
10 #include <linux/kernel.h>
11 #include <linux/notifier.h>
12 #include <linux/proc_fs.h>
13 #include <linux/slab.h>
16 #include <asm/machdep.h>
17 #include <linux/uaccess.h>
20 #include "of_helpers.h"
22 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
24 struct device_node *np;
27 np = kzalloc(sizeof(*np), GFP_KERNEL);
31 np->full_name = kstrdup(kbasename(path), GFP_KERNEL);
35 np->properties = proplist;
36 of_node_set_flag(np, OF_DYNAMIC);
39 np->parent = pseries_of_derive_parent(path);
40 if (IS_ERR(np->parent)) {
41 err = PTR_ERR(np->parent);
45 err = of_attach_node(np);
47 printk(KERN_ERR "Failed to add device node %s\n", path);
51 of_node_put(np->parent);
57 of_node_put(np->parent);
64 static int pSeries_reconfig_remove_node(struct device_node *np)
66 struct device_node *parent, *child;
68 parent = of_get_parent(np);
72 if ((child = of_get_next_child(np, NULL))) {
84 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
85 * OF device nodes. Should be deprecated as soon as we get an
86 * in-kernel wrapper for the RTAS ibm,configure-connector call.
89 static void release_prop_list(const struct property *prop)
91 struct property *next;
92 for (; prop; prop = next) {
102 * parse_next_property - process the next property from raw input buffer
103 * @buf: input buffer, must be nul-terminated
104 * @end: end of the input buffer + 1, for validation
105 * @name: return value; set to property name in buf
106 * @length: return value; set to length of value
107 * @value: return value; set to the property value in buf
109 * Note that the caller must make copies of the name and value returned,
110 * this function does no allocation or copying of the data. Return value
111 * is set to the next name in buf, or NULL on error.
113 static char * parse_next_property(char *buf, char *end, char **name, int *length,
114 unsigned char **value)
120 tmp = strchr(buf, ' ');
122 printk(KERN_ERR "property parse failed in %s at line %d\n",
129 printk(KERN_ERR "property parse failed in %s at line %d\n",
134 /* now we're on the length */
136 *length = simple_strtoul(tmp, &tmp, 10);
138 printk(KERN_ERR "property parse failed in %s at line %d\n",
142 if (*tmp != ' ' || ++tmp >= end) {
143 printk(KERN_ERR "property parse failed in %s at line %d\n",
148 /* now we're on the value */
152 printk(KERN_ERR "property parse failed in %s at line %d\n",
156 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
157 printk(KERN_ERR "property parse failed in %s at line %d\n",
163 /* and now we should be on the next name, or the end */
167 static struct property *new_property(const char *name, const int length,
168 const unsigned char *value, struct property *last)
170 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
175 if (!(new->name = kstrdup(name, GFP_KERNEL)))
177 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
180 memcpy(new->value, value, length);
181 *(((char *)new->value) + length) = 0;
182 new->length = length;
193 static int do_add_node(char *buf, size_t bufsize)
195 char *path, *end, *name;
196 struct device_node *np;
197 struct property *prop = NULL;
198 unsigned char* value;
203 buf = strchr(buf, ' ');
209 if ((np = of_find_node_by_path(path))) {
214 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
216 (buf = parse_next_property(buf, end, &name, &length, &value))) {
217 struct property *last = prop;
219 prop = new_property(name, length, value, last);
231 rv = pSeries_reconfig_add_node(path, prop);
235 release_prop_list(prop);
239 static int do_remove_node(char *buf)
241 struct device_node *node;
244 if ((node = of_find_node_by_path(buf)))
245 rv = pSeries_reconfig_remove_node(node);
251 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
259 buf = strchr(buf, ' ');
265 handle = simple_strtoul(handle_str, NULL, 0);
267 *npp = of_find_node_by_phandle(handle);
271 static int do_add_property(char *buf, size_t bufsize)
273 struct property *prop = NULL;
274 struct device_node *np;
275 unsigned char *value;
279 buf = parse_node(buf, bufsize, &np);
284 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
287 prop = new_property(name, length, value, NULL);
291 of_add_property(np, prop);
296 static int do_remove_property(char *buf, size_t bufsize)
298 struct device_node *np;
300 buf = parse_node(buf, bufsize, &np);
305 tmp = strchr(buf,' ');
309 if (strlen(buf) == 0)
312 return of_remove_property(np, of_find_property(np, buf, NULL));
315 static int do_update_property(char *buf, size_t bufsize)
317 struct device_node *np;
318 unsigned char *value;
319 char *name, *end, *next_prop;
321 struct property *newprop;
322 buf = parse_node(buf, bufsize, &np);
328 next_prop = parse_next_property(buf, end, &name, &length, &value);
335 newprop = new_property(name, length, value, NULL);
339 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
340 slb_set_size(*(int *)value);
342 return of_update_property(np, newprop);
346 * ofdt_write - perform operations on the Open Firmware device tree
349 * @buf: command and arguments
350 * @count: size of the command buffer
353 * Operations supported at this time are addition and removal of
354 * whole nodes along with their properties. Operations on individual
355 * properties are not implemented (yet).
357 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
364 kbuf = memdup_user_nul(buf, count);
366 return PTR_ERR(kbuf);
368 tmp = strchr(kbuf, ' ');
376 if (!strcmp(kbuf, "add_node"))
377 rv = do_add_node(tmp, count - (tmp - kbuf));
378 else if (!strcmp(kbuf, "remove_node"))
379 rv = do_remove_node(tmp);
380 else if (!strcmp(kbuf, "add_property"))
381 rv = do_add_property(tmp, count - (tmp - kbuf));
382 else if (!strcmp(kbuf, "remove_property"))
383 rv = do_remove_property(tmp, count - (tmp - kbuf));
384 else if (!strcmp(kbuf, "update_property"))
385 rv = do_update_property(tmp, count - (tmp - kbuf));
390 return rv ? rv : count;
393 static const struct proc_ops ofdt_proc_ops = {
394 .proc_write = ofdt_write,
395 .proc_lseek = noop_llseek,
398 /* create /proc/powerpc/ofdt write-only by root */
399 static int proc_ppc64_create_ofdt(void)
401 struct proc_dir_entry *ent;
403 ent = proc_create("powerpc/ofdt", 0200, NULL, &ofdt_proc_ops);
405 proc_set_size(ent, 0);
409 machine_device_initcall(pseries, proc_ppc64_create_ofdt);