mobility.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <linux/stringify.h>
  20. #include <asm/machdep.h>
  21. #include <asm/rtas.h>
  22. #include "pseries.h"
  23. static struct kobject *mobility_kobj;
  24. struct update_props_workarea {
  25. __be32 phandle;
  26. __be32 state;
  27. __be64 reserved;
  28. __be32 nprops;
  29. } __packed;
  30. #define NODE_ACTION_MASK 0xff000000
  31. #define NODE_COUNT_MASK 0x00ffffff
  32. #define DELETE_DT_NODE 0x01000000
  33. #define UPDATE_DT_NODE 0x02000000
  34. #define ADD_DT_NODE 0x03000000
  35. #define MIGRATION_SCOPE (1)
  36. #define PRRN_SCOPE -2
  37. static int mobility_rtas_call(int token, char *buf, s32 scope)
  38. {
  39. int rc;
  40. spin_lock(&rtas_data_buf_lock);
  41. memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
  42. rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
  43. memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
  44. spin_unlock(&rtas_data_buf_lock);
  45. return rc;
  46. }
  47. static int delete_dt_node(__be32 phandle)
  48. {
  49. struct device_node *dn;
  50. dn = of_find_node_by_phandle(be32_to_cpu(phandle));
  51. if (!dn)
  52. return -ENOENT;
  53. dlpar_detach_node(dn);
  54. of_node_put(dn);
  55. return 0;
  56. }
  57. static int update_dt_property(struct device_node *dn, struct property **prop,
  58. const char *name, u32 vd, char *value)
  59. {
  60. struct property *new_prop = *prop;
  61. int more = 0;
  62. /* A negative 'vd' value indicates that only part of the new property
  63. * value is contained in the buffer and we need to call
  64. * ibm,update-properties again to get the rest of the value.
  65. *
  66. * A negative value is also the two's compliment of the actual value.
  67. */
  68. if (vd & 0x80000000) {
  69. vd = ~vd + 1;
  70. more = 1;
  71. }
  72. if (new_prop) {
  73. /* partial property fixup */
  74. char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
  75. if (!new_data)
  76. return -ENOMEM;
  77. memcpy(new_data, new_prop->value, new_prop->length);
  78. memcpy(new_data + new_prop->length, value, vd);
  79. kfree(new_prop->value);
  80. new_prop->value = new_data;
  81. new_prop->length += vd;
  82. } else {
  83. new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
  84. if (!new_prop)
  85. return -ENOMEM;
  86. new_prop->name = kstrdup(name, GFP_KERNEL);
  87. if (!new_prop->name) {
  88. kfree(new_prop);
  89. return -ENOMEM;
  90. }
  91. new_prop->length = vd;
  92. new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
  93. if (!new_prop->value) {
  94. kfree(new_prop->name);
  95. kfree(new_prop);
  96. return -ENOMEM;
  97. }
  98. memcpy(new_prop->value, value, vd);
  99. *prop = new_prop;
  100. }
  101. if (!more) {
  102. of_update_property(dn, new_prop);
  103. *prop = NULL;
  104. }
  105. return 0;
  106. }
  107. static int update_dt_node(__be32 phandle, s32 scope)
  108. {
  109. struct update_props_workarea *upwa;
  110. struct device_node *dn;
  111. struct property *prop = NULL;
  112. int i, rc, rtas_rc;
  113. char *prop_data;
  114. char *rtas_buf;
  115. int update_properties_token;
  116. u32 nprops;
  117. u32 vd;
  118. update_properties_token = rtas_token("ibm,update-properties");
  119. if (update_properties_token == RTAS_UNKNOWN_SERVICE)
  120. return -EINVAL;
  121. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  122. if (!rtas_buf)
  123. return -ENOMEM;
  124. dn = of_find_node_by_phandle(be32_to_cpu(phandle));
  125. if (!dn) {
  126. kfree(rtas_buf);
  127. return -ENOENT;
  128. }
  129. upwa = (struct update_props_workarea *)&rtas_buf[0];
  130. upwa->phandle = phandle;
  131. do {
  132. rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
  133. scope);
  134. if (rtas_rc < 0)
  135. break;
  136. prop_data = rtas_buf + sizeof(*upwa);
  137. nprops = be32_to_cpu(upwa->nprops);
  138. /* On the first call to ibm,update-properties for a node the
  139. * the first property value descriptor contains an empty
  140. * property name, the property value length encoded as u32,
  141. * and the property value is the node path being updated.
  142. */
  143. if (*prop_data == 0) {
  144. prop_data++;
  145. vd = be32_to_cpu(*(__be32 *)prop_data);
  146. prop_data += vd + sizeof(vd);
  147. nprops--;
  148. }
  149. for (i = 0; i < nprops; i++) {
  150. char *prop_name;
  151. prop_name = prop_data;
  152. prop_data += strlen(prop_name) + 1;
  153. vd = be32_to_cpu(*(__be32 *)prop_data);
  154. prop_data += sizeof(vd);
  155. switch (vd) {
  156. case 0x00000000:
  157. /* name only property, nothing to do */
  158. break;
  159. case 0x80000000:
  160. of_remove_property(dn, of_find_property(dn,
  161. prop_name, NULL));
  162. prop = NULL;
  163. break;
  164. default:
  165. rc = update_dt_property(dn, &prop, prop_name,
  166. vd, prop_data);
  167. if (rc) {
  168. printk(KERN_ERR "Could not update %s"
  169. " property\n", prop_name);
  170. }
  171. prop_data += vd;
  172. }
  173. }
  174. } while (rtas_rc == 1);
  175. of_node_put(dn);
  176. kfree(rtas_buf);
  177. return 0;
  178. }
  179. static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
  180. {
  181. struct device_node *dn;
  182. struct device_node *parent_dn;
  183. int rc;
  184. parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
  185. if (!parent_dn)
  186. return -ENOENT;
  187. dn = dlpar_configure_connector(drc_index, parent_dn);
  188. if (!dn) {
  189. of_node_put(parent_dn);
  190. return -ENOENT;
  191. }
  192. rc = dlpar_attach_node(dn, parent_dn);
  193. if (rc)
  194. dlpar_free_cc_nodes(dn);
  195. of_node_put(parent_dn);
  196. return rc;
  197. }
  198. static void prrn_update_node(__be32 phandle)
  199. {
  200. struct pseries_hp_errorlog hp_elog;
  201. struct device_node *dn;
  202. /*
  203. * If a node is found from a the given phandle, the phandle does not
  204. * represent the drc index of an LMB and we can ignore.
  205. */
  206. dn = of_find_node_by_phandle(be32_to_cpu(phandle));
  207. if (dn) {
  208. of_node_put(dn);
  209. return;
  210. }
  211. hp_elog.resource = PSERIES_HP_ELOG_RESOURCE_MEM;
  212. hp_elog.action = PSERIES_HP_ELOG_ACTION_READD;
  213. hp_elog.id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
  214. hp_elog._drc_u.drc_index = phandle;
  215. handle_dlpar_errorlog(&hp_elog);
  216. }
  217. int pseries_devicetree_update(s32 scope)
  218. {
  219. char *rtas_buf;
  220. __be32 *data;
  221. int update_nodes_token;
  222. int rc;
  223. update_nodes_token = rtas_token("ibm,update-nodes");
  224. if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
  225. return -EINVAL;
  226. rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
  227. if (!rtas_buf)
  228. return -ENOMEM;
  229. do {
  230. rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
  231. if (rc && rc != 1)
  232. break;
  233. data = (__be32 *)rtas_buf + 4;
  234. while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
  235. int i;
  236. u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
  237. u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
  238. data++;
  239. for (i = 0; i < node_count; i++) {
  240. __be32 phandle = *data++;
  241. __be32 drc_index;
  242. switch (action) {
  243. case DELETE_DT_NODE:
  244. delete_dt_node(phandle);
  245. break;
  246. case UPDATE_DT_NODE:
  247. update_dt_node(phandle, scope);
  248. if (scope == PRRN_SCOPE)
  249. prrn_update_node(phandle);
  250. break;
  251. case ADD_DT_NODE:
  252. drc_index = *data++;
  253. add_dt_node(phandle, drc_index);
  254. break;
  255. }
  256. }
  257. }
  258. } while (rc == 1);
  259. kfree(rtas_buf);
  260. return rc;
  261. }
  262. void post_mobility_fixup(void)
  263. {
  264. int rc;
  265. int activate_fw_token;
  266. activate_fw_token = rtas_token("ibm,activate-firmware");
  267. if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
  268. printk(KERN_ERR "Could not make post-mobility "
  269. "activate-fw call.\n");
  270. return;
  271. }
  272. do {
  273. rc = rtas_call(activate_fw_token, 0, 1, NULL);
  274. } while (rtas_busy_delay(rc));
  275. if (rc)
  276. printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
  277. rc = pseries_devicetree_update(MIGRATION_SCOPE);
  278. if (rc)
  279. printk(KERN_ERR "Post-mobility device tree update "
  280. "failed: %d\n", rc);
  281. /* Possibly switch to a new RFI flush type */
  282. pseries_setup_rfi_flush();
  283. return;
  284. }
  285. static ssize_t migration_store(struct class *class,
  286. struct class_attribute *attr, const char *buf,
  287. size_t count)
  288. {
  289. u64 streamid;
  290. int rc;
  291. rc = kstrtou64(buf, 0, &streamid);
  292. if (rc)
  293. return rc;
  294. stop_topology_update();
  295. do {
  296. rc = rtas_ibm_suspend_me(streamid);
  297. if (rc == -EAGAIN)
  298. ssleep(1);
  299. } while (rc == -EAGAIN);
  300. if (rc)
  301. return rc;
  302. post_mobility_fixup();
  303. start_topology_update();
  304. return count;
  305. }
  306. /*
  307. * Used by drmgr to determine the kernel behavior of the migration interface.
  308. *
  309. * Version 1: Performs all PAPR requirements for migration including
  310. * firmware activation and device tree update.
  311. */
  312. #define MIGRATION_API_VERSION 1
  313. static CLASS_ATTR_WO(migration);
  314. static CLASS_ATTR_STRING(api_version, 0444, __stringify(MIGRATION_API_VERSION));
  315. static int __init mobility_sysfs_init(void)
  316. {
  317. int rc;
  318. mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
  319. if (!mobility_kobj)
  320. return -ENOMEM;
  321. rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
  322. if (rc)
  323. pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
  324. rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
  325. if (rc)
  326. pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
  327. return 0;
  328. }
  329. machine_device_initcall(pseries, mobility_sysfs_init);