reconfig.c 9.7 KB

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