reconfig.c 11 KB

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