Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 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/kernel.h> |
| 13 | #include <linux/kobject.h> |
| 14 | #include <linux/smp.h> |
Paul Gortmaker | b56eade | 2011-05-27 13:27:45 -0400 | [diff] [blame] | 15 | #include <linux/stat.h> |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 16 | #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 | |
| 24 | static struct kobject *mobility_kobj; |
| 25 | |
| 26 | struct update_props_workarea { |
| 27 | u32 phandle; |
| 28 | u32 state; |
| 29 | u64 reserved; |
| 30 | u32 nprops; |
Tyrel Datwyler | d0ef440 | 2013-08-14 22:23:47 -0700 | [diff] [blame] | 31 | } __packed; |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 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 Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 40 | #define MIGRATION_SCOPE (1) |
| 41 | |
| 42 | static int mobility_rtas_call(int token, char *buf, s32 scope) |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 43 | { |
| 44 | int rc; |
| 45 | |
| 46 | spin_lock(&rtas_data_buf_lock); |
| 47 | |
| 48 | memcpy(rtas_data_buf, buf, RTAS_DATA_BUF_SIZE); |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 49 | rc = rtas_call(token, 2, 1, NULL, rtas_data_buf, scope); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 50 | memcpy(buf, rtas_data_buf, RTAS_DATA_BUF_SIZE); |
| 51 | |
| 52 | spin_unlock(&rtas_data_buf_lock); |
| 53 | return rc; |
| 54 | } |
| 55 | |
| 56 | static 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 | |
| 68 | static 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 Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 72 | 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 Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 121 | of_update_property(dn, new_prop); |
Tyrel Datwyler | d8e533b | 2013-08-14 22:23:45 -0700 | [diff] [blame] | 122 | *prop = NULL; |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 128 | static int update_dt_node(u32 phandle, s32 scope) |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 129 | { |
| 130 | struct update_props_workarea *upwa; |
| 131 | struct device_node *dn; |
| 132 | struct property *prop = NULL; |
Tyrel Datwyler | 638a405 | 2013-08-14 22:23:46 -0700 | [diff] [blame] | 133 | int i, rc, rtas_rc; |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 134 | char *prop_data; |
| 135 | char *rtas_buf; |
| 136 | int update_properties_token; |
Nathan Fontenot | 2e9b7b0 | 2013-04-24 05:49:36 +0000 | [diff] [blame] | 137 | u32 vd; |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 138 | |
| 139 | update_properties_token = rtas_token("ibm,update-properties"); |
| 140 | if (update_properties_token == RTAS_UNKNOWN_SERVICE) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
| 144 | if (!rtas_buf) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | dn = of_find_node_by_phandle(phandle); |
| 148 | if (!dn) { |
| 149 | kfree(rtas_buf); |
| 150 | return -ENOENT; |
| 151 | } |
| 152 | |
| 153 | upwa = (struct update_props_workarea *)&rtas_buf[0]; |
| 154 | upwa->phandle = phandle; |
| 155 | |
| 156 | do { |
Tyrel Datwyler | 638a405 | 2013-08-14 22:23:46 -0700 | [diff] [blame] | 157 | rtas_rc = mobility_rtas_call(update_properties_token, rtas_buf, |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 158 | scope); |
Tyrel Datwyler | 638a405 | 2013-08-14 22:23:46 -0700 | [diff] [blame] | 159 | if (rtas_rc < 0) |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 160 | break; |
| 161 | |
| 162 | prop_data = rtas_buf + sizeof(*upwa); |
| 163 | |
Tyrel Datwyler | c8f5a57 | 2013-08-14 22:23:48 -0700 | [diff] [blame] | 164 | /* On the first call to ibm,update-properties for a node the |
| 165 | * the first property value descriptor contains an empty |
| 166 | * property name, the property value length encoded as u32, |
| 167 | * and the property value is the node path being updated. |
Nathan Fontenot | 2e9b7b0 | 2013-04-24 05:49:36 +0000 | [diff] [blame] | 168 | */ |
Tyrel Datwyler | c8f5a57 | 2013-08-14 22:23:48 -0700 | [diff] [blame] | 169 | if (*prop_data == 0) { |
| 170 | prop_data++; |
| 171 | vd = *(u32 *)prop_data; |
| 172 | prop_data += vd + sizeof(vd); |
| 173 | upwa->nprops--; |
| 174 | } |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 175 | |
Tyrel Datwyler | c8f5a57 | 2013-08-14 22:23:48 -0700 | [diff] [blame] | 176 | for (i = 0; i < upwa->nprops; i++) { |
Nathan Fontenot | 2e9b7b0 | 2013-04-24 05:49:36 +0000 | [diff] [blame] | 177 | char *prop_name; |
| 178 | |
| 179 | prop_name = prop_data; |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 180 | prop_data += strlen(prop_name) + 1; |
Nathan Fontenot | 2e9b7b0 | 2013-04-24 05:49:36 +0000 | [diff] [blame] | 181 | vd = *(u32 *)prop_data; |
| 182 | prop_data += sizeof(vd); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 183 | |
| 184 | switch (vd) { |
| 185 | case 0x00000000: |
| 186 | /* name only property, nothing to do */ |
| 187 | break; |
| 188 | |
| 189 | case 0x80000000: |
| 190 | prop = of_find_property(dn, prop_name, NULL); |
Nathan Fontenot | 79d1c71 | 2012-10-02 16:58:46 +0000 | [diff] [blame] | 191 | of_remove_property(dn, prop); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 192 | prop = NULL; |
| 193 | break; |
| 194 | |
| 195 | default: |
| 196 | rc = update_dt_property(dn, &prop, prop_name, |
| 197 | vd, prop_data); |
| 198 | if (rc) { |
| 199 | printk(KERN_ERR "Could not update %s" |
| 200 | " property\n", prop_name); |
| 201 | } |
| 202 | |
| 203 | prop_data += vd; |
| 204 | } |
| 205 | } |
Tyrel Datwyler | 638a405 | 2013-08-14 22:23:46 -0700 | [diff] [blame] | 206 | } while (rtas_rc == 1); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 207 | |
| 208 | of_node_put(dn); |
| 209 | kfree(rtas_buf); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int add_dt_node(u32 parent_phandle, u32 drc_index) |
| 214 | { |
| 215 | struct device_node *dn; |
| 216 | struct device_node *parent_dn; |
| 217 | int rc; |
| 218 | |
Tyrel Datwyler | 8d5ff32 | 2013-08-14 22:23:50 -0700 | [diff] [blame^] | 219 | parent_dn = of_find_node_by_phandle(parent_phandle); |
| 220 | if (!parent_dn) |
| 221 | return -ENOENT; |
| 222 | |
| 223 | dn = dlpar_configure_connector(drc_index, parent_dn); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 224 | if (!dn) |
| 225 | return -ENOENT; |
| 226 | |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 227 | rc = dlpar_attach_node(dn); |
| 228 | if (rc) |
| 229 | dlpar_free_cc_nodes(dn); |
| 230 | |
| 231 | of_node_put(parent_dn); |
| 232 | return rc; |
| 233 | } |
| 234 | |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 235 | int pseries_devicetree_update(s32 scope) |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 236 | { |
| 237 | char *rtas_buf; |
| 238 | u32 *data; |
| 239 | int update_nodes_token; |
| 240 | int rc; |
| 241 | |
| 242 | update_nodes_token = rtas_token("ibm,update-nodes"); |
| 243 | if (update_nodes_token == RTAS_UNKNOWN_SERVICE) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | rtas_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); |
| 247 | if (!rtas_buf) |
| 248 | return -ENOMEM; |
| 249 | |
| 250 | do { |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 251 | rc = mobility_rtas_call(update_nodes_token, rtas_buf, scope); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 252 | if (rc && rc != 1) |
| 253 | break; |
| 254 | |
| 255 | data = (u32 *)rtas_buf + 4; |
| 256 | while (*data & NODE_ACTION_MASK) { |
| 257 | int i; |
| 258 | u32 action = *data & NODE_ACTION_MASK; |
| 259 | int node_count = *data & NODE_COUNT_MASK; |
| 260 | |
| 261 | data++; |
| 262 | |
| 263 | for (i = 0; i < node_count; i++) { |
| 264 | u32 phandle = *data++; |
| 265 | u32 drc_index; |
| 266 | |
| 267 | switch (action) { |
| 268 | case DELETE_DT_NODE: |
| 269 | delete_dt_node(phandle); |
| 270 | break; |
| 271 | case UPDATE_DT_NODE: |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 272 | update_dt_node(phandle, scope); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 273 | break; |
| 274 | case ADD_DT_NODE: |
| 275 | drc_index = *data++; |
| 276 | add_dt_node(phandle, drc_index); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } while (rc == 1); |
| 282 | |
| 283 | kfree(rtas_buf); |
| 284 | return rc; |
| 285 | } |
| 286 | |
| 287 | void post_mobility_fixup(void) |
| 288 | { |
| 289 | int rc; |
| 290 | int activate_fw_token; |
| 291 | |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 292 | rc = pseries_devicetree_update(MIGRATION_SCOPE); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 293 | if (rc) { |
| 294 | printk(KERN_ERR "Initial post-mobility device tree update " |
| 295 | "failed: %d\n", rc); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | activate_fw_token = rtas_token("ibm,activate-firmware"); |
| 300 | if (activate_fw_token == RTAS_UNKNOWN_SERVICE) { |
| 301 | printk(KERN_ERR "Could not make post-mobility " |
| 302 | "activate-fw call.\n"); |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | rc = rtas_call(activate_fw_token, 0, 1, NULL); |
| 307 | if (!rc) { |
Nathan Fontenot | 762ec15 | 2013-04-24 05:47:11 +0000 | [diff] [blame] | 308 | rc = pseries_devicetree_update(MIGRATION_SCOPE); |
Nathan Fontenot | 410bccf | 2010-09-10 09:42:36 +0000 | [diff] [blame] | 309 | if (rc) |
| 310 | printk(KERN_ERR "Secondary post-mobility device tree " |
| 311 | "update failed: %d\n", rc); |
| 312 | } else { |
| 313 | printk(KERN_ERR "Post-mobility activate-fw failed: %d\n", rc); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | static ssize_t migrate_store(struct class *class, struct class_attribute *attr, |
| 321 | const char *buf, size_t count) |
| 322 | { |
| 323 | struct rtas_args args; |
| 324 | u64 streamid; |
| 325 | int rc; |
| 326 | |
| 327 | rc = strict_strtoull(buf, 0, &streamid); |
| 328 | if (rc) |
| 329 | return rc; |
| 330 | |
| 331 | memset(&args, 0, sizeof(args)); |
| 332 | args.token = rtas_token("ibm,suspend-me"); |
| 333 | args.nargs = 2; |
| 334 | args.nret = 1; |
| 335 | |
| 336 | args.args[0] = streamid >> 32 ; |
| 337 | args.args[1] = streamid & 0xffffffff; |
| 338 | args.rets = &args.args[args.nargs]; |
| 339 | |
| 340 | do { |
| 341 | args.rets[0] = 0; |
| 342 | rc = rtas_ibm_suspend_me(&args); |
| 343 | if (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE) |
| 344 | ssleep(1); |
| 345 | } while (!rc && args.rets[0] == RTAS_NOT_SUSPENDABLE); |
| 346 | |
| 347 | if (rc) |
| 348 | return rc; |
| 349 | else if (args.rets[0]) |
| 350 | return args.rets[0]; |
| 351 | |
| 352 | post_mobility_fixup(); |
| 353 | return count; |
| 354 | } |
| 355 | |
| 356 | static CLASS_ATTR(migration, S_IWUSR, NULL, migrate_store); |
| 357 | |
| 358 | static int __init mobility_sysfs_init(void) |
| 359 | { |
| 360 | int rc; |
| 361 | |
| 362 | mobility_kobj = kobject_create_and_add("mobility", kernel_kobj); |
| 363 | if (!mobility_kobj) |
| 364 | return -ENOMEM; |
| 365 | |
| 366 | rc = sysfs_create_file(mobility_kobj, &class_attr_migration.attr); |
| 367 | |
| 368 | return rc; |
| 369 | } |
| 370 | device_initcall(mobility_sysfs_init); |