GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / powerpc / platforms / pseries / dlpar.c
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         struct completion *hp_completion;
36         int *rc;
37 };
38
39 struct cc_workarea {
40         __be32  drc_index;
41         __be32  zero;
42         __be32  name_offset;
43         __be32  prop_length;
44         __be32  prop_offset;
45 };
46
47 void dlpar_free_cc_property(struct property *prop)
48 {
49         kfree(prop->name);
50         kfree(prop->value);
51         kfree(prop);
52 }
53
54 static 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
64         name = (char *)ccwa + be32_to_cpu(ccwa->name_offset);
65         prop->name = kstrdup(name, GFP_KERNEL);
66         if (!prop->name) {
67                 dlpar_free_cc_property(prop);
68                 return NULL;
69         }
70
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);
74         if (!prop->value) {
75                 dlpar_free_cc_property(prop);
76                 return NULL;
77         }
78
79         return prop;
80 }
81
82 static struct device_node *dlpar_parse_cc_node(struct cc_workarea *ccwa)
83 {
84         struct device_node *dn;
85         const char *name;
86
87         dn = kzalloc(sizeof(*dn), GFP_KERNEL);
88         if (!dn)
89                 return NULL;
90
91         name = (const char *)ccwa + be32_to_cpu(ccwa->name_offset);
92         dn->full_name = kstrdup(name, GFP_KERNEL);
93         if (!dn->full_name) {
94                 kfree(dn);
95                 return NULL;
96         }
97
98         of_node_set_flag(dn, OF_DYNAMIC);
99         of_node_init(dn);
100
101         return dn;
102 }
103
104 static void dlpar_free_one_cc_node(struct device_node *dn)
105 {
106         struct property *prop;
107
108         while (dn->properties) {
109                 prop = dn->properties;
110                 dn->properties = prop->next;
111                 dlpar_free_cc_property(prop);
112         }
113
114         kfree(dn->full_name);
115         kfree(dn);
116 }
117
118 void dlpar_free_cc_nodes(struct device_node *dn)
119 {
120         if (dn->child)
121                 dlpar_free_cc_nodes(dn->child);
122
123         if (dn->sibling)
124                 dlpar_free_cc_nodes(dn->sibling);
125
126         dlpar_free_one_cc_node(dn);
127 }
128
129 #define COMPLETE        0
130 #define NEXT_SIBLING    1
131 #define NEXT_CHILD      2
132 #define NEXT_PROPERTY   3
133 #define PREV_PARENT     4
134 #define MORE_MEMORY     5
135 #define ERR_CFG_USE     -9003
136
137 struct device_node *dlpar_configure_connector(__be32 drc_index,
138                                               struct device_node *parent)
139 {
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;
146         char *data_buf;
147         int cc_token;
148         int rc = -1;
149
150         cc_token = rtas_token("ibm,configure-connector");
151         if (cc_token == RTAS_UNKNOWN_SERVICE)
152                 return NULL;
153
154         data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
155         if (!data_buf)
156                 return NULL;
157
158         ccwa = (struct cc_workarea *)&data_buf[0];
159         ccwa->drc_index = drc_index;
160         ccwa->zero = 0;
161
162         do {
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.
166                  */
167                 spin_lock(&rtas_data_buf_lock);
168
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);
172
173                 spin_unlock(&rtas_data_buf_lock);
174
175                 if (rtas_busy_delay(rc))
176                         continue;
177
178                 switch (rc) {
179                 case COMPLETE:
180                         break;
181
182                 case NEXT_SIBLING:
183                         dn = dlpar_parse_cc_node(ccwa);
184                         if (!dn)
185                                 goto cc_error;
186
187                         dn->parent = last_dn->parent;
188                         last_dn->sibling = dn;
189                         last_dn = dn;
190                         break;
191
192                 case NEXT_CHILD:
193                         dn = dlpar_parse_cc_node(ccwa);
194                         if (!dn)
195                                 goto cc_error;
196
197                         if (!first_dn) {
198                                 dn->parent = parent;
199                                 first_dn = dn;
200                         } else {
201                                 dn->parent = last_dn;
202                                 if (last_dn)
203                                         last_dn->child = dn;
204                         }
205
206                         last_dn = dn;
207                         break;
208
209                 case NEXT_PROPERTY:
210                         property = dlpar_parse_cc_property(ccwa);
211                         if (!property)
212                                 goto cc_error;
213
214                         if (!last_dn->properties)
215                                 last_dn->properties = property;
216                         else
217                                 last_property->next = property;
218
219                         last_property = property;
220                         break;
221
222                 case PREV_PARENT:
223                         last_dn = last_dn->parent;
224                         break;
225
226                 case MORE_MEMORY:
227                 case ERR_CFG_USE:
228                 default:
229                         printk(KERN_ERR "Unexpected Error (%d) "
230                                "returned from configure-connector\n", rc);
231                         goto cc_error;
232                 }
233         } while (rc);
234
235 cc_error:
236         kfree(data_buf);
237
238         if (rc) {
239                 if (first_dn)
240                         dlpar_free_cc_nodes(first_dn);
241
242                 return NULL;
243         }
244
245         return first_dn;
246 }
247
248 int dlpar_attach_node(struct device_node *dn, struct device_node *parent)
249 {
250         int rc;
251
252         dn->parent = parent;
253
254         rc = of_attach_node(dn);
255         if (rc) {
256                 printk(KERN_ERR "Failed to add device node %pOF\n", dn);
257                 return rc;
258         }
259
260         return 0;
261 }
262
263 int dlpar_detach_node(struct device_node *dn)
264 {
265         struct device_node *child;
266         int rc;
267
268         child = of_get_next_child(dn, NULL);
269         while (child) {
270                 dlpar_detach_node(child);
271                 child = of_get_next_child(dn, child);
272         }
273
274         rc = of_detach_node(dn);
275         if (rc)
276                 return rc;
277
278         of_node_put(dn);
279
280         return 0;
281 }
282
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
290 #define ISOLATE                 0
291 #define UNISOLATE               1
292
293 int dlpar_acquire_drc(u32 drc_index)
294 {
295         int dr_status, rc;
296
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)
300                 return -1;
301
302         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
303         if (rc)
304                 return rc;
305
306         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
307         if (rc) {
308                 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
309                 return rc;
310         }
311
312         return 0;
313 }
314
315 int dlpar_release_drc(u32 drc_index)
316 {
317         int dr_status, rc;
318
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)
322                 return -1;
323
324         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
325         if (rc)
326                 return rc;
327
328         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
329         if (rc) {
330                 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
331                 return rc;
332         }
333
334         return 0;
335 }
336
337 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
338 {
339         int rc;
340
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);
346                 break;
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);
350                 break;
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);
356         }
357
358         switch (hp_elog->resource) {
359         case PSERIES_HP_ELOG_RESOURCE_MEM:
360                 rc = dlpar_memory(hp_elog);
361                 break;
362         case PSERIES_HP_ELOG_RESOURCE_CPU:
363                 rc = dlpar_cpu(hp_elog);
364                 break;
365         default:
366                 pr_warn_ratelimited("Invalid resource (%d) specified\n",
367                                     hp_elog->resource);
368                 rc = -EINVAL;
369         }
370
371         return rc;
372 }
373
374 static void pseries_hp_work_fn(struct work_struct *work)
375 {
376         struct pseries_hp_work *hp_work =
377                         container_of(work, struct pseries_hp_work, work);
378
379         if (hp_work->rc)
380                 *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
381         else
382                 handle_dlpar_errorlog(hp_work->errlog);
383
384         if (hp_work->hp_completion)
385                 complete(hp_work->hp_completion);
386
387         kfree(hp_work->errlog);
388         kfree((void *)work);
389 }
390
391 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
392                          struct completion *hotplug_done, int *rc)
393 {
394         struct pseries_hp_work *work;
395         struct pseries_hp_errorlog *hp_errlog_copy;
396
397         hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
398                                  GFP_KERNEL);
399         memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
400
401         work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
402         if (work) {
403                 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
404                 work->errlog = hp_errlog_copy;
405                 work->hp_completion = hotplug_done;
406                 work->rc = rc;
407                 queue_work(pseries_hp_wq, (struct work_struct *)work);
408         } else {
409                 *rc = -ENOMEM;
410                 kfree(hp_errlog_copy);
411                 complete(hotplug_done);
412         }
413 }
414
415 static int dlpar_parse_resource(char **cmd, struct pseries_hp_errorlog *hp_elog)
416 {
417         char *arg;
418
419         arg = strsep(cmd, " ");
420         if (!arg)
421                 return -EINVAL;
422
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;
427         } else {
428                 pr_err("Invalid resource specified.\n");
429                 return -EINVAL;
430         }
431
432         return 0;
433 }
434
435 static int dlpar_parse_action(char **cmd, struct pseries_hp_errorlog *hp_elog)
436 {
437         char *arg;
438
439         arg = strsep(cmd, " ");
440         if (!arg)
441                 return -EINVAL;
442
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;
447         } else {
448                 pr_err("Invalid action specified.\n");
449                 return -EINVAL;
450         }
451
452         return 0;
453 }
454
455 static int dlpar_parse_id_type(char **cmd, struct pseries_hp_errorlog *hp_elog)
456 {
457         char *arg;
458         u32 count, index;
459
460         arg = strsep(cmd, " ");
461         if (!arg)
462                 return -EINVAL;
463
464         if (sysfs_streq(arg, "indexed-count")) {
465                 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_IC;
466                 arg = strsep(cmd, " ");
467                 if (!arg) {
468                         pr_err("No DRC count specified.\n");
469                         return -EINVAL;
470                 }
471
472                 if (kstrtou32(arg, 0, &count)) {
473                         pr_err("Invalid DRC count specified.\n");
474                         return -EINVAL;
475                 }
476
477                 arg = strsep(cmd, " ");
478                 if (!arg) {
479                         pr_err("No DRC Index specified.\n");
480                         return -EINVAL;
481                 }
482
483                 if (kstrtou32(arg, 0, &index)) {
484                         pr_err("Invalid DRC Index specified.\n");
485                         return -EINVAL;
486                 }
487
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, " ");
493                 if (!arg) {
494                         pr_err("No DRC Index specified.\n");
495                         return -EINVAL;
496                 }
497
498                 if (kstrtou32(arg, 0, &index)) {
499                         pr_err("Invalid DRC Index specified.\n");
500                         return -EINVAL;
501                 }
502
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, " ");
507                 if (!arg) {
508                         pr_err("No DRC count specified.\n");
509                         return -EINVAL;
510                 }
511
512                 if (kstrtou32(arg, 0, &count)) {
513                         pr_err("Invalid DRC count specified.\n");
514                         return -EINVAL;
515                 }
516
517                 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
518         } else {
519                 pr_err("Invalid id_type specified.\n");
520                 return -EINVAL;
521         }
522
523         return 0;
524 }
525
526 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
527                            const char *buf, size_t count)
528 {
529         struct pseries_hp_errorlog *hp_elog;
530         struct completion hotplug_done;
531         char *argbuf;
532         char *args;
533         int rc;
534
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");
539                 kfree(argbuf);
540                 kfree(hp_elog);
541                 return -ENOMEM;
542         }
543
544         /*
545          * Parse out the request from the user, this will be in the form:
546          * <resource> <action> <id_type> <id>
547          */
548         rc = dlpar_parse_resource(&args, hp_elog);
549         if (rc)
550                 goto dlpar_store_out;
551
552         rc = dlpar_parse_action(&args, hp_elog);
553         if (rc)
554                 goto dlpar_store_out;
555
556         rc = dlpar_parse_id_type(&args, hp_elog);
557         if (rc)
558                 goto dlpar_store_out;
559
560         init_completion(&hotplug_done);
561         queue_hotplug_event(hp_elog, &hotplug_done, &rc);
562         wait_for_completion(&hotplug_done);
563
564 dlpar_store_out:
565         kfree(argbuf);
566         kfree(hp_elog);
567
568         if (rc)
569                 pr_err("Could not handle DLPAR request \"%s\"\n", buf);
570
571         return rc ? rc : count;
572 }
573
574 static ssize_t dlpar_show(struct class *class, struct class_attribute *attr,
575                           char *buf)
576 {
577         return sprintf(buf, "%s\n", "memory,cpu");
578 }
579
580 static CLASS_ATTR_RW(dlpar);
581
582 int __init dlpar_workqueue_init(void)
583 {
584         if (pseries_hp_wq)
585                 return 0;
586
587         pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
588                         WQ_UNBOUND, 1);
589
590         return pseries_hp_wq ? 0 : -ENOMEM;
591 }
592
593 static int __init dlpar_sysfs_init(void)
594 {
595         int rc;
596
597         rc = dlpar_workqueue_init();
598         if (rc)
599                 return rc;
600
601         return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
602 }
603 machine_device_initcall(pseries, dlpar_sysfs_init);
604