mobility.c 7.7 KB

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