Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / ppc64 / kernel / 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
19 #include <asm/prom.h>
20 #include <asm/pSeries_reconfig.h>
21 #include <asm/uaccess.h>
22
23
24
25 /*
26 * Routines for "runtime" addition and removal of device tree nodes.
27 */
28 #ifdef CONFIG_PROC_DEVICETREE
29 /*
30 * Add a node to /proc/device-tree.
31 */
32 static void add_node_proc_entries(struct device_node *np)
33 {
34 struct proc_dir_entry *ent;
35
36 ent = proc_mkdir(strrchr(np->full_name, '/') + 1, np->parent->pde);
37 if (ent)
38 proc_device_tree_add_node(np, ent);
39 }
40
41 static void remove_node_proc_entries(struct device_node *np)
42 {
43 struct property *pp = np->properties;
44 struct device_node *parent = np->parent;
45
46 while (pp) {
47 remove_proc_entry(pp->name, np->pde);
48 pp = pp->next;
49 }
50
51 /* Assuming that symlinks have the same parent directory as
52 * np->pde.
53 */
54 if (np->name_link)
55 remove_proc_entry(np->name_link->name, parent->pde);
56 if (np->addr_link)
57 remove_proc_entry(np->addr_link->name, parent->pde);
58 if (np->pde)
59 remove_proc_entry(np->pde->name, parent->pde);
60 }
61 #else /* !CONFIG_PROC_DEVICETREE */
62 static void add_node_proc_entries(struct device_node *np)
63 {
64 return;
65 }
66
67 static void remove_node_proc_entries(struct device_node *np)
68 {
69 return;
70 }
71 #endif /* CONFIG_PROC_DEVICETREE */
72
73 /**
74 * derive_parent - basically like dirname(1)
75 * @path: the full_name of a node to be added to the tree
76 *
77 * Returns the node which should be the parent of the node
78 * described by path. E.g., for path = "/foo/bar", returns
79 * the node with full_name = "/foo".
80 */
81 static struct device_node *derive_parent(const char *path)
82 {
83 struct device_node *parent = NULL;
84 char *parent_path = "/";
85 size_t parent_path_len = strrchr(path, '/') - path + 1;
86
87 /* reject if path is "/" */
88 if (!strcmp(path, "/"))
89 return ERR_PTR(-EINVAL);
90
91 if (strrchr(path, '/') != path) {
92 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
93 if (!parent_path)
94 return ERR_PTR(-ENOMEM);
95 strlcpy(parent_path, path, parent_path_len);
96 }
97 parent = of_find_node_by_path(parent_path);
98 if (!parent)
99 return ERR_PTR(-EINVAL);
100 if (strcmp(parent_path, "/"))
101 kfree(parent_path);
102 return parent;
103 }
104
105 static struct notifier_block *pSeries_reconfig_chain;
106
107 int pSeries_reconfig_notifier_register(struct notifier_block *nb)
108 {
109 return notifier_chain_register(&pSeries_reconfig_chain, nb);
110 }
111
112 void pSeries_reconfig_notifier_unregister(struct notifier_block *nb)
113 {
114 notifier_chain_unregister(&pSeries_reconfig_chain, nb);
115 }
116
117 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
118 {
119 struct device_node *np;
120 int err = -ENOMEM;
121
122 np = kcalloc(1, sizeof(*np), GFP_KERNEL);
123 if (!np)
124 goto out_err;
125
126 np->full_name = kmalloc(strlen(path) + 1, GFP_KERNEL);
127 if (!np->full_name)
128 goto out_err;
129
130 strcpy(np->full_name, path);
131
132 np->properties = proplist;
133 OF_MARK_DYNAMIC(np);
134 kref_init(&np->kref);
135
136 np->parent = derive_parent(path);
137 if (IS_ERR(np->parent)) {
138 err = PTR_ERR(np->parent);
139 goto out_err;
140 }
141
142 err = notifier_call_chain(&pSeries_reconfig_chain,
143 PSERIES_RECONFIG_ADD, np);
144 if (err == NOTIFY_BAD) {
145 printk(KERN_ERR "Failed to add device node %s\n", path);
146 err = -ENOMEM; /* For now, safe to assume kmalloc failure */
147 goto out_err;
148 }
149
150 of_attach_node(np);
151
152 add_node_proc_entries(np);
153
154 of_node_put(np->parent);
155
156 return 0;
157
158 out_err:
159 if (np) {
160 of_node_put(np->parent);
161 kfree(np->full_name);
162 kfree(np);
163 }
164 return err;
165 }
166
167 static int pSeries_reconfig_remove_node(struct device_node *np)
168 {
169 struct device_node *parent, *child;
170
171 parent = of_get_parent(np);
172 if (!parent)
173 return -EINVAL;
174
175 if ((child = of_get_next_child(np, NULL))) {
176 of_node_put(child);
177 return -EBUSY;
178 }
179
180 remove_node_proc_entries(np);
181
182 notifier_call_chain(&pSeries_reconfig_chain,
183 PSERIES_RECONFIG_REMOVE, np);
184 of_detach_node(np);
185
186 of_node_put(parent);
187 of_node_put(np); /* Must decrement the refcount */
188 return 0;
189 }
190
191 /*
192 * /proc/ppc64/ofdt - yucky binary interface for adding and removing
193 * OF device nodes. Should be deprecated as soon as we get an
194 * in-kernel wrapper for the RTAS ibm,configure-connector call.
195 */
196
197 static void release_prop_list(const struct property *prop)
198 {
199 struct property *next;
200 for (; prop; prop = next) {
201 next = prop->next;
202 kfree(prop->name);
203 kfree(prop->value);
204 kfree(prop);
205 }
206
207 }
208
209 /**
210 * parse_next_property - process the next property from raw input buffer
211 * @buf: input buffer, must be nul-terminated
212 * @end: end of the input buffer + 1, for validation
213 * @name: return value; set to property name in buf
214 * @length: return value; set to length of value
215 * @value: return value; set to the property value in buf
216 *
217 * Note that the caller must make copies of the name and value returned,
218 * this function does no allocation or copying of the data. Return value
219 * is set to the next name in buf, or NULL on error.
220 */
221 static char * parse_next_property(char *buf, char *end, char **name, int *length,
222 unsigned char **value)
223 {
224 char *tmp;
225
226 *name = buf;
227
228 tmp = strchr(buf, ' ');
229 if (!tmp) {
230 printk(KERN_ERR "property parse failed in %s at line %d\n",
231 __FUNCTION__, __LINE__);
232 return NULL;
233 }
234 *tmp = '\0';
235
236 if (++tmp >= end) {
237 printk(KERN_ERR "property parse failed in %s at line %d\n",
238 __FUNCTION__, __LINE__);
239 return NULL;
240 }
241
242 /* now we're on the length */
243 *length = -1;
244 *length = simple_strtoul(tmp, &tmp, 10);
245 if (*length == -1) {
246 printk(KERN_ERR "property parse failed in %s at line %d\n",
247 __FUNCTION__, __LINE__);
248 return NULL;
249 }
250 if (*tmp != ' ' || ++tmp >= end) {
251 printk(KERN_ERR "property parse failed in %s at line %d\n",
252 __FUNCTION__, __LINE__);
253 return NULL;
254 }
255
256 /* now we're on the value */
257 *value = tmp;
258 tmp += *length;
259 if (tmp > end) {
260 printk(KERN_ERR "property parse failed in %s at line %d\n",
261 __FUNCTION__, __LINE__);
262 return NULL;
263 }
264 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
265 printk(KERN_ERR "property parse failed in %s at line %d\n",
266 __FUNCTION__, __LINE__);
267 return NULL;
268 }
269 tmp++;
270
271 /* and now we should be on the next name, or the end */
272 return tmp;
273 }
274
275 static struct property *new_property(const char *name, const int length,
276 const unsigned char *value, struct property *last)
277 {
278 struct property *new = kmalloc(sizeof(*new), GFP_KERNEL);
279
280 if (!new)
281 return NULL;
282 memset(new, 0, sizeof(*new));
283
284 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
285 goto cleanup;
286 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
287 goto cleanup;
288
289 strcpy(new->name, name);
290 memcpy(new->value, value, length);
291 *(((char *)new->value) + length) = 0;
292 new->length = length;
293 new->next = last;
294 return new;
295
296 cleanup:
297 if (new->name)
298 kfree(new->name);
299 if (new->value)
300 kfree(new->value);
301 kfree(new);
302 return NULL;
303 }
304
305 static int do_add_node(char *buf, size_t bufsize)
306 {
307 char *path, *end, *name;
308 struct device_node *np;
309 struct property *prop = NULL;
310 unsigned char* value;
311 int length, rv = 0;
312
313 end = buf + bufsize;
314 path = buf;
315 buf = strchr(buf, ' ');
316 if (!buf)
317 return -EINVAL;
318 *buf = '\0';
319 buf++;
320
321 if ((np = of_find_node_by_path(path))) {
322 of_node_put(np);
323 return -EINVAL;
324 }
325
326 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
327 while (buf < end &&
328 (buf = parse_next_property(buf, end, &name, &length, &value))) {
329 struct property *last = prop;
330
331 prop = new_property(name, length, value, last);
332 if (!prop) {
333 rv = -ENOMEM;
334 prop = last;
335 goto out;
336 }
337 }
338 if (!buf) {
339 rv = -EINVAL;
340 goto out;
341 }
342
343 rv = pSeries_reconfig_add_node(path, prop);
344
345 out:
346 if (rv)
347 release_prop_list(prop);
348 return rv;
349 }
350
351 static int do_remove_node(char *buf)
352 {
353 struct device_node *node;
354 int rv = -ENODEV;
355
356 if ((node = of_find_node_by_path(buf)))
357 rv = pSeries_reconfig_remove_node(node);
358
359 of_node_put(node);
360 return rv;
361 }
362
363 /**
364 * ofdt_write - perform operations on the Open Firmware device tree
365 *
366 * @file: not used
367 * @buf: command and arguments
368 * @count: size of the command buffer
369 * @off: not used
370 *
371 * Operations supported at this time are addition and removal of
372 * whole nodes along with their properties. Operations on individual
373 * properties are not implemented (yet).
374 */
375 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
376 loff_t *off)
377 {
378 int rv = 0;
379 char *kbuf;
380 char *tmp;
381
382 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
383 rv = -ENOMEM;
384 goto out;
385 }
386 if (copy_from_user(kbuf, buf, count)) {
387 rv = -EFAULT;
388 goto out;
389 }
390
391 kbuf[count] = '\0';
392
393 tmp = strchr(kbuf, ' ');
394 if (!tmp) {
395 rv = -EINVAL;
396 goto out;
397 }
398 *tmp = '\0';
399 tmp++;
400
401 if (!strcmp(kbuf, "add_node"))
402 rv = do_add_node(tmp, count - (tmp - kbuf));
403 else if (!strcmp(kbuf, "remove_node"))
404 rv = do_remove_node(tmp);
405 else
406 rv = -EINVAL;
407 out:
408 kfree(kbuf);
409 return rv ? rv : count;
410 }
411
412 static struct file_operations ofdt_fops = {
413 .write = ofdt_write
414 };
415
416 /* create /proc/ppc64/ofdt write-only by root */
417 static int proc_ppc64_create_ofdt(void)
418 {
419 struct proc_dir_entry *ent;
420
421 if (!(systemcfg->platform & PLATFORM_PSERIES))
422 return 0;
423
424 ent = create_proc_entry("ppc64/ofdt", S_IWUSR, NULL);
425 if (ent) {
426 ent->nlink = 1;
427 ent->data = NULL;
428 ent->size = 0;
429 ent->proc_fops = &ofdt_fops;
430 }
431
432 return 0;
433 }
434 __initcall(proc_ppc64_create_ofdt);