blob: 30b358dc2bebdac232ced9966daadf7b969fd1c6 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000019#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110022#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
Brian King46db2f82009-08-28 12:06:29 +000024#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/**
27 * derive_parent - basically like dirname(1)
28 * @path: the full_name of a node to be added to the tree
29 *
30 * Returns the node which should be the parent of the node
31 * described by path. E.g., for path = "/foo/bar", returns
32 * the node with full_name = "/foo".
33 */
34static struct device_node *derive_parent(const char *path)
35{
36 struct device_node *parent = NULL;
37 char *parent_path = "/";
38 size_t parent_path_len = strrchr(path, '/') - path + 1;
39
40 /* reject if path is "/" */
41 if (!strcmp(path, "/"))
42 return ERR_PTR(-EINVAL);
43
44 if (strrchr(path, '/') != path) {
45 parent_path = kmalloc(parent_path_len, GFP_KERNEL);
46 if (!parent_path)
47 return ERR_PTR(-ENOMEM);
48 strlcpy(parent_path, path, parent_path_len);
49 }
50 parent = of_find_node_by_path(parent_path);
51 if (!parent)
52 return ERR_PTR(-EINVAL);
53 if (strcmp(parent_path, "/"))
54 kfree(parent_path);
55 return parent;
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
59{
60 struct device_node *np;
61 int err = -ENOMEM;
62
Pekka Enberg874ca6c2005-09-06 15:18:32 -070063 np = kzalloc(sizeof(*np), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!np)
65 goto out_err;
66
Julia Lawall74052172010-05-14 09:30:13 +000067 np->full_name = kstrdup(path, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!np->full_name)
69 goto out_err;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 np->properties = proplist;
Michael Ellermand3b814b2007-06-19 16:07:58 +100072 of_node_set_flag(np, OF_DYNAMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 kref_init(&np->kref);
74
75 np->parent = derive_parent(path);
76 if (IS_ERR(np->parent)) {
77 err = PTR_ERR(np->parent);
78 goto out_err;
79 }
80
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000081 err = of_attach_node(np);
Akinobu Mita3aef19f2011-06-21 03:35:55 +000082 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 printk(KERN_ERR "Failed to add device node %s\n", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 goto out_err;
85 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 of_node_put(np->parent);
88
89 return 0;
90
91out_err:
92 if (np) {
93 of_node_put(np->parent);
94 kfree(np->full_name);
95 kfree(np);
96 }
97 return err;
98}
99
100static int pSeries_reconfig_remove_node(struct device_node *np)
101{
102 struct device_node *parent, *child;
103
104 parent = of_get_parent(np);
105 if (!parent)
106 return -EINVAL;
107
108 if ((child = of_get_next_child(np, NULL))) {
109 of_node_put(child);
Julia Lawall842decb2008-02-04 23:34:58 -0800110 of_node_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EBUSY;
112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 of_detach_node(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 of_node_put(parent);
116 of_node_put(np); /* Must decrement the refcount */
117 return 0;
118}
119
120/*
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000121 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * OF device nodes. Should be deprecated as soon as we get an
123 * in-kernel wrapper for the RTAS ibm,configure-connector call.
124 */
125
126static void release_prop_list(const struct property *prop)
127{
128 struct property *next;
129 for (; prop; prop = next) {
130 next = prop->next;
131 kfree(prop->name);
132 kfree(prop->value);
133 kfree(prop);
134 }
135
136}
137
138/**
139 * parse_next_property - process the next property from raw input buffer
140 * @buf: input buffer, must be nul-terminated
141 * @end: end of the input buffer + 1, for validation
142 * @name: return value; set to property name in buf
143 * @length: return value; set to length of value
144 * @value: return value; set to the property value in buf
145 *
146 * Note that the caller must make copies of the name and value returned,
147 * this function does no allocation or copying of the data. Return value
148 * is set to the next name in buf, or NULL on error.
149 */
150static char * parse_next_property(char *buf, char *end, char **name, int *length,
151 unsigned char **value)
152{
153 char *tmp;
154
155 *name = buf;
156
157 tmp = strchr(buf, ' ');
158 if (!tmp) {
159 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100160 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return NULL;
162 }
163 *tmp = '\0';
164
165 if (++tmp >= end) {
166 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100167 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169 }
170
171 /* now we're on the length */
172 *length = -1;
173 *length = simple_strtoul(tmp, &tmp, 10);
174 if (*length == -1) {
175 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100176 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return NULL;
178 }
179 if (*tmp != ' ' || ++tmp >= end) {
180 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100181 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return NULL;
183 }
184
185 /* now we're on the value */
186 *value = tmp;
187 tmp += *length;
188 if (tmp > end) {
189 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100190 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return NULL;
192 }
193 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
194 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100195 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return NULL;
197 }
198 tmp++;
199
200 /* and now we should be on the next name, or the end */
201 return tmp;
202}
203
204static struct property *new_property(const char *name, const int length,
205 const unsigned char *value, struct property *last)
206{
Yan Burmanf8485352006-12-02 13:26:57 +0200207 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!new)
210 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
213 goto cleanup;
214 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
215 goto cleanup;
216
217 strcpy(new->name, name);
218 memcpy(new->value, value, length);
219 *(((char *)new->value) + length) = 0;
220 new->length = length;
221 new->next = last;
222 return new;
223
224cleanup:
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800225 kfree(new->name);
226 kfree(new->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 kfree(new);
228 return NULL;
229}
230
231static int do_add_node(char *buf, size_t bufsize)
232{
233 char *path, *end, *name;
234 struct device_node *np;
235 struct property *prop = NULL;
236 unsigned char* value;
237 int length, rv = 0;
238
239 end = buf + bufsize;
240 path = buf;
241 buf = strchr(buf, ' ');
242 if (!buf)
243 return -EINVAL;
244 *buf = '\0';
245 buf++;
246
247 if ((np = of_find_node_by_path(path))) {
248 of_node_put(np);
249 return -EINVAL;
250 }
251
252 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
253 while (buf < end &&
254 (buf = parse_next_property(buf, end, &name, &length, &value))) {
255 struct property *last = prop;
256
257 prop = new_property(name, length, value, last);
258 if (!prop) {
259 rv = -ENOMEM;
260 prop = last;
261 goto out;
262 }
263 }
264 if (!buf) {
265 rv = -EINVAL;
266 goto out;
267 }
268
269 rv = pSeries_reconfig_add_node(path, prop);
270
271out:
272 if (rv)
273 release_prop_list(prop);
274 return rv;
275}
276
277static int do_remove_node(char *buf)
278{
279 struct device_node *node;
280 int rv = -ENODEV;
281
282 if ((node = of_find_node_by_path(buf)))
283 rv = pSeries_reconfig_remove_node(node);
284
285 of_node_put(node);
286 return rv;
287}
288
Dave C Boutcher610d9152006-01-12 16:10:22 -0600289static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
290{
291 char *handle_str;
292 phandle handle;
293 *npp = NULL;
294
295 handle_str = buf;
296
297 buf = strchr(buf, ' ');
298 if (!buf)
299 return NULL;
300 *buf = '\0';
301 buf++;
302
Nathan Fontenot4b6e8052008-07-03 13:19:24 +1000303 handle = simple_strtoul(handle_str, NULL, 0);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600304
305 *npp = of_find_node_by_phandle(handle);
306 return buf;
307}
308
309static int do_add_property(char *buf, size_t bufsize)
310{
311 struct property *prop = NULL;
312 struct device_node *np;
313 unsigned char *value;
314 char *name, *end;
315 int length;
316 end = buf + bufsize;
317 buf = parse_node(buf, bufsize, &np);
318
319 if (!np)
320 return -ENODEV;
321
322 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
323 return -EINVAL;
324
325 prop = new_property(name, length, value, NULL);
326 if (!prop)
327 return -ENOMEM;
328
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000329 of_add_property(np, prop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600330
331 return 0;
332}
333
334static int do_remove_property(char *buf, size_t bufsize)
335{
336 struct device_node *np;
337 char *tmp;
338 struct property *prop;
339 buf = parse_node(buf, bufsize, &np);
340
341 if (!np)
342 return -ENODEV;
343
344 tmp = strchr(buf,' ');
345 if (tmp)
346 *tmp = '\0';
347
348 if (strlen(buf) == 0)
349 return -EINVAL;
350
351 prop = of_find_property(np, buf, NULL);
352
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000353 return of_remove_property(np, prop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600354}
355
356static int do_update_property(char *buf, size_t bufsize)
357{
358 struct device_node *np;
359 unsigned char *value;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000360 char *name, *end, *next_prop;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000361 int length;
Dong Aisheng475d0092012-07-11 15:16:37 +1000362 struct property *newprop;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600363 buf = parse_node(buf, bufsize, &np);
364 end = buf + bufsize;
365
366 if (!np)
367 return -ENODEV;
368
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000369 next_prop = parse_next_property(buf, end, &name, &length, &value);
370 if (!next_prop)
Dave C Boutcher610d9152006-01-12 16:10:22 -0600371 return -EINVAL;
372
Dong Aisheng475d0092012-07-11 15:16:37 +1000373 if (!strlen(name))
374 return -ENODEV;
375
Dave C Boutcher610d9152006-01-12 16:10:22 -0600376 newprop = new_property(name, length, value, NULL);
377 if (!newprop)
378 return -ENOMEM;
379
Brian King46db2f82009-08-28 12:06:29 +0000380 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
381 slb_set_size(*(int *)value);
382
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000383 return of_update_property(np, newprop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * ofdt_write - perform operations on the Open Firmware device tree
388 *
389 * @file: not used
390 * @buf: command and arguments
391 * @count: size of the command buffer
392 * @off: not used
393 *
394 * Operations supported at this time are addition and removal of
395 * whole nodes along with their properties. Operations on individual
396 * properties are not implemented (yet).
397 */
398static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
399 loff_t *off)
400{
401 int rv = 0;
402 char *kbuf;
403 char *tmp;
404
405 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
406 rv = -ENOMEM;
407 goto out;
408 }
409 if (copy_from_user(kbuf, buf, count)) {
410 rv = -EFAULT;
411 goto out;
412 }
413
414 kbuf[count] = '\0';
415
416 tmp = strchr(kbuf, ' ');
417 if (!tmp) {
418 rv = -EINVAL;
419 goto out;
420 }
421 *tmp = '\0';
422 tmp++;
423
424 if (!strcmp(kbuf, "add_node"))
425 rv = do_add_node(tmp, count - (tmp - kbuf));
426 else if (!strcmp(kbuf, "remove_node"))
427 rv = do_remove_node(tmp);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600428 else if (!strcmp(kbuf, "add_property"))
429 rv = do_add_property(tmp, count - (tmp - kbuf));
430 else if (!strcmp(kbuf, "remove_property"))
431 rv = do_remove_property(tmp, count - (tmp - kbuf));
432 else if (!strcmp(kbuf, "update_property"))
433 rv = do_update_property(tmp, count - (tmp - kbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 else
435 rv = -EINVAL;
436out:
437 kfree(kbuf);
438 return rv ? rv : count;
439}
440
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800441static const struct file_operations ofdt_fops = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200442 .write = ofdt_write,
443 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444};
445
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000446/* create /proc/powerpc/ofdt write-only by root */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int proc_ppc64_create_ofdt(void)
448{
449 struct proc_dir_entry *ent;
450
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100451 if (!machine_is(pseries))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000454 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
Denis V. Lunev66747132008-04-29 01:02:26 -0700455 if (ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ent->size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return 0;
459}
460__initcall(proc_ppc64_create_ofdt);