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>
17 #include <asm/machdep.h>
18 #include <linux/uaccess.h>
21 #include "of_helpers.h"
23 static int pSeries_reconfig_add_node(const char *path
, struct property
*proplist
)
25 struct device_node
*np
;
28 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
32 np
->full_name
= kstrdup(kbasename(path
), GFP_KERNEL
);
36 np
->properties
= proplist
;
37 of_node_set_flag(np
, OF_DYNAMIC
);
40 np
->parent
= pseries_of_derive_parent(path
);
41 if (IS_ERR(np
->parent
)) {
42 err
= PTR_ERR(np
->parent
);
46 err
= of_attach_node(np
);
48 printk(KERN_ERR
"Failed to add device node %s\n", path
);
52 of_node_put(np
->parent
);
58 of_node_put(np
->parent
);
65 static int pSeries_reconfig_remove_node(struct device_node
*np
)
67 struct device_node
*parent
, *child
;
69 parent
= of_get_parent(np
);
73 if ((child
= of_get_next_child(np
, NULL
))) {
85 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
86 * OF device nodes. Should be deprecated as soon as we get an
87 * in-kernel wrapper for the RTAS ibm,configure-connector call.
90 static void release_prop_list(const struct property
*prop
)
92 struct property
*next
;
93 for (; prop
; prop
= next
) {
103 * parse_next_property - process the next property from raw input buffer
104 * @buf: input buffer, must be nul-terminated
105 * @end: end of the input buffer + 1, for validation
106 * @name: return value; set to property name in buf
107 * @length: return value; set to length of value
108 * @value: return value; set to the property value in buf
110 * Note that the caller must make copies of the name and value returned,
111 * this function does no allocation or copying of the data. Return value
112 * is set to the next name in buf, or NULL on error.
114 static char * parse_next_property(char *buf
, char *end
, char **name
, int *length
,
115 unsigned char **value
)
121 tmp
= strchr(buf
, ' ');
123 printk(KERN_ERR
"property parse failed in %s at line %d\n",
130 printk(KERN_ERR
"property parse failed in %s at line %d\n",
135 /* now we're on the length */
137 *length
= simple_strtoul(tmp
, &tmp
, 10);
139 printk(KERN_ERR
"property parse failed in %s at line %d\n",
143 if (*tmp
!= ' ' || ++tmp
>= end
) {
144 printk(KERN_ERR
"property parse failed in %s at line %d\n",
149 /* now we're on the value */
153 printk(KERN_ERR
"property parse failed in %s at line %d\n",
157 else if (tmp
< end
&& *tmp
!= ' ' && *tmp
!= '\0') {
158 printk(KERN_ERR
"property parse failed in %s at line %d\n",
164 /* and now we should be on the next name, or the end */
168 static struct property
*new_property(const char *name
, const int length
,
169 const unsigned char *value
, struct property
*last
)
171 struct property
*new = kzalloc(sizeof(*new), GFP_KERNEL
);
176 if (!(new->name
= kstrdup(name
, GFP_KERNEL
)))
178 if (!(new->value
= kmalloc(length
+ 1, GFP_KERNEL
)))
181 memcpy(new->value
, value
, length
);
182 *(((char *)new->value
) + length
) = 0;
183 new->length
= length
;
194 static int do_add_node(char *buf
, size_t bufsize
)
196 char *path
, *end
, *name
;
197 struct device_node
*np
;
198 struct property
*prop
= NULL
;
199 unsigned char* value
;
204 buf
= strchr(buf
, ' ');
210 if ((np
= of_find_node_by_path(path
))) {
215 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
217 (buf
= parse_next_property(buf
, end
, &name
, &length
, &value
))) {
218 struct property
*last
= prop
;
220 prop
= new_property(name
, length
, value
, last
);
232 rv
= pSeries_reconfig_add_node(path
, prop
);
236 release_prop_list(prop
);
240 static int do_remove_node(char *buf
)
242 struct device_node
*node
;
245 if ((node
= of_find_node_by_path(buf
)))
246 rv
= pSeries_reconfig_remove_node(node
);
252 static char *parse_node(char *buf
, size_t bufsize
, struct device_node
**npp
)
260 buf
= strchr(buf
, ' ');
266 handle
= simple_strtoul(handle_str
, NULL
, 0);
268 *npp
= of_find_node_by_phandle(handle
);
272 static int do_add_property(char *buf
, size_t bufsize
)
274 struct property
*prop
= NULL
;
275 struct device_node
*np
;
276 unsigned char *value
;
280 buf
= parse_node(buf
, bufsize
, &np
);
285 if (parse_next_property(buf
, end
, &name
, &length
, &value
) == NULL
)
288 prop
= new_property(name
, length
, value
, NULL
);
292 of_add_property(np
, prop
);
297 static int do_remove_property(char *buf
, size_t bufsize
)
299 struct device_node
*np
;
301 buf
= parse_node(buf
, bufsize
, &np
);
306 tmp
= strchr(buf
,' ');
310 if (strlen(buf
) == 0)
313 return of_remove_property(np
, of_find_property(np
, buf
, NULL
));
316 static int do_update_property(char *buf
, size_t bufsize
)
318 struct device_node
*np
;
319 unsigned char *value
;
320 char *name
, *end
, *next_prop
;
322 struct property
*newprop
;
323 buf
= parse_node(buf
, bufsize
, &np
);
329 next_prop
= parse_next_property(buf
, end
, &name
, &length
, &value
);
336 newprop
= new_property(name
, length
, value
, NULL
);
340 if (!strcmp(name
, "slb-size") || !strcmp(name
, "ibm,slb-size"))
341 slb_set_size(*(int *)value
);
343 return of_update_property(np
, newprop
);
347 * ofdt_write - perform operations on the Open Firmware device tree
350 * @buf: command and arguments
351 * @count: size of the command buffer
354 * Operations supported at this time are addition and removal of
355 * whole nodes along with their properties. Operations on individual
356 * properties are not implemented (yet).
358 static ssize_t
ofdt_write(struct file
*file
, const char __user
*buf
, size_t count
,
365 kbuf
= memdup_user_nul(buf
, count
);
367 return PTR_ERR(kbuf
);
369 tmp
= strchr(kbuf
, ' ');
377 if (!strcmp(kbuf
, "add_node"))
378 rv
= do_add_node(tmp
, count
- (tmp
- kbuf
));
379 else if (!strcmp(kbuf
, "remove_node"))
380 rv
= do_remove_node(tmp
);
381 else if (!strcmp(kbuf
, "add_property"))
382 rv
= do_add_property(tmp
, count
- (tmp
- kbuf
));
383 else if (!strcmp(kbuf
, "remove_property"))
384 rv
= do_remove_property(tmp
, count
- (tmp
- kbuf
));
385 else if (!strcmp(kbuf
, "update_property"))
386 rv
= do_update_property(tmp
, count
- (tmp
- kbuf
));
391 return rv
? rv
: count
;
394 static const struct proc_ops ofdt_proc_ops
= {
395 .proc_write
= ofdt_write
,
396 .proc_lseek
= noop_llseek
,
399 /* create /proc/powerpc/ofdt write-only by root */
400 static int proc_ppc64_create_ofdt(void)
402 struct proc_dir_entry
*ent
;
404 ent
= proc_create("powerpc/ofdt", 0200, NULL
, &ofdt_proc_ops
);
406 proc_set_size(ent
, 0);
410 machine_device_initcall(pseries
, proc_ppc64_create_ofdt
);