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 prop
->length
= be32_to_cpu(ccwa
->prop_length
);
68 value
= (char *)ccwa
+ be32_to_cpu(ccwa
->prop_offset
);
69 prop
->value
= kmemdup(value
, prop
->length
, GFP_KERNEL
);
71 dlpar_free_cc_property(prop
);
78 static struct device_node
*dlpar_parse_cc_node(struct cc_workarea
*ccwa
)
80 struct device_node
*dn
;
83 dn
= kzalloc(sizeof(*dn
), GFP_KERNEL
);
87 name
= (const char *)ccwa
+ be32_to_cpu(ccwa
->name_offset
);
88 dn
->full_name
= kstrdup(name
, GFP_KERNEL
);
94 of_node_set_flag(dn
, OF_DYNAMIC
);
100 static void dlpar_free_one_cc_node(struct device_node
*dn
)
102 struct property
*prop
;
104 while (dn
->properties
) {
105 prop
= dn
->properties
;
106 dn
->properties
= prop
->next
;
107 dlpar_free_cc_property(prop
);
110 kfree(dn
->full_name
);
114 void dlpar_free_cc_nodes(struct device_node
*dn
)
117 dlpar_free_cc_nodes(dn
->child
);
120 dlpar_free_cc_nodes(dn
->sibling
);
122 dlpar_free_one_cc_node(dn
);
126 #define NEXT_SIBLING 1
128 #define NEXT_PROPERTY 3
129 #define PREV_PARENT 4
130 #define MORE_MEMORY 5
131 #define CALL_AGAIN -2
132 #define ERR_CFG_USE -9003
134 struct device_node
*dlpar_configure_connector(__be32 drc_index
,
135 struct device_node
*parent
)
137 struct device_node
*dn
;
138 struct device_node
*first_dn
= NULL
;
139 struct device_node
*last_dn
= NULL
;
140 struct property
*property
;
141 struct property
*last_property
= NULL
;
142 struct cc_workarea
*ccwa
;
147 cc_token
= rtas_token("ibm,configure-connector");
148 if (cc_token
== RTAS_UNKNOWN_SERVICE
)
151 data_buf
= kzalloc(RTAS_DATA_BUF_SIZE
, GFP_KERNEL
);
155 ccwa
= (struct cc_workarea
*)&data_buf
[0];
156 ccwa
->drc_index
= drc_index
;
160 /* Since we release the rtas_data_buf lock between configure
161 * connector calls we want to re-populate the rtas_data_buffer
162 * with the contents of the previous call.
164 spin_lock(&rtas_data_buf_lock
);
166 memcpy(rtas_data_buf
, data_buf
, RTAS_DATA_BUF_SIZE
);
167 rc
= rtas_call(cc_token
, 2, 1, NULL
, rtas_data_buf
, NULL
);
168 memcpy(data_buf
, rtas_data_buf
, RTAS_DATA_BUF_SIZE
);
170 spin_unlock(&rtas_data_buf_lock
);
177 dn
= dlpar_parse_cc_node(ccwa
);
181 dn
->parent
= last_dn
->parent
;
182 last_dn
->sibling
= dn
;
187 dn
= dlpar_parse_cc_node(ccwa
);
195 dn
->parent
= last_dn
;
204 property
= dlpar_parse_cc_property(ccwa
);
208 if (!last_dn
->properties
)
209 last_dn
->properties
= property
;
211 last_property
->next
= property
;
213 last_property
= property
;
217 last_dn
= last_dn
->parent
;
226 printk(KERN_ERR
"Unexpected Error (%d) "
227 "returned from configure-connector\n", rc
);
237 dlpar_free_cc_nodes(first_dn
);
245 int dlpar_attach_node(struct device_node
*dn
, struct device_node
*parent
)
251 rc
= of_attach_node(dn
);
253 printk(KERN_ERR
"Failed to add device node %pOF\n", dn
);
260 int dlpar_detach_node(struct device_node
*dn
)
262 struct device_node
*child
;
265 child
= of_get_next_child(dn
, NULL
);
267 dlpar_detach_node(child
);
268 child
= of_get_next_child(dn
, child
);
271 rc
= of_detach_node(dn
);
278 #define DR_ENTITY_SENSE 9003
279 #define DR_ENTITY_PRESENT 1
280 #define DR_ENTITY_UNUSABLE 2
281 #define ALLOCATION_STATE 9003
282 #define ALLOC_UNUSABLE 0
283 #define ALLOC_USABLE 1
284 #define ISOLATION_STATE 9001
288 int dlpar_acquire_drc(u32 drc_index
)
292 rc
= rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status
,
293 DR_ENTITY_SENSE
, drc_index
);
294 if (rc
|| dr_status
!= DR_ENTITY_UNUSABLE
)
297 rc
= rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_USABLE
);
301 rc
= rtas_set_indicator(ISOLATION_STATE
, drc_index
, UNISOLATE
);
303 rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_UNUSABLE
);
310 int dlpar_release_drc(u32 drc_index
)
314 rc
= rtas_call(rtas_token("get-sensor-state"), 2, 2, &dr_status
,
315 DR_ENTITY_SENSE
, drc_index
);
316 if (rc
|| dr_status
!= DR_ENTITY_PRESENT
)
319 rc
= rtas_set_indicator(ISOLATION_STATE
, drc_index
, ISOLATE
);
323 rc
= rtas_set_indicator(ALLOCATION_STATE
, drc_index
, ALLOC_UNUSABLE
);
325 rtas_set_indicator(ISOLATION_STATE
, drc_index
, UNISOLATE
);
332 static int handle_dlpar_errorlog(struct pseries_hp_errorlog
*hp_elog
)
336 /* pseries error logs are in BE format, convert to cpu type */
337 switch (hp_elog
->id_type
) {
338 case PSERIES_HP_ELOG_ID_DRC_COUNT
:
339 hp_elog
->_drc_u
.drc_count
=
340 be32_to_cpu(hp_elog
->_drc_u
.drc_count
);
342 case PSERIES_HP_ELOG_ID_DRC_INDEX
:
343 hp_elog
->_drc_u
.drc_index
=
344 be32_to_cpu(hp_elog
->_drc_u
.drc_index
);
346 case PSERIES_HP_ELOG_ID_DRC_IC
:
347 hp_elog
->_drc_u
.ic
.count
=
348 be32_to_cpu(hp_elog
->_drc_u
.ic
.count
);
349 hp_elog
->_drc_u
.ic
.index
=
350 be32_to_cpu(hp_elog
->_drc_u
.ic
.index
);
353 switch (hp_elog
->resource
) {
354 case PSERIES_HP_ELOG_RESOURCE_MEM
:
355 rc
= dlpar_memory(hp_elog
);
357 case PSERIES_HP_ELOG_RESOURCE_CPU
:
358 rc
= dlpar_cpu(hp_elog
);
361 pr_warn_ratelimited("Invalid resource (%d) specified\n",
369 static void pseries_hp_work_fn(struct work_struct
*work
)
371 struct pseries_hp_work
*hp_work
=
372 container_of(work
, struct pseries_hp_work
, work
);
375 *(hp_work
->rc
) = handle_dlpar_errorlog(hp_work
->errlog
);
377 handle_dlpar_errorlog(hp_work
->errlog
);
379 if (hp_work
->hp_completion
)
380 complete(hp_work
->hp_completion
);
382 kfree(hp_work
->errlog
);
386 void queue_hotplug_event(struct pseries_hp_errorlog
*hp_errlog
,
387 struct completion
*hotplug_done
, int *rc
)
389 struct pseries_hp_work
*work
;
390 struct pseries_hp_errorlog
*hp_errlog_copy
;
392 hp_errlog_copy
= kmalloc(sizeof(struct pseries_hp_errorlog
),
394 memcpy(hp_errlog_copy
, hp_errlog
, sizeof(struct pseries_hp_errorlog
));
396 work
= kmalloc(sizeof(struct pseries_hp_work
), GFP_KERNEL
);
398 INIT_WORK((struct work_struct
*)work
, pseries_hp_work_fn
);
399 work
->errlog
= hp_errlog_copy
;
400 work
->hp_completion
= hotplug_done
;
402 queue_work(pseries_hp_wq
, (struct work_struct
*)work
);
405 kfree(hp_errlog_copy
);
406 complete(hotplug_done
);
410 static int dlpar_parse_resource(char **cmd
, struct pseries_hp_errorlog
*hp_elog
)
414 arg
= strsep(cmd
, " ");
418 if (sysfs_streq(arg
, "memory")) {
419 hp_elog
->resource
= PSERIES_HP_ELOG_RESOURCE_MEM
;
420 } else if (sysfs_streq(arg
, "cpu")) {
421 hp_elog
->resource
= PSERIES_HP_ELOG_RESOURCE_CPU
;
423 pr_err("Invalid resource specified.\n");
430 static int dlpar_parse_action(char **cmd
, struct pseries_hp_errorlog
*hp_elog
)
434 arg
= strsep(cmd
, " ");
438 if (sysfs_streq(arg
, "add")) {
439 hp_elog
->action
= PSERIES_HP_ELOG_ACTION_ADD
;
440 } else if (sysfs_streq(arg
, "remove")) {
441 hp_elog
->action
= PSERIES_HP_ELOG_ACTION_REMOVE
;
443 pr_err("Invalid action specified.\n");
450 static int dlpar_parse_id_type(char **cmd
, struct pseries_hp_errorlog
*hp_elog
)
455 arg
= strsep(cmd
, " ");
459 if (sysfs_streq(arg
, "indexed-count")) {
460 hp_elog
->id_type
= PSERIES_HP_ELOG_ID_DRC_IC
;
461 arg
= strsep(cmd
, " ");
463 pr_err("No DRC count specified.\n");
467 if (kstrtou32(arg
, 0, &count
)) {
468 pr_err("Invalid DRC count specified.\n");
472 arg
= strsep(cmd
, " ");
474 pr_err("No DRC Index specified.\n");
478 if (kstrtou32(arg
, 0, &index
)) {
479 pr_err("Invalid DRC Index specified.\n");
483 hp_elog
->_drc_u
.ic
.count
= cpu_to_be32(count
);
484 hp_elog
->_drc_u
.ic
.index
= cpu_to_be32(index
);
485 } else if (sysfs_streq(arg
, "index")) {
486 hp_elog
->id_type
= PSERIES_HP_ELOG_ID_DRC_INDEX
;
487 arg
= strsep(cmd
, " ");
489 pr_err("No DRC Index specified.\n");
493 if (kstrtou32(arg
, 0, &index
)) {
494 pr_err("Invalid DRC Index specified.\n");
498 hp_elog
->_drc_u
.drc_index
= cpu_to_be32(index
);
499 } else if (sysfs_streq(arg
, "count")) {
500 hp_elog
->id_type
= PSERIES_HP_ELOG_ID_DRC_COUNT
;
501 arg
= strsep(cmd
, " ");
503 pr_err("No DRC count specified.\n");
507 if (kstrtou32(arg
, 0, &count
)) {
508 pr_err("Invalid DRC count specified.\n");
512 hp_elog
->_drc_u
.drc_count
= cpu_to_be32(count
);
514 pr_err("Invalid id_type specified.\n");
521 static ssize_t
dlpar_store(struct class *class, struct class_attribute
*attr
,
522 const char *buf
, size_t count
)
524 struct pseries_hp_errorlog
*hp_elog
;
525 struct completion hotplug_done
;
530 args
= argbuf
= kstrdup(buf
, GFP_KERNEL
);
531 hp_elog
= kzalloc(sizeof(*hp_elog
), GFP_KERNEL
);
532 if (!hp_elog
|| !argbuf
) {
533 pr_info("Could not allocate resources for DLPAR operation\n");
540 * Parse out the request from the user, this will be in the form:
541 * <resource> <action> <id_type> <id>
543 rc
= dlpar_parse_resource(&args
, hp_elog
);
545 goto dlpar_store_out
;
547 rc
= dlpar_parse_action(&args
, hp_elog
);
549 goto dlpar_store_out
;
551 rc
= dlpar_parse_id_type(&args
, hp_elog
);
553 goto dlpar_store_out
;
555 init_completion(&hotplug_done
);
556 queue_hotplug_event(hp_elog
, &hotplug_done
, &rc
);
557 wait_for_completion(&hotplug_done
);
564 pr_err("Could not handle DLPAR request \"%s\"\n", buf
);
566 return rc
? rc
: count
;
569 static ssize_t
dlpar_show(struct class *class, struct class_attribute
*attr
,
572 return sprintf(buf
, "%s\n", "memory,cpu");
575 static CLASS_ATTR_RW(dlpar
);
577 int __init
dlpar_workqueue_init(void)
582 pseries_hp_wq
= alloc_workqueue("pseries hotplug workqueue",
585 return pseries_hp_wq
? 0 : -ENOMEM
;
588 static int __init
dlpar_sysfs_init(void)
592 rc
= dlpar_workqueue_init();
596 return sysfs_create_file(kernel_kobj
, &class_attr_dlpar
.attr
);
598 machine_device_initcall(pseries
, dlpar_sysfs_init
);