mobility.c 7.4 KB

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