1 /* 2 * Support for dynamic reconfiguration for PCI, Memory, and CPU 3 * Hotplug and Dynamic Logical Partitioning on RPA platforms. 4 * 5 * Copyright (C) 2009 Nathan Fontenot 6 * Copyright (C) 2009 IBM Corporation 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License version 10 * 2 as published by the Free Software Foundation. 11 */ 12 13 #define pr_fmt(fmt) "dlpar: " fmt 14 15 #include <linux/kernel.h> 16 #include <linux/notifier.h> 17 #include <linux/spinlock.h> 18 #include <linux/cpu.h> 19 #include <linux/slab.h> 20 #include <linux/of.h> 21 22 #include "of_helpers.h" 23 #include "pseries.h" 24 25 #include <asm/prom.h> 26 #include <asm/machdep.h> 27 #include <linux/uaccess.h> 28 #include <asm/rtas.h> 29 30 static struct workqueue_struct *pseries_hp_wq; 31 32 struct pseries_hp_work { 33 struct work_struct work; 34 struct pseries_hp_errorlog *errlog; 35 }; 36 37 struct cc_workarea { 38 __be32 drc_index; 39 __be32 zero; 40 __be32 name_offset; 41 __be32 prop_length; 42 __be32 prop_offset; 43 }; 44 45 void dlpar_free_cc_property(struct property *prop) 46 { 47 kfree(prop->name); 48 kfree(prop->value); 49 kfree(prop); 50 } 51 52 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa) 53 { 54 struct property *prop; 55 char *name; 56 char *value; 57 58 prop = kzalloc(sizeof(*prop), GFP_KERNEL); 59 if (!prop) 60 return NULL; 61 62 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset); 63 prop->name = kstrdup(name, GFP_KERNEL); 64 65 prop->length = be32_to_cpu(ccwa->prop_length); 66 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset); 67 prop->value = kmemdup(value, prop->length, GFP_KERNEL); 68 if (!prop->value) { 69 dlpar_free_cc_property(prop); 70 return NULL; 71 } 72 73 return prop; 74 } 75 76 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa) 77 { 78 struct device_node *dn; 79 const char *name; 80 81 dn = kzalloc(sizeof(*dn), GFP_KERNEL); 82 if (!dn) 83 return NULL; 84 85 name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset); 86 dn->full_name = kstrdup(name, GFP_KERNEL); 87 if (!dn->full_name) { 88 kfree(dn); 89 return NULL; 90 } 91 92 of_node_set_flag(dn, OF_DYNAMIC); 93 of_node_init(dn); 94 95 return dn; 96 } 97 98 static void dlpar_free_one_cc_node(struct device_node *dn) 99 { 100 struct property *prop; 101 102 while (dn->properties) { 103 prop = dn->properties; 104 dn->properties = prop->next; 105 dlpar_free_cc_property(prop); 106 } 107 108 kfree(dn->full_name); 109 kfree(dn); 110 } 111 112 void dlpar_free_cc_nodes(struct device_node *dn) 113 { 114 if (dn->child) 115 dlpar_free_cc_nodes(dn->child); 116 117 if (dn->sibling) 118 dlpar_free_cc_nodes(dn->sibling); 119 120 dlpar_free_one_cc_node(dn); 121 } 122 123 #define COMPLETE 0 124 #define NEXT_SIBLING 1 125 #define NEXT_CHILD 2 126 #define NEXT_PROPERTY 3 127 #define PREV_PARENT 4 128 #define MORE_MEMORY 5 129 #define CALL_AGAIN -2 130 #define ERR_CFG_USE -9003 131 132 struct device_node *dlpar_configure_connector(__be32 drc_index, 133 struct device_node *parent) 134 { 135 struct device_node *dn; 136 struct device_node *first_dn = NULL; 137 struct device_node *last_dn = NULL; 138 struct property *property; 139 struct property *last_property = NULL; 140 struct cc_workarea *ccwa; 141 char *data_buf; 142 int cc_token; 143 int rc = -1; 144 145 cc_token = rtas_token("ibm,configure-connector"); 146 if (cc_token == RTAS_UNKNOWN_SERVICE) 147 return NULL; 148 149 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL); 150 if (!data_buf) 151 return NULL; 152 153 ccwa = (struct cc_workarea *)&data_buf[0]; 154 ccwa->drc_index = drc_index; 155 ccwa->zero = 0; 156 157 do { 158 /* Since we release the rtas_data_buf lock between configure 159 * connector calls we want to re-populate the rtas_data_buffer 160 * with the contents of the previous call. 161 */ 162 spin_lock(&rtas_data_buf_lock); 163 164 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE); 165 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL); 166 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE); 167 168 spin_unlock(&rtas_data_buf_lock); 169 170 switch (rc) { 171 case COMPLETE: 172 break; 173 174 case NEXT_SIBLING: 175 dn = dlpar_parse_cc_node(ccwa); 176 if (!dn) 177 goto cc_error; 178 179 dn->parent = last_dn->parent; 180 last_dn->sibling = dn; 181 last_dn = dn; 182 break; 183 184 case NEXT_CHILD: 185 dn = dlpar_parse_cc_node(ccwa); 186 if (!dn) 187 goto cc_error; 188 189 if (!first_dn) { 190 dn->parent = parent; 191 first_dn = dn; 192 } else { 193 dn->parent = last_dn; 194 if (last_dn) 195 last_dn->child = dn; 196 } 197 198 last_dn = dn; 199 break; 200 201 case NEXT_PROPERTY: 202 property = dlpar_parse_cc_property(ccwa); 203 if (!property) 204 goto cc_error; 205 206 if (!last_dn->properties) 207 last_dn->properties = property; 208 else 209 last_property->next = property; 210 211 last_property = property; 212 break; 213 214 case PREV_PARENT: 215 last_dn = last_dn->parent; 216 break; 217 218 case CALL_AGAIN: 219 break; 220 221 case MORE_MEMORY: 222 case ERR_CFG_USE: 223 default: 224 printk(KERN_ERR "Unexpected Error (%d) " 225 "returned from configure-connector\n", rc); 226 goto cc_error; 227 } 228 } while (rc); 229 230 cc_error: 231 kfree(data_buf); 232 233 if (rc) { 234 if (first_dn) 235 dlpar_free_cc_nodes(first_dn); 236 237 return NULL; 238 } 239 240 return first_dn; 241 } 242 243 int dlpar_attach_node(struct device_node *dn, struct device_node *parent) 244 { 245 int rc; 246 247 dn->parent = parent; 248 249 rc = of_attach_node(dn); 250 if (rc) { 251 printk(KERN_ERR "Failed to add device node %pOF\n", dn); 252 return rc; 253 } 254 255 return 0; 256 } 257 258 int dlpar_detach_node(struct device_node *dn) 259 { 260 struct device_node *child; 261 int rc; 262 263 child = of_get_next_child(dn, NULL); 264 while (child) { 265 dlpar_detach_node(child); 266 child = of_get_next_child(dn, child); 267 } 268 269 rc = of_detach_node(dn); 270 if (rc) 271 return rc; 272 273 return 0; 274 } 275 276 #define DR_ENTITY_SENSE 9003 277 #define DR_ENTITY_PRESENT 1 278 #define DR_ENTITY_UNUSABLE 2 279 #define ALLOCATION_STATE 9003 280 #define ALLOC_UNUSABLE 0 281 #define ALLOC_USABLE 1 282 #define ISOLATION_STATE 9001 283 #define ISOLATE 0 284 #define UNISOLATE 1 285 286 int dlpar_acquire_drc(u32 drc_index) 287 { 288 int dr_status, rc; 289 290 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, 291 DR_ENTITY_SENSE, drc_index); 292 if (rc || dr_status != DR_ENTITY_UNUSABLE) 293 return -1; 294 295 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE); 296 if (rc) 297 return rc; 298 299 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); 300 if (rc) { 301 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); 302 return rc; 303 } 304 305 return 0; 306 } 307 308 int dlpar_release_drc(u32 drc_index) 309 { 310 int dr_status, rc; 311 312 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status, 313 DR_ENTITY_SENSE, drc_index); 314 if (rc || dr_status != DR_ENTITY_PRESENT) 315 return -1; 316 317 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE); 318 if (rc) 319 return rc; 320 321 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE); 322 if (rc) { 323 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE); 324 return rc; 325 } 326 327 return 0; 328 } 329 330 int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog) 331 { 332 int rc; 333 334 /* pseries error logs are in BE format, convert to cpu type */ 335 switch (hp_elog->id_type) { 336 case PSERIES_HP_ELOG_ID_DRC_COUNT: 337 hp_elog->_drc_u.drc_count = 338 be32_to_cpu(hp_elog->_drc_u.drc_count); 339 break; 340 case PSERIES_HP_ELOG_ID_DRC_INDEX: 341 hp_elog->_drc_u.drc_index = 342 be32_to_cpu(hp_elog->_drc_u.drc_index); 343 break; 344 case PSERIES_HP_ELOG_ID_DRC_IC: 345 hp_elog->_drc_u.ic.count = 346 be32_to_cpu(hp_elog->_drc_u.ic.count); 347 hp_elog->_drc_u.ic.index = 348 be32_to_cpu(hp_elog->_drc_u.ic.index); 349 } 350 351 switch (hp_elog->resource) { 352 case PSERIES_HP_ELOG_RESOURCE_MEM: 353 rc = dlpar_memory(hp_elog); 354 break; 355 case PSERIES_HP_ELOG_RESOURCE_CPU: 356 rc = dlpar_cpu(hp_elog); 357 break; 358 case PSERIES_HP_ELOG_RESOURCE_PMEM: 359 rc = dlpar_hp_pmem(hp_elog); 360 break; 361 362 default: 363 pr_warn_ratelimited("Invalid resource (%d) specified\n", 364 hp_elog->resource); 365 rc = -EINVAL; 366 } 367 368 return rc; 369 } 370 371 static void pseries_hp_work_fn(struct work_struct *work) 372 { 373 struct pseries_hp_work *hp_work = 374 container_of(work, struct pseries_hp_work, work); 375 376 handle_dlpar_errorlog(hp_work->errlog); 377 378 kfree(hp_work->errlog); 379 kfree((void *)work); 380 } 381 382 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog) 383 { 384 struct pseries_hp_work *work; 385 struct pseries_hp_errorlog *hp_errlog_copy; 386 387 hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog), 388 GFP_KERNEL); 389 memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog)); 390 391 work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL); 392 if (work) { 393 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn); 394 work->errlog = hp_errlog_copy; 395 queue_work(pseries_hp_wq, (struct work_struct *)work); 396 } else { 397 kfree(hp_errlog_copy); 398 } 399 } 400 401 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog) 402 { 403 char *arg; 404 405 arg = strsep(cmd, " "); 406 if (!arg) 407 return -EINVAL; 408 409 if (sysfs_streq(arg, "memory")) { 410 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM; 411 } else if (sysfs_streq(arg, "cpu")) { 412 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU; 413 } else { 414 pr_err("Invalid resource specified.\n"); 415 return -EINVAL; 416 } 417 418 return 0; 419 } 420 421 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog) 422 { 423 char *arg; 424 425 arg = strsep(cmd, " "); 426 if (!arg) 427 return -EINVAL; 428 429 if (sysfs_streq(arg, "add")) { 430 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD; 431 } else if (sysfs_streq(arg, "remove")) { 432 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE; 433 } else { 434 pr_err("Invalid action specified.\n"); 435 return -EINVAL; 436 } 437 438 return 0; 439 } 440 441 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog) 442 { 443 char *arg; 444 u32 count, index; 445 446 arg = strsep(cmd, " "); 447 if (!arg) 448 return -EINVAL; 449 450 if (sysfs_streq(arg, "indexed-count")) { 451 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC; 452 arg = strsep(cmd, " "); 453 if (!arg) { 454 pr_err("No DRC count specified.\n"); 455 return -EINVAL; 456 } 457 458 if (kstrtou32(arg, 0, &count)) { 459 pr_err("Invalid DRC count specified.\n"); 460 return -EINVAL; 461 } 462 463 arg = strsep(cmd, " "); 464 if (!arg) { 465 pr_err("No DRC Index specified.\n"); 466 return -EINVAL; 467 } 468 469 if (kstrtou32(arg, 0, &index)) { 470 pr_err("Invalid DRC Index specified.\n"); 471 return -EINVAL; 472 } 473 474 hp_elog->_drc_u.ic.count = cpu_to_be32(count); 475 hp_elog->_drc_u.ic.index = cpu_to_be32(index); 476 } else if (sysfs_streq(arg, "index")) { 477 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX; 478 arg = strsep(cmd, " "); 479 if (!arg) { 480 pr_err("No DRC Index specified.\n"); 481 return -EINVAL; 482 } 483 484 if (kstrtou32(arg, 0, &index)) { 485 pr_err("Invalid DRC Index specified.\n"); 486 return -EINVAL; 487 } 488 489 hp_elog->_drc_u.drc_index = cpu_to_be32(index); 490 } else if (sysfs_streq(arg, "count")) { 491 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT; 492 arg = strsep(cmd, " "); 493 if (!arg) { 494 pr_err("No DRC count specified.\n"); 495 return -EINVAL; 496 } 497 498 if (kstrtou32(arg, 0, &count)) { 499 pr_err("Invalid DRC count specified.\n"); 500 return -EINVAL; 501 } 502 503 hp_elog->_drc_u.drc_count = cpu_to_be32(count); 504 } else { 505 pr_err("Invalid id_type specified.\n"); 506 return -EINVAL; 507 } 508 509 return 0; 510 } 511 512 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr, 513 const char *buf, size_t count) 514 { 515 struct pseries_hp_errorlog hp_elog; 516 char *argbuf; 517 char *args; 518 int rc; 519 520 args = argbuf = kstrdup(buf, GFP_KERNEL); 521 if (!argbuf) { 522 pr_info("Could not allocate resources for DLPAR operation\n"); 523 kfree(argbuf); 524 return -ENOMEM; 525 } 526 527 /* 528 * Parse out the request from the user, this will be in the form: 529 * <resource> <action> <id_type> <id> 530 */ 531 rc = dlpar_parse_resource(&args, &hp_elog); 532 if (rc) 533 goto dlpar_store_out; 534 535 rc = dlpar_parse_action(&args, &hp_elog); 536 if (rc) 537 goto dlpar_store_out; 538 539 rc = dlpar_parse_id_type(&args, &hp_elog); 540 if (rc) 541 goto dlpar_store_out; 542 543 rc = handle_dlpar_errorlog(&hp_elog); 544 545 dlpar_store_out: 546 kfree(argbuf); 547 548 if (rc) 549 pr_err("Could not handle DLPAR request \"%s\"\n", buf); 550 551 return rc ? rc : count; 552 } 553 554 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr, 555 char *buf) 556 { 557 return sprintf(buf, "%s\n", "memory,cpu"); 558 } 559 560 static CLASS_ATTR_RW(dlpar); 561 562 int __init dlpar_workqueue_init(void) 563 { 564 if (pseries_hp_wq) 565 return 0; 566 567 pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue", 568 WQ_UNBOUND, 1); 569 570 return pseries_hp_wq ? 0 : -ENOMEM; 571 } 572 573 static int __init dlpar_sysfs_init(void) 574 { 575 int rc; 576 577 rc = dlpar_workqueue_init(); 578 if (rc) 579 return rc; 580 581 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr); 582 } 583 machine_device_initcall(pseries, dlpar_sysfs_init); 584 585