reconfig.c 8.8 KB

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