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