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 <asm/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,
85 struct device_node *dn;
88 /* If parent node path is "/" advance path to NULL terminator to
89 * prevent double leading slashs in full_name.
94 dn = kzalloc(sizeof(*dn), GFP_KERNEL);
98 name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
99 dn->full_name = kasprintf(GFP_KERNEL, "%s/%s", path, name);
100 if (!dn->full_name) {
105 of_node_set_flag(dn, OF_DYNAMIC);
111 static void dlpar_free_one_cc_node(struct device_node *dn)
113 struct property *prop;
115 while (dn->properties) {
116 prop = dn->properties;
117 dn->properties = prop->next;
118 dlpar_free_cc_property(prop);
121 kfree(dn->full_name);
125 void dlpar_free_cc_nodes(struct device_node *dn)
128 dlpar_free_cc_nodes(dn->child);
131 dlpar_free_cc_nodes(dn->sibling);
133 dlpar_free_one_cc_node(dn);
137 #define NEXT_SIBLING 1
139 #define NEXT_PROPERTY 3
140 #define PREV_PARENT 4
141 #define MORE_MEMORY 5
142 #define ERR_CFG_USE -9003
144 struct device_node *dlpar_configure_connector(__be32 drc_index,
145 struct device_node *parent)
147 struct device_node *dn;
148 struct device_node *first_dn = NULL;
149 struct device_node *last_dn = NULL;
150 struct property *property;
151 struct property *last_property = NULL;
152 struct cc_workarea *ccwa;
154 const char *parent_path = parent->full_name;
158 cc_token = rtas_token("ibm,configure-connector");
159 if (cc_token == RTAS_UNKNOWN_SERVICE)
162 data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
166 ccwa = (struct cc_workarea *)&data_buf[0];
167 ccwa->drc_index = drc_index;
171 /* Since we release the rtas_data_buf lock between configure
172 * connector calls we want to re-populate the rtas_data_buffer
173 * with the contents of the previous call.
175 spin_lock(&rtas_data_buf_lock);
177 memcpy(rtas_data_buf, data_buf, RTAS_DATA_BUF_SIZE);
178 rc = rtas_call(cc_token, 2, 1, NULL, rtas_data_buf, NULL);
179 memcpy(data_buf, rtas_data_buf, RTAS_DATA_BUF_SIZE);
181 spin_unlock(&rtas_data_buf_lock);
183 if (rtas_busy_delay(rc))
191 dn = dlpar_parse_cc_node(ccwa, parent_path);
195 dn->parent = last_dn->parent;
196 last_dn->sibling = dn;
202 parent_path = last_dn->full_name;
204 dn = dlpar_parse_cc_node(ccwa, parent_path);
212 dn->parent = last_dn;
221 property = dlpar_parse_cc_property(ccwa);
225 if (!last_dn->properties)
226 last_dn->properties = property;
228 last_property->next = property;
230 last_property = property;
234 last_dn = last_dn->parent;
235 parent_path = last_dn->parent->full_name;
241 printk(KERN_ERR "Unexpected Error (%d) "
242 "returned from configure-connector\n", rc);
252 dlpar_free_cc_nodes(first_dn);
260 int dlpar_attach_node(struct device_node *dn)
264 dn->parent = pseries_of_derive_parent(dn->full_name);
265 if (IS_ERR(dn->parent))
266 return PTR_ERR(dn->parent);
268 rc = of_attach_node(dn);
270 printk(KERN_ERR "Failed to add device node %s\n",
275 of_node_put(dn->parent);
279 int dlpar_detach_node(struct device_node *dn)
281 struct device_node *child;
284 child = of_get_next_child(dn, NULL);
286 dlpar_detach_node(child);
287 child = of_get_next_child(dn, child);
290 rc = of_detach_node(dn);
299 #define DR_ENTITY_SENSE 9003
300 #define DR_ENTITY_PRESENT 1
301 #define DR_ENTITY_UNUSABLE 2
302 #define ALLOCATION_STATE 9003
303 #define ALLOC_UNUSABLE 0
304 #define ALLOC_USABLE 1
305 #define ISOLATION_STATE 9001
309 int dlpar_acquire_drc(u32 drc_index)
313 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
314 DR_ENTITY_SENSE, drc_index);
315 if (rc || dr_status != DR_ENTITY_UNUSABLE)
318 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
322 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
324 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
331 int dlpar_release_drc(u32 drc_index)
335 rc = rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status,
336 DR_ENTITY_SENSE, drc_index);
337 if (rc || dr_status != DR_ENTITY_PRESENT)
340 rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
344 rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
346 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
353 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
357 /* pseries error logs are in BE format, convert to cpu type */
358 switch (hp_elog->id_type) {
359 case PSERIES_HP_ELOG_ID_DRC_COUNT:
360 hp_elog->_drc_u.drc_count =
361 be32_to_cpu(hp_elog->_drc_u.drc_count);
363 case PSERIES_HP_ELOG_ID_DRC_INDEX:
364 hp_elog->_drc_u.drc_index =
365 be32_to_cpu(hp_elog->_drc_u.drc_index);
368 switch (hp_elog->resource) {
369 case PSERIES_HP_ELOG_RESOURCE_MEM:
370 rc = dlpar_memory(hp_elog);
372 case PSERIES_HP_ELOG_RESOURCE_CPU:
373 rc = dlpar_cpu(hp_elog);
376 pr_warn_ratelimited("Invalid resource (%d) specified\n",
384 static void pseries_hp_work_fn(struct work_struct *work)
386 struct pseries_hp_work *hp_work =
387 container_of(work, struct pseries_hp_work, work);
390 *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
392 handle_dlpar_errorlog(hp_work->errlog);
394 if (hp_work->hp_completion)
395 complete(hp_work->hp_completion);
397 kfree(hp_work->errlog);
401 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
402 struct completion *hotplug_done, int *rc)
404 struct pseries_hp_work *work;
405 struct pseries_hp_errorlog *hp_errlog_copy;
407 hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
409 memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
411 work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
413 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
414 work->errlog = hp_errlog_copy;
415 work->hp_completion = hotplug_done;
417 queue_work(pseries_hp_wq, (struct work_struct *)work);
420 kfree(hp_errlog_copy);
421 complete(hotplug_done);
425 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
426 const char *buf, size_t count)
428 struct pseries_hp_errorlog *hp_elog;
429 struct completion hotplug_done;
433 hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
436 goto dlpar_store_out;
439 /* Parse out the request from the user, this will be in the form
440 * <resource> <action> <id_type> <id>
443 if (!strncmp(arg, "memory", 6)) {
444 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_MEM;
445 arg += strlen("memory ");
446 } else if (!strncmp(arg, "cpu", 3)) {
447 hp_elog->resource = PSERIES_HP_ELOG_RESOURCE_CPU;
448 arg += strlen("cpu ");
450 pr_err("Invalid resource specified: \"%s\"\n", buf);
452 goto dlpar_store_out;
455 if (!strncmp(arg, "add", 3)) {
456 hp_elog->action = PSERIES_HP_ELOG_ACTION_ADD;
457 arg += strlen("add ");
458 } else if (!strncmp(arg, "remove", 6)) {
459 hp_elog->action = PSERIES_HP_ELOG_ACTION_REMOVE;
460 arg += strlen("remove ");
462 pr_err("Invalid action specified: \"%s\"\n", buf);
464 goto dlpar_store_out;
467 if (!strncmp(arg, "index", 5)) {
470 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
471 arg += strlen("index ");
472 if (kstrtou32(arg, 0, &index)) {
474 pr_err("Invalid drc_index specified: \"%s\"\n", buf);
475 goto dlpar_store_out;
478 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
479 } else if (!strncmp(arg, "count", 5)) {
482 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
483 arg += strlen("count ");
484 if (kstrtou32(arg, 0, &count)) {
486 pr_err("Invalid count specified: \"%s\"\n", buf);
487 goto dlpar_store_out;
490 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
492 pr_err("Invalid id_type specified: \"%s\"\n", buf);
494 goto dlpar_store_out;
497 init_completion(&hotplug_done);
498 queue_hotplug_event(hp_elog, &hotplug_done, &rc);
499 wait_for_completion(&hotplug_done);
503 return rc ? rc : count;
506 static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
508 static int __init pseries_dlpar_init(void)
510 pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
512 return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
514 machine_device_initcall(pseries, pseries_dlpar_init);