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>
21 #include <asm/machdep.h>
22 #include <asm/uaccess.h>
23 #include <asm/pSeries_reconfig.h>
29 * Routines for "runtime" addition and removal of device tree nodes.
31 #ifdef CONFIG_PROC_DEVICETREE
33 * Add a node to /proc/device-tree.
35 static void add_node_proc_entries(struct device_node
*np
)
37 struct proc_dir_entry
*ent
;
39 ent
= proc_mkdir(strrchr(np
->full_name
, '/') + 1, np
->parent
->pde
);
41 proc_device_tree_add_node(np
, ent
);
44 static void remove_node_proc_entries(struct device_node
*np
)
46 struct property
*pp
= np
->properties
;
47 struct device_node
*parent
= np
->parent
;
50 remove_proc_entry(pp
->name
, np
->pde
);
54 remove_proc_entry(np
->pde
->name
, parent
->pde
);
56 #else /* !CONFIG_PROC_DEVICETREE */
57 static void add_node_proc_entries(struct device_node
*np
)
62 static void remove_node_proc_entries(struct device_node
*np
)
66 #endif /* CONFIG_PROC_DEVICETREE */
69 * derive_parent - basically like dirname(1)
70 * @path: the full_name of a node to be added to the tree
72 * Returns the node which should be the parent of the node
73 * described by path. E.g., for path = "/foo/bar", returns
74 * the node with full_name = "/foo".
76 static struct device_node
*derive_parent(const char *path
)
78 struct device_node
*parent
= NULL
;
79 char *parent_path
= "/";
80 size_t parent_path_len
= strrchr(path
, '/') - path
+ 1;
82 /* reject if path is "/" */
83 if (!strcmp(path
, "/"))
84 return ERR_PTR(-EINVAL
);
86 if (strrchr(path
, '/') != path
) {
87 parent_path
= kmalloc(parent_path_len
, GFP_KERNEL
);
89 return ERR_PTR(-ENOMEM
);
90 strlcpy(parent_path
, path
, parent_path_len
);
92 parent
= of_find_node_by_path(parent_path
);
94 return ERR_PTR(-EINVAL
);
95 if (strcmp(parent_path
, "/"))
100 static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain
);
102 int pSeries_reconfig_notifier_register(struct notifier_block
*nb
)
104 return blocking_notifier_chain_register(&pSeries_reconfig_chain
, nb
);
107 void pSeries_reconfig_notifier_unregister(struct notifier_block
*nb
)
109 blocking_notifier_chain_unregister(&pSeries_reconfig_chain
, nb
);
112 int pSeries_reconfig_notify(unsigned long action
, void *p
)
114 int err
= blocking_notifier_call_chain(&pSeries_reconfig_chain
,
117 return notifier_to_errno(err
);
120 static int pSeries_reconfig_add_node(const char *path
, struct property
*proplist
)
122 struct device_node
*np
;
125 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
129 np
->full_name
= kstrdup(path
, GFP_KERNEL
);
133 np
->properties
= proplist
;
134 of_node_set_flag(np
, OF_DYNAMIC
);
135 kref_init(&np
->kref
);
137 np
->parent
= derive_parent(path
);
138 if (IS_ERR(np
->parent
)) {
139 err
= PTR_ERR(np
->parent
);
143 err
= pSeries_reconfig_notify(PSERIES_RECONFIG_ADD
, np
);
145 printk(KERN_ERR
"Failed to add device node %s\n", path
);
151 add_node_proc_entries(np
);
153 of_node_put(np
->parent
);
159 of_node_put(np
->parent
);
160 kfree(np
->full_name
);
166 static int pSeries_reconfig_remove_node(struct device_node
*np
)
168 struct device_node
*parent
, *child
;
170 parent
= of_get_parent(np
);
174 if ((child
= of_get_next_child(np
, NULL
))) {
180 remove_node_proc_entries(np
);
182 pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE
, np
);
186 of_node_put(np
); /* Must decrement the refcount */
191 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
192 * OF device nodes. Should be deprecated as soon as we get an
193 * in-kernel wrapper for the RTAS ibm,configure-connector call.
196 static void release_prop_list(const struct property
*prop
)
198 struct property
*next
;
199 for (; prop
; prop
= next
) {
209 * parse_next_property - process the next property from raw input buffer
210 * @buf: input buffer, must be nul-terminated
211 * @end: end of the input buffer + 1, for validation
212 * @name: return value; set to property name in buf
213 * @length: return value; set to length of value
214 * @value: return value; set to the property value in buf
216 * Note that the caller must make copies of the name and value returned,
217 * this function does no allocation or copying of the data. Return value
218 * is set to the next name in buf, or NULL on error.
220 static char * parse_next_property(char *buf
, char *end
, char **name
, int *length
,
221 unsigned char **value
)
227 tmp
= strchr(buf
, ' ');
229 printk(KERN_ERR
"property parse failed in %s at line %d\n",
236 printk(KERN_ERR
"property parse failed in %s at line %d\n",
241 /* now we're on the length */
243 *length
= simple_strtoul(tmp
, &tmp
, 10);
245 printk(KERN_ERR
"property parse failed in %s at line %d\n",
249 if (*tmp
!= ' ' || ++tmp
>= end
) {
250 printk(KERN_ERR
"property parse failed in %s at line %d\n",
255 /* now we're on the value */
259 printk(KERN_ERR
"property parse failed in %s at line %d\n",
263 else if (tmp
< end
&& *tmp
!= ' ' && *tmp
!= '\0') {
264 printk(KERN_ERR
"property parse failed in %s at line %d\n",
270 /* and now we should be on the next name, or the end */
274 static struct property
*new_property(const char *name
, const int length
,
275 const unsigned char *value
, struct property
*last
)
277 struct property
*new = kzalloc(sizeof(*new), GFP_KERNEL
);
282 if (!(new->name
= kmalloc(strlen(name
) + 1, GFP_KERNEL
)))
284 if (!(new->value
= kmalloc(length
+ 1, GFP_KERNEL
)))
287 strcpy(new->name
, name
);
288 memcpy(new->value
, value
, length
);
289 *(((char *)new->value
) + length
) = 0;
290 new->length
= length
;
301 static int do_add_node(char *buf
, size_t bufsize
)
303 char *path
, *end
, *name
;
304 struct device_node
*np
;
305 struct property
*prop
= NULL
;
306 unsigned char* value
;
311 buf
= strchr(buf
, ' ');
317 if ((np
= of_find_node_by_path(path
))) {
322 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
324 (buf
= parse_next_property(buf
, end
, &name
, &length
, &value
))) {
325 struct property
*last
= prop
;
327 prop
= new_property(name
, length
, value
, last
);
339 rv
= pSeries_reconfig_add_node(path
, prop
);
343 release_prop_list(prop
);
347 static int do_remove_node(char *buf
)
349 struct device_node
*node
;
352 if ((node
= of_find_node_by_path(buf
)))
353 rv
= pSeries_reconfig_remove_node(node
);
359 static char *parse_node(char *buf
, size_t bufsize
, struct device_node
**npp
)
367 buf
= strchr(buf
, ' ');
373 handle
= simple_strtoul(handle_str
, NULL
, 0);
375 *npp
= of_find_node_by_phandle(handle
);
379 static int do_add_property(char *buf
, size_t bufsize
)
381 struct property
*prop
= NULL
;
382 struct device_node
*np
;
383 unsigned char *value
;
387 buf
= parse_node(buf
, bufsize
, &np
);
392 if (parse_next_property(buf
, end
, &name
, &length
, &value
) == NULL
)
395 prop
= new_property(name
, length
, value
, NULL
);
399 prom_add_property(np
, prop
);
404 static int do_remove_property(char *buf
, size_t bufsize
)
406 struct device_node
*np
;
408 struct property
*prop
;
409 buf
= parse_node(buf
, bufsize
, &np
);
414 tmp
= strchr(buf
,' ');
418 if (strlen(buf
) == 0)
421 prop
= of_find_property(np
, buf
, NULL
);
423 return prom_remove_property(np
, prop
);
426 static int do_update_property(char *buf
, size_t bufsize
)
428 struct device_node
*np
;
429 unsigned char *value
;
430 char *name
, *end
, *next_prop
;
432 struct property
*newprop
, *oldprop
;
433 buf
= parse_node(buf
, bufsize
, &np
);
439 next_prop
= parse_next_property(buf
, end
, &name
, &length
, &value
);
443 newprop
= new_property(name
, length
, value
, NULL
);
447 if (!strcmp(name
, "slb-size") || !strcmp(name
, "ibm,slb-size"))
448 slb_set_size(*(int *)value
);
450 oldprop
= of_find_property(np
, name
,NULL
);
453 return prom_add_property(np
, newprop
);
457 rc
= prom_update_property(np
, newprop
, oldprop
);
461 /* For memory under the ibm,dynamic-reconfiguration-memory node
462 * of the device tree, adding and removing memory is just an update
463 * to the ibm,dynamic-memory property instead of adding/removing a
464 * memory node in the device tree. For these cases we still need to
465 * involve the notifier chain.
467 if (!strcmp(name
, "ibm,dynamic-memory")) {
470 next_prop
= parse_next_property(next_prop
, end
, &name
,
475 if (!strcmp(name
, "add"))
476 action
= PSERIES_DRCONF_MEM_ADD
;
478 action
= PSERIES_DRCONF_MEM_REMOVE
;
480 rc
= pSeries_reconfig_notify(action
, value
);
482 prom_update_property(np
, oldprop
, newprop
);
491 * ofdt_write - perform operations on the Open Firmware device tree
494 * @buf: command and arguments
495 * @count: size of the command buffer
498 * Operations supported at this time are addition and removal of
499 * whole nodes along with their properties. Operations on individual
500 * properties are not implemented (yet).
502 static ssize_t
ofdt_write(struct file
*file
, const char __user
*buf
, size_t count
,
509 if (!(kbuf
= kmalloc(count
+ 1, GFP_KERNEL
))) {
513 if (copy_from_user(kbuf
, buf
, count
)) {
520 tmp
= strchr(kbuf
, ' ');
528 if (!strcmp(kbuf
, "add_node"))
529 rv
= do_add_node(tmp
, count
- (tmp
- kbuf
));
530 else if (!strcmp(kbuf
, "remove_node"))
531 rv
= do_remove_node(tmp
);
532 else if (!strcmp(kbuf
, "add_property"))
533 rv
= do_add_property(tmp
, count
- (tmp
- kbuf
));
534 else if (!strcmp(kbuf
, "remove_property"))
535 rv
= do_remove_property(tmp
, count
- (tmp
- kbuf
));
536 else if (!strcmp(kbuf
, "update_property"))
537 rv
= do_update_property(tmp
, count
- (tmp
- kbuf
));
542 return rv
? rv
: count
;
545 static const struct file_operations ofdt_fops
= {
547 .llseek
= noop_llseek
,
550 /* create /proc/powerpc/ofdt write-only by root */
551 static int proc_ppc64_create_ofdt(void)
553 struct proc_dir_entry
*ent
;
555 if (!machine_is(pseries
))
558 ent
= proc_create("powerpc/ofdt", S_IWUSR
, NULL
, &ofdt_fops
);
564 __initcall(proc_ppc64_create_ofdt
);