reconfig.c 12 KB

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