blob: f7042ad492bafba5ac21e3db9ce24977fd527169 [file] [log] [blame]
Nathan Fontenot410bccf2010-09-10 09:42:36 +00001/*
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
12#include <linux/kernel.h>
13#include <linux/kobject.h>
14#include <linux/smp.h>
Paul Gortmakerb56eade2011-05-27 13:27:45 -040015#include <linux/stat.h>
Nathan Fontenot410bccf2010-09-10 09:42:36 +000016#include <linux/completion.h>
17#include <linux/device.h>
18#include <linux/delay.h>
19#include <linux/slab.h>
20
Michael Ellerman8e83e902014-07-16 12:02:43 +100021#include <asm/machdep.h>
Nathan Fontenot410bccf2010-09-10 09:42:36 +000022#include <asm/rtas.h>
23#include "pseries.h"
24
25static struct kobject *mobility_kobj;
26
27struct update_props_workarea {
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -080028 __be32 phandle;
29 __be32 state;
30 __be64 reserved;
31 __be32 nprops;
Tyrel Datwylerd0ef4402013-08-14 22:23:47 -070032} __packed;
Nathan Fontenot410bccf2010-09-10 09:42:36 +000033
34#define NODE_ACTION_MASK 0xff000000
35#define NODE_COUNT_MASK 0x00ffffff
36
37#define DELETE_DT_NODE 0x01000000
38#define UPDATE_DT_NODE 0x02000000
39#define ADD_DT_NODE 0x03000000
40
Nathan Fontenot762ec152013-04-24 05:47:11 +000041#define MIGRATION_SCOPE (1)
John Allen675d8ee2017-01-06 13:28:54 -060042#define PRRN_SCOPE -2
Nathan Fontenot762ec152013-04-24 05:47:11 +000043
44static int mobility_rtas_call(int token, char *buf, s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +000045{
46 int rc;
47
48 spin_lock(&rtas_data_buf_lock);
49
50 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
Nathan Fontenot762ec152013-04-24 05:47:11 +000051 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +000052 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
53
54 spin_unlock(&rtas_data_buf_lock);
55 return rc;
56}
57
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -080058static int delete_dt_node(__be32 phandle)
Nathan Fontenot410bccf2010-09-10 09:42:36 +000059{
60 struct device_node *dn;
61
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -080062 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
Nathan Fontenot410bccf2010-09-10 09:42:36 +000063 if (!dn)
64 return -ENOENT;
65
66 dlpar_detach_node(dn);
Tyrel Datwyler14cd820a2013-08-14 22:23:51 -070067 of_node_put(dn);
Nathan Fontenot410bccf2010-09-10 09:42:36 +000068 return 0;
69}
70
71static int update_dt_property(struct device_node *dn, struct property **prop,
72 const char *name, u32 vd, char *value)
73{
74 struct property *new_prop = *prop;
Nathan Fontenot410bccf2010-09-10 09:42:36 +000075 int more = 0;
76
77 /* A negative 'vd' value indicates that only part of the new property
78 * value is contained in the buffer and we need to call
79 * ibm,update-properties again to get the rest of the value.
80 *
81 * A negative value is also the two's compliment of the actual value.
82 */
83 if (vd & 0x80000000) {
84 vd = ~vd + 1;
85 more = 1;
86 }
87
88 if (new_prop) {
89 /* partial property fixup */
90 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
91 if (!new_data)
92 return -ENOMEM;
93
94 memcpy(new_data, new_prop->value, new_prop->length);
95 memcpy(new_data + new_prop->length, value, vd);
96
97 kfree(new_prop->value);
98 new_prop->value = new_data;
99 new_prop->length += vd;
100 } else {
101 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
102 if (!new_prop)
103 return -ENOMEM;
104
105 new_prop->name = kstrdup(name, GFP_KERNEL);
106 if (!new_prop->name) {
107 kfree(new_prop);
108 return -ENOMEM;
109 }
110
111 new_prop->length = vd;
112 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
113 if (!new_prop->value) {
114 kfree(new_prop->name);
115 kfree(new_prop);
116 return -ENOMEM;
117 }
118
119 memcpy(new_prop->value, value, vd);
120 *prop = new_prop;
121 }
122
123 if (!more) {
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000124 of_update_property(dn, new_prop);
Tyrel Datwylerd8e533b2013-08-14 22:23:45 -0700125 *prop = NULL;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000126 }
127
128 return 0;
129}
130
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800131static int update_dt_node(__be32 phandle, s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000132{
133 struct update_props_workarea *upwa;
134 struct device_node *dn;
135 struct property *prop = NULL;
Tyrel Datwyler638a4052013-08-14 22:23:46 -0700136 int i, rc, rtas_rc;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000137 char *prop_data;
138 char *rtas_buf;
139 int update_properties_token;
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800140 u32 nprops;
Nathan Fontenot2e9b7b02013-04-24 05:49:36 +0000141 u32 vd;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000142
143 update_properties_token = rtas_token("ibm,update-properties");
144 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
145 return -EINVAL;
146
147 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
148 if (!rtas_buf)
149 return -ENOMEM;
150
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800151 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000152 if (!dn) {
153 kfree(rtas_buf);
154 return -ENOENT;
155 }
156
157 upwa = (struct update_props_workarea *)&rtas_buf[0];
158 upwa->phandle = phandle;
159
160 do {
Tyrel Datwyler638a4052013-08-14 22:23:46 -0700161 rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
Nathan Fontenot762ec152013-04-24 05:47:11 +0000162 scope);
Tyrel Datwyler638a4052013-08-14 22:23:46 -0700163 if (rtas_rc < 0)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000164 break;
165
166 prop_data = rtas_buf + sizeof(*upwa);
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800167 nprops = be32_to_cpu(upwa->nprops);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000168
Tyrel Datwylerc8f5a572013-08-14 22:23:48 -0700169 /* On the first call to ibm,update-properties for a node the
170 * the first property value descriptor contains an empty
171 * property name, the property value length encoded as u32,
172 * and the property value is the node path being updated.
Nathan Fontenot2e9b7b02013-04-24 05:49:36 +0000173 */
Tyrel Datwylerc8f5a572013-08-14 22:23:48 -0700174 if (*prop_data == 0) {
175 prop_data++;
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800176 vd = be32_to_cpu(*(__be32 *)prop_data);
Tyrel Datwylerc8f5a572013-08-14 22:23:48 -0700177 prop_data += vd + sizeof(vd);
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800178 nprops--;
Tyrel Datwylerc8f5a572013-08-14 22:23:48 -0700179 }
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000180
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800181 for (i = 0; i < nprops; i++) {
Nathan Fontenot2e9b7b02013-04-24 05:49:36 +0000182 char *prop_name;
183
184 prop_name = prop_data;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000185 prop_data += strlen(prop_name) + 1;
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800186 vd = be32_to_cpu(*(__be32 *)prop_data);
Nathan Fontenot2e9b7b02013-04-24 05:49:36 +0000187 prop_data += sizeof(vd);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000188
189 switch (vd) {
190 case 0x00000000:
191 /* name only property, nothing to do */
192 break;
193
194 case 0x80000000:
Suraj Jitindar Singh925e2d12016-04-28 15:34:55 +1000195 of_remove_property(dn, of_find_property(dn,
196 prop_name, NULL));
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000197 prop = NULL;
198 break;
199
200 default:
201 rc = update_dt_property(dn, &prop, prop_name,
202 vd, prop_data);
203 if (rc) {
204 printk(KERN_ERR "Could not update %s"
205 " property\n", prop_name);
206 }
207
208 prop_data += vd;
209 }
210 }
Tyrel Datwyler638a4052013-08-14 22:23:46 -0700211 } while (rtas_rc == 1);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000212
213 of_node_put(dn);
214 kfree(rtas_buf);
215 return 0;
216}
217
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800218static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000219{
220 struct device_node *dn;
221 struct device_node *parent_dn;
222 int rc;
223
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800224 parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
Tyrel Datwyler8d5ff322013-08-14 22:23:50 -0700225 if (!parent_dn)
226 return -ENOENT;
227
228 dn = dlpar_configure_connector(drc_index, parent_dn);
Tyrel Datwylerb537ca62017-09-20 17:02:52 -0400229 if (!dn) {
230 of_node_put(parent_dn);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000231 return -ENOENT;
Tyrel Datwylerb537ca62017-09-20 17:02:52 -0400232 }
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000233
Rob Herring215ee762017-08-21 10:16:49 -0500234 rc = dlpar_attach_node(dn, parent_dn);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000235 if (rc)
236 dlpar_free_cc_nodes(dn);
237
238 of_node_put(parent_dn);
239 return rc;
240}
241
John Allen675d8ee2017-01-06 13:28:54 -0600242static void prrn_update_node(__be32 phandle)
243{
244 struct pseries_hp_errorlog *hp_elog;
245 struct device_node *dn;
246
247 /*
248 * If a node is found from a the given phandle, the phandle does not
249 * represent the drc index of an LMB and we can ignore.
250 */
251 dn = of_find_node_by_phandle(be32_to_cpu(phandle));
252 if (dn) {
253 of_node_put(dn);
254 return;
255 }
256
257 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
258 if(!hp_elog)
259 return;
260
261 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
262 hp_elog->action = PSERIES_HP_ELOG_ACTION_READD;
263 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
264 hp_elog->_drc_u.drc_index = phandle;
265
266 queue_hotplug_event(hp_elog, NULL, NULL);
267
268 kfree(hp_elog);
269}
270
Nathan Fontenot762ec152013-04-24 05:47:11 +0000271int pseries_devicetree_update(s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000272{
273 char *rtas_buf;
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800274 __be32 *data;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000275 int update_nodes_token;
276 int rc;
277
278 update_nodes_token = rtas_token("ibm,update-nodes");
279 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
280 return -EINVAL;
281
282 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
283 if (!rtas_buf)
284 return -ENOMEM;
285
286 do {
Nathan Fontenot762ec152013-04-24 05:47:11 +0000287 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000288 if (rc && rc != 1)
289 break;
290
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800291 data = (__be32 *)rtas_buf + 4;
292 while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000293 int i;
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800294 u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
295 u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000296
297 data++;
298
299 for (i = 0; i < node_count; i++) {
Tyrel Datwylerf6ff0412015-03-04 11:59:33 -0800300 __be32 phandle = *data++;
301 __be32 drc_index;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000302
303 switch (action) {
304 case DELETE_DT_NODE:
305 delete_dt_node(phandle);
306 break;
307 case UPDATE_DT_NODE:
Nathan Fontenot762ec152013-04-24 05:47:11 +0000308 update_dt_node(phandle, scope);
John Allen675d8ee2017-01-06 13:28:54 -0600309
310 if (scope == PRRN_SCOPE)
311 prrn_update_node(phandle);
312
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000313 break;
314 case ADD_DT_NODE:
315 drc_index = *data++;
316 add_dt_node(phandle, drc_index);
317 break;
318 }
319 }
320 }
321 } while (rc == 1);
322
323 kfree(rtas_buf);
324 return rc;
325}
326
327void post_mobility_fixup(void)
328{
329 int rc;
330 int activate_fw_token;
331
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000332 activate_fw_token = rtas_token("ibm,activate-firmware");
333 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
334 printk(KERN_ERR "Could not make post-mobility "
335 "activate-fw call.\n");
336 return;
337 }
338
Haren Myneni39a33b52014-02-19 12:56:52 -0800339 do {
340 rc = rtas_call(activate_fw_token, 0, 1, NULL);
341 } while (rtas_busy_delay(rc));
342
343 if (rc)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000344 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
Haren Myneni39a33b52014-02-19 12:56:52 -0800345
346 rc = pseries_devicetree_update(MIGRATION_SCOPE);
347 if (rc)
348 printk(KERN_ERR "Post-mobility device tree update "
349 "failed: %d\n", rc);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000350
351 return;
352}
353
Greg Kroah-Hartman6f428092017-06-06 15:32:03 +0200354static ssize_t migration_store(struct class *class,
355 struct class_attribute *attr, const char *buf,
356 size_t count)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000357{
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000358 u64 streamid;
359 int rc;
360
Daniel Walter1618bd52014-08-08 14:24:01 -0700361 rc = kstrtou64(buf, 0, &streamid);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000362 if (rc)
363 return rc;
364
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000365 do {
Tyrel Datwylerc03e7372015-03-27 12:47:25 -0700366 rc = rtas_ibm_suspend_me(streamid);
367 if (rc == -EAGAIN)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000368 ssleep(1);
Tyrel Datwylerc03e7372015-03-27 12:47:25 -0700369 } while (rc == -EAGAIN);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000370
371 if (rc)
372 return rc;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000373
374 post_mobility_fixup();
375 return count;
376}
377
Tyrel Datwyler288a2982015-03-04 18:25:38 -0800378/*
379 * Used by drmgr to determine the kernel behavior of the migration interface.
380 *
381 * Version 1: Performs all PAPR requirements for migration including
382 * firmware activation and device tree update.
383 */
384#define MIGRATION_API_VERSION 1
385
Greg Kroah-Hartman6f428092017-06-06 15:32:03 +0200386static CLASS_ATTR_WO(migration);
Tyrel Datwyler288a2982015-03-04 18:25:38 -0800387static CLASS_ATTR_STRING(api_version, S_IRUGO, __stringify(MIGRATION_API_VERSION));
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000388
389static int __init mobility_sysfs_init(void)
390{
391 int rc;
392
393 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
394 if (!mobility_kobj)
395 return -ENOMEM;
396
397 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
Tyrel Datwyler288a2982015-03-04 18:25:38 -0800398 if (rc)
399 pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000400
Tyrel Datwyler288a2982015-03-04 18:25:38 -0800401 rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
402 if (rc)
403 pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);
404
405 return 0;
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000406}
Michael Ellerman8e83e902014-07-16 12:02:43 +1000407machine_device_initcall(pseries, mobility_sysfs_init);