blob: 4c184ceb2450bab7690dce713c5e5c2b803be03c [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
21#include <asm/rtas.h>
22#include "pseries.h"
23
24static struct kobject *mobility_kobj;
25
26struct update_props_workarea {
27 u32 phandle;
28 u32 state;
29 u64 reserved;
30 u32 nprops;
31};
32
33#define NODE_ACTION_MASK 0xff000000
34#define NODE_COUNT_MASK 0x00ffffff
35
36#define DELETE_DT_NODE 0x01000000
37#define UPDATE_DT_NODE 0x02000000
38#define ADD_DT_NODE 0x03000000
39
Nathan Fontenot762ec152013-04-24 05:47:11 +000040#define MIGRATION_SCOPE (1)
41
42static int mobility_rtas_call(int token, char *buf, s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +000043{
44 int rc;
45
46 spin_lock(&rtas_data_buf_lock);
47
48 memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
Nathan Fontenot762ec152013-04-24 05:47:11 +000049 rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +000050 memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
51
52 spin_unlock(&rtas_data_buf_lock);
53 return rc;
54}
55
56static int delete_dt_node(u32 phandle)
57{
58 struct device_node *dn;
59
60 dn = of_find_node_by_phandle(phandle);
61 if (!dn)
62 return -ENOENT;
63
64 dlpar_detach_node(dn);
65 return 0;
66}
67
68static int update_dt_property(struct device_node *dn, struct property **prop,
69 const char *name, u32 vd, char *value)
70{
71 struct property *new_prop = *prop;
Nathan Fontenot410bccf2010-09-10 09:42:36 +000072 int more = 0;
73
74 /* A negative 'vd' value indicates that only part of the new property
75 * value is contained in the buffer and we need to call
76 * ibm,update-properties again to get the rest of the value.
77 *
78 * A negative value is also the two's compliment of the actual value.
79 */
80 if (vd & 0x80000000) {
81 vd = ~vd + 1;
82 more = 1;
83 }
84
85 if (new_prop) {
86 /* partial property fixup */
87 char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
88 if (!new_data)
89 return -ENOMEM;
90
91 memcpy(new_data, new_prop->value, new_prop->length);
92 memcpy(new_data + new_prop->length, value, vd);
93
94 kfree(new_prop->value);
95 new_prop->value = new_data;
96 new_prop->length += vd;
97 } else {
98 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
99 if (!new_prop)
100 return -ENOMEM;
101
102 new_prop->name = kstrdup(name, GFP_KERNEL);
103 if (!new_prop->name) {
104 kfree(new_prop);
105 return -ENOMEM;
106 }
107
108 new_prop->length = vd;
109 new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
110 if (!new_prop->value) {
111 kfree(new_prop->name);
112 kfree(new_prop);
113 return -ENOMEM;
114 }
115
116 memcpy(new_prop->value, value, vd);
117 *prop = new_prop;
118 }
119
120 if (!more) {
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000121 of_update_property(dn, new_prop);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000122 new_prop = NULL;
123 }
124
125 return 0;
126}
127
Nathan Fontenot762ec152013-04-24 05:47:11 +0000128static int update_dt_node(u32 phandle, s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000129{
130 struct update_props_workarea *upwa;
131 struct device_node *dn;
132 struct property *prop = NULL;
133 int i, rc;
134 char *prop_data;
135 char *rtas_buf;
136 int update_properties_token;
137
138 update_properties_token = rtas_token("ibm,update-properties");
139 if (update_properties_token == RTAS_UNKNOWN_SERVICE)
140 return -EINVAL;
141
142 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
143 if (!rtas_buf)
144 return -ENOMEM;
145
146 dn = of_find_node_by_phandle(phandle);
147 if (!dn) {
148 kfree(rtas_buf);
149 return -ENOENT;
150 }
151
152 upwa = (struct update_props_workarea *)&rtas_buf[0];
153 upwa->phandle = phandle;
154
155 do {
Nathan Fontenot762ec152013-04-24 05:47:11 +0000156 rc = mobility_rtas_call(update_properties_token, rtas_buf,
157 scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000158 if (rc < 0)
159 break;
160
161 prop_data = rtas_buf + sizeof(*upwa);
162
163 for (i = 0; i < upwa->nprops; i++) {
164 char *prop_name;
165 u32 vd;
166
167 prop_name = prop_data + 1;
168 prop_data += strlen(prop_name) + 1;
169 vd = *prop_data++;
170
171 switch (vd) {
172 case 0x00000000:
173 /* name only property, nothing to do */
174 break;
175
176 case 0x80000000:
177 prop = of_find_property(dn, prop_name, NULL);
Nathan Fontenot79d1c712012-10-02 16:58:46 +0000178 of_remove_property(dn, prop);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000179 prop = NULL;
180 break;
181
182 default:
183 rc = update_dt_property(dn, &prop, prop_name,
184 vd, prop_data);
185 if (rc) {
186 printk(KERN_ERR "Could not update %s"
187 " property\n", prop_name);
188 }
189
190 prop_data += vd;
191 }
192 }
193 } while (rc == 1);
194
195 of_node_put(dn);
196 kfree(rtas_buf);
197 return 0;
198}
199
200static int add_dt_node(u32 parent_phandle, u32 drc_index)
201{
202 struct device_node *dn;
203 struct device_node *parent_dn;
204 int rc;
205
206 dn = dlpar_configure_connector(drc_index);
207 if (!dn)
208 return -ENOENT;
209
210 parent_dn = of_find_node_by_phandle(parent_phandle);
211 if (!parent_dn) {
212 dlpar_free_cc_nodes(dn);
213 return -ENOENT;
214 }
215
216 dn->parent = parent_dn;
217 rc = dlpar_attach_node(dn);
218 if (rc)
219 dlpar_free_cc_nodes(dn);
220
221 of_node_put(parent_dn);
222 return rc;
223}
224
Nathan Fontenot762ec152013-04-24 05:47:11 +0000225int pseries_devicetree_update(s32 scope)
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000226{
227 char *rtas_buf;
228 u32 *data;
229 int update_nodes_token;
230 int rc;
231
232 update_nodes_token = rtas_token("ibm,update-nodes");
233 if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
234 return -EINVAL;
235
236 rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
237 if (!rtas_buf)
238 return -ENOMEM;
239
240 do {
Nathan Fontenot762ec152013-04-24 05:47:11 +0000241 rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000242 if (rc && rc != 1)
243 break;
244
245 data = (u32 *)rtas_buf + 4;
246 while (*data & NODE_ACTION_MASK) {
247 int i;
248 u32 action = *data & NODE_ACTION_MASK;
249 int node_count = *data & NODE_COUNT_MASK;
250
251 data++;
252
253 for (i = 0; i < node_count; i++) {
254 u32 phandle = *data++;
255 u32 drc_index;
256
257 switch (action) {
258 case DELETE_DT_NODE:
259 delete_dt_node(phandle);
260 break;
261 case UPDATE_DT_NODE:
Nathan Fontenot762ec152013-04-24 05:47:11 +0000262 update_dt_node(phandle, scope);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000263 break;
264 case ADD_DT_NODE:
265 drc_index = *data++;
266 add_dt_node(phandle, drc_index);
267 break;
268 }
269 }
270 }
271 } while (rc == 1);
272
273 kfree(rtas_buf);
274 return rc;
275}
276
277void post_mobility_fixup(void)
278{
279 int rc;
280 int activate_fw_token;
281
Nathan Fontenot762ec152013-04-24 05:47:11 +0000282 rc = pseries_devicetree_update(MIGRATION_SCOPE);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000283 if (rc) {
284 printk(KERN_ERR "Initial post-mobility device tree update "
285 "failed: %d\n", rc);
286 return;
287 }
288
289 activate_fw_token = rtas_token("ibm,activate-firmware");
290 if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
291 printk(KERN_ERR "Could not make post-mobility "
292 "activate-fw call.\n");
293 return;
294 }
295
296 rc = rtas_call(activate_fw_token, 0, 1, NULL);
297 if (!rc) {
Nathan Fontenot762ec152013-04-24 05:47:11 +0000298 rc = pseries_devicetree_update(MIGRATION_SCOPE);
Nathan Fontenot410bccf2010-09-10 09:42:36 +0000299 if (rc)
300 printk(KERN_ERR "Secondary post-mobility device tree "
301 "update failed: %d\n", rc);
302 } else {
303 printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
304 return;
305 }
306
307 return;
308}
309
310static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
311 const char *buf, size_t count)
312{
313 struct rtas_args args;
314 u64 streamid;
315 int rc;
316
317 rc = strict_strtoull(buf, 0, &streamid);
318 if (rc)
319 return rc;
320
321 memset(&args, 0, sizeof(args));
322 args.token = rtas_token("ibm,suspend-me");
323 args.nargs = 2;
324 args.nret = 1;
325
326 args.args[0] = streamid >> 32 ;
327 args.args[1] = streamid & 0xffffffff;
328 args.rets = &args.args[args.nargs];
329
330 do {
331 args.rets[0] = 0;
332 rc = rtas_ibm_suspend_me(&args);
333 if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE)
334 ssleep(1);
335 } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE);
336
337 if (rc)
338 return rc;
339 else if (args.rets[0])
340 return args.rets[0];
341
342 post_mobility_fixup();
343 return count;
344}
345
346static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
347
348static int __init mobility_sysfs_init(void)
349{
350 int rc;
351
352 mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
353 if (!mobility_kobj)
354 return -ENOMEM;
355
356 rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
357
358 return rc;
359}
360device_initcall(mobility_sysfs_init);