Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / arch / powerpc / platforms / pseries / reconfig.c
blob168651acdd83e003dae22f9bdee6147e89c1b1f5
1 /*
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>
20 #include <asm/prom.h>
21 #include <asm/machdep.h>
22 #include <asm/uaccess.h>
23 #include <asm/pSeries_reconfig.h>
24 #include <asm/mmu.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);
40 if (ent)
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;
49 while (pp) {
50 remove_proc_entry(pp->name, np->pde);
51 pp = pp->next;
53 if (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)
59 return;
62 static void remove_node_proc_entries(struct device_node *np)
64 return;
66 #endif /* CONFIG_PROC_DEVICETREE */
68 /**
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);
88 if (!parent_path)
89 return ERR_PTR(-ENOMEM);
90 strlcpy(parent_path, path, parent_path_len);
92 parent = of_find_node_by_path(parent_path);
93 if (!parent)
94 return ERR_PTR(-EINVAL);
95 if (strcmp(parent_path, "/"))
96 kfree(parent_path);
97 return parent;
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,
115 action, p);
117 return notifier_to_errno(err);
120 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
122 struct device_node *np;
123 int err = -ENOMEM;
125 np = kzalloc(sizeof(*np), GFP_KERNEL);
126 if (!np)
127 goto out_err;
129 np->full_name = kstrdup(path, GFP_KERNEL);
130 if (!np->full_name)
131 goto out_err;
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);
140 goto out_err;
143 err = pSeries_reconfig_notify(PSERIES_RECONFIG_ADD, np);
144 if (err) {
145 printk(KERN_ERR "Failed to add device node %s\n", path);
146 goto out_err;
149 of_attach_node(np);
151 add_node_proc_entries(np);
153 of_node_put(np->parent);
155 return 0;
157 out_err:
158 if (np) {
159 of_node_put(np->parent);
160 kfree(np->full_name);
161 kfree(np);
163 return err;
166 static int pSeries_reconfig_remove_node(struct device_node *np)
168 struct device_node *parent, *child;
170 parent = of_get_parent(np);
171 if (!parent)
172 return -EINVAL;
174 if ((child = of_get_next_child(np, NULL))) {
175 of_node_put(child);
176 of_node_put(parent);
177 return -EBUSY;
180 remove_node_proc_entries(np);
182 pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, np);
183 of_detach_node(np);
185 of_node_put(parent);
186 of_node_put(np); /* Must decrement the refcount */
187 return 0;
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) {
200 next = prop->next;
201 kfree(prop->name);
202 kfree(prop->value);
203 kfree(prop);
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)
223 char *tmp;
225 *name = buf;
227 tmp = strchr(buf, ' ');
228 if (!tmp) {
229 printk(KERN_ERR "property parse failed in %s at line %d\n",
230 __func__, __LINE__);
231 return NULL;
233 *tmp = '\0';
235 if (++tmp >= end) {
236 printk(KERN_ERR "property parse failed in %s at line %d\n",
237 __func__, __LINE__);
238 return NULL;
241 /* now we're on the length */
242 *length = -1;
243 *length = simple_strtoul(tmp, &tmp, 10);
244 if (*length == -1) {
245 printk(KERN_ERR "property parse failed in %s at line %d\n",
246 __func__, __LINE__);
247 return NULL;
249 if (*tmp != ' ' || ++tmp >= end) {
250 printk(KERN_ERR "property parse failed in %s at line %d\n",
251 __func__, __LINE__);
252 return NULL;
255 /* now we're on the value */
256 *value = tmp;
257 tmp += *length;
258 if (tmp > end) {
259 printk(KERN_ERR "property parse failed in %s at line %d\n",
260 __func__, __LINE__);
261 return NULL;
263 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
264 printk(KERN_ERR "property parse failed in %s at line %d\n",
265 __func__, __LINE__);
266 return NULL;
268 tmp++;
270 /* and now we should be on the next name, or the end */
271 return tmp;
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);
279 if (!new)
280 return NULL;
282 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
283 goto cleanup;
284 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
285 goto cleanup;
287 strcpy(new->name, name);
288 memcpy(new->value, value, length);
289 *(((char *)new->value) + length) = 0;
290 new->length = length;
291 new->next = last;
292 return new;
294 cleanup:
295 kfree(new->name);
296 kfree(new->value);
297 kfree(new);
298 return NULL;
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;
307 int length, rv = 0;
309 end = buf + bufsize;
310 path = buf;
311 buf = strchr(buf, ' ');
312 if (!buf)
313 return -EINVAL;
314 *buf = '\0';
315 buf++;
317 if ((np = of_find_node_by_path(path))) {
318 of_node_put(np);
319 return -EINVAL;
322 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
323 while (buf < end &&
324 (buf = parse_next_property(buf, end, &name, &length, &value))) {
325 struct property *last = prop;
327 prop = new_property(name, length, value, last);
328 if (!prop) {
329 rv = -ENOMEM;
330 prop = last;
331 goto out;
334 if (!buf) {
335 rv = -EINVAL;
336 goto out;
339 rv = pSeries_reconfig_add_node(path, prop);
341 out:
342 if (rv)
343 release_prop_list(prop);
344 return rv;
347 static int do_remove_node(char *buf)
349 struct device_node *node;
350 int rv = -ENODEV;
352 if ((node = of_find_node_by_path(buf)))
353 rv = pSeries_reconfig_remove_node(node);
355 of_node_put(node);
356 return rv;
359 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
361 char *handle_str;
362 phandle handle;
363 *npp = NULL;
365 handle_str = buf;
367 buf = strchr(buf, ' ');
368 if (!buf)
369 return NULL;
370 *buf = '\0';
371 buf++;
373 handle = simple_strtoul(handle_str, NULL, 0);
375 *npp = of_find_node_by_phandle(handle);
376 return buf;
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;
384 char *name, *end;
385 int length;
386 end = buf + bufsize;
387 buf = parse_node(buf, bufsize, &np);
389 if (!np)
390 return -ENODEV;
392 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
393 return -EINVAL;
395 prop = new_property(name, length, value, NULL);
396 if (!prop)
397 return -ENOMEM;
399 prom_add_property(np, prop);
401 return 0;
404 static int do_remove_property(char *buf, size_t bufsize)
406 struct device_node *np;
407 char *tmp;
408 struct property *prop;
409 buf = parse_node(buf, bufsize, &np);
411 if (!np)
412 return -ENODEV;
414 tmp = strchr(buf,' ');
415 if (tmp)
416 *tmp = '\0';
418 if (strlen(buf) == 0)
419 return -EINVAL;
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;
431 int rc, length;
432 struct property *newprop, *oldprop;
433 buf = parse_node(buf, bufsize, &np);
434 end = buf + bufsize;
436 if (!np)
437 return -ENODEV;
439 next_prop = parse_next_property(buf, end, &name, &length, &value);
440 if (!next_prop)
441 return -EINVAL;
443 newprop = new_property(name, length, value, NULL);
444 if (!newprop)
445 return -ENOMEM;
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);
451 if (!oldprop) {
452 if (strlen(name))
453 return prom_add_property(np, newprop);
454 return -ENODEV;
457 rc = prom_update_property(np, newprop, oldprop);
458 if (rc)
459 return rc;
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")) {
468 int action;
470 next_prop = parse_next_property(next_prop, end, &name,
471 &length, &value);
472 if (!next_prop)
473 return -EINVAL;
475 if (!strcmp(name, "add"))
476 action = PSERIES_DRCONF_MEM_ADD;
477 else
478 action = PSERIES_DRCONF_MEM_REMOVE;
480 rc = pSeries_reconfig_notify(action, value);
481 if (rc) {
482 prom_update_property(np, oldprop, newprop);
483 return rc;
487 return 0;
491 * ofdt_write - perform operations on the Open Firmware device tree
493 * @file: not used
494 * @buf: command and arguments
495 * @count: size of the command buffer
496 * @off: not used
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,
503 loff_t *off)
505 int rv = 0;
506 char *kbuf;
507 char *tmp;
509 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
510 rv = -ENOMEM;
511 goto out;
513 if (copy_from_user(kbuf, buf, count)) {
514 rv = -EFAULT;
515 goto out;
518 kbuf[count] = '\0';
520 tmp = strchr(kbuf, ' ');
521 if (!tmp) {
522 rv = -EINVAL;
523 goto out;
525 *tmp = '\0';
526 tmp++;
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));
538 else
539 rv = -EINVAL;
540 out:
541 kfree(kbuf);
542 return rv ? rv : count;
545 static const struct file_operations ofdt_fops = {
546 .write = ofdt_write,
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))
556 return 0;
558 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
559 if (ent)
560 ent->size = 0;
562 return 0;
564 __initcall(proc_ppc64_create_ofdt);