reconfig.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #include <linux/kernel.h>
  14. #include <linux/notifier.h>
  15. #include <linux/proc_fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <asm/prom.h>
  19. #include <asm/machdep.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/mmu.h>
  22. #include "of_helpers.h"
  23. static int pSeries_reconfig_add_node(const char *path, struct property *proplist)
  24. {
  25. struct device_node *np;
  26. int err = -ENOMEM;
  27. np = kzalloc(sizeof(*np), GFP_KERNEL);
  28. if (!np)
  29. goto out_err;
  30. np->full_name = kstrdup(path, GFP_KERNEL);
  31. if (!np->full_name)
  32. goto out_err;
  33. np->properties = proplist;
  34. of_node_set_flag(np, OF_DYNAMIC);
  35. of_node_init(np);
  36. np->parent = pseries_of_derive_parent(path);
  37. if (IS_ERR(np->parent)) {
  38. err = PTR_ERR(np->parent);
  39. goto out_err;
  40. }
  41. err = of_attach_node(np);
  42. if (err) {
  43. printk(KERN_ERR "Failed to add device node %s\n", path);
  44. goto out_err;
  45. }
  46. of_node_put(np->parent);
  47. return 0;
  48. out_err:
  49. if (np) {
  50. of_node_put(np->parent);
  51. kfree(np->full_name);
  52. kfree(np);
  53. }
  54. return err;
  55. }
  56. static int pSeries_reconfig_remove_node(struct device_node *np)
  57. {
  58. struct device_node *parent, *child;
  59. parent = of_get_parent(np);
  60. if (!parent)
  61. return -EINVAL;
  62. if ((child = of_get_next_child(np, NULL))) {
  63. of_node_put(child);
  64. of_node_put(parent);
  65. return -EBUSY;
  66. }
  67. of_detach_node(np);
  68. of_node_put(parent);
  69. return 0;
  70. }
  71. /*
  72. * /proc/powerpc/ofdt - yucky binary interface for adding and removing
  73. * OF device nodes. Should be deprecated as soon as we get an
  74. * in-kernel wrapper for the RTAS ibm,configure-connector call.
  75. */
  76. static void release_prop_list(const struct property *prop)
  77. {
  78. struct property *next;
  79. for (; prop; prop = next) {
  80. next = prop->next;
  81. kfree(prop->name);
  82. kfree(prop->value);
  83. kfree(prop);
  84. }
  85. }
  86. /**
  87. * parse_next_property - process the next property from raw input buffer
  88. * @buf: input buffer, must be nul-terminated
  89. * @end: end of the input buffer + 1, for validation
  90. * @name: return value; set to property name in buf
  91. * @length: return value; set to length of value
  92. * @value: return value; set to the property value in buf
  93. *
  94. * Note that the caller must make copies of the name and value returned,
  95. * this function does no allocation or copying of the data. Return value
  96. * is set to the next name in buf, or NULL on error.
  97. */
  98. static char * parse_next_property(char *buf, char *end, char **name, int *length,
  99. unsigned char **value)
  100. {
  101. char *tmp;
  102. *name = buf;
  103. tmp = strchr(buf, ' ');
  104. if (!tmp) {
  105. printk(KERN_ERR "property parse failed in %s at line %d\n",
  106. __func__, __LINE__);
  107. return NULL;
  108. }
  109. *tmp = '\0';
  110. if (++tmp >= end) {
  111. printk(KERN_ERR "property parse failed in %s at line %d\n",
  112. __func__, __LINE__);
  113. return NULL;
  114. }
  115. /* now we're on the length */
  116. *length = -1;
  117. *length = simple_strtoul(tmp, &tmp, 10);
  118. if (*length == -1) {
  119. printk(KERN_ERR "property parse failed in %s at line %d\n",
  120. __func__, __LINE__);
  121. return NULL;
  122. }
  123. if (*tmp != ' ' || ++tmp >= end) {
  124. printk(KERN_ERR "property parse failed in %s at line %d\n",
  125. __func__, __LINE__);
  126. return NULL;
  127. }
  128. /* now we're on the value */
  129. *value = tmp;
  130. tmp += *length;
  131. if (tmp > end) {
  132. printk(KERN_ERR "property parse failed in %s at line %d\n",
  133. __func__, __LINE__);
  134. return NULL;
  135. }
  136. else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
  137. printk(KERN_ERR "property parse failed in %s at line %d\n",
  138. __func__, __LINE__);
  139. return NULL;
  140. }
  141. tmp++;
  142. /* and now we should be on the next name, or the end */
  143. return tmp;
  144. }
  145. static struct property *new_property(const char *name, const int length,
  146. const unsigned char *value, struct property *last)
  147. {
  148. struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
  149. if (!new)
  150. return NULL;
  151. if (!(new->name = kstrdup(name, GFP_KERNEL)))
  152. goto cleanup;
  153. if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
  154. goto cleanup;
  155. memcpy(new->value, value, length);
  156. *(((char *)new->value) + length) = 0;
  157. new->length = length;
  158. new->next = last;
  159. return new;
  160. cleanup:
  161. kfree(new->name);
  162. kfree(new->value);
  163. kfree(new);
  164. return NULL;
  165. }
  166. static int do_add_node(char *buf, size_t bufsize)
  167. {
  168. char *path, *end, *name;
  169. struct device_node *np;
  170. struct property *prop = NULL;
  171. unsigned char* value;
  172. int length, rv = 0;
  173. end = buf + bufsize;
  174. path = buf;
  175. buf = strchr(buf, ' ');
  176. if (!buf)
  177. return -EINVAL;
  178. *buf = '\0';
  179. buf++;
  180. if ((np = of_find_node_by_path(path))) {
  181. of_node_put(np);
  182. return -EINVAL;
  183. }
  184. /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
  185. while (buf < end &&
  186. (buf = parse_next_property(buf, end, &name, &length, &value))) {
  187. struct property *last = prop;
  188. prop = new_property(name, length, value, last);
  189. if (!prop) {
  190. rv = -ENOMEM;
  191. prop = last;
  192. goto out;
  193. }
  194. }
  195. if (!buf) {
  196. rv = -EINVAL;
  197. goto out;
  198. }
  199. rv = pSeries_reconfig_add_node(path, prop);
  200. out:
  201. if (rv)
  202. release_prop_list(prop);
  203. return rv;
  204. }
  205. static int do_remove_node(char *buf)
  206. {
  207. struct device_node *node;
  208. int rv = -ENODEV;
  209. if ((node = of_find_node_by_path(buf)))
  210. rv = pSeries_reconfig_remove_node(node);
  211. of_node_put(node);
  212. return rv;
  213. }
  214. static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
  215. {
  216. char *handle_str;
  217. phandle handle;
  218. *npp = NULL;
  219. handle_str = buf;
  220. buf = strchr(buf, ' ');
  221. if (!buf)
  222. return NULL;
  223. *buf = '\0';
  224. buf++;
  225. handle = simple_strtoul(handle_str, NULL, 0);
  226. *npp = of_find_node_by_phandle(handle);
  227. return buf;
  228. }
  229. static int do_add_property(char *buf, size_t bufsize)
  230. {
  231. struct property *prop = NULL;
  232. struct device_node *np;
  233. unsigned char *value;
  234. char *name, *end;
  235. int length;
  236. end = buf + bufsize;
  237. buf = parse_node(buf, bufsize, &np);
  238. if (!np)
  239. return -ENODEV;
  240. if (parse_next_property(buf, end, &name, &length, &value) == NULL)
  241. return -EINVAL;
  242. prop = new_property(name, length, value, NULL);
  243. if (!prop)
  244. return -ENOMEM;
  245. of_add_property(np, prop);
  246. return 0;
  247. }
  248. static int do_remove_property(char *buf, size_t bufsize)
  249. {
  250. struct device_node *np;
  251. char *tmp;
  252. struct property *prop;
  253. buf = parse_node(buf, bufsize, &np);
  254. if (!np)
  255. return -ENODEV;
  256. tmp = strchr(buf,' ');
  257. if (tmp)
  258. *tmp = '\0';
  259. if (strlen(buf) == 0)
  260. return -EINVAL;
  261. prop = of_find_property(np, buf, NULL);
  262. return of_remove_property(np, prop);
  263. }
  264. static int do_update_property(char *buf, size_t bufsize)
  265. {
  266. struct device_node *np;
  267. unsigned char *value;
  268. char *name, *end, *next_prop;
  269. int length;
  270. struct property *newprop;
  271. buf = parse_node(buf, bufsize, &np);
  272. end = buf + bufsize;
  273. if (!np)
  274. return -ENODEV;
  275. next_prop = parse_next_property(buf, end, &name, &length, &value);
  276. if (!next_prop)
  277. return -EINVAL;
  278. if (!strlen(name))
  279. return -ENODEV;
  280. newprop = new_property(name, length, value, NULL);
  281. if (!newprop)
  282. return -ENOMEM;
  283. if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
  284. slb_set_size(*(int *)value);
  285. return of_update_property(np, newprop);
  286. }
  287. /**
  288. * ofdt_write - perform operations on the Open Firmware device tree
  289. *
  290. * @file: not used
  291. * @buf: command and arguments
  292. * @count: size of the command buffer
  293. * @off: not used
  294. *
  295. * Operations supported at this time are addition and removal of
  296. * whole nodes along with their properties. Operations on individual
  297. * properties are not implemented (yet).
  298. */
  299. static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
  300. loff_t *off)
  301. {
  302. int rv = 0;
  303. char *kbuf;
  304. char *tmp;
  305. if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
  306. rv = -ENOMEM;
  307. goto out;
  308. }
  309. if (copy_from_user(kbuf, buf, count)) {
  310. rv = -EFAULT;
  311. goto out;
  312. }
  313. kbuf[count] = '\0';
  314. tmp = strchr(kbuf, ' ');
  315. if (!tmp) {
  316. rv = -EINVAL;
  317. goto out;
  318. }
  319. *tmp = '\0';
  320. tmp++;
  321. if (!strcmp(kbuf, "add_node"))
  322. rv = do_add_node(tmp, count - (tmp - kbuf));
  323. else if (!strcmp(kbuf, "remove_node"))
  324. rv = do_remove_node(tmp);
  325. else if (!strcmp(kbuf, "add_property"))
  326. rv = do_add_property(tmp, count - (tmp - kbuf));
  327. else if (!strcmp(kbuf, "remove_property"))
  328. rv = do_remove_property(tmp, count - (tmp - kbuf));
  329. else if (!strcmp(kbuf, "update_property"))
  330. rv = do_update_property(tmp, count - (tmp - kbuf));
  331. else
  332. rv = -EINVAL;
  333. out:
  334. kfree(kbuf);
  335. return rv ? rv : count;
  336. }
  337. static const struct file_operations ofdt_fops = {
  338. .write = ofdt_write,
  339. .llseek = noop_llseek,
  340. };
  341. /* create /proc/powerpc/ofdt write-only by root */
  342. static int proc_ppc64_create_ofdt(void)
  343. {
  344. struct proc_dir_entry *ent;
  345. ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
  346. if (ent)
  347. proc_set_size(ent, 0);
  348. return 0;
  349. }
  350. machine_device_initcall(pseries, proc_ppc64_create_ofdt);