blob: 1773103354be458c22c32e0f79f8a10bdac08868 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
4 *
5 * Copyright (C) 2005 Nathan Lynch
6 * Copyright (C) 2005 IBM Corporation
7 *
8 *
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.
12 */
13
14#include <linux/kernel.h>
15#include <linux/kref.h>
16#include <linux/notifier.h>
17#include <linux/proc_fs.h>
18
19#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110020#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110022#include <asm/pSeries_reconfig.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24
25
26/*
27 * Routines for "runtime" addition and removal of device tree nodes.
28 */
29#ifdef CONFIG_PROC_DEVICETREE
30/*
31 * Add a node to /proc/device-tree.
32 */
33static void add_node_proc_entries(struct device_node *np)
34{
35 struct proc_dir_entry *ent;
36
37 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
38 if (ent)
39 proc_device_tree_add_node(np, ent);
40}
41
42static void remove_node_proc_entries(struct device_node *np)
43{
44 struct property *pp = np->properties;
45 struct device_node *parent = np->parent;
46
47 while (pp) {
48 remove_proc_entry(pp->name, np->pde);
49 pp = pp->next;
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (np->pde)
52 remove_proc_entry(np->pde->name, parent->pde);
53}
54#else /* !CONFIG_PROC_DEVICETREE */
55static void add_node_proc_entries(struct device_node *np)
56{
57 return;
58}
59
60static void remove_node_proc_entries(struct device_node *np)
61{
62 return;
63}
64#endif /* CONFIG_PROC_DEVICETREE */
65
66/**
67 * derive_parent - basically like dirname(1)
68 * @path: the full_name of a node to be added to the tree
69 *
70 * Returns the node which should be the parent of the node
71 * described by path. E.g., for path = "/foo/bar", returns
72 * the node with full_name = "/foo".
73 */
74static struct device_node *derive_parent(const char *path)
75{
76 struct device_node *parent = NULL;
77 char *parent_path = "/";
78 size_t parent_path_len = strrchr(path, '/') - path + 1;
79
80 /* reject if path is "/" */
81 if (!strcmp(path, "/"))
82 return ERR_PTR(-EINVAL);
83
84 if (strrchr(path, '/') != path) {
85 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
86 if (!parent_path)
87 return ERR_PTR(-ENOMEM);
88 strlcpy(parent_path, path, parent_path_len);
89 }
90 parent = of_find_node_by_path(parent_path);
91 if (!parent)
92 return ERR_PTR(-EINVAL);
93 if (strcmp(parent_path, "/"))
94 kfree(parent_path);
95 return parent;
96}
97
Alan Sterne041c682006-03-27 01:16:30 -080098static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100int pSeries_reconfig_notifier_register(struct notifier_block *nb)
101{
Alan Sterne041c682006-03-27 01:16:30 -0800102 return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
106{
Alan Sterne041c682006-03-27 01:16:30 -0800107 blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
110static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
111{
112 struct device_node *np;
113 int err = -ENOMEM;
114
Pekka Enberg874ca6c2005-09-06 15:18:32 -0700115 np = kzalloc(sizeof(*np), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (!np)
117 goto out_err;
118
119 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
120 if (!np->full_name)
121 goto out_err;
122
123 strcpy(np->full_name, path);
124
125 np->properties = proplist;
126 OF_MARK_DYNAMIC(np);
127 kref_init(&np->kref);
128
129 np->parent = derive_parent(path);
130 if (IS_ERR(np->parent)) {
131 err = PTR_ERR(np->parent);
132 goto out_err;
133 }
134
Alan Sterne041c682006-03-27 01:16:30 -0800135 err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 PSERIES_RECONFIG_ADD, np);
137 if (err == NOTIFY_BAD) {
138 printk(KERN_ERR "Failed to add device node %s\n", path);
139 err = -ENOMEM; /* For now, safe to assume kmalloc failure */
140 goto out_err;
141 }
142
143 of_attach_node(np);
144
145 add_node_proc_entries(np);
146
147 of_node_put(np->parent);
148
149 return 0;
150
151out_err:
152 if (np) {
153 of_node_put(np->parent);
154 kfree(np->full_name);
155 kfree(np);
156 }
157 return err;
158}
159
160static int pSeries_reconfig_remove_node(struct device_node *np)
161{
162 struct device_node *parent, *child;
163
164 parent = of_get_parent(np);
165 if (!parent)
166 return -EINVAL;
167
168 if ((child = of_get_next_child(np, NULL))) {
169 of_node_put(child);
170 return -EBUSY;
171 }
172
173 remove_node_proc_entries(np);
174
Alan Sterne041c682006-03-27 01:16:30 -0800175 blocking_notifier_call_chain(&pSeries_reconfig_chain,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 PSERIES_RECONFIG_REMOVE, np);
177 of_detach_node(np);
178
179 of_node_put(parent);
180 of_node_put(np); /* Must decrement the refcount */
181 return 0;
182}
183
184/*
185 * /proc/ppc64/ofdt - yucky binary interface for adding and removing
186 * OF device nodes. Should be deprecated as soon as we get an
187 * in-kernel wrapper for the RTAS ibm,configure-connector call.
188 */
189
190static void release_prop_list(const struct property *prop)
191{
192 struct property *next;
193 for (; prop; prop = next) {
194 next = prop->next;
195 kfree(prop->name);
196 kfree(prop->value);
197 kfree(prop);
198 }
199
200}
201
202/**
203 * parse_next_property - process the next property from raw input buffer
204 * @buf: input buffer, must be nul-terminated
205 * @end: end of the input buffer + 1, for validation
206 * @name: return value; set to property name in buf
207 * @length: return value; set to length of value
208 * @value: return value; set to the property value in buf
209 *
210 * Note that the caller must make copies of the name and value returned,
211 * this function does no allocation or copying of the data. Return value
212 * is set to the next name in buf, or NULL on error.
213 */
214static char * parse_next_property(char *buf, char *end, char **name, int *length,
215 unsigned char **value)
216{
217 char *tmp;
218
219 *name = buf;
220
221 tmp = strchr(buf, ' ');
222 if (!tmp) {
223 printk(KERN_ERR "property parse failed in %s at line %d\n",
224 __FUNCTION__, __LINE__);
225 return NULL;
226 }
227 *tmp = '\0';
228
229 if (++tmp >= end) {
230 printk(KERN_ERR "property parse failed in %s at line %d\n",
231 __FUNCTION__, __LINE__);
232 return NULL;
233 }
234
235 /* now we're on the length */
236 *length = -1;
237 *length = simple_strtoul(tmp, &tmp, 10);
238 if (*length == -1) {
239 printk(KERN_ERR "property parse failed in %s at line %d\n",
240 __FUNCTION__, __LINE__);
241 return NULL;
242 }
243 if (*tmp != ' ' || ++tmp >= end) {
244 printk(KERN_ERR "property parse failed in %s at line %d\n",
245 __FUNCTION__, __LINE__);
246 return NULL;
247 }
248
249 /* now we're on the value */
250 *value = tmp;
251 tmp += *length;
252 if (tmp > end) {
253 printk(KERN_ERR "property parse failed in %s at line %d\n",
254 __FUNCTION__, __LINE__);
255 return NULL;
256 }
257 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
258 printk(KERN_ERR "property parse failed in %s at line %d\n",
259 __FUNCTION__, __LINE__);
260 return NULL;
261 }
262 tmp++;
263
264 /* and now we should be on the next name, or the end */
265 return tmp;
266}
267
268static struct property *new_property(const char *name, const int length,
269 const unsigned char *value, struct property *last)
270{
271 struct property *new = kmalloc(sizeof(*new), GFP_KERNEL);
272
273 if (!new)
274 return NULL;
275 memset(new, 0, sizeof(*new));
276
277 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
278 goto cleanup;
279 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
280 goto cleanup;
281
282 strcpy(new->name, name);
283 memcpy(new->value, value, length);
284 *(((char *)new->value) + length) = 0;
285 new->length = length;
286 new->next = last;
287 return new;
288
289cleanup:
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800290 kfree(new->name);
291 kfree(new->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 kfree(new);
293 return NULL;
294}
295
296static int do_add_node(char *buf, size_t bufsize)
297{
298 char *path, *end, *name;
299 struct device_node *np;
300 struct property *prop = NULL;
301 unsigned char* value;
302 int length, rv = 0;
303
304 end = buf + bufsize;
305 path = buf;
306 buf = strchr(buf, ' ');
307 if (!buf)
308 return -EINVAL;
309 *buf = '\0';
310 buf++;
311
312 if ((np = of_find_node_by_path(path))) {
313 of_node_put(np);
314 return -EINVAL;
315 }
316
317 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
318 while (buf < end &&
319 (buf = parse_next_property(buf, end, &name, &length, &value))) {
320 struct property *last = prop;
321
322 prop = new_property(name, length, value, last);
323 if (!prop) {
324 rv = -ENOMEM;
325 prop = last;
326 goto out;
327 }
328 }
329 if (!buf) {
330 rv = -EINVAL;
331 goto out;
332 }
333
334 rv = pSeries_reconfig_add_node(path, prop);
335
336out:
337 if (rv)
338 release_prop_list(prop);
339 return rv;
340}
341
342static int do_remove_node(char *buf)
343{
344 struct device_node *node;
345 int rv = -ENODEV;
346
347 if ((node = of_find_node_by_path(buf)))
348 rv = pSeries_reconfig_remove_node(node);
349
350 of_node_put(node);
351 return rv;
352}
353
Dave C Boutcher610d9152006-01-12 16:10:22 -0600354static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
355{
356 char *handle_str;
357 phandle handle;
358 *npp = NULL;
359
360 handle_str = buf;
361
362 buf = strchr(buf, ' ');
363 if (!buf)
364 return NULL;
365 *buf = '\0';
366 buf++;
367
368 handle = simple_strtoul(handle_str, NULL, 10);
369
370 *npp = of_find_node_by_phandle(handle);
371 return buf;
372}
373
374static int do_add_property(char *buf, size_t bufsize)
375{
376 struct property *prop = NULL;
377 struct device_node *np;
378 unsigned char *value;
379 char *name, *end;
380 int length;
381 end = buf + bufsize;
382 buf = parse_node(buf, bufsize, &np);
383
384 if (!np)
385 return -ENODEV;
386
387 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
388 return -EINVAL;
389
390 prop = new_property(name, length, value, NULL);
391 if (!prop)
392 return -ENOMEM;
393
394 prom_add_property(np, prop);
395
396 return 0;
397}
398
399static int do_remove_property(char *buf, size_t bufsize)
400{
401 struct device_node *np;
402 char *tmp;
403 struct property *prop;
404 buf = parse_node(buf, bufsize, &np);
405
406 if (!np)
407 return -ENODEV;
408
409 tmp = strchr(buf,' ');
410 if (tmp)
411 *tmp = '\0';
412
413 if (strlen(buf) == 0)
414 return -EINVAL;
415
416 prop = of_find_property(np, buf, NULL);
417
418 return prom_remove_property(np, prop);
419}
420
421static int do_update_property(char *buf, size_t bufsize)
422{
423 struct device_node *np;
424 unsigned char *value;
425 char *name, *end;
426 int length;
427 struct property *newprop, *oldprop;
428 buf = parse_node(buf, bufsize, &np);
429 end = buf + bufsize;
430
431 if (!np)
432 return -ENODEV;
433
434 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
435 return -EINVAL;
436
437 newprop = new_property(name, length, value, NULL);
438 if (!newprop)
439 return -ENOMEM;
440
441 oldprop = of_find_property(np, name,NULL);
442 if (!oldprop)
443 return -ENODEV;
444
445 return prom_update_property(np, newprop, oldprop);
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/**
449 * ofdt_write - perform operations on the Open Firmware device tree
450 *
451 * @file: not used
452 * @buf: command and arguments
453 * @count: size of the command buffer
454 * @off: not used
455 *
456 * Operations supported at this time are addition and removal of
457 * whole nodes along with their properties. Operations on individual
458 * properties are not implemented (yet).
459 */
460static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
461 loff_t *off)
462{
463 int rv = 0;
464 char *kbuf;
465 char *tmp;
466
467 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
468 rv = -ENOMEM;
469 goto out;
470 }
471 if (copy_from_user(kbuf, buf, count)) {
472 rv = -EFAULT;
473 goto out;
474 }
475
476 kbuf[count] = '\0';
477
478 tmp = strchr(kbuf, ' ');
479 if (!tmp) {
480 rv = -EINVAL;
481 goto out;
482 }
483 *tmp = '\0';
484 tmp++;
485
486 if (!strcmp(kbuf, "add_node"))
487 rv = do_add_node(tmp, count - (tmp - kbuf));
488 else if (!strcmp(kbuf, "remove_node"))
489 rv = do_remove_node(tmp);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600490 else if (!strcmp(kbuf, "add_property"))
491 rv = do_add_property(tmp, count - (tmp - kbuf));
492 else if (!strcmp(kbuf, "remove_property"))
493 rv = do_remove_property(tmp, count - (tmp - kbuf));
494 else if (!strcmp(kbuf, "update_property"))
495 rv = do_update_property(tmp, count - (tmp - kbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 else
497 rv = -EINVAL;
498out:
499 kfree(kbuf);
500 return rv ? rv : count;
501}
502
503static struct file_operations ofdt_fops = {
504 .write = ofdt_write
505};
506
507/* create /proc/ppc64/ofdt write-only by root */
508static int proc_ppc64_create_ofdt(void)
509{
510 struct proc_dir_entry *ent;
511
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100512 if (!machine_is(pseries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514
515 ent = create_proc_entry("ppc64/ofdt", S_IWUSR, NULL);
516 if (ent) {
517 ent->nlink = 1;
518 ent->data = NULL;
519 ent->size = 0;
520 ent->proc_fops = &ofdt_fops;
521 }
522
523 return 0;
524}
525__initcall(proc_ppc64_create_ofdt);