Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / arch / powerpc / platforms / pseries / reconfig.c
1 /*
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 #include <linux/slab.h>
19
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>
25
26
27
28 /*
29  * Routines for "runtime" addition and removal of device tree nodes.
30  */
31 #ifdef CONFIG_PROC_DEVICETREE
32 /*
33  * Add a node to /proc/device-tree.
34  */
35 static void add_node_proc_entries(struct device_node *np)
36 {
37         struct proc_dir_entry *ent;
38
39         ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
40         if (ent)
41                 proc_device_tree_add_node(np, ent);
42 }
43
44 static void remove_node_proc_entries(struct device_node *np)
45 {
46         struct property *pp = np->properties;
47         struct device_node *parent = np->parent;
48
49         while (pp) {
50                 remove_proc_entry(pp->name, np->pde);
51                 pp = pp->next;
52         }
53         if (np->pde)
54                 remove_proc_entry(np->pde->name, parent->pde);
55 }
56 #else /* !CONFIG_PROC_DEVICETREE */
57 static void add_node_proc_entries(struct device_node *np)
58 {
59         return;
60 }
61
62 static void remove_node_proc_entries(struct device_node *np)
63 {
64         return;
65 }
66 #endif /* CONFIG_PROC_DEVICETREE */
67
68 /**
69  *      derive_parent - basically like dirname(1)
70  *      @path:  the full_name of a node to be added to the tree
71  *
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".
75  */
76 static struct device_node *derive_parent(const char *path)
77 {
78         struct device_node *parent = NULL;
79         char *parent_path = "/";
80         size_t parent_path_len = strrchr(path, '/') - path + 1;
81
82         /* reject if path is "/" */
83         if (!strcmp(path, "/"))
84                 return ERR_PTR(-EINVAL);
85
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);
91         }
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;
98 }
99
100 static BLOCKING_NOTIFIER_HEAD(pSeries_reconfig_chain);
101
102 int pSeries_reconfig_notifier_register(struct notifier_block *nb)
103 {
104         return blocking_notifier_chain_register(&pSeries_reconfig_chain, nb);
105 }
106
107 void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
108 {
109         blocking_notifier_chain_unregister(&pSeries_reconfig_chain, nb);
110 }
111
112 int pSeries_reconfig_notify(unsigned long action, void *p)
113 {
114         int err = blocking_notifier_call_chain(&pSeries_reconfig_chain,
115                                                 action, p);
116
117         return notifier_to_errno(err);
118 }
119
120 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
121 {
122         struct device_node *np;
123         int err = -ENOMEM;
124
125         np = kzalloc(sizeof(*np), GFP_KERNEL);
126         if (!np)
127                 goto out_err;
128
129         np->full_name = kstrdup(path, GFP_KERNEL);
130         if (!np->full_name)
131                 goto out_err;
132
133         np->properties = proplist;
134         of_node_set_flag(np, OF_DYNAMIC);
135         kref_init(&np->kref);
136
137         np->parent = derive_parent(path);
138         if (IS_ERR(np->parent)) {
139                 err = PTR_ERR(np->parent);
140                 goto out_err;
141         }
142
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;
147         }
148
149         of_attach_node(np);
150
151         add_node_proc_entries(np);
152
153         of_node_put(np->parent);
154
155         return 0;
156
157 out_err:
158         if (np) {
159                 of_node_put(np->parent);
160                 kfree(np->full_name);
161                 kfree(np);
162         }
163         return err;
164 }
165
166 static int pSeries_reconfig_remove_node(struct device_node *np)
167 {
168         struct device_node *parent, *child;
169
170         parent = of_get_parent(np);
171         if (!parent)
172                 return -EINVAL;
173
174         if ((child = of_get_next_child(np, NULL))) {
175                 of_node_put(child);
176                 of_node_put(parent);
177                 return -EBUSY;
178         }
179
180         remove_node_proc_entries(np);
181
182         pSeries_reconfig_notify(PSERIES_RECONFIG_REMOVE, np);
183         of_detach_node(np);
184
185         of_node_put(parent);
186         of_node_put(np); /* Must decrement the refcount */
187         return 0;
188 }
189
190 /*
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.
194  */
195
196 static void release_prop_list(const struct property *prop)
197 {
198         struct property *next;
199         for (; prop; prop = next) {
200                 next = prop->next;
201                 kfree(prop->name);
202                 kfree(prop->value);
203                 kfree(prop);
204         }
205
206 }
207
208 /**
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
215  *
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.
219  */
220 static char * parse_next_property(char *buf, char *end, char **name, int *length,
221                                   unsigned char **value)
222 {
223         char *tmp;
224
225         *name = buf;
226
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;
232         }
233         *tmp = '\0';
234
235         if (++tmp >= end) {
236                 printk(KERN_ERR "property parse failed in %s at line %d\n",
237                        __func__, __LINE__);
238                 return NULL;
239         }
240
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;
248         }
249         if (*tmp != ' ' || ++tmp >= end) {
250                 printk(KERN_ERR "property parse failed in %s at line %d\n",
251                        __func__, __LINE__);
252                 return NULL;
253         }
254
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;
262         }
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;
267         }
268         tmp++;
269
270         /* and now we should be on the next name, or the end */
271         return tmp;
272 }
273
274 static struct property *new_property(const char *name, const int length,
275                                      const unsigned char *value, struct property *last)
276 {
277         struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
278
279         if (!new)
280                 return NULL;
281
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;
286
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;
293
294 cleanup:
295         kfree(new->name);
296         kfree(new->value);
297         kfree(new);
298         return NULL;
299 }
300
301 static int do_add_node(char *buf, size_t bufsize)
302 {
303         char *path, *end, *name;
304         struct device_node *np;
305         struct property *prop = NULL;
306         unsigned char* value;
307         int length, rv = 0;
308
309         end = buf + bufsize;
310         path = buf;
311         buf = strchr(buf, ' ');
312         if (!buf)
313                 return -EINVAL;
314         *buf = '\0';
315         buf++;
316
317         if ((np = of_find_node_by_path(path))) {
318                 of_node_put(np);
319                 return -EINVAL;
320         }
321
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;
326
327                 prop = new_property(name, length, value, last);
328                 if (!prop) {
329                         rv = -ENOMEM;
330                         prop = last;
331                         goto out;
332                 }
333         }
334         if (!buf) {
335                 rv = -EINVAL;
336                 goto out;
337         }
338
339         rv = pSeries_reconfig_add_node(path, prop);
340
341 out:
342         if (rv)
343                 release_prop_list(prop);
344         return rv;
345 }
346
347 static int do_remove_node(char *buf)
348 {
349         struct device_node *node;
350         int rv = -ENODEV;
351
352         if ((node = of_find_node_by_path(buf)))
353                 rv = pSeries_reconfig_remove_node(node);
354
355         of_node_put(node);
356         return rv;
357 }
358
359 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
360 {
361         char *handle_str;
362         phandle handle;
363         *npp = NULL;
364
365         handle_str = buf;
366
367         buf = strchr(buf, ' ');
368         if (!buf)
369                 return NULL;
370         *buf = '\0';
371         buf++;
372
373         handle = simple_strtoul(handle_str, NULL, 0);
374
375         *npp = of_find_node_by_phandle(handle);
376         return buf;
377 }
378
379 static int do_add_property(char *buf, size_t bufsize)
380 {
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);
388
389         if (!np)
390                 return -ENODEV;
391
392         if (parse_next_property(buf, end, &name, &length, &value) == NULL)
393                 return -EINVAL;
394
395         prop = new_property(name, length, value, NULL);
396         if (!prop)
397                 return -ENOMEM;
398
399         prom_add_property(np, prop);
400
401         return 0;
402 }
403
404 static int do_remove_property(char *buf, size_t bufsize)
405 {
406         struct device_node *np;
407         char *tmp;
408         struct property *prop;
409         buf = parse_node(buf, bufsize, &np);
410
411         if (!np)
412                 return -ENODEV;
413
414         tmp = strchr(buf,' ');
415         if (tmp)
416                 *tmp = '\0';
417
418         if (strlen(buf) == 0)
419                 return -EINVAL;
420
421         prop = of_find_property(np, buf, NULL);
422
423         return prom_remove_property(np, prop);
424 }
425
426 static int do_update_property(char *buf, size_t bufsize)
427 {
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;
435
436         if (!np)
437                 return -ENODEV;
438
439         next_prop = parse_next_property(buf, end, &name, &length, &value);
440         if (!next_prop)
441                 return -EINVAL;
442
443         newprop = new_property(name, length, value, NULL);
444         if (!newprop)
445                 return -ENOMEM;
446
447         if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
448                 slb_set_size(*(int *)value);
449
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;
455         }
456
457         rc = prom_update_property(np, newprop, oldprop);
458         if (rc)
459                 return rc;
460
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.
466          */
467         if (!strcmp(name, "ibm,dynamic-memory")) {
468                 int action;
469
470                 next_prop = parse_next_property(next_prop, end, &name,
471                                                 &length, &value);
472                 if (!next_prop)
473                         return -EINVAL;
474
475                 if (!strcmp(name, "add"))
476                         action = PSERIES_DRCONF_MEM_ADD;
477                 else
478                         action = PSERIES_DRCONF_MEM_REMOVE;
479
480                 rc = pSeries_reconfig_notify(action, value);
481                 if (rc) {
482                         prom_update_property(np, oldprop, newprop);
483                         return rc;
484                 }
485         }
486
487         return 0;
488 }
489
490 /**
491  * ofdt_write - perform operations on the Open Firmware device tree
492  *
493  * @file: not used
494  * @buf: command and arguments
495  * @count: size of the command buffer
496  * @off: not used
497  *
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).
501  */
502 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
503                           loff_t *off)
504 {
505         int rv = 0;
506         char *kbuf;
507         char *tmp;
508
509         if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
510                 rv = -ENOMEM;
511                 goto out;
512         }
513         if (copy_from_user(kbuf, buf, count)) {
514                 rv = -EFAULT;
515                 goto out;
516         }
517
518         kbuf[count] = '\0';
519
520         tmp = strchr(kbuf, ' ');
521         if (!tmp) {
522                 rv = -EINVAL;
523                 goto out;
524         }
525         *tmp = '\0';
526         tmp++;
527
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;
543 }
544
545 static const struct file_operations ofdt_fops = {
546         .write = ofdt_write,
547         .llseek = noop_llseek,
548 };
549
550 /* create /proc/powerpc/ofdt write-only by root */
551 static int proc_ppc64_create_ofdt(void)
552 {
553         struct proc_dir_entry *ent;
554
555         if (!machine_is(pseries))
556                 return 0;
557
558         ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
559         if (ent)
560                 ent->size = 0;
561
562         return 0;
563 }
564 __initcall(proc_ppc64_create_ofdt);