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