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