mobility.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. } __packed;
  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. *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, rtas_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. rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
  128. scope);
  129. if (rtas_rc < 0)
  130. break;
  131. prop_data = rtas_buf + sizeof(*upwa);
  132. /* On the first call to ibm,update-properties for a node the
  133. * the first property value descriptor contains an empty
  134. * property name, the property value length encoded as u32,
  135. * and the property value is the node path being updated.
  136. */
  137. if (*prop_data == 0) {
  138. prop_data++;
  139. vd = *(u32 *)prop_data;
  140. prop_data += vd + sizeof(vd);
  141. upwa->nprops--;
  142. }
  143. for (i = 0; i < upwa->nprops; i++) {
  144. char *prop_name;
  145. prop_name = prop_data;
  146. prop_data += strlen(prop_name) + 1;
  147. vd = *(u32 *)prop_data;
  148. prop_data += sizeof(vd);
  149. switch (vd) {
  150. case 0x00000000:
  151. /* name only property, nothing to do */
  152. break;
  153. case 0x80000000:
  154. prop = of_find_property(dn, prop_name, NULL);
  155. of_remove_property(dn, prop);
  156. prop = NULL;
  157. break;
  158. default:
  159. rc = update_dt_property(dn, &prop, prop_name,
  160. vd, prop_data);
  161. if (rc) {
  162. printk(KERN_ERR "Could not update %s"
  163. " property\n", prop_name);
  164. }
  165. prop_data += vd;
  166. }
  167. }
  168. } while (rtas_rc == 1);
  169. of_node_put(dn);
  170. kfree(rtas_buf);
  171. return 0;
  172. }
  173. static int add_dt_node(u32 parent_phandle, u32 drc_index)
  174. {
  175. struct device_node *dn;
  176. struct device_node *parent_dn;
  177. int rc;
  178. dn = dlpar_configure_connector(drc_index);
  179. if (!dn)
  180. return -ENOENT;
  181. parent_dn = of_find_node_by_phandle(parent_phandle);
  182. if (!parent_dn) {
  183. dlpar_free_cc_nodes(dn);
  184. return -ENOENT;
  185. }
  186. dn->parent = parent_dn;
  187. rc = dlpar_attach_node(dn);
  188. if (rc)
  189. dlpar_free_cc_nodes(dn);
  190. of_node_put(parent_dn);
  191. return rc;
  192. }
  193. int pseries_devicetree_update(s32 scope)
  194. {
  195. char *rtas_buf;
  196. u32 *data;
  197. int update_nodes_token;
  198. int rc;
  199. update_nodes_token = rtas_token("ibm,update-nodes");
  200. if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
  201. return -EINVAL;
  202. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  203. if (!rtas_buf)
  204. return -ENOMEM;
  205. do {
  206. rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
  207. if (rc && rc != 1)
  208. break;
  209. data = (u32 *)rtas_buf + 4;
  210. while (*data & NODE_ACTION_MASK) {
  211. int i;
  212. u32 action = *data & NODE_ACTION_MASK;
  213. int node_count = *data & NODE_COUNT_MASK;
  214. data++;
  215. for (i = 0; i < node_count; i++) {
  216. u32 phandle = *data++;
  217. u32 drc_index;
  218. switch (action) {
  219. case DELETE_DT_NODE:
  220. delete_dt_node(phandle);
  221. break;
  222. case UPDATE_DT_NODE:
  223. update_dt_node(phandle, scope);
  224. break;
  225. case ADD_DT_NODE:
  226. drc_index = *data++;
  227. add_dt_node(phandle, drc_index);
  228. break;
  229. }
  230. }
  231. }
  232. } while (rc == 1);
  233. kfree(rtas_buf);
  234. return rc;
  235. }
  236. void post_mobility_fixup(void)
  237. {
  238. int rc;
  239. int activate_fw_token;
  240. rc = pseries_devicetree_update(MIGRATION_SCOPE);
  241. if (rc) {
  242. printk(KERN_ERR "Initial post-mobility device tree update "
  243. "failed: %d\n", rc);
  244. return;
  245. }
  246. activate_fw_token = rtas_token("ibm,activate-firmware");
  247. if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
  248. printk(KERN_ERR "Could not make post-mobility "
  249. "activate-fw call.\n");
  250. return;
  251. }
  252. rc = rtas_call(activate_fw_token, 0, 1, NULL);
  253. if (!rc) {
  254. rc = pseries_devicetree_update(MIGRATION_SCOPE);
  255. if (rc)
  256. printk(KERN_ERR "Secondary post-mobility device tree "
  257. "update failed: %d\n", rc);
  258. } else {
  259. printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
  260. return;
  261. }
  262. return;
  263. }
  264. static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
  265. const char *buf, size_t count)
  266. {
  267. struct rtas_args args;
  268. u64 streamid;
  269. int rc;
  270. rc = strict_strtoull(buf, 0, &streamid);
  271. if (rc)
  272. return rc;
  273. memset(&args, 0, sizeof(args));
  274. args.token = rtas_token("ibm,suspend-me");
  275. args.nargs = 2;
  276. args.nret = 1;
  277. args.args[0] = streamid >> 32 ;
  278. args.args[1] = streamid & 0xffffffff;
  279. args.rets = &args.args[args.nargs];
  280. do {
  281. args.rets[0] = 0;
  282. rc = rtas_ibm_suspend_me(&args);
  283. if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE)
  284. ssleep(1);
  285. } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE);
  286. if (rc)
  287. return rc;
  288. else if (args.rets[0])
  289. return args.rets[0];
  290. post_mobility_fixup();
  291. return count;
  292. }
  293. static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
  294. static int __init mobility_sysfs_init(void)
  295. {
  296. int rc;
  297. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  298. if (!mobility_kobj)
  299. return -ENOMEM;
  300. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  301. return rc;
  302. }
  303. device_initcall(mobility_sysfs_init);