of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / powerpc / platforms / pseries / reconfig.c
blob7c7fcc04254948837a3a747ef83fd6cdd6babb3b
1 /*
2 * pSeries_reconfig.c - support for dynamic reconfiguration (including PCI
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms).
5 * Copyright (C) 2005 Nathan Lynch
6 * Copyright (C) 2005 IBM Corporation
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.
14 #include <linux/kernel.h>
15 #include <linux/notifier.h>
16 #include <linux/proc_fs.h>
17 #include <linux/slab.h>
18 #include <linux/of.h>
20 #include <asm/prom.h>
21 #include <asm/machdep.h>
22 #include <asm/uaccess.h>
23 #include <asm/mmu.h>
25 #include "of_helpers.h"
27 static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
29 struct device_node *np;
30 int err = -ENOMEM;
32 np = kzalloc(sizeof(*np), GFP_KERNEL);
33 if (!np)
34 goto out_err;
36 np->full_name = kstrdup(path, GFP_KERNEL);
37 if (!np->full_name)
38 goto out_err;
40 np->properties = proplist;
41 of_node_set_flag(np, OF_DYNAMIC);
42 of_node_init(np);
44 np->parent = pseries_of_derive_parent(path);
45 if (IS_ERR(np->parent)) {
46 err = PTR_ERR(np->parent);
47 goto out_err;
50 err = of_attach_node(np);
51 if (err) {
52 printk(KERN_ERR "Failed to add device node %s\n", path);
53 goto out_err;
56 of_node_put(np->parent);
58 return 0;
60 out_err:
61 if (np) {
62 of_node_put(np->parent);
63 kfree(np->full_name);
64 kfree(np);
66 return err;
69 static int pSeries_reconfig_remove_node(struct device_node *np)
71 struct device_node *parent, *child;
73 parent = of_get_parent(np);
74 if (!parent)
75 return -EINVAL;
77 if ((child = of_get_next_child(np, NULL))) {
78 of_node_put(child);
79 of_node_put(parent);
80 return -EBUSY;
83 of_detach_node(np);
84 of_node_put(parent);
85 of_node_put(np); /* Must decrement the refcount */
86 return 0;
90 * /proc/powerpc/ofdt - yucky binary interface for adding and removing
91 * OF device nodes. Should be deprecated as soon as we get an
92 * in-kernel wrapper for the RTAS ibm,configure-connector call.
95 static void release_prop_list(const struct property *prop)
97 struct property *next;
98 for (; prop; prop = next) {
99 next = prop->next;
100 kfree(prop->name);
101 kfree(prop->value);
102 kfree(prop);
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
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.
119 static char * parse_next_property(char *buf, char *end, char **name, int *length,
120 unsigned char **value)
122 char *tmp;
124 *name = buf;
126 tmp = strchr(buf, ' ');
127 if (!tmp) {
128 printk(KERN_ERR "property parse failed in %s at line %d\n",
129 __func__, __LINE__);
130 return NULL;
132 *tmp = '\0';
134 if (++tmp >= end) {
135 printk(KERN_ERR "property parse failed in %s at line %d\n",
136 __func__, __LINE__);
137 return NULL;
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",
145 __func__, __LINE__);
146 return NULL;
148 if (*tmp != ' ' || ++tmp >= end) {
149 printk(KERN_ERR "property parse failed in %s at line %d\n",
150 __func__, __LINE__);
151 return NULL;
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",
159 __func__, __LINE__);
160 return NULL;
162 else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
163 printk(KERN_ERR "property parse failed in %s at line %d\n",
164 __func__, __LINE__);
165 return NULL;
167 tmp++;
169 /* and now we should be on the next name, or the end */
170 return tmp;
173 static struct property *new_property(const char *name, const int length,
174 const unsigned char *value, struct property *last)
176 struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
178 if (!new)
179 return NULL;
181 if (!(new->name = kstrdup(name, GFP_KERNEL)))
182 goto cleanup;
183 if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
184 goto cleanup;
186 memcpy(new->value, value, length);
187 *(((char *)new->value) + length) = 0;
188 new->length = length;
189 new->next = last;
190 return new;
192 cleanup:
193 kfree(new->name);
194 kfree(new->value);
195 kfree(new);
196 return NULL;
199 static int do_add_node(char *buf, size_t bufsize)
201 char *path, *end, *name;
202 struct device_node *np;
203 struct property *prop = NULL;
204 unsigned char* value;
205 int length, rv = 0;
207 end = buf + bufsize;
208 path = buf;
209 buf = strchr(buf, ' ');
210 if (!buf)
211 return -EINVAL;
212 *buf = '\0';
213 buf++;
215 if ((np = of_find_node_by_path(path))) {
216 of_node_put(np);
217 return -EINVAL;
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;
225 prop = new_property(name, length, value, last);
226 if (!prop) {
227 rv = -ENOMEM;
228 prop = last;
229 goto out;
232 if (!buf) {
233 rv = -EINVAL;
234 goto out;
237 rv = pSeries_reconfig_add_node(path, prop);
239 out:
240 if (rv)
241 release_prop_list(prop);
242 return rv;
245 static int do_remove_node(char *buf)
247 struct device_node *node;
248 int rv = -ENODEV;
250 if ((node = of_find_node_by_path(buf)))
251 rv = pSeries_reconfig_remove_node(node);
253 of_node_put(node);
254 return rv;
257 static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
259 char *handle_str;
260 phandle handle;
261 *npp = NULL;
263 handle_str = buf;
265 buf = strchr(buf, ' ');
266 if (!buf)
267 return NULL;
268 *buf = '\0';
269 buf++;
271 handle = simple_strtoul(handle_str, NULL, 0);
273 *npp = of_find_node_by_phandle(handle);
274 return buf;
277 static int do_add_property(char *buf, size_t bufsize)
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);
287 if (!np)
288 return -ENODEV;
290 if (parse_next_property(buf, end, &name, &length, &value) == NULL)
291 return -EINVAL;
293 prop = new_property(name, length, value, NULL);
294 if (!prop)
295 return -ENOMEM;
297 of_add_property(np, prop);
299 return 0;
302 static int do_remove_property(char *buf, size_t bufsize)
304 struct device_node *np;
305 char *tmp;
306 struct property *prop;
307 buf = parse_node(buf, bufsize, &np);
309 if (!np)
310 return -ENODEV;
312 tmp = strchr(buf,' ');
313 if (tmp)
314 *tmp = '\0';
316 if (strlen(buf) == 0)
317 return -EINVAL;
319 prop = of_find_property(np, buf, NULL);
321 return of_remove_property(np, prop);
324 static int do_update_property(char *buf, size_t bufsize)
326 struct device_node *np;
327 unsigned char *value;
328 char *name, *end, *next_prop;
329 int length;
330 struct property *newprop;
331 buf = parse_node(buf, bufsize, &np);
332 end = buf + bufsize;
334 if (!np)
335 return -ENODEV;
337 next_prop = parse_next_property(buf, end, &name, &length, &value);
338 if (!next_prop)
339 return -EINVAL;
341 if (!strlen(name))
342 return -ENODEV;
344 newprop = new_property(name, length, value, NULL);
345 if (!newprop)
346 return -ENOMEM;
348 if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
349 slb_set_size(*(int *)value);
351 return of_update_property(np, newprop);
355 * ofdt_write - perform operations on the Open Firmware device tree
357 * @file: not used
358 * @buf: command and arguments
359 * @count: size of the command buffer
360 * @off: not used
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).
366 static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
367 loff_t *off)
369 int rv = 0;
370 char *kbuf;
371 char *tmp;
373 if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
374 rv = -ENOMEM;
375 goto out;
377 if (copy_from_user(kbuf, buf, count)) {
378 rv = -EFAULT;
379 goto out;
382 kbuf[count] = '\0';
384 tmp = strchr(kbuf, ' ');
385 if (!tmp) {
386 rv = -EINVAL;
387 goto out;
389 *tmp = '\0';
390 tmp++;
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);
396 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));
402 else
403 rv = -EINVAL;
404 out:
405 kfree(kbuf);
406 return rv ? rv : count;
409 static const struct file_operations ofdt_fops = {
410 .write = ofdt_write,
411 .llseek = noop_llseek,
414 /* create /proc/powerpc/ofdt write-only by root */
415 static int proc_ppc64_create_ofdt(void)
417 struct proc_dir_entry *ent;
419 ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
420 if (ent)
421 proc_set_size(ent, 0);
423 return 0;
425 machine_device_initcall(pseries, proc_ppc64_create_ofdt);