pSeries_reconfig.c 9.2 KB

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