mobility.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/machdep.h>
  20. #include <asm/rtas.h>
  21. #include "pseries.h"
  22. static struct kobject *mobility_kobj;
  23. struct update_props_workarea {
  24. u32 phandle;
  25. u32 state;
  26. u64 reserved;
  27. u32 nprops;
  28. } __packed;
  29. #define NODE_ACTION_MASK 0xff000000
  30. #define NODE_COUNT_MASK 0x00ffffff
  31. #define DELETE_DT_NODE 0x01000000
  32. #define UPDATE_DT_NODE 0x02000000
  33. #define ADD_DT_NODE 0x03000000
  34. #define MIGRATION_SCOPE (1)
  35. static int mobility_rtas_call(int token, char *buf, s32 scope)
  36. {
  37. int rc;
  38. spin_lock(&rtas_data_buf_lock);
  39. memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
  40. rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
  41. memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  42. spin_unlock(&rtas_data_buf_lock);
  43. return rc;
  44. }
  45. static int delete_dt_node(u32 phandle)
  46. {
  47. struct device_node *dn;
  48. dn = of_find_node_by_phandle(phandle);
  49. if (!dn)
  50. return -ENOENT;
  51. dlpar_detach_node(dn);
  52. of_node_put(dn);
  53. return 0;
  54. }
  55. static int update_dt_property(struct device_node *dn, struct property **prop,
  56. const char *name, u32 vd, char *value)
  57. {
  58. struct property *new_prop = *prop;
  59. int more = 0;
  60. /* A negative 'vd' value indicates that only part of the new property
  61. * value is contained in the buffer and we need to call
  62. * ibm,update-properties again to get the rest of the value.
  63. *
  64. * A negative value is also the two's compliment of the actual value.
  65. */
  66. if (vd & 0x80000000) {
  67. vd = ~vd + 1;
  68. more = 1;
  69. }
  70. if (new_prop) {
  71. /* partial property fixup */
  72. char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
  73. if (!new_data)
  74. return -ENOMEM;
  75. memcpy(new_data, new_prop->value, new_prop->length);
  76. memcpy(new_data + new_prop->length, value, vd);
  77. kfree(new_prop->value);
  78. new_prop->value = new_data;
  79. new_prop->length += vd;
  80. } else {
  81. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  82. if (!new_prop)
  83. return -ENOMEM;
  84. new_prop->name = kstrdup(name, GFP_KERNEL);
  85. if (!new_prop->name) {
  86. kfree(new_prop);
  87. return -ENOMEM;
  88. }
  89. new_prop->length = vd;
  90. new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
  91. if (!new_prop->value) {
  92. kfree(new_prop->name);
  93. kfree(new_prop);
  94. return -ENOMEM;
  95. }
  96. memcpy(new_prop->value, value, vd);
  97. *prop = new_prop;
  98. }
  99. if (!more) {
  100. of_update_property(dn, new_prop);
  101. *prop = NULL;
  102. }
  103. return 0;
  104. }
  105. static int update_dt_node(u32 phandle, s32 scope)
  106. {
  107. struct update_props_workarea *upwa;
  108. struct device_node *dn;
  109. struct property *prop = NULL;
  110. int i, rc, rtas_rc;
  111. char *prop_data;
  112. char *rtas_buf;
  113. int update_properties_token;
  114. u32 vd;
  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. rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
  130. scope);
  131. if (rtas_rc < 0)
  132. break;
  133. prop_data = rtas_buf + sizeof(*upwa);
  134. /* On the first call to ibm,update-properties for a node the
  135. * the first property value descriptor contains an empty
  136. * property name, the property value length encoded as u32,
  137. * and the property value is the node path being updated.
  138. */
  139. if (*prop_data == 0) {
  140. prop_data++;
  141. vd = *(u32 *)prop_data;
  142. prop_data += vd + sizeof(vd);
  143. upwa->nprops--;
  144. }
  145. for (i = 0; i < upwa->nprops; i++) {
  146. char *prop_name;
  147. prop_name = prop_data;
  148. prop_data += strlen(prop_name) + 1;
  149. vd = *(u32 *)prop_data;
  150. prop_data += sizeof(vd);
  151. switch (vd) {
  152. case 0x00000000:
  153. /* name only property, nothing to do */
  154. break;
  155. case 0x80000000:
  156. prop = of_find_property(dn, prop_name, NULL);
  157. of_remove_property(dn, prop);
  158. prop = NULL;
  159. break;
  160. default:
  161. rc = update_dt_property(dn, &prop, prop_name,
  162. vd, prop_data);
  163. if (rc) {
  164. printk(KERN_ERR "Could not update %s"
  165. " property\n", prop_name);
  166. }
  167. prop_data += vd;
  168. }
  169. }
  170. } while (rtas_rc == 1);
  171. of_node_put(dn);
  172. kfree(rtas_buf);
  173. return 0;
  174. }
  175. static int add_dt_node(u32 parent_phandle, u32 drc_index)
  176. {
  177. struct device_node *dn;
  178. struct device_node *parent_dn;
  179. int rc;
  180. parent_dn = of_find_node_by_phandle(parent_phandle);
  181. if (!parent_dn)
  182. return -ENOENT;
  183. dn = dlpar_configure_connector(drc_index, parent_dn);
  184. if (!dn)
  185. return -ENOENT;
  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. activate_fw_token = rtas_token("ibm,activate-firmware");
  240. if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
  241. printk(KERN_ERR "Could not make post-mobility "
  242. "activate-fw call.\n");
  243. return;
  244. }
  245. do {
  246. rc = rtas_call(activate_fw_token, 0, 1, NULL);
  247. } while (rtas_busy_delay(rc));
  248. if (rc)
  249. printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
  250. rc = pseries_devicetree_update(MIGRATION_SCOPE);
  251. if (rc)
  252. printk(KERN_ERR "Post-mobility device tree update "
  253. "failed: %d\n", rc);
  254. return;
  255. }
  256. static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
  257. const char *buf, size_t count)
  258. {
  259. struct rtas_args args;
  260. u64 streamid;
  261. int rc;
  262. rc = kstrtou64(buf, 0, &streamid);
  263. if (rc)
  264. return rc;
  265. memset(&args, 0, sizeof(args));
  266. args.token = rtas_token("ibm,suspend-me");
  267. args.nargs = 2;
  268. args.nret = 1;
  269. args.args[0] = streamid >> 32 ;
  270. args.args[1] = streamid & 0xffffffff;
  271. args.rets = &args.args[args.nargs];
  272. do {
  273. args.rets[0] = 0;
  274. rc = rtas_ibm_suspend_me(&args);
  275. if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE)
  276. ssleep(1);
  277. } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE);
  278. if (rc)
  279. return rc;
  280. else if (args.rets[0])
  281. return args.rets[0];
  282. post_mobility_fixup();
  283. return count;
  284. }
  285. static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
  286. static int __init mobility_sysfs_init(void)
  287. {
  288. int rc;
  289. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  290. if (!mobility_kobj)
  291. return -ENOMEM;
  292. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  293. return rc;
  294. }
  295. machine_device_initcall(pseries, mobility_sysfs_init);