mobility.c 8.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Support for Partition Mobility/Migration
 *
 * Copyright (C) 2010 Nathan Fontenot
 * Copyright (C) 2010 IBM Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/smp.h>
15
#include <linux/stat.h>
16 17 18 19 20
#include <linux/completion.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/slab.h>

21
#include <asm/machdep.h>
22 23 24 25 26 27
#include <asm/rtas.h>
#include "pseries.h"

static struct kobject *mobility_kobj;

struct update_props_workarea {
28 29 30 31
	__be32 phandle;
	__be32 state;
	__be64 reserved;
	__be32 nprops;
32
} __packed;
33 34 35 36 37 38 39 40

#define NODE_ACTION_MASK	0xff000000
#define NODE_COUNT_MASK		0x00ffffff

#define DELETE_DT_NODE	0x01000000
#define UPDATE_DT_NODE	0x02000000
#define ADD_DT_NODE	0x03000000

41 42 43
#define MIGRATION_SCOPE	(1)

static int mobility_rtas_call(int token, char *buf, s32 scope)
44 45 46 47 48 49
{
	int rc;

	spin_lock(&rtas_data_buf_lock);

	memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE);
50
	rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope);
51 52 53 54 55 56
	memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);

	spin_unlock(&rtas_data_buf_lock);
	return rc;
}

57
static int delete_dt_node(__be32 phandle)
58 59 60
{
	struct device_node *dn;

61
	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
62 63 64 65
	if (!dn)
		return -ENOENT;

	dlpar_detach_node(dn);
66
	of_node_put(dn);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	return 0;
}

static int update_dt_property(struct device_node *dn, struct property **prop,
			      const char *name, u32 vd, char *value)
{
	struct property *new_prop = *prop;
	int more = 0;

	/* A negative 'vd' value indicates that only part of the new property
	 * value is contained in the buffer and we need to call
	 * ibm,update-properties again to get the rest of the value.
	 *
	 * A negative value is also the two's compliment of the actual value.
	 */
	if (vd & 0x80000000) {
		vd = ~vd + 1;
		more = 1;
	}

	if (new_prop) {
		/* partial property fixup */
		char *new_data = kzalloc(new_prop->length + vd, GFP_KERNEL);
		if (!new_data)
			return -ENOMEM;

		memcpy(new_data, new_prop->value, new_prop->length);
		memcpy(new_data + new_prop->length, value, vd);

		kfree(new_prop->value);
		new_prop->value = new_data;
		new_prop->length += vd;
	} else {
		new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
		if (!new_prop)
			return -ENOMEM;

		new_prop->name = kstrdup(name, GFP_KERNEL);
		if (!new_prop->name) {
			kfree(new_prop);
			return -ENOMEM;
		}

		new_prop->length = vd;
		new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);
		if (!new_prop->value) {
			kfree(new_prop->name);
			kfree(new_prop);
			return -ENOMEM;
		}

		memcpy(new_prop->value, value, vd);
		*prop = new_prop;
	}

	if (!more) {
123
		of_update_property(dn, new_prop);
124
		*prop = NULL;
125 126 127 128 129
	}

	return 0;
}

130
static int update_dt_node(__be32 phandle, s32 scope)
131 132 133 134
{
	struct update_props_workarea *upwa;
	struct device_node *dn;
	struct property *prop = NULL;
135
	int i, rc, rtas_rc;
136 137 138
	char *prop_data;
	char *rtas_buf;
	int update_properties_token;
139
	u32 nprops;
140
	u32 vd;
141 142 143 144 145 146 147 148 149

	update_properties_token = rtas_token("ibm,update-properties");
	if (update_properties_token == RTAS_UNKNOWN_SERVICE)
		return -EINVAL;

	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
	if (!rtas_buf)
		return -ENOMEM;

150
	dn = of_find_node_by_phandle(be32_to_cpu(phandle));
151 152 153 154 155 156 157 158 159
	if (!dn) {
		kfree(rtas_buf);
		return -ENOENT;
	}

	upwa = (struct update_props_workarea *)&rtas_buf[0];
	upwa->phandle = phandle;

	do {
160
		rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf,
161
					scope);
162
		if (rtas_rc < 0)
163 164 165
			break;

		prop_data = rtas_buf + sizeof(*upwa);
166
		nprops = be32_to_cpu(upwa->nprops);
167

168 169 170 171
		/* On the first call to ibm,update-properties for a node the
		 * the first property value descriptor contains an empty
		 * property name, the property value length encoded as u32,
		 * and the property value is the node path being updated.
172
		 */
173 174
		if (*prop_data == 0) {
			prop_data++;
175
			vd = be32_to_cpu(*(__be32 *)prop_data);
176
			prop_data += vd + sizeof(vd);
177
			nprops--;
178
		}
179

180
		for (i = 0; i < nprops; i++) {
181 182
			char *prop_name;

183
			prop_name = prop_data;
184
			prop_data += strlen(prop_name) + 1;
185
			vd = be32_to_cpu(*(__be32 *)prop_data);
186
			prop_data += sizeof(vd);
187 188 189 190 191 192 193

			switch (vd) {
			case 0x00000000:
				/* name only property, nothing to do */
				break;

			case 0x80000000:
194 195
				of_remove_property(dn, of_find_property(dn,
							prop_name, NULL));
196 197 198 199 200 201 202 203 204 205 206 207 208 209
				prop = NULL;
				break;

			default:
				rc = update_dt_property(dn, &prop, prop_name,
							vd, prop_data);
				if (rc) {
					printk(KERN_ERR "Could not update %s"
					       " property\n", prop_name);
				}

				prop_data += vd;
			}
		}
210
	} while (rtas_rc == 1);
211 212 213 214 215 216

	of_node_put(dn);
	kfree(rtas_buf);
	return 0;
}

217
static int add_dt_node(__be32 parent_phandle, __be32 drc_index)
218 219 220 221 222
{
	struct device_node *dn;
	struct device_node *parent_dn;
	int rc;

223
	parent_dn = of_find_node_by_phandle(be32_to_cpu(parent_phandle));
224
	if (!parent_dn)
225 226
		return -ENOENT;

227 228
	dn = dlpar_configure_connector(drc_index, parent_dn);
	if (!dn)
229 230 231 232 233 234 235 236 237 238
		return -ENOENT;

	rc = dlpar_attach_node(dn);
	if (rc)
		dlpar_free_cc_nodes(dn);

	of_node_put(parent_dn);
	return rc;
}

239
int pseries_devicetree_update(s32 scope)
240 241
{
	char *rtas_buf;
242
	__be32 *data;
243 244 245 246 247 248 249 250 251 252 253 254
	int update_nodes_token;
	int rc;

	update_nodes_token = rtas_token("ibm,update-nodes");
	if (update_nodes_token == RTAS_UNKNOWN_SERVICE)
		return -EINVAL;

	rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
	if (!rtas_buf)
		return -ENOMEM;

	do {
255
		rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope);
256 257 258
		if (rc && rc != 1)
			break;

259 260
		data = (__be32 *)rtas_buf + 4;
		while (be32_to_cpu(*data) & NODE_ACTION_MASK) {
261
			int i;
262 263
			u32 action = be32_to_cpu(*data) & NODE_ACTION_MASK;
			u32 node_count = be32_to_cpu(*data) & NODE_COUNT_MASK;
264 265 266 267

			data++;

			for (i = 0; i < node_count; i++) {
268 269
				__be32 phandle = *data++;
				__be32 drc_index;
270 271 272 273 274 275

				switch (action) {
				case DELETE_DT_NODE:
					delete_dt_node(phandle);
					break;
				case UPDATE_DT_NODE:
276
					update_dt_node(phandle, scope);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
					break;
				case ADD_DT_NODE:
					drc_index = *data++;
					add_dt_node(phandle, drc_index);
					break;
				}
			}
		}
	} while (rc == 1);

	kfree(rtas_buf);
	return rc;
}

void post_mobility_fixup(void)
{
	int rc;
	int activate_fw_token;

	activate_fw_token = rtas_token("ibm,activate-firmware");
	if (activate_fw_token == RTAS_UNKNOWN_SERVICE) {
		printk(KERN_ERR "Could not make post-mobility "
		       "activate-fw call.\n");
		return;
	}

303 304 305 306 307
	do {
		rc = rtas_call(activate_fw_token, 0, 1, NULL);
	} while (rtas_busy_delay(rc));

	if (rc)
308
		printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc);
309 310 311 312 313

	rc = pseries_devicetree_update(MIGRATION_SCOPE);
	if (rc)
		printk(KERN_ERR "Post-mobility device tree update "
			"failed: %d\n", rc);
314 315 316 317 318 319 320 321 322 323

	return;
}

static ssize_t migrate_store(struct class *class, struct class_attribute *attr,
			     const char *buf, size_t count)
{
	u64 streamid;
	int rc;

324
	rc = kstrtou64(buf, 0, &streamid);
325 326 327 328
	if (rc)
		return rc;

	do {
329 330
		rc = rtas_ibm_suspend_me(streamid);
		if (rc == -EAGAIN)
331
			ssleep(1);
332
	} while (rc == -EAGAIN);
333 334 335 336 337 338 339 340

	if (rc)
		return rc;

	post_mobility_fixup();
	return count;
}

341 342 343 344 345 346 347 348
/*
 * Used by drmgr to determine the kernel behavior of the migration interface.
 *
 * Version 1: Performs all PAPR requirements for migration including
 *	firmware activation and device tree update.
 */
#define MIGRATION_API_VERSION	1

349
static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store);
350
static CLASS_ATTR_STRING(api_version, S_IRUGO, __stringify(MIGRATION_API_VERSION));
351 352 353 354 355 356 357 358 359 360

static int __init mobility_sysfs_init(void)
{
	int rc;

	mobility_kobj = kobject_create_and_add("mobility", kernel_kobj);
	if (!mobility_kobj)
		return -ENOMEM;

	rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr);
361 362
	if (rc)
		pr_err("mobility: unable to create migration sysfs file (%d)\n", rc);
363

364 365 366 367 368
	rc = sysfs_create_file(mobility_kobj, &class_attr_api_version.attr.attr);
	if (rc)
		pr_err("mobility: unable to create api_version sysfs file (%d)\n", rc);

	return 0;
369
}
370
machine_device_initcall(pseries, mobility_sysfs_init);