powerpc/papr_scm: Fetch nvdimm health information from PHYP
[linux-2.6-microblaze.git] / arch / powerpc / platforms / pseries / papr_scm.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #define pr_fmt(fmt)     "papr-scm: " fmt
4
5 #include <linux/of.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/ioport.h>
9 #include <linux/slab.h>
10 #include <linux/ndctl.h>
11 #include <linux/sched.h>
12 #include <linux/libnvdimm.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
15 #include <linux/seq_buf.h>
16
17 #include <asm/plpar_wrappers.h>
18
19 #define BIND_ANY_ADDR (~0ul)
20
21 #define PAPR_SCM_DIMM_CMD_MASK \
22         ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
23          (1ul << ND_CMD_GET_CONFIG_DATA) | \
24          (1ul << ND_CMD_SET_CONFIG_DATA))
25
26 /* DIMM health bitmap bitmap indicators */
27 /* SCM device is unable to persist memory contents */
28 #define PAPR_PMEM_UNARMED                   (1ULL << (63 - 0))
29 /* SCM device failed to persist memory contents */
30 #define PAPR_PMEM_SHUTDOWN_DIRTY            (1ULL << (63 - 1))
31 /* SCM device contents are persisted from previous IPL */
32 #define PAPR_PMEM_SHUTDOWN_CLEAN            (1ULL << (63 - 2))
33 /* SCM device contents are not persisted from previous IPL */
34 #define PAPR_PMEM_EMPTY                     (1ULL << (63 - 3))
35 /* SCM device memory life remaining is critically low */
36 #define PAPR_PMEM_HEALTH_CRITICAL           (1ULL << (63 - 4))
37 /* SCM device will be garded off next IPL due to failure */
38 #define PAPR_PMEM_HEALTH_FATAL              (1ULL << (63 - 5))
39 /* SCM contents cannot persist due to current platform health status */
40 #define PAPR_PMEM_HEALTH_UNHEALTHY          (1ULL << (63 - 6))
41 /* SCM device is unable to persist memory contents in certain conditions */
42 #define PAPR_PMEM_HEALTH_NON_CRITICAL       (1ULL << (63 - 7))
43 /* SCM device is encrypted */
44 #define PAPR_PMEM_ENCRYPTED                 (1ULL << (63 - 8))
45 /* SCM device has been scrubbed and locked */
46 #define PAPR_PMEM_SCRUBBED_AND_LOCKED       (1ULL << (63 - 9))
47
48 /* Bits status indicators for health bitmap indicating unarmed dimm */
49 #define PAPR_PMEM_UNARMED_MASK (PAPR_PMEM_UNARMED |             \
50                                 PAPR_PMEM_HEALTH_UNHEALTHY)
51
52 /* Bits status indicators for health bitmap indicating unflushed dimm */
53 #define PAPR_PMEM_BAD_SHUTDOWN_MASK (PAPR_PMEM_SHUTDOWN_DIRTY)
54
55 /* Bits status indicators for health bitmap indicating unrestored dimm */
56 #define PAPR_PMEM_BAD_RESTORE_MASK  (PAPR_PMEM_EMPTY)
57
58 /* Bit status indicators for smart event notification */
59 #define PAPR_PMEM_SMART_EVENT_MASK (PAPR_PMEM_HEALTH_CRITICAL | \
60                                     PAPR_PMEM_HEALTH_FATAL |    \
61                                     PAPR_PMEM_HEALTH_UNHEALTHY)
62
63 /* private struct associated with each region */
64 struct papr_scm_priv {
65         struct platform_device *pdev;
66         struct device_node *dn;
67         uint32_t drc_index;
68         uint64_t blocks;
69         uint64_t block_size;
70         int metadata_size;
71         bool is_volatile;
72
73         uint64_t bound_addr;
74
75         struct nvdimm_bus_descriptor bus_desc;
76         struct nvdimm_bus *bus;
77         struct nvdimm *nvdimm;
78         struct resource res;
79         struct nd_region *region;
80         struct nd_interleave_set nd_set;
81
82         /* Protect dimm health data from concurrent read/writes */
83         struct mutex health_mutex;
84
85         /* Last time the health information of the dimm was updated */
86         unsigned long lasthealth_jiffies;
87
88         /* Health information for the dimm */
89         u64 health_bitmap;
90 };
91
92 static int drc_pmem_bind(struct papr_scm_priv *p)
93 {
94         unsigned long ret[PLPAR_HCALL_BUFSIZE];
95         uint64_t saved = 0;
96         uint64_t token;
97         int64_t rc;
98
99         /*
100          * When the hypervisor cannot map all the requested memory in a single
101          * hcall it returns H_BUSY and we call again with the token until
102          * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
103          * leave the system in an undefined state, so we wait.
104          */
105         token = 0;
106
107         do {
108                 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
109                                 p->blocks, BIND_ANY_ADDR, token);
110                 token = ret[0];
111                 if (!saved)
112                         saved = ret[1];
113                 cond_resched();
114         } while (rc == H_BUSY);
115
116         if (rc)
117                 return rc;
118
119         p->bound_addr = saved;
120         dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
121                 p->drc_index, (unsigned long)saved);
122         return rc;
123 }
124
125 static void drc_pmem_unbind(struct papr_scm_priv *p)
126 {
127         unsigned long ret[PLPAR_HCALL_BUFSIZE];
128         uint64_t token = 0;
129         int64_t rc;
130
131         dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
132
133         /* NB: unbind has the same retry requirements as drc_pmem_bind() */
134         do {
135
136                 /* Unbind of all SCM resources associated with drcIndex */
137                 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
138                                  p->drc_index, token);
139                 token = ret[0];
140
141                 /* Check if we are stalled for some time */
142                 if (H_IS_LONG_BUSY(rc)) {
143                         msleep(get_longbusy_msecs(rc));
144                         rc = H_BUSY;
145                 } else if (rc == H_BUSY) {
146                         cond_resched();
147                 }
148
149         } while (rc == H_BUSY);
150
151         if (rc)
152                 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
153         else
154                 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
155                         p->drc_index);
156
157         return;
158 }
159
160 static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
161 {
162         unsigned long start_addr;
163         unsigned long end_addr;
164         unsigned long ret[PLPAR_HCALL_BUFSIZE];
165         int64_t rc;
166
167
168         rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
169                          p->drc_index, 0);
170         if (rc)
171                 goto err_out;
172         start_addr = ret[0];
173
174         /* Make sure the full region is bound. */
175         rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
176                          p->drc_index, p->blocks - 1);
177         if (rc)
178                 goto err_out;
179         end_addr = ret[0];
180
181         if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
182                 goto err_out;
183
184         p->bound_addr = start_addr;
185         dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
186         return rc;
187
188 err_out:
189         dev_info(&p->pdev->dev,
190                  "Failed to query, trying an unbind followed by bind");
191         drc_pmem_unbind(p);
192         return drc_pmem_bind(p);
193 }
194
195 /*
196  * Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
197  * health information.
198  */
199 static int __drc_pmem_query_health(struct papr_scm_priv *p)
200 {
201         unsigned long ret[PLPAR_HCALL_BUFSIZE];
202         long rc;
203
204         /* issue the hcall */
205         rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);
206         if (rc != H_SUCCESS) {
207                 dev_err(&p->pdev->dev,
208                         "Failed to query health information, Err:%ld\n", rc);
209                 return -ENXIO;
210         }
211
212         p->lasthealth_jiffies = jiffies;
213         p->health_bitmap = ret[0] & ret[1];
214
215         dev_dbg(&p->pdev->dev,
216                 "Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",
217                 ret[0], ret[1]);
218
219         return 0;
220 }
221
222 /* Min interval in seconds for assuming stable dimm health */
223 #define MIN_HEALTH_QUERY_INTERVAL 60
224
225 /* Query cached health info and if needed call drc_pmem_query_health */
226 static int drc_pmem_query_health(struct papr_scm_priv *p)
227 {
228         unsigned long cache_timeout;
229         int rc;
230
231         /* Protect concurrent modifications to papr_scm_priv */
232         rc = mutex_lock_interruptible(&p->health_mutex);
233         if (rc)
234                 return rc;
235
236         /* Jiffies offset for which the health data is assumed to be same */
237         cache_timeout = p->lasthealth_jiffies +
238                 msecs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL * 1000);
239
240         /* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */
241         if (time_after(jiffies, cache_timeout))
242                 rc = __drc_pmem_query_health(p);
243         else
244                 /* Assume cached health data is valid */
245                 rc = 0;
246
247         mutex_unlock(&p->health_mutex);
248         return rc;
249 }
250
251 static int papr_scm_meta_get(struct papr_scm_priv *p,
252                              struct nd_cmd_get_config_data_hdr *hdr)
253 {
254         unsigned long data[PLPAR_HCALL_BUFSIZE];
255         unsigned long offset, data_offset;
256         int len, read;
257         int64_t ret;
258
259         if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
260                 return -EINVAL;
261
262         for (len = hdr->in_length; len; len -= read) {
263
264                 data_offset = hdr->in_length - len;
265                 offset = hdr->in_offset + data_offset;
266
267                 if (len >= 8)
268                         read = 8;
269                 else if (len >= 4)
270                         read = 4;
271                 else if (len >= 2)
272                         read = 2;
273                 else
274                         read = 1;
275
276                 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
277                                   offset, read);
278
279                 if (ret == H_PARAMETER) /* bad DRC index */
280                         return -ENODEV;
281                 if (ret)
282                         return -EINVAL; /* other invalid parameter */
283
284                 switch (read) {
285                 case 8:
286                         *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
287                         break;
288                 case 4:
289                         *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
290                         break;
291
292                 case 2:
293                         *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
294                         break;
295
296                 case 1:
297                         *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
298                         break;
299                 }
300         }
301         return 0;
302 }
303
304 static int papr_scm_meta_set(struct papr_scm_priv *p,
305                              struct nd_cmd_set_config_hdr *hdr)
306 {
307         unsigned long offset, data_offset;
308         int len, wrote;
309         unsigned long data;
310         __be64 data_be;
311         int64_t ret;
312
313         if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
314                 return -EINVAL;
315
316         for (len = hdr->in_length; len; len -= wrote) {
317
318                 data_offset = hdr->in_length - len;
319                 offset = hdr->in_offset + data_offset;
320
321                 if (len >= 8) {
322                         data = *(uint64_t *)(hdr->in_buf + data_offset);
323                         data_be = cpu_to_be64(data);
324                         wrote = 8;
325                 } else if (len >= 4) {
326                         data = *(uint32_t *)(hdr->in_buf + data_offset);
327                         data &= 0xffffffff;
328                         data_be = cpu_to_be32(data);
329                         wrote = 4;
330                 } else if (len >= 2) {
331                         data = *(uint16_t *)(hdr->in_buf + data_offset);
332                         data &= 0xffff;
333                         data_be = cpu_to_be16(data);
334                         wrote = 2;
335                 } else {
336                         data_be = *(uint8_t *)(hdr->in_buf + data_offset);
337                         data_be &= 0xff;
338                         wrote = 1;
339                 }
340
341                 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
342                                          offset, data_be, wrote);
343                 if (ret == H_PARAMETER) /* bad DRC index */
344                         return -ENODEV;
345                 if (ret)
346                         return -EINVAL; /* other invalid parameter */
347         }
348
349         return 0;
350 }
351
352 static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,
353                           struct nvdimm *nvdimm, unsigned int cmd, void *buf,
354                           unsigned int buf_len, int *cmd_rc)
355 {
356         struct nd_cmd_get_config_size *get_size_hdr;
357         struct papr_scm_priv *p;
358
359         /* Only dimm-specific calls are supported atm */
360         if (!nvdimm)
361                 return -EINVAL;
362
363         p = nvdimm_provider_data(nvdimm);
364
365         switch (cmd) {
366         case ND_CMD_GET_CONFIG_SIZE:
367                 get_size_hdr = buf;
368
369                 get_size_hdr->status = 0;
370                 get_size_hdr->max_xfer = 8;
371                 get_size_hdr->config_size = p->metadata_size;
372                 *cmd_rc = 0;
373                 break;
374
375         case ND_CMD_GET_CONFIG_DATA:
376                 *cmd_rc = papr_scm_meta_get(p, buf);
377                 break;
378
379         case ND_CMD_SET_CONFIG_DATA:
380                 *cmd_rc = papr_scm_meta_set(p, buf);
381                 break;
382
383         default:
384                 return -EINVAL;
385         }
386
387         dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
388
389         return 0;
390 }
391
392 static ssize_t flags_show(struct device *dev,
393                           struct device_attribute *attr, char *buf)
394 {
395         struct nvdimm *dimm = to_nvdimm(dev);
396         struct papr_scm_priv *p = nvdimm_provider_data(dimm);
397         struct seq_buf s;
398         u64 health;
399         int rc;
400
401         rc = drc_pmem_query_health(p);
402         if (rc)
403                 return rc;
404
405         /* Copy health_bitmap locally, check masks & update out buffer */
406         health = READ_ONCE(p->health_bitmap);
407
408         seq_buf_init(&s, buf, PAGE_SIZE);
409         if (health & PAPR_PMEM_UNARMED_MASK)
410                 seq_buf_printf(&s, "not_armed ");
411
412         if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)
413                 seq_buf_printf(&s, "flush_fail ");
414
415         if (health & PAPR_PMEM_BAD_RESTORE_MASK)
416                 seq_buf_printf(&s, "restore_fail ");
417
418         if (health & PAPR_PMEM_ENCRYPTED)
419                 seq_buf_printf(&s, "encrypted ");
420
421         if (health & PAPR_PMEM_SMART_EVENT_MASK)
422                 seq_buf_printf(&s, "smart_notify ");
423
424         if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)
425                 seq_buf_printf(&s, "scrubbed locked ");
426
427         if (seq_buf_used(&s))
428                 seq_buf_printf(&s, "\n");
429
430         return seq_buf_used(&s);
431 }
432 DEVICE_ATTR_RO(flags);
433
434 /* papr_scm specific dimm attributes */
435 static struct attribute *papr_nd_attributes[] = {
436         &dev_attr_flags.attr,
437         NULL,
438 };
439
440 static struct attribute_group papr_nd_attribute_group = {
441         .name = "papr",
442         .attrs = papr_nd_attributes,
443 };
444
445 static const struct attribute_group *papr_nd_attr_groups[] = {
446         &papr_nd_attribute_group,
447         NULL,
448 };
449
450 static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
451 {
452         struct device *dev = &p->pdev->dev;
453         struct nd_mapping_desc mapping;
454         struct nd_region_desc ndr_desc;
455         unsigned long dimm_flags;
456         int target_nid, online_nid;
457
458         p->bus_desc.ndctl = papr_scm_ndctl;
459         p->bus_desc.module = THIS_MODULE;
460         p->bus_desc.of_node = p->pdev->dev.of_node;
461         p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
462
463         if (!p->bus_desc.provider_name)
464                 return -ENOMEM;
465
466         p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
467         if (!p->bus) {
468                 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
469                 kfree(p->bus_desc.provider_name);
470                 return -ENXIO;
471         }
472
473         dimm_flags = 0;
474         set_bit(NDD_LABELING, &dimm_flags);
475
476         p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,
477                                   dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
478         if (!p->nvdimm) {
479                 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
480                 goto err;
481         }
482
483         if (nvdimm_bus_check_dimm_count(p->bus, 1))
484                 goto err;
485
486         /* now add the region */
487
488         memset(&mapping, 0, sizeof(mapping));
489         mapping.nvdimm = p->nvdimm;
490         mapping.start = 0;
491         mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
492
493         memset(&ndr_desc, 0, sizeof(ndr_desc));
494         target_nid = dev_to_node(&p->pdev->dev);
495         online_nid = numa_map_to_online_node(target_nid);
496         ndr_desc.numa_node = online_nid;
497         ndr_desc.target_node = target_nid;
498         ndr_desc.res = &p->res;
499         ndr_desc.of_node = p->dn;
500         ndr_desc.provider_data = p;
501         ndr_desc.mapping = &mapping;
502         ndr_desc.num_mappings = 1;
503         ndr_desc.nd_set = &p->nd_set;
504
505         if (p->is_volatile)
506                 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
507         else {
508                 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
509                 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
510         }
511         if (!p->region) {
512                 dev_err(dev, "Error registering region %pR from %pOF\n",
513                                 ndr_desc.res, p->dn);
514                 goto err;
515         }
516         if (target_nid != online_nid)
517                 dev_info(dev, "Region registered with target node %d and online node %d",
518                          target_nid, online_nid);
519
520         return 0;
521
522 err:    nvdimm_bus_unregister(p->bus);
523         kfree(p->bus_desc.provider_name);
524         return -ENXIO;
525 }
526
527 static int papr_scm_probe(struct platform_device *pdev)
528 {
529         struct device_node *dn = pdev->dev.of_node;
530         u32 drc_index, metadata_size;
531         u64 blocks, block_size;
532         struct papr_scm_priv *p;
533         const char *uuid_str;
534         u64 uuid[2];
535         int rc;
536
537         /* check we have all the required DT properties */
538         if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
539                 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
540                 return -ENODEV;
541         }
542
543         if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
544                 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
545                 return -ENODEV;
546         }
547
548         if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
549                 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
550                 return -ENODEV;
551         }
552
553         if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
554                 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
555                 return -ENODEV;
556         }
557
558
559         p = kzalloc(sizeof(*p), GFP_KERNEL);
560         if (!p)
561                 return -ENOMEM;
562
563         /* Initialize the dimm mutex */
564         mutex_init(&p->health_mutex);
565
566         /* optional DT properties */
567         of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
568
569         p->dn = dn;
570         p->drc_index = drc_index;
571         p->block_size = block_size;
572         p->blocks = blocks;
573         p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
574
575         /* We just need to ensure that set cookies are unique across */
576         uuid_parse(uuid_str, (uuid_t *) uuid);
577         /*
578          * cookie1 and cookie2 are not really little endian
579          * we store a little endian representation of the
580          * uuid str so that we can compare this with the label
581          * area cookie irrespective of the endian config with which
582          * the kernel is built.
583          */
584         p->nd_set.cookie1 = cpu_to_le64(uuid[0]);
585         p->nd_set.cookie2 = cpu_to_le64(uuid[1]);
586
587         /* might be zero */
588         p->metadata_size = metadata_size;
589         p->pdev = pdev;
590
591         /* request the hypervisor to bind this region to somewhere in memory */
592         rc = drc_pmem_bind(p);
593
594         /* If phyp says drc memory still bound then force unbound and retry */
595         if (rc == H_OVERLAP)
596                 rc = drc_pmem_query_n_bind(p);
597
598         if (rc != H_SUCCESS) {
599                 dev_err(&p->pdev->dev, "bind err: %d\n", rc);
600                 rc = -ENXIO;
601                 goto err;
602         }
603
604         /* setup the resource for the newly bound range */
605         p->res.start = p->bound_addr;
606         p->res.end   = p->bound_addr + p->blocks * p->block_size - 1;
607         p->res.name  = pdev->name;
608         p->res.flags = IORESOURCE_MEM;
609
610         rc = papr_scm_nvdimm_init(p);
611         if (rc)
612                 goto err2;
613
614         platform_set_drvdata(pdev, p);
615
616         return 0;
617
618 err2:   drc_pmem_unbind(p);
619 err:    kfree(p);
620         return rc;
621 }
622
623 static int papr_scm_remove(struct platform_device *pdev)
624 {
625         struct papr_scm_priv *p = platform_get_drvdata(pdev);
626
627         nvdimm_bus_unregister(p->bus);
628         drc_pmem_unbind(p);
629         kfree(p->bus_desc.provider_name);
630         kfree(p);
631
632         return 0;
633 }
634
635 static const struct of_device_id papr_scm_match[] = {
636         { .compatible = "ibm,pmemory" },
637         { },
638 };
639
640 static struct platform_driver papr_scm_driver = {
641         .probe = papr_scm_probe,
642         .remove = papr_scm_remove,
643         .driver = {
644                 .name = "papr_scm",
645                 .of_match_table = papr_scm_match,
646         },
647 };
648
649 module_platform_driver(papr_scm_driver);
650 MODULE_DEVICE_TABLE(of, papr_scm_match);
651 MODULE_LICENSE("GPL");
652 MODULE_AUTHOR("IBM Corporation");