2 * Support for dynamic reconfiguration for PCI, Memory, and CPU
3 * Hotplug and Dynamic Logical Partitioning on RPA platforms.
5 * Copyright (C) 2009 Nathan Fontenot
6 * Copyright (C) 2009 IBM Corporation
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.
13 #define pr_fmt(fmt) "dlpar: " fmt
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>
22 #include "of_helpers.h"
26 #include <asm/machdep.h>
27 #include <linux/uaccess.h>
30 static struct workqueue_struct *pseries_hp_wq;
32 struct pseries_hp_work {
33 struct work_struct work;
34 struct pseries_hp_errorlog *errlog;
35 struct completion *hp_completion;
47 void dlpar_free_cc_property(struct property *prop)
54 static struct property *dlpar_parse_cc_property(struct cc_workarea *ccwa)
56 struct property *prop;
60 prop = kzalloc(sizeof(*prop), GFP_KERNEL);
64 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
65 prop->name = kstrdup(name, GFP_KERNEL);
67 dlpar_free_cc_property(prop);
71 prop->length = be32_to_cpu(ccwa->prop_length);
72 value = (char *)ccwa + be32_to_cpu(ccwa->prop_offset);
73 prop->value = kmemdup(value, prop->length, GFP_KERNEL);
75 dlpar_free_cc_property(prop);
82 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
84 struct device_node *dn;
87 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
91 name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
92 dn->full_name = kstrdup(name, GFP_KERNEL);
98 of_node_set_flag(dn, OF_DYNAMIC);
104 static void dlpar_free_one_cc_node(struct device_node *dn)
106 struct property *prop;
108 while (dn->properties) {
109 prop = dn->properties;
110 dn->properties = prop->next;
111 dlpar_free_cc_property(prop);
114 kfree(dn->full_name);
118 void dlpar_free_cc_nodes(struct device_node *dn)
121 dlpar_free_cc_nodes(dn->child);
124 dlpar_free_cc_nodes(dn->sibling);
126 dlpar_free_one_cc_node(dn);
130 #define NEXT_SIBLING 1
132 #define NEXT_PROPERTY 3
133 #define PREV_PARENT 4
134 #define MORE_MEMORY 5
135 #define ERR_CFG_USE -9003
137 struct device_node *dlpar_configure_connector(__be32 drc_index,
138 struct device_node *parent)
140 struct device_node *dn;
141 struct device_node *first_dn = NULL;
142 struct device_node *last_dn = NULL;
143 struct property *property;
144 struct property *last_property = NULL;
145 struct cc_workarea *ccwa;
150 cc_token = rtas_token("ibm,configure-connector");
151 if (cc_token == RTAS_UNKNOWN_SERVICE)
154 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
158 ccwa = (struct cc_workarea *)&data_buf[0];
159 ccwa->drc_index = drc_index;
163 /* Since we release the rtas_data_buf lock between configure
164 * connector calls we want to re-populate the rtas_data_buffer
165 * with the contents of the previous call.
167 spin_lock(&rtas_data_buf_lock);
169 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
170 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
171 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
173 spin_unlock(&rtas_data_buf_lock);
175 if (rtas_busy_delay(rc))
183 dn = dlpar_parse_cc_node(ccwa);
187 dn->parent = last_dn->parent;
188 last_dn->sibling = dn;
193 dn = dlpar_parse_cc_node(ccwa);
201 dn->parent = last_dn;
210 property = dlpar_parse_cc_property(ccwa);
214 if (!last_dn->properties)
215 last_dn->properties = property;
217 last_property->next = property;
219 last_property = property;
223 last_dn = last_dn->parent;
229 printk(KERN_ERR "Unexpected Error (%d) "
230 "returned from configure-connector\n", rc);
240 dlpar_free_cc_nodes(first_dn);
248 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
254 rc = of_attach_node(dn);
256 printk(KERN_ERR "Failed to add device node %pOF\n", dn);
263 int dlpar_detach_node(struct device_node *dn)
265 struct device_node *child;
268 child = of_get_next_child(dn, NULL);
270 dlpar_detach_node(child);
271 child = of_get_next_child(dn, child);
274 rc = of_detach_node(dn);
283 #define DR_ENTITY_SENSE 9003
284 #define DR_ENTITY_PRESENT 1
285 #define DR_ENTITY_UNUSABLE 2
286 #define ALLOCATION_STATE 9003
287 #define ALLOC_UNUSABLE 0
288 #define ALLOC_USABLE 1
289 #define ISOLATION_STATE 9001
293 int dlpar_acquire_drc(u32 drc_index)
297 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
298 DR_ENTITY_SENSE, drc_index);
299 if (rc || dr_status != DR_ENTITY_UNUSABLE)
302 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
306 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
308 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
315 int dlpar_release_drc(u32 drc_index)
319 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
320 DR_ENTITY_SENSE, drc_index);
321 if (rc || dr_status != DR_ENTITY_PRESENT)
324 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
328 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
330 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
337 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
341 /* pseries error logs are in BE format, convert to cpu type */
342 switch (hp_elog->id_type) {
343 case PSERIES_HP_ELOG_ID_DRC_COUNT:
344 hp_elog->_drc_u.drc_count =
345 be32_to_cpu(hp_elog->_drc_u.drc_count);
347 case PSERIES_HP_ELOG_ID_DRC_INDEX:
348 hp_elog->_drc_u.drc_index =
349 be32_to_cpu(hp_elog->_drc_u.drc_index);
351 case PSERIES_HP_ELOG_ID_DRC_IC:
352 hp_elog->_drc_u.ic.count =
353 be32_to_cpu(hp_elog->_drc_u.ic.count);
354 hp_elog->_drc_u.ic.index =
355 be32_to_cpu(hp_elog->_drc_u.ic.index);
358 switch (hp_elog->resource) {
359 case PSERIES_HP_ELOG_RESOURCE_MEM:
360 rc = dlpar_memory(hp_elog);
362 case PSERIES_HP_ELOG_RESOURCE_CPU:
363 rc = dlpar_cpu(hp_elog);
366 pr_warn_ratelimited("Invalid resource (%d) specified\n",
374 static void pseries_hp_work_fn(struct work_struct *work)
376 struct pseries_hp_work *hp_work =
377 container_of(work, struct pseries_hp_work, work);
380 *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
382 handle_dlpar_errorlog(hp_work->errlog);
384 if (hp_work->hp_completion)
385 complete(hp_work->hp_completion);
387 kfree(hp_work->errlog);
391 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
392 struct completion *hotplug_done, int *rc)
394 struct pseries_hp_work *work;
395 struct pseries_hp_errorlog *hp_errlog_copy;
397 hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
399 memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
401 work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
403 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
404 work->errlog = hp_errlog_copy;
405 work->hp_completion = hotplug_done;
407 queue_work(pseries_hp_wq, (struct work_struct *)work);
410 kfree(hp_errlog_copy);
411 complete(hotplug_done);
415 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
419 arg = strsep(cmd, " ");
423 if (sysfs_streq(arg, "memory")) {
424 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
425 } else if (sysfs_streq(arg, "cpu")) {
426 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
428 pr_err("Invalid resource specified.\n");
435 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
439 arg = strsep(cmd, " ");
443 if (sysfs_streq(arg, "add")) {
444 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
445 } else if (sysfs_streq(arg, "remove")) {
446 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
448 pr_err("Invalid action specified.\n");
455 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
460 arg = strsep(cmd, " ");
464 if (sysfs_streq(arg, "indexed-count")) {
465 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
466 arg = strsep(cmd, " ");
468 pr_err("No DRC count specified.\n");
472 if (kstrtou32(arg, 0, &count)) {
473 pr_err("Invalid DRC count specified.\n");
477 arg = strsep(cmd, " ");
479 pr_err("No DRC Index specified.\n");
483 if (kstrtou32(arg, 0, &index)) {
484 pr_err("Invalid DRC Index specified.\n");
488 hp_elog->_drc_u.ic.count = cpu_to_be32(count);
489 hp_elog->_drc_u.ic.index = cpu_to_be32(index);
490 } else if (sysfs_streq(arg, "index")) {
491 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
492 arg = strsep(cmd, " ");
494 pr_err("No DRC Index specified.\n");
498 if (kstrtou32(arg, 0, &index)) {
499 pr_err("Invalid DRC Index specified.\n");
503 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
504 } else if (sysfs_streq(arg, "count")) {
505 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
506 arg = strsep(cmd, " ");
508 pr_err("No DRC count specified.\n");
512 if (kstrtou32(arg, 0, &count)) {
513 pr_err("Invalid DRC count specified.\n");
517 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
519 pr_err("Invalid id_type specified.\n");
526 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
527 const char *buf, size_t count)
529 struct pseries_hp_errorlog *hp_elog;
530 struct completion hotplug_done;
535 args = argbuf = kstrdup(buf, GFP_KERNEL);
536 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
537 if (!hp_elog || !argbuf) {
538 pr_info("Could not allocate resources for DLPAR operation\n");
545 * Parse out the request from the user, this will be in the form:
546 * <resource> <action> <id_type> <id>
548 rc = dlpar_parse_resource(&args, hp_elog);
550 goto dlpar_store_out;
552 rc = dlpar_parse_action(&args, hp_elog);
554 goto dlpar_store_out;
556 rc = dlpar_parse_id_type(&args, hp_elog);
558 goto dlpar_store_out;
560 init_completion(&hotplug_done);
561 queue_hotplug_event(hp_elog, &hotplug_done, &rc);
562 wait_for_completion(&hotplug_done);
569 pr_err("Could not handle DLPAR request \"%s\"\n", buf);
571 return rc ? rc : count;
574 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
577 return sprintf(buf, "%s\n", "memory,cpu");
580 static CLASS_ATTR_RW(dlpar);
582 int __init dlpar_workqueue_init(void)
587 pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
590 return pseries_hp_wq ? 0 : -ENOMEM;
593 static int __init dlpar_sysfs_init(void)
597 rc = dlpar_workqueue_init();
601 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
603 machine_device_initcall(pseries, dlpar_sysfs_init);