/arch/ppc64/kernel/pSeries_reconfig.c

https://bitbucket.org/evzijst/gittest · C · 434 lines · 301 code · 69 blank · 64 comment · 54 complexity · 0d323771be85ec46dc8b83509797fc5c MD5 · raw file

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