mobility.c 9.4 KB

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