blob: 7c7fcc04254948837a3a747ef83fd6cdd6babb3b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/notifier.h>
16#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000018#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <asm/prom.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110021#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
Brian King46db2f82009-08-28 12:06:29 +000023#include <asm/mmu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Andy Shevchenko948ad1a2015-10-01 12:46:06 +030025#include "of_helpers.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
28{
29 struct device_node *np;
30 int err = -ENOMEM;
31
Pekka Enberg874ca6c2005-09-06 15:18:32 -070032 np = kzalloc(sizeof(*np), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (!np)
34 goto out_err;
35
Julia Lawall74052172010-05-14 09:30:13 +000036 np->full_name = kstrdup(path, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 if (!np->full_name)
38 goto out_err;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 np->properties = proplist;
Michael Ellermand3b814b2007-06-19 16:07:58 +100041 of_node_set_flag(np, OF_DYNAMIC);
Tyrel Datwyler97a9a712014-07-10 14:50:57 -040042 of_node_init(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andy Shevchenko948ad1a2015-10-01 12:46:06 +030044 np->parent = pseries_of_derive_parent(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (IS_ERR(np->parent)) {
46 err = PTR_ERR(np->parent);
47 goto out_err;
48 }
49
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000050 err = of_attach_node(np);
Akinobu Mita3aef19f2011-06-21 03:35:55 +000051 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 printk(KERN_ERR "Failed to add device node %s\n", path);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 goto out_err;
54 }
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 of_node_put(np->parent);
57
58 return 0;
59
60out_err:
61 if (np) {
62 of_node_put(np->parent);
63 kfree(np->full_name);
64 kfree(np);
65 }
66 return err;
67}
68
69static int pSeries_reconfig_remove_node(struct device_node *np)
70{
71 struct device_node *parent, *child;
72
73 parent = of_get_parent(np);
74 if (!parent)
75 return -EINVAL;
76
77 if ((child = of_get_next_child(np, NULL))) {
78 of_node_put(child);
Julia Lawall842decb2008-02-04 23:34:58 -080079 of_node_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EBUSY;
81 }
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 of_detach_node(np);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 of_node_put(parent);
85 of_node_put(np); /* Must decrement the refcount */
86 return 0;
87}
88
89/*
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +000090 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * OF device nodes. Should be deprecated as soon as we get an
92 * in-kernel wrapper for the RTAS ibm,configure-connector call.
93 */
94
95static void release_prop_list(const struct property *prop)
96{
97 struct property *next;
98 for (; prop; prop = next) {
99 next = prop->next;
100 kfree(prop->name);
101 kfree(prop->value);
102 kfree(prop);
103 }
104
105}
106
107/**
108 * parse_next_property - process the next property from raw input buffer
109 * @buf: input buffer, must be nul-terminated
110 * @end: end of the input buffer + 1, for validation
111 * @name: return value; set to property name in buf
112 * @length: return value; set to length of value
113 * @value: return value; set to the property value in buf
114 *
115 * Note that the caller must make copies of the name and value returned,
116 * this function does no allocation or copying of the data. Return value
117 * is set to the next name in buf, or NULL on error.
118 */
119static char * parse_next_property(char *buf, char *end, char **name, int *length,
120 unsigned char **value)
121{
122 char *tmp;
123
124 *name = buf;
125
126 tmp = strchr(buf, ' ');
127 if (!tmp) {
128 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100129 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return NULL;
131 }
132 *tmp = '\0';
133
134 if (++tmp >= end) {
135 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100136 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return NULL;
138 }
139
140 /* now we're on the length */
141 *length = -1;
142 *length = simple_strtoul(tmp, &tmp, 10);
143 if (*length == -1) {
144 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100145 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return NULL;
147 }
148 if (*tmp != ' ' || ++tmp >= end) {
149 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100150 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return NULL;
152 }
153
154 /* now we're on the value */
155 *value = tmp;
156 tmp += *length;
157 if (tmp > end) {
158 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100159 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return NULL;
161 }
162 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
163 printk(KERN_ERR "property parse failed in %s at line %d\n",
Harvey Harrisone48b1b42008-03-29 08:21:07 +1100164 __func__, __LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return NULL;
166 }
167 tmp++;
168
169 /* and now we should be on the next name, or the end */
170 return tmp;
171}
172
173static struct property *new_property(const char *name, const int length,
174 const unsigned char *value, struct property *last)
175{
Yan Burmanf8485352006-12-02 13:26:57 +0200176 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (!new)
179 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Grant Likelyc22618a2012-11-14 22:37:12 +0000181 if (!(new->name = kstrdup(name, GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto cleanup;
183 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
184 goto cleanup;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 memcpy(new->value, value, length);
187 *(((char *)new->value) + length) = 0;
188 new->length = length;
189 new->next = last;
190 return new;
191
192cleanup:
Jesper Juhlb2325fe2005-11-07 01:01:35 -0800193 kfree(new->name);
194 kfree(new->value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree(new);
196 return NULL;
197}
198
199static int do_add_node(char *buf, size_t bufsize)
200{
201 char *path, *end, *name;
202 struct device_node *np;
203 struct property *prop = NULL;
204 unsigned char* value;
205 int length, rv = 0;
206
207 end = buf + bufsize;
208 path = buf;
209 buf = strchr(buf, ' ');
210 if (!buf)
211 return -EINVAL;
212 *buf = '\0';
213 buf++;
214
215 if ((np = of_find_node_by_path(path))) {
216 of_node_put(np);
217 return -EINVAL;
218 }
219
220 /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
221 while (buf < end &&
222 (buf = parse_next_property(buf, end, &name, &length, &value))) {
223 struct property *last = prop;
224
225 prop = new_property(name, length, value, last);
226 if (!prop) {
227 rv = -ENOMEM;
228 prop = last;
229 goto out;
230 }
231 }
232 if (!buf) {
233 rv = -EINVAL;
234 goto out;
235 }
236
237 rv = pSeries_reconfig_add_node(path, prop);
238
239out:
240 if (rv)
241 release_prop_list(prop);
242 return rv;
243}
244
245static int do_remove_node(char *buf)
246{
247 struct device_node *node;
248 int rv = -ENODEV;
249
250 if ((node = of_find_node_by_path(buf)))
251 rv = pSeries_reconfig_remove_node(node);
252
253 of_node_put(node);
254 return rv;
255}
256
Dave C Boutcher610d9152006-01-12 16:10:22 -0600257static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
258{
259 char *handle_str;
260 phandle handle;
261 *npp = NULL;
262
263 handle_str = buf;
264
265 buf = strchr(buf, ' ');
266 if (!buf)
267 return NULL;
268 *buf = '\0';
269 buf++;
270
Nathan Fontenot4b6e8052008-07-03 13:19:24 +1000271 handle = simple_strtoul(handle_str, NULL, 0);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600272
273 *npp = of_find_node_by_phandle(handle);
274 return buf;
275}
276
277static int do_add_property(char *buf, size_t bufsize)
278{
279 struct property *prop = NULL;
280 struct device_node *np;
281 unsigned char *value;
282 char *name, *end;
283 int length;
284 end = buf + bufsize;
285 buf = parse_node(buf, bufsize, &np);
286
287 if (!np)
288 return -ENODEV;
289
290 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
291 return -EINVAL;
292
293 prop = new_property(name, length, value, NULL);
294 if (!prop)
295 return -ENOMEM;
296
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000297 of_add_property(np, prop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600298
299 return 0;
300}
301
302static int do_remove_property(char *buf, size_t bufsize)
303{
304 struct device_node *np;
305 char *tmp;
306 struct property *prop;
307 buf = parse_node(buf, bufsize, &np);
308
309 if (!np)
310 return -ENODEV;
311
312 tmp = strchr(buf,' ');
313 if (tmp)
314 *tmp = '\0';
315
316 if (strlen(buf) == 0)
317 return -EINVAL;
318
319 prop = of_find_property(np, buf, NULL);
320
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000321 return of_remove_property(np, prop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600322}
323
324static int do_update_property(char *buf, size_t bufsize)
325{
326 struct device_node *np;
327 unsigned char *value;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000328 char *name, *end, *next_prop;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000329 int length;
Dong Aisheng475d0092012-07-11 15:16:37 +1000330 struct property *newprop;
Dave C Boutcher610d9152006-01-12 16:10:22 -0600331 buf = parse_node(buf, bufsize, &np);
332 end = buf + bufsize;
333
334 if (!np)
335 return -ENODEV;
336
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000337 next_prop = parse_next_property(buf, end, &name, &length, &value);
338 if (!next_prop)
Dave C Boutcher610d9152006-01-12 16:10:22 -0600339 return -EINVAL;
340
Dong Aisheng475d0092012-07-11 15:16:37 +1000341 if (!strlen(name))
342 return -ENODEV;
343
Dave C Boutcher610d9152006-01-12 16:10:22 -0600344 newprop = new_property(name, length, value, NULL);
345 if (!newprop)
346 return -ENOMEM;
347
Brian King46db2f82009-08-28 12:06:29 +0000348 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
349 slb_set_size(*(int *)value);
350
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000351 return of_update_property(np, newprop);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/**
355 * ofdt_write - perform operations on the Open Firmware device tree
356 *
357 * @file: not used
358 * @buf: command and arguments
359 * @count: size of the command buffer
360 * @off: not used
361 *
362 * Operations supported at this time are addition and removal of
363 * whole nodes along with their properties. Operations on individual
364 * properties are not implemented (yet).
365 */
366static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
367 loff_t *off)
368{
369 int rv = 0;
370 char *kbuf;
371 char *tmp;
372
373 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
374 rv = -ENOMEM;
375 goto out;
376 }
377 if (copy_from_user(kbuf, buf, count)) {
378 rv = -EFAULT;
379 goto out;
380 }
381
382 kbuf[count] = '\0';
383
384 tmp = strchr(kbuf, ' ');
385 if (!tmp) {
386 rv = -EINVAL;
387 goto out;
388 }
389 *tmp = '\0';
390 tmp++;
391
392 if (!strcmp(kbuf, "add_node"))
393 rv = do_add_node(tmp, count - (tmp - kbuf));
394 else if (!strcmp(kbuf, "remove_node"))
395 rv = do_remove_node(tmp);
Dave C Boutcher610d9152006-01-12 16:10:22 -0600396 else if (!strcmp(kbuf, "add_property"))
397 rv = do_add_property(tmp, count - (tmp - kbuf));
398 else if (!strcmp(kbuf, "remove_property"))
399 rv = do_remove_property(tmp, count - (tmp - kbuf));
400 else if (!strcmp(kbuf, "update_property"))
401 rv = do_update_property(tmp, count - (tmp - kbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 else
403 rv = -EINVAL;
404out:
405 kfree(kbuf);
406 return rv ? rv : count;
407}
408
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800409static const struct file_operations ofdt_fops = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200410 .write = ofdt_write,
411 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000414/* create /proc/powerpc/ofdt write-only by root */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static int proc_ppc64_create_ofdt(void)
416{
417 struct proc_dir_entry *ent;
418
Benjamin Herrenschmidt188917e2009-09-24 19:29:13 +0000419 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
Denis V. Lunev66747132008-04-29 01:02:26 -0700420 if (ent)
David Howells271a15e2013-04-12 00:38:51 +0100421 proc_set_size(ent, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return 0;
424}
Michael Ellerman8e83e902014-07-16 12:02:43 +1000425machine_device_initcall(pseries, proc_ppc64_create_ofdt);