mobility.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. static int mobility_rtas_call(int token, char *buf)
  34. {
  35. int rc;
  36. spin_lock(&rtas_data_buf_lock);
  37. memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
  38. rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, 1);
  39. memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  40. spin_unlock(&rtas_data_buf_lock);
  41. return rc;
  42. }
  43. static int delete_dt_node(u32 phandle)
  44. {
  45. struct device_node *dn;
  46. dn = of_find_node_by_phandle(phandle);
  47. if (!dn)
  48. return -ENOENT;
  49. dlpar_detach_node(dn);
  50. return 0;
  51. }
  52. static int update_dt_property(struct device_node *dn, struct property **prop,
  53. const char *name, u32 vd, char *value)
  54. {
  55. struct property *new_prop = *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. prom_update_property(dn, new_prop);
  98. new_prop = NULL;
  99. }
  100. return 0;
  101. }
  102. static int update_dt_node(u32 phandle)
  103. {
  104. struct update_props_workarea *upwa;
  105. struct device_node *dn;
  106. struct property *prop = NULL;
  107. int i, rc;
  108. char *prop_data;
  109. char *rtas_buf;
  110. int update_properties_token;
  111. update_properties_token = rtas_token("ibm,update-properties");
  112. if (update_properties_token == RTAS_UNKNOWN_SERVICE)
  113. return -EINVAL;
  114. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  115. if (!rtas_buf)
  116. return -ENOMEM;
  117. dn = of_find_node_by_phandle(phandle);
  118. if (!dn) {
  119. kfree(rtas_buf);
  120. return -ENOENT;
  121. }
  122. upwa = (struct update_props_workarea *)&rtas_buf[0];
  123. upwa->phandle = phandle;
  124. do {
  125. rc = mobility_rtas_call(update_properties_token, rtas_buf);
  126. if (rc < 0)
  127. break;
  128. prop_data = rtas_buf + sizeof(*upwa);
  129. for (i = 0; i < upwa->nprops; i++) {
  130. char *prop_name;
  131. u32 vd;
  132. prop_name = prop_data + 1;
  133. prop_data += strlen(prop_name) + 1;
  134. vd = *prop_data++;
  135. switch (vd) {
  136. case 0x00000000:
  137. /* name only property, nothing to do */
  138. break;
  139. case 0x80000000:
  140. prop = of_find_property(dn, prop_name, NULL);
  141. prom_remove_property(dn, prop);
  142. prop = NULL;
  143. break;
  144. default:
  145. rc = update_dt_property(dn, &prop, prop_name,
  146. vd, prop_data);
  147. if (rc) {
  148. printk(KERN_ERR "Could not update %s"
  149. " property\n", prop_name);
  150. }
  151. prop_data += vd;
  152. }
  153. }
  154. } while (rc == 1);
  155. of_node_put(dn);
  156. kfree(rtas_buf);
  157. return 0;
  158. }
  159. static int add_dt_node(u32 parent_phandle, u32 drc_index)
  160. {
  161. struct device_node *dn;
  162. struct device_node *parent_dn;
  163. int rc;
  164. dn = dlpar_configure_connector(drc_index);
  165. if (!dn)
  166. return -ENOENT;
  167. parent_dn = of_find_node_by_phandle(parent_phandle);
  168. if (!parent_dn) {
  169. dlpar_free_cc_nodes(dn);
  170. return -ENOENT;
  171. }
  172. dn->parent = parent_dn;
  173. rc = dlpar_attach_node(dn);
  174. if (rc)
  175. dlpar_free_cc_nodes(dn);
  176. of_node_put(parent_dn);
  177. return rc;
  178. }
  179. static int pseries_devicetree_update(void)
  180. {
  181. char *rtas_buf;
  182. u32 *data;
  183. int update_nodes_token;
  184. int rc;
  185. update_nodes_token = rtas_token("ibm,update-nodes");
  186. if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
  187. return -EINVAL;
  188. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  189. if (!rtas_buf)
  190. return -ENOMEM;
  191. do {
  192. rc = mobility_rtas_call(update_nodes_token, rtas_buf);
  193. if (rc && rc != 1)
  194. break;
  195. data = (u32 *)rtas_buf + 4;
  196. while (*data & NODE_ACTION_MASK) {
  197. int i;
  198. u32 action = *data & NODE_ACTION_MASK;
  199. int node_count = *data & NODE_COUNT_MASK;
  200. data++;
  201. for (i = 0; i < node_count; i++) {
  202. u32 phandle = *data++;
  203. u32 drc_index;
  204. switch (action) {
  205. case DELETE_DT_NODE:
  206. delete_dt_node(phandle);
  207. break;
  208. case UPDATE_DT_NODE:
  209. update_dt_node(phandle);
  210. break;
  211. case ADD_DT_NODE:
  212. drc_index = *data++;
  213. add_dt_node(phandle, drc_index);
  214. break;
  215. }
  216. }
  217. }
  218. } while (rc == 1);
  219. kfree(rtas_buf);
  220. return rc;
  221. }
  222. void post_mobility_fixup(void)
  223. {
  224. int rc;
  225. int activate_fw_token;
  226. rc = pseries_devicetree_update();
  227. if (rc) {
  228. printk(KERN_ERR "Initial post-mobility device tree update "
  229. "failed: %d\n", rc);
  230. return;
  231. }
  232. activate_fw_token = rtas_token("ibm,activate-firmware");
  233. if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
  234. printk(KERN_ERR "Could not make post-mobility "
  235. "activate-fw call.\n");
  236. return;
  237. }
  238. rc = rtas_call(activate_fw_token, 0, 1, NULL);
  239. if (!rc) {
  240. rc = pseries_devicetree_update();
  241. if (rc)
  242. printk(KERN_ERR "Secondary post-mobility device tree "
  243. "update failed: %d\n", rc);
  244. } else {
  245. printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
  246. return;
  247. }
  248. return;
  249. }
  250. static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. struct rtas_args args;
  254. u64 streamid;
  255. int rc;
  256. rc = strict_strtoull(buf, 0, &streamid);
  257. if (rc)
  258. return rc;
  259. memset(&args, 0, sizeof(args));
  260. args.token = rtas_token("ibm,suspend-me");
  261. args.nargs = 2;
  262. args.nret = 1;
  263. args.args[0] = streamid >> 32 ;
  264. args.args[1] = streamid & 0xffffffff;
  265. args.rets = &args.args[args.nargs];
  266. do {
  267. args.rets[0] = 0;
  268. rc = rtas_ibm_suspend_me(&args);
  269. if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE)
  270. ssleep(1);
  271. } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE);
  272. if (rc)
  273. return rc;
  274. else if (args.rets[0])
  275. return args.rets[0];
  276. post_mobility_fixup();
  277. return count;
  278. }
  279. static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
  280. static int __init mobility_sysfs_init(void)
  281. {
  282. int rc;
  283. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  284. if (!mobility_kobj)
  285. return -ENOMEM;
  286. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  287. return rc;
  288. }
  289. device_initcall(mobility_sysfs_init);