reconfig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. of_node_put(parent);
  141. return -EBUSY;
  142. }
  143. remove_node_proc_entries(np);
  144. blocking_notifier_call_chain(&pSeries_reconfig_chain,
  145. PSERIES_RECONFIG_REMOVE, np);
  146. of_detach_node(np);
  147. of_node_put(parent);
  148. of_node_put(np); /* Must decrement the refcount */
  149. return 0;
  150. }
  151. /*
  152. * /proc/ppc64/ofdt - yucky binary interface for adding and removing
  153. * OF device nodes. Should be deprecated as soon as we get an
  154. * in-kernel wrapper for the RTAS ibm,configure-connector call.
  155. */
  156. static void release_prop_list(const struct property *prop)
  157. {
  158. struct property *next;
  159. for (; prop; prop = next) {
  160. next = prop->next;
  161. kfree(prop->name);
  162. kfree(prop->value);
  163. kfree(prop);
  164. }
  165. }
  166. /**
  167. * parse_next_property - process the next property from raw input buffer
  168. * @buf: input buffer, must be nul-terminated
  169. * @end: end of the input buffer + 1, for validation
  170. * @name: return value; set to property name in buf
  171. * @length: return value; set to length of value
  172. * @value: return value; set to the property value in buf
  173. *
  174. * Note that the caller must make copies of the name and value returned,
  175. * this function does no allocation or copying of the data. Return value
  176. * is set to the next name in buf, or NULL on error.
  177. */
  178. static char * parse_next_property(char *buf, char *end, char **name, int *length,
  179. unsigned char **value)
  180. {
  181. char *tmp;
  182. *name = buf;
  183. tmp = strchr(buf, ' ');
  184. if (!tmp) {
  185. printk(KERN_ERR "property parse failed in %s at line %d\n",
  186. __func__, __LINE__);
  187. return NULL;
  188. }
  189. *tmp = '\0';
  190. if (++tmp >= end) {
  191. printk(KERN_ERR "property parse failed in %s at line %d\n",
  192. __func__, __LINE__);
  193. return NULL;
  194. }
  195. /* now we're on the length */
  196. *length = -1;
  197. *length = simple_strtoul(tmp, &tmp, 10);
  198. if (*length == -1) {
  199. printk(KERN_ERR "property parse failed in %s at line %d\n",
  200. __func__, __LINE__);
  201. return NULL;
  202. }
  203. if (*tmp != ' ' || ++tmp >= end) {
  204. printk(KERN_ERR "property parse failed in %s at line %d\n",
  205. __func__, __LINE__);
  206. return NULL;
  207. }
  208. /* now we're on the value */
  209. *value = tmp;
  210. tmp += *length;
  211. if (tmp > end) {
  212. printk(KERN_ERR "property parse failed in %s at line %d\n",
  213. __func__, __LINE__);
  214. return NULL;
  215. }
  216. else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
  217. printk(KERN_ERR "property parse failed in %s at line %d\n",
  218. __func__, __LINE__);
  219. return NULL;
  220. }
  221. tmp++;
  222. /* and now we should be on the next name, or the end */
  223. return tmp;
  224. }
  225. static struct property *new_property(const char *name, const int length,
  226. const unsigned char *value, struct property *last)
  227. {
  228. struct property *new = kzalloc(sizeof(*new), GFP_KERNEL);
  229. if (!new)
  230. return NULL;
  231. if (!(new->name = kmalloc(strlen(name) + 1, GFP_KERNEL)))
  232. goto cleanup;
  233. if (!(new->value = kmalloc(length + 1, GFP_KERNEL)))
  234. goto cleanup;
  235. strcpy(new->name, name);
  236. memcpy(new->value, value, length);
  237. *(((char *)new->value) + length) = 0;
  238. new->length = length;
  239. new->next = last;
  240. return new;
  241. cleanup:
  242. kfree(new->name);
  243. kfree(new->value);
  244. kfree(new);
  245. return NULL;
  246. }
  247. static int do_add_node(char *buf, size_t bufsize)
  248. {
  249. char *path, *end, *name;
  250. struct device_node *np;
  251. struct property *prop = NULL;
  252. unsigned char* value;
  253. int length, rv = 0;
  254. end = buf + bufsize;
  255. path = buf;
  256. buf = strchr(buf, ' ');
  257. if (!buf)
  258. return -EINVAL;
  259. *buf = '\0';
  260. buf++;
  261. if ((np = of_find_node_by_path(path))) {
  262. of_node_put(np);
  263. return -EINVAL;
  264. }
  265. /* rv = build_prop_list(tmp, bufsize - (tmp - buf), &proplist); */
  266. while (buf < end &&
  267. (buf = parse_next_property(buf, end, &name, &length, &value))) {
  268. struct property *last = prop;
  269. prop = new_property(name, length, value, last);
  270. if (!prop) {
  271. rv = -ENOMEM;
  272. prop = last;
  273. goto out;
  274. }
  275. }
  276. if (!buf) {
  277. rv = -EINVAL;
  278. goto out;
  279. }
  280. rv = pSeries_reconfig_add_node(path, prop);
  281. out:
  282. if (rv)
  283. release_prop_list(prop);
  284. return rv;
  285. }
  286. static int do_remove_node(char *buf)
  287. {
  288. struct device_node *node;
  289. int rv = -ENODEV;
  290. if ((node = of_find_node_by_path(buf)))
  291. rv = pSeries_reconfig_remove_node(node);
  292. of_node_put(node);
  293. return rv;
  294. }
  295. static char *parse_node(char *buf, size_t bufsize, struct device_node **npp)
  296. {
  297. char *handle_str;
  298. phandle handle;
  299. *npp = NULL;
  300. handle_str = buf;
  301. buf = strchr(buf, ' ');
  302. if (!buf)
  303. return NULL;
  304. *buf = '\0';
  305. buf++;
  306. handle = simple_strtoul(handle_str, NULL, 0);
  307. *npp = of_find_node_by_phandle(handle);
  308. return buf;
  309. }
  310. static int do_add_property(char *buf, size_t bufsize)
  311. {
  312. struct property *prop = NULL;
  313. struct device_node *np;
  314. unsigned char *value;
  315. char *name, *end;
  316. int length;
  317. end = buf + bufsize;
  318. buf = parse_node(buf, bufsize, &np);
  319. if (!np)
  320. return -ENODEV;
  321. if (parse_next_property(buf, end, &name, &length, &value) == NULL)
  322. return -EINVAL;
  323. prop = new_property(name, length, value, NULL);
  324. if (!prop)
  325. return -ENOMEM;
  326. prom_add_property(np, prop);
  327. return 0;
  328. }
  329. static int do_remove_property(char *buf, size_t bufsize)
  330. {
  331. struct device_node *np;
  332. char *tmp;
  333. struct property *prop;
  334. buf = parse_node(buf, bufsize, &np);
  335. if (!np)
  336. return -ENODEV;
  337. tmp = strchr(buf,' ');
  338. if (tmp)
  339. *tmp = '\0';
  340. if (strlen(buf) == 0)
  341. return -EINVAL;
  342. prop = of_find_property(np, buf, NULL);
  343. return prom_remove_property(np, prop);
  344. }
  345. static int do_update_property(char *buf, size_t bufsize)
  346. {
  347. struct device_node *np;
  348. unsigned char *value;
  349. char *name, *end, *next_prop;
  350. int rc, length;
  351. struct property *newprop, *oldprop;
  352. buf = parse_node(buf, bufsize, &np);
  353. end = buf + bufsize;
  354. if (!np)
  355. return -ENODEV;
  356. next_prop = parse_next_property(buf, end, &name, &length, &value);
  357. if (!next_prop)
  358. return -EINVAL;
  359. newprop = new_property(name, length, value, NULL);
  360. if (!newprop)
  361. return -ENOMEM;
  362. oldprop = of_find_property(np, name,NULL);
  363. if (!oldprop)
  364. return -ENODEV;
  365. rc = prom_update_property(np, newprop, oldprop);
  366. if (rc)
  367. return rc;
  368. /* For memory under the ibm,dynamic-reconfiguration-memory node
  369. * of the device tree, adding and removing memory is just an update
  370. * to the ibm,dynamic-memory property instead of adding/removing a
  371. * memory node in the device tree. For these cases we still need to
  372. * involve the notifier chain.
  373. */
  374. if (!strcmp(name, "ibm,dynamic-memory")) {
  375. int action;
  376. next_prop = parse_next_property(next_prop, end, &name,
  377. &length, &value);
  378. if (!next_prop)
  379. return -EINVAL;
  380. if (!strcmp(name, "add"))
  381. action = PSERIES_DRCONF_MEM_ADD;
  382. else
  383. action = PSERIES_DRCONF_MEM_REMOVE;
  384. rc = blocking_notifier_call_chain(&pSeries_reconfig_chain,
  385. action, value);
  386. if (rc == NOTIFY_BAD) {
  387. rc = prom_update_property(np, oldprop, newprop);
  388. return -ENOMEM;
  389. }
  390. }
  391. return 0;
  392. }
  393. /**
  394. * ofdt_write - perform operations on the Open Firmware device tree
  395. *
  396. * @file: not used
  397. * @buf: command and arguments
  398. * @count: size of the command buffer
  399. * @off: not used
  400. *
  401. * Operations supported at this time are addition and removal of
  402. * whole nodes along with their properties. Operations on individual
  403. * properties are not implemented (yet).
  404. */
  405. static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
  406. loff_t *off)
  407. {
  408. int rv = 0;
  409. char *kbuf;
  410. char *tmp;
  411. if (!(kbuf = kmalloc(count + 1, GFP_KERNEL))) {
  412. rv = -ENOMEM;
  413. goto out;
  414. }
  415. if (copy_from_user(kbuf, buf, count)) {
  416. rv = -EFAULT;
  417. goto out;
  418. }
  419. kbuf[count] = '\0';
  420. tmp = strchr(kbuf, ' ');
  421. if (!tmp) {
  422. rv = -EINVAL;
  423. goto out;
  424. }
  425. *tmp = '\0';
  426. tmp++;
  427. if (!strcmp(kbuf, "add_node"))
  428. rv = do_add_node(tmp, count - (tmp - kbuf));
  429. else if (!strcmp(kbuf, "remove_node"))
  430. rv = do_remove_node(tmp);
  431. else if (!strcmp(kbuf, "add_property"))
  432. rv = do_add_property(tmp, count - (tmp - kbuf));
  433. else if (!strcmp(kbuf, "remove_property"))
  434. rv = do_remove_property(tmp, count - (tmp - kbuf));
  435. else if (!strcmp(kbuf, "update_property"))
  436. rv = do_update_property(tmp, count - (tmp - kbuf));
  437. else
  438. rv = -EINVAL;
  439. out:
  440. kfree(kbuf);
  441. return rv ? rv : count;
  442. }
  443. static const struct file_operations ofdt_fops = {
  444. .write = ofdt_write
  445. };
  446. /* create /proc/ppc64/ofdt write-only by root */
  447. static int proc_ppc64_create_ofdt(void)
  448. {
  449. struct proc_dir_entry *ent;
  450. if (!machine_is(pseries))
  451. return 0;
  452. ent = proc_create("ppc64/ofdt", S_IWUSR, NULL, &ofdt_fops);
  453. if (ent)
  454. ent->size = 0;
  455. return 0;
  456. }
  457. __initcall(proc_ppc64_create_ofdt);