Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/powerpc/platforms/pseries/papr_scm.c
26481 views
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/seq_file.h>
10
#include <linux/slab.h>
11
#include <linux/ndctl.h>
12
#include <linux/sched.h>
13
#include <linux/libnvdimm.h>
14
#include <linux/platform_device.h>
15
#include <linux/delay.h>
16
#include <linux/seq_buf.h>
17
#include <linux/nd.h>
18
19
#include <asm/plpar_wrappers.h>
20
#include <uapi/linux/papr_pdsm.h>
21
#include <linux/papr_scm.h>
22
#include <asm/mce.h>
23
#include <linux/unaligned.h>
24
#include <linux/perf_event.h>
25
26
#define BIND_ANY_ADDR (~0ul)
27
28
#define PAPR_SCM_DIMM_CMD_MASK \
29
((1ul << ND_CMD_GET_CONFIG_SIZE) | \
30
(1ul << ND_CMD_GET_CONFIG_DATA) | \
31
(1ul << ND_CMD_SET_CONFIG_DATA) | \
32
(1ul << ND_CMD_CALL))
33
34
/* Struct holding a single performance metric */
35
struct papr_scm_perf_stat {
36
u8 stat_id[8];
37
__be64 stat_val;
38
} __packed;
39
40
/* Struct exchanged between kernel and PHYP for fetching drc perf stats */
41
struct papr_scm_perf_stats {
42
u8 eye_catcher[8];
43
/* Should be PAPR_SCM_PERF_STATS_VERSION */
44
__be32 stats_version;
45
/* Number of stats following */
46
__be32 num_statistics;
47
/* zero or more performance matrics */
48
struct papr_scm_perf_stat scm_statistic[];
49
} __packed;
50
51
/* private struct associated with each region */
52
struct papr_scm_priv {
53
struct platform_device *pdev;
54
struct device_node *dn;
55
uint32_t drc_index;
56
uint64_t blocks;
57
uint64_t block_size;
58
int metadata_size;
59
bool is_volatile;
60
bool hcall_flush_required;
61
62
uint64_t bound_addr;
63
64
struct nvdimm_bus_descriptor bus_desc;
65
struct nvdimm_bus *bus;
66
struct nvdimm *nvdimm;
67
struct resource res;
68
struct nd_region *region;
69
struct nd_interleave_set nd_set;
70
struct list_head region_list;
71
72
/* Protect dimm health data from concurrent read/writes */
73
struct mutex health_mutex;
74
75
/* Last time the health information of the dimm was updated */
76
unsigned long lasthealth_jiffies;
77
78
/* Health information for the dimm */
79
u64 health_bitmap;
80
81
/* Holds the last known dirty shutdown counter value */
82
u64 dirty_shutdown_counter;
83
84
/* length of the stat buffer as expected by phyp */
85
size_t stat_buffer_len;
86
87
/* The bits which needs to be overridden */
88
u64 health_bitmap_inject_mask;
89
};
90
91
static int papr_scm_pmem_flush(struct nd_region *nd_region,
92
struct bio *bio __maybe_unused)
93
{
94
struct papr_scm_priv *p = nd_region_provider_data(nd_region);
95
unsigned long ret_buf[PLPAR_HCALL_BUFSIZE], token = 0;
96
long rc;
97
98
dev_dbg(&p->pdev->dev, "flush drc 0x%x", p->drc_index);
99
100
do {
101
rc = plpar_hcall(H_SCM_FLUSH, ret_buf, p->drc_index, token);
102
token = ret_buf[0];
103
104
/* Check if we are stalled for some time */
105
if (H_IS_LONG_BUSY(rc)) {
106
msleep(get_longbusy_msecs(rc));
107
rc = H_BUSY;
108
} else if (rc == H_BUSY) {
109
cond_resched();
110
}
111
} while (rc == H_BUSY);
112
113
if (rc) {
114
dev_err(&p->pdev->dev, "flush error: %ld", rc);
115
rc = -EIO;
116
} else {
117
dev_dbg(&p->pdev->dev, "flush drc 0x%x complete", p->drc_index);
118
}
119
120
return rc;
121
}
122
123
static LIST_HEAD(papr_nd_regions);
124
static DEFINE_MUTEX(papr_ndr_lock);
125
126
static int drc_pmem_bind(struct papr_scm_priv *p)
127
{
128
unsigned long ret[PLPAR_HCALL_BUFSIZE];
129
uint64_t saved = 0;
130
uint64_t token;
131
int64_t rc;
132
133
/*
134
* When the hypervisor cannot map all the requested memory in a single
135
* hcall it returns H_BUSY and we call again with the token until
136
* we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS
137
* leave the system in an undefined state, so we wait.
138
*/
139
token = 0;
140
141
do {
142
rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0,
143
p->blocks, BIND_ANY_ADDR, token);
144
token = ret[0];
145
if (!saved)
146
saved = ret[1];
147
cond_resched();
148
} while (rc == H_BUSY);
149
150
if (rc)
151
return rc;
152
153
p->bound_addr = saved;
154
dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n",
155
p->drc_index, (unsigned long)saved);
156
return rc;
157
}
158
159
static void drc_pmem_unbind(struct papr_scm_priv *p)
160
{
161
unsigned long ret[PLPAR_HCALL_BUFSIZE];
162
uint64_t token = 0;
163
int64_t rc;
164
165
dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index);
166
167
/* NB: unbind has the same retry requirements as drc_pmem_bind() */
168
do {
169
170
/* Unbind of all SCM resources associated with drcIndex */
171
rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC,
172
p->drc_index, token);
173
token = ret[0];
174
175
/* Check if we are stalled for some time */
176
if (H_IS_LONG_BUSY(rc)) {
177
msleep(get_longbusy_msecs(rc));
178
rc = H_BUSY;
179
} else if (rc == H_BUSY) {
180
cond_resched();
181
}
182
183
} while (rc == H_BUSY);
184
185
if (rc)
186
dev_err(&p->pdev->dev, "unbind error: %lld\n", rc);
187
else
188
dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n",
189
p->drc_index);
190
191
return;
192
}
193
194
static int drc_pmem_query_n_bind(struct papr_scm_priv *p)
195
{
196
unsigned long start_addr;
197
unsigned long end_addr;
198
unsigned long ret[PLPAR_HCALL_BUFSIZE];
199
int64_t rc;
200
201
202
rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
203
p->drc_index, 0);
204
if (rc)
205
goto err_out;
206
start_addr = ret[0];
207
208
/* Make sure the full region is bound. */
209
rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret,
210
p->drc_index, p->blocks - 1);
211
if (rc)
212
goto err_out;
213
end_addr = ret[0];
214
215
if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size))
216
goto err_out;
217
218
p->bound_addr = start_addr;
219
dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr);
220
return rc;
221
222
err_out:
223
dev_info(&p->pdev->dev,
224
"Failed to query, trying an unbind followed by bind");
225
drc_pmem_unbind(p);
226
return drc_pmem_bind(p);
227
}
228
229
/*
230
* Query the Dimm performance stats from PHYP and copy them (if returned) to
231
* provided struct papr_scm_perf_stats instance 'stats' that can hold atleast
232
* (num_stats + header) bytes.
233
* - If buff_stats == NULL the return value is the size in bytes of the buffer
234
* needed to hold all supported performance-statistics.
235
* - If buff_stats != NULL and num_stats == 0 then we copy all known
236
* performance-statistics to 'buff_stat' and expect to be large enough to
237
* hold them.
238
* - if buff_stats != NULL and num_stats > 0 then copy the requested
239
* performance-statistics to buff_stats.
240
*/
241
static ssize_t drc_pmem_query_stats(struct papr_scm_priv *p,
242
struct papr_scm_perf_stats *buff_stats,
243
unsigned int num_stats)
244
{
245
unsigned long ret[PLPAR_HCALL_BUFSIZE];
246
size_t size;
247
s64 rc;
248
249
/* Setup the out buffer */
250
if (buff_stats) {
251
memcpy(buff_stats->eye_catcher,
252
PAPR_SCM_PERF_STATS_EYECATCHER, 8);
253
buff_stats->stats_version =
254
cpu_to_be32(PAPR_SCM_PERF_STATS_VERSION);
255
buff_stats->num_statistics =
256
cpu_to_be32(num_stats);
257
258
/*
259
* Calculate the buffer size based on num-stats provided
260
* or use the prefetched max buffer length
261
*/
262
if (num_stats)
263
/* Calculate size from the num_stats */
264
size = sizeof(struct papr_scm_perf_stats) +
265
num_stats * sizeof(struct papr_scm_perf_stat);
266
else
267
size = p->stat_buffer_len;
268
} else {
269
/* In case of no out buffer ignore the size */
270
size = 0;
271
}
272
273
/* Do the HCALL asking PHYP for info */
274
rc = plpar_hcall(H_SCM_PERFORMANCE_STATS, ret, p->drc_index,
275
buff_stats ? virt_to_phys(buff_stats) : 0,
276
size);
277
278
/* Check if the error was due to an unknown stat-id */
279
if (rc == H_PARTIAL) {
280
dev_err(&p->pdev->dev,
281
"Unknown performance stats, Err:0x%016lX\n", ret[0]);
282
return -ENOENT;
283
} else if (rc == H_AUTHORITY) {
284
dev_info(&p->pdev->dev,
285
"Permission denied while accessing performance stats");
286
return -EPERM;
287
} else if (rc == H_UNSUPPORTED) {
288
dev_dbg(&p->pdev->dev, "Performance stats unsupported\n");
289
return -EOPNOTSUPP;
290
} else if (rc != H_SUCCESS) {
291
dev_err(&p->pdev->dev,
292
"Failed to query performance stats, Err:%lld\n", rc);
293
return -EIO;
294
295
} else if (!size) {
296
/* Handle case where stat buffer size was requested */
297
dev_dbg(&p->pdev->dev,
298
"Performance stats size %ld\n", ret[0]);
299
return ret[0];
300
}
301
302
/* Successfully fetched the requested stats from phyp */
303
dev_dbg(&p->pdev->dev,
304
"Performance stats returned %d stats\n",
305
be32_to_cpu(buff_stats->num_statistics));
306
return 0;
307
}
308
309
#ifdef CONFIG_PERF_EVENTS
310
#define to_nvdimm_pmu(_pmu) container_of(_pmu, struct nvdimm_pmu, pmu)
311
312
static const char * const nvdimm_events_map[] = {
313
[1] = "CtlResCt",
314
[2] = "CtlResTm",
315
[3] = "PonSecs ",
316
[4] = "MemLife ",
317
[5] = "CritRscU",
318
[6] = "HostLCnt",
319
[7] = "HostSCnt",
320
[8] = "HostSDur",
321
[9] = "HostLDur",
322
[10] = "MedRCnt ",
323
[11] = "MedWCnt ",
324
[12] = "MedRDur ",
325
[13] = "MedWDur ",
326
[14] = "CchRHCnt",
327
[15] = "CchWHCnt",
328
[16] = "FastWCnt",
329
};
330
331
static int papr_scm_pmu_get_value(struct perf_event *event, struct device *dev, u64 *count)
332
{
333
struct papr_scm_perf_stat *stat;
334
struct papr_scm_perf_stats *stats;
335
struct papr_scm_priv *p = dev_get_drvdata(dev);
336
int rc, size;
337
338
/* Invalid eventcode */
339
if (event->attr.config == 0 || event->attr.config >= ARRAY_SIZE(nvdimm_events_map))
340
return -EINVAL;
341
342
/* Allocate request buffer enough to hold single performance stat */
343
size = sizeof(struct papr_scm_perf_stats) +
344
sizeof(struct papr_scm_perf_stat);
345
346
if (!p)
347
return -EINVAL;
348
349
stats = kzalloc(size, GFP_KERNEL);
350
if (!stats)
351
return -ENOMEM;
352
353
stat = &stats->scm_statistic[0];
354
memcpy(&stat->stat_id,
355
nvdimm_events_map[event->attr.config],
356
sizeof(stat->stat_id));
357
stat->stat_val = 0;
358
359
rc = drc_pmem_query_stats(p, stats, 1);
360
if (rc < 0) {
361
kfree(stats);
362
return rc;
363
}
364
365
*count = be64_to_cpu(stat->stat_val);
366
kfree(stats);
367
return 0;
368
}
369
370
static int papr_scm_pmu_event_init(struct perf_event *event)
371
{
372
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
373
struct papr_scm_priv *p;
374
375
if (!nd_pmu)
376
return -EINVAL;
377
378
/* test the event attr type for PMU enumeration */
379
if (event->attr.type != event->pmu->type)
380
return -ENOENT;
381
382
/* it does not support event sampling mode */
383
if (is_sampling_event(event))
384
return -EOPNOTSUPP;
385
386
/* no branch sampling */
387
if (has_branch_stack(event))
388
return -EOPNOTSUPP;
389
390
p = (struct papr_scm_priv *)nd_pmu->dev->driver_data;
391
if (!p)
392
return -EINVAL;
393
394
/* Invalid eventcode */
395
if (event->attr.config == 0 || event->attr.config > 16)
396
return -EINVAL;
397
398
return 0;
399
}
400
401
static int papr_scm_pmu_add(struct perf_event *event, int flags)
402
{
403
u64 count;
404
int rc;
405
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
406
407
if (!nd_pmu)
408
return -EINVAL;
409
410
if (flags & PERF_EF_START) {
411
rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &count);
412
if (rc)
413
return rc;
414
415
local64_set(&event->hw.prev_count, count);
416
}
417
418
return 0;
419
}
420
421
static void papr_scm_pmu_read(struct perf_event *event)
422
{
423
u64 prev, now;
424
int rc;
425
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
426
427
if (!nd_pmu)
428
return;
429
430
rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &now);
431
if (rc)
432
return;
433
434
prev = local64_xchg(&event->hw.prev_count, now);
435
local64_add(now - prev, &event->count);
436
}
437
438
static void papr_scm_pmu_del(struct perf_event *event, int flags)
439
{
440
papr_scm_pmu_read(event);
441
}
442
443
static void papr_scm_pmu_register(struct papr_scm_priv *p)
444
{
445
struct nvdimm_pmu *nd_pmu;
446
int rc, nodeid;
447
448
nd_pmu = kzalloc(sizeof(*nd_pmu), GFP_KERNEL);
449
if (!nd_pmu) {
450
rc = -ENOMEM;
451
goto pmu_err_print;
452
}
453
454
if (!p->stat_buffer_len) {
455
rc = -ENOENT;
456
goto pmu_check_events_err;
457
}
458
459
nd_pmu->pmu.task_ctx_nr = perf_invalid_context;
460
nd_pmu->pmu.name = nvdimm_name(p->nvdimm);
461
nd_pmu->pmu.event_init = papr_scm_pmu_event_init;
462
nd_pmu->pmu.read = papr_scm_pmu_read;
463
nd_pmu->pmu.add = papr_scm_pmu_add;
464
nd_pmu->pmu.del = papr_scm_pmu_del;
465
466
nd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_INTERRUPT |
467
PERF_PMU_CAP_NO_EXCLUDE;
468
469
/*updating the cpumask variable */
470
nodeid = numa_map_to_online_node(dev_to_node(&p->pdev->dev));
471
nd_pmu->arch_cpumask = *cpumask_of_node(nodeid);
472
473
rc = register_nvdimm_pmu(nd_pmu, p->pdev);
474
if (rc)
475
goto pmu_check_events_err;
476
477
/*
478
* Set archdata.priv value to nvdimm_pmu structure, to handle the
479
* unregistering of pmu device.
480
*/
481
p->pdev->archdata.priv = nd_pmu;
482
return;
483
484
pmu_check_events_err:
485
kfree(nd_pmu);
486
pmu_err_print:
487
dev_info(&p->pdev->dev, "nvdimm pmu didn't register rc=%d\n", rc);
488
}
489
490
#else
491
static void papr_scm_pmu_register(struct papr_scm_priv *p) { }
492
#endif
493
494
/*
495
* Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
496
* health information.
497
*/
498
static int __drc_pmem_query_health(struct papr_scm_priv *p)
499
{
500
unsigned long ret[PLPAR_HCALL_BUFSIZE];
501
u64 bitmap = 0;
502
long rc;
503
504
/* issue the hcall */
505
rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index);
506
if (rc == H_SUCCESS)
507
bitmap = ret[0] & ret[1];
508
else if (rc == H_FUNCTION)
509
dev_info_once(&p->pdev->dev,
510
"Hcall H_SCM_HEALTH not implemented, assuming empty health bitmap");
511
else {
512
513
dev_err(&p->pdev->dev,
514
"Failed to query health information, Err:%ld\n", rc);
515
return -ENXIO;
516
}
517
518
p->lasthealth_jiffies = jiffies;
519
/* Allow injecting specific health bits via inject mask. */
520
if (p->health_bitmap_inject_mask)
521
bitmap = (bitmap & ~p->health_bitmap_inject_mask) |
522
p->health_bitmap_inject_mask;
523
WRITE_ONCE(p->health_bitmap, bitmap);
524
dev_dbg(&p->pdev->dev,
525
"Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n",
526
ret[0], ret[1]);
527
528
return 0;
529
}
530
531
/* Min interval in seconds for assuming stable dimm health */
532
#define MIN_HEALTH_QUERY_INTERVAL 60
533
534
/* Query cached health info and if needed call drc_pmem_query_health */
535
static int drc_pmem_query_health(struct papr_scm_priv *p)
536
{
537
unsigned long cache_timeout;
538
int rc;
539
540
/* Protect concurrent modifications to papr_scm_priv */
541
rc = mutex_lock_interruptible(&p->health_mutex);
542
if (rc)
543
return rc;
544
545
/* Jiffies offset for which the health data is assumed to be same */
546
cache_timeout = p->lasthealth_jiffies +
547
secs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL);
548
549
/* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */
550
if (time_after(jiffies, cache_timeout))
551
rc = __drc_pmem_query_health(p);
552
else
553
/* Assume cached health data is valid */
554
rc = 0;
555
556
mutex_unlock(&p->health_mutex);
557
return rc;
558
}
559
560
static int papr_scm_meta_get(struct papr_scm_priv *p,
561
struct nd_cmd_get_config_data_hdr *hdr)
562
{
563
unsigned long data[PLPAR_HCALL_BUFSIZE];
564
unsigned long offset, data_offset;
565
int len, read;
566
int64_t ret;
567
568
if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
569
return -EINVAL;
570
571
for (len = hdr->in_length; len; len -= read) {
572
573
data_offset = hdr->in_length - len;
574
offset = hdr->in_offset + data_offset;
575
576
if (len >= 8)
577
read = 8;
578
else if (len >= 4)
579
read = 4;
580
else if (len >= 2)
581
read = 2;
582
else
583
read = 1;
584
585
ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index,
586
offset, read);
587
588
if (ret == H_PARAMETER) /* bad DRC index */
589
return -ENODEV;
590
if (ret)
591
return -EINVAL; /* other invalid parameter */
592
593
switch (read) {
594
case 8:
595
*(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]);
596
break;
597
case 4:
598
*(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff);
599
break;
600
601
case 2:
602
*(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff);
603
break;
604
605
case 1:
606
*(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff);
607
break;
608
}
609
}
610
return 0;
611
}
612
613
static int papr_scm_meta_set(struct papr_scm_priv *p,
614
struct nd_cmd_set_config_hdr *hdr)
615
{
616
unsigned long offset, data_offset;
617
int len, wrote;
618
unsigned long data;
619
__be64 data_be;
620
int64_t ret;
621
622
if ((hdr->in_offset + hdr->in_length) > p->metadata_size)
623
return -EINVAL;
624
625
for (len = hdr->in_length; len; len -= wrote) {
626
627
data_offset = hdr->in_length - len;
628
offset = hdr->in_offset + data_offset;
629
630
if (len >= 8) {
631
data = *(uint64_t *)(hdr->in_buf + data_offset);
632
data_be = cpu_to_be64(data);
633
wrote = 8;
634
} else if (len >= 4) {
635
data = *(uint32_t *)(hdr->in_buf + data_offset);
636
data &= 0xffffffff;
637
data_be = cpu_to_be32(data);
638
wrote = 4;
639
} else if (len >= 2) {
640
data = *(uint16_t *)(hdr->in_buf + data_offset);
641
data &= 0xffff;
642
data_be = cpu_to_be16(data);
643
wrote = 2;
644
} else {
645
data_be = *(uint8_t *)(hdr->in_buf + data_offset);
646
data_be &= 0xff;
647
wrote = 1;
648
}
649
650
ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index,
651
offset, data_be, wrote);
652
if (ret == H_PARAMETER) /* bad DRC index */
653
return -ENODEV;
654
if (ret)
655
return -EINVAL; /* other invalid parameter */
656
}
657
658
return 0;
659
}
660
661
/*
662
* Do a sanity checks on the inputs args to dimm-control function and return
663
* '0' if valid. Validation of PDSM payloads happens later in
664
* papr_scm_service_pdsm.
665
*/
666
static int is_cmd_valid(struct nvdimm *nvdimm, unsigned int cmd, void *buf,
667
unsigned int buf_len)
668
{
669
unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK;
670
struct nd_cmd_pkg *nd_cmd;
671
struct papr_scm_priv *p;
672
enum papr_pdsm pdsm;
673
674
/* Only dimm-specific calls are supported atm */
675
if (!nvdimm)
676
return -EINVAL;
677
678
/* get the provider data from struct nvdimm */
679
p = nvdimm_provider_data(nvdimm);
680
681
if (!test_bit(cmd, &cmd_mask)) {
682
dev_dbg(&p->pdev->dev, "Unsupported cmd=%u\n", cmd);
683
return -EINVAL;
684
}
685
686
/* For CMD_CALL verify pdsm request */
687
if (cmd == ND_CMD_CALL) {
688
/* Verify the envelope and envelop size */
689
if (!buf ||
690
buf_len < (sizeof(struct nd_cmd_pkg) + ND_PDSM_HDR_SIZE)) {
691
dev_dbg(&p->pdev->dev, "Invalid pkg size=%u\n",
692
buf_len);
693
return -EINVAL;
694
}
695
696
/* Verify that the nd_cmd_pkg.nd_family is correct */
697
nd_cmd = (struct nd_cmd_pkg *)buf;
698
699
if (nd_cmd->nd_family != NVDIMM_FAMILY_PAPR) {
700
dev_dbg(&p->pdev->dev, "Invalid pkg family=0x%llx\n",
701
nd_cmd->nd_family);
702
return -EINVAL;
703
}
704
705
pdsm = (enum papr_pdsm)nd_cmd->nd_command;
706
707
/* Verify if the pdsm command is valid */
708
if (pdsm <= PAPR_PDSM_MIN || pdsm >= PAPR_PDSM_MAX) {
709
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid PDSM\n",
710
pdsm);
711
return -EINVAL;
712
}
713
714
/* Have enough space to hold returned 'nd_pkg_pdsm' header */
715
if (nd_cmd->nd_size_out < ND_PDSM_HDR_SIZE) {
716
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid payload\n",
717
pdsm);
718
return -EINVAL;
719
}
720
}
721
722
/* Let the command be further processed */
723
return 0;
724
}
725
726
static int papr_pdsm_fuel_gauge(struct papr_scm_priv *p,
727
union nd_pdsm_payload *payload)
728
{
729
int rc, size;
730
u64 statval;
731
struct papr_scm_perf_stat *stat;
732
struct papr_scm_perf_stats *stats;
733
734
/* Silently fail if fetching performance metrics isn't supported */
735
if (!p->stat_buffer_len)
736
return 0;
737
738
/* Allocate request buffer enough to hold single performance stat */
739
size = sizeof(struct papr_scm_perf_stats) +
740
sizeof(struct papr_scm_perf_stat);
741
742
stats = kzalloc(size, GFP_KERNEL);
743
if (!stats)
744
return -ENOMEM;
745
746
stat = &stats->scm_statistic[0];
747
memcpy(&stat->stat_id, "MemLife ", sizeof(stat->stat_id));
748
stat->stat_val = 0;
749
750
/* Fetch the fuel gauge and populate it in payload */
751
rc = drc_pmem_query_stats(p, stats, 1);
752
if (rc < 0) {
753
dev_dbg(&p->pdev->dev, "Err(%d) fetching fuel gauge\n", rc);
754
goto free_stats;
755
}
756
757
statval = be64_to_cpu(stat->stat_val);
758
dev_dbg(&p->pdev->dev,
759
"Fetched fuel-gauge %llu", statval);
760
payload->health.extension_flags |=
761
PDSM_DIMM_HEALTH_RUN_GAUGE_VALID;
762
payload->health.dimm_fuel_gauge = statval;
763
764
rc = sizeof(struct nd_papr_pdsm_health);
765
766
free_stats:
767
kfree(stats);
768
return rc;
769
}
770
771
/* Add the dirty-shutdown-counter value to the pdsm */
772
static int papr_pdsm_dsc(struct papr_scm_priv *p,
773
union nd_pdsm_payload *payload)
774
{
775
payload->health.extension_flags |= PDSM_DIMM_DSC_VALID;
776
payload->health.dimm_dsc = p->dirty_shutdown_counter;
777
778
return sizeof(struct nd_papr_pdsm_health);
779
}
780
781
/* Fetch the DIMM health info and populate it in provided package. */
782
static int papr_pdsm_health(struct papr_scm_priv *p,
783
union nd_pdsm_payload *payload)
784
{
785
int rc;
786
787
/* Ensure dimm health mutex is taken preventing concurrent access */
788
rc = mutex_lock_interruptible(&p->health_mutex);
789
if (rc)
790
goto out;
791
792
/* Always fetch upto date dimm health data ignoring cached values */
793
rc = __drc_pmem_query_health(p);
794
if (rc) {
795
mutex_unlock(&p->health_mutex);
796
goto out;
797
}
798
799
/* update health struct with various flags derived from health bitmap */
800
payload->health = (struct nd_papr_pdsm_health) {
801
.extension_flags = 0,
802
.dimm_unarmed = !!(p->health_bitmap & PAPR_PMEM_UNARMED_MASK),
803
.dimm_bad_shutdown = !!(p->health_bitmap & PAPR_PMEM_BAD_SHUTDOWN_MASK),
804
.dimm_bad_restore = !!(p->health_bitmap & PAPR_PMEM_BAD_RESTORE_MASK),
805
.dimm_scrubbed = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
806
.dimm_locked = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED),
807
.dimm_encrypted = !!(p->health_bitmap & PAPR_PMEM_ENCRYPTED),
808
.dimm_health = PAPR_PDSM_DIMM_HEALTHY,
809
};
810
811
/* Update field dimm_health based on health_bitmap flags */
812
if (p->health_bitmap & PAPR_PMEM_HEALTH_FATAL)
813
payload->health.dimm_health = PAPR_PDSM_DIMM_FATAL;
814
else if (p->health_bitmap & PAPR_PMEM_HEALTH_CRITICAL)
815
payload->health.dimm_health = PAPR_PDSM_DIMM_CRITICAL;
816
else if (p->health_bitmap & PAPR_PMEM_HEALTH_UNHEALTHY)
817
payload->health.dimm_health = PAPR_PDSM_DIMM_UNHEALTHY;
818
819
/* struct populated hence can release the mutex now */
820
mutex_unlock(&p->health_mutex);
821
822
/* Populate the fuel gauge meter in the payload */
823
papr_pdsm_fuel_gauge(p, payload);
824
/* Populate the dirty-shutdown-counter field */
825
papr_pdsm_dsc(p, payload);
826
827
rc = sizeof(struct nd_papr_pdsm_health);
828
829
out:
830
return rc;
831
}
832
833
/* Inject a smart error Add the dirty-shutdown-counter value to the pdsm */
834
static int papr_pdsm_smart_inject(struct papr_scm_priv *p,
835
union nd_pdsm_payload *payload)
836
{
837
int rc;
838
u32 supported_flags = 0;
839
u64 inject_mask = 0, clear_mask = 0;
840
u64 mask;
841
842
/* Check for individual smart error flags and update inject/clear masks */
843
if (payload->smart_inject.flags & PDSM_SMART_INJECT_HEALTH_FATAL) {
844
supported_flags |= PDSM_SMART_INJECT_HEALTH_FATAL;
845
if (payload->smart_inject.fatal_enable)
846
inject_mask |= PAPR_PMEM_HEALTH_FATAL;
847
else
848
clear_mask |= PAPR_PMEM_HEALTH_FATAL;
849
}
850
851
if (payload->smart_inject.flags & PDSM_SMART_INJECT_BAD_SHUTDOWN) {
852
supported_flags |= PDSM_SMART_INJECT_BAD_SHUTDOWN;
853
if (payload->smart_inject.unsafe_shutdown_enable)
854
inject_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;
855
else
856
clear_mask |= PAPR_PMEM_SHUTDOWN_DIRTY;
857
}
858
859
dev_dbg(&p->pdev->dev, "[Smart-inject] inject_mask=%#llx clear_mask=%#llx\n",
860
inject_mask, clear_mask);
861
862
/* Prevent concurrent access to dimm health bitmap related members */
863
rc = mutex_lock_interruptible(&p->health_mutex);
864
if (rc)
865
return rc;
866
867
/* Use inject/clear masks to set health_bitmap_inject_mask */
868
mask = READ_ONCE(p->health_bitmap_inject_mask);
869
mask = (mask & ~clear_mask) | inject_mask;
870
WRITE_ONCE(p->health_bitmap_inject_mask, mask);
871
872
/* Invalidate cached health bitmap */
873
p->lasthealth_jiffies = 0;
874
875
mutex_unlock(&p->health_mutex);
876
877
/* Return the supported flags back to userspace */
878
payload->smart_inject.flags = supported_flags;
879
880
return sizeof(struct nd_papr_pdsm_health);
881
}
882
883
/*
884
* 'struct pdsm_cmd_desc'
885
* Identifies supported PDSMs' expected length of in/out payloads
886
* and pdsm service function.
887
*
888
* size_in : Size of input payload if any in the PDSM request.
889
* size_out : Size of output payload if any in the PDSM request.
890
* service : Service function for the PDSM request. Return semantics:
891
* rc < 0 : Error servicing PDSM and rc indicates the error.
892
* rc >=0 : Serviced successfully and 'rc' indicate number of
893
* bytes written to payload.
894
*/
895
struct pdsm_cmd_desc {
896
u32 size_in;
897
u32 size_out;
898
int (*service)(struct papr_scm_priv *dimm,
899
union nd_pdsm_payload *payload);
900
};
901
902
/* Holds all supported PDSMs' command descriptors */
903
static const struct pdsm_cmd_desc __pdsm_cmd_descriptors[] = {
904
[PAPR_PDSM_MIN] = {
905
.size_in = 0,
906
.size_out = 0,
907
.service = NULL,
908
},
909
/* New PDSM command descriptors to be added below */
910
911
[PAPR_PDSM_HEALTH] = {
912
.size_in = 0,
913
.size_out = sizeof(struct nd_papr_pdsm_health),
914
.service = papr_pdsm_health,
915
},
916
917
[PAPR_PDSM_SMART_INJECT] = {
918
.size_in = sizeof(struct nd_papr_pdsm_smart_inject),
919
.size_out = sizeof(struct nd_papr_pdsm_smart_inject),
920
.service = papr_pdsm_smart_inject,
921
},
922
/* Empty */
923
[PAPR_PDSM_MAX] = {
924
.size_in = 0,
925
.size_out = 0,
926
.service = NULL,
927
},
928
};
929
930
/* Given a valid pdsm cmd return its command descriptor else return NULL */
931
static inline const struct pdsm_cmd_desc *pdsm_cmd_desc(enum papr_pdsm cmd)
932
{
933
if (cmd >= 0 || cmd < ARRAY_SIZE(__pdsm_cmd_descriptors))
934
return &__pdsm_cmd_descriptors[cmd];
935
936
return NULL;
937
}
938
939
/*
940
* For a given pdsm request call an appropriate service function.
941
* Returns errors if any while handling the pdsm command package.
942
*/
943
static int papr_scm_service_pdsm(struct papr_scm_priv *p,
944
struct nd_cmd_pkg *pkg)
945
{
946
/* Get the PDSM header and PDSM command */
947
struct nd_pkg_pdsm *pdsm_pkg = (struct nd_pkg_pdsm *)pkg->nd_payload;
948
enum papr_pdsm pdsm = (enum papr_pdsm)pkg->nd_command;
949
const struct pdsm_cmd_desc *pdsc;
950
int rc;
951
952
/* Fetch corresponding pdsm descriptor for validation and servicing */
953
pdsc = pdsm_cmd_desc(pdsm);
954
955
/* Validate pdsm descriptor */
956
/* Ensure that reserved fields are 0 */
957
if (pdsm_pkg->reserved[0] || pdsm_pkg->reserved[1]) {
958
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid reserved field\n",
959
pdsm);
960
return -EINVAL;
961
}
962
963
/* If pdsm expects some input, then ensure that the size_in matches */
964
if (pdsc->size_in &&
965
pkg->nd_size_in != (pdsc->size_in + ND_PDSM_HDR_SIZE)) {
966
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_in=%d\n",
967
pdsm, pkg->nd_size_in);
968
return -EINVAL;
969
}
970
971
/* If pdsm wants to return data, then ensure that size_out matches */
972
if (pdsc->size_out &&
973
pkg->nd_size_out != (pdsc->size_out + ND_PDSM_HDR_SIZE)) {
974
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_out=%d\n",
975
pdsm, pkg->nd_size_out);
976
return -EINVAL;
977
}
978
979
/* Service the pdsm */
980
if (pdsc->service) {
981
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Servicing..\n", pdsm);
982
983
rc = pdsc->service(p, &pdsm_pkg->payload);
984
985
if (rc < 0) {
986
/* error encountered while servicing pdsm */
987
pdsm_pkg->cmd_status = rc;
988
pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
989
} else {
990
/* pdsm serviced and 'rc' bytes written to payload */
991
pdsm_pkg->cmd_status = 0;
992
pkg->nd_fw_size = ND_PDSM_HDR_SIZE + rc;
993
}
994
} else {
995
dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Unsupported PDSM request\n",
996
pdsm);
997
pdsm_pkg->cmd_status = -ENOENT;
998
pkg->nd_fw_size = ND_PDSM_HDR_SIZE;
999
}
1000
1001
return pdsm_pkg->cmd_status;
1002
}
1003
1004
static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc,
1005
struct nvdimm *nvdimm, unsigned int cmd, void *buf,
1006
unsigned int buf_len, int *cmd_rc)
1007
{
1008
struct nd_cmd_get_config_size *get_size_hdr;
1009
struct nd_cmd_pkg *call_pkg = NULL;
1010
struct papr_scm_priv *p;
1011
int rc;
1012
1013
rc = is_cmd_valid(nvdimm, cmd, buf, buf_len);
1014
if (rc) {
1015
pr_debug("Invalid cmd=0x%x. Err=%d\n", cmd, rc);
1016
return rc;
1017
}
1018
1019
/* Use a local variable in case cmd_rc pointer is NULL */
1020
if (!cmd_rc)
1021
cmd_rc = &rc;
1022
1023
p = nvdimm_provider_data(nvdimm);
1024
1025
switch (cmd) {
1026
case ND_CMD_GET_CONFIG_SIZE:
1027
get_size_hdr = buf;
1028
1029
get_size_hdr->status = 0;
1030
get_size_hdr->max_xfer = 8;
1031
get_size_hdr->config_size = p->metadata_size;
1032
*cmd_rc = 0;
1033
break;
1034
1035
case ND_CMD_GET_CONFIG_DATA:
1036
*cmd_rc = papr_scm_meta_get(p, buf);
1037
break;
1038
1039
case ND_CMD_SET_CONFIG_DATA:
1040
*cmd_rc = papr_scm_meta_set(p, buf);
1041
break;
1042
1043
case ND_CMD_CALL:
1044
call_pkg = (struct nd_cmd_pkg *)buf;
1045
*cmd_rc = papr_scm_service_pdsm(p, call_pkg);
1046
break;
1047
1048
default:
1049
dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd);
1050
return -EINVAL;
1051
}
1052
1053
dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc);
1054
1055
return 0;
1056
}
1057
1058
static ssize_t health_bitmap_inject_show(struct device *dev,
1059
struct device_attribute *attr,
1060
char *buf)
1061
{
1062
struct nvdimm *dimm = to_nvdimm(dev);
1063
struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1064
1065
return sprintf(buf, "%#llx\n",
1066
READ_ONCE(p->health_bitmap_inject_mask));
1067
}
1068
1069
static DEVICE_ATTR_ADMIN_RO(health_bitmap_inject);
1070
1071
static ssize_t perf_stats_show(struct device *dev,
1072
struct device_attribute *attr, char *buf)
1073
{
1074
int index;
1075
ssize_t rc;
1076
struct seq_buf s;
1077
struct papr_scm_perf_stat *stat;
1078
struct papr_scm_perf_stats *stats;
1079
struct nvdimm *dimm = to_nvdimm(dev);
1080
struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1081
1082
if (!p->stat_buffer_len)
1083
return -ENOENT;
1084
1085
/* Allocate the buffer for phyp where stats are written */
1086
stats = kzalloc(p->stat_buffer_len, GFP_KERNEL);
1087
if (!stats)
1088
return -ENOMEM;
1089
1090
/* Ask phyp to return all dimm perf stats */
1091
rc = drc_pmem_query_stats(p, stats, 0);
1092
if (rc)
1093
goto free_stats;
1094
/*
1095
* Go through the returned output buffer and print stats and
1096
* values. Since stat_id is essentially a char string of
1097
* 8 bytes, simply use the string format specifier to print it.
1098
*/
1099
seq_buf_init(&s, buf, PAGE_SIZE);
1100
for (index = 0, stat = stats->scm_statistic;
1101
index < be32_to_cpu(stats->num_statistics);
1102
++index, ++stat) {
1103
seq_buf_printf(&s, "%.8s = 0x%016llX\n",
1104
stat->stat_id,
1105
be64_to_cpu(stat->stat_val));
1106
}
1107
1108
free_stats:
1109
kfree(stats);
1110
return rc ? rc : (ssize_t)seq_buf_used(&s);
1111
}
1112
static DEVICE_ATTR_ADMIN_RO(perf_stats);
1113
1114
static ssize_t flags_show(struct device *dev,
1115
struct device_attribute *attr, char *buf)
1116
{
1117
struct nvdimm *dimm = to_nvdimm(dev);
1118
struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1119
struct seq_buf s;
1120
u64 health;
1121
int rc;
1122
1123
rc = drc_pmem_query_health(p);
1124
if (rc)
1125
return rc;
1126
1127
/* Copy health_bitmap locally, check masks & update out buffer */
1128
health = READ_ONCE(p->health_bitmap);
1129
1130
seq_buf_init(&s, buf, PAGE_SIZE);
1131
if (health & PAPR_PMEM_UNARMED_MASK)
1132
seq_buf_printf(&s, "not_armed ");
1133
1134
if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK)
1135
seq_buf_printf(&s, "flush_fail ");
1136
1137
if (health & PAPR_PMEM_BAD_RESTORE_MASK)
1138
seq_buf_printf(&s, "restore_fail ");
1139
1140
if (health & PAPR_PMEM_ENCRYPTED)
1141
seq_buf_printf(&s, "encrypted ");
1142
1143
if (health & PAPR_PMEM_SMART_EVENT_MASK)
1144
seq_buf_printf(&s, "smart_notify ");
1145
1146
if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED)
1147
seq_buf_printf(&s, "scrubbed locked ");
1148
1149
if (seq_buf_used(&s))
1150
seq_buf_printf(&s, "\n");
1151
1152
return seq_buf_used(&s);
1153
}
1154
DEVICE_ATTR_RO(flags);
1155
1156
static ssize_t dirty_shutdown_show(struct device *dev,
1157
struct device_attribute *attr, char *buf)
1158
{
1159
struct nvdimm *dimm = to_nvdimm(dev);
1160
struct papr_scm_priv *p = nvdimm_provider_data(dimm);
1161
1162
return sysfs_emit(buf, "%llu\n", p->dirty_shutdown_counter);
1163
}
1164
DEVICE_ATTR_RO(dirty_shutdown);
1165
1166
static umode_t papr_nd_attribute_visible(struct kobject *kobj,
1167
struct attribute *attr, int n)
1168
{
1169
struct device *dev = kobj_to_dev(kobj);
1170
struct nvdimm *nvdimm = to_nvdimm(dev);
1171
struct papr_scm_priv *p = nvdimm_provider_data(nvdimm);
1172
1173
/* For if perf-stats not available remove perf_stats sysfs */
1174
if (attr == &dev_attr_perf_stats.attr && p->stat_buffer_len == 0)
1175
return 0;
1176
1177
return attr->mode;
1178
}
1179
1180
/* papr_scm specific dimm attributes */
1181
static struct attribute *papr_nd_attributes[] = {
1182
&dev_attr_flags.attr,
1183
&dev_attr_perf_stats.attr,
1184
&dev_attr_dirty_shutdown.attr,
1185
&dev_attr_health_bitmap_inject.attr,
1186
NULL,
1187
};
1188
1189
static const struct attribute_group papr_nd_attribute_group = {
1190
.name = "papr",
1191
.is_visible = papr_nd_attribute_visible,
1192
.attrs = papr_nd_attributes,
1193
};
1194
1195
static const struct attribute_group *papr_nd_attr_groups[] = {
1196
&papr_nd_attribute_group,
1197
NULL,
1198
};
1199
1200
static int papr_scm_nvdimm_init(struct papr_scm_priv *p)
1201
{
1202
struct device *dev = &p->pdev->dev;
1203
struct nd_mapping_desc mapping;
1204
struct nd_region_desc ndr_desc;
1205
unsigned long dimm_flags;
1206
int target_nid, online_nid;
1207
1208
p->bus_desc.ndctl = papr_scm_ndctl;
1209
p->bus_desc.module = THIS_MODULE;
1210
p->bus_desc.of_node = p->pdev->dev.of_node;
1211
p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL);
1212
1213
/* Set the dimm command family mask to accept PDSMs */
1214
set_bit(NVDIMM_FAMILY_PAPR, &p->bus_desc.dimm_family_mask);
1215
1216
if (!p->bus_desc.provider_name)
1217
return -ENOMEM;
1218
1219
p->bus = nvdimm_bus_register(NULL, &p->bus_desc);
1220
if (!p->bus) {
1221
dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn);
1222
kfree(p->bus_desc.provider_name);
1223
return -ENXIO;
1224
}
1225
1226
dimm_flags = 0;
1227
set_bit(NDD_LABELING, &dimm_flags);
1228
1229
/*
1230
* Check if the nvdimm is unarmed. No locking needed as we are still
1231
* initializing. Ignore error encountered if any.
1232
*/
1233
__drc_pmem_query_health(p);
1234
1235
if (p->health_bitmap & PAPR_PMEM_UNARMED_MASK)
1236
set_bit(NDD_UNARMED, &dimm_flags);
1237
1238
p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups,
1239
dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL);
1240
if (!p->nvdimm) {
1241
dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn);
1242
goto err;
1243
}
1244
1245
if (nvdimm_bus_check_dimm_count(p->bus, 1))
1246
goto err;
1247
1248
/* now add the region */
1249
1250
memset(&mapping, 0, sizeof(mapping));
1251
mapping.nvdimm = p->nvdimm;
1252
mapping.start = 0;
1253
mapping.size = p->blocks * p->block_size; // XXX: potential overflow?
1254
1255
memset(&ndr_desc, 0, sizeof(ndr_desc));
1256
target_nid = dev_to_node(&p->pdev->dev);
1257
online_nid = numa_map_to_online_node(target_nid);
1258
ndr_desc.numa_node = online_nid;
1259
ndr_desc.target_node = target_nid;
1260
ndr_desc.res = &p->res;
1261
ndr_desc.of_node = p->dn;
1262
ndr_desc.provider_data = p;
1263
ndr_desc.mapping = &mapping;
1264
ndr_desc.num_mappings = 1;
1265
ndr_desc.nd_set = &p->nd_set;
1266
1267
if (p->hcall_flush_required) {
1268
set_bit(ND_REGION_ASYNC, &ndr_desc.flags);
1269
ndr_desc.flush = papr_scm_pmem_flush;
1270
}
1271
1272
if (p->is_volatile)
1273
p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc);
1274
else {
1275
set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags);
1276
p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc);
1277
}
1278
if (!p->region) {
1279
dev_err(dev, "Error registering region %pR from %pOF\n",
1280
ndr_desc.res, p->dn);
1281
goto err;
1282
}
1283
if (target_nid != online_nid)
1284
dev_info(dev, "Region registered with target node %d and online node %d",
1285
target_nid, online_nid);
1286
1287
mutex_lock(&papr_ndr_lock);
1288
list_add_tail(&p->region_list, &papr_nd_regions);
1289
mutex_unlock(&papr_ndr_lock);
1290
1291
return 0;
1292
1293
err: nvdimm_bus_unregister(p->bus);
1294
kfree(p->bus_desc.provider_name);
1295
return -ENXIO;
1296
}
1297
1298
static void papr_scm_add_badblock(struct nd_region *region,
1299
struct nvdimm_bus *bus, u64 phys_addr)
1300
{
1301
u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES);
1302
1303
if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) {
1304
pr_err("Bad block registration for 0x%llx failed\n", phys_addr);
1305
return;
1306
}
1307
1308
pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n",
1309
aligned_addr, aligned_addr + L1_CACHE_BYTES);
1310
1311
nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON);
1312
}
1313
1314
static int handle_mce_ue(struct notifier_block *nb, unsigned long val,
1315
void *data)
1316
{
1317
struct machine_check_event *evt = data;
1318
struct papr_scm_priv *p;
1319
u64 phys_addr;
1320
bool found = false;
1321
1322
if (evt->error_type != MCE_ERROR_TYPE_UE)
1323
return NOTIFY_DONE;
1324
1325
if (list_empty(&papr_nd_regions))
1326
return NOTIFY_DONE;
1327
1328
/*
1329
* The physical address obtained here is PAGE_SIZE aligned, so get the
1330
* exact address from the effective address
1331
*/
1332
phys_addr = evt->u.ue_error.physical_address +
1333
(evt->u.ue_error.effective_address & ~PAGE_MASK);
1334
1335
if (!evt->u.ue_error.physical_address_provided ||
1336
!is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT)))
1337
return NOTIFY_DONE;
1338
1339
/* mce notifier is called from a process context, so mutex is safe */
1340
mutex_lock(&papr_ndr_lock);
1341
list_for_each_entry(p, &papr_nd_regions, region_list) {
1342
if (phys_addr >= p->res.start && phys_addr <= p->res.end) {
1343
found = true;
1344
break;
1345
}
1346
}
1347
1348
if (found)
1349
papr_scm_add_badblock(p->region, p->bus, phys_addr);
1350
1351
mutex_unlock(&papr_ndr_lock);
1352
1353
return found ? NOTIFY_OK : NOTIFY_DONE;
1354
}
1355
1356
static struct notifier_block mce_ue_nb = {
1357
.notifier_call = handle_mce_ue
1358
};
1359
1360
static int papr_scm_probe(struct platform_device *pdev)
1361
{
1362
struct device_node *dn = pdev->dev.of_node;
1363
u32 drc_index, metadata_size;
1364
u64 blocks, block_size;
1365
struct papr_scm_priv *p;
1366
u8 uuid_raw[UUID_SIZE];
1367
const char *uuid_str;
1368
ssize_t stat_size;
1369
uuid_t uuid;
1370
int rc;
1371
1372
/* check we have all the required DT properties */
1373
if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) {
1374
dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn);
1375
return -ENODEV;
1376
}
1377
1378
if (of_property_read_u64(dn, "ibm,block-size", &block_size)) {
1379
dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn);
1380
return -ENODEV;
1381
}
1382
1383
if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) {
1384
dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn);
1385
return -ENODEV;
1386
}
1387
1388
if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) {
1389
dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn);
1390
return -ENODEV;
1391
}
1392
1393
/*
1394
* open firmware platform device create won't update the NUMA
1395
* distance table. For PAPR SCM devices we use numa_map_to_online_node()
1396
* to find the nearest online NUMA node and that requires correct
1397
* distance table information.
1398
*/
1399
update_numa_distance(dn);
1400
1401
p = kzalloc(sizeof(*p), GFP_KERNEL);
1402
if (!p)
1403
return -ENOMEM;
1404
1405
/* Initialize the dimm mutex */
1406
mutex_init(&p->health_mutex);
1407
1408
/* optional DT properties */
1409
of_property_read_u32(dn, "ibm,metadata-size", &metadata_size);
1410
1411
p->dn = dn;
1412
p->drc_index = drc_index;
1413
p->block_size = block_size;
1414
p->blocks = blocks;
1415
p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required");
1416
p->hcall_flush_required = of_property_read_bool(dn, "ibm,hcall-flush-required");
1417
1418
if (of_property_read_u64(dn, "ibm,persistence-failed-count",
1419
&p->dirty_shutdown_counter))
1420
p->dirty_shutdown_counter = 0;
1421
1422
/* We just need to ensure that set cookies are unique across */
1423
uuid_parse(uuid_str, &uuid);
1424
1425
/*
1426
* The cookie1 and cookie2 are not really little endian.
1427
* We store a raw buffer representation of the
1428
* uuid string so that we can compare this with the label
1429
* area cookie irrespective of the endian configuration
1430
* with which the kernel is built.
1431
*
1432
* Historically we stored the cookie in the below format.
1433
* for a uuid string 72511b67-0b3b-42fd-8d1d-5be3cae8bcaa
1434
* cookie1 was 0xfd423b0b671b5172
1435
* cookie2 was 0xaabce8cae35b1d8d
1436
*/
1437
export_uuid(uuid_raw, &uuid);
1438
p->nd_set.cookie1 = get_unaligned_le64(&uuid_raw[0]);
1439
p->nd_set.cookie2 = get_unaligned_le64(&uuid_raw[8]);
1440
1441
/* might be zero */
1442
p->metadata_size = metadata_size;
1443
p->pdev = pdev;
1444
1445
/* request the hypervisor to bind this region to somewhere in memory */
1446
rc = drc_pmem_bind(p);
1447
1448
/* If phyp says drc memory still bound then force unbound and retry */
1449
if (rc == H_OVERLAP)
1450
rc = drc_pmem_query_n_bind(p);
1451
1452
if (rc != H_SUCCESS) {
1453
dev_err(&p->pdev->dev, "bind err: %d\n", rc);
1454
rc = -ENXIO;
1455
goto err;
1456
}
1457
1458
/* setup the resource for the newly bound range */
1459
p->res.start = p->bound_addr;
1460
p->res.end = p->bound_addr + p->blocks * p->block_size - 1;
1461
p->res.name = pdev->name;
1462
p->res.flags = IORESOURCE_MEM;
1463
1464
/* Try retrieving the stat buffer and see if its supported */
1465
stat_size = drc_pmem_query_stats(p, NULL, 0);
1466
if (stat_size > 0) {
1467
p->stat_buffer_len = stat_size;
1468
dev_dbg(&p->pdev->dev, "Max perf-stat size %lu-bytes\n",
1469
p->stat_buffer_len);
1470
}
1471
1472
rc = papr_scm_nvdimm_init(p);
1473
if (rc)
1474
goto err2;
1475
1476
platform_set_drvdata(pdev, p);
1477
papr_scm_pmu_register(p);
1478
1479
return 0;
1480
1481
err2: drc_pmem_unbind(p);
1482
err: kfree(p);
1483
return rc;
1484
}
1485
1486
static void papr_scm_remove(struct platform_device *pdev)
1487
{
1488
struct papr_scm_priv *p = platform_get_drvdata(pdev);
1489
1490
mutex_lock(&papr_ndr_lock);
1491
list_del(&p->region_list);
1492
mutex_unlock(&papr_ndr_lock);
1493
1494
nvdimm_bus_unregister(p->bus);
1495
drc_pmem_unbind(p);
1496
1497
if (pdev->archdata.priv)
1498
unregister_nvdimm_pmu(pdev->archdata.priv);
1499
1500
pdev->archdata.priv = NULL;
1501
kfree(p->bus_desc.provider_name);
1502
kfree(p);
1503
}
1504
1505
static const struct of_device_id papr_scm_match[] = {
1506
{ .compatible = "ibm,pmemory" },
1507
{ .compatible = "ibm,pmemory-v2" },
1508
{ },
1509
};
1510
1511
static struct platform_driver papr_scm_driver = {
1512
.probe = papr_scm_probe,
1513
.remove = papr_scm_remove,
1514
.driver = {
1515
.name = "papr_scm",
1516
.of_match_table = papr_scm_match,
1517
},
1518
};
1519
1520
static int __init papr_scm_init(void)
1521
{
1522
int ret;
1523
1524
ret = platform_driver_register(&papr_scm_driver);
1525
if (!ret)
1526
mce_register_notifier(&mce_ue_nb);
1527
1528
return ret;
1529
}
1530
module_init(papr_scm_init);
1531
1532
static void __exit papr_scm_exit(void)
1533
{
1534
mce_unregister_notifier(&mce_ue_nb);
1535
platform_driver_unregister(&papr_scm_driver);
1536
}
1537
module_exit(papr_scm_exit);
1538
1539
MODULE_DEVICE_TABLE(of, papr_scm_match);
1540
MODULE_DESCRIPTION("PAPR Storage Class Memory interface driver");
1541
MODULE_LICENSE("GPL");
1542
MODULE_AUTHOR("IBM Corporation");
1543
1544