mobility.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Support for Partition Mobility/Migration
  3. *
  4. * Copyright (C) 2010 Nathan Fontenot
  5. * Copyright (C) 2010 IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/kobject.h>
  13. #include <linux/smp.h>
  14. #include <linux/completion.h>
  15. #include <linux/device.h>
  16. #include <linux/delay.h>
  17. #include <linux/slab.h>
  18. #include <asm/rtas.h>
  19. #include "pseries.h"
  20. static struct kobject *mobility_kobj;
  21. struct update_props_workarea {
  22. u32 phandle;
  23. u32 state;
  24. u64 reserved;
  25. u32 nprops;
  26. };
  27. #define NODE_ACTION_MASK 0xff000000
  28. #define NODE_COUNT_MASK 0x00ffffff
  29. #define DELETE_DT_NODE 0x01000000
  30. #define UPDATE_DT_NODE 0x02000000
  31. #define ADD_DT_NODE 0x03000000
  32. static int mobility_rtas_call(int token, char *buf)
  33. {
  34. int rc;
  35. spin_lock(&rtas_data_buf_lock);
  36. memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
  37. rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, 1);
  38. memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  39. spin_unlock(&rtas_data_buf_lock);
  40. return rc;
  41. }
  42. static int delete_dt_node(u32 phandle)
  43. {
  44. struct device_node *dn;
  45. dn = of_find_node_by_phandle(phandle);
  46. if (!dn)
  47. return -ENOENT;
  48. dlpar_detach_node(dn);
  49. return 0;
  50. }
  51. static int update_dt_property(struct device_node *dn, struct property **prop,
  52. const char *name, u32 vd, char *value)
  53. {
  54. struct property *new_prop = *prop;
  55. struct property *old_prop;
  56. int more = 0;
  57. /* A negative 'vd' value indicates that only part of the new property
  58. * value is contained in the buffer and we need to call
  59. * ibm,update-properties again to get the rest of the value.
  60. *
  61. * A negative value is also the two's compliment of the actual value.
  62. */
  63. if (vd & 0x80000000) {
  64. vd = ~vd + 1;
  65. more = 1;
  66. }
  67. if (new_prop) {
  68. /* partial property fixup */
  69. char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
  70. if (!new_data)
  71. return -ENOMEM;
  72. memcpy(new_data, new_prop->value, new_prop->length);
  73. memcpy(new_data + new_prop->length, value, vd);
  74. kfree(new_prop->value);
  75. new_prop->value = new_data;
  76. new_prop->length += vd;
  77. } else {
  78. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  79. if (!new_prop)
  80. return -ENOMEM;
  81. new_prop->name = kstrdup(name, GFP_KERNEL);
  82. if (!new_prop->name) {
  83. kfree(new_prop);
  84. return -ENOMEM;
  85. }
  86. new_prop->length = vd;
  87. new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
  88. if (!new_prop->value) {
  89. kfree(new_prop->name);
  90. kfree(new_prop);
  91. return -ENOMEM;
  92. }
  93. memcpy(new_prop->value, value, vd);
  94. *prop = new_prop;
  95. }
  96. if (!more) {
  97. old_prop = of_find_property(dn, new_prop->name, NULL);
  98. if (old_prop)
  99. prom_update_property(dn, new_prop, old_prop);
  100. else
  101. prom_add_property(dn, new_prop);
  102. new_prop = NULL;
  103. }
  104. return 0;
  105. }
  106. static int update_dt_node(u32 phandle)
  107. {
  108. struct update_props_workarea *upwa;
  109. struct device_node *dn;
  110. struct property *prop = NULL;
  111. int i, rc;
  112. char *prop_data;
  113. char *rtas_buf;
  114. int update_properties_token;
  115. update_properties_token = rtas_token("ibm,update-properties");
  116. if (update_properties_token == RTAS_UNKNOWN_SERVICE)
  117. return -EINVAL;
  118. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  119. if (!rtas_buf)
  120. return -ENOMEM;
  121. dn = of_find_node_by_phandle(phandle);
  122. if (!dn) {
  123. kfree(rtas_buf);
  124. return -ENOENT;
  125. }
  126. upwa = (struct update_props_workarea *)&rtas_buf[0];
  127. upwa->phandle = phandle;
  128. do {
  129. rc = mobility_rtas_call(update_properties_token, rtas_buf);
  130. if (rc < 0)
  131. break;
  132. prop_data = rtas_buf + sizeof(*upwa);
  133. for (i = 0; i < upwa->nprops; i++) {
  134. char *prop_name;
  135. u32 vd;
  136. prop_name = prop_data + 1;
  137. prop_data += strlen(prop_name) + 1;
  138. vd = *prop_data++;
  139. switch (vd) {
  140. case 0x00000000:
  141. /* name only property, nothing to do */
  142. break;
  143. case 0x80000000:
  144. prop = of_find_property(dn, prop_name, NULL);
  145. prom_remove_property(dn, prop);
  146. prop = NULL;
  147. break;
  148. default:
  149. rc = update_dt_property(dn, &prop, prop_name,
  150. vd, prop_data);
  151. if (rc) {
  152. printk(KERN_ERR "Could not update %s"
  153. " property\n", prop_name);
  154. }
  155. prop_data += vd;
  156. }
  157. }
  158. } while (rc == 1);
  159. of_node_put(dn);
  160. kfree(rtas_buf);
  161. return 0;
  162. }
  163. static int add_dt_node(u32 parent_phandle, u32 drc_index)
  164. {
  165. struct device_node *dn;
  166. struct device_node *parent_dn;
  167. int rc;
  168. dn = dlpar_configure_connector(drc_index);
  169. if (!dn)
  170. return -ENOENT;
  171. parent_dn = of_find_node_by_phandle(parent_phandle);
  172. if (!parent_dn) {
  173. dlpar_free_cc_nodes(dn);
  174. return -ENOENT;
  175. }
  176. dn->parent = parent_dn;
  177. rc = dlpar_attach_node(dn);
  178. if (rc)
  179. dlpar_free_cc_nodes(dn);
  180. of_node_put(parent_dn);
  181. return rc;
  182. }
  183. static int pseries_devicetree_update(void)
  184. {
  185. char *rtas_buf;
  186. u32 *data;
  187. int update_nodes_token;
  188. int rc;
  189. update_nodes_token = rtas_token("ibm,update-nodes");
  190. if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
  191. return -EINVAL;
  192. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  193. if (!rtas_buf)
  194. return -ENOMEM;
  195. do {
  196. rc = mobility_rtas_call(update_nodes_token, rtas_buf);
  197. if (rc && rc != 1)
  198. break;
  199. data = (u32 *)rtas_buf + 4;
  200. while (*data & NODE_ACTION_MASK) {
  201. int i;
  202. u32 action = *data & NODE_ACTION_MASK;
  203. int node_count = *data & NODE_COUNT_MASK;
  204. data++;
  205. for (i = 0; i < node_count; i++) {
  206. u32 phandle = *data++;
  207. u32 drc_index;
  208. switch (action) {
  209. case DELETE_DT_NODE:
  210. delete_dt_node(phandle);
  211. break;
  212. case UPDATE_DT_NODE:
  213. update_dt_node(phandle);
  214. break;
  215. case ADD_DT_NODE:
  216. drc_index = *data++;
  217. add_dt_node(phandle, drc_index);
  218. break;
  219. }
  220. }
  221. }
  222. } while (rc == 1);
  223. kfree(rtas_buf);
  224. return rc;
  225. }
  226. void post_mobility_fixup(void)
  227. {
  228. int rc;
  229. int activate_fw_token;
  230. rc = pseries_devicetree_update();
  231. if (rc) {
  232. printk(KERN_ERR "Initial post-mobility device tree update "
  233. "failed: %d\n", rc);
  234. return;
  235. }
  236. activate_fw_token = rtas_token("ibm,activate-firmware");
  237. if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
  238. printk(KERN_ERR "Could not make post-mobility "
  239. "activate-fw call.\n");
  240. return;
  241. }
  242. rc = rtas_call(activate_fw_token, 0, 1, NULL);
  243. if (!rc) {
  244. rc = pseries_devicetree_update();
  245. if (rc)
  246. printk(KERN_ERR "Secondary post-mobility device tree "
  247. "update failed: %d\n", rc);
  248. } else {
  249. printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
  250. return;
  251. }
  252. return;
  253. }
  254. static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. struct rtas_args args;
  258. u64 streamid;
  259. int rc;
  260. rc = strict_strtoull(buf, 0, &streamid);
  261. if (rc)
  262. return rc;
  263. memset(&args, 0, sizeof(args));
  264. args.token = rtas_token("ibm,suspend-me");
  265. args.nargs = 2;
  266. args.nret = 1;
  267. args.args[0] = streamid >> 32 ;
  268. args.args[1] = streamid & 0xffffffff;
  269. args.rets = &args.args[args.nargs];
  270. do {
  271. args.rets[0] = 0;
  272. rc = rtas_ibm_suspend_me(&args);
  273. if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE)
  274. ssleep(1);
  275. } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE);
  276. if (rc)
  277. return rc;
  278. else if (args.rets[0])
  279. return args.rets[0];
  280. post_mobility_fixup();
  281. return count;
  282. }
  283. static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
  284. static int __init mobility_sysfs_init(void)
  285. {
  286. int rc;
  287. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  288. if (!mobility_kobj)
  289. return -ENOMEM;
  290. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  291. return rc;
  292. }
  293. device_initcall(mobility_sysfs_init);