reconfig.c 12 KB

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