reconfig.c 9.7 KB

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