reconfig.c 12 KB

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