reconfig.c 8.6 KB

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