GNU Linux-libre 4.9.309-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 <asm/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                                                const char *path)
84 {
85         struct device_node *dn;
86         char *name;
87
88         /* If parent node path is "/" advance path to NULL terminator to
89          * prevent double leading slashs in full_name.
90          */
91         if (!path[1])
92                 path++;
93
94         dn = kzalloc(sizeof(*dn), GFP_KERNEL);
95         if (!dn)
96                 return NULL;
97
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) {
101                 kfree(dn);
102                 return NULL;
103         }
104
105         of_node_set_flag(dn, OF_DYNAMIC);
106         of_node_init(dn);
107
108         return dn;
109 }
110
111 static void dlpar_free_one_cc_node(struct device_node *dn)
112 {
113         struct property *prop;
114
115         while (dn->properties) {
116                 prop = dn->properties;
117                 dn->properties = prop->next;
118                 dlpar_free_cc_property(prop);
119         }
120
121         kfree(dn->full_name);
122         kfree(dn);
123 }
124
125 void dlpar_free_cc_nodes(struct device_node *dn)
126 {
127         if (dn->child)
128                 dlpar_free_cc_nodes(dn->child);
129
130         if (dn->sibling)
131                 dlpar_free_cc_nodes(dn->sibling);
132
133         dlpar_free_one_cc_node(dn);
134 }
135
136 #define COMPLETE        0
137 #define NEXT_SIBLING    1
138 #define NEXT_CHILD      2
139 #define NEXT_PROPERTY   3
140 #define PREV_PARENT     4
141 #define MORE_MEMORY     5
142 #define ERR_CFG_USE     -9003
143
144 struct device_node *dlpar_configure_connector(__be32 drc_index,
145                                               struct device_node *parent)
146 {
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;
153         char *data_buf;
154         const char *parent_path = parent->full_name;
155         int cc_token;
156         int rc = -1;
157
158         cc_token = rtas_token("ibm,configure-connector");
159         if (cc_token == RTAS_UNKNOWN_SERVICE)
160                 return NULL;
161
162         data_buf = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
163         if (!data_buf)
164                 return NULL;
165
166         ccwa = (struct cc_workarea *)&data_buf[0];
167         ccwa->drc_index = drc_index;
168         ccwa->zero = 0;
169
170         do {
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.
174                  */
175                 spin_lock(&rtas_data_buf_lock);
176
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);
180
181                 spin_unlock(&rtas_data_buf_lock);
182
183                 if (rtas_busy_delay(rc))
184                         continue;
185
186                 switch (rc) {
187                 case COMPLETE:
188                         break;
189
190                 case NEXT_SIBLING:
191                         dn = dlpar_parse_cc_node(ccwa, parent_path);
192                         if (!dn)
193                                 goto cc_error;
194
195                         dn->parent = last_dn->parent;
196                         last_dn->sibling = dn;
197                         last_dn = dn;
198                         break;
199
200                 case NEXT_CHILD:
201                         if (first_dn)
202                                 parent_path = last_dn->full_name;
203
204                         dn = dlpar_parse_cc_node(ccwa, parent_path);
205                         if (!dn)
206                                 goto cc_error;
207
208                         if (!first_dn) {
209                                 dn->parent = parent;
210                                 first_dn = dn;
211                         } else {
212                                 dn->parent = last_dn;
213                                 if (last_dn)
214                                         last_dn->child = dn;
215                         }
216
217                         last_dn = dn;
218                         break;
219
220                 case NEXT_PROPERTY:
221                         property = dlpar_parse_cc_property(ccwa);
222                         if (!property)
223                                 goto cc_error;
224
225                         if (!last_dn->properties)
226                                 last_dn->properties = property;
227                         else
228                                 last_property->next = property;
229
230                         last_property = property;
231                         break;
232
233                 case PREV_PARENT:
234                         last_dn = last_dn->parent;
235                         parent_path = last_dn->parent->full_name;
236                         break;
237
238                 case MORE_MEMORY:
239                 case ERR_CFG_USE:
240                 default:
241                         printk(KERN_ERR "Unexpected Error (%d) "
242                                "returned from configure-connector\n", rc);
243                         goto cc_error;
244                 }
245         } while (rc);
246
247 cc_error:
248         kfree(data_buf);
249
250         if (rc) {
251                 if (first_dn)
252                         dlpar_free_cc_nodes(first_dn);
253
254                 return NULL;
255         }
256
257         return first_dn;
258 }
259
260 int dlpar_attach_node(struct device_node *dn)
261 {
262         int rc;
263
264         dn->parent = pseries_of_derive_parent(dn->full_name);
265         if (IS_ERR(dn->parent))
266                 return PTR_ERR(dn->parent);
267
268         rc = of_attach_node(dn);
269         if (rc) {
270                 printk(KERN_ERR "Failed to add device node %s\n",
271                        dn->full_name);
272                 return rc;
273         }
274
275         of_node_put(dn->parent);
276         return 0;
277 }
278
279 int dlpar_detach_node(struct device_node *dn)
280 {
281         struct device_node *child;
282         int rc;
283
284         child = of_get_next_child(dn, NULL);
285         while (child) {
286                 dlpar_detach_node(child);
287                 child = of_get_next_child(dn, child);
288         }
289
290         rc = of_detach_node(dn);
291         if (rc)
292                 return rc;
293
294         of_node_put(dn);
295
296         return 0;
297 }
298
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
306 #define ISOLATE                 0
307 #define UNISOLATE               1
308
309 int dlpar_acquire_drc(u32 drc_index)
310 {
311         int dr_status, rc;
312
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)
316                 return -1;
317
318         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_USABLE);
319         if (rc)
320                 return rc;
321
322         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
323         if (rc) {
324                 rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
325                 return rc;
326         }
327
328         return 0;
329 }
330
331 int dlpar_release_drc(u32 drc_index)
332 {
333         int dr_status, rc;
334
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)
338                 return -1;
339
340         rc = rtas_set_indicator(ISOLATION_STATE, drc_index, ISOLATE);
341         if (rc)
342                 return rc;
343
344         rc = rtas_set_indicator(ALLOCATION_STATE, drc_index, ALLOC_UNUSABLE);
345         if (rc) {
346                 rtas_set_indicator(ISOLATION_STATE, drc_index, UNISOLATE);
347                 return rc;
348         }
349
350         return 0;
351 }
352
353 static int handle_dlpar_errorlog(struct pseries_hp_errorlog *hp_elog)
354 {
355         int rc;
356
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);
362                 break;
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);
366         }
367
368         switch (hp_elog->resource) {
369         case PSERIES_HP_ELOG_RESOURCE_MEM:
370                 rc = dlpar_memory(hp_elog);
371                 break;
372         case PSERIES_HP_ELOG_RESOURCE_CPU:
373                 rc = dlpar_cpu(hp_elog);
374                 break;
375         default:
376                 pr_warn_ratelimited("Invalid resource (%d) specified\n",
377                                     hp_elog->resource);
378                 rc = -EINVAL;
379         }
380
381         return rc;
382 }
383
384 static void pseries_hp_work_fn(struct work_struct *work)
385 {
386         struct pseries_hp_work *hp_work =
387                         container_of(work, struct pseries_hp_work, work);
388
389         if (hp_work->rc)
390                 *(hp_work->rc) = handle_dlpar_errorlog(hp_work->errlog);
391         else
392                 handle_dlpar_errorlog(hp_work->errlog);
393
394         if (hp_work->hp_completion)
395                 complete(hp_work->hp_completion);
396
397         kfree(hp_work->errlog);
398         kfree((void *)work);
399 }
400
401 void queue_hotplug_event(struct pseries_hp_errorlog *hp_errlog,
402                          struct completion *hotplug_done, int *rc)
403 {
404         struct pseries_hp_work *work;
405         struct pseries_hp_errorlog *hp_errlog_copy;
406
407         hp_errlog_copy = kmalloc(sizeof(struct pseries_hp_errorlog),
408                                  GFP_KERNEL);
409         memcpy(hp_errlog_copy, hp_errlog, sizeof(struct pseries_hp_errorlog));
410
411         work = kmalloc(sizeof(struct pseries_hp_work), GFP_KERNEL);
412         if (work) {
413                 INIT_WORK((struct work_struct *)work, pseries_hp_work_fn);
414                 work->errlog = hp_errlog_copy;
415                 work->hp_completion = hotplug_done;
416                 work->rc = rc;
417                 queue_work(pseries_hp_wq, (struct work_struct *)work);
418         } else {
419                 *rc = -ENOMEM;
420                 kfree(hp_errlog_copy);
421                 complete(hotplug_done);
422         }
423 }
424
425 static ssize_t dlpar_store(struct class *class, struct class_attribute *attr,
426                            const char *buf, size_t count)
427 {
428         struct pseries_hp_errorlog *hp_elog;
429         struct completion hotplug_done;
430         const char *arg;
431         int rc;
432
433         hp_elog = kzalloc(sizeof(*hp_elog), GFP_KERNEL);
434         if (!hp_elog) {
435                 rc = -ENOMEM;
436                 goto dlpar_store_out;
437         }
438
439         /* Parse out the request from the user, this will be in the form
440          * <resource> <action> <id_type> <id>
441          */
442         arg = buf;
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 ");
449         } else {
450                 pr_err("Invalid resource specified: \"%s\"\n", buf);
451                 rc = -EINVAL;
452                 goto dlpar_store_out;
453         }
454
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 ");
461         } else {
462                 pr_err("Invalid action specified: \"%s\"\n", buf);
463                 rc = -EINVAL;
464                 goto dlpar_store_out;
465         }
466
467         if (!strncmp(arg, "index", 5)) {
468                 u32 index;
469
470                 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_INDEX;
471                 arg += strlen("index ");
472                 if (kstrtou32(arg, 0, &index)) {
473                         rc = -EINVAL;
474                         pr_err("Invalid drc_index specified: \"%s\"\n", buf);
475                         goto dlpar_store_out;
476                 }
477
478                 hp_elog->_drc_u.drc_index = cpu_to_be32(index);
479         } else if (!strncmp(arg, "count", 5)) {
480                 u32 count;
481
482                 hp_elog->id_type = PSERIES_HP_ELOG_ID_DRC_COUNT;
483                 arg += strlen("count ");
484                 if (kstrtou32(arg, 0, &count)) {
485                         rc = -EINVAL;
486                         pr_err("Invalid count specified: \"%s\"\n", buf);
487                         goto dlpar_store_out;
488                 }
489
490                 hp_elog->_drc_u.drc_count = cpu_to_be32(count);
491         } else {
492                 pr_err("Invalid id_type specified: \"%s\"\n", buf);
493                 rc = -EINVAL;
494                 goto dlpar_store_out;
495         }
496
497         init_completion(&hotplug_done);
498         queue_hotplug_event(hp_elog, &hotplug_done, &rc);
499         wait_for_completion(&hotplug_done);
500
501 dlpar_store_out:
502         kfree(hp_elog);
503         return rc ? rc : count;
504 }
505
506 static CLASS_ATTR(dlpar, S_IWUSR, NULL, dlpar_store);
507
508 static int __init pseries_dlpar_init(void)
509 {
510         pseries_hp_wq = alloc_workqueue("pseries hotplug workqueue",
511                                         WQ_UNBOUND, 1);
512         return sysfs_create_file(kernel_kobj, &class_attr_dlpar.attr);
513 }
514 machine_device_initcall(pseries, pseries_dlpar_init);
515