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/pSeries_reconfig.h>
21 #include <asm/uaccess.h>
26 * Routines for "runtime" addition and removal of device tree nodes.
28 #ifdef CONFIG_PROC_DEVICETREE
30 * Add a node to /proc/device-tree.
32 static void add_node_proc_entries(struct device_node
*np
)
34 struct proc_dir_entry
*ent
;
36 ent
= proc_mkdir(strrchr(np
->full_name
, '/') + 1, np
->parent
->pde
);
38 proc_device_tree_add_node(np
, ent
);
41 static void remove_node_proc_entries(struct device_node
*np
)
43 struct property
*pp
= np
->properties
;
44 struct device_node
*parent
= np
->parent
;
47 remove_proc_entry(pp
->name
, np
->pde
);
51 remove_proc_entry(np
->pde
->name
, parent
->pde
);
53 #else /* !CONFIG_PROC_DEVICETREE */
54 static void add_node_proc_entries(struct device_node
*np
)
59 static void remove_node_proc_entries(struct device_node
*np
)
63 #endif /* CONFIG_PROC_DEVICETREE */
66 * derive_parent - basically like dirname(1)
67 * @path: the full_name of a node to be added to the tree
69 * Returns the node which should be the parent of the node
70 * described by path. E.g., for path = "/foo/bar", returns
71 * the node with full_name = "/foo".
73 static struct device_node
*derive_parent(const char *path
)
75 struct device_node
*parent
= NULL
;
76 char *parent_path
= "/";
77 size_t parent_path_len
= strrchr(path
, '/') - path
+ 1;
79 /* reject if path is "/" */
80 if (!strcmp(path
, "/"))
81 return ERR_PTR(-EINVAL
);
83 if (strrchr(path
, '/') != path
) {
84 parent_path
= kmalloc(parent_path_len
, GFP_KERNEL
);
86 return ERR_PTR(-ENOMEM
);
87 strlcpy(parent_path
, path
, parent_path_len
);
89 parent
= of_find_node_by_path(parent_path
);
91 return ERR_PTR(-EINVAL
);
92 if (strcmp(parent_path
, "/"))
97 static struct notifier_block
*pSeries_reconfig_chain
;
99 int pSeries_reconfig_notifier_register(struct notifier_block
*nb
)
101 return notifier_chain_register(&pSeries_reconfig_chain
, nb
);
104 void pSeries_reconfig_notifier_unregister(struct notifier_block
*nb
)
106 notifier_chain_unregister(&pSeries_reconfig_chain
, nb
);
109 static int pSeries_reconfig_add_node(const char *path
, struct property
*proplist
)
111 struct device_node
*np
;
114 np
= kzalloc(sizeof(*np
), GFP_KERNEL
);
118 np
->full_name
= kmalloc(strlen(path
) + 1, GFP_KERNEL
);
122 strcpy(np
->full_name
, path
);
124 np
->properties
= proplist
;
126 kref_init(&np
->kref
);
128 np
->parent
= derive_parent(path
);
129 if (IS_ERR(np
->parent
)) {
130 err
= PTR_ERR(np
->parent
);
134 err
= notifier_call_chain(&pSeries_reconfig_chain
,
135 PSERIES_RECONFIG_ADD
, np
);
136 if (err
== NOTIFY_BAD
) {
137 printk(KERN_ERR
"Failed to add device node %s\n", path
);
138 err
= -ENOMEM
; /* For now, safe to assume kmalloc failure */
144 add_node_proc_entries(np
);
146 of_node_put(np
->parent
);
152 of_node_put(np
->parent
);
153 kfree(np
->full_name
);
159 static int pSeries_reconfig_remove_node(struct device_node
*np
)
161 struct device_node
*parent
, *child
;
163 parent
= of_get_parent(np
);
167 if ((child
= of_get_next_child(np
, NULL
))) {
172 remove_node_proc_entries(np
);
174 notifier_call_chain(&pSeries_reconfig_chain
,
175 PSERIES_RECONFIG_REMOVE
, np
);
179 of_node_put(np
); /* Must decrement the refcount */
184 * /proc/ppc64/ofdt - yucky binary interface for adding and removing
185 * OF device nodes. Should be deprecated as soon as we get an
186 * in-kernel wrapper for the RTAS ibm,configure-connector call.
189 static void release_prop_list(const struct property
*prop
)
191 struct property
*next
;
192 for (; prop
; prop
= next
) {
202 * parse_next_property - process the next property from raw input buffer
203 * @buf: input buffer, must be nul-terminated
204 * @end: end of the input buffer + 1, for validation
205 * @name: return value; set to property name in buf
206 * @length: return value; set to length of value
207 * @value: return value; set to the property value in buf
209 * Note that the caller must make copies of the name and value returned,
210 * this function does no allocation or copying of the data. Return value
211 * is set to the next name in buf, or NULL on error.
213 static char * parse_next_property(char *buf
, char *end
, char **name
, int *length
,
214 unsigned char **value
)
220 tmp
= strchr(buf
, ' ');
222 printk(KERN_ERR
"property parse failed in %s at line %d\n",
223 __FUNCTION__
, __LINE__
);
229 printk(KERN_ERR
"property parse failed in %s at line %d\n",
230 __FUNCTION__
, __LINE__
);
234 /* now we're on the length */
236 *length
= simple_strtoul(tmp
, &tmp
, 10);
238 printk(KERN_ERR
"property parse failed in %s at line %d\n",
239 __FUNCTION__
, __LINE__
);
242 if (*tmp
!= ' ' || ++tmp
>= end
) {
243 printk(KERN_ERR
"property parse failed in %s at line %d\n",
244 __FUNCTION__
, __LINE__
);
248 /* now we're on the value */
252 printk(KERN_ERR
"property parse failed in %s at line %d\n",
253 __FUNCTION__
, __LINE__
);
256 else if (tmp
< end
&& *tmp
!= ' ' && *tmp
!= '\0') {
257 printk(KERN_ERR
"property parse failed in %s at line %d\n",
258 __FUNCTION__
, __LINE__
);
263 /* and now we should be on the next name, or the end */
267 static struct property
*new_property(const char *name
, const int length
,
268 const unsigned char *value
, struct property
*last
)
270 struct property
*new = kmalloc(sizeof(*new), GFP_KERNEL
);
274 memset(new, 0, sizeof(*new));
276 if (!(new->name
= kmalloc(strlen(name
) + 1, GFP_KERNEL
)))
278 if (!(new->value
= kmalloc(length
+ 1, GFP_KERNEL
)))
281 strcpy(new->name
, name
);
282 memcpy(new->value
, value
, length
);
283 *(((char *)new->value
) + length
) = 0;
284 new->length
= length
;
295 static int do_add_node(char *buf
, size_t bufsize
)
297 char *path
, *end
, *name
;
298 struct device_node
*np
;
299 struct property
*prop
= NULL
;
300 unsigned char* value
;
305 buf
= strchr(buf
, ' ');
311 if ((np
= of_find_node_by_path(path
))) {
316 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
318 (buf
= parse_next_property(buf
, end
, &name
, &length
, &value
))) {
319 struct property
*last
= prop
;
321 prop
= new_property(name
, length
, value
, last
);
333 rv
= pSeries_reconfig_add_node(path
, prop
);
337 release_prop_list(prop
);
341 static int do_remove_node(char *buf
)
343 struct device_node
*node
;
346 if ((node
= of_find_node_by_path(buf
)))
347 rv
= pSeries_reconfig_remove_node(node
);
354 * ofdt_write - perform operations on the Open Firmware device tree
357 * @buf: command and arguments
358 * @count: size of the command buffer
361 * Operations supported at this time are addition and removal of
362 * whole nodes along with their properties. Operations on individual
363 * properties are not implemented (yet).
365 static ssize_t
ofdt_write(struct file
*file
, const char __user
*buf
, size_t count
,
372 if (!(kbuf
= kmalloc(count
+ 1, GFP_KERNEL
))) {
376 if (copy_from_user(kbuf
, buf
, count
)) {
383 tmp
= strchr(kbuf
, ' ');
391 if (!strcmp(kbuf
, "add_node"))
392 rv
= do_add_node(tmp
, count
- (tmp
- kbuf
));
393 else if (!strcmp(kbuf
, "remove_node"))
394 rv
= do_remove_node(tmp
);
399 return rv
? rv
: count
;
402 static struct file_operations ofdt_fops
= {
406 /* create /proc/ppc64/ofdt write-only by root */
407 static int proc_ppc64_create_ofdt(void)
409 struct proc_dir_entry
*ent
;
411 if (!platform_is_pseries())
414 ent
= create_proc_entry("ppc64/ofdt", S_IWUSR
, NULL
);
419 ent
->proc_fops
= &ofdt_fops
;
424 __initcall(proc_ppc64_create_ofdt
);