reconfig.c 12 KB

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