reconfig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 = kmalloc(strlen(name) + 1, GFP_KERNEL)))
  182. goto cleanup;
  183. if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
  184. goto cleanup;
  185. strcpy(new->name, name);
  186. memcpy(new->value, value, length);
  187. *(((char *)new->value) + length) = 0;
  188. new->length = length;
  189. new->next = last;
  190. return new;
  191. cleanup:
  192. kfree(new->name);
  193. kfree(new->value);
  194. kfree(new);
  195. return NULL;
  196. }
  197. static int do_add_node(char *buf, size_t bufsize)
  198. {
  199. char *path, *end, *name;
  200. struct device_node *np;
  201. struct property *prop = NULL;
  202. unsigned char* value;
  203. int length, rv = 0;
  204. end = buf + bufsize;
  205. path = buf;
  206. buf = strchr(buf, ' ');
  207. if (!buf)
  208. return -EINVAL;
  209. *buf = '\0';
  210. buf++;
  211. if ((np = of_find_node_by_path(path))) {
  212. of_node_put(np);
  213. return -EINVAL;
  214. }
  215. /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
  216. while (buf < end &&
  217. (buf = parse_next_property(buf, end, &name, &length, &value))) {
  218. struct property *last = prop;
  219. prop = new_property(name, length, value, last);
  220. if (!prop) {
  221. rv = -ENOMEM;
  222. prop = last;
  223. goto out;
  224. }
  225. }
  226. if (!buf) {
  227. rv = -EINVAL;
  228. goto out;
  229. }
  230. rv = pSeries_reconfig_add_node(path, prop);
  231. out:
  232. if (rv)
  233. release_prop_list(prop);
  234. return rv;
  235. }
  236. static int do_remove_node(char *buf)
  237. {
  238. struct device_node *node;
  239. int rv = -ENODEV;
  240. if ((node = of_find_node_by_path(buf)))
  241. rv = pSeries_reconfig_remove_node(node);
  242. of_node_put(node);
  243. return rv;
  244. }
  245. static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
  246. {
  247. char *handle_str;
  248. phandle handle;
  249. *npp = NULL;
  250. handle_str = buf;
  251. buf = strchr(buf, ' ');
  252. if (!buf)
  253. return NULL;
  254. *buf = '\0';
  255. buf++;
  256. handle = simple_strtoul(handle_str, NULL, 0);
  257. *npp = of_find_node_by_phandle(handle);
  258. return buf;
  259. }
  260. static int do_add_property(char *buf, size_t bufsize)
  261. {
  262. struct property *prop = NULL;
  263. struct device_node *np;
  264. unsigned char *value;
  265. char *name, *end;
  266. int length;
  267. end = buf + bufsize;
  268. buf = parse_node(buf, bufsize, &np);
  269. if (!np)
  270. return -ENODEV;
  271. if (parse_next_property(buf, end, &name, &length, &value) == NULL)
  272. return -EINVAL;
  273. prop = new_property(name, length, value, NULL);
  274. if (!prop)
  275. return -ENOMEM;
  276. of_add_property(np, prop);
  277. return 0;
  278. }
  279. static int do_remove_property(char *buf, size_t bufsize)
  280. {
  281. struct device_node *np;
  282. char *tmp;
  283. struct property *prop;
  284. buf = parse_node(buf, bufsize, &np);
  285. if (!np)
  286. return -ENODEV;
  287. tmp = strchr(buf,' ');
  288. if (tmp)
  289. *tmp = '\0';
  290. if (strlen(buf) == 0)
  291. return -EINVAL;
  292. prop = of_find_property(np, buf, NULL);
  293. return of_remove_property(np, prop);
  294. }
  295. static int do_update_property(char *buf, size_t bufsize)
  296. {
  297. struct device_node *np;
  298. unsigned char *value;
  299. char *name, *end, *next_prop;
  300. int length;
  301. struct property *newprop;
  302. buf = parse_node(buf, bufsize, &np);
  303. end = buf + bufsize;
  304. if (!np)
  305. return -ENODEV;
  306. next_prop = parse_next_property(buf, end, &name, &length, &value);
  307. if (!next_prop)
  308. return -EINVAL;
  309. if (!strlen(name))
  310. return -ENODEV;
  311. newprop = new_property(name, length, value, NULL);
  312. if (!newprop)
  313. return -ENOMEM;
  314. if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
  315. slb_set_size(*(int *)value);
  316. return of_update_property(np, newprop);
  317. }
  318. /**
  319. * ofdt_write - perform operations on the Open Firmware device tree
  320. *
  321. * @file: not used
  322. * @buf: command and arguments
  323. * @count: size of the command buffer
  324. * @off: not used
  325. *
  326. * Operations supported at this time are addition and removal of
  327. * whole nodes along with their properties. Operations on individual
  328. * properties are not implemented (yet).
  329. */
  330. static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
  331. loff_t *off)
  332. {
  333. int rv = 0;
  334. char *kbuf;
  335. char *tmp;
  336. if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
  337. rv = -ENOMEM;
  338. goto out;
  339. }
  340. if (copy_from_user(kbuf, buf, count)) {
  341. rv = -EFAULT;
  342. goto out;
  343. }
  344. kbuf[count] = '\0';
  345. tmp = strchr(kbuf, ' ');
  346. if (!tmp) {
  347. rv = -EINVAL;
  348. goto out;
  349. }
  350. *tmp = '\0';
  351. tmp++;
  352. if (!strcmp(kbuf, "add_node"))
  353. rv = do_add_node(tmp, count - (tmp - kbuf));
  354. else if (!strcmp(kbuf, "remove_node"))
  355. rv = do_remove_node(tmp);
  356. else if (!strcmp(kbuf, "add_property"))
  357. rv = do_add_property(tmp, count - (tmp - kbuf));
  358. else if (!strcmp(kbuf, "remove_property"))
  359. rv = do_remove_property(tmp, count - (tmp - kbuf));
  360. else if (!strcmp(kbuf, "update_property"))
  361. rv = do_update_property(tmp, count - (tmp - kbuf));
  362. else
  363. rv = -EINVAL;
  364. out:
  365. kfree(kbuf);
  366. return rv ? rv : count;
  367. }
  368. static const struct file_operations ofdt_fops = {
  369. .write = ofdt_write,
  370. .llseek = noop_llseek,
  371. };
  372. /* create /proc/powerpc/ofdt write-only by root */
  373. static int proc_ppc64_create_ofdt(void)
  374. {
  375. struct proc_dir_entry *ent;
  376. if (!machine_is(pseries))
  377. return 0;
  378. ent = proc_create("powerpc/ofdt", S_IWUSR, NULL, &ofdt_fops);
  379. if (ent)
  380. ent->size = 0;
  381. return 0;
  382. }
  383. __initcall(proc_ppc64_create_ofdt);